END_TEST START_TEST(test_dbmail_mailbox_orderedsubject) { char *res; char *args; char **array; uint64_t idx = 0; DbmailMailbox *mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("HEADER FROM foo.org ( SINCE 1-Jan-2005 )"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, 0); dbmail_mailbox_search(mb); dbmail_mailbox_set_uid(mb,TRUE); res = dbmail_mailbox_orderedsubject(mb); fail_unless(res!= NULL, "dbmail_mailbox_orderedsubject failed"); g_free(res); dbmail_mailbox_set_uid(mb,FALSE); res = dbmail_mailbox_orderedsubject(mb); fail_unless(res!= NULL, "dbmail_mailbox_orderedsubject failed"); g_free(res); dbmail_mailbox_free(mb); g_strfreev(array); }
END_TEST START_TEST(test_dbmail_mailbox_free) { DbmailMailbox *mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); dbmail_mailbox_free(mb); }
END_TEST START_TEST(test_dbmail_mailbox_dump) { int c = 0; FILE *o = fopen("/dev/null","w"); DbmailMailbox *mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); c = dbmail_mailbox_dump(mb,o); fail_unless(c>=0,"dbmail_mailbox_dump failed"); dbmail_mailbox_free(mb); // fprintf(stderr,"dumped [%d] messages\n", c); }
END_TEST START_TEST(test_dbmail_mailbox_search_parsed_2) { uint64_t idx=0; gboolean sorted = 0; DbmailMailbox *mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); char *args = g_strdup("UID 1,* BODY the"); char **array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); dbmail_mailbox_free(mb); g_strfreev(array); }
END_TEST START_TEST(test_dbmail_mailbox_sort) { char *args; char **array; uint64_t idx = 0; gboolean sorted = 1; DbmailMailbox *mb; // first case mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival cc date reverse from size subject to ) us-ascii HEADER FROM test SINCE 1-Feb-1994"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_sort(mb); dbmail_mailbox_free(mb); g_strfreev(array); }
END_TEST START_TEST(test_dbmail_mailbox_get_set) { guint c, d, r; GTree *set; DbmailMailbox *mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); dbmail_mailbox_set_uid(mb,TRUE); r = dbmail_mailbox_open(mb); fail_unless(r == DM_SUCCESS, "dbmail_mailbox_open failed"); // basic tests; set = dbmail_mailbox_get_set(mb, "1:*", 0); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); c = g_tree_nnodes(set); fail_unless(c>1,"dbmail_mailbox_get_set failed [%d]", c); g_tree_destroy(set); set = dbmail_mailbox_get_set(mb,"*:1",0); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); d = g_tree_nnodes(set); fail_unless(c==d,"dbmail_mailbox_get_set failed [%d != %d]", c, d); g_tree_destroy(set); set = dbmail_mailbox_get_set(mb,"1,*",0); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); d = g_tree_nnodes(set); fail_unless(d==2,"mailbox_get_set failed [%d != 2]", d); g_tree_destroy(set); set = dbmail_mailbox_get_set(mb,"1,*",0); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); d = g_tree_nnodes(set); fail_unless(d==2,"mailbox_get_set failed"); g_tree_destroy(set); set = dbmail_mailbox_get_set(mb,"1",0); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); d = g_tree_nnodes(set); fail_unless(d==1,"mailbox_get_set failed"); g_tree_destroy(set); set = dbmail_mailbox_get_set(mb,"-1:1",0); fail_unless(set == NULL); // UID sets char *s, *t; set = dbmail_mailbox_get_set(mb, "1:*", 1); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); s = tree_as_string(set); c = g_tree_nnodes(set); fail_unless(c>1,"dbmail_mailbox_get_set failed"); g_tree_destroy(set); set = dbmail_mailbox_get_set(mb, "1:*", 0); fail_unless(set != NULL,"dbmail_mailbox_get_set failed"); t = tree_as_string(set); fail_unless(strncmp(s,t,1024)==0,"mismatch between <1:*> and <UID 1:*>\n%s\n%s", s,t); g_tree_destroy(set); g_free(s); g_free(t); dbmail_mailbox_free(mb); // empty box mb = dbmail_mailbox_new(get_mailbox_id("empty")); dbmail_mailbox_open(mb); set = dbmail_mailbox_get_set(mb, "1:*", 0); fail_unless(set == NULL,"dbmail_mailbox_get_set failed"); set = dbmail_mailbox_get_set(mb, "*", 0); fail_unless(set == NULL,"dbmail_mailbox_get_set failed"); set = dbmail_mailbox_get_set(mb, "1", 0); fail_unless(set == NULL,"dbmail_mailbox_get_set failed"); dbmail_mailbox_free(mb); }
END_TEST START_TEST(test_dbmail_mailbox_search) { char *args; char **array; uint64_t idx = 0; gboolean sorted = 1; int all, found, notfound; DbmailMailbox *mb; // first case mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM foo SINCE 1-Feb-1994 ( SENTSINCE 1-Feb-1995 OR BEFORE 1-Jan-2006 SINCE 1-Jan-2005 )"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_sort(mb); dbmail_mailbox_search(mb); dbmail_mailbox_free(mb); g_strfreev(array); // second case // idx=0; sorted = 0; mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("1:*"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); all = g_tree_nnodes(mb->found); dbmail_mailbox_free(mb); g_strfreev(array); idx=0; sorted = 0; mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("1:* TEXT @"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); found = g_tree_nnodes(mb->found); dbmail_mailbox_free(mb); g_strfreev(array); idx=0; sorted = 0; mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("1:* NOT TEXT @"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); notfound = g_tree_nnodes(mb->found); dbmail_mailbox_free(mb); g_strfreev(array); fail_unless((all - found) == notfound, "dbmail_mailbox_search failed: SEARCH NOT (all: %d, found: %d, notfound: %d)", all, found, notfound); // third case idx=0; sorted = 0; mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("UID 1,* BODY [email protected]"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); dbmail_mailbox_free(mb); g_strfreev(array); // idx=0; sorted = 0; mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("1"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); found = g_tree_nnodes(mb->found); fail_unless(found==1,"dbmail_mailbox_search failed: SEARCH UID 1"); dbmail_mailbox_free(mb); g_strfreev(array); // idx=0; sorted = 0; mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("OR FROM myclient SUBJECT myclient"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_search(mb); // found = g_tree_nnodes(mb->ids); // fail_unless(found==1,"dbmail_mailbox_search failed: SEARCH UID 1"); dbmail_mailbox_free(mb); g_strfreev(array); }
END_TEST START_TEST(test_dbmail_mailbox_build_imap_search) { char *args; char **array; uint64_t idx = 0; gboolean sorted = 1; DbmailMailbox *mb, *mc, *md; // first case mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM [email protected] SINCE 1-Feb-1994"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_free(mb); g_strfreev(array); // second case idx = 0; mc = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival ) ( cc ) us-ascii HEADER FROM [email protected] " "SINCE 1-Feb-1990"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mc, array, &idx, sorted); dbmail_mailbox_free(mc); g_strfreev(array); // third case idx = 0; md = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM test ( SINCE 1-Feb-1995 OR HEADER SUBJECT test " "HEADER SUBJECT foo )"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(md, array, &idx, sorted); fail_unless(g_node_max_height(g_node_get_root(md->search))==4, "build_search: tree too shallow"); dbmail_mailbox_free(md); g_strfreev(array); // fourth case idx = 0; md = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("1,* ( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM test ( SINCE 1-Feb-1995 OR HEADER SUBJECT test " "HEADER SUBJECT foo )"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(md, array, &idx, 1); fail_unless(g_node_max_height(g_node_get_root(md->search))==4, "build_search: tree too shallow"); dbmail_mailbox_free(md); g_strfreev(array); }
static int mailbox_dump(u64_t mailbox_idnr, const char *dumpfile, const char *search, int delete_after_dump) { FILE *ostream; DbmailMailbox *mb = NULL; ImapSession *s = NULL; int result = 0; /* * For dbmail the usual filesystem semantics don't really * apply. Mailboxes can contain other mailboxes as well as * messages. For now however, this is solved by appending * the mailboxname with .mbox * * TODO: facilitate maildir type exports */ mb = dbmail_mailbox_new(mailbox_idnr); if (search) { s = dbmail_imap_session_new(); s->ci = client_init(NULL); if (! (imap4_tokenizer_main(s, search))) { qerrorf("error parsing search string"); dbmail_imap_session_delete(&s); dbmail_mailbox_free(mb); return 1; } if (dbmail_mailbox_build_imap_search(mb, s->args, &(s->args_idx), SEARCH_UNORDERED) < 0) { qerrorf("invalid search string"); dbmail_imap_session_delete(&s); dbmail_mailbox_free(mb); return 1; } dbmail_mailbox_search(mb); dbmail_imap_session_delete(&s); } if (strcmp(dumpfile, "-") == 0) { ostream = stdout; } else if (! (ostream = fopen(dumpfile, "a"))) { int err = errno; qerrorf("opening [%s] failed [%s]\n", dumpfile, strerror(err)); result = -1; goto cleanup; } if (dbmail_mailbox_dump(mb, ostream) < 0) { qerrorf("Export failed\n"); result = -1; goto cleanup; } if (delete_after_dump) { int deleted_flag[IMAP_NFLAGS]; memset(deleted_flag, 0, IMAP_NFLAGS * sizeof(int)); deleted_flag[IMAP_FLAG_DELETED] = 1; GList *ids = g_tree_keys(MailboxState_getIds(mb->mbstate)); while (ids) { // Flag the selected messages \\Deleted // Following this, dbmail-util -d sets deleted status if (delete_after_dump & 1) { if (db_set_msgflag(*(u64_t *)ids->data, deleted_flag, NULL, IMAPFA_ADD, NULL) < 0) { qerrorf("Error setting flags for message [%llu]\n", *(u64_t *)ids->data); result = -1; } } // Set deleted status on each message // Following this, dbmail-util -p sets purge status if (delete_after_dump & 2) { if (! db_set_message_status(*(u64_t *)ids->data, MESSAGE_STATUS_DELETE)) { qerrorf("Error setting status for message [%llu]\n", *(u64_t *)ids->data); result = -1; } } if (!g_list_next(ids)) break; ids = g_list_next(ids); } g_list_free(g_list_first(ids)); } cleanup: if (mb) dbmail_mailbox_free(mb); if ((ostream) && (ostream != stdout)) fclose(ostream); return result; }