4 * Implements the STORE command in IMAP.
6 * Copyright (c) 2001-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ctdl_module.h"
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.h"
63 #include "imap_fetch.h"
64 #include "imap_store.h"
69 * imap_do_store() calls imap_do_store_msg() to tweak the settings of
70 * an individual message.
72 * We also implement the ".SILENT" protocol option here. :(
74 void imap_do_store_msg(int seq, char *oper, unsigned int bits_to_twiddle) {
77 if (!strncasecmp(oper, "FLAGS", 5)) {
78 IMAP->flags[seq] &= IMAP_MASK_SYSTEM;
79 IMAP->flags[seq] |= bits_to_twiddle;
81 else if (!strncasecmp(oper, "+FLAGS", 6)) {
82 IMAP->flags[seq] |= bits_to_twiddle;
84 else if (!strncasecmp(oper, "-FLAGS", 6)) {
85 IMAP->flags[seq] &= (~bits_to_twiddle);
91 * imap_store() calls imap_do_store() to perform the actual bit twiddling
94 void imap_do_store(int num_items, char **itemlist) {
96 unsigned int bits_to_twiddle = 0;
104 int last_item_twiddled = (-1);
106 if (num_items < 2) return;
108 if (bmstrcasestr(oper, ".SILENT")) {
113 * ss_msglist is an array of message numbers to manipulate. We
114 * are going to supply this array to CtdlSetSeen() later.
116 ss_msglist = malloc(IMAP->num_msgs * sizeof(long));
117 if (ss_msglist == NULL) return;
120 * Ok, go ahead and parse the flags.
122 for (i=1; i<num_items; ++i) {
123 strcpy(whichflags, itemlist[i]);
124 if (whichflags[0]=='(') {
125 safestrncpy(whichflags, &whichflags[1],
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 last_item_twiddled = i;
163 ss_msglist[num_ss++] = IMAP->msgids[i];
164 imap_do_store_msg(i, oper, bits_to_twiddle);
167 cprintf("* %d FETCH (", i+1);
177 * Now manipulate the database -- all in one shot.
179 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
181 if (bits_to_twiddle & IMAP_SEEN) {
182 CtdlSetSeen(ss_msglist, num_ss,
183 ((IMAP->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
189 if (bits_to_twiddle & IMAP_ANSWERED) {
190 CtdlSetSeen(ss_msglist, num_ss,
191 ((IMAP->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
192 ctdlsetseen_answered,
202 * The following two commands implement "instant expunge" if enabled.
204 if (config.c_instant_expunge) {
206 imap_rescan_msgids();
213 * This function is called by the main command loop.
215 void imap_store(int num_parms, char *parms[]) {
222 cprintf("%s BAD invalid parameters\r\n", parms[0]);
226 if (imap_is_message_set(parms[2])) {
227 imap_pick_range(parms[2], 0);
230 cprintf("%s BAD invalid parameters\r\n", parms[0]);
235 for (i=3; i<num_parms; ++i) {
236 strcat(items, parms[i]);
237 if (i < (num_parms-1)) strcat(items, " ");
240 num_items = imap_extract_data_items(itemlist, items);
242 cprintf("%s BAD invalid data item list\r\n", parms[0]);
246 imap_do_store(num_items, itemlist);
247 cprintf("%s OK STORE completed\r\n", parms[0]);
251 * This function is called by the main command loop.
253 void imap_uidstore(int num_parms, char *parms[]) {
260 cprintf("%s BAD invalid parameters\r\n", parms[0]);
264 if (imap_is_message_set(parms[3])) {
265 imap_pick_range(parms[3], 1);
268 cprintf("%s BAD invalid parameters\r\n", parms[0]);
273 for (i=4; i<num_parms; ++i) {
274 strcat(items, parms[i]);
275 if (i < (num_parms-1)) strcat(items, " ");
278 num_items = imap_extract_data_items(itemlist, items);
280 cprintf("%s BAD invalid data item list\r\n", parms[0]);
284 imap_do_store(num_items, itemlist);
285 cprintf("%s OK UID STORE completed\r\n", parms[0]);