6 * Copyright (c) 2001-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
60 #include "internet_addressing.h"
61 #include "imap_tools.h"
62 #include "serv_imap.h"
63 #include "imap_fetch.h"
64 #include "imap_misc.h"
66 #include "ctdl_module.h"
73 * imap_copy() calls imap_do_copy() to do its actual work, once it's
74 * validated and boiled down the request a bit. (returns 0 on success)
76 int imap_do_copy(const char *destination_folder) {
78 char roomname[ROOMNAMELEN];
79 struct ctdlroom qrbuf;
80 long *selected_msgs = NULL;
83 if (IMAP->num_msgs < 1) {
87 i = imap_grabroom(roomname, destination_folder, 1);
88 if (i != 0) return(i);
91 * Copy all the message pointers in one shot.
93 selected_msgs = malloc(sizeof(long) * IMAP->num_msgs);
94 if (selected_msgs == NULL) return(-1);
96 for (i = 0; i < IMAP->num_msgs; ++i) {
97 if (IMAP->flags[i] & IMAP_SELECTED) {
98 selected_msgs[num_selected++] = IMAP->msgids[i];
102 if (num_selected > 0) {
103 CtdlSaveMsgPointersInRoom(roomname, selected_msgs, num_selected, 1, NULL);
107 /* Don't bother wasting any more time if there were no messages. */
108 if (num_selected == 0) {
112 /* Enumerate lists of messages for which flags are toggled */
113 long *seen_yes = NULL;
114 int num_seen_yes = 0;
115 long *seen_no = NULL;
117 long *answ_yes = NULL;
118 int num_answ_yes = 0;
119 long *answ_no = NULL;
122 seen_yes = malloc(num_selected * sizeof(long));
123 seen_no = malloc(num_selected * sizeof(long));
124 answ_yes = malloc(num_selected * sizeof(long));
125 answ_no = malloc(num_selected * sizeof(long));
127 for (i = 0; i < IMAP->num_msgs; ++i) {
128 if (IMAP->flags[i] & IMAP_SELECTED) {
129 if (IMAP->flags[i] & IMAP_SEEN) {
130 seen_yes[num_seen_yes++] = IMAP->msgids[i];
132 if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
133 seen_no[num_seen_no++] = IMAP->msgids[i];
135 if (IMAP->flags[i] & IMAP_ANSWERED) {
136 answ_yes[num_answ_yes++] = IMAP->msgids[i];
138 if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
139 answ_no[num_answ_no++] = IMAP->msgids[i];
144 /* Set the flags... */
145 i = CtdlGetRoom(&qrbuf, roomname);
147 CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
148 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
149 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
150 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
163 * Output the [COPYUID xxx yyy] response code required by RFC2359
164 * to tell the client the UID's of the messages that were copied (if any).
165 * We are assuming that the IMAP_SELECTED flag is still set on any relevant
166 * messages in our source room. Since the Citadel system uses UID's that
167 * are both globally unique and persistent across a room-to-room copy, we
168 * can get this done quite easily.
170 void imap_output_copyuid_response(void) {
174 for (i = 0; i < IMAP->num_msgs; ++i) {
175 if (IMAP->flags[i] & IMAP_SELECTED) {
177 if (num_output == 1) {
178 cprintf("[COPYUID ");
180 else if (num_output > 1) {
183 cprintf("%ld", IMAP->msgids[i]);
186 if (num_output > 0) {
193 * This function is called by the main command loop.
195 void imap_copy(int num_parms, ConstStr *Params) {
198 if (num_parms != 4) {
199 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
203 if (imap_is_message_set(Params[2].Key)) {
204 imap_pick_range(Params[2].Key, 0);
207 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
211 ret = imap_do_copy(Params[3].Key);
213 cprintf("%s OK ", Params[0].Key);
214 imap_output_copyuid_response();
215 cprintf("COPY completed\r\n");
218 cprintf("%s NO COPY failed (error %d)\r\n", Params[0].Key, ret);
223 * This function is called by the main command loop.
225 void imap_uidcopy(int num_parms, ConstStr *Params) {
227 if (num_parms != 5) {
228 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
232 if (imap_is_message_set(Params[3].Key)) {
233 imap_pick_range(Params[3].Key, 1);
236 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
240 if (imap_do_copy(Params[4].Key) == 0) {
241 cprintf("%s OK ", Params[0].Key);
242 imap_output_copyuid_response();
243 cprintf("UID COPY completed\r\n");
246 cprintf("%s NO UID COPY failed\r\n", Params[0].Key);
252 * imap_do_append_flags() is called by imap_append() to set any flags that
253 * the client specified at append time.
255 * FIXME find a way to do these in bulk so we don't max out our db journal
257 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
259 char this_flag[sizeof flags];
262 if (new_message_flags == NULL) return;
263 if (IsEmptyStr(new_message_flags)) return;
265 safestrncpy(flags, new_message_flags, sizeof flags);
267 for (i=0; i<num_tokens(flags, ' '); ++i) {
268 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
269 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
270 if (!strcasecmp(this_flag, "Seen")) {
271 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
274 if (!strcasecmp(this_flag, "Answered")) {
275 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
283 * This function is called by the main command loop.
285 void imap_append(int num_parms, ConstStr *Params) {
287 long bytes_transferred;
288 struct CtdlMessage *msg = NULL;
289 long new_msgnum = (-1L);
291 char roomname[ROOMNAMELEN];
293 char savedroom[ROOMNAMELEN];
296 char new_message_flags[SIZ];
300 cprintf("%s BAD usage error\r\n", Params[0].Key);
304 if ( (Params[num_parms-1].Key[0] != '{')
305 || (Params[num_parms-1].Key[Params[num_parms-1].len-1] != '}') ) {
306 cprintf("%s BAD no message literal supplied\r\n", Params[0].Key);
310 strcpy(new_message_flags, "");
311 if (num_parms >= 5) {
312 for (i=3; i<num_parms; ++i) {
313 strcat(new_message_flags, Params[i].Key);
314 strcat(new_message_flags, " ");
316 stripallbut(new_message_flags, '(', ')');
319 /* This is how we'd do this if it were relevant in our data store.
320 * if (num_parms >= 6) {
321 * new_message_internaldate = parms[4];
325 literal_length = atol(&Params[num_parms-1].Key[1]);
326 if (literal_length < 1) {
327 cprintf("%s BAD Message length must be at least 1.\r\n",
333 imap_free_transmitted_message(); /* just in case. */
335 Imap->TransmittedMessage = NewStrBufPlain(NULL, literal_length);
337 if (Imap->TransmittedMessage == NULL) {
338 cprintf("%s NO Cannot allocate memory.\r\n", Params[0].Key);
342 cprintf("+ Transmit message now.\r\n");
344 bytes_transferred = 0;
345 client_read_blob(Imap->TransmittedMessage, literal_length, config.c_sleeping);
347 if ((ret < 0) || (StrLength(Imap->TransmittedMessage) < literal_length)) {
348 cprintf("%s NO Read failed.\r\n", Params[0].Key);
352 /* Client will transmit a trailing CRLF after the literal (the message
353 * text) is received. This call to client_getln() absorbs it.
356 client_getln(buf, sizeof buf);
358 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
359 CtdlLogPrintf(CTDL_DEBUG, "Converting CRLF to LF\n");
360 StrBufToUnixLF(Imap->TransmittedMessage);
362 CtdlLogPrintf(CTDL_DEBUG, "Converting message format\n");
363 msg = convert_internet_message_buf(&Imap->TransmittedMessage);
365 ret = imap_grabroom(roomname, Params[2].Key, 1);
367 cprintf("%s NO Invalid mailbox name or access denied\r\n",
373 * CtdlUserGoto() formally takes us to the desired room. (If another
374 * folder is selected, save its name so we can return there!!!!!)
376 if (Imap->selected) {
377 strcpy(savedroom, CC->room.QRname);
379 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
381 /* If the user is locally authenticated, FORCE the From: header to
382 * show up as the real sender. FIXME do we really want to do this?
383 * Probably should make it site-definable or even room-definable.
385 * For now, we allow "forgeries" if the room is one of the user's
389 if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (config.c_imap_keep_from == 0)) {
390 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
391 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
392 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
393 msg->cm_fields['A'] = strdup(CC->user.fullname);
394 msg->cm_fields['N'] = strdup(config.c_nodename);
395 msg->cm_fields['H'] = strdup(config.c_humannode);
402 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf, NULL, POST_LOGGED_IN);
405 /* Nope ... print an error message */
406 cprintf("%s NO %s\r\n", Params[0].Key, buf);
410 /* Yes ... go ahead and post! */
412 new_msgnum = CtdlSubmitMsg(msg, NULL, "", 0);
414 if (new_msgnum >= 0L) {
415 cprintf("%s OK [APPENDUID %ld %ld] APPEND completed\r\n",
416 Params[0].Key, GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
419 cprintf("%s BAD Error %ld saving message to disk.\r\n",
420 Params[0].Key, new_msgnum);
425 * IMAP protocol response to client has already been sent by now.
427 * If another folder is selected, go back to that room so we can resume
428 * our happy day without violent explosions.
430 if (Imap->selected) {
431 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
434 /* We don't need this buffer anymore */
435 CtdlFreeMessage(msg);
437 if (new_message_flags != NULL) {
438 imap_do_append_flags(new_msgnum, new_message_flags);