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>
36 #include <libcitadel.h>
37 #include "../../citadel.h"
38 #include "../../server.h"
39 #include "../../sysdep_decls.h"
40 #include "../../citserver.h"
41 #include "../../support.h"
42 #include "../../config.h"
43 #include "../../user_ops.h"
44 #include "../../database.h"
45 #include "../../room_ops.h"
46 #include "../../msgbase.h"
47 #include "../../internet_addressing.h"
48 #include "serv_imap.h"
49 #include "imap_tools.h"
50 #include "imap_fetch.h"
51 #include "imap_store.h"
52 #include "../../genstamp.h"
56 * imap_do_store() calls imap_do_store_msg() to tweak the settings of
57 * an individual message.
59 * We also implement the ".SILENT" protocol option here. :(
61 void imap_do_store_msg(int seq, const char *oper, unsigned int bits_to_twiddle) {
64 if (!strncasecmp(oper, "FLAGS", 5)) {
65 Imap->flags[seq] &= IMAP_MASK_SYSTEM;
66 Imap->flags[seq] |= bits_to_twiddle;
68 else if (!strncasecmp(oper, "+FLAGS", 6)) {
69 Imap->flags[seq] |= bits_to_twiddle;
71 else if (!strncasecmp(oper, "-FLAGS", 6)) {
72 Imap->flags[seq] &= (~bits_to_twiddle);
78 * imap_store() calls imap_do_store() to perform the actual bit twiddling
81 void imap_do_store(citimap_command *Cmd) {
83 unsigned int bits_to_twiddle = 0;
91 int last_item_twiddled = (-1);
94 if (Cmd->num_parms < 2) return;
95 oper = Cmd->Params[0].Key;
96 if (cbmstrcasestr(oper, ".SILENT")) {
101 * ss_msglist is an array of message numbers to manipulate. We
102 * are going to supply this array to CtdlSetSeen() later.
104 ss_msglist = malloc(Imap->num_msgs * sizeof(long));
105 if (ss_msglist == NULL) return;
108 * Ok, go ahead and parse the flags.
110 for (i=1; i<Cmd->num_parms; ++i) {///TODO: why strcpy?
111 strcpy(whichflags, Cmd->Params[i].Key);
112 if (whichflags[0]=='(') {
113 safestrncpy(whichflags, &whichflags[1],
116 if (whichflags[strlen(whichflags)-1]==')') {
117 whichflags[strlen(whichflags)-1]=0;
121 /* A client might twiddle more than one bit at a time.
122 * Note that we check for the flag names without the leading
123 * backslash because imap_parameterize() strips them out.
125 num_flags = num_tokens(whichflags, ' ');
126 for (j=0; j<num_flags; ++j) {
127 extract_token(flag, whichflags, j, ' ', sizeof flag);
129 if ((!strcasecmp(flag, "\\Deleted"))
130 || (!strcasecmp(flag, "Deleted"))) {
131 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
132 bits_to_twiddle |= IMAP_DELETED;
135 if ((!strcasecmp(flag, "\\Seen"))
136 || (!strcasecmp(flag, "Seen"))) {
137 bits_to_twiddle |= IMAP_SEEN;
139 if ((!strcasecmp(flag, "\\Answered"))
140 || (!strcasecmp(flag, "Answered"))) {
141 bits_to_twiddle |= IMAP_ANSWERED;
146 if (Imap->num_msgs > 0) {
147 for (i = 0; i < Imap->num_msgs; ++i) {
148 if (Imap->flags[i] & IMAP_SELECTED) {
149 last_item_twiddled = i;
151 ss_msglist[num_ss++] = Imap->msgids[i];
152 imap_do_store_msg(i, oper, bits_to_twiddle);
155 IAPrintf("* %d FETCH (", i+1);
165 * Now manipulate the database -- all in one shot.
167 if ( (last_item_twiddled >= 0) && (num_ss > 0) ) {
169 if (bits_to_twiddle & IMAP_SEEN) {
170 CtdlSetSeen(ss_msglist, num_ss,
171 ((Imap->flags[last_item_twiddled] & IMAP_SEEN) ? 1 : 0),
177 if (bits_to_twiddle & IMAP_ANSWERED) {
178 CtdlSetSeen(ss_msglist, num_ss,
179 ((Imap->flags[last_item_twiddled] & IMAP_ANSWERED) ? 1 : 0),
180 ctdlsetseen_answered,
188 imap_do_expunge(); // Citadel always expunges immediately.
189 imap_rescan_msgids();
194 * This function is called by the main command loop.
196 void imap_store(int num_parms, ConstStr *Params) {
201 IReply("BAD invalid parameters");
205 if (imap_is_message_set(Params[2].Key)) {
206 imap_pick_range(Params[2].Key, 0);
209 IReply("BAD invalid parameters");
213 memset(&Cmd, 0, sizeof(citimap_command));
214 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
215 MakeStringOf(Cmd.CmdBuf, 3);
217 num_items = imap_extract_data_items(&Cmd);
219 IReply("BAD invalid data item list");
220 FreeStrBuf(&Cmd.CmdBuf);
226 IReply("OK STORE completed");
227 FreeStrBuf(&Cmd.CmdBuf);
232 * This function is called by the main command loop.
234 void imap_uidstore(int num_parms, ConstStr *Params) {
239 IReply("BAD invalid parameters");
243 if (imap_is_message_set(Params[3].Key)) {
244 imap_pick_range(Params[3].Key, 1);
247 IReply("BAD invalid parameters");
251 memset(&Cmd, 0, sizeof(citimap_command));
252 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
253 MakeStringOf(Cmd.CmdBuf, 4);
255 num_items = imap_extract_data_items(&Cmd);
257 IReply("BAD invalid data item list");
258 FreeStrBuf(&Cmd.CmdBuf);
264 IReply("OK UID STORE completed");
265 FreeStrBuf(&Cmd.CmdBuf);