4 * Implements the STORE command in IMAP.
7 * Copyright (c) 2001-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "sysdep_decls.h"
54 #include "citserver.h"
62 #include "internet_addressing.h"
63 #include "serv_imap.h"
64 #include "imap_tools.h"
65 #include "imap_fetch.h"
66 #include "imap_store.h"
75 * imap_do_store() calls imap_do_store_msg() to tweak the settings of
76 * an individual message.
78 * We also implement the ".SILENT" protocol option here. :(
80 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
83 if (!strncasecmp(oper, "FLAGS", 5)) {
84 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
85 IMAP->flags[seq] |= bits_to_twiddle;
87 else if (!strncasecmp(oper, "+FLAGS", 6)) {
88 IMAP->flags[seq] |= bits_to_twiddle;
90 else if (!strncasecmp(oper, "-FLAGS", 6)) {
91 IMAP->flags[seq] &= (~bits_to_twiddle);
98 * imap_store() calls imap_do_store() to perform the actual bit twiddling
101 void imap_do_store(int num_items, char **itemlist) {
103 unsigned int bits_to_twiddle = 0;
106 char whichflags[256];
111 int last_item_twiddled = (-1);
113 if (num_items < 2) return;
115 if (bmstrcasestr(oper, ".SILENT")) {
120 * ss_msglist is an array of message numbers to manipulate. We
121 * are going to supply this array to CtdlSetSeen() later.
123 ss_msglist = malloc(IMAP->num_msgs * sizeof(long));
124 if (ss_msglist == NULL) return;
127 * Ok, go ahead and parse the flags.
129 for (i=1; i<num_items; ++i) {
130 strcpy(whichflags, itemlist[i]);
131 if (whichflags[0]=='(') {
132 safestrncpy(whichflags, &whichflags[1],
135 if (whichflags[strlen(whichflags)-1]==')') {
136 whichflags[strlen(whichflags)-1]=0;
140 /* A client might twiddle more than one bit at a time.
141 * Note that we check for the flag names without the leading
142 * backslash because imap_parameterize() strips them out.
144 num_flags = num_tokens(whichflags, ' ');
145 for (j=0; j<num_flags; ++j) {
146 extract_token(flag, whichflags, j, ' ', sizeof flag);
148 if ((!strcasecmp(flag, "\\Deleted"))
149 || (!strcasecmp(flag, "Deleted"))) {
150 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
151 bits_to_twiddle |= IMAP_DELETED;
154 if ((!strcasecmp(flag, "\\Seen"))
155 || (!strcasecmp(flag, "Seen"))) {
156 bits_to_twiddle |= IMAP_SEEN;
158 if ((!strcasecmp(flag, "\\Answered"))
159 || (!strcasecmp(flag, "Answered"))) {
160 bits_to_twiddle |= IMAP_ANSWERED;
165 if (IMAP->num_msgs > 0) {
166 for (i = 0; i < IMAP->num_msgs; ++i) {
167 if (IMAP->flags[i] & IMAP_SELECTED) {
168 last_item_twiddled = i;
170 ss_msglist[num_ss++] = IMAP->msgids[i];
171 imap_do_store_msg(i, oper, bits_to_twiddle);
174 cprintf("* %d FETCH (", i+1);
184 * Now manipulate the database -- all in one shot.
186 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
188 if (bits_to_twiddle & IMAP_SEEN) {
189 CtdlSetSeen(ss_msglist, num_ss,
190 ((IMAP->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
196 if (bits_to_twiddle & IMAP_ANSWERED) {
197 CtdlSetSeen(ss_msglist, num_ss,
198 ((IMAP->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
199 ctdlsetseen_answered,
209 * The following two commands implement "instant expunge" if enabled.
211 if (config.c_instant_expunge) {
213 imap_rescan_msgids();
220 * This function is called by the main command loop.
222 void imap_store(int num_parms, char *parms[]) {
229 cprintf("%s BAD invalid parameters\r\n", parms[0]);
233 if (imap_is_message_set(parms[2])) {
234 imap_pick_range(parms[2], 0);
237 cprintf("%s BAD invalid parameters\r\n", parms[0]);
242 for (i=3; i<num_parms; ++i) {
243 strcat(items, parms[i]);
244 if (i < (num_parms-1)) strcat(items, " ");
247 num_items = imap_extract_data_items(itemlist, items);
249 cprintf("%s BAD invalid data item list\r\n", parms[0]);
253 imap_do_store(num_items, itemlist);
254 cprintf("%s OK STORE completed\r\n", parms[0]);
258 * This function is called by the main command loop.
260 void imap_uidstore(int num_parms, char *parms[]) {
267 cprintf("%s BAD invalid parameters\r\n", parms[0]);
271 if (imap_is_message_set(parms[3])) {
272 imap_pick_range(parms[3], 1);
275 cprintf("%s BAD invalid parameters\r\n", parms[0]);
280 for (i=4; i<num_parms; ++i) {
281 strcat(items, parms[i]);
282 if (i < (num_parms-1)) strcat(items, " ");
285 num_items = imap_extract_data_items(itemlist, items);
287 cprintf("%s BAD invalid data item list\r\n", parms[0]);
291 imap_do_store(num_items, itemlist);
292 cprintf("%s OK UID STORE completed\r\n", parms[0]);