2 * Copyright (c) 1987-2020 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 #include <sys/types.h>
30 #include <libcitadel.h>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
41 #include "internet_addressing.h"
42 #include "serv_imap.h"
43 #include "imap_tools.h"
44 #include "imap_fetch.h"
45 #include "imap_misc.h"
47 #include "ctdl_module.h"
54 * imap_copy() calls imap_do_copy() to do its actual work, once it's
55 * validated and boiled down the request a bit. (returns 0 on success)
57 int imap_do_copy(const char *destination_folder) {
60 char roomname[ROOMNAMELEN];
61 struct ctdlroom qrbuf;
62 long *selected_msgs = NULL;
65 if (Imap->num_msgs < 1) {
69 i = imap_grabroom(roomname, destination_folder, 1);
70 if (i != 0) return(i);
73 * Copy all the message pointers in one shot.
75 selected_msgs = malloc(sizeof(long) * Imap->num_msgs);
76 if (selected_msgs == NULL) return(-1);
78 for (i = 0; i < Imap->num_msgs; ++i) {
79 if (Imap->flags[i] & IMAP_SELECTED) {
80 selected_msgs[num_selected++] = Imap->msgids[i];
84 if (num_selected > 0) {
85 CtdlSaveMsgPointersInRoom(roomname, selected_msgs, num_selected, 1, NULL, 0);
89 /* Don't bother wasting any more time if there were no messages. */
90 if (num_selected == 0) {
94 /* Enumerate lists of messages for which flags are toggled */
95 long *seen_yes = NULL;
99 long *answ_yes = NULL;
100 int num_answ_yes = 0;
101 long *answ_no = NULL;
104 seen_yes = malloc(num_selected * sizeof(long));
105 seen_no = malloc(num_selected * sizeof(long));
106 answ_yes = malloc(num_selected * sizeof(long));
107 answ_no = malloc(num_selected * sizeof(long));
109 for (i = 0; i < Imap->num_msgs; ++i) {
110 if (Imap->flags[i] & IMAP_SELECTED) {
111 if (Imap->flags[i] & IMAP_SEEN) {
112 seen_yes[num_seen_yes++] = Imap->msgids[i];
114 if ((Imap->flags[i] & IMAP_SEEN) == 0) {
115 seen_no[num_seen_no++] = Imap->msgids[i];
117 if (Imap->flags[i] & IMAP_ANSWERED) {
118 answ_yes[num_answ_yes++] = Imap->msgids[i];
120 if ((Imap->flags[i] & IMAP_ANSWERED) == 0) {
121 answ_no[num_answ_no++] = Imap->msgids[i];
126 /* Set the flags... */
127 i = CtdlGetRoom(&qrbuf, roomname);
129 CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
130 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
131 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
132 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
145 * Output the [COPYUID xxx yyy] response code required by RFC2359
146 * to tell the client the UID's of the messages that were copied (if any).
147 * We are assuming that the IMAP_SELECTED flag is still set on any relevant
148 * messages in our source room. Since the Citadel system uses UID's that
149 * are both globally unique and persistent across a room-to-room copy, we
150 * can get this done quite easily.
152 void imap_output_copyuid_response(citimap *Imap) {
154 StrBuf *MsgsCopied = NewStrBuf();
156 for (i = 0; i < Imap->num_msgs; ++i) {
157 if (Imap->flags[i] & IMAP_SELECTED) {
158 if (StrLength(MsgsCopied) > 0) {
159 StrBufAppendBufPlain(MsgsCopied, HKEY(","), 0);
161 StrBufAppendPrintf(MsgsCopied, "%ld", Imap->msgids[i]);
165 if (StrLength(MsgsCopied) > 0) {
166 IAPrintf("[COPYUID %ld %s %s] ", GLOBAL_UIDVALIDITY_VALUE, ChrPtr(MsgsCopied), ChrPtr(MsgsCopied));
169 FreeStrBuf(&MsgsCopied);
174 * This function is called by the main command loop.
176 void imap_copy(int num_parms, ConstStr *Params) {
179 if (num_parms != 4) {
180 IReply("BAD invalid parameters");
184 if (imap_is_message_set(Params[2].Key)) {
185 imap_pick_range(Params[2].Key, 0);
188 IReply("BAD invalid parameters");
192 ret = imap_do_copy(Params[3].Key);
194 IAPrintf("%s OK ", Params[0].Key);
195 imap_output_copyuid_response(IMAP);
196 IAPuts("COPY completed\r\n");
199 IReplyPrintf("NO COPY failed (error %d)", ret);
204 * This function is called by the main command loop.
206 void imap_uidcopy(int num_parms, ConstStr *Params) {
208 if (num_parms != 5) {
209 IReply("BAD invalid parameters");
213 if (imap_is_message_set(Params[3].Key)) {
214 imap_pick_range(Params[3].Key, 1);
217 IReply("BAD invalid parameters");
221 if (imap_do_copy(Params[4].Key) == 0) {
222 IAPrintf("%s OK ", Params[0].Key);
223 imap_output_copyuid_response(IMAP);
224 IAPuts("UID COPY completed\r\n");
227 IReply("NO UID COPY failed");
233 * imap_do_append_flags() is called by imap_append() to set any flags that
234 * the client specified at append time.
236 * FIXME find a way to do these in bulk so we don't max out our db journal
238 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
240 char this_flag[sizeof flags];
243 if (new_message_flags == NULL) return;
244 if (IsEmptyStr(new_message_flags)) return;
246 safestrncpy(flags, new_message_flags, sizeof flags);
248 for (i=0; i<num_tokens(flags, ' '); ++i) {
249 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
250 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
251 if (!strcasecmp(this_flag, "Seen")) {
252 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
255 if (!strcasecmp(this_flag, "Answered")) {
256 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
264 * This function is called by the main command loop.
266 void imap_append(int num_parms, ConstStr *Params) {
268 struct CtdlMessage *msg = NULL;
269 long new_msgnum = (-1L);
271 char roomname[ROOMNAMELEN];
274 char savedroom[ROOMNAMELEN];
277 char new_message_flags[SIZ];
281 IReply("BAD usage error");
285 if ( (Params[num_parms-1].Key[0] != '{')
286 || (Params[num_parms-1].Key[Params[num_parms-1].len-1] != '}') ) {
287 IReply("BAD no message literal supplied");
291 *new_message_flags = '\0';
292 if (num_parms >= 5) {
293 for (i=3; i<num_parms; ++i) {
294 strcat(new_message_flags, Params[i].Key);
295 strcat(new_message_flags, " ");
297 stripallbut(new_message_flags, '(', ')');
300 /* This is how we'd do this if it were relevant in our data store.
301 * if (num_parms >= 6) {
302 * new_message_internaldate = parms[4];
306 literal_length = atol(&Params[num_parms-1].Key[1]);
307 if (literal_length < 1) {
308 IReply("BAD Message length must be at least 1.");
313 imap_free_transmitted_message(); /* just in case. */
315 Imap->TransmittedMessage = NewStrBufPlain(NULL, literal_length);
317 if (Imap->TransmittedMessage == NULL) {
318 IReply("NO Cannot allocate memory.");
322 IAPrintf("+ Transmit message now.\r\n");
326 client_read_blob(Imap->TransmittedMessage, literal_length, CtdlGetConfigInt("c_sleeping"));
328 if ((ret < 0) || (StrLength(Imap->TransmittedMessage) < literal_length)) {
329 IReply("NO Read failed.");
333 /* Client will transmit a trailing CRLF after the literal (the message
334 * text) is received. This call to client_getln() absorbs it.
337 client_getln(dummy, sizeof dummy);
339 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
340 syslog(LOG_DEBUG, "Converting CRLF to LF");
341 StrBufToUnixLF(Imap->TransmittedMessage);
343 syslog(LOG_DEBUG, "Converting message format");
344 msg = convert_internet_message_buf(&Imap->TransmittedMessage);
346 ret = imap_grabroom(roomname, Params[2].Key, 1);
348 IReply("NO Invalid mailbox name or access denied");
353 * CtdlUserGoto() formally takes us to the desired room. (If another
354 * folder is selected, save its name so we can return there!!!!!)
356 if (Imap->selected) {
357 strcpy(savedroom, CC->room.QRname);
359 CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
361 /* If the user is locally authenticated, FORCE the From: header to
362 * show up as the real sender. (Configurable setting)
365 if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (CtdlGetConfigInt("c_imap_keep_from") == 0))
367 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
374 ret = CtdlDoIHavePermissionToPostInThisRoom(errbuf, sizeof errbuf, NULL, POST_LOGGED_IN, 0);
377 /* Nope ... print an error message */
378 IReplyPrintf("NO %s", errbuf);
382 /* Yes ... go ahead and post! */
384 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
386 if (new_msgnum >= 0L) {
387 IReplyPrintf("OK [APPENDUID %ld %ld] APPEND completed",
388 GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
391 IReplyPrintf("BAD Error %ld saving message to disk.",
397 * IMAP protocol response to client has already been sent by now.
399 * If another folder is selected, go back to that room so we can resume
400 * our happy day without violent explosions.
402 if (Imap->selected) {
403 CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
406 /* We don't need this buffer anymore */
409 if (IsEmptyStr(new_message_flags)) {
410 imap_do_append_flags(new_msgnum, new_message_flags);