GTree * MailboxState_get_set(MailboxState_T M, const char *set, gboolean uid) { GTree *inset, *a, *b; GList *sets = NULL; GString *t; uint64_t lo = 0, hi = 0; gboolean error = FALSE; if (uid) inset = MailboxState_getIds(M); else inset = MailboxState_getMsn(M); a = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL, (GDestroyNotify)uint64_free, (GDestroyNotify)uint64_free); b = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL, (GDestroyNotify)uint64_free, (GDestroyNotify)uint64_free); if (! uid) { lo = 1; hi = MailboxState_getExists(M); } else { GList *ids = g_tree_keys(inset); if (ids) { ids = g_list_last(ids); hi = *((uint64_t *)ids->data); ids = g_list_first(ids); lo = *((uint64_t *)ids->data); g_list_free(g_list_first(ids)); } } t = g_string_new(set); sets = g_string_split(t,","); g_string_free(t,TRUE); sets = g_list_first(sets); while(sets) { uint64_t l = 0, r = 0; char *rest = (char *)sets->data; if (strlen(rest) < 1) break; if (g_tree_nnodes(inset) == 0) { // empty box if (rest[0] == '*') { uint64_t *k = mempool_pop(small_pool, sizeof(uint64_t)); uint64_t *v = mempool_pop(small_pool, sizeof(uint64_t)); *k = 1; *v = MailboxState_getUidnext(M); g_tree_insert(b, k, v); } else { if (! (l = dm_strtoull(sets->data, &rest, 10))) { error = TRUE; break; } if (rest[0]) { if (rest[0] != ':') { error = TRUE; break; } rest++; if ((rest[0] != '*') && (! dm_strtoull(rest, NULL, 10))) { error = TRUE; break; } } uint64_t *k = mempool_pop(small_pool, sizeof(uint64_t)); uint64_t *v = mempool_pop(small_pool, sizeof(uint64_t)); *k = 1; *v = MailboxState_getUidnext(M); g_tree_insert(b, k, v); } } else { if (rest[0] == '*') { l = hi; r = l; if (strlen(rest) > 1) rest++; } else { if (! (l = dm_strtoull(sets->data,&rest,10))) { error = TRUE; break; } if (l == 0xffffffff) l = hi; // outlook l = max(l,lo); r = l; } if (rest[0]==':') { if (strlen(rest)>1) rest++; if (rest[0] == '*') r = hi; else { if (! (r = dm_strtoull(rest,NULL,10))) { error = TRUE; break; } if (r == 0xffffffff) r = hi; // outlook } if (!r) break; } if (! (l && r)) break; find_range(inset, min(l,r), max(l,r), a, uid); if (g_tree_merge(b,a,IST_SUBSEARCH_OR)) { error = TRUE; TRACE(TRACE_ERR, "cannot compare null trees"); break; } } if (! g_list_next(sets)) break; sets = g_list_next(sets); } g_list_destroy(sets); if (a) g_tree_destroy(a); if (error) { g_tree_destroy(b); b = NULL; TRACE(TRACE_DEBUG, "return NULL"); } return b; }
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; }