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;
69 if (IMAP->num_msgs < 1) {
73 i = imap_grabroom(roomname, destination_folder, 0);
74 if (i != 0) return(i);
77 * Optimization note ... ajc 2005oct09
79 * As we can see below, we're going to end up making lots of
80 * repeated calls to CtdlCopyMsgToRoom() if the user selects
81 * multiple messages and then does a copy or move operation.
83 * The plan (documented in this comment so I don't forget what I was
84 * going to do) is to refactor CtdlCopyMsgToRoom() so that it accepts
85 * a list of message ID's instead of a single one. Then we can alter
86 * the code which appears below, to copy all the message pointers in
90 gettimeofday(&tv1, NULL);
91 for (i = 0; i < IMAP->num_msgs; ++i) {
92 if (IMAP->flags[i] & IMAP_SELECTED) {
94 CtdlCopyMsgToRoom(IMAP->msgids[i], roomname);
98 /* Don't bother wasting any more time if there were no messages. */
99 if (num_selected == 0) {
103 /* Enumerate lists of messages for which flags are toggled */
104 long *seen_yes = NULL;
105 int num_seen_yes = 0;
106 long *seen_no = NULL;
108 long *answ_yes = NULL;
109 int num_answ_yes = 0;
110 long *answ_no = NULL;
113 seen_yes = malloc(num_selected * sizeof(long));
114 seen_no = malloc(num_selected * sizeof(long));
115 answ_yes = malloc(num_selected * sizeof(long));
116 answ_no = malloc(num_selected * sizeof(long));
118 for (i = 0; i < IMAP->num_msgs; ++i) {
119 if (IMAP->flags[i] & IMAP_SELECTED) {
120 if (IMAP->flags[i] & IMAP_SEEN) {
121 seen_yes[num_seen_yes++] = IMAP->msgids[i];
123 if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
124 seen_no[num_seen_no++] = IMAP->msgids[i];
126 if (IMAP->flags[i] & IMAP_ANSWERED) {
127 answ_yes[num_answ_yes++] = IMAP->msgids[i];
129 if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
130 answ_no[num_answ_no++] = IMAP->msgids[i];
135 /* Set the flags... */
136 gettimeofday(&tv2, NULL);
137 i = getroom(&qrbuf, roomname);
139 CtdlSetSeen(seen_yes, num_seen_yes, 1,
140 ctdlsetseen_seen, NULL, &qrbuf
142 CtdlSetSeen(seen_no, num_seen_no, 0,
143 ctdlsetseen_seen, NULL, &qrbuf
145 CtdlSetSeen(answ_yes, num_answ_yes, 1,
146 ctdlsetseen_answered, NULL, &qrbuf
148 CtdlSetSeen(answ_no, num_answ_no, 0,
149 ctdlsetseen_answered, NULL, &qrbuf
158 gettimeofday(&tv3, NULL);
159 lprintf(CTDL_DEBUG, "Copying the pointers took %ld microseconds\n",
160 (tv2.tv_usec + (tv2.tv_sec * 1000000))
161 - (tv1.tv_usec + (tv1.tv_sec * 1000000))
163 lprintf(CTDL_DEBUG, "Setting the flags took %ld microseconds\n",
164 (tv3.tv_usec + (tv3.tv_sec * 1000000))
165 - (tv2.tv_usec + (tv2.tv_sec * 1000000))
173 * This function is called by the main command loop.
175 void imap_copy(int num_parms, char *parms[]) {
178 if (num_parms != 4) {
179 cprintf("%s BAD invalid parameters\r\n", parms[0]);
183 if (imap_is_message_set(parms[2])) {
184 imap_pick_range(parms[2], 0);
187 cprintf("%s BAD invalid parameters\r\n", parms[0]);
191 ret = imap_do_copy(parms[3]);
193 cprintf("%s OK COPY completed\r\n", parms[0]);
196 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
201 * This function is called by the main command loop.
203 void imap_uidcopy(int num_parms, char *parms[]) {
205 if (num_parms != 5) {
206 cprintf("%s BAD invalid parameters\r\n", parms[0]);
210 if (imap_is_message_set(parms[3])) {
211 imap_pick_range(parms[3], 1);
214 cprintf("%s BAD invalid parameters\r\n", parms[0]);
218 if (imap_do_copy(parms[4]) == 0) {
219 cprintf("%s OK UID COPY completed\r\n", parms[0]);
222 cprintf("%s NO UID COPY failed\r\n", parms[0]);
228 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
230 void imap_print_instant_messages(void) {
231 struct ExpressMessage *ptr, *holdptr;
232 char *dumpomatic = NULL;
238 if (CC->FirstExpressMessage == NULL) {
241 begin_critical_section(S_SESSION_TABLE);
242 ptr = CC->FirstExpressMessage;
243 CC->FirstExpressMessage = NULL;
244 end_critical_section(S_SESSION_TABLE);
246 while (ptr != NULL) {
247 localtime_r(&(ptr->timestamp), &stamp);
248 size = strlen(ptr->text) + SIZ;
249 dumpomatic = malloc(size);
250 strcpy(dumpomatic, "");
251 if (ptr->flags && EM_BROADCAST)
252 strcat(dumpomatic, "Broadcast message ");
253 else if (ptr->flags && EM_CHAT)
254 strcat(dumpomatic, "Chat request ");
255 else if (ptr->flags && EM_GO_AWAY)
256 strcat(dumpomatic, "Please logoff now, as requested ");
258 strcat(dumpomatic, "Message ");
260 /* Timestamp. Can this be improved? */
261 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
262 sprintf(tmp, "at 12:%02d%cm",
264 stamp.tm_hour ? 'p' : 'a');
265 else if (stamp.tm_hour > 12) /* pm */
266 sprintf(tmp, "at %d:%02dpm",
270 sprintf(tmp, "at %d:%02dam",
271 stamp.tm_hour, stamp.tm_min);
272 strcat(dumpomatic, tmp);
274 size2 = strlen(dumpomatic);
275 snprintf(&dumpomatic[size2], size - size2,
276 " from %s:\n", ptr->sender);
277 if (ptr->text != NULL)
278 strcat(dumpomatic, ptr->text);
281 if (ptr->text != NULL) free(ptr->text);
285 for (i=0; i<strlen(dumpomatic); ++i) {
286 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
287 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
288 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
291 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
299 * imap_do_append_flags() is called by imap_append() to set any flags that
300 * the client specified at append time.
302 * FIXME find a way to do these in bulk so we don't max out our db journal
304 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
306 char this_flag[sizeof flags];
309 if (new_message_flags == NULL) return;
310 if (strlen(new_message_flags) == 0) return;
312 safestrncpy(flags, new_message_flags, sizeof flags);
314 for (i=0; i<num_tokens(flags, ' '); ++i) {
315 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
316 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
317 if (!strcasecmp(this_flag, "Seen")) {
318 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
321 if (!strcasecmp(this_flag, "Answered")) {
322 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
330 * This function is called by the main command loop.
332 void imap_append(int num_parms, char *parms[]) {
334 long bytes_transferred;
335 long stripped_length = 0;
336 struct CtdlMessage *msg;
337 long new_msgnum = (-1L);
339 char roomname[ROOMNAMELEN];
341 char savedroom[ROOMNAMELEN];
344 char new_message_flags[SIZ];
347 cprintf("%s BAD usage error\r\n", parms[0]);
351 if ( (parms[num_parms-1][0] != '{')
352 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
353 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
357 strcpy(new_message_flags, "");
358 if (num_parms >= 5) {
359 for (i=3; i<num_parms; ++i) {
360 strcat(new_message_flags, parms[i]);
361 strcat(new_message_flags, " ");
363 stripallbut(new_message_flags, '(', ')');
366 /* This is how we'd do this if it were relevant in our data store.
367 * if (num_parms >= 6) {
368 * new_message_internaldate = parms[4];
372 literal_length = atol(&parms[num_parms-1][1]);
373 if (literal_length < 1) {
374 cprintf("%s BAD Message length must be at least 1.\r\n",
379 imap_free_transmitted_message(); /* just in case. */
380 IMAP->transmitted_message = malloc(literal_length + 1);
381 if (IMAP->transmitted_message == NULL) {
382 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
385 IMAP->transmitted_length = literal_length;
387 cprintf("+ Transmit message now.\r\n");
389 bytes_transferred = 0;
391 ret = client_read(IMAP->transmitted_message, literal_length);
392 IMAP->transmitted_message[literal_length] = 0;
395 cprintf("%s NO Read failed.\r\n", parms[0]);
399 /* Client will transmit a trailing CRLF after the literal (the message
400 * text) is received. This call to client_getln() absorbs it.
403 client_getln(buf, sizeof buf);
405 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
406 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
408 for (i=0; i<literal_length; ++i) {
409 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
410 IMAP->transmitted_message[stripped_length++] =
411 IMAP->transmitted_message[i];
414 literal_length = stripped_length;
415 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
417 lprintf(CTDL_DEBUG, "Converting message format\n");
418 msg = convert_internet_message(IMAP->transmitted_message);
419 IMAP->transmitted_message = NULL;
420 IMAP->transmitted_length = 0;
422 ret = imap_grabroom(roomname, parms[2], 0);
424 cprintf("%s NO Invalid mailbox name or access denied\r\n",
430 * usergoto() formally takes us to the desired room. (If another
431 * folder is selected, save its name so we can return there!!!!!)
433 if (IMAP->selected) {
434 strcpy(savedroom, CC->room.QRname);
436 usergoto(roomname, 0, 0, &msgs, &new);
438 /* If the user is locally authenticated, FORCE the From: header to
439 * show up as the real sender. FIXME do we really want to do this?
440 * Probably should make it site-definable or even room-definable.
442 * For now, we allow "forgeries" if the room is one of the user's
446 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
447 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
448 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
449 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
450 msg->cm_fields['A'] = strdup(CC->user.fullname);
451 msg->cm_fields['N'] = strdup(config.c_nodename);
452 msg->cm_fields['H'] = strdup(config.c_humannode);
459 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
462 /* Nope ... print an error message */
463 cprintf("%s NO %s\r\n", parms[0], buf);
467 /* Yes ... go ahead and post! */
469 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
471 if (new_msgnum >= 0L) {
472 cprintf("%s OK APPEND completed\r\n", parms[0]);
475 cprintf("%s BAD Error %ld saving message to disk.\r\n",
476 parms[0], new_msgnum);
481 * IMAP protocol response to client has already been sent by now.
483 * If another folder is selected, go back to that room so we can resume
484 * our happy day without violent explosions.
486 if (IMAP->selected) {
487 usergoto(savedroom, 0, 0, &msgs, &new);
490 /* We don't need this buffer anymore */
491 CtdlFreeMessage(msg);
493 if (new_message_flags != NULL) {
494 imap_do_append_flags(new_msgnum, new_message_flags);