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.
10 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
14 #include <sys/types.h>
21 #include "serv_calendar.h"
24 #include "citserver.h"
25 #include "sysdep_decls.h"
28 #include "dynloader.h"
33 #include "mime_parser.h"
39 struct ical_respond_data {
40 char desired_partnum[SIZ];
44 /* Session-local data for calendaring. */
48 * Write a calendar object into the specified user's calendar room.
50 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
55 strcpy(temp, tmpnam(NULL));
56 ser = icalcomponent_as_ical_string(cal);
57 if (ser == NULL) return;
59 /* Make a temp file out of it */
60 fp = fopen(temp, "w");
61 if (fp == NULL) return;
62 fwrite(ser, strlen(ser), 1, fp);
65 /* This handy API function does all the work for us.
67 CtdlWriteObject(USERCALENDARROOM, /* which room */
68 "text/calendar", /* MIME type */
72 0, /* don't delete others of this type */
80 * Add a calendar object to the user's calendar
82 void ical_add(icalcomponent *cal, int recursion_level) {
86 * The VEVENT subcomponent is the one we're interested in saving.
88 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
90 ical_write_to_cal(&CC->usersupp, cal);
94 /* If the component has subcomponents, recurse through them. */
95 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
97 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
98 /* Recursively process subcomponent */
99 ical_add(c, recursion_level+1);
107 * Send a reply to a meeting invitation.
109 * 'request' is the invitation to reply to.
110 * 'action' is the string "accept" or "decline".
112 * (Sorry about this being more than 80 columns ... there was just
113 * no easy way to break it down sensibly.)
115 void ical_send_a_reply(icalcomponent *request, char *action) {
116 icalcomponent *the_reply = NULL;
117 icalcomponent *vevent = NULL;
118 icalproperty *attendee = NULL;
119 char attendee_string[SIZ];
120 icalproperty *organizer = NULL;
121 char organizer_string[SIZ];
122 icalproperty *summary = NULL;
123 char summary_string[SIZ];
124 icalproperty *me_attend = NULL;
125 struct recptypes *recp = NULL;
126 icalparameter *partstat = NULL;
127 char *serialized_reply = NULL;
128 char *reply_message_text = NULL;
129 struct CtdlMessage *msg = NULL;
130 struct recptypes *valid = NULL;
132 strcpy(organizer_string, "");
133 strcpy(summary_string, "Calendar item");
135 if (request == NULL) {
136 lprintf(3, "ERROR: trying to reply to NULL event?\n");
140 the_reply = icalcomponent_new_clone(request);
141 if (the_reply == NULL) {
142 lprintf(3, "ERROR: cannot clone request\n");
146 /* Change the method from REQUEST to REPLY */
147 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
149 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
150 if (vevent != NULL) {
151 /* Hunt for attendees, removing ones that aren't us.
152 * (Actually, remove them all, cloning our own one so we can
153 * re-insert it later)
155 while (attendee = icalcomponent_get_first_property(vevent,
156 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
158 if (icalproperty_get_attendee(attendee)) {
159 strcpy(attendee_string,
160 icalproperty_get_attendee(attendee) );
161 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
162 strcpy(attendee_string, &attendee_string[7]);
163 striplt(attendee_string);
164 recp = validate_recipients(attendee_string);
166 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
167 if (me_attend) icalproperty_free(me_attend);
168 me_attend = icalproperty_new_clone(attendee);
175 icalcomponent_remove_property(vevent, attendee);
176 icalproperty_free(attendee);
179 /* We found our own address in the attendee list. */
181 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
182 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
184 if (!strcasecmp(action, "accept")) {
185 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
187 else if (!strcasecmp(action, "decline")) {
188 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
190 else if (!strcasecmp(action, "tentative")) {
191 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
194 if (partstat) icalproperty_add_parameter(me_attend, partstat);
196 /* Now insert it back into the vevent. */
197 icalcomponent_add_property(vevent, me_attend);
200 /* Figure out who to send this thing to */
201 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
202 if (organizer != NULL) {
203 if (icalproperty_get_organizer(organizer)) {
204 strcpy(organizer_string,
205 icalproperty_get_organizer(organizer) );
208 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
209 strcpy(organizer_string, &organizer_string[7]);
210 striplt(organizer_string);
212 strcpy(organizer_string, "");
215 /* Extract the summary string -- we'll use it as the
216 * message subject for the reply
218 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
219 if (summary != NULL) {
220 if (icalproperty_get_summary(summary)) {
221 strcpy(summary_string,
222 icalproperty_get_summary(summary) );
228 /* Now generate the reply message and send it out. */
229 serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
230 icalcomponent_free(the_reply); /* don't need this anymore */
231 if (serialized_reply == NULL) return;
233 reply_message_text = mallok(strlen(serialized_reply) + SIZ);
234 if (reply_message_text != NULL) {
235 sprintf(reply_message_text,
236 "Content-type: text/calendar\r\n\r\n%s\r\n",
240 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
241 CC->quickroom.QRname, 0, FMT_RFC822,
243 summary_string, /* Use summary for subject */
247 valid = validate_recipients(organizer_string);
248 CtdlSubmitMsg(msg, valid, "");
249 CtdlFreeMessage(msg);
252 phree(serialized_reply);
258 * Callback function for mime parser that hunts for calendar content types
259 * and turns them into calendar objects
261 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
262 void *content, char *cbtype, size_t length, char *encoding,
265 struct ical_respond_data *ird = NULL;
267 ird = (struct ical_respond_data *) cbuserdata;
268 if (ird->cal != NULL) {
269 icalcomponent_free(ird->cal);
272 if (strcasecmp(partnum, ird->desired_partnum)) return;
273 ird->cal = icalcomponent_new_from_string(content);
278 * Respond to a meeting request.
280 void ical_respond(long msgnum, char *partnum, char *action) {
281 struct CtdlMessage *msg;
282 struct ical_respond_data ird;
285 (strcasecmp(action, "accept"))
286 && (strcasecmp(action, "decline"))
288 cprintf("%d Action must be 'accept' or 'decline'\n",
289 ERROR + ILLEGAL_VALUE
294 msg = CtdlFetchMessage(msgnum);
296 cprintf("%d Message %ld not found.\n",
303 memset(&ird, 0, sizeof ird);
304 strcpy(ird.desired_partnum, partnum);
305 mime_parser(msg->cm_fields['M'],
307 *ical_locate_part, /* callback function */
309 (void *) &ird, /* user data */
313 /* We're done with the incoming message, because we now have a
314 * calendar object in memory.
316 CtdlFreeMessage(msg);
319 * Here is the real meat of this function. Handle the event.
321 if (ird.cal != NULL) {
322 /* Save this in the user's calendar if necessary */
323 if (!strcasecmp(action, "accept")) {
324 ical_add(ird.cal, 0);
327 /* Send a reply if necessary */
328 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
329 ical_send_a_reply(ird.cal, action);
332 /* Now that we've processed this message, we don't need it
333 * anymore. So delete it.
335 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
337 /* Free the memory we allocated and return a response. */
338 icalcomponent_free(ird.cal);
340 cprintf("%d ok\n", CIT_OK);
344 cprintf("%d No calendar object found\n", ERROR);
348 /* should never get here */
353 * Figure out the UID of the calendar event being referred to in a
354 * REPLY object. This function is recursive.
356 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
357 icalcomponent *subcomponent;
360 /* If this object is a REPLY, then extract the UID. */
361 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
362 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
364 strcpy(uidbuf, icalproperty_get_comment(p));
368 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
369 * UID of the reply; we want the UID of the invitation being replied to.
371 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
372 subcomponent != NULL;
373 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
374 ical_learn_uid_of_reply(uidbuf, subcomponent);
380 * ical_update_my_calendar_with_reply() refers to this callback function; when we
381 * locate the message containing the calendar event we're replying to, this function
382 * gets called. It basically just sticks the message number in a supplied buffer.
384 void ical_hunt_for_event_to_update(long msgnum, void *data) {
387 msgnumptr = (long *) data;
392 struct original_event_container {
397 * Callback function for mime parser that hunts for calendar content types
398 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
399 * to fetch the object being updated)
401 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
402 void *content, char *cbtype, size_t length, char *encoding,
405 struct original_event_container *oec = NULL;
407 if (strcasecmp(cbtype, "text/calendar")) {
410 oec = (struct original_event_container *) cbuserdata;
411 if (oec->c != NULL) {
412 icalcomponent_free(oec->c);
414 oec->c = icalcomponent_new_from_string(content);
419 * Merge updated attendee information from a REPLY into an existing event.
421 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
423 icalproperty *e_attendee, *r_attendee;
425 /* First things first. If we're not looking at a VEVENT component,
426 * recurse through subcomponents until we find one.
428 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
429 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
431 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
432 ical_merge_attendee_reply(c, reply);
437 /* Now do the same thing with the reply.
439 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
440 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
442 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
443 ical_merge_attendee_reply(event, c);
448 /* Clone the reply, because we're going to rip its guts out. */
449 reply = icalcomponent_new_clone(reply);
451 /* At this point we're looking at the correct subcomponents.
452 * Iterate through the attendees looking for a match.
455 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
457 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
459 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
461 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
463 /* Check to see if these two attendees match...
466 icalproperty_get_attendee(e_attendee),
467 icalproperty_get_attendee(r_attendee)
469 /* ...and if they do, remove the attendee from the event
470 * and replace it with the attendee from the reply. (The
471 * reply's copy will have the same address, but an updated
475 icalcomponent_remove_property(event, e_attendee);
477 icalproperty_free(e_attendee);
479 icalcomponent_remove_property(reply, r_attendee);
481 icalcomponent_add_property(event, r_attendee);
484 /* Since we diddled both sets of attendees, we have to start
485 * the iteration over again. This will not create an infinite
486 * loop because we removed the attendee from the reply. (That's
487 * why we cloned the reply, and that's what we mean by "ripping
496 /* Free the *clone* of the reply. */
497 icalcomponent_free(reply);
504 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
505 * calendar event. The object has already been deserialized for us; all
506 * we have to do here is hunt for the event in our calendar, merge in the
507 * updated attendee status, and save it again.
509 * This function returns 0 on success, 1 if the event was not found in the
510 * user's calendar, or 2 if an internal error occurred.
512 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
514 char hold_rm[ROOMNAMELEN];
515 long msgnum_being_replaced = 0;
516 struct CtdlMessage *template = NULL;
517 struct CtdlMessage *msg;
518 struct original_event_container oec;
519 icalcomponent *original_event;
520 char *serialized_event = NULL;
521 char roomname[ROOMNAMELEN];
522 char *message_text = NULL;
524 /* Figure out just what event it is we're dealing with */
525 strcpy(uid, "--==<< InVaLiD uId >>==--");
526 ical_learn_uid_of_reply(uid, cal);
527 lprintf(9, "UID of event being replied to is <%s>\n", uid);
529 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
531 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
532 getroom(&CC->quickroom, hold_rm);
533 lprintf(3, "cannot get user calendar room\n");
538 * Pound through the user's calendar looking for a message with
539 * the Citadel EUID set to the value we're looking for. Since
540 * Citadel always sets the message EUID to the vCalendar UID of
541 * the event, this will work.
543 template = (struct CtdlMessage *)
544 mallok(sizeof(struct CtdlMessage));
545 memset(template, 0, sizeof(struct CtdlMessage));
546 template->cm_fields['E'] = strdoop(uid);
547 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
548 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
549 CtdlFreeMessage(template);
550 getroom(&CC->quickroom, hold_rm); /* return to saved room */
552 lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
553 if (msgnum_being_replaced == 0) {
554 return(1); /* no calendar event found */
557 /* Now we know the ID of the message containing the event being updated.
558 * We don't actually have to delete it; that'll get taken care of by the
559 * server when we save another event with the same UID. This just gives
560 * us the ability to load the event into memory so we can diddle the
563 msg = CtdlFetchMessage(msgnum_being_replaced);
565 return(2); /* internal error */
568 mime_parser(msg->cm_fields['M'],
570 *ical_locate_original_event, /* callback function */
572 &oec, /* user data */
575 CtdlFreeMessage(msg);
577 original_event = oec.c;
578 if (original_event == NULL) {
579 lprintf(3, "ERROR: Original_component is NULL.\n");
583 /* Merge the attendee's updated status into the event */
584 ical_merge_attendee_reply(original_event, cal);
587 serialized_event = strdoop(icalcomponent_as_ical_string(original_event));
588 icalcomponent_free(original_event); /* Don't need this anymore. */
589 if (serialized_event == NULL) return(2);
591 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
593 message_text = mallok(strlen(serialized_event) + SIZ);
594 if (message_text != NULL) {
595 sprintf(message_text,
596 "Content-type: text/calendar\r\n\r\n%s\r\n",
600 msg = CtdlMakeMessage(&CC->usersupp,
601 "", /* No recipient */
609 CIT_ICAL->avoid_sending_invitations = 1;
610 CtdlSubmitMsg(msg, NULL, roomname);
611 CtdlFreeMessage(msg);
612 CIT_ICAL->avoid_sending_invitations = 0;
615 phree(serialized_event);
621 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
622 * simply extracts the calendar object from the message, deserializes it, and
623 * passes it up to ical_update_my_calendar_with_reply() for processing.
625 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
626 struct CtdlMessage *msg;
627 struct ical_respond_data ird;
631 (strcasecmp(action, "update"))
632 && (strcasecmp(action, "ignore"))
634 cprintf("%d Action must be 'update' or 'ignore'\n",
635 ERROR + ILLEGAL_VALUE
640 msg = CtdlFetchMessage(msgnum);
642 cprintf("%d Message %ld not found.\n",
649 memset(&ird, 0, sizeof ird);
650 strcpy(ird.desired_partnum, partnum);
651 mime_parser(msg->cm_fields['M'],
653 *ical_locate_part, /* callback function */
655 (void *) &ird, /* user data */
659 /* We're done with the incoming message, because we now have a
660 * calendar object in memory.
662 CtdlFreeMessage(msg);
665 * Here is the real meat of this function. Handle the event.
667 if (ird.cal != NULL) {
668 /* Update the user's calendar if necessary */
669 if (!strcasecmp(action, "update")) {
670 ret = ical_update_my_calendar_with_reply(ird.cal);
672 cprintf("%d Your calendar has been updated with this reply.\n",
676 cprintf("%d This event does not exist in your calendar.\n",
677 ERROR + FILE_NOT_FOUND);
680 cprintf("%d An internal error occurred.\n",
681 ERROR + INTERNAL_ERROR);
685 cprintf("%d This reply has been ignored.\n", CIT_OK);
688 /* Now that we've processed this message, we don't need it
689 * anymore. So delete it. FIXME uncomment this when ready!
691 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
693 /* Free the memory we allocated and return a response. */
694 icalcomponent_free(ird.cal);
699 cprintf("%d No calendar object found\n", ERROR);
703 /* should never get here */
708 * Search for a property in both the top level and in a VEVENT subcomponent
710 icalproperty *ical_ctdl_get_subprop(
712 icalproperty_kind which_prop
717 p = icalcomponent_get_first_property(cal, which_prop);
719 c = icalcomponent_get_first_component(cal,
720 ICAL_VEVENT_COMPONENT);
722 p = icalcomponent_get_first_property(c, which_prop);
730 * Check to see if two events overlap. Returns nonzero if they do.
732 int ical_ctdl_is_overlap(
733 struct icaltimetype t1start,
734 struct icaltimetype t1end,
735 struct icaltimetype t2start,
736 struct icaltimetype t2end
739 if (icaltime_is_null_time(t1start)) return(0);
740 if (icaltime_is_null_time(t2start)) return(0);
742 /* First, check for all-day events */
743 if (t1start.is_date) {
744 if (!icaltime_compare_date_only(t1start, t2start)) {
747 if (!icaltime_is_null_time(t2end)) {
748 if (!icaltime_compare_date_only(t1start, t2end)) {
754 if (t2start.is_date) {
755 if (!icaltime_compare_date_only(t2start, t1start)) {
758 if (!icaltime_is_null_time(t1end)) {
759 if (!icaltime_compare_date_only(t2start, t1end)) {
765 /* Now check for overlaps using date *and* time. */
767 /* First, bail out if either event 1 or event 2 is missing end time. */
768 if (icaltime_is_null_time(t1end)) return(0);
769 if (icaltime_is_null_time(t2end)) return(0);
771 /* If event 1 ends before event 2 starts, we're in the clear. */
772 if (icaltime_compare(t1end, t2start) <= 0) return(0);
774 /* If event 2 ends before event 1 starts, we're also ok. */
775 if (icaltime_compare(t2end, t1start) <= 0) return(0);
777 /* Otherwise, they overlap. */
784 * Backend for ical_hunt_for_conflicts()
786 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
788 struct CtdlMessage *msg;
789 struct ical_respond_data ird;
790 struct icaltimetype t1start, t1end, t2start, t2end;
792 char conflict_event_uid[SIZ];
793 char conflict_event_summary[SIZ];
794 char compare_uid[SIZ];
796 cal = (icalcomponent *)data;
797 strcpy(compare_uid, "");
798 strcpy(conflict_event_uid, "");
799 strcpy(conflict_event_summary, "");
801 msg = CtdlFetchMessage(msgnum);
802 if (msg == NULL) return;
803 memset(&ird, 0, sizeof ird);
804 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
805 mime_parser(msg->cm_fields['M'],
807 *ical_locate_part, /* callback function */
809 (void *) &ird, /* user data */
812 CtdlFreeMessage(msg);
814 if (ird.cal == NULL) return;
816 t1start = icaltime_null_time();
817 t1end = icaltime_null_time();
818 t2start = icaltime_null_time();
819 t1end = icaltime_null_time();
821 /* Now compare cal to ird.cal */
822 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
823 if (p == NULL) return;
824 if (p != NULL) t2start = icalproperty_get_dtstart(p);
826 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
827 if (p != NULL) t2end = icalproperty_get_dtend(p);
829 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
830 if (p == NULL) return;
831 if (p != NULL) t1start = icalproperty_get_dtstart(p);
833 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
834 if (p != NULL) t1end = icalproperty_get_dtend(p);
836 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
838 strcpy(compare_uid, icalproperty_get_comment(p));
841 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
843 strcpy(conflict_event_uid, icalproperty_get_comment(p));
846 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
848 strcpy(conflict_event_summary, icalproperty_get_comment(p));
852 icalcomponent_free(ird.cal);
854 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
855 cprintf("%ld||%s|%s|%d|\n",
858 conflict_event_summary,
859 ( ((strlen(compare_uid)>0)
860 &&(!strcasecmp(compare_uid,
861 conflict_event_uid))) ? 1 : 0
870 * Phase 2 of "hunt for conflicts" operation.
871 * At this point we have a calendar object which represents the VEVENT that
872 * we're considering adding to the calendar. Now hunt through the user's
873 * calendar room, and output zero or more existing VEVENTs which conflict
876 void ical_hunt_for_conflicts(icalcomponent *cal) {
877 char hold_rm[ROOMNAMELEN];
879 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
881 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
882 getroom(&CC->quickroom, hold_rm);
883 cprintf("%d You do not have a calendar.\n", ERROR);
887 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
889 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
891 ical_hunt_for_conflicts_backend,
896 getroom(&CC->quickroom, hold_rm); /* return to saved room */
903 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
905 void ical_conflicts(long msgnum, char *partnum) {
906 struct CtdlMessage *msg;
907 struct ical_respond_data ird;
909 msg = CtdlFetchMessage(msgnum);
911 cprintf("%d Message %ld not found.\n",
918 memset(&ird, 0, sizeof ird);
919 strcpy(ird.desired_partnum, partnum);
920 mime_parser(msg->cm_fields['M'],
922 *ical_locate_part, /* callback function */
924 (void *) &ird, /* user data */
928 CtdlFreeMessage(msg);
930 if (ird.cal != NULL) {
931 ical_hunt_for_conflicts(ird.cal);
932 icalcomponent_free(ird.cal);
936 cprintf("%d No calendar object found\n", ERROR);
940 /* should never get here */
947 * All Citadel calendar commands from the client come through here.
949 void cmd_ical(char *argbuf)
956 if (CtdlAccessCheck(ac_logged_in)) return;
958 extract(subcmd, argbuf, 0);
960 if (!strcmp(subcmd, "test")) {
961 cprintf("%d This server supports calendaring\n", CIT_OK);
965 else if (!strcmp(subcmd, "respond")) {
966 msgnum = extract_long(argbuf, 1);
967 extract(partnum, argbuf, 2);
968 extract(action, argbuf, 3);
969 ical_respond(msgnum, partnum, action);
972 else if (!strcmp(subcmd, "handle_rsvp")) {
973 msgnum = extract_long(argbuf, 1);
974 extract(partnum, argbuf, 2);
975 extract(action, argbuf, 3);
976 ical_handle_rsvp(msgnum, partnum, action);
979 else if (!strcmp(subcmd, "conflicts")) {
980 msgnum = extract_long(argbuf, 1);
981 extract(partnum, argbuf, 2);
982 ical_conflicts(msgnum, partnum);
986 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
990 /* should never get here */
996 * We don't know if the calendar room exists so we just create it at login
998 void ical_create_room(void)
1000 struct quickroom qr;
1003 /* Create the calendar room if it doesn't already exist */
1004 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
1006 /* Set expiration policy to manual; otherwise objects will be lost! */
1007 if (lgetroom(&qr, USERCALENDARROOM)) {
1008 lprintf(3, "Couldn't get the user calendar room!\n");
1011 qr.QRep.expire_mode = EXPIRE_MANUAL;
1014 /* Set the view to a calendar view */
1015 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
1016 vbuf.v_view = 3; /* 3 = calendar */
1017 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
1019 /* Create the tasks list room if it doesn't already exist */
1020 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
1022 /* Set expiration policy to manual; otherwise objects will be lost! */
1023 if (lgetroom(&qr, USERTASKSROOM)) {
1024 lprintf(3, "Couldn't get the user calendar room!\n");
1027 qr.QRep.expire_mode = EXPIRE_MANUAL;
1030 /* Set the view to a task list view */
1031 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
1032 vbuf.v_view = 4; /* 4 = tasks */
1033 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
1040 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1041 * finds a VEVENT. FIXME ... finish implementing.
1043 void ical_send_out_invitations(icalcomponent *cal) {
1044 icalcomponent *the_request = NULL;
1045 char *serialized_request = NULL;
1046 char *request_message_text = NULL;
1047 struct CtdlMessage *msg = NULL;
1048 struct recptypes *valid = NULL;
1049 char attendees_string[SIZ];
1050 char this_attendee[SIZ];
1051 icalproperty *attendee = NULL;
1052 char summary_string[SIZ];
1053 icalproperty *summary = NULL;
1054 icalcomponent *encaps = NULL;
1057 lprintf(3, "ERROR: trying to reply to NULL event?\n");
1061 /* Clone the event */
1062 the_request = icalcomponent_new_clone(cal);
1063 if (the_request == NULL) {
1064 lprintf(3, "ERROR: cannot clone calendar object\n");
1068 /* Extract the summary string -- we'll use it as the
1069 * message subject for the request
1071 strcpy(summary_string, "Meeting request");
1072 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1073 if (summary != NULL) {
1074 if (icalproperty_get_summary(summary)) {
1075 strcpy(summary_string,
1076 icalproperty_get_summary(summary) );
1080 /* Determine who the recipients of this message are (the attendees) */
1081 strcpy(attendees_string, "");
1082 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1083 if (icalproperty_get_attendee(attendee)) {
1084 strcpy(this_attendee, icalproperty_get_attendee(attendee) );
1085 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1086 strcpy(this_attendee, &this_attendee[7]);
1087 snprintf(&attendees_string[strlen(attendees_string)],
1088 sizeof(attendees_string) - strlen(attendees_string),
1096 lprintf(9, "attendees_string: <%s>\n", attendees_string);
1098 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1099 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
1100 if (encaps == NULL) {
1101 lprintf(3, "Error at %s:%d - could not allocate component!\n",
1102 __FILE__, __LINE__);
1103 icalcomponent_free(the_request);
1107 /* Set the Product ID */
1108 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1110 /* Set the Version Number */
1111 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1113 /* Set the method to REQUEST */
1114 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1116 /* FIXME: here we need to insert a VTIMEZONE object. */
1118 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1119 * are responsible for "the_request"'s memory -- it will be freed
1120 * when we free "encaps".
1122 icalcomponent_add_component(encaps, the_request);
1125 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
1126 icalcomponent_free(encaps); /* Don't need this anymore. */
1127 if (serialized_request == NULL) return;
1129 request_message_text = mallok(strlen(serialized_request) + SIZ);
1130 if (request_message_text != NULL) {
1131 sprintf(request_message_text,
1132 "Content-type: text/calendar\r\n\r\n%s\r\n",
1136 msg = CtdlMakeMessage(&CC->usersupp,
1137 "", /* No single recipient here */
1138 CC->quickroom.QRname, 0, FMT_RFC822,
1140 summary_string, /* Use summary for subject */
1141 request_message_text);
1144 valid = validate_recipients(attendees_string);
1145 CtdlSubmitMsg(msg, valid, "");
1146 CtdlFreeMessage(msg);
1149 phree(serialized_request);
1154 * When a calendar object is being saved, determine whether it's a VEVENT
1155 * and the user saving it is the organizer. If so, send out invitations
1156 * to any listed attendees.
1159 void ical_saving_vevent(icalcomponent *cal) {
1161 icalproperty *organizer = NULL;
1162 char organizer_string[SIZ];
1164 /* Don't send out invitations if we've been asked not to. */
1165 if (CIT_ICAL->avoid_sending_invitations) {
1169 strcpy(organizer_string, "");
1171 * The VEVENT subcomponent is the one we're interested in.
1172 * Send out invitations if, and only if, this user is the Organizer.
1174 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1175 organizer = icalcomponent_get_first_property(cal,
1176 ICAL_ORGANIZER_PROPERTY);
1177 if (organizer != NULL) {
1178 if (icalproperty_get_organizer(organizer)) {
1179 strcpy(organizer_string,
1180 icalproperty_get_organizer(organizer));
1183 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1184 strcpy(organizer_string, &organizer_string[7]);
1185 striplt(organizer_string);
1187 * If the user saving the event is listed as the
1188 * organizer, then send out invitations.
1190 if (CtdlIsMe(organizer_string)) {
1191 ical_send_out_invitations(cal);
1196 /* If the component has subcomponents, recurse through them. */
1197 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1199 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1200 /* Recursively process subcomponent */
1201 ical_saving_vevent(c);
1209 * Back end for ical_obj_beforesave()
1210 * This hunts for the UID of the calendar event.
1212 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
1213 char *disp, void *content, char *cbtype, size_t length,
1214 char *encoding, void *cbuserdata)
1219 /* If this is a text/calendar object, hunt for the UID and drop it in
1220 * the "user data" pointer for the MIME parser. When
1221 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1224 if (!strcasecmp(cbtype, "text/calendar")) {
1225 cal = icalcomponent_new_from_string(content);
1227 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1229 strcpy((char *)cbuserdata,
1230 icalproperty_get_comment(p)
1233 icalcomponent_free(cal);
1243 * See if we need to prevent the object from being saved (we don't allow
1244 * MIME types other than text/calendar in the Calendar> room). Also, when
1245 * saving an event to the calendar, set the message's Citadel extended message
1246 * ID to the UID of the object. This causes our replication checker to
1247 * automatically delete any existing instances of the same object. (Isn't
1250 int ical_obj_beforesave(struct CtdlMessage *msg)
1252 char roomname[ROOMNAMELEN];
1258 * Only messages with content-type text/calendar
1259 * may be saved to Calendar>. If the message is bound for
1260 * Calendar> but doesn't have this content-type, throw an error
1261 * so that the message may not be posted.
1264 /* First determine if this is our room */
1265 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1266 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1267 return 0; /* It's not the Calendar room. */
1270 /* Then determine content-type of the message */
1272 /* It must be an RFC822 message! */
1273 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1274 if (msg->cm_format_type != 4)
1275 return 1; /* You tried to save a non-RFC822 message! */
1277 /* Find the Content-Type: header */
1278 p = msg->cm_fields['M'];
1281 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1282 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1284 mime_parser(msg->cm_fields['M'],
1286 *ical_ctdl_set_extended_msgid,
1291 if (strlen(eidbuf) > 0) {
1292 if (msg->cm_fields['E'] != NULL) {
1293 phree(msg->cm_fields['E']);
1295 msg->cm_fields['E'] = strdoop(eidbuf);
1306 /* Oops! No Content-Type in this message! How'd that happen? */
1307 lprintf(7, "RFC822 message with no Content-Type header!\n");
1313 * Things we need to do after saving a calendar event.
1315 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1316 char *disp, void *content, char *cbtype, size_t length,
1317 char *encoding, void *cbuserdata)
1321 /* If this is a text/calendar object, hunt for the UID and drop it in
1322 * the "user data" pointer for the MIME parser. When
1323 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1326 if (!strcasecmp(cbtype, "text/calendar")) {
1327 cal = icalcomponent_new_from_string(content);
1329 ical_saving_vevent(cal);
1330 icalcomponent_free(cal);
1337 * Things we need to do after saving a calendar event.
1339 int ical_obj_aftersave(struct CtdlMessage *msg)
1341 char roomname[ROOMNAMELEN];
1346 * If this isn't the Calendar> room, no further action is necessary.
1349 /* First determine if this is our room */
1350 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1351 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1352 return 0; /* It's not the Calendar room. */
1355 /* Then determine content-type of the message */
1357 /* It must be an RFC822 message! */
1358 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1359 if (msg->cm_format_type != 4) return(1);
1361 /* Find the Content-Type: header */
1362 p = msg->cm_fields['M'];
1365 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1366 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1367 mime_parser(msg->cm_fields['M'],
1369 *ical_obj_aftersave_backend,
1383 /* Oops! No Content-Type in this message! How'd that happen? */
1384 lprintf(7, "RFC822 message with no Content-Type header!\n");
1389 void ical_session_startup(void) {
1390 CtdlAllocUserData(SYM_CIT_ICAL, sizeof(struct cit_ical));
1394 #endif /* HAVE_ICAL_H */
1397 * Register this module with the Citadel server.
1399 char *Dynamic_Module_Init(void)
1402 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1403 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1404 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1405 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
1406 CtdlRegisterSessionHook(ical_session_startup, EVT_START);