CamelMimeMessage * test_message_create_simple (void) { CamelMimeMessage *msg; CamelInternetAddress *addr; msg = camel_mime_message_new (); addr = camel_internet_address_new (); camel_internet_address_add (addr, "Michael Zucchi", "*****@*****.**"); camel_mime_message_set_from (msg, addr); camel_address_remove ((CamelAddress *) addr, -1); camel_internet_address_add (addr, "POSTMASTER", "*****@*****.**"); camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_TO, addr); camel_address_remove ((CamelAddress *) addr, -1); camel_internet_address_add (addr, "Michael Zucchi", "*****@*****.**"); camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_CC, addr); check_unref (addr, 1); camel_mime_message_set_subject (msg, "Simple message subject"); camel_mime_message_set_date (msg, time (0), 930); return msg; }
static void address_finalize (GObject *object) { CamelAddress *address = CAMEL_ADDRESS (object); camel_address_remove (address, -1); g_ptr_array_free (address->addresses, TRUE); /* Chain up to parent's finalize() method. */ G_OBJECT_CLASS (camel_address_parent_class)->finalize (object); }
/* Expands groups to individual addresses, or removes empty groups completely. * Usual email addresses are left untouched. */ void em_utils_expand_groups (CamelInternetAddress *addresses) { gint ii, len; const gchar *addr; CamelAddress *addrs; g_return_if_fail (CAMEL_IS_INTERNET_ADDRESS (addresses)); addrs = CAMEL_ADDRESS (addresses); len = camel_address_length (addrs); for (ii = len - 1; ii >= 0; ii--) { addr = NULL; if (!camel_internet_address_get (addresses, ii, NULL, &addr)) { camel_address_remove (addrs, ii); } else if (addr) { gchar *encoded = camel_internet_address_encode_address (NULL, NULL, addr); if (encoded) { CamelInternetAddress *iaddr = camel_internet_address_new (); gint decoded; /* decode expands respective groups */ decoded = camel_address_decode (CAMEL_ADDRESS (iaddr), encoded); if (decoded <= 0 || decoded > 1) { camel_address_remove (addrs, ii); if (decoded > 1) camel_address_cat (addrs, CAMEL_ADDRESS (iaddr)); } g_object_unref (iaddr); g_free (encoded); } } } }
gint main (gint argc, gchar **argv) { gint i; CamelInternetAddress *addr, *addr2; gchar *name; const gchar *charset; const gchar *real, *where; gchar *enc, *enc2, *format, *format2; camel_test_init (argc, argv); camel_test_start ("CamelInternetAddress, basics"); addr = camel_internet_address_new (); push ("Test blank address"); check (camel_address_length (CAMEL_ADDRESS (addr)) == 0); check (camel_internet_address_get (addr, 0, &real, &where) == FALSE); pull (); push ("Test blank clone"); addr2 = CAMEL_INTERNET_ADDRESS (camel_address_new_clone (CAMEL_ADDRESS (addr))); test_address_compare (addr, addr2); check_unref (addr2, 1); pull (); push ("Test add 1"); camel_internet_address_add (addr, "Zed", "*****@*****.**"); check (camel_address_length (CAMEL_ADDRESS (addr)) == 1); check (camel_internet_address_get (addr, 0, &real, &where) == TRUE); check_msg (string_equal ("Zed", real), "real = '%s'", real); check (strcmp (where, "*****@*****.**") == 0); pull (); push ("Test clone 1"); addr2 = CAMEL_INTERNET_ADDRESS (camel_address_new_clone (CAMEL_ADDRESS (addr))); test_address_compare (addr, addr2); check_unref (addr2, 1); pull (); push ("Test add many"); for (i = 1; i < 10; i++) { gchar name[16], a[32]; sprintf (name, "Zed %d", i); sprintf (a, "nowhere@here-%d.com.au", i); camel_internet_address_add (addr, name, a); check (camel_address_length (CAMEL_ADDRESS (addr)) == i + 1); check (camel_internet_address_get (addr, i, &real, &where) == TRUE); check_msg (string_equal (name, real), "name = '%s' real = '%s'", name, real); check (strcmp (where, a) == 0); } pull (); /* put a few of these in to make it look like its doing something impressive ... :) */ camel_test_end (); camel_test_start ("CamelInternetAddress, search"); push ("Test search"); camel_test_nonfatal ("Address comparisons should ignore whitespace??"); check (camel_internet_address_find_name (addr, "Zed 1", &where) == 1); check (camel_internet_address_find_name (addr, "Zed 9", &where) == 9); check (camel_internet_address_find_name (addr, "Zed", &where) == 0); check (camel_internet_address_find_name (addr, " Zed", &where) == 0); check (camel_internet_address_find_name (addr, "Zed ", &where) == 0); check (camel_internet_address_find_name (addr, " Zed ", &where) == 0); check (camel_internet_address_find_name (addr, "Zed 20", &where) == -1); check (camel_internet_address_find_name (addr, "", &where) == -1); /* interface dont handle nulls :) */ /*check(camel_internet_address_find_name(addr, NULL, &where) == -1);*/ check (camel_internet_address_find_address (addr, "*****@*****.**", &where) == 1); check (camel_internet_address_find_address (addr, "nowhere@here-1 . com.au", &where) == 1); check (camel_internet_address_find_address (addr, "nowhere@here-2 .com.au ", &where) == 2); check (camel_internet_address_find_address (addr, " nowhere @here-3.com.au", &where) == 3); check (camel_internet_address_find_address (addr, "[email protected] ", &where) == -1); check (camel_internet_address_find_address (addr, "", &where) == -1); /*check(camel_internet_address_find_address(addr, NULL, &where) == -1);*/ camel_test_fatal (); pull (); camel_test_end (); camel_test_start ("CamelInternetAddress, copy/cat/clone"); push ("Test clone many"); addr2 = CAMEL_INTERNET_ADDRESS (camel_address_new_clone (CAMEL_ADDRESS (addr))); test_address_compare (addr, addr2); pull (); push ("Test remove items"); camel_address_remove (CAMEL_ADDRESS (addr2), 0); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 9); camel_address_remove (CAMEL_ADDRESS (addr2), 0); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 8); camel_address_remove (CAMEL_ADDRESS (addr2), 5); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 7); camel_address_remove (CAMEL_ADDRESS (addr2), 10); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 7); camel_address_remove (CAMEL_ADDRESS (addr2), -1); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 0); check_unref (addr2, 1); pull (); push ("Testing copy/cat"); push ("clone + cat"); addr2 = CAMEL_INTERNET_ADDRESS (camel_address_new_clone (CAMEL_ADDRESS (addr))); camel_address_cat (CAMEL_ADDRESS (addr2), CAMEL_ADDRESS (addr)); check (camel_address_length (CAMEL_ADDRESS (addr)) == 10); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 20); check_unref (addr2, 1); pull (); push ("cat + cat + copy"); addr2 = camel_internet_address_new (); camel_address_cat (CAMEL_ADDRESS (addr2), CAMEL_ADDRESS (addr)); test_address_compare (addr, addr2); camel_address_cat (CAMEL_ADDRESS (addr2), CAMEL_ADDRESS (addr)); check (camel_address_length (CAMEL_ADDRESS (addr)) == 10); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 20); camel_address_copy (CAMEL_ADDRESS (addr2), CAMEL_ADDRESS (addr)); test_address_compare (addr, addr2); check_unref (addr2, 1); pull (); push ("copy"); addr2 = camel_internet_address_new (); camel_address_copy (CAMEL_ADDRESS (addr2), CAMEL_ADDRESS (addr)); test_address_compare (addr, addr2); check_unref (addr2, 1); pull (); pull (); check_unref (addr, 1); camel_test_end (); camel_test_start ("CamelInternetAddress, I18N"); for (i = 0; i < G_N_ELEMENTS (test_lines); i++) { push ("Testing text line %d (%s) '%s'", i, test_lines[i].type, test_lines[i].line); addr = camel_internet_address_new (); /* first, convert to api format (utf-8) */ charset = test_lines[i].type; name = to_utf8 (test_lines[i].line, charset); push ("Address setup"); camel_internet_address_add (addr, name, "*****@*****.**"); check (camel_internet_address_get (addr, 0, &real, &where) == TRUE); check_msg (string_equal (name, real), "name = '%s' real = '%s'", name, real); check (strcmp (where, "*****@*****.**") == 0); test_free (name); check (camel_internet_address_get (addr, 1, &real, &where) == FALSE); check (camel_address_length (CAMEL_ADDRESS (addr)) == 1); pull (); push ("Address encode/decode"); enc = camel_address_encode (CAMEL_ADDRESS (addr)); addr2 = camel_internet_address_new (); check (camel_address_decode (CAMEL_ADDRESS (addr2), enc) == 1); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 1); enc2 = camel_address_encode (CAMEL_ADDRESS (addr2)); check_msg (string_equal (enc, enc2), "enc = '%s' enc2 = '%s'", enc, enc2); test_free (enc2); push ("Compare addresses"); test_address_compare (addr, addr2); pull (); check_unref (addr2, 1); test_free (enc); pull (); /* FIXME: format/unformat arne't guaranteed to be reversible, at least at the moment */ camel_test_nonfatal ("format/unformat not (yet) reversible for all cases"); push ("Address format/unformat"); format = camel_address_format (CAMEL_ADDRESS (addr)); addr2 = camel_internet_address_new (); check (camel_address_unformat (CAMEL_ADDRESS (addr2), format) == 1); check (camel_address_length (CAMEL_ADDRESS (addr2)) == 1); format2 = camel_address_format (CAMEL_ADDRESS (addr2)); check_msg (string_equal (format, format2), "format = '%s\n\tformat2 = '%s'", format, format2); test_free (format2); /* currently format/unformat doesn't handle ,'s and other special chars at all */ if (camel_address_length (CAMEL_ADDRESS (addr2)) == 1) { push ("Compare addresses"); test_address_compare (addr, addr2); pull (); } test_free (format); pull (); camel_test_fatal (); check_unref (addr2, 1); check_unref (addr, 1); pull (); } camel_test_end (); camel_test_start ("CamelInternetAddress, I18N decode"); for (i = 0; i < G_N_ELEMENTS (test_address); i++) { push ("Testing address line %d '%s'", i, test_address[i].addr); addr = camel_internet_address_new (); push ("checking decoded"); check (camel_address_decode (CAMEL_ADDRESS (addr), test_address[i].addr) == test_address[i].count); format = camel_address_format (CAMEL_ADDRESS (addr)); check (strcmp (format, test_address[i].utf8) == 0); test_free (format); pull (); push ("Comparing re-encoded output"); addr2 = CAMEL_INTERNET_ADDRESS (camel_internet_address_new ()); enc = camel_address_encode (CAMEL_ADDRESS (addr)); check_msg (camel_address_decode (CAMEL_ADDRESS (addr2), enc) == test_address[i].count, "enc = '%s'", enc); test_free (enc); test_address_compare (addr, addr2); check_unref (addr2, 1); pull (); check_unref (addr, 1); pull (); } camel_test_end (); camel_test_start ("CamelInternerAddress name & email decoder"); for (i = 0; i < G_N_ELEMENTS (test_decode); i++) { gchar *line; const gchar *name, *email; gint jj; name = test_decode[i].name; email = test_decode[i].email; for (jj = 0; jj < G_N_ELEMENTS (line_decode_formats); jj++) { if (line_decode_formats[jj].without_name) { line = g_strdup_printf (line_decode_formats[jj].without_name, email); check_address_line_decode (i, line, NULL, email); g_free (line); } if (!name) continue; line = g_strdup_printf (line_decode_formats[jj].with_name, name, email); check_address_line_decode (i, line, name, email); g_free (line); } } camel_test_end (); return 0; }
/* FIXME: check format of fields. */ static gboolean process_header (CamelMedium *medium, const gchar *name, const gchar *value) { CamelHeaderType header_type; CamelMimeMessage *message = CAMEL_MIME_MESSAGE (medium); CamelInternetAddress *addr; const gchar *charset; gchar *unfolded; header_type = (CamelHeaderType) g_hash_table_lookup (header_name_table, name); switch (header_type) { case HEADER_FROM: addr = camel_internet_address_new (); unfolded = camel_header_unfold (value); if (camel_address_decode ((CamelAddress *) addr, unfolded) <= 0) { g_object_unref (addr); } else { if (message->from) g_object_unref (message->from); message->from = addr; } g_free (unfolded); break; case HEADER_REPLY_TO: addr = camel_internet_address_new (); unfolded = camel_header_unfold (value); if (camel_address_decode ((CamelAddress *) addr, unfolded) <= 0) { g_object_unref (addr); } else { if (message->reply_to) g_object_unref (message->reply_to); message->reply_to = addr; } g_free (unfolded); break; case HEADER_SUBJECT: g_free (message->subject); if (((CamelDataWrapper *) message)->mime_type) { charset = camel_content_type_param (((CamelDataWrapper *) message)->mime_type, "charset"); charset = camel_iconv_charset_name (charset); } else charset = NULL; unfolded = camel_header_unfold (value); message->subject = g_strstrip (camel_header_decode_string (unfolded, charset)); g_free (unfolded); break; case HEADER_TO: case HEADER_CC: case HEADER_BCC: case HEADER_RESENT_TO: case HEADER_RESENT_CC: case HEADER_RESENT_BCC: addr = g_hash_table_lookup (message->recipients, name); if (value) { unfolded = camel_header_unfold (value); camel_address_decode (CAMEL_ADDRESS (addr), unfolded); g_free (unfolded); } else { camel_address_remove (CAMEL_ADDRESS (addr), -1); } return FALSE; case HEADER_DATE: if (value) { message->date = camel_header_decode_date (value, &message->date_offset); } else { message->date = CAMEL_MESSAGE_DATE_CURRENT; message->date_offset = 0; } break; case HEADER_MESSAGE_ID: g_free (message->message_id); if (value) message->message_id = camel_header_msgid_decode (value); else message->message_id = NULL; break; default: return FALSE; } return TRUE; }