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;
67 if (IMAP->num_msgs < 1) {
71 i = imap_grabroom(roomname, destination_folder, 0);
72 if (i != 0) return(i);
75 * Optimization note ... ajc 2005oct09
77 * As we can see below, we're going to end up making lots of
78 * repeated calls to CtdlCopyMsgToRoom() if the user selects
79 * multiple messages and then does a copy or move operation.
81 * The plan (documented in this comment so I don't forget what I was
82 * going to do) is to refactor CtdlCopyMsgToRoom() so that it accepts
83 * a list of message ID's instead of a single one. Then we can alter
84 * the code which appears below, to copy all the message pointers in
87 * But the REAL resource waster is further down, where we call
88 * the CtdlSetSeen() API for each message moved. That's a HUGE
89 * performance drag. Fix that too.
92 for (i = 0; i < IMAP->num_msgs; ++i) {
93 if (IMAP->flags[i] & IMAP_SELECTED) {
94 CtdlCopyMsgToRoom(IMAP->msgids[i], roomname);
98 /* Set the flags... */
99 i = getroom(&qrbuf, roomname);
100 if (i != 0) return(i);
102 for (i = 0; i < IMAP->num_msgs; ++i) {
103 if (IMAP->flags[i] & IMAP_SELECTED) {
104 CtdlSetSeen(&IMAP->msgids[i], 1,
105 ((IMAP->flags[i] & IMAP_SEEN) ? 1 : 0),
109 CtdlSetSeen(&IMAP->msgids[i], 1,
110 ((IMAP->flags[i] & IMAP_ANSWERED) ? 1 : 0),
111 ctdlsetseen_answered,
123 * This function is called by the main command loop.
125 void imap_copy(int num_parms, char *parms[]) {
128 if (num_parms != 4) {
129 cprintf("%s BAD invalid parameters\r\n", parms[0]);
133 if (imap_is_message_set(parms[2])) {
134 imap_pick_range(parms[2], 0);
137 cprintf("%s BAD invalid parameters\r\n", parms[0]);
141 ret = imap_do_copy(parms[3]);
143 cprintf("%s OK COPY completed\r\n", parms[0]);
146 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
151 * This function is called by the main command loop.
153 void imap_uidcopy(int num_parms, char *parms[]) {
155 if (num_parms != 5) {
156 cprintf("%s BAD invalid parameters\r\n", parms[0]);
160 if (imap_is_message_set(parms[3])) {
161 imap_pick_range(parms[3], 1);
164 cprintf("%s BAD invalid parameters\r\n", parms[0]);
168 if (imap_do_copy(parms[4]) == 0) {
169 cprintf("%s OK UID COPY completed\r\n", parms[0]);
172 cprintf("%s NO UID COPY failed\r\n", parms[0]);
178 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
180 void imap_print_instant_messages(void) {
181 struct ExpressMessage *ptr, *holdptr;
182 char *dumpomatic = NULL;
188 if (CC->FirstExpressMessage == NULL) {
191 begin_critical_section(S_SESSION_TABLE);
192 ptr = CC->FirstExpressMessage;
193 CC->FirstExpressMessage = NULL;
194 end_critical_section(S_SESSION_TABLE);
196 while (ptr != NULL) {
197 localtime_r(&(ptr->timestamp), &stamp);
198 size = strlen(ptr->text) + SIZ;
199 dumpomatic = malloc(size);
200 strcpy(dumpomatic, "");
201 if (ptr->flags && EM_BROADCAST)
202 strcat(dumpomatic, "Broadcast message ");
203 else if (ptr->flags && EM_CHAT)
204 strcat(dumpomatic, "Chat request ");
205 else if (ptr->flags && EM_GO_AWAY)
206 strcat(dumpomatic, "Please logoff now, as requested ");
208 strcat(dumpomatic, "Message ");
210 /* Timestamp. Can this be improved? */
211 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
212 sprintf(tmp, "at 12:%02d%cm",
214 stamp.tm_hour ? 'p' : 'a');
215 else if (stamp.tm_hour > 12) /* pm */
216 sprintf(tmp, "at %d:%02dpm",
220 sprintf(tmp, "at %d:%02dam",
221 stamp.tm_hour, stamp.tm_min);
222 strcat(dumpomatic, tmp);
224 size2 = strlen(dumpomatic);
225 snprintf(&dumpomatic[size2], size - size2,
226 " from %s:\n", ptr->sender);
227 if (ptr->text != NULL)
228 strcat(dumpomatic, ptr->text);
231 if (ptr->text != NULL) free(ptr->text);
235 for (i=0; i<strlen(dumpomatic); ++i) {
236 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
237 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
238 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
241 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
249 * imap_do_append_flags() is called by imap_append() to set any flags that
250 * the client specified at append time.
252 * FIXME find a way to do these in bulk so we don't max out our db journal
254 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
256 char this_flag[sizeof flags];
259 if (new_message_flags == NULL) return;
260 if (strlen(new_message_flags) == 0) return;
262 safestrncpy(flags, new_message_flags, sizeof flags);
264 for (i=0; i<num_tokens(flags, ' '); ++i) {
265 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
266 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
267 if (!strcasecmp(this_flag, "Seen")) {
268 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
271 if (!strcasecmp(this_flag, "Answered")) {
272 CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
280 * This function is called by the main command loop.
282 void imap_append(int num_parms, char *parms[]) {
284 long bytes_transferred;
285 long stripped_length = 0;
286 struct CtdlMessage *msg;
287 long new_msgnum = (-1L);
289 char roomname[ROOMNAMELEN];
291 char savedroom[ROOMNAMELEN];
294 char new_message_flags[SIZ];
297 cprintf("%s BAD usage error\r\n", parms[0]);
301 if ( (parms[num_parms-1][0] != '{')
302 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
303 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
307 strcpy(new_message_flags, "");
308 if (num_parms >= 5) {
309 for (i=3; i<num_parms; ++i) {
310 strcat(new_message_flags, parms[i]);
311 strcat(new_message_flags, " ");
313 stripallbut(new_message_flags, '(', ')');
316 /* This is how we'd do this if it were relevant in our data store.
317 * if (num_parms >= 6) {
318 * new_message_internaldate = parms[4];
322 literal_length = atol(&parms[num_parms-1][1]);
323 if (literal_length < 1) {
324 cprintf("%s BAD Message length must be at least 1.\r\n",
329 imap_free_transmitted_message(); /* just in case. */
330 IMAP->transmitted_message = malloc(literal_length + 1);
331 if (IMAP->transmitted_message == NULL) {
332 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
335 IMAP->transmitted_length = literal_length;
337 cprintf("+ Transmit message now.\r\n");
339 bytes_transferred = 0;
341 ret = client_read(IMAP->transmitted_message, literal_length);
342 IMAP->transmitted_message[literal_length] = 0;
345 cprintf("%s NO Read failed.\r\n", parms[0]);
349 /* Client will transmit a trailing CRLF after the literal (the message
350 * text) is received. This call to client_getln() absorbs it.
353 client_getln(buf, sizeof buf);
355 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
356 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
358 for (i=0; i<literal_length; ++i) {
359 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
360 IMAP->transmitted_message[stripped_length++] =
361 IMAP->transmitted_message[i];
364 literal_length = stripped_length;
365 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
367 lprintf(CTDL_DEBUG, "Converting message format\n");
368 msg = convert_internet_message(IMAP->transmitted_message);
369 IMAP->transmitted_message = NULL;
370 IMAP->transmitted_length = 0;
372 ret = imap_grabroom(roomname, parms[2], 0);
374 cprintf("%s NO Invalid mailbox name or access denied\r\n",
380 * usergoto() formally takes us to the desired room. (If another
381 * folder is selected, save its name so we can return there!!!!!)
383 if (IMAP->selected) {
384 strcpy(savedroom, CC->room.QRname);
386 usergoto(roomname, 0, 0, &msgs, &new);
388 /* If the user is locally authenticated, FORCE the From: header to
389 * show up as the real sender. FIXME do we really want to do this?
390 * Probably should make it site-definable or even room-definable.
392 * For now, we allow "forgeries" if the room is one of the user's
396 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
397 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
398 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
399 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
400 msg->cm_fields['A'] = strdup(CC->user.fullname);
401 msg->cm_fields['N'] = strdup(config.c_nodename);
402 msg->cm_fields['H'] = strdup(config.c_humannode);
409 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
412 /* Nope ... print an error message */
413 cprintf("%s NO %s\r\n", parms[0], buf);
417 /* Yes ... go ahead and post! */
419 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
421 if (new_msgnum >= 0L) {
422 cprintf("%s OK APPEND completed\r\n", parms[0]);
425 cprintf("%s BAD Error %ld saving message to disk.\r\n",
426 parms[0], new_msgnum);
431 * IMAP protocol response to client has already been sent by now.
433 * If another folder is selected, go back to that room so we can resume
434 * our happy day without violent explosions.
436 if (IMAP->selected) {
437 usergoto(savedroom, 0, 0, &msgs, &new);
440 /* We don't need this buffer anymore */
441 CtdlFreeMessage(msg);
443 if (new_message_flags != NULL) {
444 imap_do_append_flags(new_msgnum, new_message_flags);