]> code.citadel.org Git - citadel.git/blob - citadel/imap_misc.c
* The 'set the flags' portion of IMAP COPY now sets the flags of all messages
[citadel.git] / citadel / imap_misc.c
1 /*
2  * $Id$
3  *
4  *
5  */
6
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "serv_extensions.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "policy.h"
43 #include "database.h"
44 #include "msgbase.h"
45 #include "tools.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"
51 #include "genstamp.h"
52
53
54
55
56
57
58 /*
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)
61  */
62 int imap_do_copy(char *destination_folder) {
63         int i;
64         char roomname[ROOMNAMELEN];
65         struct ctdlroom qrbuf;
66         struct timeval tv1, tv2, tv3;
67         int num_selected = 0;
68
69         if (IMAP->num_msgs < 1) {
70                 return(0);
71         }
72
73         i = imap_grabroom(roomname, destination_folder, 0);
74         if (i != 0) return(i);
75
76         /*
77          * Optimization note ... ajc 2005oct09
78          * 
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.
82          *
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
87          * one shot.
88          * 
89          */
90         gettimeofday(&tv1, NULL);
91         for (i = 0; i < IMAP->num_msgs; ++i) {
92                 if (IMAP->flags[i] & IMAP_SELECTED) {
93                         ++num_selected;
94                         CtdlCopyMsgToRoom(IMAP->msgids[i], roomname);
95                 }
96         }
97
98         /* Don't bother wasting any more time if there were no messages. */
99         if (num_selected == 0) {
100                 return(0);
101         }
102
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;
107         int num_seen_no = 0;
108         long *answ_yes = NULL;
109         int num_answ_yes = 0;
110         long *answ_no = NULL;
111         int num_answ_no = 0;
112
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));
117
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];
122                         }
123                         if ((IMAP->flags[i] & IMAP_SEEN) == 0) {
124                                 seen_no[num_seen_no++] = IMAP->msgids[i];
125                         }
126                         if (IMAP->flags[i] & IMAP_ANSWERED) {
127                                 answ_yes[num_answ_yes++] = IMAP->msgids[i];
128                         }
129                         if ((IMAP->flags[i] & IMAP_ANSWERED) == 0) {
130                                 answ_no[num_answ_no++] = IMAP->msgids[i];
131                         }
132                 }
133         }
134
135         /* Set the flags... */
136         gettimeofday(&tv2, NULL);
137         i = getroom(&qrbuf, roomname);
138         if (i == 0) {
139                 CtdlSetSeen(seen_yes, num_seen_yes, 1,
140                         ctdlsetseen_seen, NULL, &qrbuf
141                 );
142                 CtdlSetSeen(seen_no, num_seen_no, 0,
143                         ctdlsetseen_seen, NULL, &qrbuf
144                 );
145                 CtdlSetSeen(answ_yes, num_answ_yes, 1,
146                         ctdlsetseen_answered, NULL, &qrbuf
147                 );
148                 CtdlSetSeen(answ_no, num_answ_no, 0,
149                         ctdlsetseen_answered, NULL, &qrbuf
150                 );
151         }
152
153         free(seen_yes);
154         free(seen_no);
155         free(answ_yes);
156         free(answ_no);
157
158         gettimeofday(&tv3, NULL);
159         lprintf(CTDL_DEBUG, "Copying the pointers took %ld microseconds\n",
160                 (tv2.tv_usec + (tv2.tv_sec * 1000000))
161                 - (tv1.tv_usec + (tv1.tv_sec * 1000000))
162         );
163         lprintf(CTDL_DEBUG, "Setting the flags took %ld microseconds\n",
164                 (tv3.tv_usec + (tv3.tv_sec * 1000000))
165                 - (tv2.tv_usec + (tv2.tv_sec * 1000000))
166         );
167         return(0);
168 }
169
170
171
172 /*
173  * This function is called by the main command loop.
174  */
175 void imap_copy(int num_parms, char *parms[]) {
176         int ret;
177
178         if (num_parms != 4) {
179                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
180                 return;
181         }
182
183         if (imap_is_message_set(parms[2])) {
184                 imap_pick_range(parms[2], 0);
185         }
186         else {
187                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
188                 return;
189         }
190
191         ret = imap_do_copy(parms[3]);
192         if (!ret) {
193                 cprintf("%s OK COPY completed\r\n", parms[0]);
194         }
195         else {
196                 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
197         }
198 }
199
200 /*
201  * This function is called by the main command loop.
202  */
203 void imap_uidcopy(int num_parms, char *parms[]) {
204
205         if (num_parms != 5) {
206                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
207                 return;
208         }
209
210         if (imap_is_message_set(parms[3])) {
211                 imap_pick_range(parms[3], 1);
212         }
213         else {
214                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
215                 return;
216         }
217
218         if (imap_do_copy(parms[4]) == 0) {
219                 cprintf("%s OK UID COPY completed\r\n", parms[0]);
220         }
221         else {
222                 cprintf("%s NO UID COPY failed\r\n", parms[0]);
223         }
224 }
225
226
227 /*
228  * Poll for instant messages (yeah, we can do this in IMAP ... I think)
229  */
230 void imap_print_instant_messages(void) {
231         struct ExpressMessage *ptr, *holdptr;
232         char *dumpomatic = NULL;
233         char tmp[SIZ];
234         int i;
235         size_t size, size2;
236         struct tm stamp;
237
238         if (CC->FirstExpressMessage == NULL) {
239                 return;
240         }
241         begin_critical_section(S_SESSION_TABLE);
242         ptr = CC->FirstExpressMessage;
243         CC->FirstExpressMessage = NULL;
244         end_critical_section(S_SESSION_TABLE);
245
246         while (ptr != NULL) {
247                 localtime_r(&(ptr->timestamp), &stamp);
248                 size = strlen(ptr->text) + SIZ;
249                 dumpomatic = malloc(size);
250                 strcpy(dumpomatic, "");
251                 if (ptr->flags && EM_BROADCAST)
252                         strcat(dumpomatic, "Broadcast message ");
253                 else if (ptr->flags && EM_CHAT)
254                         strcat(dumpomatic, "Chat request ");
255                 else if (ptr->flags && EM_GO_AWAY)
256                         strcat(dumpomatic, "Please logoff now, as requested ");
257                 else
258                         strcat(dumpomatic, "Message ");
259
260                 /* Timestamp.  Can this be improved? */
261                 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
262                         sprintf(tmp, "at 12:%02d%cm",
263                                 stamp.tm_min, 
264                                 stamp.tm_hour ? 'p' : 'a');
265                 else if (stamp.tm_hour > 12)            /* pm */
266                         sprintf(tmp, "at %d:%02dpm",
267                                 stamp.tm_hour - 12,
268                                 stamp.tm_min);
269                 else                                    /* am */
270                         sprintf(tmp, "at %d:%02dam",
271                                 stamp.tm_hour, stamp.tm_min);
272                 strcat(dumpomatic, tmp);
273
274                 size2 = strlen(dumpomatic);
275                 snprintf(&dumpomatic[size2], size - size2,
276                         " from %s:\n", ptr->sender);
277                 if (ptr->text != NULL)
278                         strcat(dumpomatic, ptr->text);
279
280                 holdptr = ptr->next;
281                 if (ptr->text != NULL) free(ptr->text);
282                 free(ptr);
283                 ptr = holdptr;
284
285                 for (i=0; i<strlen(dumpomatic); ++i) {
286                         if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
287                         if (dumpomatic[i]=='\\') dumpomatic[i]='/';
288                         if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
289                 }
290
291                 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
292                 free(dumpomatic);
293         }
294         cprintf("000\n");
295 }
296
297
298 /*
299  * imap_do_append_flags() is called by imap_append() to set any flags that
300  * the client specified at append time.
301  *
302  * FIXME find a way to do these in bulk so we don't max out our db journal
303  */
304 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
305         char flags[32];
306         char this_flag[sizeof flags];
307         int i;
308
309         if (new_message_flags == NULL) return;
310         if (strlen(new_message_flags) == 0) return;
311
312         safestrncpy(flags, new_message_flags, sizeof flags);
313
314         for (i=0; i<num_tokens(flags, ' '); ++i) {
315                 extract_token(this_flag, flags, i, ' ', sizeof this_flag);
316                 if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
317                 if (!strcasecmp(this_flag, "Seen")) {
318                         CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
319                                 NULL, NULL);
320                 }
321                 if (!strcasecmp(this_flag, "Answered")) {
322                         CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
323                                 NULL, NULL);
324                 }
325         }
326 }
327
328
329 /*
330  * This function is called by the main command loop.
331  */
332 void imap_append(int num_parms, char *parms[]) {
333         long literal_length;
334         long bytes_transferred;
335         long stripped_length = 0;
336         struct CtdlMessage *msg;
337         long new_msgnum = (-1L);
338         int ret = 0;
339         char roomname[ROOMNAMELEN];
340         char buf[SIZ];
341         char savedroom[ROOMNAMELEN];
342         int msgs, new;
343         int i;
344         char new_message_flags[SIZ];
345
346         if (num_parms < 4) {
347                 cprintf("%s BAD usage error\r\n", parms[0]);
348                 return;
349         }
350
351         if ( (parms[num_parms-1][0] != '{')
352            || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') )  {
353                 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
354                 return;
355         }
356
357         strcpy(new_message_flags, "");
358         if (num_parms >= 5) {
359                 for (i=3; i<num_parms; ++i) {
360                         strcat(new_message_flags, parms[i]);
361                         strcat(new_message_flags, " ");
362                 }
363                 stripallbut(new_message_flags, '(', ')');
364         }
365
366         /* This is how we'd do this if it were relevant in our data store.
367          * if (num_parms >= 6) {
368          *  new_message_internaldate = parms[4];
369          * }
370          */
371
372         literal_length = atol(&parms[num_parms-1][1]);
373         if (literal_length < 1) {
374                 cprintf("%s BAD Message length must be at least 1.\r\n",
375                         parms[0]);
376                 return;
377         }
378
379         imap_free_transmitted_message();        /* just in case. */
380         IMAP->transmitted_message = malloc(literal_length + 1);
381         if (IMAP->transmitted_message == NULL) {
382                 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
383                 return;
384         }
385         IMAP->transmitted_length = literal_length;
386
387         cprintf("+ Transmit message now.\r\n");
388
389         bytes_transferred = 0;
390
391         ret = client_read(IMAP->transmitted_message, literal_length);
392         IMAP->transmitted_message[literal_length] = 0;
393
394         if (ret != 1) {
395                 cprintf("%s NO Read failed.\r\n", parms[0]);
396                 return;
397         }
398
399         /* Client will transmit a trailing CRLF after the literal (the message
400          * text) is received.  This call to client_getln() absorbs it.
401          */
402         flush_output();
403         client_getln(buf, sizeof buf);
404
405         /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
406         lprintf(CTDL_DEBUG, "Converting CRLF to LF\n");
407         stripped_length = 0;
408         for (i=0; i<literal_length; ++i) {
409                 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
410                         IMAP->transmitted_message[stripped_length++] =
411                                 IMAP->transmitted_message[i];
412                 }
413         }
414         literal_length = stripped_length;
415         IMAP->transmitted_message[literal_length] = 0;  /* reterminate it */
416
417         lprintf(CTDL_DEBUG, "Converting message format\n");
418         msg = convert_internet_message(IMAP->transmitted_message);
419         IMAP->transmitted_message = NULL;
420         IMAP->transmitted_length = 0;
421
422         ret = imap_grabroom(roomname, parms[2], 0);
423         if (ret != 0) {
424                 cprintf("%s NO Invalid mailbox name or access denied\r\n",
425                         parms[0]);
426                 return;
427         }
428
429         /*
430          * usergoto() formally takes us to the desired room.  (If another
431          * folder is selected, save its name so we can return there!!!!!)
432          */
433         if (IMAP->selected) {
434                 strcpy(savedroom, CC->room.QRname);
435         }
436         usergoto(roomname, 0, 0, &msgs, &new);
437
438         /* If the user is locally authenticated, FORCE the From: header to
439          * show up as the real sender.  FIXME do we really want to do this?
440          * Probably should make it site-definable or even room-definable.
441          *
442          * For now, we allow "forgeries" if the room is one of the user's
443          * private mailboxes.
444          */
445         if (CC->logged_in) {
446            if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
447                 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
448                 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
449                 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
450                 msg->cm_fields['A'] = strdup(CC->user.fullname);
451                 msg->cm_fields['N'] = strdup(config.c_nodename);
452                 msg->cm_fields['H'] = strdup(config.c_humannode);
453             }
454         }
455
456         /* 
457          * Can we post here?
458          */
459         ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
460
461         if (ret) {
462                 /* Nope ... print an error message */
463                 cprintf("%s NO %s\r\n", parms[0], buf);
464         }
465
466         else {
467                 /* Yes ... go ahead and post! */
468                 if (msg != NULL) {
469                         new_msgnum = CtdlSubmitMsg(msg, NULL, "");
470                 }
471                 if (new_msgnum >= 0L) {
472                         cprintf("%s OK APPEND completed\r\n", parms[0]);
473                 }
474                 else {
475                         cprintf("%s BAD Error %ld saving message to disk.\r\n",
476                                 parms[0], new_msgnum);
477                 }
478         }
479
480         /*
481          * IMAP protocol response to client has already been sent by now.
482          *
483          * If another folder is selected, go back to that room so we can resume
484          * our happy day without violent explosions.
485          */
486         if (IMAP->selected) {
487                 usergoto(savedroom, 0, 0, &msgs, &new);
488         }
489
490         /* We don't need this buffer anymore */
491         CtdlFreeMessage(msg);
492
493         if (new_message_flags != NULL) {
494                 imap_do_append_flags(new_msgnum, new_message_flags);
495         }
496 }