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);
74 for (i = 0; i < IMAP->num_msgs; ++i) {
75 if (IMAP->flags[i] & IMAP_SELECTED) {
76 CtdlCopyMsgToRoom(IMAP->msgids[i], roomname);
80 /* Set the flags... */
81 i = getroom(&qrbuf, roomname);
82 if (i != 0) return(i);
84 for (i = 0; i < IMAP->num_msgs; ++i) {
85 if (IMAP->flags[i] & IMAP_SELECTED) {
86 CtdlSetSeen(IMAP->msgids[i],
87 ((IMAP->flags[i] & IMAP_SEEN) ? 1 : 0),
91 CtdlSetSeen(IMAP->msgids[i],
92 ((IMAP->flags[i] & IMAP_ANSWERED) ? 1 : 0),
105 * This function is called by the main command loop.
107 void imap_copy(int num_parms, char *parms[]) {
110 if (num_parms != 4) {
111 cprintf("%s BAD invalid parameters\r\n", parms[0]);
115 if (imap_is_message_set(parms[2])) {
116 imap_pick_range(parms[2], 0);
119 cprintf("%s BAD invalid parameters\r\n", parms[0]);
123 ret = imap_do_copy(parms[3]);
125 cprintf("%s OK COPY completed\r\n", parms[0]);
128 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
133 * This function is called by the main command loop.
135 void imap_uidcopy(int num_parms, char *parms[]) {
137 if (num_parms != 5) {
138 cprintf("%s BAD invalid parameters\r\n", parms[0]);
142 if (imap_is_message_set(parms[3])) {
143 imap_pick_range(parms[3], 1);
146 cprintf("%s BAD invalid parameters\r\n", parms[0]);
150 if (imap_do_copy(parms[4]) == 0) {
151 cprintf("%s OK UID COPY completed\r\n", parms[0]);
154 cprintf("%s NO UID COPY failed\r\n", parms[0]);
160 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
162 void imap_print_instant_messages(void) {
163 struct ExpressMessage *ptr, *holdptr;
164 char *dumpomatic = NULL;
170 if (CC->FirstExpressMessage == NULL) {
173 begin_critical_section(S_SESSION_TABLE);
174 ptr = CC->FirstExpressMessage;
175 CC->FirstExpressMessage = NULL;
176 end_critical_section(S_SESSION_TABLE);
178 while (ptr != NULL) {
179 localtime_r(&(ptr->timestamp), &stamp);
180 size = strlen(ptr->text) + SIZ;
181 dumpomatic = malloc(size);
182 strcpy(dumpomatic, "");
183 if (ptr->flags && EM_BROADCAST)
184 strcat(dumpomatic, "Broadcast message ");
185 else if (ptr->flags && EM_CHAT)
186 strcat(dumpomatic, "Chat request ");
187 else if (ptr->flags && EM_GO_AWAY)
188 strcat(dumpomatic, "Please logoff now, as requested ");
190 strcat(dumpomatic, "Message ");
192 /* Timestamp. Can this be improved? */
193 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
194 sprintf(tmp, "at 12:%02d%cm",
196 stamp.tm_hour ? 'p' : 'a');
197 else if (stamp.tm_hour > 12) /* pm */
198 sprintf(tmp, "at %d:%02dpm",
202 sprintf(tmp, "at %d:%02dam",
203 stamp.tm_hour, stamp.tm_min);
204 strcat(dumpomatic, tmp);
206 size2 = strlen(dumpomatic);
207 snprintf(&dumpomatic[size2], size - size2,
208 " from %s:\n", ptr->sender);
209 if (ptr->text != NULL)
210 strcat(dumpomatic, ptr->text);
213 if (ptr->text != NULL) free(ptr->text);
217 for (i=0; i<strlen(dumpomatic); ++i) {
218 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
219 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
220 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
223 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
231 * imap_do_append_flags() is called by imap_append() to set any flags that
232 * the client specified at append time.
234 * FIXME find a way to do these in bulk so we don't max out our db journal
236 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
238 char this_flag[sizeof flags];
241 if (new_message_flags == NULL) return;
242 if (strlen(new_message_flags) == 0) return;
244 safestrncpy(flags, new_message_flags, sizeof flags);
246 for (i=0; i<num_tokens(flags, ' '); ++i) {
247 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
248 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
249 if (!strcasecmp(this_flag, "Seen")) {
250 CtdlSetSeen(new_msgnum, 1, ctdlsetseen_seen,
253 if (!strcasecmp(this_flag, "Answered")) {
254 CtdlSetSeen(new_msgnum, 1, ctdlsetseen_answered,
262 * This function is called by the main command loop.
264 void imap_append(int num_parms, char *parms[]) {
266 long bytes_transferred;
267 long stripped_length = 0;
268 struct CtdlMessage *msg;
269 long new_msgnum = (-1L);
271 char roomname[ROOMNAMELEN];
273 char savedroom[ROOMNAMELEN];
276 char new_message_flags[SIZ];
279 cprintf("%s BAD usage error\r\n", parms[0]);
283 if ( (parms[num_parms-1][0] != '{')
284 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
285 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
289 strcpy(new_message_flags, "");
290 if (num_parms >= 5) {
291 for (i=3; i<num_parms; ++i) {
292 strcat(new_message_flags, parms[i]);
293 strcat(new_message_flags, " ");
295 stripallbut(new_message_flags, '(', ')');
298 /* This is how we'd do this if it were relevant in our data store.
299 * if (num_parms >= 6) {
300 * new_message_internaldate = parms[4];
304 literal_length = atol(&parms[num_parms-1][1]);
305 if (literal_length < 1) {
306 cprintf("%s BAD Message length must be at least 1.\r\n",
311 imap_free_transmitted_message(); /* just in case. */
312 IMAP->transmitted_message = malloc(literal_length + 1);
313 if (IMAP->transmitted_message == NULL) {
314 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
317 IMAP->transmitted_length = literal_length;
319 cprintf("+ Transmit message now.\r\n");
321 bytes_transferred = 0;
323 ret = client_read(IMAP->transmitted_message, literal_length);
324 IMAP->transmitted_message[literal_length] = 0;
327 cprintf("%s NO Read failed.\r\n", parms[0]);
331 /* Client will transmit a trailing CRLF after the literal (the message
332 * text) is received. This call to client_getln() absorbs it.
335 client_getln(buf, sizeof buf);
337 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
338 lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
340 for (i=0; i<literal_length; ++i) {
341 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
342 IMAP->transmitted_message[stripped_length++] =
343 IMAP->transmitted_message[i];
346 literal_length = stripped_length;
347 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
349 lprintf(CTDL_DEBUG, "Converting message format\n");
350 msg = convert_internet_message(IMAP->transmitted_message);
351 IMAP->transmitted_message = NULL;
352 IMAP->transmitted_length = 0;
354 ret = imap_grabroom(roomname, parms[2], 0);
356 cprintf("%s NO Invalid mailbox name or access denied\r\n",
362 * usergoto() formally takes us to the desired room. (If another
363 * folder is selected, save its name so we can return there!!!!!)
365 if (IMAP->selected) {
366 strcpy(savedroom, CC->room.QRname);
368 usergoto(roomname, 0, 0, &msgs, &new);
370 /* If the user is locally authenticated, FORCE the From: header to
371 * show up as the real sender. FIXME do we really want to do this?
372 * Probably should make it site-definable or even room-definable.
374 * For now, we allow "forgeries" if the room is one of the user's
378 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
379 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
380 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
381 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
382 msg->cm_fields['A'] = strdup(CC->user.fullname);
383 msg->cm_fields['N'] = strdup(config.c_nodename);
384 msg->cm_fields['H'] = strdup(config.c_humannode);
391 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
394 /* Nope ... print an error message */
395 cprintf("%s NO %s\r\n", parms[0], buf);
399 /* Yes ... go ahead and post! */
401 new_msgnum = CtdlSubmitMsg(msg, NULL, "");
403 if (new_msgnum >= 0L) {
404 cprintf("%s OK APPEND completed\r\n", parms[0]);
407 cprintf("%s BAD Error %ld saving message to disk.\r\n",
408 parms[0], new_msgnum);
413 * IMAP protocol response to client has already been sent by now.
415 * If another folder is selected, go back to that room so we can resume
416 * our happy day without violent explosions.
418 if (IMAP->selected) {
419 usergoto(savedroom, 0, 0, &msgs, &new);
422 /* We don't need this buffer anymore */
423 CtdlFreeMessage(msg);
425 if (new_message_flags != NULL) {
426 imap_do_append_flags(new_msgnum, new_message_flags);