]> code.citadel.org Git - citadel.git/blob - citadel/imap_store.c
8ba5ba5e713d348128f65eb167b702ca27be2626
[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.  Leave it to an
64  * idiot like Mark Crispin to make things unnecessarily complicated.
65  */
66 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
67         int silent = 0;
68
69         if (!strncasecmp(oper, "FLAGS", 5)) {
70                 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
71                 IMAP->flags[seq] |= bits_to_twiddle;
72                 silent = strncasecmp(&oper[5], ".SILENT", 7);
73         }
74         else if (!strncasecmp(oper, "+FLAGS", 6)) {
75                 IMAP->flags[seq] |= bits_to_twiddle;
76                 silent = strncasecmp(&oper[6], ".SILENT", 7);
77         }
78         else if (!strncasecmp(oper, "-FLAGS", 6)) {
79                 IMAP->flags[seq] &= (~bits_to_twiddle);
80                 silent = strncasecmp(&oper[6], ".SILENT", 7);
81         }
82
83         if (bits_to_twiddle & IMAP_SEEN) {
84                 CtdlSetSeen(IMAP->msgids[seq],
85                                 ((IMAP->flags[seq] & IMAP_SEEN) ? 1 : 0) );
86         }
87
88         /* 'silent' is actually the value returned from a strncasecmp() so
89          * we want that option only if its value is zero.  Seems backwards
90          * but that's the way it's supposed to be.
91          */
92         if (silent) {
93                 cprintf("* %d FETCH (", seq+1);
94                 imap_fetch_flags(seq);
95                 cprintf(")\r\n");
96         }
97 }
98
99
100
101 /*
102  * imap_store() calls imap_do_store() to perform the actual bit twiddling
103  * on the flags.
104  */
105 void imap_do_store(int num_items, char **itemlist) {
106         int i, j;
107         unsigned int bits_to_twiddle = 0;
108         char *oper;
109         char flag[SIZ];
110         char whichflags[SIZ];
111         char num_flags;
112
113         if (num_items < 2) return;
114         oper = itemlist[0];
115
116         for (i=1; i<num_items; ++i) {
117                 strcpy(whichflags, itemlist[i]);
118                 if (whichflags[0]=='(') strcpy(whichflags, &whichflags[1]);
119                 if (whichflags[strlen(whichflags)-1]==')') {
120                         whichflags[strlen(whichflags)-1]=0;
121                 }
122                 striplt(whichflags);
123
124                 /* A client might twiddle more than one bit at a time */
125                 num_flags = num_tokens(whichflags, ' ');
126                 for (j=0; j<num_flags; ++j) {
127                         extract_token(flag, whichflags, j, ' ');
128
129                         if (!strcasecmp(flag, "\\Deleted")) {
130                                 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
131                                         bits_to_twiddle |= IMAP_DELETED;
132                                 }
133                         }
134                         if (!strcasecmp(flag, "\\Seen")) {
135                                 bits_to_twiddle |= IMAP_SEEN;
136                         }
137                 }
138         }
139
140         if (IMAP->num_msgs > 0) {
141                 for (i = 0; i < IMAP->num_msgs; ++i) {
142                         if (IMAP->flags[i] & IMAP_SELECTED) {
143                                 imap_do_store_msg(i, oper, bits_to_twiddle);
144                         }
145                 }
146         }
147 }
148
149
150 /*
151  * This function is called by the main command loop.
152  */
153 void imap_store(int num_parms, char *parms[]) {
154         char items[1024];
155         char *itemlist[256];
156         int num_items;
157         int i;
158
159         if (num_parms < 3) {
160                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
161                 return;
162         }
163
164         if (imap_is_message_set(parms[2])) {
165                 imap_pick_range(parms[2], 0);
166         }
167         else {
168                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
169                 return;
170         }
171
172         strcpy(items, "");
173         for (i=3; i<num_parms; ++i) {
174                 strcat(items, parms[i]);
175                 if (i < (num_parms-1)) strcat(items, " ");
176         }
177
178         num_items = imap_extract_data_items(itemlist, items);
179         if (num_items < 1) {
180                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
181                 return;
182         }
183
184         imap_do_store(num_items, itemlist);
185         cprintf("%s OK STORE completed\r\n", parms[0]);
186 }
187
188 /*
189  * This function is called by the main command loop.
190  */
191 void imap_uidstore(int num_parms, char *parms[]) {
192         char items[1024];
193         char *itemlist[256];
194         int num_items;
195         int i;
196
197         if (num_parms < 4) {
198                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
199                 return;
200         }
201
202         if (imap_is_message_set(parms[3])) {
203                 imap_pick_range(parms[3], 1);
204         }
205         else {
206                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
207                 return;
208         }
209
210         strcpy(items, "");
211         for (i=4; i<num_parms; ++i) {
212                 strcat(items, parms[i]);
213                 if (i < (num_parms-1)) strcat(items, " ");
214         }
215
216         num_items = imap_extract_data_items(itemlist, items);
217         if (num_items < 1) {
218                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
219                 return;
220         }
221
222         imap_do_store(num_items, itemlist);
223         cprintf("%s OK UID STORE completed\r\n", parms[0]);
224 }
225
226