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