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"
58 #include "internet_addressing.h"
59 #include "serv_imap.h"
60 #include "imap_tools.h"
61 #include "imap_fetch.h"
62 #include "imap_store.h"
67 * imap_do_store() calls imap_do_store_msg() to tweak the settings of
68 * an individual message.
70 * We also implement the ".SILENT" protocol option here. :(
72 void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
75 if (!strncasecmp(oper, "FLAGS", 5)) {
76 Imap->flags[seq] &= IMAP_MASK_SYSTEM;
77 Imap->flags[seq] |= bits_to_twiddle;
79 else if (!strncasecmp(oper, "+FLAGS", 6)) {
80 Imap->flags[seq] |= bits_to_twiddle;
82 else if (!strncasecmp(oper, "-FLAGS", 6)) {
83 Imap->flags[seq] &= (~bits_to_twiddle);
89 * imap_store() calls imap_do_store() to perform the actual bit twiddling
92 void imap_do_store(citimap_command *Cmd) {
94 unsigned int bits_to_twiddle = 0;
102 int last_item_twiddled = (-1);
103 citimap *Imap = IMAP;
105 if (Cmd->num_parms < 2) return;
106 oper = Cmd->Params[0].Key;
107 if (cbmstrcasestr(oper, ".SILENT")) {
112 * ss_msglist is an array of message numbers to manipulate. We
113 * are going to supply this array to CtdlSetSeen() later.
115 ss_msglist = malloc(Imap->num_msgs * sizeof(long));
116 if (ss_msglist == NULL) return;
119 * Ok, go ahead and parse the flags.
121 for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy?
122 strcpy(whichflags, Cmd->Params[i].Key);
123 if (whichflags[0]=='(') {
124 safestrncpy(whichflags, &whichflags[1],
127 if (whichflags[strlen(whichflags)-1]==')') {
128 whichflags[strlen(whichflags)-1]=0;
132 /* A client might twiddle more than one bit at a time.
133 * Note that we check for the flag names without the leading
134 * backslash because imap_parameterize() strips them out.
136 num_flags = num_tokens(whichflags, ' ');
137 for (j=0; j<num_flags; ++j) {
138 extract_token(flag, whichflags, j, ' ', sizeof flag);
140 if ((!strcasecmp(flag, "\\Deleted"))
141 || (!strcasecmp(flag, "Deleted"))) {
142 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
143 bits_to_twiddle |= IMAP_DELETED;
146 if ((!strcasecmp(flag, "\\Seen"))
147 || (!strcasecmp(flag, "Seen"))) {
148 bits_to_twiddle |= IMAP_SEEN;
150 if ((!strcasecmp(flag, "\\Answered"))
151 || (!strcasecmp(flag, "Answered"))) {
152 bits_to_twiddle |= IMAP_ANSWERED;
157 if (Imap->num_msgs > 0) {
158 for (i = 0; i < Imap->num_msgs; ++i) {
159 if (Imap->flags[i] & IMAP_SELECTED) {
160 last_item_twiddled = i;
162 ss_msglist[num_ss++] = Imap->msgids[i];
163 imap_do_store_msg(i, oper, bits_to_twiddle);
166 IAPrintf("* %d FETCH (", i+1);
176 * Now manipulate the database -- all in one shot.
178 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
180 if (bits_to_twiddle & IMAP_SEEN) {
181 CtdlSetSeen(ss_msglist, num_ss,
182 ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
188 if (bits_to_twiddle & IMAP_ANSWERED) {
189 CtdlSetSeen(ss_msglist, num_ss,
190 ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
191 ctdlsetseen_answered,
199 imap_do_expunge(); // Citadel always expunges immediately.
200 imap_rescan_msgids();
205 * This function is called by the main command loop.
207 void imap_store(int num_parms, ConstStr *Params) {
212 IReply("BAD invalid parameters");
216 if (imap_is_message_set(Params[2].Key)) {
217 imap_pick_range(Params[2].Key, 0);
220 IReply("BAD invalid parameters");
224 memset(&Cmd, 0, sizeof(citimap_command));
225 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
226 MakeStringOf(Cmd.CmdBuf, 3);
228 num_items = imap_extract_data_items(&Cmd);
230 IReply("BAD invalid data item list");
231 FreeStrBuf(&Cmd.CmdBuf);
237 IReply("OK STORE completed");
238 FreeStrBuf(&Cmd.CmdBuf);
243 * This function is called by the main command loop.
245 void imap_uidstore(int num_parms, ConstStr *Params) {
250 IReply("BAD invalid parameters");
254 if (imap_is_message_set(Params[3].Key)) {
255 imap_pick_range(Params[3].Key, 1);
258 IReply("BAD invalid parameters");
262 memset(&Cmd, 0, sizeof(citimap_command));
263 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
264 MakeStringOf(Cmd.CmdBuf, 4);
266 num_items = imap_extract_data_items(&Cmd);
268 IReply("BAD invalid data item list");
269 FreeStrBuf(&Cmd.CmdBuf);
275 IReply("OK UID STORE completed");
276 FreeStrBuf(&Cmd.CmdBuf);