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