/* Delete message from mailbox if it is Trash and the message date less then passed date */ int do_erase_old(int days, char * mbtrash_name) { Connection_T c; PreparedStatement_T s; ResultSet_T r; char expire [DEF_FRAGSIZE]; memset(expire,0,sizeof(expire)); snprintf(expire, DEF_FRAGSIZE-1, db_get_sql(SQL_EXPIRE), days); c = db_con_get(); s = db_stmt_prepare(c,"SELECT msg.message_idnr FROM %smessages msg " "JOIN %sphysmessage phys ON msg.physmessage_id = phys.id " "JOIN %smailboxes mb ON msg.mailbox_idnr = mb.mailbox_idnr " "WHERE mb.name = ? AND msg.status < %d " "AND phys.internal_date < %s ", DBPFX, DBPFX, DBPFX, MESSAGE_STATUS_DELETE, expire); db_stmt_set_str(s, 1, mbtrash_name); TRY r = db_stmt_query(s); while(db_result_next(r)) { uint64_t id = db_result_get_u64(r, 0); qprintf("Deleting message id(%" PRIu64 ")\n", id); db_set_message_status(id,MESSAGE_STATUS_PURGE); } CATCH(SQLException) LOG_SQLERROR; return -1; FINALLY db_con_close(c); END_TRY; return 0; }
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; }