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