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"
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"
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);
149 * This function is called by the main command loop.
151 void imap_copy(int num_parms, char *parms[]) {
154 if (num_parms != 4) {
155 cprintf("%s BAD invalid parameters\r\n", parms[0]);
159 if (imap_is_message_set(parms[2])) {
160 imap_pick_range(parms[2], 0);
163 cprintf("%s BAD invalid parameters\r\n", parms[0]);
167 ret = imap_do_copy(parms[3]);
169 cprintf("%s OK COPY completed\r\n", parms[0]);
172 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
177 * This function is called by the main command loop.
179 void imap_uidcopy(int num_parms, char *parms[]) {
181 if (num_parms != 5) {
182 cprintf("%s BAD invalid parameters\r\n", parms[0]);
186 if (imap_is_message_set(parms[3])) {
187 imap_pick_range(parms[3], 1);
190 cprintf("%s BAD invalid parameters\r\n", parms[0]);
194 if (imap_do_copy(parms[4]) == 0) {
195 cprintf("%s OK UID COPY completed\r\n", parms[0]);
198 cprintf("%s NO UID COPY failed\r\n", parms[0]);
204 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
206 void imap_print_instant_messages(void) {
207 struct ExpressMessage *ptr, *holdptr;
208 char *dumpomatic = NULL;
214 if (CC->FirstExpressMessage == NULL) {
217 begin_critical_section(S_SESSION_TABLE);
218 ptr = CC->FirstExpressMessage;
219 CC->FirstExpressMessage = NULL;
220 end_critical_section(S_SESSION_TABLE);
222 while (ptr != NULL) {
223 localtime_r(&(ptr->timestamp), &stamp);
224 size = strlen(ptr->text) + SIZ;
225 dumpomatic = malloc(size);
226 strcpy(dumpomatic, "");
227 if (ptr->flags && EM_BROADCAST)
228 strcat(dumpomatic, "Broadcast message ");
229 else if (ptr->flags && EM_CHAT)
230 strcat(dumpomatic, "Chat request ");
231 else if (ptr->flags && EM_GO_AWAY)
232 strcat(dumpomatic, "Please logoff now, as requested ");
234 strcat(dumpomatic, "Message ");
236 /* Timestamp. Can this be improved? */
237 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
238 sprintf(tmp, "at 12:%02d%cm",
240 stamp.tm_hour ? 'p' : 'a');
241 else if (stamp.tm_hour > 12) /* pm */
242 sprintf(tmp, "at %d:%02dpm",
246 sprintf(tmp, "at %d:%02dam",
247 stamp.tm_hour, stamp.tm_min);
248 strcat(dumpomatic, tmp);
250 size2 = strlen(dumpomatic);
251 snprintf(&dumpomatic[size2], size - size2,
252 " from %s:\n", ptr->sender);
253 if (ptr->text != NULL)
254 strcat(dumpomatic, ptr->text);
257 if (ptr->text != NULL) free(ptr->text);
261 for (i=0; i<strlen(dumpomatic); ++i) {
262 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
263 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
264 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
267 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
275 * imap_do_append_flags() is called by imap_append() to set any flags that
276 * the client specified at append time.
278 * FIXME find a way to do these in bulk so we don't max out our db journal
280 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
282 char this_flag[sizeof flags];
285 if (new_message_flags == NULL) return;
286 if (strlen(new_message_flags) == 0) return;
288 safestrncpy(flags, new_message_flags, sizeof flags);
290 for (i=0; i<num_tokens(flags, ' '); ++i) {
291 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
292 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
293 if (!strcasecmp(this_flag, "Seen")) {
294 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
297 if (!strcasecmp(this_flag, "Answered")) {
298 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
306 * This function is called by the main command loop.
308 void imap_append(int num_parms, char *parms[]) {
310 long bytes_transferred;
311 long stripped_length = 0;
312 struct CtdlMessage *msg = NULL;
313 long new_msgnum = (-1L);
315 char roomname[ROOMNAMELEN];
317 char savedroom[ROOMNAMELEN];
320 char new_message_flags[SIZ];
323 cprintf("%s BAD usage error\r\n", parms[0]);
327 if ( (parms[num_parms-1][0] != '{')
328 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
329 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
333 strcpy(new_message_flags, "");
334 if (num_parms >= 5) {
335 for (i=3; i<num_parms; ++i) {
336 strcat(new_message_flags, parms[i]);
337 strcat(new_message_flags, " ");
339 stripallbut(new_message_flags, '(', ')');
342 /* This is how we'd do this if it were relevant in our data store.
343 * if (num_parms >= 6) {
344 * new_message_internaldate = parms[4];
348 literal_length = atol(&parms[num_parms-1][1]);
349 if (literal_length < 1) {
350 cprintf("%s BAD Message length must be at least 1.\r\n",
355 imap_free_transmitted_message(); /* just in case. */
356 IMAP->transmitted_message = malloc(literal_length + 1);
357 if (IMAP->transmitted_message == NULL) {
358 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
361 IMAP->transmitted_length = literal_length;
363 cprintf("+ Transmit message now.\r\n");
365 bytes_transferred = 0;
367 ret = client_read(IMAP->transmitted_message, literal_length);
368 IMAP->transmitted_message[literal_length] = 0;
371 cprintf("%s NO Read failed.\r\n", parms[0]);
375 /* Client will transmit a trailing CRLF after the literal (the message
376 * text) is received. This call to client_getln() absorbs it.
379 client_getln(buf, sizeof buf);
381 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
382 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
384 for (i=0; i<literal_length; ++i) {
385 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
386 IMAP->transmitted_message[stripped_length++] =
387 IMAP->transmitted_message[i];
390 literal_length = stripped_length;
391 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
393 lprintf(CTDL_DEBUG, "Converting message format\n");
394 msg = convert_internet_message(IMAP->transmitted_message);
395 IMAP->transmitted_message = NULL;
396 IMAP->transmitted_length = 0;
398 ret = imap_grabroom(roomname, parms[2], 0);
400 cprintf("%s NO Invalid mailbox name or access denied\r\n",
406 * usergoto() formally takes us to the desired room. (If another
407 * folder is selected, save its name so we can return there!!!!!)
409 if (IMAP->selected) {
410 strcpy(savedroom, CC->room.QRname);
412 usergoto(roomname, 0, 0, &msgs, &new);
414 /* If the user is locally authenticated, FORCE the From: header to
415 * show up as the real sender. FIXME do we really want to do this?
416 * Probably should make it site-definable or even room-definable.
418 * For now, we allow "forgeries" if the room is one of the user's
422 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
423 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
424 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
425 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
426 msg->cm_fields['A'] = strdup(CC->user.fullname);
427 msg->cm_fields['N'] = strdup(config.c_nodename);
428 msg->cm_fields['H'] = strdup(config.c_humannode);
435 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
438 /* Nope ... print an error message */
439 cprintf("%s NO %s\r\n", parms[0], buf);
443 /* Yes ... go ahead and post! */
445 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
447 if (new_msgnum >= 0L) {
448 cprintf("%s OK APPEND completed\r\n", parms[0]);
451 cprintf("%s BAD Error %ld saving message to disk.\r\n",
452 parms[0], new_msgnum);
457 * IMAP protocol response to client has already been sent by now.
459 * If another folder is selected, go back to that room so we can resume
460 * our happy day without violent explosions.
462 if (IMAP->selected) {
463 usergoto(savedroom, 0, 0, &msgs, &new);
466 /* We don't need this buffer anymore */
467 CtdlFreeMessage(msg);
469 if (new_message_flags != NULL) {
470 imap_do_append_flags(new_msgnum, new_message_flags);