2 * Copyright (c) 1987-2010 by the citadel.org team
4 * This program is free 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
45 #include <libcitadel.h>
48 #include "sysdep_decls.h"
49 #include "citserver.h"
55 #include "internet_addressing.h"
56 #include "imap_tools.h"
57 #include "serv_imap.h"
58 #include "imap_fetch.h"
59 #include "imap_misc.h"
61 #include "ctdl_module.h"
68 * imap_copy() calls imap_do_copy() to do its actual work, once it's
69 * validated and boiled down the request a bit. (returns 0 on success)
71 int imap_do_copy(const char *destination_folder) {
73 char roomname[ROOMNAMELEN];
74 struct ctdlroom qrbuf;
75 long *selected_msgs = NULL;
78 if (IMAP->num_msgs < 1) {
82 i = imap_grabroom(roomname, destination_folder, 1);
83 if (i != 0) return(i);
86 * Copy all the message pointers in one shot.
88 selected_msgs = malloc(sizeof(long) * IMAP->num_msgs);
89 if (selected_msgs == NULL) return(-1);
91 for (i = 0; i < IMAP->num_msgs; ++i) {
92 if (IMAP->flags[i] & IMAP_SELECTED) {
93 selected_msgs[num_selected++] = IMAP->msgids[i];
97 if (num_selected > 0) {
98 CtdlSaveMsgPointersInRoom(roomname, selected_msgs, num_selected, 1, NULL, 0);
102 /* Don't bother wasting any more time if there were no messages. */
103 if (num_selected == 0) {
107 /* Enumerate lists of messages for which flags are toggled */
108 long *seen_yes = NULL;
109 int num_seen_yes = 0;
110 long *seen_no = NULL;
112 long *answ_yes = NULL;
113 int num_answ_yes = 0;
114 long *answ_no = NULL;
117 seen_yes = malloc(num_selected * sizeof(long));
118 seen_no = malloc(num_selected * sizeof(long));
119 answ_yes = malloc(num_selected * sizeof(long));
120 answ_no = malloc(num_selected * sizeof(long));
122 for (i = 0; i < IMAP->num_msgs; ++i) {
123 if (IMAP->flags[i] & IMAP_SELECTED) {
124 if (IMAP->flags[i] & IMAP_SEEN) {
125 seen_yes[num_seen_yes++] = IMAP->msgids[i];
127 if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
128 seen_no[num_seen_no++] = IMAP->msgids[i];
130 if (IMAP->flags[i] & IMAP_ANSWERED) {
131 answ_yes[num_answ_yes++] = IMAP->msgids[i];
133 if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
134 answ_no[num_answ_no++] = IMAP->msgids[i];
139 /* Set the flags... */
140 i = CtdlGetRoom(&qrbuf, roomname);
142 CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
143 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
144 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
145 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
158 * Output the [COPYUID xxx yyy] response code required by RFC2359
159 * to tell the client the UID's of the messages that were copied (if any).
160 * We are assuming that the IMAP_SELECTED flag is still set on any relevant
161 * messages in our source room. Since the Citadel system uses UID's that
162 * are both globally unique and persistent across a room-to-room copy, we
163 * can get this done quite easily.
165 void imap_output_copyuid_response(void) {
169 for (i = 0; i < IMAP->num_msgs; ++i) {
170 if (IMAP->flags[i] & IMAP_SELECTED) {
172 if (num_output == 1) {
173 cprintf("[COPYUID ");
175 else if (num_output > 1) {
178 cprintf("%ld", IMAP->msgids[i]);
181 if (num_output > 0) {
188 * This function is called by the main command loop.
190 void imap_copy(int num_parms, ConstStr *Params) {
193 if (num_parms != 4) {
194 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
198 if (imap_is_message_set(Params[2].Key)) {
199 imap_pick_range(Params[2].Key, 0);
202 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
206 ret = imap_do_copy(Params[3].Key);
208 cprintf("%s OK ", Params[0].Key);
209 imap_output_copyuid_response();
210 cprintf("COPY completed\r\n");
213 cprintf("%s NO COPY failed (error %d)\r\n", Params[0].Key, ret);
218 * This function is called by the main command loop.
220 void imap_uidcopy(int num_parms, ConstStr *Params) {
222 if (num_parms != 5) {
223 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
227 if (imap_is_message_set(Params[3].Key)) {
228 imap_pick_range(Params[3].Key, 1);
231 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
235 if (imap_do_copy(Params[4].Key) == 0) {
236 cprintf("%s OK ", Params[0].Key);
237 imap_output_copyuid_response();
238 cprintf("UID COPY completed\r\n");
241 cprintf("%s NO UID COPY failed\r\n", Params[0].Key);
247 * imap_do_append_flags() is called by imap_append() to set any flags that
248 * the client specified at append time.
250 * FIXME find a way to do these in bulk so we don't max out our db journal
252 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
254 char this_flag[sizeof flags];
257 if (new_message_flags == NULL) return;
258 if (IsEmptyStr(new_message_flags)) return;
260 safestrncpy(flags, new_message_flags, sizeof flags);
262 for (i=0; i<num_tokens(flags, ' '); ++i) {
263 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
264 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
265 if (!strcasecmp(this_flag, "Seen")) {
266 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
269 if (!strcasecmp(this_flag, "Answered")) {
270 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
278 * This function is called by the main command loop.
280 void imap_append(int num_parms, ConstStr *Params) {
282 long bytes_transferred;
283 struct CtdlMessage *msg = NULL;
284 long new_msgnum = (-1L);
286 char roomname[ROOMNAMELEN];
289 char savedroom[ROOMNAMELEN];
292 char new_message_flags[SIZ];
296 cprintf("%s BAD usage error\r\n", Params[0].Key);
300 if ( (Params[num_parms-1].Key[0] != '{')
301 || (Params[num_parms-1].Key[Params[num_parms-1].len-1] != '}') ) {
302 cprintf("%s BAD no message literal supplied\r\n", Params[0].Key);
306 strcpy(new_message_flags, "");
307 if (num_parms >= 5) {
308 for (i=3; i<num_parms; ++i) {
309 strcat(new_message_flags, Params[i].Key);
310 strcat(new_message_flags, " ");
312 stripallbut(new_message_flags, '(', ')');
315 /* This is how we'd do this if it were relevant in our data store.
316 * if (num_parms >= 6) {
317 * new_message_internaldate = parms[4];
321 literal_length = atol(&Params[num_parms-1].Key[1]);
322 if (literal_length < 1) {
323 cprintf("%s BAD Message length must be at least 1.\r\n",
329 imap_free_transmitted_message(); /* just in case. */
331 Imap->TransmittedMessage = NewStrBufPlain(NULL, literal_length);
333 if (Imap->TransmittedMessage == NULL) {
334 cprintf("%s NO Cannot allocate memory.\r\n", Params[0].Key);
338 cprintf("+ Transmit message now.\r\n");
340 bytes_transferred = 0;
341 client_read_blob(Imap->TransmittedMessage, literal_length, config.c_sleeping);
343 if ((ret < 0) || (StrLength(Imap->TransmittedMessage) < literal_length)) {
344 cprintf("%s NO Read failed.\r\n", Params[0].Key);
348 /* Client will transmit a trailing CRLF after the literal (the message
349 * text) is received. This call to client_getln() absorbs it.
352 client_getln(dummy, sizeof dummy);
354 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
355 CtdlLogPrintf(CTDL_DEBUG, "Converting CRLF to LF\n");
356 StrBufToUnixLF(Imap->TransmittedMessage);
358 CtdlLogPrintf(CTDL_DEBUG, "Converting message format\n");
359 msg = convert_internet_message_buf(&Imap->TransmittedMessage);
361 ret = imap_grabroom(roomname, Params[2].Key, 1);
363 cprintf("%s NO Invalid mailbox name or access denied\r\n",
369 * CtdlUserGoto() formally takes us to the desired room. (If another
370 * folder is selected, save its name so we can return there!!!!!)
372 if (Imap->selected) {
373 strcpy(savedroom, CC->room.QRname);
375 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
377 /* If the user is locally authenticated, FORCE the From: header to
378 * show up as the real sender. FIXME do we really want to do this?
379 * Probably should make it site-definable or even room-definable.
381 * For now, we allow "forgeries" if the room is one of the user's
385 if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (config.c_imap_keep_from == 0)) {
386 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
387 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
388 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
389 msg->cm_fields['A'] = strdup(CC->user.fullname);
390 msg->cm_fields['N'] = strdup(config.c_nodename);
391 msg->cm_fields['H'] = strdup(config.c_humannode);
398 ret = CtdlDoIHavePermissionToPostInThisRoom(errbuf, sizeof errbuf, NULL, POST_LOGGED_IN);
401 /* Nope ... print an error message */
402 cprintf("%s NO %s\r\n", Params[0].Key, errbuf);
406 /* Yes ... go ahead and post! */
408 new_msgnum = CtdlSubmitMsg(msg, NULL, "", 0);
410 if (new_msgnum >= 0L) {
411 cprintf("%s OK [APPENDUID %ld %ld] APPEND completed\r\n",
412 Params[0].Key, GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
415 cprintf("%s BAD Error %ld saving message to disk.\r\n",
416 Params[0].Key, new_msgnum);
421 * IMAP protocol response to client has already been sent by now.
423 * If another folder is selected, go back to that room so we can resume
424 * our happy day without violent explosions.
426 if (Imap->selected) {
427 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
430 /* We don't need this buffer anymore */
431 CtdlFreeMessage(msg);
433 if (new_message_flags != NULL) {
434 imap_do_append_flags(new_msgnum, new_message_flags);