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