]> code.citadel.org Git - citadel.git/blob - citadel/serv_calendar.c
* Put in a skeleton "hunt for conflicts" code
[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;
120
121         ird = (struct ical_respond_data *) cbuserdata;
122         if (strcasecmp(partnum, ird->desired_partnum)) return;
123         ird->cal = icalcomponent_new_from_string(content);
124 }
125
126
127 /*
128  * Respond to a meeting request.
129  */
130 void ical_respond(long msgnum, char *partnum, char *action) {
131         struct CtdlMessage *msg;
132         struct ical_respond_data ird;
133
134         if (
135            (strcasecmp(action, "accept"))
136            && (strcasecmp(action, "decline"))
137         ) {
138                 cprintf("%d Action must be 'accept' or 'decline'\n",
139                         ERROR + ILLEGAL_VALUE
140                 );
141                 return;
142         }
143
144         msg = CtdlFetchMessage(msgnum);
145         if (msg == NULL) {
146                 cprintf("%d Message %ld not found.\n",
147                         ERROR+ILLEGAL_VALUE,
148                         (long)msgnum
149                 );
150                 return;
151         }
152
153         memset(&ird, 0, sizeof ird);
154         strcpy(ird.desired_partnum, partnum);
155         mime_parser(msg->cm_fields['M'],
156                 NULL,
157                 *ical_locate_part,              /* callback function */
158                 NULL, NULL,
159                 (void *) &ird,                  /* user data */
160                 0
161         );
162
163         CtdlFreeMessage(msg);
164
165         if (ird.cal != NULL) {
166                 /* Save this in the user's calendar if necessary */
167                 if (!strcasecmp(action, "accept")) {
168                         ical_add(ird.cal, 0);
169                 }
170
171                 /* Send a reply if necessary */
172                 /* FIXME ... do this */
173
174                 /* Delete the message from the inbox */
175                 /* FIXME ... do this */
176
177                 icalcomponent_free(ird.cal);
178                 cprintf("%d ok\n", CIT_OK);
179                 return;
180         }
181         else {
182                 cprintf("%d No calendar object found\n", ERROR);
183                 return;
184         }
185
186         /* should never get here */
187 }
188
189
190
191 /* 
192  * Phase 2 of "hunt for conflicts" operation.
193  * At this point we have a calendar object which represents the VEVENT that
194  * we're considering adding to the calendar.  Now hunt through the user's
195  * calendar room, and output zero or more existing VEVENTs which conflict
196  * with this one.
197  */
198 void ical_hunt_for_conflicts(icalcomponent *cal) {
199
200         cprintf("%d FIXME not implemented\n", ERROR);
201
202 }
203
204
205
206 /*
207  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
208  */
209 void ical_conflicts(long msgnum, char *partnum) {
210         struct CtdlMessage *msg;
211         struct ical_respond_data ird;
212
213         msg = CtdlFetchMessage(msgnum);
214         if (msg == NULL) {
215                 cprintf("%d Message %ld not found.\n",
216                         ERROR+ILLEGAL_VALUE,
217                         (long)msgnum
218                 );
219                 return;
220         }
221
222         memset(&ird, 0, sizeof ird);
223         strcpy(ird.desired_partnum, partnum);
224         mime_parser(msg->cm_fields['M'],
225                 NULL,
226                 *ical_locate_part,              /* callback function */
227                 NULL, NULL,
228                 (void *) &ird,                  /* user data */
229                 0
230         );
231
232         CtdlFreeMessage(msg);
233
234         if (ird.cal != NULL) {
235                 ical_hunt_for_conflicts(ird.cal);
236                 icalcomponent_free(ird.cal);
237                 return;
238         }
239         else {
240                 cprintf("%d No calendar object found\n", ERROR);
241                 return;
242         }
243
244         /* should never get here */
245 }
246
247
248
249
250 /*
251  * All Citadel calendar commands from the client come through here.
252  */
253 void cmd_ical(char *argbuf)
254 {
255         char subcmd[SIZ];
256         long msgnum;
257         char partnum[SIZ];
258         char action[SIZ];
259
260         if (CtdlAccessCheck(ac_logged_in)) return;
261
262         extract(subcmd, argbuf, 0);
263
264         if (!strcmp(subcmd, "test")) {
265                 cprintf("%d This server supports calendaring\n", CIT_OK);
266                 return;
267         }
268
269         else if (!strcmp(subcmd, "respond")) {
270                 msgnum = extract_long(argbuf, 1);
271                 extract(partnum, argbuf, 2);
272                 extract(action, argbuf, 3);
273                 ical_respond(msgnum, partnum, action);
274         }
275
276         else if (!strcmp(subcmd, "conflicts")) {
277                 msgnum = extract_long(argbuf, 1);
278                 extract(partnum, argbuf, 2);
279                 ical_conflicts(msgnum, partnum);
280         }
281
282         else {
283                 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
284                 return;
285         }
286
287         /* should never get here */
288 }
289
290 #endif /* HAVE_ICAL_H */
291
292
293 /*
294  * We don't know if the calendar room exists so we just create it at login
295  */
296 void ical_create_room(void)
297 {
298         struct quickroom qr;
299         struct visit vbuf;
300
301         /* Create the calendar room if it doesn't already exist */
302         create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
303
304         /* Set expiration policy to manual; otherwise objects will be lost! */
305         if (lgetroom(&qr, USERCALENDARROOM)) {
306                 lprintf(3, "Couldn't get the user calendar room!\n");
307                 return;
308         }
309         qr.QRep.expire_mode = EXPIRE_MANUAL;
310         lputroom(&qr);
311
312         /* Set the view to a calendar view */
313         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
314         vbuf.v_view = 3;        /* 3 = calendar */
315         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
316
317         /* Create the tasks list room if it doesn't already exist */
318         create_room(USERTASKSROOM, 4, "", 0, 1, 0);
319
320         /* Set expiration policy to manual; otherwise objects will be lost! */
321         if (lgetroom(&qr, USERTASKSROOM)) {
322                 lprintf(3, "Couldn't get the user calendar room!\n");
323                 return;
324         }
325         qr.QRep.expire_mode = EXPIRE_MANUAL;
326         lputroom(&qr);
327
328         /* Set the view to a task list view */
329         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
330         vbuf.v_view = 4;        /* 4 = tasks */
331         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
332
333         return;
334 }
335
336
337 /* See if we need to prevent the object from being saved */
338 int ical_obj_beforesave(struct CtdlMessage *msg)
339 {
340         char roomname[ROOMNAMELEN];
341         char *p;
342         int a;
343         
344         /*
345          * Only messages with content-type text/calendar or text/x-calendar
346          * may be saved to Calendar>.  If the message is bound for
347          * Calendar> but doesn't have this content-type, throw an error
348          * so that the message may not be posted.
349          */
350
351         /* First determine if this is our room */
352         MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
353         if (strncmp(roomname, msg->cm_fields['O'], ROOMNAMELEN))
354                 return 0;       /* It's not us... */
355
356         /* Then determine content-type of the message */
357         
358         /* It must be an RFC822 message! */
359         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
360         if (msg->cm_format_type != 4)
361                 return 1;       /* You tried to save a non-RFC822 message! */
362         
363         /* Find the Content-Type: header */
364         p = msg->cm_fields['M'];
365         a = strlen(p);
366         while (--a > 0) {
367                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
368                         if (!strncasecmp(p + 14, "text/x-calendar", 15) ||
369                             !strncasecmp(p + 14, "text/calendar", 13))
370                                 return 0;
371                         else
372                                 return 1;
373                 }
374                 p++;
375         }
376         
377         /* Oops!  No Content-Type in this message!  How'd that happen? */
378         lprintf(7, "RFC822 message with no Content-Type header!\n");
379         return 1;
380 }
381
382
383
384 /* Register this module with the Citadel server. */
385 char *Dynamic_Module_Init(void)
386 {
387         CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
388 #ifdef HAVE_ICAL_H
389         CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
390         CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
391 #endif
392         return "$Id$";
393 }