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"
61 #include "internet_addressing.h"
62 #include "serv_imap.h"
63 #include "imap_tools.h"
64 #include "imap_fetch.h"
65 #include "imap_misc.h"
67 #include "ctdl_module.h"
74 * imap_copy() calls imap_do_copy() to do its actual work, once it's
75 * validated and boiled down the request a bit. (returns 0 on success)
77 int imap_do_copy(char *destination_folder) {
79 char roomname[ROOMNAMELEN];
80 struct ctdlroom qrbuf;
81 long *selected_msgs = NULL;
84 if (IMAP->num_msgs < 1) {
88 i = imap_grabroom(roomname, destination_folder, 1);
89 if (i != 0) return(i);
92 * Copy all the message pointers in one shot.
94 selected_msgs = malloc(sizeof(long) * IMAP->num_msgs);
95 if (selected_msgs == NULL) return(-1);
97 for (i = 0; i < IMAP->num_msgs; ++i) {
98 if (IMAP->flags[i] & IMAP_SELECTED) {
99 selected_msgs[num_selected++] = IMAP->msgids[i];
103 if (num_selected > 0) {
104 CtdlSaveMsgPointersInRoom(roomname, selected_msgs, num_selected, 1, NULL);
108 /* Don't bother wasting any more time if there were no messages. */
109 if (num_selected == 0) {
113 /* Enumerate lists of messages for which flags are toggled */
114 long *seen_yes = NULL;
115 int num_seen_yes = 0;
116 long *seen_no = NULL;
118 long *answ_yes = NULL;
119 int num_answ_yes = 0;
120 long *answ_no = NULL;
123 seen_yes = malloc(num_selected * sizeof(long));
124 seen_no = malloc(num_selected * sizeof(long));
125 answ_yes = malloc(num_selected * sizeof(long));
126 answ_no = malloc(num_selected * sizeof(long));
128 for (i = 0; i < IMAP->num_msgs; ++i) {
129 if (IMAP->flags[i] & IMAP_SELECTED) {
130 if (IMAP->flags[i] & IMAP_SEEN) {
131 seen_yes[num_seen_yes++] = IMAP->msgids[i];
133 if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
134 seen_no[num_seen_no++] = IMAP->msgids[i];
136 if (IMAP->flags[i] & IMAP_ANSWERED) {
137 answ_yes[num_answ_yes++] = IMAP->msgids[i];
139 if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
140 answ_no[num_answ_no++] = IMAP->msgids[i];
145 /* Set the flags... */
146 i = getroom(&qrbuf, roomname);
148 CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
149 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
150 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
151 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
164 * Output the [COPYUID xxx yyy] response code required by RFC2359
165 * to tell the client the UID's of the messages that were copied (if any).
166 * We are assuming that the IMAP_SELECTED flag is still set on any relevant
167 * messages in our source room. Since the Citadel system uses UID's that
168 * are both globally unique and persistent across a room-to-room copy, we
169 * can get this done quite easily.
171 void imap_output_copyuid_response(void) {
175 for (i = 0; i < IMAP->num_msgs; ++i) {
176 if (IMAP->flags[i] & IMAP_SELECTED) {
178 if (num_output == 1) {
179 cprintf("[COPYUID ");
181 else if (num_output > 1) {
184 cprintf("%ld", IMAP->msgids[i]);
187 if (num_output > 0) {
194 * This function is called by the main command loop.
196 void imap_copy(int num_parms, char *parms[]) {
199 if (num_parms != 4) {
200 cprintf("%s BAD invalid parameters\r\n", parms[0]);
204 if (imap_is_message_set(parms[2])) {
205 imap_pick_range(parms[2], 0);
208 cprintf("%s BAD invalid parameters\r\n", parms[0]);
212 ret = imap_do_copy(parms[3]);
214 cprintf("%s OK ", parms[0]);
215 imap_output_copyuid_response();
216 cprintf("COPY completed\r\n");
219 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
224 * This function is called by the main command loop.
226 void imap_uidcopy(int num_parms, char *parms[]) {
228 if (num_parms != 5) {
229 cprintf("%s BAD invalid parameters\r\n", parms[0]);
233 if (imap_is_message_set(parms[3])) {
234 imap_pick_range(parms[3], 1);
237 cprintf("%s BAD invalid parameters\r\n", parms[0]);
241 if (imap_do_copy(parms[4]) == 0) {
242 cprintf("%s OK ", parms[0]);
243 imap_output_copyuid_response();
244 cprintf("UID COPY completed\r\n");
247 cprintf("%s NO UID COPY failed\r\n", parms[0]);
253 * imap_do_append_flags() is called by imap_append() to set any flags that
254 * the client specified at append time.
256 * FIXME find a way to do these in bulk so we don't max out our db journal
258 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
260 char this_flag[sizeof flags];
263 if (new_message_flags == NULL) return;
264 if (IsEmptyStr(new_message_flags)) return;
266 safestrncpy(flags, new_message_flags, sizeof flags);
268 for (i=0; i<num_tokens(flags, ' '); ++i) {
269 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
270 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
271 if (!strcasecmp(this_flag, "Seen")) {
272 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
275 if (!strcasecmp(this_flag, "Answered")) {
276 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
284 * This function is called by the main command loop.
286 void imap_append(int num_parms, char *parms[]) {
288 long bytes_transferred;
289 long stripped_length = 0;
290 struct CtdlMessage *msg = NULL;
291 long new_msgnum = (-1L);
293 char roomname[ROOMNAMELEN];
295 char savedroom[ROOMNAMELEN];
298 char new_message_flags[SIZ];
299 struct citimap *Imap;
302 cprintf("%s BAD usage error\r\n", parms[0]);
306 if ( (parms[num_parms-1][0] != '{')
307 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
308 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
312 strcpy(new_message_flags, "");
313 if (num_parms >= 5) {
314 for (i=3; i<num_parms; ++i) {
315 strcat(new_message_flags, parms[i]);
316 strcat(new_message_flags, " ");
318 stripallbut(new_message_flags, '(', ')');
321 /* This is how we'd do this if it were relevant in our data store.
322 * if (num_parms >= 6) {
323 * new_message_internaldate = parms[4];
327 literal_length = atol(&parms[num_parms-1][1]);
328 if (literal_length < 1) {
329 cprintf("%s BAD Message length must be at least 1.\r\n",
335 imap_free_transmitted_message(); /* just in case. */
336 Imap->transmitted_message = malloc(literal_length + 1);
337 if (Imap->transmitted_message == NULL) {
338 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
341 Imap->transmitted_length = literal_length;
343 cprintf("+ Transmit message now.\r\n");
345 bytes_transferred = 0;
347 ret = client_read(Imap->transmitted_message, literal_length);
348 Imap->transmitted_message[literal_length] = 0;
351 cprintf("%s NO Read failed.\r\n", parms[0]);
355 /* Client will transmit a trailing CRLF after the literal (the message
356 * text) is received. This call to client_getln() absorbs it.
359 client_getln(buf, sizeof buf);
361 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
362 CtdlLogPrintf(CTDL_DEBUG, "Converting CRLF to LF\n");
364 for (i=0; i<literal_length; ++i) {
365 if (strncmp(&Imap->transmitted_message[i], "\r\n", 2)) {
366 Imap->transmitted_message[stripped_length++] =
367 Imap->transmitted_message[i];
370 literal_length = stripped_length;
371 Imap->transmitted_message[literal_length] = 0; /* reterminate it */
373 CtdlLogPrintf(CTDL_DEBUG, "Converting message format\n");
374 msg = convert_internet_message(Imap->transmitted_message);
375 Imap->transmitted_message = NULL;
376 Imap->transmitted_length = 0;
378 ret = imap_grabroom(roomname, parms[2], 1);
380 cprintf("%s NO Invalid mailbox name or access denied\r\n",
386 * usergoto() formally takes us to the desired room. (If another
387 * folder is selected, save its name so we can return there!!!!!)
389 if (Imap->selected) {
390 strcpy(savedroom, CC->room.QRname);
392 usergoto(roomname, 0, 0, &msgs, &new);
394 /* If the user is locally authenticated, FORCE the From: header to
395 * show up as the real sender. FIXME do we really want to do this?
396 * Probably should make it site-definable or even room-definable.
398 * For now, we allow "forgeries" if the room is one of the user's
402 if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (config.c_imap_keep_from == 0)) {
403 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
404 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
405 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
406 msg->cm_fields['A'] = strdup(CC->user.fullname);
407 msg->cm_fields['N'] = strdup(config.c_nodename);
408 msg->cm_fields['H'] = strdup(config.c_humannode);
415 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf, NULL, POST_LOGGED_IN);
418 /* Nope ... print an error message */
419 cprintf("%s NO %s\r\n", parms[0], buf);
423 /* Yes ... go ahead and post! */
425 new_msgnum = CtdlSubmitMsg(msg, NULL, "", 0);
427 if (new_msgnum >= 0L) {
428 cprintf("%s OK [APPENDUID %ld %ld] APPEND completed\r\n",
429 parms[0], GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
432 cprintf("%s BAD Error %ld saving message to disk.\r\n",
433 parms[0], new_msgnum);
438 * IMAP protocol response to client has already been sent by now.
440 * If another folder is selected, go back to that room so we can resume
441 * our happy day without violent explosions.
443 if (Imap->selected) {
444 usergoto(savedroom, 0, 0, &msgs, &new);
447 /* We don't need this buffer anymore */
448 CtdlFreeMessage(msg);
450 if (new_message_flags != NULL) {
451 imap_do_append_flags(new_msgnum, new_message_flags);