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],
84 ((IMAP->flags[seq] & IMAP_SEEN) ? 1 : 0),
88 if (bits_to_twiddle & IMAP_ANSWERED) {
89 CtdlSetSeen(IMAP->msgids[seq],
90 ((IMAP->flags[seq] & IMAP_ANSWERED) ? 1 : 0),
95 /* 'silent' is actually the value returned from a strncasecmp() so
96 * we want that option only if its value is zero. Seems backwards
97 * but that's the way it's supposed to be.
100 cprintf("* %d FETCH (", seq+1);
101 imap_fetch_flags(seq);
109 * imap_store() calls imap_do_store() to perform the actual bit twiddling
112 void imap_do_store(int num_items, char **itemlist) {
114 unsigned int bits_to_twiddle = 0;
117 char whichflags[256];
120 if (num_items < 2) return;
123 for (i=1; i<num_items; ++i) {
124 strcpy(whichflags, itemlist[i]);
125 if (whichflags[0]=='(') safestrncpy(whichflags, &whichflags[1], sizeof whichflags);
126 if (whichflags[strlen(whichflags)-1]==')') {
127 whichflags[strlen(whichflags)-1]=0;
131 /* A client might twiddle more than one bit at a time.
132 * Note that we check for the flag names without the leading
133 * backslash because imap_parameterize() strips them out.
135 num_flags = num_tokens(whichflags, ' ');
136 for (j=0; j<num_flags; ++j) {
137 extract_token(flag, whichflags, j, ' ', sizeof flag);
139 if ((!strcasecmp(flag, "\\Deleted"))
140 || (!strcasecmp(flag, "Deleted"))) {
141 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
142 bits_to_twiddle |= IMAP_DELETED;
145 if ((!strcasecmp(flag, "\\Seen"))
146 || (!strcasecmp(flag, "Seen"))) {
147 bits_to_twiddle |= IMAP_SEEN;
149 if ((!strcasecmp(flag, "\\Answered"))
150 || (!strcasecmp(flag, "\\Answered"))) {
151 bits_to_twiddle |= IMAP_ANSWERED;
156 if (IMAP->num_msgs > 0) {
157 for (i = 0; i < IMAP->num_msgs; ++i) {
158 if (IMAP->flags[i] & IMAP_SELECTED) {
159 imap_do_store_msg(i, oper, bits_to_twiddle);
167 * This function is called by the main command loop.
169 void imap_store(int num_parms, char *parms[]) {
176 cprintf("%s BAD invalid parameters\r\n", parms[0]);
180 if (imap_is_message_set(parms[2])) {
181 imap_pick_range(parms[2], 0);
184 cprintf("%s BAD invalid parameters\r\n", parms[0]);
189 for (i=3; i<num_parms; ++i) {
190 strcat(items, parms[i]);
191 if (i < (num_parms-1)) strcat(items, " ");
194 num_items = imap_extract_data_items(itemlist, items);
196 cprintf("%s BAD invalid data item list\r\n", parms[0]);
200 imap_do_store(num_items, itemlist);
201 cprintf("%s OK STORE completed\r\n", parms[0]);
205 * This function is called by the main command loop.
207 void imap_uidstore(int num_parms, char *parms[]) {
214 cprintf("%s BAD invalid parameters\r\n", parms[0]);
218 if (imap_is_message_set(parms[3])) {
219 imap_pick_range(parms[3], 1);
222 cprintf("%s BAD invalid parameters\r\n", parms[0]);
227 for (i=4; i<num_parms; ++i) {
228 lprintf(9, "item %d: %s\n", i, parms[i]);
229 strcat(items, parms[i]);
230 if (i < (num_parms-1)) strcat(items, " ");
233 num_items = imap_extract_data_items(itemlist, items);
235 cprintf("%s BAD invalid data item list\r\n", parms[0]);
239 imap_do_store(num_items, itemlist);
240 cprintf("%s OK UID STORE completed\r\n", parms[0]);