Skip to content

Commit

Permalink
SIEVE: add support for keyword flags and fix baseflags (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjstevns committed Aug 1, 2012
1 parent d9bca57 commit 41cfc3d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
14 changes: 7 additions & 7 deletions src/dbmail-message.c
Expand Up @@ -2008,7 +2008,7 @@ dsn_class_t sort_and_deliver(DbmailMessage *message,
if (source == BOX_BRUTEFORCE) {
TRACE(TRACE_NOTICE, "Beginning brute force delivery for user [%lu] to mailbox [%s].",
useridnr, mailbox);
return sort_deliver_to_mailbox(message, useridnr, mailbox, source, NULL);
return sort_deliver_to_mailbox(message, useridnr, mailbox, source, NULL, NULL);
}

/* This is the only condition when called from pipe.c, actually. */
Expand Down Expand Up @@ -2074,7 +2074,7 @@ dsn_class_t sort_and_deliver(DbmailMessage *message,
ret = DSN_CLASS_OK;
TRACE(TRACE_INFO, "Keep was cancelled. Message may be discarded.");
} else {
ret = sort_deliver_to_mailbox(message, useridnr, mailbox, source, NULL);
ret = sort_deliver_to_mailbox(message, useridnr, mailbox, source, NULL, NULL);
TRACE(TRACE_INFO, "Keep was not cancelled. Message will be delivered by default.");
}

Expand All @@ -2094,7 +2094,7 @@ dsn_class_t sort_and_deliver(DbmailMessage *message,

dsn_class_t sort_deliver_to_mailbox(DbmailMessage *message,
uint64_t useridnr, const char *mailbox, mailbox_source source,
int *msgflags)
int *msgflags, GList *keywords)
{
uint64_t mboxidnr, newmsgidnr;
Field_T val;
Expand Down Expand Up @@ -2136,7 +2136,7 @@ dsn_class_t sort_deliver_to_mailbox(DbmailMessage *message,
TRACE(TRACE_NOTICE, "already tried to deliver to INBOX");
return DSN_CLASS_FAIL;
}
return sort_deliver_to_mailbox(message, useridnr, "INBOX", BOX_DEFAULT, msgflags);
return sort_deliver_to_mailbox(message, useridnr, "INBOX", BOX_DEFAULT, msgflags, keywords);
case 1:
// Has right.
TRACE(TRACE_INFO, "user [%lu] has right to deliver mail to [%s]",
Expand Down Expand Up @@ -2171,10 +2171,10 @@ dsn_class_t sort_deliver_to_mailbox(DbmailMessage *message,
default:
TRACE(TRACE_NOTICE, "message id=%lu, size=%zd is inserted",
newmsgidnr, msgsize);
if (msgflags) {
TRACE(TRACE_NOTICE, "message id=%lu, setting imap flags",
if (msgflags || keywords) {
TRACE(TRACE_NOTICE, "message id=%llu, setting imap flags",
newmsgidnr);
db_set_msgflag(newmsgidnr, msgflags, NULL, IMAPFA_ADD, NULL);
db_set_msgflag(newmsgidnr, msgflags, keywords, IMAPFA_ADD, NULL);
db_mailbox_seq_update(mboxidnr);
}
message->id = newmsgidnr;
Expand Down
2 changes: 1 addition & 1 deletion src/dbmail-message.h
Expand Up @@ -118,7 +118,7 @@ dsn_class_t sort_and_deliver(DbmailMessage *self,

dsn_class_t sort_deliver_to_mailbox(DbmailMessage *message,
uint64_t useridnr, const char *mailbox, mailbox_source source,
int *msgflags);
int *msgflags, GList *keywords);

// from dm_pipe.h
//
Expand Down
2 changes: 1 addition & 1 deletion src/dm_db.c
Expand Up @@ -2886,7 +2886,7 @@ int db_set_msgflag(uint64_t msg_idnr, int *flags, GList *keywords, int action_ty
memset(query,0,DEF_QUERYSIZE);
pos += snprintf(query, DEF_QUERYSIZE, "UPDATE %smessages SET ", DBPFX);

for (i = 0; i < IMAP_NFLAGS; i++) {
for (i = 0; flags && i < IMAP_NFLAGS; i++) {
if (flags[i])
TRACE(TRACE_DEBUG,"set %s", db_flag_desc[i]);

Expand Down
40 changes: 33 additions & 7 deletions src/modules/sortsieve.c
Expand Up @@ -169,8 +169,8 @@ int send_alert(uint64_t user_idnr, char *subject, char *body)
uint64_t tmpid = new_message->id;

if (sort_deliver_to_mailbox(new_message, user_idnr,
"INBOX", BOX_BRUTEFORCE, msgflags) != DSN_CLASS_OK) {
TRACE(TRACE_ERR, "Unable to deliver alert [%s] to user [%lu]", subject, user_idnr);
"INBOX", BOX_BRUTEFORCE, msgflags, NULL) != DSN_CLASS_OK) {
TRACE(TRACE_ERR, "Unable to deliver alert [%s] to user [%llu]", subject, user_idnr);
}

g_free(to);
Expand Down Expand Up @@ -346,19 +346,27 @@ int sort_fileinto(sieve2_context_t *s, void *my)
{
struct sort_context *m = (struct sort_context *)my;
extern const char * imap_flag_desc[];
char * const * flags;
char * const * flaglist;
const char * mailbox;
int msgflags[IMAP_NFLAGS];
int *has_msgflags = NULL;
GList *keywords = NULL;
char *allflags = NULL;
char **flags = NULL;

mailbox = sieve2_getvalue_string(s, "mailbox");
flags = sieve2_getvalue_stringlist(s, "flags");
flaglist = sieve2_getvalue_stringlist(s, "flags");
allflags = g_strjoinv(" ", (char **)flaglist);
flags = g_strsplit(allflags, " ", 0);

/* This condition exists for the KEEP callback. */
if (! mailbox) {
mailbox = "INBOX";
}

TRACE(TRACE_INFO, "Action is FILEINTO: mailbox is [%s] flags are [%s]",
mailbox, allflags);

/* If there were any imapflags, set them. */
if (flags) {
int i, j;
Expand All @@ -367,28 +375,46 @@ int sort_fileinto(sieve2_context_t *s, void *my)
// Loop through all script/user-specified flags.
for (i = 0; flags[i]; i++) {
// Find the ones we support.
int baseflag = FALSE;
char *flag = strrchr(flags[i], '\\');
if (flag)
flag++;
else
flag = flags[i];

for (j = 0; imap_flag_desc[j] && j < IMAP_NFLAGS; j++) {
if (g_strcasestr(imap_flag_desc[j], flags[i])) {
if (g_strcasestr(imap_flag_desc[j], flag)) {
TRACE(TRACE_DEBUG, "set baseflag [%s]", flag);
// Flag 'em.
msgflags[j] = 1;
baseflag = TRUE;
// Only pass msgflags if we found something.
has_msgflags = msgflags;
}
}
if (! baseflag) {
TRACE(TRACE_DEBUG, "set keyword [%s]", flag);
keywords = g_list_append(keywords, g_strdup(flag));
}

}
g_strfreev(flags);
}
g_free(allflags);

TRACE(TRACE_INFO, "Action is FILEINTO: mailbox is [%s] flags are [%s]", mailbox, (char *)flags);

/* Don't cancel the keep if there's a problem storing the message. */
if (sort_deliver_to_mailbox(m->message, m->user_idnr,
mailbox, BOX_SORTING, has_msgflags) != DSN_CLASS_OK) {
mailbox, BOX_SORTING, has_msgflags, keywords) != DSN_CLASS_OK) {
TRACE(TRACE_ERR, "Could not file message into mailbox; not cancelling keep.");
m->result->cancelkeep = 0;
} else {
m->result->cancelkeep = 1;
}

if (keywords)
g_list_destroy(keywords);

return SIEVE2_OK;
}

Expand Down

0 comments on commit 41cfc3d

Please sign in to comment.