stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / imap_store.c
index a7558ba1641f46b41ed5e92be465fc024d47ba7a..429f8edbb980c8162eb9bd62b068f14e07c425e3 100644 (file)
@@ -1,11 +1,9 @@
 /*
- * $Id$
- *
  * Implements the STORE command in IMAP.
  *
  * Copyright (c) 2001-2009 by the citadel.org team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
 #include "support.h"
 #include "config.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
+#include "room_ops.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
-#include "imap_tools.h"
 #include "serv_imap.h"
+#include "imap_tools.h"
 #include "imap_fetch.h"
 #include "imap_store.h"
 #include "genstamp.h"
  *
  * We also implement the ".SILENT" protocol option here.  :(
  */
-void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
-
+void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
+       citimap *Imap = IMAP;
 
        if (!strncasecmp(oper, "FLAGS", 5)) {
-               IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
-               IMAP->flags[seq] |= bits_to_twiddle;
+               Imap->flags[seq] &= IMAP_MASK_SYSTEM;
+               Imap->flags[seq] |= bits_to_twiddle;
        }
        else if (!strncasecmp(oper, "+FLAGS", 6)) {
-               IMAP->flags[seq] |= bits_to_twiddle;
+               Imap->flags[seq] |= bits_to_twiddle;
        }
        else if (!strncasecmp(oper, "-FLAGS", 6)) {
-               IMAP->flags[seq] &= (~bits_to_twiddle);
+               Imap->flags[seq] &= (~bits_to_twiddle);
        }
 }
 
@@ -91,10 +78,10 @@ void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
  * imap_store() calls imap_do_store() to perform the actual bit twiddling
  * on the flags.
  */
-void imap_do_store(int num_items, char **itemlist) {
+void imap_do_store(citimap_command *Cmd) {
        int i, j;
        unsigned int bits_to_twiddle = 0;
-       char *oper;
+       const char *oper;
        char flag[32];
        char whichflags[256];
        char num_flags;
@@ -102,10 +89,11 @@ void imap_do_store(int num_items, char **itemlist) {
        long *ss_msglist;
        int num_ss = 0;
        int last_item_twiddled = (-1);
+       citimap *Imap = IMAP;
 
-       if (num_items < 2) return;
-       oper = itemlist[0];
-       if (bmstrcasestr(oper, ".SILENT")) {
+       if (Cmd->num_parms < 2) return;
+       oper = Cmd->Params[0].Key;
+       if (cbmstrcasestr(oper, ".SILENT")) {
                silent = 1;
        }
 
@@ -113,14 +101,14 @@ void imap_do_store(int num_items, char **itemlist) {
         * ss_msglist is an array of message numbers to manipulate.  We
         * are going to supply this array to CtdlSetSeen() later.
         */
-       ss_msglist = malloc(IMAP->num_msgs * sizeof(long));
+       ss_msglist = malloc(Imap->num_msgs * sizeof(long));
        if (ss_msglist == NULL) return;
 
        /*
         * Ok, go ahead and parse the flags.
         */
-       for (i=1; i<num_items; ++i) {
-               strcpy(whichflags, itemlist[i]);
+       for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy? 
+               strcpy(whichflags, Cmd->Params[i].Key);
                if (whichflags[0]=='(') {
                        safestrncpy(whichflags, &whichflags[1], 
                                sizeof whichflags);
@@ -155,18 +143,18 @@ void imap_do_store(int num_items, char **itemlist) {
                }
        }
 
-       if (IMAP->num_msgs > 0) {
-               for (i = 0; i < IMAP->num_msgs; ++i) {
-                       if (IMAP->flags[i] & IMAP_SELECTED) {
+       if (Imap->num_msgs > 0) {
+               for (i = 0; i < Imap->num_msgs; ++i) {
+                       if (Imap->flags[i] & IMAP_SELECTED) {
                                last_item_twiddled = i;
 
-                               ss_msglist[num_ss++] = IMAP->msgids[i];
+                               ss_msglist[num_ss++] = Imap->msgids[i];
                                imap_do_store_msg(i, oper, bits_to_twiddle);
 
                                if (!silent) {
-                                       cprintf("* %d FETCH (", i+1);
+                                       IAPrintf("* %d FETCH (", i+1);
                                        imap_fetch_flags(i);
-                                       cprintf(")\r\n");
+                                       IAPuts(")\r\n");
                                }
 
                        }
@@ -180,7 +168,7 @@ void imap_do_store(int num_items, char **itemlist) {
 
                if (bits_to_twiddle & IMAP_SEEN) {
                        CtdlSetSeen(ss_msglist, num_ss,
-                               ((IMAP->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
+                               ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
                                ctdlsetseen_seen,
                                NULL, NULL
                        );
@@ -188,7 +176,7 @@ void imap_do_store(int num_items, char **itemlist) {
 
                if (bits_to_twiddle & IMAP_ANSWERED) {
                        CtdlSetSeen(ss_msglist, num_ss,
-                               ((IMAP->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
+                               ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
                                ctdlsetseen_answered,
                                NULL, NULL
                        );
@@ -197,15 +185,8 @@ void imap_do_store(int num_items, char **itemlist) {
        }
 
        free(ss_msglist);
-
-       /*
-        * The following two commands implement "instant expunge" if enabled.
-        */
-       if (config.c_instant_expunge) {
-               imap_do_expunge();
-               imap_rescan_msgids();
-       }
-
+       imap_do_expunge();              // Citadel always expunges immediately.
+       imap_rescan_msgids();
 }
 
 
@@ -213,13 +194,11 @@ void imap_do_store(int num_items, char **itemlist) {
  * This function is called by the main command loop.
  */
 void imap_store(int num_parms, ConstStr *Params) {
-       char items[1024];
-       char *itemlist[256];
+       citimap_command Cmd;
        int num_items;
-       int i;
 
        if (num_parms < 3) {
-               cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
+               IReply("BAD invalid parameters");
                return;
        }
 
@@ -227,37 +206,37 @@ void imap_store(int num_parms, ConstStr *Params) {
                imap_pick_range(Params[2].Key, 0);
        }
        else {
-               cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
+               IReply("BAD invalid parameters");
                return;
        }
 
-       strcpy(items, "");
-       for (i=3; i<num_parms; ++i) {
-               strcat(items, Params[i].Key);
-               if (i < (num_parms-1)) strcat(items, " ");
-       }
+       memset(&Cmd, 0, sizeof(citimap_command));
+       Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
+       MakeStringOf(Cmd.CmdBuf, 3);
 
-       num_items = imap_extract_data_items(itemlist, items);
+       num_items = imap_extract_data_items(&Cmd);
        if (num_items < 1) {
-               cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
+               IReply("BAD invalid data item list");
+               FreeStrBuf(&Cmd.CmdBuf);
+               free(Cmd.Params);
                return;
        }
 
-       imap_do_store(num_items, itemlist);
-       cprintf("%s OK STORE completed\r\n", Params[0].Key);
+       imap_do_store(&Cmd);
+       IReply("OK STORE completed");
+       FreeStrBuf(&Cmd.CmdBuf);
+       free(Cmd.Params);
 }
 
 /*
  * This function is called by the main command loop.
  */
 void imap_uidstore(int num_parms, ConstStr *Params) {
-       char items[1024];
-       char *itemlist[256];
+       citimap_command Cmd;
        int num_items;
-       int i;
 
        if (num_parms < 4) {
-               cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
+               IReply("BAD invalid parameters");
                return;
        }
 
@@ -265,24 +244,26 @@ void imap_uidstore(int num_parms, ConstStr *Params) {
                imap_pick_range(Params[3].Key, 1);
        }
        else {
-               cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
+               IReply("BAD invalid parameters");
                return;
        }
 
-       strcpy(items, "");
-       for (i=4; i<num_parms; ++i) {
-               strcat(items, Params[i].Key);
-               if (i < (num_parms-1)) strcat(items, " ");
-       }
+       memset(&Cmd, 0, sizeof(citimap_command));
+       Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
+       MakeStringOf(Cmd.CmdBuf, 4);
 
-       num_items = imap_extract_data_items(itemlist, items);
+       num_items = imap_extract_data_items(&Cmd);
        if (num_items < 1) {
-               cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
+               IReply("BAD invalid data item list");
+               FreeStrBuf(&Cmd.CmdBuf);
+               free(Cmd.Params);
                return;
        }
 
-       imap_do_store(num_items, itemlist);
-       cprintf("%s OK UID STORE completed\r\n", Params[0].Key);
+       imap_do_store(&Cmd);
+       IReply("OK UID STORE completed");
+       FreeStrBuf(&Cmd.CmdBuf);
+       free(Cmd.Params);
 }