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 "imap_tools.h"
62 #include "serv_imap.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, const 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(citimap_command *Cmd) {
96 unsigned int bits_to_twiddle = 0;
104 int last_item_twiddled = (-1);
105 citimap *Imap = IMAP;
107 if (Cmd->num_parms < 2) return;
108 oper = Cmd->Params[0].Key;
109 if (bmstrcasestr(oper, ".SILENT")) {
114 * ss_msglist is an array of message numbers to manipulate. We
115 * are going to supply this array to CtdlSetSeen() later.
117 ss_msglist = malloc(Imap->num_msgs * sizeof(long));
118 if (ss_msglist == NULL) return;
121 * Ok, go ahead and parse the flags.
123 for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy?
124 strcpy(whichflags, Cmd->Params[i].Key);
125 if (whichflags[0]=='(') {
126 safestrncpy(whichflags, &whichflags[1],
129 if (whichflags[strlen(whichflags)-1]==')') {
130 whichflags[strlen(whichflags)-1]=0;
134 /* A client might twiddle more than one bit at a time.
135 * Note that we check for the flag names without the leading
136 * backslash because imap_parameterize() strips them out.
138 num_flags = num_tokens(whichflags, ' ');
139 for (j=0; j<num_flags; ++j) {
140 extract_token(flag, whichflags, j, ' ', sizeof flag);
142 if ((!strcasecmp(flag, "\\Deleted"))
143 || (!strcasecmp(flag, "Deleted"))) {
144 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
145 bits_to_twiddle |= IMAP_DELETED;
148 if ((!strcasecmp(flag, "\\Seen"))
149 || (!strcasecmp(flag, "Seen"))) {
150 bits_to_twiddle |= IMAP_SEEN;
152 if ((!strcasecmp(flag, "\\Answered"))
153 || (!strcasecmp(flag, "Answered"))) {
154 bits_to_twiddle |= IMAP_ANSWERED;
159 if (Imap->num_msgs > 0) {
160 for (i = 0; i < Imap->num_msgs; ++i) {
161 if (Imap->flags[i] & IMAP_SELECTED) {
162 last_item_twiddled = i;
164 ss_msglist[num_ss++] = Imap->msgids[i];
165 imap_do_store_msg(i, oper, bits_to_twiddle);
168 cprintf("* %d FETCH (", i+1);
178 * Now manipulate the database -- all in one shot.
180 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
182 if (bits_to_twiddle & IMAP_SEEN) {
183 CtdlSetSeen(ss_msglist, num_ss,
184 ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
190 if (bits_to_twiddle & IMAP_ANSWERED) {
191 CtdlSetSeen(ss_msglist, num_ss,
192 ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
193 ctdlsetseen_answered,
203 * The following two commands implement "instant expunge" if enabled.
205 if (config.c_instant_expunge) {
207 imap_rescan_msgids();
214 * This function is called by the main command loop.
216 void imap_store(int num_parms, ConstStr *Params) {
221 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
225 if (imap_is_message_set(Params[2].Key)) {
226 imap_pick_range(Params[2].Key, 0);
229 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
233 memset(&Cmd, 0, sizeof(citimap_command));
234 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
235 MakeStringOf(Cmd.CmdBuf, 3);
237 num_items = imap_extract_data_items(&Cmd);
239 cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
240 FreeStrBuf(&Cmd.CmdBuf);
246 cprintf("%s OK STORE completed\r\n", Params[0].Key);
247 FreeStrBuf(&Cmd.CmdBuf);
252 * This function is called by the main command loop.
254 void imap_uidstore(int num_parms, ConstStr *Params) {
259 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
263 if (imap_is_message_set(Params[3].Key)) {
264 imap_pick_range(Params[3].Key, 1);
267 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
271 memset(&Cmd, 0, sizeof(citimap_command));
272 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
273 MakeStringOf(Cmd.CmdBuf, 4);
275 num_items = imap_extract_data_items(&Cmd);
277 cprintf("%s BAD invalid data item list\r\n", Params[0].Key);
278 FreeStrBuf(&Cmd.CmdBuf);
284 cprintf("%s OK UID STORE completed\r\n", Params[0].Key);
285 FreeStrBuf(&Cmd.CmdBuf);