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;
71 silent = strncasecmp(&oper[5], ".SILENT", 7);
73 else if (!strncasecmp(oper, "+FLAGS", 6)) {
74 IMAP->flags[seq] |= bits_to_twiddle;
75 silent = strncasecmp(&oper[6], ".SILENT", 7);
77 else if (!strncasecmp(oper, "-FLAGS", 6)) {
78 IMAP->flags[seq] &= (~bits_to_twiddle);
79 silent = strncasecmp(&oper[6], ".SILENT", 7);
82 if (bits_to_twiddle & IMAP_SEEN) {
83 CtdlSetSeen(&IMAP->msgids[seq], 1,
84 ((IMAP->flags[seq] & IMAP_SEEN) ? 1 : 0),
89 if (bits_to_twiddle & IMAP_ANSWERED) {
90 CtdlSetSeen(&IMAP->msgids[seq], 1,
91 ((IMAP->flags[seq] & IMAP_ANSWERED) ? 1 : 0),
97 /* 'silent' is actually the value returned from a strncasecmp() so
98 * we want that option only if its value is zero. Seems backwards
99 * but that's the way it's supposed to be.
102 cprintf("* %d FETCH (", seq+1);
103 imap_fetch_flags(seq);
111 * imap_store() calls imap_do_store() to perform the actual bit twiddling
114 void imap_do_store(int num_items, char **itemlist) {
116 unsigned int bits_to_twiddle = 0;
119 char whichflags[256];
122 if (num_items < 2) return;
125 for (i=1; i<num_items; ++i) {
126 strcpy(whichflags, itemlist[i]);
127 if (whichflags[0]=='(') safestrncpy(whichflags, &whichflags[1], sizeof whichflags);
128 if (whichflags[strlen(whichflags)-1]==')') {
129 whichflags[strlen(whichflags)-1]=0;
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.
137 num_flags = num_tokens(whichflags, ' ');
138 for (j=0; j<num_flags; ++j) {
139 extract_token(flag, whichflags, j, ' ', sizeof flag);
141 if ((!strcasecmp(flag, "\\Deleted"))
142 || (!strcasecmp(flag, "Deleted"))) {
143 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
144 bits_to_twiddle |= IMAP_DELETED;
147 if ((!strcasecmp(flag, "\\Seen"))
148 || (!strcasecmp(flag, "Seen"))) {
149 bits_to_twiddle |= IMAP_SEEN;
151 if ((!strcasecmp(flag, "\\Answered"))
152 || (!strcasecmp(flag, "\\Answered"))) {
153 bits_to_twiddle |= IMAP_ANSWERED;
158 if (IMAP->num_msgs > 0) {
159 for (i = 0; i < IMAP->num_msgs; ++i) {
160 if (IMAP->flags[i] & IMAP_SELECTED) {
161 imap_do_store_msg(i, oper, bits_to_twiddle);
167 * The following two commands implement "instant expunge" if enabled.
169 if (config.c_instant_expunge) {
171 imap_rescan_msgids();
178 * This function is called by the main command loop.
180 void imap_store(int num_parms, char *parms[]) {
187 cprintf("%s BAD invalid parameters\r\n", parms[0]);
191 if (imap_is_message_set(parms[2])) {
192 imap_pick_range(parms[2], 0);
195 cprintf("%s BAD invalid parameters\r\n", parms[0]);
200 for (i=3; i<num_parms; ++i) {
201 strcat(items, parms[i]);
202 if (i < (num_parms-1)) strcat(items, " ");
205 num_items = imap_extract_data_items(itemlist, items);
207 cprintf("%s BAD invalid data item list\r\n", parms[0]);
211 imap_do_store(num_items, itemlist);
212 cprintf("%s OK STORE completed\r\n", parms[0]);
216 * This function is called by the main command loop.
218 void imap_uidstore(int num_parms, char *parms[]) {
225 cprintf("%s BAD invalid parameters\r\n", parms[0]);
229 if (imap_is_message_set(parms[3])) {
230 imap_pick_range(parms[3], 1);
233 cprintf("%s BAD invalid parameters\r\n", parms[0]);
238 for (i=4; i<num_parms; ++i) {
239 strcat(items, parms[i]);
240 if (i < (num_parms-1)) strcat(items, " ");
243 num_items = imap_extract_data_items(itemlist, items);
245 cprintf("%s BAD invalid data item list\r\n", parms[0]);
249 imap_do_store(num_items, itemlist);
250 cprintf("%s OK UID STORE completed\r\n", parms[0]);