2 * Copyright (c) 1987-2011 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.
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 "serv_imap.h"
57 #include "imap_tools.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) {
74 char roomname[ROOMNAMELEN];
75 struct ctdlroom qrbuf;
76 long *selected_msgs = NULL;
79 if (Imap->num_msgs < 1) {
83 i = imap_grabroom(roomname, destination_folder, 1);
84 if (i != 0) return(i);
87 * Copy all the message pointers in one shot.
89 selected_msgs = malloc(sizeof(long) * Imap->num_msgs);
90 if (selected_msgs == NULL) return(-1);
92 for (i = 0; i < Imap->num_msgs; ++i) {
93 if (Imap->flags[i] & IMAP_SELECTED) {
94 selected_msgs[num_selected++] = Imap->msgids[i];
98 if (num_selected > 0) {
99 CtdlSaveMsgPointersInRoom(roomname, selected_msgs, num_selected, 1, NULL, 0);
103 /* Don't bother wasting any more time if there were no messages. */
104 if (num_selected == 0) {
108 /* Enumerate lists of messages for which flags are toggled */
109 long *seen_yes = NULL;
110 int num_seen_yes = 0;
111 long *seen_no = NULL;
113 long *answ_yes = NULL;
114 int num_answ_yes = 0;
115 long *answ_no = NULL;
118 seen_yes = malloc(num_selected * sizeof(long));
119 seen_no = malloc(num_selected * sizeof(long));
120 answ_yes = malloc(num_selected * sizeof(long));
121 answ_no = malloc(num_selected * sizeof(long));
123 for (i = 0; i < Imap->num_msgs; ++i) {
124 if (Imap->flags[i] & IMAP_SELECTED) {
125 if (Imap->flags[i] & IMAP_SEEN) {
126 seen_yes[num_seen_yes++] = Imap->msgids[i];
128 if ((Imap->flags[i] & IMAP_SEEN) == 0) {
129 seen_no[num_seen_no++] = Imap->msgids[i];
131 if (Imap->flags[i] & IMAP_ANSWERED) {
132 answ_yes[num_answ_yes++] = Imap->msgids[i];
134 if ((Imap->flags[i] & IMAP_ANSWERED) == 0) {
135 answ_no[num_answ_no++] = Imap->msgids[i];
140 /* Set the flags... */
141 i = CtdlGetRoom(&qrbuf, roomname);
143 CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
144 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
145 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
146 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
159 * Output the [COPYUID xxx yyy] response code required by RFC2359
160 * to tell the client the UID's of the messages that were copied (if any).
161 * We are assuming that the IMAP_SELECTED flag is still set on any relevant
162 * messages in our source room. Since the Citadel system uses UID's that
163 * are both globally unique and persistent across a room-to-room copy, we
164 * can get this done quite easily.
166 void imap_output_copyuid_response(void) {
167 citimap *Imap = IMAP;
171 for (i = 0; i < Imap->num_msgs; ++i) {
172 if (Imap->flags[i] & IMAP_SELECTED) {
174 if (num_output == 1) {
177 else if (num_output > 1) {
180 IAPrintf("%ld", Imap->msgids[i]);
183 if (num_output > 0) {
190 * This function is called by the main command loop.
192 void imap_copy(int num_parms, ConstStr *Params) {
195 if (num_parms != 4) {
196 IReply("BAD invalid parameters");
200 if (imap_is_message_set(Params[2].Key)) {
201 imap_pick_range(Params[2].Key, 0);
204 IReply("BAD invalid parameters");
208 ret = imap_do_copy(Params[3].Key);
210 IAPrintf("%s OK ", Params[0].Key);
211 imap_output_copyuid_response();
212 IAPuts("COPY completed\r\n");
215 IReplyPrintf("NO COPY failed (error %d)", ret);
220 * This function is called by the main command loop.
222 void imap_uidcopy(int num_parms, ConstStr *Params) {
224 if (num_parms != 5) {
225 IReply("BAD invalid parameters");
229 if (imap_is_message_set(Params[3].Key)) {
230 imap_pick_range(Params[3].Key, 1);
233 IReply("BAD invalid parameters");
237 if (imap_do_copy(Params[4].Key) == 0) {
238 IAPrintf("%s OK ", Params[0].Key);
239 imap_output_copyuid_response();
240 IAPuts("UID COPY completed\r\n");
243 IReply("NO UID COPY failed");
249 * imap_do_append_flags() is called by imap_append() to set any flags that
250 * the client specified at append time.
252 * FIXME find a way to do these in bulk so we don't max out our db journal
254 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
256 char this_flag[sizeof flags];
259 if (new_message_flags == NULL) return;
260 if (IsEmptyStr(new_message_flags)) return;
262 safestrncpy(flags, new_message_flags, sizeof flags);
264 for (i=0; i<num_tokens(flags, ' '); ++i) {
265 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
266 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
267 if (!strcasecmp(this_flag, "Seen")) {
268 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
271 if (!strcasecmp(this_flag, "Answered")) {
272 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
280 * This function is called by the main command loop.
282 void imap_append(int num_parms, ConstStr *Params) {
283 struct CitContext *CCC = CC;
285 struct CtdlMessage *msg = NULL;
286 long new_msgnum = (-1L);
288 char roomname[ROOMNAMELEN];
291 char savedroom[ROOMNAMELEN];
294 char new_message_flags[SIZ];
298 IReply("BAD usage error");
302 if ( (Params[num_parms-1].Key[0] != '{')
303 || (Params[num_parms-1].Key[Params[num_parms-1].len-1] != '}') ) {
304 IReply("BAD no message literal supplied");
308 strcpy(new_message_flags, "");
309 if (num_parms >= 5) {
310 for (i=3; i<num_parms; ++i) {
311 strcat(new_message_flags, Params[i].Key);
312 strcat(new_message_flags, " ");
314 stripallbut(new_message_flags, '(', ')');
317 /* This is how we'd do this if it were relevant in our data store.
318 * if (num_parms >= 6) {
319 * new_message_internaldate = parms[4];
323 literal_length = atol(&Params[num_parms-1].Key[1]);
324 if (literal_length < 1) {
325 IReply("BAD Message length must be at least 1.");
330 imap_free_transmitted_message(); /* just in case. */
332 Imap->TransmittedMessage = NewStrBufPlain(NULL, literal_length);
334 if (Imap->TransmittedMessage == NULL) {
335 IReply("NO Cannot allocate memory.");
339 IAPrintf("+ Transmit message now.\r\n");
343 client_read_blob(Imap->TransmittedMessage, literal_length, config.c_sleeping);
345 if ((ret < 0) || (StrLength(Imap->TransmittedMessage) < literal_length)) {
346 IReply("NO Read failed.");
350 /* Client will transmit a trailing CRLF after the literal (the message
351 * text) is received. This call to client_getln() absorbs it.
354 client_getln(dummy, sizeof dummy);
356 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
357 IMAPM_syslog(LOG_DEBUG, "Converting CRLF to LF");
358 StrBufToUnixLF(Imap->TransmittedMessage);
360 IMAPM_syslog(LOG_DEBUG, "Converting message format");
361 msg = convert_internet_message_buf(&Imap->TransmittedMessage);
363 ret = imap_grabroom(roomname, Params[2].Key, 1);
365 IReply("NO Invalid mailbox name or access denied");
370 * CtdlUserGoto() formally takes us to the desired room. (If another
371 * folder is selected, save its name so we can return there!!!!!)
373 if (Imap->selected) {
374 strcpy(savedroom, CC->room.QRname);
376 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
378 /* If the user is locally authenticated, FORCE the From: header to
379 * show up as the real sender. FIXME do we really want to do this?
380 * Probably should make it site-definable or even room-definable.
382 * For now, we allow "forgeries" if the room is one of the user's
386 if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (config.c_imap_keep_from == 0)) {
387 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
388 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
389 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
390 msg->cm_fields['A'] = strdup(CC->user.fullname);
391 msg->cm_fields['N'] = strdup(config.c_nodename);
392 msg->cm_fields['H'] = strdup(config.c_humannode);
399 ret = CtdlDoIHavePermissionToPostInThisRoom(errbuf, sizeof errbuf, NULL, POST_LOGGED_IN, 0);
402 /* Nope ... print an error message */
403 IReplyPrintf("NO %s", errbuf);
407 /* Yes ... go ahead and post! */
409 new_msgnum = CtdlSubmitMsg(msg, NULL, "", 0);
411 if (new_msgnum >= 0L) {
412 IReplyPrintf("OK [APPENDUID %ld %ld] APPEND completed",
413 GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
416 IReplyPrintf("BAD Error %ld saving message to disk.",
422 * IMAP protocol response to client has already been sent by now.
424 * If another folder is selected, go back to that room so we can resume
425 * our happy day without violent explosions.
427 if (Imap->selected) {
428 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
431 /* We don't need this buffer anymore */
432 CtdlFreeMessage(msg);
434 if (new_message_flags != NULL) {
435 imap_do_append_flags(new_msgnum, new_message_flags);