]> code.citadel.org Git - citadel.git/blob - citadel/serv_calendar.c
* serv_calendar.c: registered a fixed output hook for text/calendar.
[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 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 <stdlib.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <limits.h>
17 #include <stdio.h>
18 #include <string.h>
19 #ifdef HAVE_STRINGS_H
20 #include <strings.h>
21 #endif
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 "serv_extensions.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 #include "internet_addressing.h"
35 #include "serv_calendar.h"
36
37 #ifdef CITADEL_WITH_CALENDAR_SERVICE
38
39 #include <ical.h>
40 #include "ical_dezonify.h"
41
42 struct ical_respond_data {
43         char desired_partnum[SIZ];
44         icalcomponent *cal;
45 };
46
47
48 /*
49  * Utility function to create a new VCALENDAR component with some of the
50  * required fields already set the way we like them.
51  */
52 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
53         icalcomponent *encaps;
54
55         encaps = icalcomponent_new_vcalendar();
56         if (encaps == NULL) {
57                 lprintf(CTDL_CRIT, "Error at %s:%d - could not allocate component!\n",
58                         __FILE__, __LINE__);
59                 return NULL;
60         }
61
62         /* Set the Product ID */
63         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
64
65         /* Set the Version Number */
66         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
67
68         return(encaps);
69 }
70
71
72 /*
73  * Utility function to encapsulate a subcomponent into a full VCALENDAR
74  */
75 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
76         icalcomponent *encaps;
77
78         /* If we're already looking at a full VCALENDAR component,
79          * don't bother ... just return itself.
80          */
81         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
82                 return subcomp;
83         }
84
85         /* Encapsulate the VEVENT component into a complete VCALENDAR */
86         encaps = icalcomponent_new_citadel_vcalendar();
87         if (encaps == NULL) return NULL;
88
89         /* Encapsulate the subcomponent inside */
90         icalcomponent_add_component(encaps, subcomp);
91
92         /* Convert all timestamps to UTC so we don't have to deal with
93          * stupid VTIMEZONE crap.
94          */
95         ical_dezonify(encaps);
96
97         /* Return the object we just created. */
98         return(encaps);
99 }
100
101
102
103
104 /*
105  * Write a calendar object into the specified user's calendar room.
106  * 
107  * ok
108  */
109 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
110         char temp[PATH_MAX];
111         FILE *fp;
112         char *ser;
113         icalcomponent *encaps;
114
115         if (cal == NULL) return;
116
117         /* If the supplied object is a subcomponent, encapsulate it in
118          * a full VCALENDAR component, and save that instead.
119          */
120         if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
121                 encaps = ical_encapsulate_subcomponent(
122                         icalcomponent_new_clone(cal)
123                 );
124                 ical_write_to_cal(u, encaps);
125                 icalcomponent_free(encaps);
126                 return;
127         }
128
129         strcpy(temp, tmpnam(NULL));
130         ser = icalcomponent_as_ical_string(cal);
131         if (ser == NULL) return;
132
133         /* Make a temp file out of it */
134         fp = fopen(temp, "w");
135         if (fp == NULL) return;
136         fwrite(ser, strlen(ser), 1, fp);
137         fclose(fp);
138
139         /* This handy API function does all the work for us.
140          */
141         CtdlWriteObject(USERCALENDARROOM,       /* which room */
142                         "text/calendar",        /* MIME type */
143                         temp,                   /* temp file */
144                         u,                      /* which user */
145                         0,                      /* not binary */
146                         0,              /* don't delete others of this type */
147                         0);                     /* no flags */
148
149         unlink(temp);
150 }
151
152
153 /*
154  * Add a calendar object to the user's calendar
155  * 
156  * ok because it uses ical_write_to_cal()
157  */
158 void ical_add(icalcomponent *cal, int recursion_level) {
159         icalcomponent *c;
160
161         /*
162          * The VEVENT subcomponent is the one we're interested in saving.
163          */
164         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
165         
166                 ical_write_to_cal(&CC->user, cal);
167
168         }
169
170         /* If the component has subcomponents, recurse through them. */
171         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
172             (c != 0);
173             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
174                 /* Recursively process subcomponent */
175                 ical_add(c, recursion_level+1);
176         }
177
178 }
179
180
181
182 /*
183  * Send a reply to a meeting invitation.
184  *
185  * 'request' is the invitation to reply to.
186  * 'action' is the string "accept" or "decline".
187  *
188  * (Sorry about this being more than 80 columns ... there was just
189  * no easy way to break it down sensibly.)
190  * 
191  * ok
192  */
193 void ical_send_a_reply(icalcomponent *request, char *action) {
194         icalcomponent *the_reply = NULL;
195         icalcomponent *vevent = NULL;
196         icalproperty *attendee = NULL;
197         char attendee_string[SIZ];
198         icalproperty *organizer = NULL;
199         char organizer_string[SIZ];
200         icalproperty *summary = NULL;
201         char summary_string[SIZ];
202         icalproperty *me_attend = NULL;
203         struct recptypes *recp = NULL;
204         icalparameter *partstat = NULL;
205         char *serialized_reply = NULL;
206         char *reply_message_text = NULL;
207         struct CtdlMessage *msg = NULL;
208         struct recptypes *valid = NULL;
209
210         strcpy(organizer_string, "");
211         strcpy(summary_string, "Calendar item");
212
213         if (request == NULL) {
214                 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
215                 return;
216         }
217
218         the_reply = icalcomponent_new_clone(request);
219         if (the_reply == NULL) {
220                 lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
221                 return;
222         }
223
224         /* Change the method from REQUEST to REPLY */
225         icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
226
227         vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
228         if (vevent != NULL) {
229                 /* Hunt for attendees, removing ones that aren't us.
230                  * (Actually, remove them all, cloning our own one so we can
231                  * re-insert it later)
232                  */
233                 while (attendee = icalcomponent_get_first_property(vevent,
234                     ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
235                 ) {
236                         if (icalproperty_get_attendee(attendee)) {
237                                 strcpy(attendee_string,
238                                         icalproperty_get_attendee(attendee) );
239                                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
240                                         strcpy(attendee_string, &attendee_string[7]);
241                                         striplt(attendee_string);
242                                         recp = validate_recipients(attendee_string);
243                                         if (recp != NULL) {
244                                                 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
245                                                         if (me_attend) icalproperty_free(me_attend);
246                                                         me_attend = icalproperty_new_clone(attendee);
247                                                 }
248                                                 free(recp);
249                                         }
250                                 }
251                         }
252                         /* Remove it... */
253                         icalcomponent_remove_property(vevent, attendee);
254                         icalproperty_free(attendee);
255                 }
256
257                 /* We found our own address in the attendee list. */
258                 if (me_attend) {
259                         /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
260                         icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
261
262                         if (!strcasecmp(action, "accept")) {
263                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
264                         }
265                         else if (!strcasecmp(action, "decline")) {
266                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
267                         }
268                         else if (!strcasecmp(action, "tentative")) {
269                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
270                         }
271
272                         if (partstat) icalproperty_add_parameter(me_attend, partstat);
273
274                         /* Now insert it back into the vevent. */
275                         icalcomponent_add_property(vevent, me_attend);
276                 }
277
278                 /* Figure out who to send this thing to */
279                 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
280                 if (organizer != NULL) {
281                         if (icalproperty_get_organizer(organizer)) {
282                                 strcpy(organizer_string,
283                                         icalproperty_get_organizer(organizer) );
284                         }
285                 }
286                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
287                         strcpy(organizer_string, &organizer_string[7]);
288                         striplt(organizer_string);
289                 } else {
290                         strcpy(organizer_string, "");
291                 }
292
293                 /* Extract the summary string -- we'll use it as the
294                  * message subject for the reply
295                  */
296                 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
297                 if (summary != NULL) {
298                         if (icalproperty_get_summary(summary)) {
299                                 strcpy(summary_string,
300                                         icalproperty_get_summary(summary) );
301                         }
302                 }
303
304         }
305
306         /* Now generate the reply message and send it out. */
307         serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
308         icalcomponent_free(the_reply);  /* don't need this anymore */
309         if (serialized_reply == NULL) return;
310
311         reply_message_text = malloc(strlen(serialized_reply) + SIZ);
312         if (reply_message_text != NULL) {
313                 sprintf(reply_message_text,
314                         "Content-type: text/calendar\r\n\r\n%s\r\n",
315                         serialized_reply
316                 );
317
318                 msg = CtdlMakeMessage(&CC->user,
319                         organizer_string,       /* to */
320                         "",                     /* cc */
321                         CC->room.QRname, 0, FMT_RFC822,
322                         "",
323                         summary_string,         /* Use summary for subject */
324                         reply_message_text);
325         
326                 if (msg != NULL) {
327                         valid = validate_recipients(organizer_string);
328                         CtdlSubmitMsg(msg, valid, "");
329                         CtdlFreeMessage(msg);
330                 }
331         }
332         free(serialized_reply);
333 }
334
335
336
337 /*
338  * Callback function for mime parser that hunts for calendar content types
339  * and turns them into calendar objects
340  */
341 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
342                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
343                 void *cbuserdata) {
344
345         struct ical_respond_data *ird = NULL;
346
347         ird = (struct ical_respond_data *) cbuserdata;
348
349         /* desired_partnum can be set to "_HUNT_" to have it just look for
350          * the first part with a content type of text/calendar.  Otherwise
351          * we have to only process the right one.
352          */
353         if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
354                 if (strcasecmp(partnum, ird->desired_partnum)) {
355                         return;
356                 }
357         }
358
359         if (strcasecmp(cbtype, "text/calendar")) {
360                 return;
361         }
362
363         if (ird->cal != NULL) {
364                 icalcomponent_free(ird->cal);
365                 ird->cal = NULL;
366         }
367
368         ird->cal = icalcomponent_new_from_string(content);
369         if (ird->cal != NULL) {
370                 ical_dezonify(ird->cal);
371         }
372 }
373
374
375 /*
376  * Respond to a meeting request.
377  */
378 void ical_respond(long msgnum, char *partnum, char *action) {
379         struct CtdlMessage *msg;
380         struct ical_respond_data ird;
381
382         if (
383            (strcasecmp(action, "accept"))
384            && (strcasecmp(action, "decline"))
385         ) {
386                 cprintf("%d Action must be 'accept' or 'decline'\n",
387                         ERROR + ILLEGAL_VALUE
388                 );
389                 return;
390         }
391
392         msg = CtdlFetchMessage(msgnum, 1);
393         if (msg == NULL) {
394                 cprintf("%d Message %ld not found.\n",
395                         ERROR + ILLEGAL_VALUE,
396                         (long)msgnum
397                 );
398                 return;
399         }
400
401         memset(&ird, 0, sizeof ird);
402         strcpy(ird.desired_partnum, partnum);
403         mime_parser(msg->cm_fields['M'],
404                 NULL,
405                 *ical_locate_part,              /* callback function */
406                 NULL, NULL,
407                 (void *) &ird,                  /* user data */
408                 0
409         );
410
411         /* We're done with the incoming message, because we now have a
412          * calendar object in memory.
413          */
414         CtdlFreeMessage(msg);
415
416         /*
417          * Here is the real meat of this function.  Handle the event.
418          */
419         if (ird.cal != NULL) {
420                 /* Save this in the user's calendar if necessary */
421                 if (!strcasecmp(action, "accept")) {
422                         ical_add(ird.cal, 0);
423                 }
424
425                 /* Send a reply if necessary */
426                 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
427                         ical_send_a_reply(ird.cal, action);
428                 }
429
430                 /* Now that we've processed this message, we don't need it
431                  * anymore.  So delete it.
432                  */
433                 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
434
435                 /* Free the memory we allocated and return a response. */
436                 icalcomponent_free(ird.cal);
437                 ird.cal = NULL;
438                 cprintf("%d ok\n", CIT_OK);
439                 return;
440         }
441         else {
442                 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
443                 return;
444         }
445
446         /* should never get here */
447 }
448
449
450 /*
451  * Figure out the UID of the calendar event being referred to in a
452  * REPLY object.  This function is recursive.
453  */
454 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
455         icalcomponent *subcomponent;
456         icalproperty *p;
457
458         /* If this object is a REPLY, then extract the UID. */
459         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
460                 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
461                 if (p != NULL) {
462                         strcpy(uidbuf, icalproperty_get_comment(p));
463                 }
464         }
465
466         /* Otherwise, recurse through any VEVENT subcomponents.  We do NOT want the
467          * UID of the reply; we want the UID of the invitation being replied to.
468          */
469         for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
470             subcomponent != NULL;
471             subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
472                 ical_learn_uid_of_reply(uidbuf, subcomponent);
473         }
474 }
475
476
477 /*
478  * ical_update_my_calendar_with_reply() refers to this callback function; when we
479  * locate the message containing the calendar event we're replying to, this function
480  * gets called.  It basically just sticks the message number in a supplied buffer.
481  */
482 void ical_hunt_for_event_to_update(long msgnum, void *data) {
483         long *msgnumptr;
484
485         msgnumptr = (long *) data;
486         *msgnumptr = msgnum;
487 }
488
489
490 struct original_event_container {
491         icalcomponent *c;
492 };
493
494 /*
495  * Callback function for mime parser that hunts for calendar content types
496  * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
497  * to fetch the object being updated)
498  */
499 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
500                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
501                 void *cbuserdata) {
502
503         struct original_event_container *oec = NULL;
504
505         if (strcasecmp(cbtype, "text/calendar")) {
506                 return;
507         }
508         oec = (struct original_event_container *) cbuserdata;
509         if (oec->c != NULL) {
510                 icalcomponent_free(oec->c);
511         }
512         oec->c = icalcomponent_new_from_string(content);
513 }
514
515
516 /*
517  * Merge updated attendee information from a REPLY into an existing event.
518  */
519 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
520         icalcomponent *c;
521         icalproperty *e_attendee, *r_attendee;
522
523         /* First things first.  If we're not looking at a VEVENT component,
524          * recurse through subcomponents until we find one.
525          */
526         if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
527                 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
528                     c != NULL;
529                     c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
530                         ical_merge_attendee_reply(c, reply);
531                 }
532                 return;
533         }
534
535         /* Now do the same thing with the reply.
536          */
537         if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
538                 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
539                     c != NULL;
540                     c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
541                         ical_merge_attendee_reply(event, c);
542                 }
543                 return;
544         }
545
546         /* Clone the reply, because we're going to rip its guts out. */
547         reply = icalcomponent_new_clone(reply);
548
549         /* At this point we're looking at the correct subcomponents.
550          * Iterate through the attendees looking for a match.
551          */
552 STARTOVER:
553         for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
554             e_attendee != NULL;
555             e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
556
557                 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
558                     r_attendee != NULL;
559                     r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
560
561                         /* Check to see if these two attendees match...
562                          */
563                         if (!strcasecmp(
564                            icalproperty_get_attendee(e_attendee),
565                            icalproperty_get_attendee(r_attendee)
566                         )) {
567                                 /* ...and if they do, remove the attendee from the event
568                                  * and replace it with the attendee from the reply.  (The
569                                  * reply's copy will have the same address, but an updated
570                                  * status.)
571                                  */
572                                 icalcomponent_remove_property(event, e_attendee);
573                                 icalproperty_free(e_attendee);
574                                 icalcomponent_remove_property(reply, r_attendee);
575                                 icalcomponent_add_property(event, r_attendee);
576
577                                 /* Since we diddled both sets of attendees, we have to start
578                                  * the iteration over again.  This will not create an infinite
579                                  * loop because we removed the attendee from the reply.  (That's
580                                  * why we cloned the reply, and that's what we mean by "ripping
581                                  * its guts out.")
582                                  */
583                                 goto STARTOVER;
584                         }
585         
586                 }
587         }
588
589         /* Free the *clone* of the reply. */
590         icalcomponent_free(reply);
591 }
592
593
594
595
596 /*
597  * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
598  * calendar event.  The object has already been deserialized for us; all
599  * we have to do here is hunt for the event in our calendar, merge in the
600  * updated attendee status, and save it again.
601  *
602  * This function returns 0 on success, 1 if the event was not found in the
603  * user's calendar, or 2 if an internal error occurred.
604  */
605 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
606         char uid[SIZ];
607         char hold_rm[ROOMNAMELEN];
608         long msgnum_being_replaced = 0;
609         struct CtdlMessage *template = NULL;
610         struct CtdlMessage *msg;
611         struct original_event_container oec;
612         icalcomponent *original_event;
613         char *serialized_event = NULL;
614         char roomname[ROOMNAMELEN];
615         char *message_text = NULL;
616
617         /* Figure out just what event it is we're dealing with */
618         strcpy(uid, "--==<< InVaLiD uId >>==--");
619         ical_learn_uid_of_reply(uid, cal);
620         lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
621
622         strcpy(hold_rm, CC->room.QRname);       /* save current room */
623
624         if (getroom(&CC->room, USERCALENDARROOM) != 0) {
625                 getroom(&CC->room, hold_rm);
626                 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
627                 return(2);
628         }
629
630         /*
631          * Pound through the user's calendar looking for a message with
632          * the Citadel EUID set to the value we're looking for.  Since
633          * Citadel always sets the message EUID to the vCalendar UID of
634          * the event, this will work.
635          */
636         template = (struct CtdlMessage *)
637                 malloc(sizeof(struct CtdlMessage));
638         memset(template, 0, sizeof(struct CtdlMessage));
639         template->cm_fields['E'] = strdup(uid);
640         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
641                 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
642         CtdlFreeMessage(template);
643         getroom(&CC->room, hold_rm);    /* return to saved room */
644
645         lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
646         if (msgnum_being_replaced == 0) {
647                 return(1);                      /* no calendar event found */
648         }
649
650         /* Now we know the ID of the message containing the event being updated.
651          * We don't actually have to delete it; that'll get taken care of by the
652          * server when we save another event with the same UID.  This just gives
653          * us the ability to load the event into memory so we can diddle the
654          * attendees.
655          */
656         msg = CtdlFetchMessage(msgnum_being_replaced, 1);
657         if (msg == NULL) {
658                 return(2);                      /* internal error */
659         }
660         oec.c = NULL;
661         mime_parser(msg->cm_fields['M'],
662                 NULL,
663                 *ical_locate_original_event,    /* callback function */
664                 NULL, NULL,
665                 &oec,                           /* user data */
666                 0
667         );
668         CtdlFreeMessage(msg);
669
670         original_event = oec.c;
671         if (original_event == NULL) {
672                 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
673                 return(2);
674         }
675
676         /* Merge the attendee's updated status into the event */
677         ical_merge_attendee_reply(original_event, cal);
678
679         /* Serialize it */
680         serialized_event = strdup(icalcomponent_as_ical_string(original_event));
681         icalcomponent_free(original_event);     /* Don't need this anymore. */
682         if (serialized_event == NULL) return(2);
683
684         MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
685
686         message_text = malloc(strlen(serialized_event) + SIZ);
687         if (message_text != NULL) {
688                 sprintf(message_text,
689                         "Content-type: text/calendar\r\n\r\n%s\r\n",
690                         serialized_event
691                 );
692
693                 msg = CtdlMakeMessage(&CC->user,
694                         "",                     /* No recipient */
695                         "",                     /* No recipient */
696                         roomname,
697                         0, FMT_RFC822,
698                         "",
699                         "",             /* no subject */
700                         message_text);
701         
702                 if (msg != NULL) {
703                         CIT_ICAL->avoid_sending_invitations = 1;
704                         CtdlSubmitMsg(msg, NULL, roomname);
705                         CtdlFreeMessage(msg);
706                         CIT_ICAL->avoid_sending_invitations = 0;
707                 }
708         }
709         free(serialized_event);
710         return(0);
711 }
712
713
714 /*
715  * Handle an incoming RSVP for an event.  (This is the server subcommand part; it
716  * simply extracts the calendar object from the message, deserializes it, and
717  * passes it up to ical_update_my_calendar_with_reply() for processing.
718  */
719 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
720         struct CtdlMessage *msg;
721         struct ical_respond_data ird;
722         int ret;
723
724         if (
725            (strcasecmp(action, "update"))
726            && (strcasecmp(action, "ignore"))
727         ) {
728                 cprintf("%d Action must be 'update' or 'ignore'\n",
729                         ERROR + ILLEGAL_VALUE
730                 );
731                 return;
732         }
733
734         msg = CtdlFetchMessage(msgnum, 1);
735         if (msg == NULL) {
736                 cprintf("%d Message %ld not found.\n",
737                         ERROR + ILLEGAL_VALUE,
738                         (long)msgnum
739                 );
740                 return;
741         }
742
743         memset(&ird, 0, sizeof ird);
744         strcpy(ird.desired_partnum, partnum);
745         mime_parser(msg->cm_fields['M'],
746                 NULL,
747                 *ical_locate_part,              /* callback function */
748                 NULL, NULL,
749                 (void *) &ird,                  /* user data */
750                 0
751         );
752
753         /* We're done with the incoming message, because we now have a
754          * calendar object in memory.
755          */
756         CtdlFreeMessage(msg);
757
758         /*
759          * Here is the real meat of this function.  Handle the event.
760          */
761         if (ird.cal != NULL) {
762                 /* Update the user's calendar if necessary */
763                 if (!strcasecmp(action, "update")) {
764                         ret = ical_update_my_calendar_with_reply(ird.cal);
765                         if (ret == 0) {
766                                 cprintf("%d Your calendar has been updated with this reply.\n",
767                                         CIT_OK);
768                         }
769                         else if (ret == 1) {
770                                 cprintf("%d This event does not exist in your calendar.\n",
771                                         ERROR + FILE_NOT_FOUND);
772                         }
773                         else {
774                                 cprintf("%d An internal error occurred.\n",
775                                         ERROR + INTERNAL_ERROR);
776                         }
777                 }
778                 else {
779                         cprintf("%d This reply has been ignored.\n", CIT_OK);
780                 }
781
782                 /* Now that we've processed this message, we don't need it
783                  * anymore.  So delete it.  (Maybe make this optional?)
784                  */
785                 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
786
787                 /* Free the memory we allocated and return a response. */
788                 icalcomponent_free(ird.cal);
789                 ird.cal = NULL;
790                 return;
791         }
792         else {
793                 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
794                 return;
795         }
796
797         /* should never get here */
798 }
799
800
801 /*
802  * Search for a property in both the top level and in a VEVENT subcomponent
803  */
804 icalproperty *ical_ctdl_get_subprop(
805                 icalcomponent *cal,
806                 icalproperty_kind which_prop
807 ) {
808         icalproperty *p;
809         icalcomponent *c;
810
811         p = icalcomponent_get_first_property(cal, which_prop);
812         if (p == NULL) {
813                 c = icalcomponent_get_first_component(cal,
814                                                         ICAL_VEVENT_COMPONENT);
815                 if (c != NULL) {
816                         p = icalcomponent_get_first_property(c, which_prop);
817                 }
818         }
819         return p;
820 }
821
822
823 /*
824  * Check to see if two events overlap.  Returns nonzero if they do.
825  * (This function is used in both Citadel and WebCit.  If you change it in
826  * one place, change it in the other.  Better yet, put it in a library.)
827  */
828 int ical_ctdl_is_overlap(
829                         struct icaltimetype t1start,
830                         struct icaltimetype t1end,
831                         struct icaltimetype t2start,
832                         struct icaltimetype t2end
833 ) {
834
835         if (icaltime_is_null_time(t1start)) return(0);
836         if (icaltime_is_null_time(t2start)) return(0);
837
838         /* First, check for all-day events */
839         if (t1start.is_date) {
840                 if (!icaltime_compare_date_only(t1start, t2start)) {
841                         return(1);
842                 }
843                 if (!icaltime_is_null_time(t2end)) {
844                         if (!icaltime_compare_date_only(t1start, t2end)) {
845                                 return(1);
846                         }
847                 }
848         }
849
850         if (t2start.is_date) {
851                 if (!icaltime_compare_date_only(t2start, t1start)) {
852                         return(1);
853                 }
854                 if (!icaltime_is_null_time(t1end)) {
855                         if (!icaltime_compare_date_only(t2start, t1end)) {
856                                 return(1);
857                         }
858                 }
859         }
860
861         /* Now check for overlaps using date *and* time. */
862
863         /* First, bail out if either event 1 or event 2 is missing end time. */
864         if (icaltime_is_null_time(t1end)) return(0);
865         if (icaltime_is_null_time(t2end)) return(0);
866
867         /* If event 1 ends before event 2 starts, we're in the clear. */
868         if (icaltime_compare(t1end, t2start) <= 0) return(0);
869
870         /* If event 2 ends before event 1 starts, we're also ok. */
871         if (icaltime_compare(t2end, t1start) <= 0) return(0);
872
873         /* Otherwise, they overlap. */
874         return(1);
875 }
876
877
878
879 /*
880  * Backend for ical_hunt_for_conflicts()
881  */
882 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
883         icalcomponent *cal;
884         struct CtdlMessage *msg;
885         struct ical_respond_data ird;
886         struct icaltimetype t1start, t1end, t2start, t2end;
887         icalproperty *p;
888         char conflict_event_uid[SIZ];
889         char conflict_event_summary[SIZ];
890         char compare_uid[SIZ];
891
892         cal = (icalcomponent *)data;
893         strcpy(compare_uid, "");
894         strcpy(conflict_event_uid, "");
895         strcpy(conflict_event_summary, "");
896
897         msg = CtdlFetchMessage(msgnum, 1);
898         if (msg == NULL) return;
899         memset(&ird, 0, sizeof ird);
900         strcpy(ird.desired_partnum, "_HUNT_");
901         mime_parser(msg->cm_fields['M'],
902                 NULL,
903                 *ical_locate_part,              /* callback function */
904                 NULL, NULL,
905                 (void *) &ird,                  /* user data */
906                 0
907         );
908         CtdlFreeMessage(msg);
909
910         if (ird.cal == NULL) return;
911
912         t1start = icaltime_null_time();
913         t1end = icaltime_null_time();
914         t2start = icaltime_null_time();
915         t1end = icaltime_null_time();
916
917         /* Now compare cal to ird.cal */
918         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
919         if (p == NULL) return;
920         if (p != NULL) t2start = icalproperty_get_dtstart(p);
921         
922         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
923         if (p != NULL) t2end = icalproperty_get_dtend(p);
924
925         p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
926         if (p == NULL) return;
927         if (p != NULL) t1start = icalproperty_get_dtstart(p);
928         
929         p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
930         if (p != NULL) t1end = icalproperty_get_dtend(p);
931         
932         p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
933         if (p != NULL) {
934                 strcpy(compare_uid, icalproperty_get_comment(p));
935         }
936
937         p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
938         if (p != NULL) {
939                 strcpy(conflict_event_uid, icalproperty_get_comment(p));
940         }
941
942         p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
943         if (p != NULL) {
944                 strcpy(conflict_event_summary, icalproperty_get_comment(p));
945         }
946
947         icalcomponent_free(ird.cal);
948
949         if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
950                 cprintf("%ld||%s|%s|%d|\n",
951                         msgnum,
952                         conflict_event_uid,
953                         conflict_event_summary,
954                         (       ((strlen(compare_uid)>0)
955                                 &&(!strcasecmp(compare_uid,
956                                 conflict_event_uid))) ? 1 : 0
957                         )
958                 );
959         }
960 }
961
962
963
964 /* 
965  * Phase 2 of "hunt for conflicts" operation.
966  * At this point we have a calendar object which represents the VEVENT that
967  * we're considering adding to the calendar.  Now hunt through the user's
968  * calendar room, and output zero or more existing VEVENTs which conflict
969  * with this one.
970  */
971 void ical_hunt_for_conflicts(icalcomponent *cal) {
972         char hold_rm[ROOMNAMELEN];
973
974         strcpy(hold_rm, CC->room.QRname);       /* save current room */
975
976         if (getroom(&CC->room, USERCALENDARROOM) != 0) {
977                 getroom(&CC->room, hold_rm);
978                 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
979                 return;
980         }
981
982         cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
983
984         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
985                 NULL,
986                 ical_hunt_for_conflicts_backend,
987                 (void *) cal
988         );
989
990         cprintf("000\n");
991         getroom(&CC->room, hold_rm);    /* return to saved room */
992
993 }
994
995
996
997 /*
998  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
999  */
1000 void ical_conflicts(long msgnum, char *partnum) {
1001         struct CtdlMessage *msg;
1002         struct ical_respond_data ird;
1003
1004         msg = CtdlFetchMessage(msgnum, 1);
1005         if (msg == NULL) {
1006                 cprintf("%d Message %ld not found.\n",
1007                         ERROR + ILLEGAL_VALUE,
1008                         (long)msgnum
1009                 );
1010                 return;
1011         }
1012
1013         memset(&ird, 0, sizeof ird);
1014         strcpy(ird.desired_partnum, partnum);
1015         mime_parser(msg->cm_fields['M'],
1016                 NULL,
1017                 *ical_locate_part,              /* callback function */
1018                 NULL, NULL,
1019                 (void *) &ird,                  /* user data */
1020                 0
1021         );
1022
1023         CtdlFreeMessage(msg);
1024
1025         if (ird.cal != NULL) {
1026                 ical_hunt_for_conflicts(ird.cal);
1027                 icalcomponent_free(ird.cal);
1028                 return;
1029         }
1030         else {
1031                 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1032                 return;
1033         }
1034
1035         /* should never get here */
1036 }
1037
1038
1039
1040 /*
1041  * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1042  */
1043 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1044         icalproperty *p;
1045         icalvalue *v;
1046         struct icalperiodtype my_period;
1047
1048         if (cal == NULL) return;
1049         my_period = icalperiodtype_null_period();
1050
1051         if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1052                 ical_add_to_freebusy(fb,
1053                         icalcomponent_get_first_component(
1054                                 cal, ICAL_VEVENT_COMPONENT
1055                         )
1056                 );
1057                 return;
1058         }
1059
1060         ical_dezonify(cal);
1061
1062         /* If this event is not opaque, the user isn't publishing it as
1063          * busy time, so don't bother doing anything else.
1064          */
1065         p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1066         if (p != NULL) {
1067                 v = icalproperty_get_value(p);
1068                 if (v != NULL) {
1069                         if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1070                                 return;
1071                         }
1072                 }
1073         }
1074
1075         /* Convert the DTSTART and DTEND properties to an icalperiod. */
1076         p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1077         if (p != NULL) {
1078                 my_period.start = icalproperty_get_dtstart(p);
1079         }
1080
1081         p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1082         if (p != NULL) {
1083                 my_period.end = icalproperty_get_dtstart(p);
1084         }
1085
1086         /* Now add it. */
1087         icalcomponent_add_property(fb,
1088                 icalproperty_new_freebusy(my_period)
1089         );
1090
1091         /* Make sure the DTSTART property of the freebusy *list* is set to
1092          * the DTSTART property of the *earliest event*.
1093          */
1094         p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1095         if (p == NULL) {
1096                 icalcomponent_set_dtstart(fb,
1097                         icalcomponent_get_dtstart(cal) );
1098         }
1099         else {
1100                 if (icaltime_compare(
1101                         icalcomponent_get_dtstart(cal),
1102                         icalcomponent_get_dtstart(fb)
1103                    ) < 0) {
1104                         icalcomponent_set_dtstart(fb,
1105                                 icalcomponent_get_dtstart(cal) );
1106                 }
1107         }
1108
1109         /* Make sure the DTEND property of the freebusy *list* is set to
1110          * the DTEND property of the *latest event*.
1111          */
1112         p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1113         if (p == NULL) {
1114                 icalcomponent_set_dtend(fb,
1115                         icalcomponent_get_dtend(cal) );
1116         }
1117         else {
1118                 if (icaltime_compare(
1119                         icalcomponent_get_dtend(cal),
1120                         icalcomponent_get_dtend(fb)
1121                    ) > 0) {
1122                         icalcomponent_set_dtend(fb,
1123                                 icalcomponent_get_dtend(cal) );
1124                 }
1125         }
1126
1127 }
1128
1129
1130
1131 /*
1132  * Backend for ical_freebusy()
1133  *
1134  * This function simply loads the messages in the user's calendar room,
1135  * which contain VEVENTs, then strips them of all non-freebusy data, and
1136  * adds them to the supplied VCALENDAR.
1137  *
1138  */
1139 void ical_freebusy_backend(long msgnum, void *data) {
1140         icalcomponent *cal;
1141         struct CtdlMessage *msg;
1142         struct ical_respond_data ird;
1143
1144         cal = (icalcomponent *)data;
1145
1146         msg = CtdlFetchMessage(msgnum, 1);
1147         if (msg == NULL) return;
1148         memset(&ird, 0, sizeof ird);
1149         strcpy(ird.desired_partnum, "_HUNT_");
1150         mime_parser(msg->cm_fields['M'],
1151                 NULL,
1152                 *ical_locate_part,              /* callback function */
1153                 NULL, NULL,
1154                 (void *) &ird,                  /* user data */
1155                 0
1156         );
1157         CtdlFreeMessage(msg);
1158
1159         if (ird.cal == NULL) return;
1160
1161         ical_add_to_freebusy(cal, ird.cal);
1162
1163         /* Now free the memory. */
1164         icalcomponent_free(ird.cal);
1165 }
1166
1167
1168
1169 /*
1170  * Grab another user's free/busy times
1171  */
1172 void ical_freebusy(char *who) {
1173         struct ctdluser usbuf;
1174         char calendar_room_name[ROOMNAMELEN];
1175         char hold_rm[ROOMNAMELEN];
1176         char *serialized_request = NULL;
1177         icalcomponent *encaps = NULL;
1178         icalcomponent *fb = NULL;
1179         int found_user = (-1);
1180         struct recptypes *recp = NULL;
1181         char buf[256];
1182         char host[256];
1183         char type[256];
1184         int i = 0;
1185         int config_lines = 0;
1186
1187         /* First try an exact match. */
1188         found_user = getuser(&usbuf, who);
1189
1190         /* If not found, try it as an unqualified email address. */
1191         if (found_user != 0) {
1192                 strcpy(buf, who);
1193                 recp = validate_recipients(buf);
1194                 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1195                 if (recp != NULL) {
1196                         if (recp->num_local == 1) {
1197                                 found_user = getuser(&usbuf, recp->recp_local);
1198                         }
1199                         free(recp);
1200                 }
1201         }
1202
1203         /* If still not found, try it as an address qualified with the
1204          * primary FQDN of this Citadel node.
1205          */
1206         if (found_user != 0) {
1207                 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1208                 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1209                 recp = validate_recipients(buf);
1210                 if (recp != NULL) {
1211                         if (recp->num_local == 1) {
1212                                 found_user = getuser(&usbuf, recp->recp_local);
1213                         }
1214                         free(recp);
1215                 }
1216         }
1217
1218         /* Still not found?  Try qualifying it with every domain we
1219          * might have addresses in.
1220          */
1221         if (found_user != 0) {
1222                 config_lines = num_tokens(inetcfg, '\n');
1223                 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1224                         extract_token(buf, inetcfg, i, '\n', sizeof buf);
1225                         extract_token(host, buf, 0, '|', sizeof host);
1226                         extract_token(type, buf, 1, '|', sizeof type);
1227
1228                         if ( (!strcasecmp(type, "localhost"))
1229                            || (!strcasecmp(type, "directory")) ) {
1230                                 snprintf(buf, sizeof buf, "%s@%s", who, host);
1231                                 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1232                                 recp = validate_recipients(buf);
1233                                 if (recp != NULL) {
1234                                         if (recp->num_local == 1) {
1235                                                 found_user = getuser(&usbuf, recp->recp_local);
1236                                         }
1237                                         free(recp);
1238                                 }
1239                         }
1240                 }
1241         }
1242
1243         if (found_user != 0) {
1244                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1245                 return;
1246         }
1247
1248         MailboxName(calendar_room_name, sizeof calendar_room_name,
1249                 &usbuf, USERCALENDARROOM);
1250
1251         strcpy(hold_rm, CC->room.QRname);       /* save current room */
1252
1253         if (getroom(&CC->room, calendar_room_name) != 0) {
1254                 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1255                 getroom(&CC->room, hold_rm);
1256                 return;
1257         }
1258
1259         /* Create a VFREEBUSY subcomponent */
1260         lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1261         fb = icalcomponent_new_vfreebusy();
1262         if (fb == NULL) {
1263                 cprintf("%d Internal error: cannot allocate memory.\n",
1264                         ERROR + INTERNAL_ERROR);
1265                 icalcomponent_free(encaps);
1266                 getroom(&CC->room, hold_rm);
1267                 return;
1268         }
1269
1270         /* Set the method to PUBLISH */
1271         icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1272
1273         /* Set the DTSTAMP to right now. */
1274         icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1275
1276         /* Add the user's email address as ORGANIZER */
1277         sprintf(buf, "MAILTO:%s", who);
1278         if (strchr(buf, '@') == NULL) {
1279                 strcat(buf, "@");
1280                 strcat(buf, config.c_fqdn);
1281         }
1282         for (i=0; i<strlen(buf); ++i) {
1283                 if (buf[i]==' ') buf[i] = '_';
1284         }
1285         icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1286
1287         /* Add busy time from events */
1288         lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1289         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
1290                 NULL, ical_freebusy_backend, (void *)fb
1291         );
1292
1293         /* If values for DTSTART and DTEND are still not present, set them
1294          * to yesterday and tomorrow as default values.
1295          */
1296         if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1297                 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1298         }
1299         if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1300                 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1301         }
1302
1303         /* Put the freebusy component into the calendar component */
1304         lprintf(CTDL_DEBUG, "Encapsulating\n");
1305         encaps = ical_encapsulate_subcomponent(fb);
1306         if (encaps == NULL) {
1307                 icalcomponent_free(fb);
1308                 cprintf("%d Internal error: cannot allocate memory.\n",
1309                         ERROR + INTERNAL_ERROR);
1310                 getroom(&CC->room, hold_rm);
1311                 return;
1312         }
1313
1314         /* Set the method to PUBLISH */
1315         lprintf(CTDL_DEBUG, "Setting method\n");
1316         icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1317
1318         /* Serialize it */
1319         lprintf(CTDL_DEBUG, "Serializing\n");
1320         serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1321         icalcomponent_free(encaps);     /* Don't need this anymore. */
1322
1323         cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1324         if (serialized_request != NULL) {
1325                 client_write(serialized_request, strlen(serialized_request));
1326                 free(serialized_request);
1327         }
1328         cprintf("\n000\n");
1329
1330         /* Go back to the room from which we came... */
1331         getroom(&CC->room, hold_rm);
1332 }
1333
1334
1335
1336
1337 /*
1338  * All Citadel calendar commands from the client come through here.
1339  */
1340 void cmd_ical(char *argbuf)
1341 {
1342         char subcmd[64];
1343         long msgnum;
1344         char partnum[256];
1345         char action[256];
1346         char who[256];
1347
1348         extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1349
1350         /* Allow "test" and "freebusy" subcommands without logging in. */
1351
1352         if (!strcasecmp(subcmd, "test")) {
1353                 cprintf("%d This server supports calendaring\n", CIT_OK);
1354                 return;
1355         }
1356
1357         if (!strcasecmp(subcmd, "freebusy")) {
1358                 extract_token(who, argbuf, 1, '|', sizeof who);
1359                 ical_freebusy(who);
1360                 return;
1361         }
1362
1363         if (!strcasecmp(subcmd, "sgi")) {
1364                 CIT_ICAL->server_generated_invitations =
1365                         (extract_int(argbuf, 1) ? 1 : 0) ;
1366                 cprintf("%d %d\n",
1367                         CIT_OK, CIT_ICAL->server_generated_invitations);
1368                 return;
1369         }
1370
1371         if (CtdlAccessCheck(ac_logged_in)) return;
1372
1373         if (!strcasecmp(subcmd, "respond")) {
1374                 msgnum = extract_long(argbuf, 1);
1375                 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1376                 extract_token(action, argbuf, 3, '|', sizeof action);
1377                 ical_respond(msgnum, partnum, action);
1378                 return;
1379         }
1380
1381         if (!strcasecmp(subcmd, "handle_rsvp")) {
1382                 msgnum = extract_long(argbuf, 1);
1383                 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1384                 extract_token(action, argbuf, 3, '|', sizeof action);
1385                 ical_handle_rsvp(msgnum, partnum, action);
1386                 return;
1387         }
1388
1389         if (!strcasecmp(subcmd, "conflicts")) {
1390                 msgnum = extract_long(argbuf, 1);
1391                 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1392                 ical_conflicts(msgnum, partnum);
1393                 return;
1394         }
1395
1396         cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1397 }
1398
1399
1400
1401 /*
1402  * We don't know if the calendar room exists so we just create it at login
1403  */
1404 void ical_create_room(void)
1405 {
1406         struct ctdlroom qr;
1407         struct visit vbuf;
1408
1409         /* Create the calendar room if it doesn't already exist */
1410         create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1411
1412         /* Set expiration policy to manual; otherwise objects will be lost! */
1413         if (lgetroom(&qr, USERCALENDARROOM)) {
1414                 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1415                 return;
1416         }
1417         qr.QRep.expire_mode = EXPIRE_MANUAL;
1418         qr.QRdefaultview = VIEW_CALENDAR;       /* 3 = calendar view */
1419         lputroom(&qr);
1420
1421         /* Set the view to a calendar view */
1422         CtdlGetRelationship(&vbuf, &CC->user, &qr);
1423         vbuf.v_view = VIEW_CALENDAR;
1424         CtdlSetRelationship(&vbuf, &CC->user, &qr);
1425
1426         /* Create the tasks list room if it doesn't already exist */
1427         create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1428
1429         /* Set expiration policy to manual; otherwise objects will be lost! */
1430         if (lgetroom(&qr, USERTASKSROOM)) {
1431                 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1432                 return;
1433         }
1434         qr.QRep.expire_mode = EXPIRE_MANUAL;
1435         qr.QRdefaultview = VIEW_TASKS;
1436         lputroom(&qr);
1437
1438         /* Set the view to a task list view */
1439         CtdlGetRelationship(&vbuf, &CC->user, &qr);
1440         vbuf.v_view = VIEW_TASKS;
1441         CtdlSetRelationship(&vbuf, &CC->user, &qr);
1442
1443         /* Create the notes room if it doesn't already exist */
1444         create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1445
1446         /* Set expiration policy to manual; otherwise objects will be lost! */
1447         if (lgetroom(&qr, USERNOTESROOM)) {
1448                 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1449                 return;
1450         }
1451         qr.QRep.expire_mode = EXPIRE_MANUAL;
1452         qr.QRdefaultview = VIEW_NOTES;
1453         lputroom(&qr);
1454
1455         /* Set the view to a notes view */
1456         CtdlGetRelationship(&vbuf, &CC->user, &qr);
1457         vbuf.v_view = VIEW_NOTES;
1458         CtdlSetRelationship(&vbuf, &CC->user, &qr);
1459
1460         return;
1461 }
1462
1463
1464 /*
1465  * ical_send_out_invitations() is called by ical_saving_vevent() when it
1466  * finds a VEVENT.
1467  */
1468 void ical_send_out_invitations(icalcomponent *cal) {
1469         icalcomponent *the_request = NULL;
1470         char *serialized_request = NULL;
1471         icalcomponent *encaps = NULL;
1472         char *request_message_text = NULL;
1473         struct CtdlMessage *msg = NULL;
1474         struct recptypes *valid = NULL;
1475         char attendees_string[SIZ];
1476         int num_attendees = 0;
1477         char this_attendee[256];
1478         icalproperty *attendee = NULL;
1479         char summary_string[SIZ];
1480         icalproperty *summary = NULL;
1481
1482         if (cal == NULL) {
1483                 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1484                 return;
1485         }
1486
1487
1488         /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1489         if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1490                 ical_send_out_invitations(
1491                         icalcomponent_get_first_component(
1492                                 cal, ICAL_VEVENT_COMPONENT
1493                         )
1494                 );
1495                 return;
1496         }
1497
1498         /* Clone the event */
1499         the_request = icalcomponent_new_clone(cal);
1500         if (the_request == NULL) {
1501                 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1502                 return;
1503         }
1504
1505         /* Extract the summary string -- we'll use it as the
1506          * message subject for the request
1507          */
1508         strcpy(summary_string, "Meeting request");
1509         summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1510         if (summary != NULL) {
1511                 if (icalproperty_get_summary(summary)) {
1512                         strcpy(summary_string,
1513                                 icalproperty_get_summary(summary) );
1514                 }
1515         }
1516
1517         /* Determine who the recipients of this message are (the attendees) */
1518         strcpy(attendees_string, "");
1519         for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1520                 if (icalproperty_get_attendee(attendee)) {
1521                         safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1522                         if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1523                                 strcpy(this_attendee, &this_attendee[7]);
1524
1525                                 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) {   /* don't send an invitation to myself! */
1526                                         snprintf(&attendees_string[strlen(attendees_string)],
1527                                                 sizeof(attendees_string) - strlen(attendees_string),
1528                                                 "%s, ",
1529                                                 this_attendee
1530                                         );
1531                                         ++num_attendees;
1532                                 }
1533                         }
1534                 }
1535         }
1536
1537         lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1538
1539         /* If there are no attendees, there are no invitations to send, so...
1540          * don't bother putting one together!  Punch out, Maverick!
1541          */
1542         if (num_attendees == 0) {
1543                 icalcomponent_free(the_request);
1544                 return;
1545         }
1546
1547         /* Encapsulate the VEVENT component into a complete VCALENDAR */
1548         encaps = icalcomponent_new_vcalendar();
1549         if (encaps == NULL) {
1550                 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1551                         __FILE__, __LINE__);
1552                 icalcomponent_free(the_request);
1553                 return;
1554         }
1555
1556         /* Set the Product ID */
1557         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1558
1559         /* Set the Version Number */
1560         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1561
1562         /* Set the method to REQUEST */
1563         icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1564
1565         /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1566         ical_dezonify(the_request);
1567
1568         /* Here we go: put the VEVENT into the VCALENDAR.  We now no longer
1569          * are responsible for "the_request"'s memory -- it will be freed
1570          * when we free "encaps".
1571          */
1572         icalcomponent_add_component(encaps, the_request);
1573
1574         /* Serialize it */
1575         serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1576         icalcomponent_free(encaps);     /* Don't need this anymore. */
1577         if (serialized_request == NULL) return;
1578
1579         request_message_text = malloc(strlen(serialized_request) + SIZ);
1580         if (request_message_text != NULL) {
1581                 sprintf(request_message_text,
1582                         "Content-type: text/calendar\r\n\r\n%s\r\n",
1583                         serialized_request
1584                 );
1585
1586                 msg = CtdlMakeMessage(&CC->user,
1587                         "",                     /* No single recipient here */
1588                         "",                     /* No single recipient here */
1589                         CC->room.QRname, 0, FMT_RFC822,
1590                         "",
1591                         summary_string,         /* Use summary for subject */
1592                         request_message_text);
1593         
1594                 if (msg != NULL) {
1595                         valid = validate_recipients(attendees_string);
1596                         CtdlSubmitMsg(msg, valid, "");
1597                         CtdlFreeMessage(msg);
1598                 }
1599         }
1600         free(serialized_request);
1601 }
1602
1603
1604 /*
1605  * When a calendar object is being saved, determine whether it's a VEVENT
1606  * and the user saving it is the organizer.  If so, send out invitations
1607  * to any listed attendees.
1608  *
1609  */
1610 void ical_saving_vevent(icalcomponent *cal) {
1611         icalcomponent *c;
1612         icalproperty *organizer = NULL;
1613         char organizer_string[SIZ];
1614
1615         /* Don't send out invitations unless the client wants us to. */
1616         if (CIT_ICAL->server_generated_invitations == 0) {
1617                 return;
1618         }
1619
1620         /* Don't send out invitations if we've been asked not to. */
1621         if (CIT_ICAL->avoid_sending_invitations > 0) {
1622                 return;
1623         }
1624
1625         strcpy(organizer_string, "");
1626         /*
1627          * The VEVENT subcomponent is the one we're interested in.
1628          * Send out invitations if, and only if, this user is the Organizer.
1629          */
1630         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1631                 organizer = icalcomponent_get_first_property(cal,
1632                                                 ICAL_ORGANIZER_PROPERTY);
1633                 if (organizer != NULL) {
1634                         if (icalproperty_get_organizer(organizer)) {
1635                                 strcpy(organizer_string,
1636                                         icalproperty_get_organizer(organizer));
1637                         }
1638                 }
1639                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1640                         strcpy(organizer_string, &organizer_string[7]);
1641                         striplt(organizer_string);
1642                         /*
1643                          * If the user saving the event is listed as the
1644                          * organizer, then send out invitations.
1645                          */
1646                         if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1647                                 ical_send_out_invitations(cal);
1648                         }
1649                 }
1650         }
1651
1652         /* If the component has subcomponents, recurse through them. */
1653         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1654             (c != NULL);
1655             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1656                 /* Recursively process subcomponent */
1657                 ical_saving_vevent(c);
1658         }
1659
1660 }
1661
1662
1663
1664 /*
1665  * Back end for ical_obj_beforesave()
1666  * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1667  * the summary of the event (becomes message subject),
1668  * and the start time (becomes message date/time).
1669  */
1670 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1671                 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1672                 char *encoding, void *cbuserdata)
1673 {
1674         icalcomponent *cal, *nested_event, *nested_todo;
1675         icalproperty *p;
1676         struct icalmessagemod *imm;
1677         char new_uid[SIZ];
1678
1679         imm = (struct icalmessagemod *)cbuserdata;
1680
1681         /* If this is a text/calendar object, hunt for the UID and drop it in
1682          * the "user data" pointer for the MIME parser.  When
1683          * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1684          * to that string.
1685          */
1686         if (!strcasecmp(cbtype, "text/calendar")) {
1687                 cal = icalcomponent_new_from_string(content);
1688                 if (cal != NULL) {
1689                         if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1690                                 nested_event = icalcomponent_get_first_component(
1691                                         cal, ICAL_VEVENT_COMPONENT
1692                                 );
1693                                 nested_todo = icalcomponent_get_first_component(
1694                                         cal, ICAL_VTODO_COMPONENT
1695                                 );
1696                                 if (nested_event != NULL) {
1697                                         cal = nested_event;
1698                                 }
1699                                 else if (nested_todo != NULL) {
1700                                         cal = nested_todo;
1701                                 }
1702                         }
1703                 }
1704                 if (cal != NULL) {
1705                         p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1706                         if (p == NULL) {
1707                                 /* If there's no uid we must generate one */
1708                                 generate_uuid(new_uid);
1709                                 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1710                                 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1711                         }
1712                         if (p != NULL) {
1713                                 strcpy(imm->uid, icalproperty_get_comment(p));
1714                                 /* strcpy(imm->subject, icalproperty_get_comment(p)); old aethera hack */
1715                         }
1716                         p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1717                         if (p != NULL) {
1718                                 strcpy(imm->subject, icalproperty_get_comment(p));
1719                         }
1720                         p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1721                         if (p != NULL) {
1722                                 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1723                         }
1724                         icalcomponent_free(cal);
1725                 }
1726         }
1727 }
1728
1729
1730
1731
1732
1733 /*
1734  * See if we need to prevent the object from being saved (we don't allow
1735  * MIME types other than text/calendar in "calendar" or "tasks"  rooms).  Also,
1736  * when saving an event to the calendar, set the message's Citadel exclusive
1737  * message ID to the UID of the object.  This causes our replication checker to
1738  * automatically delete any existing instances of the same object.  (Isn't
1739  * that cool?)
1740  *
1741  * We also set the message's Subject to the event summary, and the Date/time to
1742  * the event start time.
1743  */
1744 int ical_obj_beforesave(struct CtdlMessage *msg)
1745 {
1746         char *p;
1747         int a;
1748         struct icalmessagemod imm;
1749
1750         /* First determine if this is a calendar or tasks room */
1751         if ( (CC->curr_view != VIEW_CALENDAR)
1752            &&(CC->curr_view != VIEW_TASKS) ) {
1753                 return(0);              /* Not a vCalendar-centric room */
1754         }
1755
1756         /* It must be an RFC822 message! */
1757         if (msg->cm_format_type != 4) {
1758                 return 1;       /* You tried to save a non-RFC822 message! */
1759         }
1760         
1761         /* Find the Content-Type: header */
1762         p = msg->cm_fields['M'];
1763         a = strlen(p);
1764         while (--a > 0) {
1765                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
1766                         if (!strncasecmp(p + 14, "text/calendar", 13)) {
1767                                 memset(&imm, 0, sizeof(struct icalmessagemod));
1768                                 mime_parser(msg->cm_fields['M'],
1769                                         NULL,
1770                                         *ical_ctdl_set_exclusive_msgid,
1771                                         NULL, NULL,
1772                                         (void *)&imm,
1773                                         0
1774                                 );
1775                                 if (strlen(imm.uid) > 0) {
1776                                         if (msg->cm_fields['E'] != NULL) {
1777                                                 free(msg->cm_fields['E']);
1778                                         }
1779                                         msg->cm_fields['E'] = strdup(imm.uid);
1780                                 }
1781                                 if (strlen(imm.subject) > 0) {
1782                                         if (msg->cm_fields['U'] != NULL) {
1783                                                 free(msg->cm_fields['U']);
1784                                         }
1785                                         msg->cm_fields['U'] = strdup(imm.subject);
1786                                 }
1787                                 if (imm.dtstart > 0) {
1788                                         if (msg->cm_fields['T'] != NULL) {
1789                                                 free(msg->cm_fields['T']);
1790                                         }
1791                                         msg->cm_fields['T'] = strdup("000000000000000000");
1792                                         sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1793                                 }
1794                                 return 0;
1795                         }
1796                         else {
1797                                 return 1;
1798                         }
1799                 }
1800                 p++;
1801         }
1802         
1803         /* Oops!  No Content-Type in this message!  How'd that happen? */
1804         lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
1805         return 1;
1806 }
1807
1808
1809 /*
1810  * Things we need to do after saving a calendar event.
1811  */
1812 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1813                 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1814                 char *encoding, void *cbuserdata)
1815 {
1816         icalcomponent *cal;
1817
1818         /* If this is a text/calendar object, hunt for the UID and drop it in
1819          * the "user data" pointer for the MIME parser.  When
1820          * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1821          * to that string.
1822          */
1823         if (!strcasecmp(cbtype, "text/calendar")) {
1824                 cal = icalcomponent_new_from_string(content);
1825                 if (cal != NULL) {
1826                         ical_saving_vevent(cal);
1827                         icalcomponent_free(cal);
1828                 }
1829         }
1830 }
1831
1832
1833 /* 
1834  * Things we need to do after saving a calendar event.
1835  */
1836 int ical_obj_aftersave(struct CtdlMessage *msg)
1837 {
1838         char roomname[ROOMNAMELEN];
1839         char *p;
1840         int a;
1841
1842         /*
1843          * If this isn't the Calendar> room, no further action is necessary.
1844          */
1845
1846         /* First determine if this is our room */
1847         MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
1848         if (strcasecmp(roomname, CC->room.QRname)) {
1849                 return 0;       /* It's not the Calendar room. */
1850         }
1851
1852         /* Then determine content-type of the message */
1853         
1854         /* It must be an RFC822 message! */
1855         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1856         if (msg->cm_format_type != 4) return(1);
1857         
1858         /* Find the Content-Type: header */
1859         p = msg->cm_fields['M'];
1860         a = strlen(p);
1861         while (--a > 0) {
1862                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
1863                         if (!strncasecmp(p + 14, "text/calendar", 13)) {
1864                                 mime_parser(msg->cm_fields['M'],
1865                                         NULL,
1866                                         *ical_obj_aftersave_backend,
1867                                         NULL, NULL,
1868                                         NULL,
1869                                         0
1870                                 );
1871                                 return 0;
1872                         }
1873                         else {
1874                                 return 1;
1875                         }
1876                 }
1877                 p++;
1878         }
1879         
1880         /* Oops!  No Content-Type in this message!  How'd that happen? */
1881         lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
1882         return 1;
1883 }
1884
1885
1886 void ical_session_startup(void) {
1887         CIT_ICAL = malloc(sizeof(struct cit_ical));
1888         memset(CIT_ICAL, 0, sizeof(struct cit_ical));
1889 }
1890
1891 void ical_session_shutdown(void) {
1892         free(CIT_ICAL);
1893 }
1894
1895
1896 /*
1897  * Back end for ical_fixed_output()
1898  */
1899 void ical_fixed_output_backend(icalcomponent *cal,
1900                         int recursion_level
1901 ) {
1902         icalcomponent *c;
1903         icalproperty *method = NULL;
1904         icalproperty_method the_method = ICAL_METHOD_NONE;
1905         icalproperty *p;
1906         struct icaltimetype t;
1907         time_t tt;
1908         char buf[256];
1909
1910         /* Look for a method */
1911         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
1912
1913         /* See what we need to do with this */
1914         if (method != NULL) {
1915                 the_method = icalproperty_get_method(method);
1916                 switch(the_method) {
1917                     case ICAL_METHOD_REQUEST:
1918                         cprintf("Meeting invitation\n");
1919                         break;
1920                     case ICAL_METHOD_REPLY:
1921                         cprintf("Attendee's reply to your invitation\n");
1922                         break;
1923                     case ICAL_METHOD_PUBLISH:
1924                         cprintf("Published event\n");
1925                         break;
1926                     default:
1927                         cprintf("This is an unknown type of calendar item.\n");
1928                         break;
1929                 }
1930         }
1931
1932         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
1933         if (p != NULL) {
1934                 cprintf("Summary: %s\n", (const char *)icalproperty_get_comment(p));
1935         }
1936
1937         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
1938         if (p != NULL) {
1939                 cprintf("Location: %s\n", (const char *)icalproperty_get_comment(p));
1940         }
1941
1942         /*
1943          * Only show start/end times if we're actually looking at the VEVENT
1944          * component.  Otherwise it shows bogus dates for things like timezone.
1945          */
1946         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1947
1948                 p = icalcomponent_get_first_property(cal,
1949                                                 ICAL_DTSTART_PROPERTY);
1950                 if (p != NULL) {
1951                         t = icalproperty_get_dtstart(p);
1952
1953                         if (t.is_date) {
1954                                 cprintf("Date: %s %d, %d\n",
1955                                         ascmonths[t.month - 1],
1956                                         t.day, t.year
1957                                 );
1958                         }
1959                         else {
1960                                 tt = icaltime_as_timet(t);
1961                                 fmt_date(buf, sizeof buf, tt, 0);
1962                                 cprintf("Starting date/time: %s\n", buf);
1963                         }
1964                 }
1965         
1966                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1967                 if (p != NULL) {
1968                         t = icalproperty_get_dtend(p);
1969                         tt = icaltime_as_timet(t);
1970                         fmt_date(buf, sizeof buf, tt, 0);
1971                         cprintf("Ending date/time: %s\n", buf);
1972                 }
1973
1974         }
1975
1976         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
1977         if (p != NULL) {
1978                 cprintf("Description: %s\n", (const char *)icalproperty_get_comment(p));
1979         }
1980
1981         /* If the component has attendees, iterate through them. */
1982         for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
1983                 cprintf("Attendee: ");
1984                 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
1985                 if (!strncasecmp(buf, "MAILTO:", 7)) {
1986
1987                         /* screen name or email address */
1988                         strcpy(buf, &buf[7]);
1989                         striplt(buf);
1990                         cprintf("%s ", buf);
1991                 }
1992                 cprintf("\n");
1993         }
1994
1995         /* If the component has subcomponents, recurse through them. */
1996         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1997             (c != 0);
1998             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1999                 /* Recursively process subcomponent */
2000                 ical_fixed_output_backend(c, recursion_level+1);
2001         }
2002 }
2003
2004
2005
2006 /*
2007  * Function to output a calendar item  as plain text.  Nobody uses MSG0
2008  * anymore, so really this is just so we expose the vCard data to the full
2009  * text indexer.
2010  */
2011 void ical_fixed_output(char *ptr, int len) {
2012         icalcomponent *cal;
2013         char *stringy_cal;
2014
2015         stringy_cal = malloc(len + 1);
2016         safestrncpy(stringy_cal, ptr, len + 1);
2017         cal = icalcomponent_new_from_string(stringy_cal);
2018         free(stringy_cal);
2019
2020         if (cal == NULL) {
2021                 cprintf("There was an error parsing this calendar item.\n");
2022                 return;
2023         }
2024
2025         ical_dezonify(cal);
2026         ical_fixed_output_backend(cal, 0);
2027
2028         /* Free the memory we obtained from libical's constructor */
2029         icalcomponent_free(cal);
2030 }
2031
2032
2033 #endif  /* CITADEL_WITH_CALENDAR_SERVICE */
2034
2035 /*
2036  * Register this module with the Citadel server.
2037  */
2038 char *serv_calendar_init(void)
2039 {
2040 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2041         CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2042         CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2043         CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2044         CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2045         CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2046         CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2047         CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2048 #endif
2049         return "$Id$";
2050 }