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