]> code.citadel.org Git - citadel.git/blob - citadel/serv_calendar.c
* When sending out invitations, encapsulate the VEVENT component inside a
[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 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
11
12 #include "sysdep.h"
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <limits.h>
16 #include <stdio.h>
17 #include <string.h>
18 #ifdef HAVE_STRINGS_H
19 #include <strings.h>
20 #endif
21 #include "serv_calendar.h"
22 #include "citadel.h"
23 #include "server.h"
24 #include "citserver.h"
25 #include "sysdep_decls.h"
26 #include "support.h"
27 #include "config.h"
28 #include "dynloader.h"
29 #include "user_ops.h"
30 #include "room_ops.h"
31 #include "tools.h"
32 #include "msgbase.h"
33 #include "mime_parser.h"
34
35 #ifdef HAVE_ICAL_H
36
37 #include <ical.h>
38
39 struct ical_respond_data {
40         char desired_partnum[SIZ];
41         icalcomponent *cal;
42 };
43
44
45 /*
46  * Write a calendar object into the specified user's calendar room.
47  */
48 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
49         char temp[PATH_MAX];
50         FILE *fp;
51         char *ser;
52
53         strcpy(temp, tmpnam(NULL));
54         ser = icalcomponent_as_ical_string(cal);
55         if (ser == NULL) return;
56
57         /* Make a temp file out of it */
58         fp = fopen(temp, "w");
59         if (fp == NULL) return;
60         fwrite(ser, strlen(ser), 1, fp);
61         fclose(fp);
62
63         /* This handy API function does all the work for us.
64          */
65         CtdlWriteObject(USERCALENDARROOM,       /* which room */
66                         "text/calendar",        /* MIME type */
67                         temp,                   /* temp file */
68                         u,                      /* which user */
69                         0,                      /* not binary */
70                         0,              /* don't delete others of this type */
71                         0);                     /* no flags */
72
73         unlink(temp);
74 }
75
76
77 /*
78  * Add a calendar object to the user's calendar
79  */
80 void ical_add(icalcomponent *cal, int recursion_level) {
81         icalcomponent *c;
82
83         /*
84          * The VEVENT subcomponent is the one we're interested in saving.
85          */
86         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
87         
88                 ical_write_to_cal(&CC->usersupp, cal);
89
90         }
91
92         /* If the component has subcomponents, recurse through them. */
93         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
94             (c != 0);
95             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
96                 /* Recursively process subcomponent */
97                 ical_add(c, recursion_level+1);
98         }
99
100 }
101
102
103
104 /*
105  * Send a reply to a meeting invitation.
106  *
107  * 'request' is the invitation to reply to.
108  * 'action' is the string "accept" or "decline".
109  *
110  * (Sorry about this being more than 80 columns ... there was just
111  * no easy way to break it down sensibly.)
112  */
113 void ical_send_a_reply(icalcomponent *request, char *action) {
114         icalcomponent *the_reply = NULL;
115         icalcomponent *vevent = NULL;
116         icalproperty *attendee = NULL;
117         char attendee_string[SIZ];
118         icalproperty *organizer = NULL;
119         char organizer_string[SIZ];
120         icalproperty *summary = NULL;
121         char summary_string[SIZ];
122         icalproperty *me_attend = NULL;
123         struct recptypes *recp = NULL;
124         icalparameter *partstat = NULL;
125         char *serialized_reply = NULL;
126         char *reply_message_text = NULL;
127         struct CtdlMessage *msg = NULL;
128         struct recptypes *valid = NULL;
129
130         strcpy(organizer_string, "");
131         strcpy(summary_string, "Calendar item");
132
133         if (request == NULL) {
134                 lprintf(3, "ERROR: trying to reply to NULL event?\n");
135                 return;
136         }
137
138         the_reply = icalcomponent_new_clone(request);
139         if (the_reply == NULL) {
140                 lprintf(3, "ERROR: cannot clone request\n");
141                 return;
142         }
143
144         /* Change the method from REQUEST to REPLY */
145         icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
146
147         vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
148         if (vevent != NULL) {
149                 /* Hunt for attendees, removing ones that aren't us.
150                  * (Actually, remove them all, cloning our own one so we can
151                  * re-insert it later)
152                  */
153                 while (attendee = icalcomponent_get_first_property(vevent,
154                     ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
155                 ) {
156                         if (icalproperty_get_attendee(attendee)) {
157                                 strcpy(attendee_string,
158                                         icalproperty_get_attendee(attendee) );
159                                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
160                                         strcpy(attendee_string, &attendee_string[7]);
161                                         striplt(attendee_string);
162                                         recp = validate_recipients(attendee_string);
163                                         if (recp != NULL) {
164                                                 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
165                                                         if (me_attend) icalproperty_free(me_attend);
166                                                         me_attend = icalproperty_new_clone(attendee);
167                                                 }
168                                                 phree(recp);
169                                         }
170                                 }
171                         }
172                         /* Remove it... */
173                         icalcomponent_remove_property(vevent, attendee);
174                         icalproperty_free(attendee);
175                 }
176
177                 /* We found our own address in the attendee list. */
178                 if (me_attend) {
179                         /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
180                         icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
181
182                         if (!strcasecmp(action, "accept")) {
183                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
184                         }
185                         else if (!strcasecmp(action, "decline")) {
186                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
187                         }
188                         else if (!strcasecmp(action, "tentative")) {
189                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
190                         }
191
192                         if (partstat) icalproperty_add_parameter(me_attend, partstat);
193
194                         /* Now insert it back into the vevent. */
195                         icalcomponent_add_property(vevent, me_attend);
196                 }
197
198                 /* Figure out who to send this thing to */
199                 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
200                 if (organizer != NULL) {
201                         if (icalproperty_get_organizer(organizer)) {
202                                 strcpy(organizer_string,
203                                         icalproperty_get_organizer(organizer) );
204                         }
205                 }
206                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
207                         strcpy(organizer_string, &organizer_string[7]);
208                         striplt(organizer_string);
209                 } else {
210                         strcpy(organizer_string, "");
211                 }
212
213                 /* Extract the summary string -- we'll use it as the
214                  * message subject for the reply
215                  */
216                 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
217                 if (summary != NULL) {
218                         if (icalproperty_get_summary(summary)) {
219                                 strcpy(summary_string,
220                                         icalproperty_get_summary(summary) );
221                         }
222                 }
223
224         }
225
226         /* Now generate the reply message and send it out. */
227         serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
228         icalcomponent_free(the_reply);  /* don't need this anymore */
229         if (serialized_reply == NULL) return;
230
231         reply_message_text = mallok(strlen(serialized_reply) + SIZ);
232         if (reply_message_text != NULL) {
233                 sprintf(reply_message_text,
234                         "Content-type: text/calendar\r\n\r\n%s\r\n",
235                         serialized_reply
236                 );
237
238                 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
239                         CC->quickroom.QRname, 0, FMT_RFC822,
240                         "",
241                         summary_string,         /* Use summary for subject */
242                         reply_message_text);
243         
244                 if (msg != NULL) {
245                         valid = validate_recipients(organizer_string);
246                         CtdlSubmitMsg(msg, valid, "");
247                         CtdlFreeMessage(msg);
248                 }
249         }
250         phree(serialized_reply);
251 }
252
253
254
255 /*
256  * Callback function for mime parser that hunts for calendar content types
257  * and turns them into calendar objects
258  */
259 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
260                 void *content, char *cbtype, size_t length, char *encoding,
261                 void *cbuserdata) {
262
263         struct ical_respond_data *ird = NULL;
264
265         ird = (struct ical_respond_data *) cbuserdata;
266         if (ird->cal != NULL) {
267                 icalcomponent_free(ird->cal);
268                 ird->cal = NULL;
269         }
270         if (strcasecmp(partnum, ird->desired_partnum)) return;
271         ird->cal = icalcomponent_new_from_string(content);
272 }
273
274
275 /*
276  * Respond to a meeting request.
277  */
278 void ical_respond(long msgnum, char *partnum, char *action) {
279         struct CtdlMessage *msg;
280         struct ical_respond_data ird;
281
282         if (
283            (strcasecmp(action, "accept"))
284            && (strcasecmp(action, "decline"))
285         ) {
286                 cprintf("%d Action must be 'accept' or 'decline'\n",
287                         ERROR + ILLEGAL_VALUE
288                 );
289                 return;
290         }
291
292         msg = CtdlFetchMessage(msgnum);
293         if (msg == NULL) {
294                 cprintf("%d Message %ld not found.\n",
295                         ERROR+ILLEGAL_VALUE,
296                         (long)msgnum
297                 );
298                 return;
299         }
300
301         memset(&ird, 0, sizeof ird);
302         strcpy(ird.desired_partnum, partnum);
303         mime_parser(msg->cm_fields['M'],
304                 NULL,
305                 *ical_locate_part,              /* callback function */
306                 NULL, NULL,
307                 (void *) &ird,                  /* user data */
308                 0
309         );
310
311         /* We're done with the incoming message, because we now have a
312          * calendar object in memory.
313          */
314         CtdlFreeMessage(msg);
315
316         /*
317          * Here is the real meat of this function.  Handle the event.
318          */
319         if (ird.cal != NULL) {
320                 /* Save this in the user's calendar if necessary */
321                 if (!strcasecmp(action, "accept")) {
322                         ical_add(ird.cal, 0);
323                 }
324
325                 /* Send a reply if necessary */
326                 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
327                         ical_send_a_reply(ird.cal, action);
328                 }
329
330                 /* Now that we've processed this message, we don't need it
331                  * anymore.  So delete it.
332                  */
333                 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
334
335                 /* Free the memory we allocated and return a response. */
336                 icalcomponent_free(ird.cal);
337                 ird.cal = NULL;
338                 cprintf("%d ok\n", CIT_OK);
339                 return;
340         }
341         else {
342                 cprintf("%d No calendar object found\n", ERROR);
343                 return;
344         }
345
346         /* should never get here */
347 }
348
349
350 /*
351  * Search for a property in both the top level and in a VEVENT subcomponent
352  */
353 icalproperty *ical_ctdl_get_subprop(
354                 icalcomponent *cal,
355                 icalproperty_kind which_prop
356 ) {
357         icalproperty *p;
358         icalcomponent *c;
359
360         p = icalcomponent_get_first_property(cal, which_prop);
361         if (p == NULL) {
362                 c = icalcomponent_get_first_component(cal,
363                                                         ICAL_VEVENT_COMPONENT);
364                 if (c != NULL) {
365                         p = icalcomponent_get_first_property(c, which_prop);
366                 }
367         }
368         return p;
369 }
370
371
372 /*
373  * Check to see if two events overlap.  Returns nonzero if they do.
374  */
375 int ical_ctdl_is_overlap(
376                         struct icaltimetype t1start,
377                         struct icaltimetype t1end,
378                         struct icaltimetype t2start,
379                         struct icaltimetype t2end
380 ) {
381
382         if (icaltime_is_null_time(t1start)) return(0);
383         if (icaltime_is_null_time(t2start)) return(0);
384
385         /* First, check for all-day events */
386         if (t1start.is_date) {
387                 if (!icaltime_compare_date_only(t1start, t2start)) {
388                         return(1);
389                 }
390                 if (!icaltime_is_null_time(t2end)) {
391                         if (!icaltime_compare_date_only(t1start, t2end)) {
392                                 return(1);
393                         }
394                 }
395         }
396
397         if (t2start.is_date) {
398                 if (!icaltime_compare_date_only(t2start, t1start)) {
399                         return(1);
400                 }
401                 if (!icaltime_is_null_time(t1end)) {
402                         if (!icaltime_compare_date_only(t2start, t1end)) {
403                                 return(1);
404                         }
405                 }
406         }
407
408         /* Now check for overlaps using date *and* time. */
409
410         /* First, bail out if either event 1 or event 2 is missing end time. */
411         if (icaltime_is_null_time(t1end)) return(0);
412         if (icaltime_is_null_time(t2end)) return(0);
413
414         /* If event 1 ends before event 2 starts, we're in the clear. */
415         if (icaltime_compare(t1end, t2start) <= 0) return(0);
416
417         /* If event 2 ends before event 1 starts, we're also ok. */
418         if (icaltime_compare(t2end, t1start) <= 0) return(0);
419
420         /* Otherwise, they overlap. */
421         return(1);
422 }
423
424
425
426 /*
427  * Backend for ical_hunt_for_conflicts()
428  */
429 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
430         icalcomponent *cal;
431         struct CtdlMessage *msg;
432         struct ical_respond_data ird;
433         struct icaltimetype t1start, t1end, t2start, t2end;
434         icalproperty *p;
435         char conflict_event_uid[SIZ];
436         char conflict_event_summary[SIZ];
437         char compare_uid[SIZ];
438
439         cal = (icalcomponent *)data;
440         strcpy(compare_uid, "");
441         strcpy(conflict_event_uid, "");
442         strcpy(conflict_event_summary, "");
443
444         msg = CtdlFetchMessage(msgnum);
445         if (msg == NULL) return;
446         memset(&ird, 0, sizeof ird);
447         strcpy(ird.desired_partnum, "1");       /* hopefully it's always 1 */
448         mime_parser(msg->cm_fields['M'],
449                 NULL,
450                 *ical_locate_part,              /* callback function */
451                 NULL, NULL,
452                 (void *) &ird,                  /* user data */
453                 0
454         );
455         CtdlFreeMessage(msg);
456
457         if (ird.cal == NULL) return;
458
459         t1start = icaltime_null_time();
460         t1end = icaltime_null_time();
461         t2start = icaltime_null_time();
462         t1end = icaltime_null_time();
463
464         /* Now compare cal to ird.cal */
465         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
466         if (p == NULL) return;
467         if (p != NULL) t2start = icalproperty_get_dtstart(p);
468         
469         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
470         if (p != NULL) t2end = icalproperty_get_dtend(p);
471
472         p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
473         if (p == NULL) return;
474         if (p != NULL) t1start = icalproperty_get_dtstart(p);
475         
476         p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
477         if (p != NULL) t1end = icalproperty_get_dtend(p);
478         
479         p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
480         if (p != NULL) {
481                 strcpy(compare_uid, icalproperty_get_comment(p));
482         }
483
484         p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
485         if (p != NULL) {
486                 strcpy(conflict_event_uid, icalproperty_get_comment(p));
487         }
488
489         p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
490         if (p != NULL) {
491                 strcpy(conflict_event_summary, icalproperty_get_comment(p));
492         }
493
494
495         icalcomponent_free(ird.cal);
496
497         if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
498                 cprintf("%ld||%s|%s|%d|\n",
499                         msgnum,
500                         conflict_event_uid,
501                         conflict_event_summary,
502                         (       ((strlen(compare_uid)>0)
503                                 &&(!strcasecmp(compare_uid,
504                                 conflict_event_uid))) ? 1 : 0
505                         )
506                 );
507         }
508 }
509
510
511
512 /* 
513  * Phase 2 of "hunt for conflicts" operation.
514  * At this point we have a calendar object which represents the VEVENT that
515  * we're considering adding to the calendar.  Now hunt through the user's
516  * calendar room, and output zero or more existing VEVENTs which conflict
517  * with this one.
518  */
519 void ical_hunt_for_conflicts(icalcomponent *cal) {
520         char hold_rm[ROOMNAMELEN];
521
522         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
523
524         if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
525                 getroom(&CC->quickroom, hold_rm);
526                 cprintf("%d You do not have a calendar.\n", ERROR);
527                 return;
528         }
529
530         cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
531
532         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
533                 NULL,
534                 ical_hunt_for_conflicts_backend,
535                 (void *) cal
536         );
537
538         cprintf("000\n");
539         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
540
541 }
542
543
544
545 /*
546  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
547  */
548 void ical_conflicts(long msgnum, char *partnum) {
549         struct CtdlMessage *msg;
550         struct ical_respond_data ird;
551
552         msg = CtdlFetchMessage(msgnum);
553         if (msg == NULL) {
554                 cprintf("%d Message %ld not found.\n",
555                         ERROR+ILLEGAL_VALUE,
556                         (long)msgnum
557                 );
558                 return;
559         }
560
561         memset(&ird, 0, sizeof ird);
562         strcpy(ird.desired_partnum, partnum);
563         mime_parser(msg->cm_fields['M'],
564                 NULL,
565                 *ical_locate_part,              /* callback function */
566                 NULL, NULL,
567                 (void *) &ird,                  /* user data */
568                 0
569         );
570
571         CtdlFreeMessage(msg);
572
573         if (ird.cal != NULL) {
574                 ical_hunt_for_conflicts(ird.cal);
575                 icalcomponent_free(ird.cal);
576                 return;
577         }
578         else {
579                 cprintf("%d No calendar object found\n", ERROR);
580                 return;
581         }
582
583         /* should never get here */
584 }
585
586
587
588
589 /*
590  * All Citadel calendar commands from the client come through here.
591  */
592 void cmd_ical(char *argbuf)
593 {
594         char subcmd[SIZ];
595         long msgnum;
596         char partnum[SIZ];
597         char action[SIZ];
598
599         if (CtdlAccessCheck(ac_logged_in)) return;
600
601         extract(subcmd, argbuf, 0);
602
603         if (!strcmp(subcmd, "test")) {
604                 cprintf("%d This server supports calendaring\n", CIT_OK);
605                 return;
606         }
607
608         else if (!strcmp(subcmd, "respond")) {
609                 msgnum = extract_long(argbuf, 1);
610                 extract(partnum, argbuf, 2);
611                 extract(action, argbuf, 3);
612                 ical_respond(msgnum, partnum, action);
613         }
614
615         else if (!strcmp(subcmd, "conflicts")) {
616                 msgnum = extract_long(argbuf, 1);
617                 extract(partnum, argbuf, 2);
618                 ical_conflicts(msgnum, partnum);
619         }
620
621         else {
622                 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
623                 return;
624         }
625
626         /* should never get here */
627 }
628
629
630
631 /*
632  * We don't know if the calendar room exists so we just create it at login
633  */
634 void ical_create_room(void)
635 {
636         struct quickroom qr;
637         struct visit vbuf;
638
639         /* Create the calendar room if it doesn't already exist */
640         create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
641
642         /* Set expiration policy to manual; otherwise objects will be lost! */
643         if (lgetroom(&qr, USERCALENDARROOM)) {
644                 lprintf(3, "Couldn't get the user calendar room!\n");
645                 return;
646         }
647         qr.QRep.expire_mode = EXPIRE_MANUAL;
648         lputroom(&qr);
649
650         /* Set the view to a calendar view */
651         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
652         vbuf.v_view = 3;        /* 3 = calendar */
653         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
654
655         /* Create the tasks list room if it doesn't already exist */
656         create_room(USERTASKSROOM, 4, "", 0, 1, 0);
657
658         /* Set expiration policy to manual; otherwise objects will be lost! */
659         if (lgetroom(&qr, USERTASKSROOM)) {
660                 lprintf(3, "Couldn't get the user calendar room!\n");
661                 return;
662         }
663         qr.QRep.expire_mode = EXPIRE_MANUAL;
664         lputroom(&qr);
665
666         /* Set the view to a task list view */
667         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
668         vbuf.v_view = 4;        /* 4 = tasks */
669         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
670
671         return;
672 }
673
674
675 /*
676  * ical_send_out_invitations() is called by ical_saving_vevent() when it
677  * finds a VEVENT.   FIXME ... finish implementing.
678  */
679 void ical_send_out_invitations(icalcomponent *cal) {
680         icalcomponent *the_request = NULL;
681         char *serialized_request = NULL;
682         char *request_message_text = NULL;
683         struct CtdlMessage *msg = NULL;
684         struct recptypes *valid = NULL;
685         char attendees_string[SIZ];
686         char this_attendee[SIZ];
687         icalproperty *attendee = NULL;
688         char summary_string[SIZ];
689         icalproperty *summary = NULL;
690         icalcomponent *encaps = NULL;
691
692         if (cal == NULL) {
693                 lprintf(3, "ERROR: trying to reply to NULL event?\n");
694                 return;
695         }
696
697         /* Clone the event */
698         the_request = icalcomponent_new_clone(cal);
699         if (the_request == NULL) {
700                 lprintf(3, "ERROR: cannot clone calendar object\n");
701                 return;
702         }
703
704         /* Extract the summary string -- we'll use it as the
705          * message subject for the request
706          */
707         strcpy(summary_string, "Meeting request");
708         summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
709         if (summary != NULL) {
710                 if (icalproperty_get_summary(summary)) {
711                         strcpy(summary_string,
712                                 icalproperty_get_summary(summary) );
713                 }
714         }
715
716         /* Determine who the recipients of this message are (the attendees) */
717         strcpy(attendees_string, "");
718         for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
719                 if (icalproperty_get_attendee(attendee)) {
720                         strcpy(this_attendee, icalproperty_get_attendee(attendee) );
721                         if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
722                                 strcpy(this_attendee, &this_attendee[7]);
723                                 snprintf(&attendees_string[strlen(attendees_string)],
724                                         sizeof(attendees_string) - strlen(attendees_string),
725                                         "%s, ",
726                                         this_attendee
727                                 );
728                         }
729                 }
730         }
731
732         lprintf(9, "attendees_string: <%s>\n", attendees_string);
733
734         /* Encapsulate the VEVENT component into a complete VCALENDAR */
735         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
736         if (encaps == NULL) {
737                 lprintf(3, "Error at %s:%d - could not allocate component!\n",
738                         __FILE__, __LINE__);
739                 icalcomponent_free(the_request);
740                 return;
741         }
742
743         /* Set the Product ID */
744         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
745
746         /* Set the Version Number */
747         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
748
749         /* Set the method to REQUEST */
750         icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
751
752         /* FIXME: here we need to insert a VTIMEZONE object. */
753
754         /* Here we go: put the VEVENT into the VCALENDAR.  We now no longer
755          * are responsible for "the_request"'s memory -- it will be freed
756          * when we free "encaps".
757          */
758         icalcomponent_add_component(encaps, the_request);
759
760         /* Serialize it */
761         serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
762         icalcomponent_free(encaps);     /* Don't need this anymore. */
763         if (serialized_request == NULL) return;
764
765         request_message_text = mallok(strlen(serialized_request) + SIZ);
766         if (request_message_text != NULL) {
767                 sprintf(request_message_text,
768                         "Content-type: text/calendar\r\n\r\n%s\r\n",
769                         serialized_request
770                 );
771
772                 msg = CtdlMakeMessage(&CC->usersupp,
773                         "",                     /* No single recipient here */
774                         CC->quickroom.QRname, 0, FMT_RFC822,
775                         "",
776                         summary_string,         /* Use summary for subject */
777                         request_message_text);
778         
779                 if (msg != NULL) {
780                         valid = validate_recipients(attendees_string);
781                         CtdlSubmitMsg(msg, valid, "");
782                         CtdlFreeMessage(msg);
783                 }
784         }
785         phree(serialized_request);
786 }
787
788
789 /*
790  * When a calendar object is being saved, determine whether it's a VEVENT
791  * and the user saving it is the organizer.  If so, send out invitations
792  * to any listed attendees.
793  *
794  */
795 void ical_saving_vevent(icalcomponent *cal) {
796         icalcomponent *c;
797         icalproperty *organizer = NULL;
798         char organizer_string[SIZ];
799
800         strcpy(organizer_string, "");
801         /*
802          * The VEVENT subcomponent is the one we're interested in.
803          * Send out invitations if, and only if, this user is the Organizer.
804          */
805         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
806                 organizer = icalcomponent_get_first_property(cal,
807                                                 ICAL_ORGANIZER_PROPERTY);
808                 if (organizer != NULL) {
809                         if (icalproperty_get_organizer(organizer)) {
810                                 strcpy(organizer_string,
811                                         icalproperty_get_organizer(organizer));
812                         }
813                 }
814                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
815                         strcpy(organizer_string, &organizer_string[7]);
816                         striplt(organizer_string);
817                         /*
818                          * If the user saving the event is listed as the
819                          * organizer, then send out invitations.
820                          */
821                         if (CtdlIsMe(organizer_string)) {
822                                 ical_send_out_invitations(cal);
823                         }
824                 }
825         }
826
827         /* If the component has subcomponents, recurse through them. */
828         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
829             (c != NULL);
830             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
831                 /* Recursively process subcomponent */
832                 ical_saving_vevent(c);
833         }
834
835 }
836
837
838
839 /*
840  * Back end for ical_obj_beforesave()
841  * This hunts for the UID of the calendar event.
842  */
843 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
844                 char *disp, void *content, char *cbtype, size_t length,
845                 char *encoding, void *cbuserdata)
846 {
847         icalcomponent *cal;
848         icalproperty *p;
849
850         /* If this is a text/calendar object, hunt for the UID and drop it in
851          * the "user data" pointer for the MIME parser.  When
852          * ical_obj_beforesave() sees it there, it'll set the Extended msgid
853          * to that string.
854          */
855         if (!strcasecmp(cbtype, "text/calendar")) {
856                 cal = icalcomponent_new_from_string(content);
857                 if (cal != NULL) {
858                         p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
859                         if (p != NULL) {
860                                 strcpy((char *)cbuserdata,
861                                         icalproperty_get_comment(p)
862                                 );
863                         }
864                         icalcomponent_free(cal);
865                 }
866         }
867 }
868
869
870
871
872
873 /*
874  * See if we need to prevent the object from being saved (we don't allow
875  * MIME types other than text/calendar in the Calendar> room).  Also, when
876  * saving an event to the calendar, set the message's Citadel extended message
877  * ID to the UID of the object.  This causes our replication checker to
878  * automatically delete any existing instances of the same object.  (Isn't
879  * that cool?)
880  */
881 int ical_obj_beforesave(struct CtdlMessage *msg)
882 {
883         char roomname[ROOMNAMELEN];
884         char *p;
885         int a;
886         char eidbuf[SIZ];
887
888         /*
889          * Only messages with content-type text/calendar
890          * may be saved to Calendar>.  If the message is bound for
891          * Calendar> but doesn't have this content-type, throw an error
892          * so that the message may not be posted.
893          */
894
895         /* First determine if this is our room */
896         MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
897         if (strcasecmp(roomname, CC->quickroom.QRname)) {
898                 return 0;       /* It's not the Calendar room. */
899         }
900
901         /* Then determine content-type of the message */
902         
903         /* It must be an RFC822 message! */
904         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
905         if (msg->cm_format_type != 4)
906                 return 1;       /* You tried to save a non-RFC822 message! */
907         
908         /* Find the Content-Type: header */
909         p = msg->cm_fields['M'];
910         a = strlen(p);
911         while (--a > 0) {
912                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
913                         if (!strncasecmp(p + 14, "text/calendar", 13)) {
914                                 strcpy(eidbuf, "");
915                                 mime_parser(msg->cm_fields['M'],
916                                         NULL,
917                                         *ical_ctdl_set_extended_msgid,
918                                         NULL, NULL,
919                                         (void *)eidbuf,
920                                         0
921                                 );
922                                 if (strlen(eidbuf) > 0) {
923                                         if (msg->cm_fields['E'] != NULL) {
924                                                 phree(msg->cm_fields['E']);
925                                         }
926                                         msg->cm_fields['E'] = strdoop(eidbuf);
927                                 }
928                                 return 0;
929                         }
930                         else {
931                                 return 1;
932                         }
933                 }
934                 p++;
935         }
936         
937         /* Oops!  No Content-Type in this message!  How'd that happen? */
938         lprintf(7, "RFC822 message with no Content-Type header!\n");
939         return 1;
940 }
941
942
943 /*
944  * Things we need to do after saving a calendar event.
945  */
946 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
947                 char *disp, void *content, char *cbtype, size_t length,
948                 char *encoding, void *cbuserdata)
949 {
950         icalcomponent *cal;
951
952         /* If this is a text/calendar object, hunt for the UID and drop it in
953          * the "user data" pointer for the MIME parser.  When
954          * ical_obj_beforesave() sees it there, it'll set the Extended msgid
955          * to that string.
956          */
957         if (!strcasecmp(cbtype, "text/calendar")) {
958                 cal = icalcomponent_new_from_string(content);
959                 if (cal != NULL) {
960                         ical_saving_vevent(cal);
961                         icalcomponent_free(cal);
962                 }
963         }
964 }
965
966
967 /* 
968  * Things we need to do after saving a calendar event.
969  */
970 int ical_obj_aftersave(struct CtdlMessage *msg)
971 {
972         char roomname[ROOMNAMELEN];
973         char *p;
974         int a;
975
976         /*
977          * If this isn't the Calendar> room, no further action is necessary.
978          */
979
980         /* First determine if this is our room */
981         MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
982         if (strcasecmp(roomname, CC->quickroom.QRname)) {
983                 return 0;       /* It's not the Calendar room. */
984         }
985
986         /* Then determine content-type of the message */
987         
988         /* It must be an RFC822 message! */
989         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
990         if (msg->cm_format_type != 4) return(1);
991         
992         /* Find the Content-Type: header */
993         p = msg->cm_fields['M'];
994         a = strlen(p);
995         while (--a > 0) {
996                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
997                         if (!strncasecmp(p + 14, "text/calendar", 13)) {
998                                 mime_parser(msg->cm_fields['M'],
999                                         NULL,
1000                                         *ical_obj_aftersave_backend,
1001                                         NULL, NULL,
1002                                         NULL,
1003                                         0
1004                                 );
1005                                 return 0;
1006                         }
1007                         else {
1008                                 return 1;
1009                         }
1010                 }
1011                 p++;
1012         }
1013         
1014         /* Oops!  No Content-Type in this message!  How'd that happen? */
1015         lprintf(7, "RFC822 message with no Content-Type header!\n");
1016         return 1;
1017 }
1018
1019
1020 #endif  /* HAVE_ICAL_H */
1021
1022 /*
1023  * Register this module with the Citadel server.
1024  */
1025 char *Dynamic_Module_Init(void)
1026 {
1027 #ifdef HAVE_ICAL_H
1028         CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1029         CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1030         CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1031         CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
1032 #endif
1033         return "$Id$";
1034 }