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. Leave it to an
64 * idiot like Mark Crispin to make things unnecessarily complicated.
66 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
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);
74 else if (!strncasecmp(oper, "+FLAGS", 6)) {
75 IMAP->flags[seq] |= bits_to_twiddle;
76 silent = strncasecmp(&oper[6], ".SILENT", 7);
78 else if (!strncasecmp(oper, "-FLAGS", 6)) {
79 IMAP->flags[seq] &= (~bits_to_twiddle);
80 silent = strncasecmp(&oper[6], ".SILENT", 7);
83 if (bits_to_twiddle & IMAP_SEEN) {
84 CtdlSetSeen(IMAP->msgids[seq],
85 ((IMAP->flags[seq] & IMAP_SEEN) ? 1 : 0) );
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.
93 cprintf("* %d FETCH (", seq+1);
94 imap_fetch_flags(seq);
102 * imap_store() calls imap_do_store() to perform the actual bit twiddling
105 void imap_do_store(int num_items, char **itemlist) {
107 unsigned int bits_to_twiddle = 0;
110 char whichflags[SIZ];
113 if (num_items < 2) return;
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;
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, ' ');
129 if (!strcasecmp(flag, "\\Deleted")) {
130 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
131 bits_to_twiddle |= IMAP_DELETED;
134 if (!strcasecmp(flag, "\\Seen")) {
135 bits_to_twiddle |= IMAP_SEEN;
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);
151 * This function is called by the main command loop.
153 void imap_store(int num_parms, char *parms[]) {
160 cprintf("%s BAD invalid parameters\r\n", parms[0]);
164 if (imap_is_message_set(parms[2])) {
165 imap_pick_range(parms[2], 0);
168 cprintf("%s BAD invalid parameters\r\n", parms[0]);
173 for (i=3; i<num_parms; ++i) {
174 strcat(items, parms[i]);
175 if (i < (num_parms-1)) strcat(items, " ");
178 num_items = imap_extract_data_items(itemlist, items);
180 cprintf("%s BAD invalid data item list\r\n", parms[0]);
184 imap_do_store(num_items, itemlist);
185 cprintf("%s OK STORE completed\r\n", parms[0]);
189 * This function is called by the main command loop.
191 void imap_uidstore(int num_parms, char *parms[]) {
198 cprintf("%s BAD invalid parameters\r\n", parms[0]);
202 if (imap_is_message_set(parms[3])) {
203 imap_pick_range(parms[3], 1);
206 cprintf("%s BAD invalid parameters\r\n", parms[0]);
211 for (i=4; i<num_parms; ++i) {
212 strcat(items, parms[i]);
213 if (i < (num_parms-1)) strcat(items, " ");
216 num_items = imap_extract_data_items(itemlist, items);
218 cprintf("%s BAD invalid data item list\r\n", parms[0]);
222 imap_do_store(num_items, itemlist);
223 cprintf("%s OK UID STORE completed\r\n", parms[0]);