* Probable completion of STATUS, COPY, STORE, and EXPUNGE commands in IMAP
[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 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <limits.h>
23 #include "citadel.h"
24 #include "server.h"
25 #include <time.h>
26 #include "sysdep_decls.h"
27 #include "citserver.h"
28 #include "support.h"
29 #include "config.h"
30 #include "dynloader.h"
31 #include "room_ops.h"
32 #include "user_ops.h"
33 #include "policy.h"
34 #include "database.h"
35 #include "msgbase.h"
36 #include "tools.h"
37 #include "internet_addressing.h"
38 #include "serv_imap.h"
39 #include "imap_tools.h"
40 #include "imap_fetch.h"
41 #include "imap_store.h"
42 #include "genstamp.h"
43
44
45
46
47
48
49 /*
50  * imap_do_store() calls imap_do_store_msg() to output the deta of an
51  * individual message, once it has been successfully loaded from disk.
52  */
53 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
54         
55         if (!strncasecmp(oper, "FLAGS", 5)) {
56                 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
57                 IMAP->flags[seq] |= bits_to_twiddle;
58         }
59         else if (!strncasecmp(oper, "+FLAGS", 6)) {
60                 IMAP->flags[seq] |= bits_to_twiddle;
61         }
62         else if (!strncasecmp(oper, "-FLAGS", 6)) {
63                 IMAP->flags[seq] &= (~bits_to_twiddle);
64         }
65
66         cprintf("* %d FETCH (", seq+1);
67         imap_fetch_flags(seq);
68         cprintf(")\r\n");
69 }
70
71
72
73 /*
74  * imap_store() calls imap_do_store() to perform the actual bit twiddling
75  * on flags.  We brazenly ignore the ".silent" protocol option because it's not
76  * harmful to send the data anyway.  Fix it yourself if you don't like that.
77  */
78 void imap_do_store(int num_items, char **itemlist) {
79         int i;
80         unsigned int bits_to_twiddle = 0;
81         char *oper;
82         char flag[SIZ];
83
84         if (num_items < 2) return;
85         oper = itemlist[0];
86
87         for (i=1; i<num_items; ++i) {
88                 strcpy(flag, itemlist[i]);
89                 if (flag[0]=='(') strcpy(flag, &flag[1]);
90                 if (flag[strlen(flag)-1]==')') flag[strlen(flag)-1]=0;
91                 striplt(flag);
92
93                 if (!strcasecmp(flag, "\\Deleted")) {
94                         bits_to_twiddle |= IMAP_DELETED;
95                 }
96         }
97         
98         if (IMAP->num_msgs > 0) {
99                 for (i = 0; i < IMAP->num_msgs; ++i) {
100                         if (IMAP->flags[i] && IMAP_SELECTED) {
101                                 imap_do_store_msg(i, oper, bits_to_twiddle);
102                         }
103                 }
104         }
105 }
106
107
108 /*
109  * This function is called by the main command loop.
110  */
111 void imap_store(int num_parms, char *parms[]) {
112         char items[1024];
113         char *itemlist[256];
114         int num_items;
115         int i;
116
117         if (num_parms < 3) {
118                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
119                 return;
120         }
121
122         if (imap_is_message_set(parms[2])) {
123                 imap_pick_range(parms[2], 0);
124         }
125         else {
126                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
127                 return;
128         }
129
130         strcpy(items, "");
131         for (i=3; i<num_parms; ++i) {
132                 strcat(items, parms[i]);
133                 if (i < (num_parms-1)) strcat(items, " ");
134         }
135
136         num_items = imap_extract_data_items(itemlist, items);
137         if (num_items < 1) {
138                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
139                 return;
140         }
141
142         imap_do_store(num_items, itemlist);
143         cprintf("%s OK STORE completed\r\n", parms[0]);
144 }
145
146 /*
147  * This function is called by the main command loop.
148  */
149 void imap_uidstore(int num_parms, char *parms[]) {
150         char items[1024];
151         char *itemlist[256];
152         int num_items;
153         int i;
154
155         if (num_parms < 4) {
156                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
157                 return;
158         }
159
160         if (imap_is_message_set(parms[3])) {
161                 imap_pick_range(parms[3], 1);
162         }
163         else {
164                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
165                 return;
166         }
167
168         strcpy(items, "");
169         for (i=4; i<num_parms; ++i) {
170                 strcat(items, parms[i]);
171                 if (i < (num_parms-1)) strcat(items, " ");
172         }
173
174         num_items = imap_extract_data_items(itemlist, items);
175         if (num_items < 1) {
176                 cprintf("%s BAD invalid data item list\r\n", parms[0]);
177                 return;
178         }
179
180         imap_do_store(num_items, itemlist);
181         cprintf("%s OK UID STORE completed\r\n", parms[0]);
182 }
183
184