2 * Implements the STORE command in IMAP.
4 * Copyright (c) 2001-2009 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ctdl_module.h"
30 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
47 #include <libcitadel.h>
50 #include "sysdep_decls.h"
51 #include "citserver.h"
57 #include "internet_addressing.h"
58 #include "serv_imap.h"
59 #include "imap_tools.h"
60 #include "imap_fetch.h"
61 #include "imap_store.h"
66 * imap_do_store() calls imap_do_store_msg() to tweak the settings of
67 * an individual message.
69 * We also implement the ".SILENT" protocol option here. :(
71 void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
74 if (!strncasecmp(oper, "FLAGS", 5)) {
75 Imap->flags[seq] &= IMAP_MASK_SYSTEM;
76 Imap->flags[seq] |= bits_to_twiddle;
78 else if (!strncasecmp(oper, "+FLAGS", 6)) {
79 Imap->flags[seq] |= bits_to_twiddle;
81 else if (!strncasecmp(oper, "-FLAGS", 6)) {
82 Imap->flags[seq] &= (~bits_to_twiddle);
88 * imap_store() calls imap_do_store() to perform the actual bit twiddling
91 void imap_do_store(citimap_command *Cmd) {
93 unsigned int bits_to_twiddle = 0;
101 int last_item_twiddled = (-1);
102 citimap *Imap = IMAP;
104 if (Cmd->num_parms < 2) return;
105 oper = Cmd->Params[0].Key;
106 if (cbmstrcasestr(oper, ".SILENT")) {
111 * ss_msglist is an array of message numbers to manipulate. We
112 * are going to supply this array to CtdlSetSeen() later.
114 ss_msglist = malloc(Imap->num_msgs * sizeof(long));
115 if (ss_msglist == NULL) return;
118 * Ok, go ahead and parse the flags.
120 for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy?
121 strcpy(whichflags, Cmd->Params[i].Key);
122 if (whichflags[0]=='(') {
123 safestrncpy(whichflags, &whichflags[1],
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 last_item_twiddled = i;
161 ss_msglist[num_ss++] = Imap->msgids[i];
162 imap_do_store_msg(i, oper, bits_to_twiddle);
165 IAPrintf("* %d FETCH (", i+1);
175 * Now manipulate the database -- all in one shot.
177 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
179 if (bits_to_twiddle & IMAP_SEEN) {
180 CtdlSetSeen(ss_msglist, num_ss,
181 ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
187 if (bits_to_twiddle & IMAP_ANSWERED) {
188 CtdlSetSeen(ss_msglist, num_ss,
189 ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
190 ctdlsetseen_answered,
200 * The following two commands implement "instant expunge" if enabled.
202 if (config.c_instant_expunge) {
204 imap_rescan_msgids();
210 * This function is called by the main command loop.
212 void imap_store(int num_parms, ConstStr *Params) {
217 IReply("BAD invalid parameters");
221 if (imap_is_message_set(Params[2].Key)) {
222 imap_pick_range(Params[2].Key, 0);
225 IReply("BAD invalid parameters");
229 memset(&Cmd, 0, sizeof(citimap_command));
230 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
231 MakeStringOf(Cmd.CmdBuf, 3);
233 num_items = imap_extract_data_items(&Cmd);
235 IReply("BAD invalid data item list");
236 FreeStrBuf(&Cmd.CmdBuf);
242 IReply("OK STORE completed");
243 FreeStrBuf(&Cmd.CmdBuf);
248 * This function is called by the main command loop.
250 void imap_uidstore(int num_parms, ConstStr *Params) {
255 IReply("BAD invalid parameters");
259 if (imap_is_message_set(Params[3].Key)) {
260 imap_pick_range(Params[3].Key, 1);
263 IReply("BAD invalid parameters");
267 memset(&Cmd, 0, sizeof(citimap_command));
268 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
269 MakeStringOf(Cmd.CmdBuf, 4);
271 num_items = imap_extract_data_items(&Cmd);
273 IReply("BAD invalid data item list");
274 FreeStrBuf(&Cmd.CmdBuf);
280 IReply("OK UID STORE completed");
281 FreeStrBuf(&Cmd.CmdBuf);