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 //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_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_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 //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 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_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_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 //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 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); }
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); }
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 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); }
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_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); }
static DbmailMessage * message_init(const char *message) { GString *s; DbmailMessage *m; s = g_string_new(message); m = dbmail_message_new(); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); fail_unless(m != NULL, "dbmail_message_init_with_string failed"); return 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); }
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); }
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; }
/* * 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); }
int lmtp(ClientSession_T * session) { DbmailMessage *msg; ClientBase_T *ci = session->ci; int helpcmd; const char *class, *subject, *detail; size_t tmplen = 0, tmppos = 0; char *tmpaddr = NULL, *tmpbody = NULL, *arg; switch (session->command_type) { case LMTP_QUIT: ci_write(ci, "221 %s BYE\r\n", session->hostname); session->state = QUIT; return 1; case LMTP_NOOP: ci_write(ci, "250 OK\r\n"); return 1; case LMTP_RSET: ci_write(ci, "250 OK\r\n"); lmtp_rset(session,TRUE); return 1; case LMTP_LHLO: /* Reply wth our hostname and a list of features. * The RFC requires a couple of SMTP extensions * with a MUST statement, so just hardcode them. * */ ci_write(ci, "250-%s\r\n250-PIPELINING\r\n" "250-ENHANCEDSTATUSCODES\r\n250 SIZE\r\n", session->hostname); /* This is a SHOULD implement: * "250-8BITMIME\r\n" * Might as well do these, too: * "250-CHUNKING\r\n" * "250-BINARYMIME\r\n" * */ client_session_reset(session); session->state = AUTH; client_session_set_timeout(session, server_conf->timeout); return 1; case LMTP_HELP: session->args = g_list_first(session->args); if (session->args && session->args->data) arg = (char *)session->args->data; else arg = NULL; if (arg == NULL) helpcmd = LMTP_END; else for (helpcmd = LMTP_LHLO; helpcmd < LMTP_END; helpcmd++) if (strcasecmp (arg, commands[helpcmd]) == 0) break; TRACE(TRACE_DEBUG, "LMTP_HELP requested for commandtype %d", helpcmd); if ((helpcmd == LMTP_LHLO) || (helpcmd == LMTP_DATA) || (helpcmd == LMTP_RSET) || (helpcmd == LMTP_QUIT) || (helpcmd == LMTP_NOOP) || (helpcmd == LMTP_HELP)) { ci_write(ci, "%s", LMTP_HELP_TEXT[helpcmd]); } else ci_write(ci, "%s", LMTP_HELP_TEXT[LMTP_END]); return 1; case LMTP_VRFY: /* RFC 2821 says this SHOULD be implemented... * and the goal is to say if the given address * is a valid delivery address at this server. */ ci_write(ci, "502 Command not implemented\r\n"); return 1; case LMTP_EXPN: /* RFC 2821 says this SHOULD be implemented... * and the goal is to return the membership * of the specified mailing list. */ ci_write(ci, "502 Command not implemented\r\n"); return 1; case LMTP_MAIL: /* We need to LHLO first because the client * needs to know what extensions we support. * */ if (session->state != AUTH) { ci_write(ci, "550 Command out of sequence.\r\n"); return 1; } if (g_list_length(session->from) > 0) { ci_write(ci, "500 Sender already received. Use RSET to clear.\r\n"); return 1; } /* First look for an email address. * Don't bother verifying or whatever, * just find something between angle brackets! * */ session->args = g_list_first(session->args); if (! (session->args && session->args->data)) return 1; arg = (char *)session->args->data; if (find_bounded(arg, '<', '>', &tmpaddr, &tmplen, &tmppos) < 0) { ci_write(ci, "500 No address found. Missing <> boundries.\r\n"); return 1; } /* Second look for a BODY keyword. * See if it has an argument, and if we * support that feature. Don't give an OK * if we can't handle it yet, like 8BIT! * */ /* Find the '=' following the address * then advance one character past it * (but only if there's more string!) * */ if ((tmpbody = strstr(arg + tmppos, "=")) != NULL) if (strlen(tmpbody)) tmpbody++; /* This is all a bit nested now... */ if (tmpbody) { if (MATCH(tmpbody, "8BITMIME")) { // RFC1652 ci_write(ci, "500 Please use 7BIT MIME only.\r\n"); return 1; } if (MATCH(tmpbody, "BINARYMIME")) { // RFC3030 ci_write(ci, "500 Please use 7BIT MIME only.\r\n"); return 1; } } session->from = g_list_prepend(session->from, g_strdup(tmpaddr)); ci_write(ci, "250 Sender <%s> OK\r\n", (char *)(session->from->data)); g_free(tmpaddr); return 1; case LMTP_RCPT: if (session->state != AUTH) { ci_write(ci, "550 Command out of sequence.\r\n"); return 1; } session->args = g_list_first(session->args); if (! (session->args && session->args->data)) return 1; arg = (char *)session->args->data; if (find_bounded(arg, '<', '>', &tmpaddr, &tmplen, &tmppos) < 0 || tmplen < 1) { ci_write(ci, "500 No address found. Missing <> boundries or address is null.\r\n"); return 1; } Delivery_T *dsnuser = g_new0(Delivery_T,1); dsnuser_init(dsnuser); /* find_bounded() allocated tmpaddr for us, and that's ok * since dsnuser_free() will free it for us later on. */ dsnuser->address = tmpaddr; if (dsnuser_resolve(dsnuser) != 0) { TRACE(TRACE_ERR, "dsnuser_resolve_list failed"); ci_write(ci, "430 Temporary failure in recipient lookup\r\n"); dsnuser_free(dsnuser); g_free(dsnuser); return 1; } /* Class 2 means the address was deliverable in some way. */ switch (dsnuser->dsn.class) { case DSN_CLASS_OK: ci_write(ci, "250 Recipient <%s> OK\r\n", dsnuser->address); session->rcpt = g_list_prepend(session->rcpt, dsnuser); break; default: ci_write(ci, "550 Recipient <%s> FAIL\r\n", dsnuser->address); dsnuser_free(dsnuser); g_free(dsnuser); break; } return 1; /* Here's where it gets really exciting! */ case LMTP_DATA: msg = dbmail_message_new(); dbmail_message_init_with_string(msg, session->rbuff); dbmail_message_set_header(msg, "Return-Path", (char *)session->from->data); g_string_truncate(session->rbuff,0); g_string_maybe_shrink(session->rbuff); if (insert_messages(msg, session->rcpt) == -1) { ci_write(ci, "430 Message not received\r\n"); dbmail_message_free(msg); return 1; } /* The DATA command itself it not given a reply except * that of the status of each of the remaining recipients. */ /* The replies MUST be in the order received */ session->rcpt = g_list_reverse(session->rcpt); while (session->rcpt) { Delivery_T * dsnuser = (Delivery_T *)session->rcpt->data; dsn_tostring(dsnuser->dsn, &class, &subject, &detail); /* Give a simple OK, otherwise a detailed message. */ switch (dsnuser->dsn.class) { case DSN_CLASS_OK: ci_write(ci, "%d%d%d Recipient <%s> OK\r\n", dsnuser->dsn.class, dsnuser->dsn.subject, dsnuser->dsn.detail, dsnuser->address); break; default: ci_write(ci, "%d%d%d Recipient <%s> %s %s %s\r\n", dsnuser->dsn.class, dsnuser->dsn.subject, dsnuser->dsn.detail, dsnuser->address, class, subject, detail); } if (! g_list_next(session->rcpt)) break; session->rcpt = g_list_next(session->rcpt); } dbmail_message_free(msg); /* Reset the session after a successful delivery; * MTA's like Exim prefer to immediately begin the * next delivery without an RSET or a reconnect. */ lmtp_rset(session,TRUE); return 1; default: return lmtp_error(session, "500 What are you trying to say here?\r\n"); } return 1; }