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, ctdlsetseen_seen, NULL, &qrbuf);
138 CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
139 CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
140 CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
148 gettimeofday(&tv3, NULL);
149 lprintf(CTDL_DEBUG, "Copying pointers: %ld microseconds / Setting flags: %ld microseconds\n",
150 (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000)),
151 (tv3.tv_usec + (tv3.tv_sec * 1000000)) - (tv2.tv_usec + (tv2.tv_sec * 1000000))
159 * This function is called by the main command loop.
161 void imap_copy(int num_parms, char *parms[]) {
164 if (num_parms != 4) {
165 cprintf("%s BAD invalid parameters\r\n", parms[0]);
169 if (imap_is_message_set(parms[2])) {
170 imap_pick_range(parms[2], 0);
173 cprintf("%s BAD invalid parameters\r\n", parms[0]);
177 ret = imap_do_copy(parms[3]);
179 cprintf("%s OK COPY completed\r\n", parms[0]);
182 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
187 * This function is called by the main command loop.
189 void imap_uidcopy(int num_parms, char *parms[]) {
191 if (num_parms != 5) {
192 cprintf("%s BAD invalid parameters\r\n", parms[0]);
196 if (imap_is_message_set(parms[3])) {
197 imap_pick_range(parms[3], 1);
200 cprintf("%s BAD invalid parameters\r\n", parms[0]);
204 if (imap_do_copy(parms[4]) == 0) {
205 cprintf("%s OK UID COPY completed\r\n", parms[0]);
208 cprintf("%s NO UID COPY failed\r\n", parms[0]);
214 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
216 void imap_print_instant_messages(void) {
217 struct ExpressMessage *ptr, *holdptr;
218 char *dumpomatic = NULL;
224 if (CC->FirstExpressMessage == NULL) {
227 begin_critical_section(S_SESSION_TABLE);
228 ptr = CC->FirstExpressMessage;
229 CC->FirstExpressMessage = NULL;
230 end_critical_section(S_SESSION_TABLE);
232 while (ptr != NULL) {
233 localtime_r(&(ptr->timestamp), &stamp);
234 size = strlen(ptr->text) + SIZ;
235 dumpomatic = malloc(size);
236 strcpy(dumpomatic, "");
237 if (ptr->flags && EM_BROADCAST)
238 strcat(dumpomatic, "Broadcast message ");
239 else if (ptr->flags && EM_CHAT)
240 strcat(dumpomatic, "Chat request ");
241 else if (ptr->flags && EM_GO_AWAY)
242 strcat(dumpomatic, "Please logoff now, as requested ");
244 strcat(dumpomatic, "Message ");
246 /* Timestamp. Can this be improved? */
247 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
248 sprintf(tmp, "at 12:%02d%cm",
250 stamp.tm_hour ? 'p' : 'a');
251 else if (stamp.tm_hour > 12) /* pm */
252 sprintf(tmp, "at %d:%02dpm",
256 sprintf(tmp, "at %d:%02dam",
257 stamp.tm_hour, stamp.tm_min);
258 strcat(dumpomatic, tmp);
260 size2 = strlen(dumpomatic);
261 snprintf(&dumpomatic[size2], size - size2,
262 " from %s:\n", ptr->sender);
263 if (ptr->text != NULL)
264 strcat(dumpomatic, ptr->text);
267 if (ptr->text != NULL) free(ptr->text);
271 for (i=0; i<strlen(dumpomatic); ++i) {
272 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
273 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
274 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
277 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
285 * imap_do_append_flags() is called by imap_append() to set any flags that
286 * the client specified at append time.
288 * FIXME find a way to do these in bulk so we don't max out our db journal
290 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
292 char this_flag[sizeof flags];
295 if (new_message_flags == NULL) return;
296 if (strlen(new_message_flags) == 0) return;
298 safestrncpy(flags, new_message_flags, sizeof flags);
300 for (i=0; i<num_tokens(flags, ' '); ++i) {
301 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
302 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
303 if (!strcasecmp(this_flag, "Seen")) {
304 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
307 if (!strcasecmp(this_flag, "Answered")) {
308 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
316 * This function is called by the main command loop.
318 void imap_append(int num_parms, char *parms[]) {
320 long bytes_transferred;
321 long stripped_length = 0;
322 struct CtdlMessage *msg;
323 long new_msgnum = (-1L);
325 char roomname[ROOMNAMELEN];
327 char savedroom[ROOMNAMELEN];
330 char new_message_flags[SIZ];
333 cprintf("%s BAD usage error\r\n", parms[0]);
337 if ( (parms[num_parms-1][0] != '{')
338 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
339 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
343 strcpy(new_message_flags, "");
344 if (num_parms >= 5) {
345 for (i=3; i<num_parms; ++i) {
346 strcat(new_message_flags, parms[i]);
347 strcat(new_message_flags, " ");
349 stripallbut(new_message_flags, '(', ')');
352 /* This is how we'd do this if it were relevant in our data store.
353 * if (num_parms >= 6) {
354 * new_message_internaldate = parms[4];
358 literal_length = atol(&parms[num_parms-1][1]);
359 if (literal_length < 1) {
360 cprintf("%s BAD Message length must be at least 1.\r\n",
365 imap_free_transmitted_message(); /* just in case. */
366 IMAP->transmitted_message = malloc(literal_length + 1);
367 if (IMAP->transmitted_message == NULL) {
368 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
371 IMAP->transmitted_length = literal_length;
373 cprintf("+ Transmit message now.\r\n");
375 bytes_transferred = 0;
377 ret = client_read(IMAP->transmitted_message, literal_length);
378 IMAP->transmitted_message[literal_length] = 0;
381 cprintf("%s NO Read failed.\r\n", parms[0]);
385 /* Client will transmit a trailing CRLF after the literal (the message
386 * text) is received. This call to client_getln() absorbs it.
389 client_getln(buf, sizeof buf);
391 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
392 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
394 for (i=0; i<literal_length; ++i) {
395 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
396 IMAP->transmitted_message[stripped_length++] =
397 IMAP->transmitted_message[i];
400 literal_length = stripped_length;
401 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
403 lprintf(CTDL_DEBUG, "Converting message format\n");
404 msg = convert_internet_message(IMAP->transmitted_message);
405 IMAP->transmitted_message = NULL;
406 IMAP->transmitted_length = 0;
408 ret = imap_grabroom(roomname, parms[2], 0);
410 cprintf("%s NO Invalid mailbox name or access denied\r\n",
416 * usergoto() formally takes us to the desired room. (If another
417 * folder is selected, save its name so we can return there!!!!!)
419 if (IMAP->selected) {
420 strcpy(savedroom, CC->room.QRname);
422 usergoto(roomname, 0, 0, &msgs, &new);
424 /* If the user is locally authenticated, FORCE the From: header to
425 * show up as the real sender. FIXME do we really want to do this?
426 * Probably should make it site-definable or even room-definable.
428 * For now, we allow "forgeries" if the room is one of the user's
432 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
433 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
434 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
435 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
436 msg->cm_fields['A'] = strdup(CC->user.fullname);
437 msg->cm_fields['N'] = strdup(config.c_nodename);
438 msg->cm_fields['H'] = strdup(config.c_humannode);
445 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
448 /* Nope ... print an error message */
449 cprintf("%s NO %s\r\n", parms[0], buf);
453 /* Yes ... go ahead and post! */
455 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
457 if (new_msgnum >= 0L) {
458 cprintf("%s OK APPEND completed\r\n", parms[0]);
461 cprintf("%s BAD Error %ld saving message to disk.\r\n",
462 parms[0], new_msgnum);
467 * IMAP protocol response to client has already been sent by now.
469 * If another folder is selected, go back to that room so we can resume
470 * our happy day without violent explosions.
472 if (IMAP->selected) {
473 usergoto(savedroom, 0, 0, &msgs, &new);
476 /* We don't need this buffer anymore */
477 CtdlFreeMessage(msg);
479 if (new_message_flags != NULL) {
480 imap_do_append_flags(new_msgnum, new_message_flags);