16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
33 #include <libcitadel.h>
36 #include "sysdep_decls.h"
37 #include "citserver.h"
45 #include "internet_addressing.h"
46 #include "serv_imap.h"
47 #include "imap_tools.h"
48 #include "imap_fetch.h"
49 #include "imap_misc.h"
51 #include "ctdl_module.h"
58 * imap_copy() calls imap_do_copy() to do its actual work, once it's
59 * validated and boiled down the request a bit. (returns 0 on success)
61 int imap_do_copy(char *destination_folder) {
63 char roomname[ROOMNAMELEN];
64 struct ctdlroom qrbuf;
65 long *selected_msgs = NULL;
68 if (IMAP->num_msgs < 1) {
72 i = imap_grabroom(roomname, destination_folder, 0);
73 if (i != 0) return(i);
76 * Copy all the message pointers in one shot.
78 selected_msgs = malloc(sizeof(long) * IMAP->num_msgs);
79 if (selected_msgs == NULL) return(-1);
81 for (i = 0; i < IMAP->num_msgs; ++i) {
82 if (IMAP->flags[i] & IMAP_SELECTED) {
83 selected_msgs[num_selected++] = IMAP->msgids[i];
87 if (num_selected > 0) {
88 CtdlCopyMsgsToRoom(selected_msgs, num_selected, roomname);
92 /* Don't bother wasting any more time if there were no messages. */
93 if (num_selected == 0) {
97 /* Enumerate lists of messages for which flags are toggled */
98 long *seen_yes = NULL;
100 long *seen_no = NULL;
102 long *answ_yes = NULL;
103 int num_answ_yes = 0;
104 long *answ_no = NULL;
107 seen_yes = malloc(num_selected * sizeof(long));
108 seen_no = malloc(num_selected * sizeof(long));
109 answ_yes = malloc(num_selected * sizeof(long));
110 answ_no = malloc(num_selected * sizeof(long));
112 for (i = 0; i < IMAP->num_msgs; ++i) {
113 if (IMAP->flags[i] & IMAP_SELECTED) {
114 if (IMAP->flags[i] & IMAP_SEEN) {
115 seen_yes[num_seen_yes++] = IMAP->msgids[i];
117 if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
118 seen_no[num_seen_no++] = IMAP->msgids[i];
120 if (IMAP->flags[i] & IMAP_ANSWERED) {
121 answ_yes[num_answ_yes++] = IMAP->msgids[i];
123 if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
124 answ_no[num_answ_no++] = IMAP->msgids[i];
129 /* Set the flags... */
130 i = getroom(&qrbuf, roomname);
132 CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
133 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
134 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
135 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
148 * Output the [COPYUID xxx yyy] response code required by RFC2359
149 * to tell the client the UID's of the messages that were copied (if any).
150 * We are assuming that the IMAP_SELECTED flag is still set on any relevant
151 * messages in our source room. Since the Citadel system uses UID's that
152 * are both globally unique and persistent across a room-to-room copy, we
153 * can get this done quite easily.
155 void imap_output_copyuid_response(void) {
159 for (i = 0; i < IMAP->num_msgs; ++i) {
160 if (IMAP->flags[i] & IMAP_SELECTED) {
162 if (num_output == 1) {
163 cprintf("[COPYUID ");
165 else if (num_output > 1) {
168 cprintf("%ld", IMAP->msgids[i]);
171 if (num_output > 0) {
178 * This function is called by the main command loop.
180 void imap_copy(int num_parms, char *parms[]) {
183 if (num_parms != 4) {
184 cprintf("%s BAD invalid parameters\r\n", parms[0]);
188 if (imap_is_message_set(parms[2])) {
189 imap_pick_range(parms[2], 0);
192 cprintf("%s BAD invalid parameters\r\n", parms[0]);
196 ret = imap_do_copy(parms[3]);
198 cprintf("%s OK ", parms[0]);
199 imap_output_copyuid_response();
200 cprintf("COPY completed\r\n");
203 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
208 * This function is called by the main command loop.
210 void imap_uidcopy(int num_parms, char *parms[]) {
212 if (num_parms != 5) {
213 cprintf("%s BAD invalid parameters\r\n", parms[0]);
217 if (imap_is_message_set(parms[3])) {
218 imap_pick_range(parms[3], 1);
221 cprintf("%s BAD invalid parameters\r\n", parms[0]);
225 if (imap_do_copy(parms[4]) == 0) {
226 cprintf("%s OK ", parms[0]);
227 imap_output_copyuid_response();
228 cprintf("UID COPY completed\r\n");
231 cprintf("%s NO UID COPY failed\r\n", parms[0]);
237 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
239 void imap_print_instant_messages(void) {
240 struct ExpressMessage *ptr, *holdptr;
241 char *dumpomatic = NULL;
247 if (CC->FirstExpressMessage == NULL) {
250 begin_critical_section(S_SESSION_TABLE);
251 ptr = CC->FirstExpressMessage;
252 CC->FirstExpressMessage = NULL;
253 end_critical_section(S_SESSION_TABLE);
255 while (ptr != NULL) {
256 localtime_r(&(ptr->timestamp), &stamp);
257 size = strlen(ptr->text) + SIZ;
258 dumpomatic = malloc(size);
259 strcpy(dumpomatic, "");
260 if (ptr->flags && EM_BROADCAST)
261 strcat(dumpomatic, "Broadcast message ");
262 else if (ptr->flags && EM_CHAT)
263 strcat(dumpomatic, "Chat request ");
264 else if (ptr->flags && EM_GO_AWAY)
265 strcat(dumpomatic, "Please logoff now, as requested ");
267 strcat(dumpomatic, "Message ");
269 /* Timestamp. Can this be improved? */
270 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
271 sprintf(tmp, "at 12:%02d%cm",
273 stamp.tm_hour ? 'p' : 'a');
274 else if (stamp.tm_hour > 12) /* pm */
275 sprintf(tmp, "at %d:%02dpm",
279 sprintf(tmp, "at %d:%02dam",
280 stamp.tm_hour, stamp.tm_min);
281 strcat(dumpomatic, tmp);
283 size2 = strlen(dumpomatic);
284 snprintf(&dumpomatic[size2], size - size2,
285 " from %s:\n", ptr->sender);
286 if (ptr->text != NULL)
287 strcat(dumpomatic, ptr->text);
290 if (ptr->text != NULL) free(ptr->text);
294 for (i=0; dumpomatic[i]; ++i) {
295 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
296 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
297 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
300 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
308 * imap_do_append_flags() is called by imap_append() to set any flags that
309 * the client specified at append time.
311 * FIXME find a way to do these in bulk so we don't max out our db journal
313 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
315 char this_flag[sizeof flags];
318 if (new_message_flags == NULL) return;
319 if (IsEmptyStr(new_message_flags)) return;
321 safestrncpy(flags, new_message_flags, sizeof flags);
323 for (i=0; i<num_tokens(flags, ' '); ++i) {
324 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
325 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
326 if (!strcasecmp(this_flag, "Seen")) {
327 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
330 if (!strcasecmp(this_flag, "Answered")) {
331 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
339 * This function is called by the main command loop.
341 void imap_append(int num_parms, char *parms[]) {
343 long bytes_transferred;
344 long stripped_length = 0;
345 struct CtdlMessage *msg = NULL;
346 long new_msgnum = (-1L);
348 char roomname[ROOMNAMELEN];
350 char savedroom[ROOMNAMELEN];
353 char new_message_flags[SIZ];
354 struct citimap *Imap;
357 cprintf("%s BAD usage error\r\n", parms[0]);
361 if ( (parms[num_parms-1][0] != '{')
362 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
363 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
367 strcpy(new_message_flags, "");
368 if (num_parms >= 5) {
369 for (i=3; i<num_parms; ++i) {
370 strcat(new_message_flags, parms[i]);
371 strcat(new_message_flags, " ");
373 stripallbut(new_message_flags, '(', ')');
376 /* This is how we'd do this if it were relevant in our data store.
377 * if (num_parms >= 6) {
378 * new_message_internaldate = parms[4];
382 literal_length = atol(&parms[num_parms-1][1]);
383 if (literal_length < 1) {
384 cprintf("%s BAD Message length must be at least 1.\r\n",
390 imap_free_transmitted_message(); /* just in case. */
391 Imap->transmitted_message = malloc(literal_length + 1);
392 if (Imap->transmitted_message == NULL) {
393 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
396 Imap->transmitted_length = literal_length;
398 cprintf("+ Transmit message now.\r\n");
400 bytes_transferred = 0;
402 ret = client_read(Imap->transmitted_message, literal_length);
403 Imap->transmitted_message[literal_length] = 0;
406 cprintf("%s NO Read failed.\r\n", parms[0]);
410 /* Client will transmit a trailing CRLF after the literal (the message
411 * text) is received. This call to client_getln() absorbs it.
414 client_getln(buf, sizeof buf);
416 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
417 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
419 for (i=0; i<literal_length; ++i) {
420 if (strncmp(&Imap->transmitted_message[i], "\r\n", 2)) {
421 Imap->transmitted_message[stripped_length++] =
422 Imap->transmitted_message[i];
425 literal_length = stripped_length;
426 Imap->transmitted_message[literal_length] = 0; /* reterminate it */
428 lprintf(CTDL_DEBUG, "Converting message format\n");
429 msg = convert_internet_message(Imap->transmitted_message);
430 Imap->transmitted_message = NULL;
431 Imap->transmitted_length = 0;
433 ret = imap_grabroom(roomname, parms[2], 0);
435 cprintf("%s NO Invalid mailbox name or access denied\r\n",
441 * usergoto() formally takes us to the desired room. (If another
442 * folder is selected, save its name so we can return there!!!!!)
444 if (Imap->selected) {
445 strcpy(savedroom, CC->room.QRname);
447 usergoto(roomname, 0, 0, &msgs, &new);
449 /* If the user is locally authenticated, FORCE the From: header to
450 * show up as the real sender. FIXME do we really want to do this?
451 * Probably should make it site-definable or even room-definable.
453 * For now, we allow "forgeries" if the room is one of the user's
457 if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (config.c_imap_keep_from == 0)) {
458 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
459 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
460 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
461 msg->cm_fields['A'] = strdup(CC->user.fullname);
462 msg->cm_fields['N'] = strdup(config.c_nodename);
463 msg->cm_fields['H'] = strdup(config.c_humannode);
470 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf, NULL, POST_LOGGED_IN);
473 /* Nope ... print an error message */
474 cprintf("%s NO %s\r\n", parms[0], buf);
478 /* Yes ... go ahead and post! */
480 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
482 if (new_msgnum >= 0L) {
483 cprintf("%s OK [APPENDUID %ld %ld] APPEND completed\r\n",
484 parms[0], GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
487 cprintf("%s BAD Error %ld saving message to disk.\r\n",
488 parms[0], new_msgnum);
493 * IMAP protocol response to client has already been sent by now.
495 * If another folder is selected, go back to that room so we can resume
496 * our happy day without violent explosions.
498 if (Imap->selected) {
499 usergoto(savedroom, 0, 0, &msgs, &new);
502 /* We don't need this buffer anymore */
503 CtdlFreeMessage(msg);
505 if (new_message_flags != NULL) {
506 imap_do_append_flags(new_msgnum, new_message_flags);