* extract_token() now expects to be supplied with the size of the
[citadel.git] / citadel / imap_store.c
1 /*
2  * $Id$
3  *
4  * Implements the STORE command in IMAP.
5  *
6  */
7
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/wait.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <limits.h>
34 #include "citadel.h"
35 #include "server.h"
36 #include "sysdep_decls.h"
37 #include "citserver.h"
38 #include "support.h"
39 #include "config.h"
40 #include "serv_extensions.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "policy.h"
44 #include "database.h"
45 #include "msgbase.h"
46 #include "tools.h"
47 #include "internet_addressing.h"
48 #include "serv_imap.h"
49 #include "imap_tools.h"
50 #include "imap_fetch.h"
51 #include "imap_store.h"
52 #include "genstamp.h"
53
54
55
56
57
58
59 /*
60  * imap_do_store() calls imap_do_store_msg() to tweak the settings of
61  * an individual message.
62  *
63  * We also implement the ".SILENT" protocol option here.  :(
64  */
65 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
66         int silent = 0;
67
68         if (!strncasecmp(oper, "FLAGS", 5)) {
69                 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
70                 IMAP->flags[seq] |= bits_to_twiddle;
71                 silent = strncasecmp(&oper[5], ".SILENT", 7);
72         }
73         else if (!strncasecmp(oper, "+FLAGS", 6)) {
74                 IMAP->flags[seq] |= bits_to_twiddle;
75                 silent = strncasecmp(&oper[6], ".SILENT", 7);
76         }
77         else if (!strncasecmp(oper, "-FLAGS", 6)) {
78                 IMAP->flags[seq] &= (~bits_to_twiddle);
79                 silent = strncasecmp(&oper[6], ".SILENT", 7);
80         }
81
82         if (bits_to_twiddle & IMAP_SEEN) {
83                 CtdlSetSeen(IMAP->msgids[seq],
84                                 ((IMAP->flags[seq] & IMAP_SEEN) ? 1 : 0),
85                                 ctdlsetseen_seen
86                 );
87         }
88         if (bits_to_twiddle & IMAP_ANSWERED) {
89                 CtdlSetSeen(IMAP->msgids[seq],
90                                 ((IMAP->flags[seq] & IMAP_ANSWERED) ? 1 : 0),
91                                 ctdlsetseen_answered
92                 );
93         }
94
95         /* 'silent' is actually the value returned from a strncasecmp() so
96          * we want that option only if its value is zero.  Seems backwards
97          * but that's the way it's supposed to be.
98          */
99         if (silent) {
100                 cprintf("* %d FETCH (", seq+1);
101                 imap_fetch_flags(seq);
102                 cprintf(")\r\n");
103         }
104 }
105
106
107
108 /*
109  * imap_store() calls imap_do_store() to perform the actual bit twiddling
110  * on the flags.
111  */
112 void imap_do_store(int num_items, char **itemlist) {
113         int i, j;
114         unsigned int bits_to_twiddle = 0;
115         char *oper;
116         char flag[32];
117         char whichflags[256];
118         char num_flags;
119
120         if (num_items < 2) return;
121         oper = itemlist[0];
122
123         for (i=1; i<num_items; ++i) {
124                 strcpy(whichflags, itemlist[i]);
125                 if (whichflags[0]=='(') safestrncpy(whichflags, &whichflags[1], sizeof whichflags);
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                                 imap_do_store_msg(i, oper, bits_to_twiddle);
160                         }
161                 }
162         }
163 }
164
165
166 /*
167  * This function is called by the main command loop.
168  */
169 void imap_store(int num_parms, char *parms[]) {
170         char items[1024];
171         char *itemlist[256];
172         int num_items;
173         int i;
174
175         if (num_parms < 3) {
176                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
177                 return;
178         }
179
180         if (imap_is_message_set(parms[2])) {
181                 imap_pick_range(parms[2], 0);
182         }
183         else {
184                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
185                 return;
186         }
187
188         strcpy(items, "");
189         for (i=3; i<num_parms; ++i) {
190                 strcat(items, parms[i]);
191                 if (i < (num_parms-1)) strcat(items, " ");
192         }
193
194         num_items = imap_extract_data_items(itemlist, items);
195         if (num_items < 1) {
196                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
197                 return;
198         }
199
200         imap_do_store(num_items, itemlist);
201         cprintf("%s OK STORE completed\r\n", parms[0]);
202 }
203
204 /*
205  * This function is called by the main command loop.
206  */
207 void imap_uidstore(int num_parms, char *parms[]) {
208         char items[1024];
209         char *itemlist[256];
210         int num_items;
211         int i;
212
213         if (num_parms < 4) {
214                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
215                 return;
216         }
217
218         if (imap_is_message_set(parms[3])) {
219                 imap_pick_range(parms[3], 1);
220         }
221         else {
222                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
223                 return;
224         }
225
226         strcpy(items, "");
227         for (i=4; i<num_parms; ++i) {
228                 lprintf(9, "item %d: %s\n", i, parms[i]);
229                 strcat(items, parms[i]);
230                 if (i < (num_parms-1)) strcat(items, " ");
231         }
232
233         num_items = imap_extract_data_items(itemlist, items);
234         if (num_items < 1) {
235                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
236                 return;
237         }
238
239         imap_do_store(num_items, itemlist);
240         cprintf("%s OK UID STORE completed\r\n", parms[0]);
241 }
242
243