]> code.citadel.org Git - citadel.git/blob - citadel/serv_calendar.c
* More conflict checking stuff
[citadel.git] / citadel / serv_calendar.c
1 /* 
2  * $Id$ 
3  *
4  * This module implements iCalendar object processing and the Calendar>
5  * room on a Citadel/UX server.  It handles iCalendar objects using the
6  * iTIP protocol.  See RFCs 2445 and 2446.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <string.h>
16 #ifdef HAVE_STRINGS_H
17 #include <strings.h>
18 #endif
19 #include "serv_calendar.h"
20 #include "citadel.h"
21 #include "server.h"
22 #include "citserver.h"
23 #include "sysdep_decls.h"
24 #include "support.h"
25 #include "config.h"
26 #include "dynloader.h"
27 #include "user_ops.h"
28 #include "room_ops.h"
29 #include "tools.h"
30 #include "msgbase.h"
31 #include "mime_parser.h"
32 #ifdef HAVE_ICAL_H
33 #include <ical.h>
34 #endif
35
36
37 #ifdef HAVE_ICAL_H
38
39 struct ical_respond_data {
40         char desired_partnum[SIZ];
41         icalcomponent *cal;
42 };
43
44
45
46
47
48 /*
49  * Write our config to disk
50  */
51 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
52         char temp[PATH_MAX];
53         FILE *fp;
54         char *ser;
55
56         strcpy(temp, tmpnam(NULL));
57         ser = icalcomponent_as_ical_string(cal);
58         if (ser == NULL) return;
59
60         /* Make a temp file out of it */
61         fp = fopen(temp, "w");
62         if (fp == NULL) return;
63         fwrite(ser, strlen(ser), 1, fp);
64         fclose(fp);
65
66         /* This handy API function does all the work for us.
67          * NOTE: normally we would want to set that last argument to 1, to
68          * force the system to delete the user's old vCard.  But it doesn't
69          * have to, because the vcard_upload_beforesave() hook above
70          * is going to notice what we're trying to do, and delete the old vCard.
71          */
72         CtdlWriteObject(USERCALENDARROOM,       /* which room */
73                         "text/calendar",        /* MIME type */
74                         temp,                   /* temp file */
75                         u,                      /* which user */
76                         0,                      /* not binary */
77                         0,              /* don't delete others of this type */
78                         0);                     /* no flags */
79
80         unlink(temp);
81 }
82
83
84 /*
85  * Add a calendar object to the user's calendar
86  */
87 void ical_add(icalcomponent *cal, int recursion_level) {
88         icalcomponent *c;
89
90         /*
91          * The VEVENT subcomponent is the one we're interested in saving.
92          */
93         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
94         
95                 ical_write_to_cal(&CC->usersupp, cal);
96
97         }
98
99         /* If the component has subcomponents, recurse through them. */
100         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
101             (c != 0);
102             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
103                 /* Recursively process subcomponent */
104                 ical_add(c, recursion_level+1);
105         }
106
107 }
108
109
110
111 /*
112  * Callback function for mime parser that hunts for calendar content types
113  * and turns them into calendar objects
114  */
115 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
116                 void *content, char *cbtype, size_t length, char *encoding,
117                 void *cbuserdata) {
118
119         struct ical_respond_data *ird = NULL;
120
121         ird = (struct ical_respond_data *) cbuserdata;
122         if (ird->cal != NULL) {
123                 icalcomponent_free(ird->cal);
124                 ird->cal = NULL;
125         }
126         if (strcasecmp(partnum, ird->desired_partnum)) return;
127         ird->cal = icalcomponent_new_from_string(content);
128 }
129
130
131 /*
132  * Respond to a meeting request.
133  */
134 void ical_respond(long msgnum, char *partnum, char *action) {
135         struct CtdlMessage *msg;
136         struct ical_respond_data ird;
137
138         if (
139            (strcasecmp(action, "accept"))
140            && (strcasecmp(action, "decline"))
141         ) {
142                 cprintf("%d Action must be 'accept' or 'decline'\n",
143                         ERROR + ILLEGAL_VALUE
144                 );
145                 return;
146         }
147
148         msg = CtdlFetchMessage(msgnum);
149         if (msg == NULL) {
150                 cprintf("%d Message %ld not found.\n",
151                         ERROR+ILLEGAL_VALUE,
152                         (long)msgnum
153                 );
154                 return;
155         }
156
157         memset(&ird, 0, sizeof ird);
158         strcpy(ird.desired_partnum, partnum);
159         mime_parser(msg->cm_fields['M'],
160                 NULL,
161                 *ical_locate_part,              /* callback function */
162                 NULL, NULL,
163                 (void *) &ird,                  /* user data */
164                 0
165         );
166
167         CtdlFreeMessage(msg);
168
169         if (ird.cal != NULL) {
170                 /* Save this in the user's calendar if necessary */
171                 if (!strcasecmp(action, "accept")) {
172                         ical_add(ird.cal, 0);
173                 }
174
175                 /* Send a reply if necessary */
176                 /* FIXME ... do this */
177
178                 /* Delete the message from the inbox */
179                 /* FIXME ... do this */
180
181                 icalcomponent_free(ird.cal);
182                 ird.cal = NULL;
183                 cprintf("%d ok\n", CIT_OK);
184                 return;
185         }
186         else {
187                 cprintf("%d No calendar object found\n", ERROR);
188                 return;
189         }
190
191         /* should never get here */
192 }
193
194
195
196 /*
197  * Backend for ical_hunt_for_conflicts()
198  */
199 void vcard_hunt_for_conflicts_backend(long msgnum, void *data) {
200         icalcomponent *cal;
201         struct CtdlMessage *msg;
202         struct ical_respond_data ird;
203
204         cal = (icalcomponent *)data;
205
206         msg = CtdlFetchMessage(msgnum);
207         if (msg == NULL) return;
208         memset(&ird, 0, sizeof ird);
209         strcpy(ird.desired_partnum, "1");       /* hopefully it's always 1 */
210         mime_parser(msg->cm_fields['M'],
211                 NULL,
212                 *ical_locate_part,              /* callback function */
213                 NULL, NULL,
214                 (void *) &ird,                  /* user data */
215                 0
216         );
217         CtdlFreeMessage(msg);
218
219         if (ird.cal == NULL) return;
220
221         /* Now compare cal to ird.cal */
222         cprintf("hunted msg %ld\n", msgnum);
223
224         icalcomponent_free(ird.cal);
225 }
226
227
228
229 /* 
230  * Phase 2 of "hunt for conflicts" operation.
231  * At this point we have a calendar object which represents the VEVENT that
232  * we're considering adding to the calendar.  Now hunt through the user's
233  * calendar room, and output zero or more existing VEVENTs which conflict
234  * with this one.
235  */
236 void ical_hunt_for_conflicts(icalcomponent *cal) {
237         char hold_rm[ROOMNAMELEN];
238
239         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
240
241         if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
242                 getroom(&CC->quickroom, hold_rm);
243                 cprintf("%d You do not have a calendar.\n", ERROR);
244                 return;
245         }
246
247         cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
248
249         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
250                 NULL,
251                 vcard_hunt_for_conflicts_backend,
252                 (void *) cal
253         );
254
255         cprintf("000\n");
256         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
257
258 }
259
260
261
262 /*
263  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
264  */
265 void ical_conflicts(long msgnum, char *partnum) {
266         struct CtdlMessage *msg;
267         struct ical_respond_data ird;
268
269         msg = CtdlFetchMessage(msgnum);
270         if (msg == NULL) {
271                 cprintf("%d Message %ld not found.\n",
272                         ERROR+ILLEGAL_VALUE,
273                         (long)msgnum
274                 );
275                 return;
276         }
277
278         memset(&ird, 0, sizeof ird);
279         strcpy(ird.desired_partnum, partnum);
280         mime_parser(msg->cm_fields['M'],
281                 NULL,
282                 *ical_locate_part,              /* callback function */
283                 NULL, NULL,
284                 (void *) &ird,                  /* user data */
285                 0
286         );
287
288         CtdlFreeMessage(msg);
289
290         if (ird.cal != NULL) {
291                 ical_hunt_for_conflicts(ird.cal);
292                 icalcomponent_free(ird.cal);
293                 return;
294         }
295         else {
296                 cprintf("%d No calendar object found\n", ERROR);
297                 return;
298         }
299
300         /* should never get here */
301 }
302
303
304
305
306 /*
307  * All Citadel calendar commands from the client come through here.
308  */
309 void cmd_ical(char *argbuf)
310 {
311         char subcmd[SIZ];
312         long msgnum;
313         char partnum[SIZ];
314         char action[SIZ];
315
316         if (CtdlAccessCheck(ac_logged_in)) return;
317
318         extract(subcmd, argbuf, 0);
319
320         if (!strcmp(subcmd, "test")) {
321                 cprintf("%d This server supports calendaring\n", CIT_OK);
322                 return;
323         }
324
325         else if (!strcmp(subcmd, "respond")) {
326                 msgnum = extract_long(argbuf, 1);
327                 extract(partnum, argbuf, 2);
328                 extract(action, argbuf, 3);
329                 ical_respond(msgnum, partnum, action);
330         }
331
332         else if (!strcmp(subcmd, "conflicts")) {
333                 msgnum = extract_long(argbuf, 1);
334                 extract(partnum, argbuf, 2);
335                 ical_conflicts(msgnum, partnum);
336         }
337
338         else {
339                 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
340                 return;
341         }
342
343         /* should never get here */
344 }
345
346 #endif /* HAVE_ICAL_H */
347
348
349 /*
350  * We don't know if the calendar room exists so we just create it at login
351  */
352 void ical_create_room(void)
353 {
354         struct quickroom qr;
355         struct visit vbuf;
356
357         /* Create the calendar room if it doesn't already exist */
358         create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
359
360         /* Set expiration policy to manual; otherwise objects will be lost! */
361         if (lgetroom(&qr, USERCALENDARROOM)) {
362                 lprintf(3, "Couldn't get the user calendar room!\n");
363                 return;
364         }
365         qr.QRep.expire_mode = EXPIRE_MANUAL;
366         lputroom(&qr);
367
368         /* Set the view to a calendar view */
369         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
370         vbuf.v_view = 3;        /* 3 = calendar */
371         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
372
373         /* Create the tasks list room if it doesn't already exist */
374         create_room(USERTASKSROOM, 4, "", 0, 1, 0);
375
376         /* Set expiration policy to manual; otherwise objects will be lost! */
377         if (lgetroom(&qr, USERTASKSROOM)) {
378                 lprintf(3, "Couldn't get the user calendar room!\n");
379                 return;
380         }
381         qr.QRep.expire_mode = EXPIRE_MANUAL;
382         lputroom(&qr);
383
384         /* Set the view to a task list view */
385         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
386         vbuf.v_view = 4;        /* 4 = tasks */
387         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
388
389         return;
390 }
391
392
393 /* See if we need to prevent the object from being saved */
394 int ical_obj_beforesave(struct CtdlMessage *msg)
395 {
396         char roomname[ROOMNAMELEN];
397         char *p;
398         int a;
399         
400         /*
401          * Only messages with content-type text/calendar or text/x-calendar
402          * may be saved to Calendar>.  If the message is bound for
403          * Calendar> but doesn't have this content-type, throw an error
404          * so that the message may not be posted.
405          */
406
407         /* First determine if this is our room */
408         MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
409         if (strncmp(roomname, msg->cm_fields['O'], ROOMNAMELEN))
410                 return 0;       /* It's not us... */
411
412         /* Then determine content-type of the message */
413         
414         /* It must be an RFC822 message! */
415         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
416         if (msg->cm_format_type != 4)
417                 return 1;       /* You tried to save a non-RFC822 message! */
418         
419         /* Find the Content-Type: header */
420         p = msg->cm_fields['M'];
421         a = strlen(p);
422         while (--a > 0) {
423                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
424                         if (!strncasecmp(p + 14, "text/x-calendar", 15) ||
425                             !strncasecmp(p + 14, "text/calendar", 13))
426                                 return 0;
427                         else
428                                 return 1;
429                 }
430                 p++;
431         }
432         
433         /* Oops!  No Content-Type in this message!  How'd that happen? */
434         lprintf(7, "RFC822 message with no Content-Type header!\n");
435         return 1;
436 }
437
438
439
440 /* Register this module with the Citadel server. */
441 char *Dynamic_Module_Init(void)
442 {
443         CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
444 #ifdef HAVE_ICAL_H
445         CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
446         CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
447 #endif
448         return "$Id$";
449 }