* move policy.c into modules/expire/expire_policy.c, since it just controls this.
[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 "database.h"
58 #include "msgbase.h"
59 #include "internet_addressing.h"
60 #include "imap_tools.h"
61 #include "serv_imap.h"
62 #include "imap_fetch.h"
63 #include "imap_store.h"
64 #include "genstamp.h"
65
66
67 /*
68  * imap_do_store() calls imap_do_store_msg() to tweak the settings of
69  * an individual message.
70  *
71  * We also implement the ".SILENT" protocol option here.  :(
72  */
73 void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
74
75
76         if (!strncasecmp(oper, "FLAGS", 5)) {
77                 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
78                 IMAP->flags[seq] |= bits_to_twiddle;
79         }
80         else if (!strncasecmp(oper, "+FLAGS", 6)) {
81                 IMAP->flags[seq] |= bits_to_twiddle;
82         }
83         else if (!strncasecmp(oper, "-FLAGS", 6)) {
84                 IMAP->flags[seq] &= (~bits_to_twiddle);
85         }
86 }
87
88
89 /*
90  * imap_store() calls imap_do_store() to perform the actual bit twiddling
91  * on the flags.
92  */
93 void imap_do_store(citimap_command *Cmd) {
94         int i, j;
95         unsigned int bits_to_twiddle = 0;
96         const char *oper;
97         char flag[32];
98         char whichflags[256];
99         char num_flags;
100         int silent = 0;
101         long *ss_msglist;
102         int num_ss = 0;
103         int last_item_twiddled = (-1);
104         citimap *Imap = IMAP;
105
106         if (Cmd->num_parms < 2) return;
107         oper = Cmd->Params[0].Key;
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<Cmd->num_parms; ++i) {///TODO: why strcpy? 
123                 strcpy(whichflags, Cmd->Params[i].Key);
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, ConstStr *Params) {
216         citimap_command Cmd;
217         int num_items;
218
219         if (num_parms < 3) {
220                 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
221                 return;
222         }
223
224         if (imap_is_message_set(Params[2].Key)) {
225                 imap_pick_range(Params[2].Key, 0);
226         }
227         else {
228                 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
229                 return;
230         }
231
232         memset(&Cmd, 0, sizeof(citimap_command));
233         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
234         MakeStringOf(Cmd.CmdBuf, 3);
235
236         num_items = imap_extract_data_items(&Cmd);
237         if (num_items < 1) {
238                 cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
239                 FreeStrBuf(&Cmd.CmdBuf);
240                 free(Cmd.Params);
241                 return;
242         }
243
244         imap_do_store(&Cmd);
245         cprintf("%s OK STORE completed\r\n", Params[0].Key);
246         FreeStrBuf(&Cmd.CmdBuf);
247         free(Cmd.Params);
248 }
249
250 /*
251  * This function is called by the main command loop.
252  */
253 void imap_uidstore(int num_parms, ConstStr *Params) {
254         citimap_command Cmd;
255         int num_items;
256
257         if (num_parms < 4) {
258                 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
259                 return;
260         }
261
262         if (imap_is_message_set(Params[3].Key)) {
263                 imap_pick_range(Params[3].Key, 1);
264         }
265         else {
266                 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
267                 return;
268         }
269
270         memset(&Cmd, 0, sizeof(citimap_command));
271         Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
272         MakeStringOf(Cmd.CmdBuf, 4);
273
274         num_items = imap_extract_data_items(&Cmd);
275         if (num_items < 1) {
276                 cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
277                 FreeStrBuf(&Cmd.CmdBuf);
278                 free(Cmd.Params);
279                 return;
280         }
281
282         imap_do_store(&Cmd);
283         cprintf("%s OK UID STORE completed\r\n", Params[0].Key);
284         FreeStrBuf(&Cmd.CmdBuf);
285         free(Cmd.Params);
286 }
287
288