Gaim is the legal property of its developers, whose names are too numerous to list here. Please refer to the COPYRIGHT file distributed with this source distribution This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, #include <glib/gstring.h> /* this should become "util.h" if we ever get this into gaim proper */ Utility structure used in both MIME document and parts, which maps field names to their values, and keeps an easily accessible list of struct _GaimMimeDocument { struct mime_fields fields; struct mime_fields fields; struct _GaimMimeDocument *doc; static void fields_set(struct mime_fields *mf, const char *key, const char *val) { g_return_if_fail(mf != NULL); g_return_if_fail(mf->map != NULL); k = g_ascii_strdown(key, -1); /* append to the keys list only if it's not already there */ if(! g_hash_table_lookup(mf->map, k)) { mf->keys = g_list_append(mf->keys, k); /* important to use insert. If the key is already in the table, then it's already in the keys list. Insert will free the new instance of the key rather than the old instance. */ g_hash_table_insert(mf->map, k, v); static const char *fields_get(struct mime_fields *mf, g_return_val_if_fail(mf != NULL, NULL); g_return_val_if_fail(mf->map != NULL, NULL); kdown = g_ascii_strdown(key, -1); ret = g_hash_table_lookup(mf->map, kdown); static void fields_init(struct mime_fields *mf) { g_return_if_fail(mf != NULL); mf->map = g_hash_table_new_full(g_str_hash, g_str_equal, static void fields_loadline(struct mime_fields *mf, const char *line, gsize len) { /* split the line into key: value */ /* feh, need it to be NUL terminated */ key = g_strndup(line, len); /* normalize whitespace (sorta) and trim on key and value */ tokens = g_strsplit(key, "\t\r\n", 0); key = g_strjoinv("", tokens); tokens = g_strsplit(val, "\t\r\n", 0); val = g_strjoinv("", tokens); fields_set(mf, key, val); static void fields_load(struct mime_fields *mf, char **buf, gsize *len) { while( (tail = g_strstr_len(*buf, *len, "\r\n")) ) { /* determine the current line */ /* advance our search space past the CRLF */ /* empty line, end of headers */ /* look out for line continuations */ tail = g_strstr_len(*buf, *len, "\r\n"); /* advance our search space past the CRLF (again) */ /* process our super-cool line */ fields_loadline(mf, line, ln); static void field_write(const char *key, const char *val, GString *str) { g_string_append_printf(str, "%s: %s\r\n", key, val); static void fields_write(struct mime_fields *mf, GString *str) { g_return_if_fail(mf != NULL); g_hash_table_foreach(mf->map, (GHFunc) field_write, str); g_string_append(str, "\r\n"); static void fields_destroy(struct mime_fields *mf) { g_return_if_fail(mf != NULL); g_hash_table_destroy(mf->map); static GaimMimePart *part_new(GaimMimeDocument *doc) { part = g_new0(GaimMimePart, 1); fields_init(&part->fields); part->data = g_string_new(NULL); doc->parts = g_list_prepend(doc->parts, part); static void part_load(GaimMimePart *part, const char *buf, gsize len) { fields_load(&part->fields, &b, &n); /* the remainder will have a blank line, if there's anything at all, so check if there's anything then trim off the trailing four g_string_append_len(part->data, b, n); static void part_write(GaimMimePart *part, GString *str) { fields_write(&part->fields, str); g_string_append_printf(str, "%s\r\n\r\n", part->data->str); static void part_free(GaimMimePart *part) { fields_destroy(&part->fields); g_string_free(part->data, TRUE); GaimMimePart *gaim_mime_part_new(GaimMimeDocument *doc) { g_return_val_if_fail(doc != NULL, NULL); const GList *gaim_mime_part_get_fields(GaimMimePart *part) { g_return_val_if_fail(part != NULL, NULL); return part->fields.keys; const char *gaim_mime_part_get_field(GaimMimePart *part, g_return_val_if_fail(part != NULL, NULL); return fields_get(&part->fields, field); char *gaim_mime_part_get_field_decoded(GaimMimePart *part, g_return_val_if_fail(part != NULL, NULL); f = fields_get(&part->fields, field); return gaim_mime_decode_field(f); void gaim_mime_part_set_field(GaimMimePart *part, g_return_if_fail(part != NULL); fields_set(&part->fields, field, value); const char *gaim_mime_part_get_data(GaimMimePart *part) { g_return_val_if_fail(part != NULL, NULL); g_return_val_if_fail(part->data != NULL, NULL); void gaim_mime_part_get_data_decoded(GaimMimePart *part, guchar **data, gsize *len) { g_return_if_fail(part != NULL); g_return_if_fail(data != NULL); g_return_if_fail(len != NULL); g_return_if_fail(part->data != NULL); enc = gaim_mime_part_get_field(part, "content-transfer-encoding"); *data = (guchar *)g_strdup(part->data->str); } else if(! g_ascii_strcasecmp(enc, "7bit")) { *data = (guchar *)g_strdup(part->data->str); } else if(! g_ascii_strcasecmp(enc, "8bit")) { *data = (guchar *)g_strdup(part->data->str); } else if(! g_ascii_strcasecmp(enc, "base16")) { *data = gaim_base16_decode(part->data->str, len); } else if(! g_ascii_strcasecmp(enc, "base64")) { *data = gaim_base64_decode(part->data->str, len); } else if(! g_ascii_strcasecmp(enc, "quoted-printable")) { *data = gaim_quotedp_decode(part->data->str, len); gaim_debug_warning("mime", "gaim_mime_part_get_data_decoded:" " unknown encoding '%s'\n", enc); gsize gaim_mime_part_get_length(GaimMimePart *part) { g_return_val_if_fail(part != NULL, 0); g_return_val_if_fail(part->data != NULL, 0); void gaim_mime_part_set_data(GaimMimePart *part, const char *data) { g_return_if_fail(part != NULL); g_string_free(part->data, TRUE); part->data = g_string_new(data); GaimMimeDocument *gaim_mime_document_new() { doc = g_new0(GaimMimeDocument, 1); fields_init(&doc->fields); static void doc_parts_load(GaimMimeDocument *doc, const char *buf, gsize len) { bnd = g_strdup_printf("--%s", boundary); for(b = g_strstr_len(b, n, bnd); b; ) { /* skip the trailing \r\n or -- as well */ /* find the next boundary */ tail = g_strstr_len(b, n, bnd); GaimMimePart *part = part_new(doc); GaimMimeDocument *gaim_mime_document_parsen(const char *buf, gsize len) { g_return_val_if_fail(buf != NULL, NULL); doc = gaim_mime_document_new(); fields_load(&doc->fields, &b, &n); const char *ct = fields_get(&doc->fields, "content-type"); if(ct && gaim_str_has_prefix(ct, "multipart")) { char *bd = strrchr(ct, '='); doc_parts_load(doc, bd, b, n); GaimMimeDocument *gaim_mime_document_parse(const char *buf) { g_return_val_if_fail(buf != NULL, NULL); return gaim_mime_document_parsen(buf, strlen(buf)); void gaim_mime_document_write(GaimMimeDocument *doc, GString *str) { g_return_if_fail(doc != NULL); g_return_if_fail(str != NULL); const char *ct = fields_get(&doc->fields, "content-type"); if(ct && gaim_str_has_prefix(ct, "multipart")) { char *b = strrchr(ct, '='); fields_write(&doc->fields, str); for(l = doc->parts; l; l = l->next) { g_string_append_printf(str, "--%s\r\n", bd); part_write(l->data, str); g_string_append_printf(str, "--%s--\r\n", bd); const GList *gaim_mime_document_get_fields(GaimMimeDocument *doc) { g_return_val_if_fail(doc != NULL, NULL); const char *gaim_mime_document_get_field(GaimMimeDocument *doc, g_return_val_if_fail(doc != NULL, NULL); return fields_get(&doc->fields, field); void gaim_mime_document_set_field(GaimMimeDocument *doc, g_return_if_fail(doc != NULL); fields_set(&doc->fields, field, value); const GList *gaim_mime_document_get_parts(GaimMimeDocument *doc) { g_return_val_if_fail(doc != NULL, NULL); void gaim_mime_document_free(GaimMimeDocument *doc) { fields_destroy(&doc->fields); part_free(doc->parts->data); doc->parts = g_list_delete_link(doc->parts, doc->parts);