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 pointers: %ld microseconds / Setting flags: %ld microseconds\n",
160 (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000)),
161 (tv3.tv_usec + (tv3.tv_sec * 1000000)) - (tv2.tv_usec + (tv2.tv_sec * 1000000))
169 * This function is called by the main command loop.
171 void imap_copy(int num_parms, char *parms[]) {
174 if (num_parms != 4) {
175 cprintf("%s BAD invalid parameters\r\n", parms[0]);
179 if (imap_is_message_set(parms[2])) {
180 imap_pick_range(parms[2], 0);
183 cprintf("%s BAD invalid parameters\r\n", parms[0]);
187 ret = imap_do_copy(parms[3]);
189 cprintf("%s OK COPY completed\r\n", parms[0]);
192 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
197 * This function is called by the main command loop.
199 void imap_uidcopy(int num_parms, char *parms[]) {
201 if (num_parms != 5) {
202 cprintf("%s BAD invalid parameters\r\n", parms[0]);
206 if (imap_is_message_set(parms[3])) {
207 imap_pick_range(parms[3], 1);
210 cprintf("%s BAD invalid parameters\r\n", parms[0]);
214 if (imap_do_copy(parms[4]) == 0) {
215 cprintf("%s OK UID COPY completed\r\n", parms[0]);
218 cprintf("%s NO UID COPY failed\r\n", parms[0]);
224 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
226 void imap_print_instant_messages(void) {
227 struct ExpressMessage *ptr, *holdptr;
228 char *dumpomatic = NULL;
234 if (CC->FirstExpressMessage == NULL) {
237 begin_critical_section(S_SESSION_TABLE);
238 ptr = CC->FirstExpressMessage;
239 CC->FirstExpressMessage = NULL;
240 end_critical_section(S_SESSION_TABLE);
242 while (ptr != NULL) {
243 localtime_r(&(ptr->timestamp), &stamp);
244 size = strlen(ptr->text) + SIZ;
245 dumpomatic = malloc(size);
246 strcpy(dumpomatic, "");
247 if (ptr->flags && EM_BROADCAST)
248 strcat(dumpomatic, "Broadcast message ");
249 else if (ptr->flags && EM_CHAT)
250 strcat(dumpomatic, "Chat request ");
251 else if (ptr->flags && EM_GO_AWAY)
252 strcat(dumpomatic, "Please logoff now, as requested ");
254 strcat(dumpomatic, "Message ");
256 /* Timestamp. Can this be improved? */
257 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
258 sprintf(tmp, "at 12:%02d%cm",
260 stamp.tm_hour ? 'p' : 'a');
261 else if (stamp.tm_hour > 12) /* pm */
262 sprintf(tmp, "at %d:%02dpm",
266 sprintf(tmp, "at %d:%02dam",
267 stamp.tm_hour, stamp.tm_min);
268 strcat(dumpomatic, tmp);
270 size2 = strlen(dumpomatic);
271 snprintf(&dumpomatic[size2], size - size2,
272 " from %s:\n", ptr->sender);
273 if (ptr->text != NULL)
274 strcat(dumpomatic, ptr->text);
277 if (ptr->text != NULL) free(ptr->text);
281 for (i=0; i<strlen(dumpomatic); ++i) {
282 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
283 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
284 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
287 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
295 * imap_do_append_flags() is called by imap_append() to set any flags that
296 * the client specified at append time.
298 * FIXME find a way to do these in bulk so we don't max out our db journal
300 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
302 char this_flag[sizeof flags];
305 if (new_message_flags == NULL) return;
306 if (strlen(new_message_flags) == 0) return;
308 safestrncpy(flags, new_message_flags, sizeof flags);
310 for (i=0; i<num_tokens(flags, ' '); ++i) {
311 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
312 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
313 if (!strcasecmp(this_flag, "Seen")) {
314 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
317 if (!strcasecmp(this_flag, "Answered")) {
318 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
326 * This function is called by the main command loop.
328 void imap_append(int num_parms, char *parms[]) {
330 long bytes_transferred;
331 long stripped_length = 0;
332 struct CtdlMessage *msg;
333 long new_msgnum = (-1L);
335 char roomname[ROOMNAMELEN];
337 char savedroom[ROOMNAMELEN];
340 char new_message_flags[SIZ];
343 cprintf("%s BAD usage error\r\n", parms[0]);
347 if ( (parms[num_parms-1][0] != '{')
348 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
349 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
353 strcpy(new_message_flags, "");
354 if (num_parms >= 5) {
355 for (i=3; i<num_parms; ++i) {
356 strcat(new_message_flags, parms[i]);
357 strcat(new_message_flags, " ");
359 stripallbut(new_message_flags, '(', ')');
362 /* This is how we'd do this if it were relevant in our data store.
363 * if (num_parms >= 6) {
364 * new_message_internaldate = parms[4];
368 literal_length = atol(&parms[num_parms-1][1]);
369 if (literal_length < 1) {
370 cprintf("%s BAD Message length must be at least 1.\r\n",
375 imap_free_transmitted_message(); /* just in case. */
376 IMAP->transmitted_message = malloc(literal_length + 1);
377 if (IMAP->transmitted_message == NULL) {
378 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
381 IMAP->transmitted_length = literal_length;
383 cprintf("+ Transmit message now.\r\n");
385 bytes_transferred = 0;
387 ret = client_read(IMAP->transmitted_message, literal_length);
388 IMAP->transmitted_message[literal_length] = 0;
391 cprintf("%s NO Read failed.\r\n", parms[0]);
395 /* Client will transmit a trailing CRLF after the literal (the message
396 * text) is received. This call to client_getln() absorbs it.
399 client_getln(buf, sizeof buf);
401 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
402 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
404 for (i=0; i<literal_length; ++i) {
405 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
406 IMAP->transmitted_message[stripped_length++] =
407 IMAP->transmitted_message[i];
410 literal_length = stripped_length;
411 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
413 lprintf(CTDL_DEBUG, "Converting message format\n");
414 msg = convert_internet_message(IMAP->transmitted_message);
415 IMAP->transmitted_message = NULL;
416 IMAP->transmitted_length = 0;
418 ret = imap_grabroom(roomname, parms[2], 0);
420 cprintf("%s NO Invalid mailbox name or access denied\r\n",
426 * usergoto() formally takes us to the desired room. (If another
427 * folder is selected, save its name so we can return there!!!!!)
429 if (IMAP->selected) {
430 strcpy(savedroom, CC->room.QRname);
432 usergoto(roomname, 0, 0, &msgs, &new);
434 /* If the user is locally authenticated, FORCE the From: header to
435 * show up as the real sender. FIXME do we really want to do this?
436 * Probably should make it site-definable or even room-definable.
438 * For now, we allow "forgeries" if the room is one of the user's
442 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
443 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
444 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
445 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
446 msg->cm_fields['A'] = strdup(CC->user.fullname);
447 msg->cm_fields['N'] = strdup(config.c_nodename);
448 msg->cm_fields['H'] = strdup(config.c_humannode);
455 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
458 /* Nope ... print an error message */
459 cprintf("%s NO %s\r\n", parms[0], buf);
463 /* Yes ... go ahead and post! */
465 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
467 if (new_msgnum >= 0L) {
468 cprintf("%s OK APPEND completed\r\n", parms[0]);
471 cprintf("%s BAD Error %ld saving message to disk.\r\n",
472 parms[0], new_msgnum);
477 * IMAP protocol response to client has already been sent by now.
479 * If another folder is selected, go back to that room so we can resume
480 * our happy day without violent explosions.
482 if (IMAP->selected) {
483 usergoto(savedroom, 0, 0, &msgs, &new);
486 /* We don't need this buffer anymore */
487 CtdlFreeMessage(msg);
489 if (new_message_flags != NULL) {
490 imap_do_append_flags(new_msgnum, new_message_flags);