END_TEST START_TEST(test_dbmail_message_get_size) { DbmailMessage *m; size_t i, j; /* */ m = dbmail_message_new(); m = dbmail_message_init_with_string(m, g_string_new(rfc822)); i = dbmail_message_get_size(m, FALSE); fail_unless(i==277, "dbmail_message_get_size failed"); j = dbmail_message_get_size(m, TRUE); fail_unless(j==289, "dbmail_message_get_size failed"); dbmail_message_free(m); /* */ m = dbmail_message_new(); m = dbmail_message_init_with_string(m, g_string_new("From: paul\n\n")); i = dbmail_message_get_size(m, FALSE); fail_unless(i==12, "dbmail_message_get_size failed [%d]", i); j = dbmail_message_get_size(m, TRUE); fail_unless(j==14, "dbmail_message_get_size failed [%d]", j); dbmail_message_free(m); }
END_TEST START_TEST(test_dbmail_message_construct) { const gchar *sender = "*****@*****.**"; const gchar *subject = "Some test"; const gchar *recipient = "<*****@*****.**> Bar"; gchar *body = g_strdup("\ntesting\n\nááááä\n\n"); gchar *expect = g_strdup("From: [email protected]\n" "Subject: Some test\n" "To: [email protected]\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: base64\n" "\n" "CnRlc3RpbmcKCuHh4eHk"); gchar *result; DbmailMessage *message = dbmail_message_new(); message = dbmail_message_construct(message,recipient,sender,subject,body); result = dbmail_message_to_string(message); fail_unless(MATCH(expect,result),"dbmail_message_construct failed\n%s\n%s", expect, result); dbmail_message_free(message); g_free(body); g_free(result); g_free(expect); body = g_strdup("Mit freundlichen Gr=C3=BC=C3=9Fen"); message = dbmail_message_new(); message = dbmail_message_construct(message,recipient,sender,subject,body); //printf ("%s\n", dbmail_message_to_string(message)); dbmail_message_free(message); g_free(body); }
END_TEST START_TEST(test_dbmail_message_cache_headers) { DbmailMessage *m = dbmail_message_new(); char *s = g_new0(char,20); GString *j = g_string_new(multipart_message); m = dbmail_message_init_with_string(m,j); dbmail_message_set_header(m, "References", "<*****@*****.**> <*****@*****.**> <*****@*****.**> "); dbmail_message_store(m); dbmail_message_free(m); g_string_free(j,TRUE); sprintf(s,"%.*s",10,"abcdefghijklmnopqrstuvwxyz"); fail_unless(MATCH(s,"abcdefghij"),"string truncate failed"); g_free(s); m = dbmail_message_new(); j = g_string_new(multipart_message); m = dbmail_message_init_with_string(m,j); dbmail_message_set_header(m, "Subject", "=?utf-8?Q?[xxxxxxxxxxxxxxxxxx.xx_0000747]:_=C3=84nderungen_an_der_Artikel?= =?utf-8?Q?-Detailseite?=" ); dbmail_message_store(m); dbmail_message_free(m); g_string_free(j,TRUE); }
END_TEST START_TEST(test_dbmail_message_store2) { DbmailMessage *m, *n; u64_t physid; char *t; char *expect; m = message_init(broken_message2); dbmail_message_set_header(m, "Return-Path", dbmail_message_get_header(m, "From")); expect = dbmail_message_to_string(m); fail_unless(m != NULL, "dbmail_message_store2 failed"); dbmail_message_store(m); physid = dbmail_message_get_physid(m); dbmail_message_free(m); n = dbmail_message_new(); dbmail_message_set_physid(n, physid); n = dbmail_message_retrieve(n,physid,DBMAIL_MESSAGE_FILTER_FULL); fail_unless(n != NULL, "_mime_retrieve failed"); t = dbmail_message_to_string(n); COMPARE(expect,t); dbmail_message_free(n); g_free(expect); g_free(t); }
END_TEST //gchar * dbmail_message_body_to_string(DbmailMessage *self); START_TEST(test_dbmail_message_body_to_string) { char *result; GString *s; DbmailMessage *m; s = g_string_new(multipart_message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m,s); result = dbmail_message_body_to_string(m); fail_unless(strlen(result)==1057, "dbmail_message_body_to_string failed [%d] != [1057]\n[%s]\n", strlen(result),result); dbmail_message_free(m); g_string_free(s,TRUE); g_free(result); s = g_string_new(outlook_multipart); m = dbmail_message_new(); m = dbmail_message_init_with_string(m,s); result = dbmail_message_body_to_string(m); fail_unless(strlen(result)==330, "dbmail_message_body_to_string failed [330 != %d:%s]", strlen(result), result); dbmail_message_free(m); g_string_free(s,TRUE); g_free(result); }
END_TEST //DbmailMessage * dbmail_message_retrieve(DbmailMessage *self, u64_t physid, int filter); START_TEST(test_dbmail_message_retrieve) { DbmailMessage *m, *n; GString *s; u64_t physid; s = g_string_new(multipart_message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m, s); fail_unless(m != NULL, "dbmail_message_init_with_string failed"); dbmail_message_set_header(m, "References", "<*****@*****.**> <*****@*****.**> <*****@*****.**> "); dbmail_message_store(m); physid = dbmail_message_get_physid(m); fail_unless(physid > 0, "dbmail_message_get_physid failed"); n = dbmail_message_new(); n = dbmail_message_retrieve(n,physid,DBMAIL_MESSAGE_FILTER_HEAD); fail_unless(n != NULL, "dbmail_message_retrieve failed"); fail_unless(n->content != NULL, "dbmail_message_retrieve failed"); dbmail_message_free(m); dbmail_message_free(n); g_string_free(s,TRUE); }
END_TEST START_TEST(test_dbmail_message_get_header) { char *t; DbmailMessage *h = dbmail_message_new(); DbmailMessage *m = dbmail_message_new(); GString *s, *j; s = g_string_new(multipart_message); m = dbmail_message_init_with_string(m, s); t = dbmail_message_hdrs_to_string(m); j = g_string_new(t); h = dbmail_message_init_with_string(h, j); g_free(t); fail_unless(dbmail_message_get_header(m, "X-Foobar")==NULL, "get_header failed on full message"); fail_unless(strcmp(dbmail_message_get_header(m,"Subject"),"multipart/mixed")==0,"get_header failed on full message"); fail_unless(dbmail_message_get_header(h, "X-Foobar")==NULL, "get_header failed on headers-only message"); fail_unless(strcmp(dbmail_message_get_header(h,"Subject"),"multipart/mixed")==0,"get_header failed on headers-only message"); dbmail_message_free(m); dbmail_message_free(h); g_string_free(s,TRUE); g_string_free(j,TRUE); }
END_TEST START_TEST(test_dbmail_message_encoded) { DbmailMessage *m = dbmail_message_new(); GString *s = g_string_new(encoded_message_koi); //const char *exp = ":: [ Arrty ] :: [ Roy (L) Stèphanie ] <*****@*****.**>"; u64_t id = 0; m = dbmail_message_init_with_string(m, s); fail_unless(strcmp(dbmail_message_get_header(m,"From"),"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=<*****@*****.**>")==0, "dbmail_message_get_header failed for koi-8 encoded header"); dbmail_message_free(m); m = dbmail_message_new(); g_string_printf(s, "%s", utf7_header); m = dbmail_message_init_with_string(m, s); g_string_free(s, TRUE); dbmail_message_store(m); id = dbmail_message_get_physid(m); dbmail_message_free(m); m = dbmail_message_new(); m = dbmail_message_retrieve(m, id, DBMAIL_MESSAGE_FILTER_FULL); dbmail_message_free(m); }
END_TEST START_TEST(test_imap_get_envelope_latin) { char *t; char *expect = g_new0(char,1024); DbmailMessage *m; GString *s; /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_latin_1); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); t = imap_get_envelope(GMIME_MESSAGE(m->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"=?iso-8859-1?Q?Re:_M=F3dulo_Extintores?=\" ((\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" NIL \"nobody\" \"nowhere.org\")) ((\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" NIL \"nobody\" \"nowhere.org\")) ((\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" NIL \"nobody\" \"nowhere.org\")) ((NIL NIL \"nobody\" \"foo.org\")) NIL NIL NIL NIL)",1024); fail_unless(strcmp(t,expect)==0,"imap_get_envelope failed\n%s\n%s\n ", expect, t); g_free(t); dbmail_message_free(m); /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_latin_2); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"=?ISO-8859-2?Q?Re=3A_=5Bgentoo-dev=5D_New_developer=3A__?= =?ISO-8859-2?Q?Miroslav_=A9ulc_=28fordfrog=29?=\" ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((NIL NIL \"gentoo-dev\" \"lists.gentoo.org\")) NIL NIL NIL NIL)",1024); t = imap_get_envelope(GMIME_MESSAGE(m->content)); fail_unless(strcmp(t,expect)==0,"imap_get_envelope failed\n%s\n%s\n ", expect, t); g_free(t); g_free(expect); dbmail_message_free(m); /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_utf8); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); //strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"=?ISO-8859-2?Q?Re=3A_=5Bgentoo-dev=5D_New_developer=3A__?= =?ISO-8859-2?Q?Miroslav_=A9ulc_=28fordfrog=29?=\" ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((NIL NIL \"gentoo-dev\" \"lists.gentoo.org\")) NIL NIL NIL NIL)",1024); t = imap_get_envelope(GMIME_MESSAGE(m->content)); //fail_unless(strcmp(t,expect)==0,"imap_get_envelope failed\n%s\n%s\n ", expect, t); g_free(t); //g_free(expect); dbmail_message_free(m); }
END_TEST START_TEST(test_imap_get_envelope_8bit_id) { DbmailMessage *message; char *result, *expect; const char *msgid = "<000001c1f64e$c4a34180$0100007f@z=F0=B5=D241>"; expect = g_new0(char, 1024); /* text/plain */ message = dbmail_message_new(NULL); message = dbmail_message_init_with_string(message, rfc822); dbmail_message_set_header(message,"Message-ID",msgid); result = imap_get_envelope(GMIME_MESSAGE(message->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"dbmail test message\" ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"testuser\" \"foo.org\")) NIL NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_envelope failed"); g_free(result); dbmail_message_set_header(message,"Message-ID","<*****@*****.**>"); result = imap_get_envelope(GMIME_MESSAGE(message->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"dbmail test message\" ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"testuser\" \"foo.org\")) NIL NIL NIL \"<*****@*****.**>\")",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_envelope failed"); dbmail_message_free(message); g_free(result); g_free(expect); }
END_TEST START_TEST(test_dbmail_message_get_header_repeated) { GTuples *headers; GString *s; DbmailMessage *m; s = g_string_new(multipart_message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m,s); headers = dbmail_message_get_header_repeated(m, "Received"); fail_unless(headers != NULL, "dbmail_message_get_header_repeated failed"); fail_unless(headers->len==2, "dbmail_message_get_header_repeated failed"); g_tuples_destroy(headers); headers = dbmail_message_get_header_repeated(m, "received"); fail_unless(headers != NULL, "dbmail_message_get_header_repeated failed"); fail_unless(headers->len==2, "dbmail_message_get_header_repeated failed"); g_tuples_destroy(headers); dbmail_message_free(m); g_string_free(s,TRUE); }
int send_alert(uint64_t user_idnr, char *subject, char *body) { DbmailMessage *new_message; Field_T postmaster; char *from; int msgflags[IMAP_NFLAGS]; // Only send each unique alert once a day. char *tmp = g_strconcat(subject, body, NULL); char *userchar = g_strdup_printf("%" PRIu64 "", user_idnr); char handle[FIELDSIZE]; memset(handle, 0, sizeof(handle)); dm_md5(tmp, handle); if (db_replycache_validate(userchar, "send_alert", handle, 1) != DM_SUCCESS) { TRACE(TRACE_INFO, "Already sent alert [%s] to user [%" PRIu64 "] today", subject, user_idnr); g_free(userchar); g_free(tmp); return 0; } else { TRACE(TRACE_INFO, "Sending alert [%s] to user [%" PRIu64 "]", subject, user_idnr); db_replycache_register(userchar, "send_alert", handle); g_free(userchar); g_free(tmp); } // From the Postmaster. if (config_get_value("POSTMASTER", "DBMAIL", postmaster) < 0) { TRACE(TRACE_NOTICE, "no config value for POSTMASTER"); } if (strlen(postmaster)) from = postmaster; else from = DEFAULT_POSTMASTER; // Set the \Flagged flag. memset(msgflags, 0, sizeof(msgflags)); msgflags[IMAP_FLAG_FLAGGED] = 1; // Get the user's login name. char *to = auth_get_userid(user_idnr); new_message = dbmail_message_new(NULL); new_message = dbmail_message_construct(new_message, to, from, subject, body); // Pre-insert the message and get a new_message->id dbmail_message_store(new_message); uint64_t tmpid = new_message->id; if (sort_deliver_to_mailbox(new_message, user_idnr, "INBOX", BOX_BRUTEFORCE, msgflags, NULL) != DSN_CLASS_OK) { TRACE(TRACE_ERR, "Unable to deliver alert [%s] to user [%" PRIu64 "]", subject, user_idnr); } g_free(to); db_delete_message(tmpid); dbmail_message_free(new_message); return 0; }
END_TEST //DbmailMessage * dbmail_message_init_with_stream(DbmailMessage *self, GMimeStream *stream, int type); /* START_TEST(test_dbmail_message_init_with_stream) { } END_TEST */ //gchar * dbmail_message_hdrs_to_string(DbmailMessage *self); START_TEST(test_dbmail_message_hdrs_to_string) { char *result; GString *s; DbmailMessage *m; s = g_string_new(multipart_message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m, s); result = dbmail_message_hdrs_to_string(m); fail_unless(strlen(result)==676, "dbmail_message_hdrs_to_string failed [%d] != [634]\n[%s]\n", strlen(result), result); g_string_free(s,TRUE); dbmail_message_free(m); g_free(result); }
END_TEST //void dbmail_message_free(DbmailMessage *self); START_TEST(test_dbmail_message_free) { DbmailMessage *m = dbmail_message_new(); dbmail_message_free(m); }
END_TEST START_TEST(test_imap_get_structure) { DbmailMessage *message; char *result; char *expect = g_new0(char,1024); /* bare bones */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(simple)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); dbmail_message_free(message); g_free(result); /* text/plain */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(rfc822)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); strncpy(expect,"(\"text\" \"plain\" (\"charset\" \"us-ascii\") NIL NIL \"7bit\" 32 4 NIL NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_structure failed\n[%s]!=\n[%s]\n[%s]\n", result, expect, g_mime_object_get_body(message->content)); g_free(result); dbmail_message_free(message); /* multipart */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(multipart_message)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); strncpy(expect,"((\"text\" \"html\" NIL NIL NIL \"7BIT\" 30 3 NIL (\"inline\" NIL) NIL NIL)" "(\"text\" \"plain\" (\"name\" \"testfile\" \"charset\" \"us-ascii\") NIL NIL \"base64\" 432 7 NIL NIL NIL NIL)" " \"mixed\" (\"boundary\" \"boundary\") NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_structure failed\n[%s] !=\n[%s]\n", expect, result); g_free(result); dbmail_message_free(message); /* multipart alternative */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(multipart_alternative)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); strncpy(expect,"(((\"text\" \"plain\" (\"charset\" \"ISO-8859-1\") NIL NIL \"7bit\" 281 10 NIL NIL NIL NIL)(\"text\" \"html\" (\"charset\" \"ISO-8859-1\") NIL NIL \"7bit\" 759 17 NIL NIL NIL NIL) \"alternative\" (\"boundary\" \"------------040302030903000400040101\") NIL NIL NIL)(\"image\" \"jpeg\" (\"name\" \"jesse_2.jpg\") NIL NIL \"base64\" 262 NIL (\"inline\" (\"filename\" \"jesse_2.jpg\")) NIL NIL) \"mixed\" (\"boundary\" \"------------050000030206040804030909\") NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_structure failed\n[%s]!=\n[%s]\n", result, expect); g_free(result); dbmail_message_free(message); g_free(expect); }
static char *store_and_retrieve(DbmailMessage *m) { u64_t physid; DbmailMessage *n; char *t; dbmail_message_store(m); physid = dbmail_message_get_physid(m); fail_unless(physid != 0,"dbmail_message_store failed. physid [%llu]", physid); dbmail_message_free(m); n = dbmail_message_new(); dbmail_message_set_physid(n, physid); n = dbmail_message_retrieve(n,physid,DBMAIL_MESSAGE_FILTER_FULL); fail_unless(n != NULL, "_mime_retrieve failed"); t = dbmail_message_to_string(n); dbmail_message_free(n); return t; }
END_TEST START_TEST(test_dbmail_message_to_string) { char *result; DbmailMessage *m; m = message_init(multipart_message); result = dbmail_message_to_string(m); COMPARE(multipart_message, result); g_free(result); dbmail_message_free(m); // m = message_init(simple_with_from); result = dbmail_message_to_string(m); COMPARE(simple_with_from, result); g_free(result); dbmail_message_free(m); }
END_TEST START_TEST(test_dbmail_message_8bit) { DbmailMessage *m = dbmail_message_new(); GString *s = g_string_new(raw_message_koi); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); dbmail_message_store(m); dbmail_message_free(m); /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_utf8); m = dbmail_message_init_with_string(m, s); g_string_free(s, TRUE); dbmail_message_store(m); dbmail_message_free(m); }
END_TEST START_TEST(test_dbmail_message_set_header) { DbmailMessage *m; GString *s; s = g_string_new(multipart_message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m,s); dbmail_message_set_header(m, "X-Foobar","Foo Bar"); fail_unless(dbmail_message_get_header(m, "X-Foobar")!=NULL, "set_header failed"); dbmail_message_free(m); g_string_free(s,TRUE); }
END_TEST //void dbmail_message_set_class(DbmailMessage *self, int klass); START_TEST(test_dbmail_message_set_class) { int result; DbmailMessage *m = dbmail_message_new(); result = dbmail_message_set_class(m,DBMAIL_MESSAGE); fail_unless(result==0,"dbmail_message_set_class failed"); result = dbmail_message_set_class(m,DBMAIL_MESSAGE_PART); fail_unless(result==0,"dbmail_message_set_class failed"); result = dbmail_message_set_class(m,10); fail_unless(result!=0,"dbmail_message_set_class failed"); dbmail_message_free(m); }
END_TEST START_TEST(test_imap_get_envelope_koi) { char *t; const char *exp = "(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"test\" ((\"=?iso-8859-5?b?sN3i3t0gvdXl3uDe6Njl?=\" NIL \"bad\" \"foo.ru\")) ((\"=?iso-8859-5?b?sN3i3t0gvdXl3uDe6Njl?=\" NIL \"bad\" \"foo.ru\")) ((\"=?iso-8859-5?b?sN3i3t0gvdXl3uDe6Njl?=\" NIL \"bad\" \"foo.ru\")) ((NIL NIL \"nobody\" \"foo.ru\")) NIL NIL NIL NIL)"; DbmailMessage *m = dbmail_message_new(NULL); m = dbmail_message_init_with_string(m, encoded_message_koi); t = imap_get_envelope(GMIME_MESSAGE(m->content)); fail_unless(strcmp(t,exp)==0,"encode/decode/encode loop failed\n[%s] !=\n[%s]", t,exp); g_free(t); dbmail_message_free(m); }
int do_migrate(int migrate_limit) { C c; R r; int id = 0; int count = 0; DbmailMessage *m; qprintf ("Mirgrate legacy 2.2.x messageblks to mimeparts...\n"); if (!yes_to_all) { qprintf ("\tmigration skipped. Use -y option to perform mirgration.\n"); return 0; } qprintf ("Preparing to migrate %d physmessages.\n", migrate_limit); c = db_con_get(); TRY r = db_query(c, "SELECT DISTINCT(physmessage_id) FROM %smessageblks LIMIT %d", DBPFX, migrate_limit); qprintf ("Migrating %d physmessages...\n", migrate_limit); while (db_result_next(r)) { count++; id = db_result_get_u64(r,0); m = dbmail_message_new(); m = dbmail_message_retrieve(m, id, DBMAIL_MESSAGE_FILTER_FULL); if(!dm_message_store(m)) { if(verbose) qprintf ("%d ",id); db_update("DELETE FROM %smessageblks WHERE physmessage_id = %d", DBPFX, id); } else { if(!verbose) qprintf ("migrating physmessage_id: %d\n",id); qprintf ("failed\n"); return -1; } dbmail_message_free(m); } CATCH(SQLException) LOG_SQLERROR; return -1; FINALLY db_con_close(c); END_TRY; qprintf ("Migration complete. Migrated %d physmessages.\n", count); return 0; }
END_TEST START_TEST(test_dbmail_message_get_internal_date) { DbmailMessage *m; GString *s; s = g_string_new(rfc822); m = dbmail_message_new(); m = dbmail_message_init_with_string(m, s); // From_ contains: Wed Sep 14 16:47:48 2005 const char *expect = "2005-09-14 16:47:48"; const char *expect03 = "2003-09-14 16:47:48"; const char *expect75 = "1975-09-14 16:47:48"; char *result; /* baseline */ result = dbmail_message_get_internal_date(m, 0); fail_unless(MATCH(expect,result),"dbmail_message_get_internal_date failed exp [%s] got [%s]", expect, result); g_free(result); /* should be the same */ result = dbmail_message_get_internal_date(m, 2007); fail_unless(MATCH(expect,result),"dbmail_message_get_internal_date failed exp [%s] got [%s]", expect, result); g_free(result); /* capped to 2004, which should also be the same */ result = dbmail_message_get_internal_date(m, 2004); fail_unless(MATCH(expect,result),"dbmail_message_get_internal_date failed exp [%s] got [%s]", expect, result); g_free(result); /* capped to 2003, should be different */ result = dbmail_message_get_internal_date(m, 2003); fail_unless(MATCH(expect03,result),"dbmail_message_get_internal_date failed exp [%s] got [%s]", expect03, result); g_free(result); /* capped to 1975, should be way different */ result = dbmail_message_get_internal_date(m, 1975); fail_unless(MATCH(expect75,result),"dbmail_message_get_internal_date failed exp [%s] got [%s]", expect75, result); g_free(result); g_string_free(s,TRUE); dbmail_message_free(m); }
END_TEST //DbmailMessage * dbmail_message_init_with_string(DbmailMessage *self, const GString *content); START_TEST(test_dbmail_message_init_with_string) { DbmailMessage *m; GTuples *t; m = message_init(multipart_message); t = g_relation_select(m->headers, "Received", 0); fail_unless(t->len==2,"Too few or too many headers in tuple [%d]\n", t->len); g_tuples_destroy(t); dbmail_message_free(m); // m = message_init(simple_message_part); // fail_unless(dbmail_message_get_class(m)==DBMAIL_MESSAGE_PART, "init_with string failed"); // dbmail_message_free(m); }
int do_migrate(int migrate_limit) { Connection_T c; ResultSet_T r; int id = 0; volatile int count = 0; DbmailMessage *m; qprintf ("Migrate legacy 2.2.x messageblks to mimeparts...\n"); if (!yes_to_all) { qprintf ("\tmigration skipped. Use -y option to perform migration.\n"); return 0; } qprintf ("Preparing to migrate up to %d physmessages.\n", migrate_limit); c = db_con_get(); TRY db_begin_transaction(c); r = db_query(c, "SELECT DISTINCT(physmessage_id) FROM %smessageblks LIMIT %d", DBPFX, migrate_limit); while (db_result_next(r)) { count++; id = db_result_get_u64(r,0); m = dbmail_message_new(NULL); m = dbmail_message_retrieve(m, id); if(! dm_message_store(m)) { if(verbose) qprintf ("%d ",id); db_update("DELETE FROM %smessageblks WHERE physmessage_id = %d", DBPFX, id); } dbmail_message_free(m); } db_commit_transaction(c); CATCH(SQLException) LOG_SQLERROR; db_rollback_transaction(c); return -1; FINALLY db_con_close(c); END_TRY; qprintf ("Migration complete. Migrated %d physmessages.\n", count); return 0; }
END_TEST START_TEST(test_imap_get_envelope_koi) { char *t; const char *exp = "(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"test\" ((\"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=\" NIL \"bad\" \"foo.ru\")) ((\"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=\" NIL \"bad\" \"foo.ru\")) ((\"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=\" NIL \"bad\" \"foo.ru\")) ((NIL NIL \"nobody\" \"foo.ru\")) NIL NIL NIL NIL)"; DbmailMessage *m = dbmail_message_new(); GString *s = g_string_new(encoded_message_koi); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); t = imap_get_envelope(GMIME_MESSAGE(m->content)); fail_unless(strcmp(t,exp)==0,"encode/decode/encode loop failed\n[%s] !=\n[%s]", t,exp); g_free(t); dbmail_message_free(m); }
/* * Send a vacation message. FIXME: this should provide * MIME support, to comply with the Sieve-Vacation spec. */ static int send_vacation(DbmailMessage *message, const char *to, const char *from, const char *subject, const char *body, const char *handle) { int result; const char *x_dbmail_vacation = dbmail_message_get_header(message, "X-Dbmail-Vacation"); if (x_dbmail_vacation) { TRACE(TRACE_NOTICE, "vacation loop detected [%s]", x_dbmail_vacation); return 0; } DbmailMessage *new_message = dbmail_message_new(); new_message = dbmail_message_construct(new_message, to, from, subject, body); dbmail_message_set_header(new_message, "X-DBMail-Vacation", handle); result = send_mail(new_message, to, from, NULL, SENDMESSAGE, SENDMAIL); dbmail_message_free(new_message); return result; }
END_TEST //int dbmail_message_get_class(DbmailMessage *self); START_TEST(test_dbmail_message_get_class) { int klass; DbmailMessage *m = dbmail_message_new(); /* default */ klass = dbmail_message_get_class(m); fail_unless(klass==DBMAIL_MESSAGE,"dbmail_message_get_class failed"); /* set explicitely */ dbmail_message_set_class(m,DBMAIL_MESSAGE_PART); klass = dbmail_message_get_class(m); fail_unless(klass==DBMAIL_MESSAGE_PART,"dbmail_message_get_class failed"); dbmail_message_set_class(m,DBMAIL_MESSAGE); klass = dbmail_message_get_class(m); fail_unless(klass==DBMAIL_MESSAGE,"dbmail_message_get_class failed"); dbmail_message_free(m); }
END_TEST START_TEST(test_dbmail_message_get_header_addresses) { GList * result; GString *s; DbmailMessage *m; s = g_string_new(multipart_message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m,s); result = dbmail_message_get_header_addresses(m, "Cc"); result = g_list_first(result); fail_unless(result != NULL, "dbmail_message_get_header_addresses failed"); fail_unless(g_list_length(result)==2,"dbmail_message_get_header_addresses failed"); fail_unless(strcmp((char *)result->data,"*****@*****.**")==0, "dbmail_message_get_header_addresses failed"); g_list_destroy(result); dbmail_message_free(m); g_string_free(s,TRUE); }
void Http_getMessages(T R) { DbmailMessage *m = dbmail_message_new(); struct evbuffer *buf; uint64_t pid; uint64_t id = 0; if (! Request_getId(R)) return; if (! (id = strtoull(Request_getId(R), NULL, 10))) { Request_error(R, HTTP_NOTFOUND, "Not found"); return; } db_get_physmessage_id(id, &pid); if (! pid) { Request_error(R, HTTP_NOTFOUND, "Not found"); return; } buf = evbuffer_new(); m = dbmail_message_retrieve(m, pid); if (Request_getMethod(R) == NULL) { /* * retrieve message meta-data * C < GET /messages/1245911 */ uint64_t size = dbmail_message_get_size(m, TRUE); Request_setContentType(R,"application/json; charset=utf-8"); evbuffer_add_printf(buf, "{\"messages\": {\n"); evbuffer_add_printf(buf, " \"%lu\":{\"size\":%lu}", id, size); evbuffer_add_printf(buf, "\n}}\n"); } else if (MATCH(Request_getMethod(R), "view")) { /* * retrieve message by message_idnr * C < GET /messages/1245911/view */ char *s = dbmail_message_to_string(m); Request_setContentType(R, "message/rfc822; charset=utf-8"); evbuffer_add_printf(buf, "%s", s); g_free(s); } else if (MATCH(Request_getMethod(R),"headers")) { Request_setContentType(R, "text/plain; charset=utf-8"); if (Request_getArg(R) && strlen(Request_getArg(R))) { /* * retrieve selected message headers * C < GET /messages/1245911/headers/subject,from,to */ int i = 0; char **headerlist = g_strsplit(Request_getArg(R),",",0); while (headerlist[i]) { char *hname = headerlist[i]; hname[0] = g_ascii_toupper(hname[0]); TRACE(TRACE_DEBUG,"header: [%s]", headerlist[i]); GList * headers = dbmail_message_get_header_repeated(m, headerlist[i]); while(headers) { evbuffer_add_printf(buf, "%s: %s\n", hname, (char *)headers->data); if (! g_list_next(headers)) break; headers = g_list_next(headers); } i++; } } else { /* * retrieve all message headers * C < GET /messages/1245911/headers */ char *s = dbmail_message_hdrs_to_string(m); Request_setContentType(R, "text/plain; charset=utf-8"); evbuffer_add_printf(buf, "%s", s); g_free(s); } } if (EVBUFFER_LENGTH(buf)) Request_send(R, HTTP_OK, "OK", buf); else Request_error(R, HTTP_SERVUNAVAIL, "Server error"); evbuffer_free(buf); dbmail_message_free(m); }