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