16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
39 #include "serv_extensions.h"
46 #include "internet_addressing.h"
47 #include "serv_imap.h"
48 #include "imap_tools.h"
49 #include "imap_fetch.h"
50 #include "imap_misc.h"
59 * imap_copy() calls imap_do_copy() to do its actual work, once it's
60 * validated and boiled down the request a bit. (returns 0 on success)
62 int imap_do_copy(char *destination_folder) {
64 char roomname[ROOMNAMELEN];
65 struct ctdlroom qrbuf;
66 struct timeval tv1, tv2, tv3;
67 long *selected_msgs = NULL;
70 if (IMAP->num_msgs < 1) {
74 i = imap_grabroom(roomname, destination_folder, 0);
75 if (i != 0) return(i);
78 * Copy all the message pointers in one shot.
80 gettimeofday(&tv1, NULL);
82 selected_msgs = malloc(sizeof(long) * IMAP->num_msgs);
83 if (selected_msgs == NULL) return(-1);
85 for (i = 0; i < IMAP->num_msgs; ++i) {
86 if (IMAP->flags[i] & IMAP_SELECTED) {
87 selected_msgs[num_selected++] = IMAP->msgids[i];
91 if (num_selected > 0) {
92 CtdlCopyMsgsToRoom(selected_msgs, num_selected, roomname);
96 /* Don't bother wasting any more time if there were no messages. */
97 if (num_selected == 0) {
101 /* Enumerate lists of messages for which flags are toggled */
102 long *seen_yes = NULL;
103 int num_seen_yes = 0;
104 long *seen_no = NULL;
106 long *answ_yes = NULL;
107 int num_answ_yes = 0;
108 long *answ_no = NULL;
111 seen_yes = malloc(num_selected * sizeof(long));
112 seen_no = malloc(num_selected * sizeof(long));
113 answ_yes = malloc(num_selected * sizeof(long));
114 answ_no = malloc(num_selected * sizeof(long));
116 for (i = 0; i < IMAP->num_msgs; ++i) {
117 if (IMAP->flags[i] & IMAP_SELECTED) {
118 if (IMAP->flags[i] & IMAP_SEEN) {
119 seen_yes[num_seen_yes++] = IMAP->msgids[i];
121 if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
122 seen_no[num_seen_no++] = IMAP->msgids[i];
124 if (IMAP->flags[i] & IMAP_ANSWERED) {
125 answ_yes[num_answ_yes++] = IMAP->msgids[i];
127 if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
128 answ_no[num_answ_no++] = IMAP->msgids[i];
133 /* Set the flags... */
134 gettimeofday(&tv2, NULL);
135 i = getroom(&qrbuf, roomname);
137 CtdlSetSeen(seen_yes, num_seen_yes, 1,
138 ctdlsetseen_seen, NULL, &qrbuf
140 CtdlSetSeen(seen_no, num_seen_no, 0,
141 ctdlsetseen_seen, NULL, &qrbuf
143 CtdlSetSeen(answ_yes, num_answ_yes, 1,
144 ctdlsetseen_answered, NULL, &qrbuf
146 CtdlSetSeen(answ_no, num_answ_no, 0,
147 ctdlsetseen_answered, NULL, &qrbuf
156 gettimeofday(&tv3, NULL);
157 lprintf(CTDL_DEBUG, "Copying pointers: %ld microseconds / Setting flags: %ld microseconds\n",
158 (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000)),
159 (tv3.tv_usec + (tv3.tv_sec * 1000000)) - (tv2.tv_usec + (tv2.tv_sec * 1000000))
167 * This function is called by the main command loop.
169 void imap_copy(int num_parms, char *parms[]) {
172 if (num_parms != 4) {
173 cprintf("%s BAD invalid parameters\r\n", parms[0]);
177 if (imap_is_message_set(parms[2])) {
178 imap_pick_range(parms[2], 0);
181 cprintf("%s BAD invalid parameters\r\n", parms[0]);
185 ret = imap_do_copy(parms[3]);
187 cprintf("%s OK COPY completed\r\n", parms[0]);
190 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
195 * This function is called by the main command loop.
197 void imap_uidcopy(int num_parms, char *parms[]) {
199 if (num_parms != 5) {
200 cprintf("%s BAD invalid parameters\r\n", parms[0]);
204 if (imap_is_message_set(parms[3])) {
205 imap_pick_range(parms[3], 1);
208 cprintf("%s BAD invalid parameters\r\n", parms[0]);
212 if (imap_do_copy(parms[4]) == 0) {
213 cprintf("%s OK UID COPY completed\r\n", parms[0]);
216 cprintf("%s NO UID COPY failed\r\n", parms[0]);
222 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
224 void imap_print_instant_messages(void) {
225 struct ExpressMessage *ptr, *holdptr;
226 char *dumpomatic = NULL;
232 if (CC->FirstExpressMessage == NULL) {
235 begin_critical_section(S_SESSION_TABLE);
236 ptr = CC->FirstExpressMessage;
237 CC->FirstExpressMessage = NULL;
238 end_critical_section(S_SESSION_TABLE);
240 while (ptr != NULL) {
241 localtime_r(&(ptr->timestamp), &stamp);
242 size = strlen(ptr->text) + SIZ;
243 dumpomatic = malloc(size);
244 strcpy(dumpomatic, "");
245 if (ptr->flags && EM_BROADCAST)
246 strcat(dumpomatic, "Broadcast message ");
247 else if (ptr->flags && EM_CHAT)
248 strcat(dumpomatic, "Chat request ");
249 else if (ptr->flags && EM_GO_AWAY)
250 strcat(dumpomatic, "Please logoff now, as requested ");
252 strcat(dumpomatic, "Message ");
254 /* Timestamp. Can this be improved? */
255 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
256 sprintf(tmp, "at 12:%02d%cm",
258 stamp.tm_hour ? 'p' : 'a');
259 else if (stamp.tm_hour > 12) /* pm */
260 sprintf(tmp, "at %d:%02dpm",
264 sprintf(tmp, "at %d:%02dam",
265 stamp.tm_hour, stamp.tm_min);
266 strcat(dumpomatic, tmp);
268 size2 = strlen(dumpomatic);
269 snprintf(&dumpomatic[size2], size - size2,
270 " from %s:\n", ptr->sender);
271 if (ptr->text != NULL)
272 strcat(dumpomatic, ptr->text);
275 if (ptr->text != NULL) free(ptr->text);
279 for (i=0; i<strlen(dumpomatic); ++i) {
280 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
281 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
282 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
285 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
293 * imap_do_append_flags() is called by imap_append() to set any flags that
294 * the client specified at append time.
296 * FIXME find a way to do these in bulk so we don't max out our db journal
298 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
300 char this_flag[sizeof flags];
303 if (new_message_flags == NULL) return;
304 if (strlen(new_message_flags) == 0) return;
306 safestrncpy(flags, new_message_flags, sizeof flags);
308 for (i=0; i<num_tokens(flags, ' '); ++i) {
309 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
310 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
311 if (!strcasecmp(this_flag, "Seen")) {
312 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
315 if (!strcasecmp(this_flag, "Answered")) {
316 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
324 * This function is called by the main command loop.
326 void imap_append(int num_parms, char *parms[]) {
328 long bytes_transferred;
329 long stripped_length = 0;
330 struct CtdlMessage *msg;
331 long new_msgnum = (-1L);
333 char roomname[ROOMNAMELEN];
335 char savedroom[ROOMNAMELEN];
338 char new_message_flags[SIZ];
341 cprintf("%s BAD usage error\r\n", parms[0]);
345 if ( (parms[num_parms-1][0] != '{')
346 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
347 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
351 strcpy(new_message_flags, "");
352 if (num_parms >= 5) {
353 for (i=3; i<num_parms; ++i) {
354 strcat(new_message_flags, parms[i]);
355 strcat(new_message_flags, " ");
357 stripallbut(new_message_flags, '(', ')');
360 /* This is how we'd do this if it were relevant in our data store.
361 * if (num_parms >= 6) {
362 * new_message_internaldate = parms[4];
366 literal_length = atol(&parms[num_parms-1][1]);
367 if (literal_length < 1) {
368 cprintf("%s BAD Message length must be at least 1.\r\n",
373 imap_free_transmitted_message(); /* just in case. */
374 IMAP->transmitted_message = malloc(literal_length + 1);
375 if (IMAP->transmitted_message == NULL) {
376 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
379 IMAP->transmitted_length = literal_length;
381 cprintf("+ Transmit message now.\r\n");
383 bytes_transferred = 0;
385 ret = client_read(IMAP->transmitted_message, literal_length);
386 IMAP->transmitted_message[literal_length] = 0;
389 cprintf("%s NO Read failed.\r\n", parms[0]);
393 /* Client will transmit a trailing CRLF after the literal (the message
394 * text) is received. This call to client_getln() absorbs it.
397 client_getln(buf, sizeof buf);
399 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
400 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
402 for (i=0; i<literal_length; ++i) {
403 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
404 IMAP->transmitted_message[stripped_length++] =
405 IMAP->transmitted_message[i];
408 literal_length = stripped_length;
409 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
411 lprintf(CTDL_DEBUG, "Converting message format\n");
412 msg = convert_internet_message(IMAP->transmitted_message);
413 IMAP->transmitted_message = NULL;
414 IMAP->transmitted_length = 0;
416 ret = imap_grabroom(roomname, parms[2], 0);
418 cprintf("%s NO Invalid mailbox name or access denied\r\n",
424 * usergoto() formally takes us to the desired room. (If another
425 * folder is selected, save its name so we can return there!!!!!)
427 if (IMAP->selected) {
428 strcpy(savedroom, CC->room.QRname);
430 usergoto(roomname, 0, 0, &msgs, &new);
432 /* If the user is locally authenticated, FORCE the From: header to
433 * show up as the real sender. FIXME do we really want to do this?
434 * Probably should make it site-definable or even room-definable.
436 * For now, we allow "forgeries" if the room is one of the user's
440 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
441 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
442 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
443 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
444 msg->cm_fields['A'] = strdup(CC->user.fullname);
445 msg->cm_fields['N'] = strdup(config.c_nodename);
446 msg->cm_fields['H'] = strdup(config.c_humannode);
453 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
456 /* Nope ... print an error message */
457 cprintf("%s NO %s\r\n", parms[0], buf);
461 /* Yes ... go ahead and post! */
463 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
465 if (new_msgnum >= 0L) {
466 cprintf("%s OK APPEND completed\r\n", parms[0]);
469 cprintf("%s BAD Error %ld saving message to disk.\r\n",
470 parms[0], new_msgnum);
475 * IMAP protocol response to client has already been sent by now.
477 * If another folder is selected, go back to that room so we can resume
478 * our happy day without violent explosions.
480 if (IMAP->selected) {
481 usergoto(savedroom, 0, 0, &msgs, &new);
484 /* We don't need this buffer anymore */
485 CtdlFreeMessage(msg);
487 if (new_message_flags != NULL) {
488 imap_do_append_flags(new_msgnum, new_message_flags);