Instant expunge is now the only mode of operation. The expunge semantics have been...
[citadel.git] / citadel / modules / imap / imap_store.c
1 /*
2  * Implements the STORE command in IMAP.
3  *
4  * Copyright (c) 2001-2009 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "ctdl_module.h"
22
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42
43 #include <sys/wait.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "sysdep_decls.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "user_ops.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "internet_addressing.h"
58 #include "serv_imap.h"
59 #include "imap_tools.h"
60 #include "imap_fetch.h"
61 #include "imap_store.h"
62 #include "genstamp.h"
63
64
65 /*
66  * imap_do_store() calls imap_do_store_msg() to tweak the settings of
67  * an individual message.
68  *
69  * We also implement the ".SILENT" protocol option here.  :(
70  */
71 void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
72         citimap *Imap = IMAP;
73
74         if (!strncasecmp(oper, "FLAGS", 5)) {
75                 Imap->flags[seq] &= IMAP_MASK_SYSTEM;
76                 Imap->flags[seq] |= bits_to_twiddle;
77         }
78         else if (!strncasecmp(oper, "+FLAGS", 6)) {
79                 Imap->flags[seq] |= bits_to_twiddle;
80         }
81         else if (!strncasecmp(oper, "-FLAGS", 6)) {
82                 Imap->flags[seq] &= (~bits_to_twiddle);
83         }
84 }
85
86
87 /*
88  * imap_store() calls imap_do_store() to perform the actual bit twiddling
89  * on the flags.
90  */
91 void imap_do_store(citimap_command *Cmd) {
92         int i, j;
93         unsigned int bits_to_twiddle = 0;
94         const char *oper;
95         char flag[32];
96         char whichflags[256];
97         char num_flags;
98         int silent = 0;
99         long *ss_msglist;
100         int num_ss = 0;
101         int last_item_twiddled = (-1);
102         citimap *Imap = IMAP;
103
104         if (Cmd->num_parms < 2) return;
105         oper = Cmd->Params[0].Key;
106         if (cbmstrcasestr(oper, ".SILENT")) {
107                 silent = 1;
108         }
109
110         /*
111          * ss_msglist is an array of message numbers to manipulate.  We
112          * are going to supply this array to CtdlSetSeen() later.
113          */
114         ss_msglist = malloc(Imap->num_msgs * sizeof(long));
115         if (ss_msglist == NULL) return;
116
117         /*
118          * Ok, go ahead and parse the flags.
119          */
120         for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy? 
121                 strcpy(whichflags, Cmd->Params[i].Key);
122                 if (whichflags[0]=='(') {
123                         safestrncpy(whichflags, &whichflags[1], 
124                                 sizeof whichflags);
125                 }
126                 if (whichflags[strlen(whichflags)-1]==')') {
127                         whichflags[strlen(whichflags)-1]=0;
128                 }
129                 striplt(whichflags);
130
131                 /* A client might twiddle more than one bit at a time.
132                  * Note that we check for the flag names without the leading
133                  * backslash because imap_parameterize() strips them out.
134                  */
135                 num_flags = num_tokens(whichflags, ' ');
136                 for (j=0; j<num_flags; ++j) {
137                         extract_token(flag, whichflags, j, ' ', sizeof flag);
138
139                         if ((!strcasecmp(flag, "\\Deleted"))
140                            || (!strcasecmp(flag, "Deleted"))) {
141                                 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
142                                         bits_to_twiddle |= IMAP_DELETED;
143                                 }
144                         }
145                         if ((!strcasecmp(flag, "\\Seen"))
146                            || (!strcasecmp(flag, "Seen"))) {
147                                 bits_to_twiddle |= IMAP_SEEN;
148                         }
149                         if ((!strcasecmp(flag, "\\Answered")) 
150                            || (!strcasecmp(flag, "Answered"))) {
151                                 bits_to_twiddle |= IMAP_ANSWERED;
152                         }
153                 }
154         }
155
156         if (Imap->num_msgs > 0) {
157                 for (i = 0; i < Imap->num_msgs; ++i) {
158                         if (Imap->flags[i] & IMAP_SELECTED) {
159                                 last_item_twiddled = i;
160
161                                 ss_msglist[num_ss++] = Imap->msgids[i];
162                                 imap_do_store_msg(i, oper, bits_to_twiddle);
163
164                                 if (!silent) {
165                                         IAPrintf("* %d FETCH (", i+1);
166                                         imap_fetch_flags(i);
167                                         IAPuts(")\r\n");
168                                 }
169
170                         }
171                 }
172         }
173
174         /*
175          * Now manipulate the database -- all in one shot.
176          */
177         if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
178
179                 if (bits_to_twiddle & IMAP_SEEN) {
180                         CtdlSetSeen(ss_msglist, num_ss,
181                                 ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
182                                 ctdlsetseen_seen,
183                                 NULL, NULL
184                         );
185                 }
186
187                 if (bits_to_twiddle & IMAP_ANSWERED) {
188                         CtdlSetSeen(ss_msglist, num_ss,
189                                 ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
190                                 ctdlsetseen_answered,
191                                 NULL, NULL
192                         );
193                 }
194
195         }
196
197         free(ss_msglist);
198         imap_do_expunge();              // Citadel always expunges immediately.
199         imap_rescan_msgids();
200 }
201
202
203 /*
204  * This function is called by the main command loop.
205  */
206 void imap_store(int num_parms, ConstStr *Params) {
207         citimap_command Cmd;
208         int num_items;
209
210         if (num_parms < 3) {
211                 IReply("BAD invalid parameters");
212                 return;
213         }
214
215         if (imap_is_message_set(Params[2].Key)) {
216                 imap_pick_range(Params[2].Key, 0);
217         }
218         else {
219                 IReply("BAD invalid parameters");
220                 return;
221         }
222
223         memset(&Cmd, 0, sizeof(citimap_command));
224         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
225         MakeStringOf(Cmd.CmdBuf, 3);
226
227         num_items = imap_extract_data_items(&Cmd);
228         if (num_items < 1) {
229                 IReply("BAD invalid data item list");
230                 FreeStrBuf(&Cmd.CmdBuf);
231                 free(Cmd.Params);
232                 return;
233         }
234
235         imap_do_store(&Cmd);
236         IReply("OK STORE completed");
237         FreeStrBuf(&Cmd.CmdBuf);
238         free(Cmd.Params);
239 }
240
241 /*
242  * This function is called by the main command loop.
243  */
244 void imap_uidstore(int num_parms, ConstStr *Params) {
245         citimap_command Cmd;
246         int num_items;
247
248         if (num_parms < 4) {
249                 IReply("BAD invalid parameters");
250                 return;
251         }
252
253         if (imap_is_message_set(Params[3].Key)) {
254                 imap_pick_range(Params[3].Key, 1);
255         }
256         else {
257                 IReply("BAD invalid parameters");
258                 return;
259         }
260
261         memset(&Cmd, 0, sizeof(citimap_command));
262         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
263         MakeStringOf(Cmd.CmdBuf, 4);
264
265         num_items = imap_extract_data_items(&Cmd);
266         if (num_items < 1) {
267                 IReply("BAD invalid data item list");
268                 FreeStrBuf(&Cmd.CmdBuf);
269                 free(Cmd.Params);
270                 return;
271         }
272
273         imap_do_store(&Cmd);
274         IReply("OK UID STORE completed");
275         FreeStrBuf(&Cmd.CmdBuf);
276         free(Cmd.Params);
277 }
278
279