4 * Implements the STORE command in IMAP.
17 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
36 #include "sysdep_decls.h"
37 #include "citserver.h"
40 #include "serv_extensions.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"
60 * imap_do_store() calls imap_do_store_msg() to tweak the settings of
61 * an individual message.
63 * We also implement the ".SILENT" protocol option here. :(
65 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
68 if (!strncasecmp(oper, "FLAGS", 5)) {
69 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
70 IMAP->flags[seq] |= bits_to_twiddle;
72 else if (!strncasecmp(oper, "+FLAGS", 6)) {
73 IMAP->flags[seq] |= bits_to_twiddle;
75 else if (!strncasecmp(oper, "-FLAGS", 6)) {
76 IMAP->flags[seq] &= (~bits_to_twiddle);
83 * imap_store() calls imap_do_store() to perform the actual bit twiddling
86 void imap_do_store(int num_items, char **itemlist) {
88 unsigned int bits_to_twiddle = 0;
96 int last_item_twiddled = (-1);
98 if (num_items < 2) return;
100 if (bmstrcasestr(oper, ".SILENT")) {
105 * ss_msglist is an array of message numbers to manipulate. We
106 * are going to supply this array to CtdlSetSeen() later.
108 ss_msglist = malloc(IMAP->num_msgs * sizeof(long));
109 if (ss_msglist == NULL) return;
112 * Ok, go ahead and parse the flags.
114 for (i=1; i<num_items; ++i) {
115 strcpy(whichflags, itemlist[i]);
116 if (whichflags[0]=='(') {
117 safestrncpy(whichflags, &whichflags[1],
120 if (whichflags[strlen(whichflags)-1]==')') {
121 whichflags[strlen(whichflags)-1]=0;
125 /* A client might twiddle more than one bit at a time.
126 * Note that we check for the flag names without the leading
127 * backslash because imap_parameterize() strips them out.
129 num_flags = num_tokens(whichflags, ' ');
130 for (j=0; j<num_flags; ++j) {
131 extract_token(flag, whichflags, j, ' ', sizeof flag);
133 if ((!strcasecmp(flag, "\\Deleted"))
134 || (!strcasecmp(flag, "Deleted"))) {
135 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
136 bits_to_twiddle |= IMAP_DELETED;
139 if ((!strcasecmp(flag, "\\Seen"))
140 || (!strcasecmp(flag, "Seen"))) {
141 bits_to_twiddle |= IMAP_SEEN;
143 if ((!strcasecmp(flag, "\\Answered"))
144 || (!strcasecmp(flag, "\\Answered"))) {
145 bits_to_twiddle |= IMAP_ANSWERED;
150 if (IMAP->num_msgs > 0) {
151 for (i = 0; i < IMAP->num_msgs; ++i) {
152 if (IMAP->flags[i] & IMAP_SELECTED) {
153 last_item_twiddled = i;
155 ss_msglist[num_ss++] = IMAP->msgids[i];
156 imap_do_store_msg(i, oper, bits_to_twiddle);
159 cprintf("* %d FETCH (", i+1);
169 * Now manipulate the database -- all in one shot.
171 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
173 if (bits_to_twiddle & IMAP_SEEN) {
174 CtdlSetSeen(ss_msglist, num_ss,
175 ((IMAP->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
181 if (bits_to_twiddle & IMAP_ANSWERED) {
182 CtdlSetSeen(ss_msglist, num_ss,
183 ((IMAP->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
184 ctdlsetseen_answered,
194 * The following two commands implement "instant expunge" if enabled.
196 if (config.c_instant_expunge) {
198 imap_rescan_msgids();
205 * This function is called by the main command loop.
207 void imap_store(int num_parms, char *parms[]) {
214 cprintf("%s BAD invalid parameters\r\n", parms[0]);
218 if (imap_is_message_set(parms[2])) {
219 imap_pick_range(parms[2], 0);
222 cprintf("%s BAD invalid parameters\r\n", parms[0]);
227 for (i=3; i<num_parms; ++i) {
228 strcat(items, parms[i]);
229 if (i < (num_parms-1)) strcat(items, " ");
232 num_items = imap_extract_data_items(itemlist, items);
234 cprintf("%s BAD invalid data item list\r\n", parms[0]);
238 imap_do_store(num_items, itemlist);
239 cprintf("%s OK STORE completed\r\n", parms[0]);
243 * This function is called by the main command loop.
245 void imap_uidstore(int num_parms, char *parms[]) {
252 cprintf("%s BAD invalid parameters\r\n", parms[0]);
256 if (imap_is_message_set(parms[3])) {
257 imap_pick_range(parms[3], 1);
260 cprintf("%s BAD invalid parameters\r\n", parms[0]);
265 for (i=4; i<num_parms; ++i) {
266 strcat(items, parms[i]);
267 if (i < (num_parms-1)) strcat(items, " ");
270 num_items = imap_extract_data_items(itemlist, items);
272 cprintf("%s BAD invalid data item list\r\n", parms[0]);
276 imap_do_store(num_items, itemlist);
277 cprintf("%s OK UID STORE completed\r\n", parms[0]);