8c4188bdbb93118c01f8b881662f4342ff3457f1
[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 "room_ops.h"
57 #include "msgbase.h"
58 #include "internet_addressing.h"
59 #include "serv_imap.h"
60 #include "imap_tools.h"
61 #include "imap_fetch.h"
62 #include "imap_store.h"
63 #include "genstamp.h"
64
65
66 /*
67  * imap_do_store() calls imap_do_store_msg() to tweak the settings of
68  * an individual message.
69  *
70  * We also implement the ".SILENT" protocol option here.  :(
71  */
72 void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
73         citimap *Imap = IMAP;
74
75         if (!strncasecmp(oper, "FLAGS", 5)) {
76                 Imap->flags[seq] &= IMAP_MASK_SYSTEM;
77                 Imap->flags[seq] |= bits_to_twiddle;
78         }
79         else if (!strncasecmp(oper, "+FLAGS", 6)) {
80                 Imap->flags[seq] |= bits_to_twiddle;
81         }
82         else if (!strncasecmp(oper, "-FLAGS", 6)) {
83                 Imap->flags[seq] &= (~bits_to_twiddle);
84         }
85 }
86
87
88 /*
89  * imap_store() calls imap_do_store() to perform the actual bit twiddling
90  * on the flags.
91  */
92 void imap_do_store(citimap_command *Cmd) {
93         int i, j;
94         unsigned int bits_to_twiddle = 0;
95         const char *oper;
96         char flag[32];
97         char whichflags[256];
98         char num_flags;
99         int silent = 0;
100         long *ss_msglist;
101         int num_ss = 0;
102         int last_item_twiddled = (-1);
103         citimap *Imap = IMAP;
104
105         if (Cmd->num_parms < 2) return;
106         oper = Cmd->Params[0].Key;
107         if (cbmstrcasestr(oper, ".SILENT")) {
108                 silent = 1;
109         }
110
111         /*
112          * ss_msglist is an array of message numbers to manipulate.  We
113          * are going to supply this array to CtdlSetSeen() later.
114          */
115         ss_msglist = malloc(Imap->num_msgs * sizeof(long));
116         if (ss_msglist == NULL) return;
117
118         /*
119          * Ok, go ahead and parse the flags.
120          */
121         for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy? 
122                 strcpy(whichflags, Cmd->Params[i].Key);
123                 if (whichflags[0]=='(') {
124                         safestrncpy(whichflags, &whichflags[1], 
125                                 sizeof whichflags);
126                 }
127                 if (whichflags[strlen(whichflags)-1]==')') {
128                         whichflags[strlen(whichflags)-1]=0;
129                 }
130                 striplt(whichflags);
131
132                 /* A client might twiddle more than one bit at a time.
133                  * Note that we check for the flag names without the leading
134                  * backslash because imap_parameterize() strips them out.
135                  */
136                 num_flags = num_tokens(whichflags, ' ');
137                 for (j=0; j<num_flags; ++j) {
138                         extract_token(flag, whichflags, j, ' ', sizeof flag);
139
140                         if ((!strcasecmp(flag, "\\Deleted"))
141                            || (!strcasecmp(flag, "Deleted"))) {
142                                 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
143                                         bits_to_twiddle |= IMAP_DELETED;
144                                 }
145                         }
146                         if ((!strcasecmp(flag, "\\Seen"))
147                            || (!strcasecmp(flag, "Seen"))) {
148                                 bits_to_twiddle |= IMAP_SEEN;
149                         }
150                         if ((!strcasecmp(flag, "\\Answered")) 
151                            || (!strcasecmp(flag, "Answered"))) {
152                                 bits_to_twiddle |= IMAP_ANSWERED;
153                         }
154                 }
155         }
156
157         if (Imap->num_msgs > 0) {
158                 for (i = 0; i < Imap->num_msgs; ++i) {
159                         if (Imap->flags[i] & IMAP_SELECTED) {
160                                 last_item_twiddled = i;
161
162                                 ss_msglist[num_ss++] = Imap->msgids[i];
163                                 imap_do_store_msg(i, oper, bits_to_twiddle);
164
165                                 if (!silent) {
166                                         IAPrintf("* %d FETCH (", i+1);
167                                         imap_fetch_flags(i);
168                                         IAPuts(")\r\n");
169                                 }
170
171                         }
172                 }
173         }
174
175         /*
176          * Now manipulate the database -- all in one shot.
177          */
178         if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
179
180                 if (bits_to_twiddle & IMAP_SEEN) {
181                         CtdlSetSeen(ss_msglist, num_ss,
182                                 ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
183                                 ctdlsetseen_seen,
184                                 NULL, NULL
185                         );
186                 }
187
188                 if (bits_to_twiddle & IMAP_ANSWERED) {
189                         CtdlSetSeen(ss_msglist, num_ss,
190                                 ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
191                                 ctdlsetseen_answered,
192                                 NULL, NULL
193                         );
194                 }
195
196         }
197
198         free(ss_msglist);
199         imap_do_expunge();              // Citadel always expunges immediately.
200         imap_rescan_msgids();
201 }
202
203
204 /*
205  * This function is called by the main command loop.
206  */
207 void imap_store(int num_parms, ConstStr *Params) {
208         citimap_command Cmd;
209         int num_items;
210
211         if (num_parms < 3) {
212                 IReply("BAD invalid parameters");
213                 return;
214         }
215
216         if (imap_is_message_set(Params[2].Key)) {
217                 imap_pick_range(Params[2].Key, 0);
218         }
219         else {
220                 IReply("BAD invalid parameters");
221                 return;
222         }
223
224         memset(&Cmd, 0, sizeof(citimap_command));
225         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
226         MakeStringOf(Cmd.CmdBuf, 3);
227
228         num_items = imap_extract_data_items(&Cmd);
229         if (num_items < 1) {
230                 IReply("BAD invalid data item list");
231                 FreeStrBuf(&Cmd.CmdBuf);
232                 free(Cmd.Params);
233                 return;
234         }
235
236         imap_do_store(&Cmd);
237         IReply("OK STORE completed");
238         FreeStrBuf(&Cmd.CmdBuf);
239         free(Cmd.Params);
240 }
241
242 /*
243  * This function is called by the main command loop.
244  */
245 void imap_uidstore(int num_parms, ConstStr *Params) {
246         citimap_command Cmd;
247         int num_items;
248
249         if (num_parms < 4) {
250                 IReply("BAD invalid parameters");
251                 return;
252         }
253
254         if (imap_is_message_set(Params[3].Key)) {
255                 imap_pick_range(Params[3].Key, 1);
256         }
257         else {
258                 IReply("BAD invalid parameters");
259                 return;
260         }
261
262         memset(&Cmd, 0, sizeof(citimap_command));
263         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
264         MakeStringOf(Cmd.CmdBuf, 4);
265
266         num_items = imap_extract_data_items(&Cmd);
267         if (num_items < 1) {
268                 IReply("BAD invalid data item list");
269                 FreeStrBuf(&Cmd.CmdBuf);
270                 free(Cmd.Params);
271                 return;
272         }
273
274         imap_do_store(&Cmd);
275         IReply("OK UID STORE completed");
276         FreeStrBuf(&Cmd.CmdBuf);
277         free(Cmd.Params);
278 }
279
280