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];
46 * Write a calendar object into the specified user's calendar room.
48 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
53 strcpy(temp, tmpnam(NULL));
54 ser = icalcomponent_as_ical_string(cal);
55 if (ser == NULL) return;
57 /* Make a temp file out of it */
58 fp = fopen(temp, "w");
59 if (fp == NULL) return;
60 fwrite(ser, strlen(ser), 1, fp);
63 /* This handy API function does all the work for us.
65 CtdlWriteObject(USERCALENDARROOM, /* which room */
66 "text/calendar", /* MIME type */
70 0, /* don't delete others of this type */
78 * Add a calendar object to the user's calendar
80 void ical_add(icalcomponent *cal, int recursion_level) {
84 * The VEVENT subcomponent is the one we're interested in saving.
86 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
88 ical_write_to_cal(&CC->usersupp, cal);
92 /* If the component has subcomponents, recurse through them. */
93 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
95 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
96 /* Recursively process subcomponent */
97 ical_add(c, recursion_level+1);
105 * Send a reply to a meeting invitation.
107 * 'request' is the invitation to reply to.
108 * 'action' is the string "accept" or "decline".
110 * (Sorry about this being more than 80 columns ... there was just
111 * no easy way to break it down sensibly.)
113 void ical_send_a_reply(icalcomponent *request, char *action) {
114 icalcomponent *the_reply = NULL;
115 icalcomponent *vevent = NULL;
116 icalproperty *attendee = NULL;
117 char attendee_string[SIZ];
118 icalproperty *organizer = NULL;
119 char organizer_string[SIZ];
120 icalproperty *summary = NULL;
121 char summary_string[SIZ];
122 icalproperty *me_attend = NULL;
123 struct recptypes *recp = NULL;
124 icalparameter *partstat = NULL;
125 char *serialized_reply = NULL;
126 char *reply_message_text = NULL;
127 struct CtdlMessage *msg = NULL;
128 struct recptypes *valid = NULL;
130 strcpy(organizer_string, "");
131 strcpy(summary_string, "Calendar item");
133 if (request == NULL) {
134 lprintf(3, "ERROR: trying to reply to NULL event?\n");
138 the_reply = icalcomponent_new_clone(request);
139 if (the_reply == NULL) {
140 lprintf(3, "ERROR: cannot clone request\n");
144 /* Change the method from REQUEST to REPLY */
145 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
147 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
148 if (vevent != NULL) {
149 /* Hunt for attendees, removing ones that aren't us.
150 * (Actually, remove them all, cloning our own one so we can
151 * re-insert it later)
153 while (attendee = icalcomponent_get_first_property(vevent,
154 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
156 if (icalproperty_get_attendee(attendee)) {
157 strcpy(attendee_string,
158 icalproperty_get_attendee(attendee) );
159 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
160 strcpy(attendee_string, &attendee_string[7]);
161 striplt(attendee_string);
162 recp = validate_recipients(attendee_string);
164 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
165 if (me_attend) icalproperty_free(me_attend);
166 me_attend = icalproperty_new_clone(attendee);
173 icalcomponent_remove_property(vevent, attendee);
174 icalproperty_free(attendee);
177 /* We found our own address in the attendee list. */
179 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
180 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
182 if (!strcasecmp(action, "accept")) {
183 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
185 else if (!strcasecmp(action, "decline")) {
186 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
188 else if (!strcasecmp(action, "tentative")) {
189 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
192 if (partstat) icalproperty_add_parameter(me_attend, partstat);
194 /* Now insert it back into the vevent. */
195 icalcomponent_add_property(vevent, me_attend);
198 /* Figure out who to send this thing to */
199 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
200 if (organizer != NULL) {
201 if (icalproperty_get_organizer(organizer)) {
202 strcpy(organizer_string,
203 icalproperty_get_organizer(organizer) );
206 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
207 strcpy(organizer_string, &organizer_string[7]);
208 striplt(organizer_string);
210 strcpy(organizer_string, "");
213 /* Extract the summary string -- we'll use it as the
214 * message subject for the reply
216 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
217 if (summary != NULL) {
218 if (icalproperty_get_summary(summary)) {
219 strcpy(summary_string,
220 icalproperty_get_summary(summary) );
226 /* Now generate the reply message and send it out. */
227 serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
228 icalcomponent_free(the_reply); /* don't need this anymore */
229 if (serialized_reply == NULL) return;
231 reply_message_text = mallok(strlen(serialized_reply) + SIZ);
232 if (reply_message_text != NULL) {
233 sprintf(reply_message_text,
234 "Content-type: text/calendar\r\n\r\n%s\r\n",
238 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
239 CC->quickroom.QRname, 0, FMT_RFC822,
241 summary_string, /* Use summary for subject */
245 valid = validate_recipients(organizer_string);
246 CtdlSubmitMsg(msg, valid, "");
247 CtdlFreeMessage(msg);
250 phree(serialized_reply);
256 * Callback function for mime parser that hunts for calendar content types
257 * and turns them into calendar objects
259 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
260 void *content, char *cbtype, size_t length, char *encoding,
263 struct ical_respond_data *ird = NULL;
265 ird = (struct ical_respond_data *) cbuserdata;
266 if (ird->cal != NULL) {
267 icalcomponent_free(ird->cal);
270 if (strcasecmp(partnum, ird->desired_partnum)) return;
271 ird->cal = icalcomponent_new_from_string(content);
276 * Respond to a meeting request.
278 void ical_respond(long msgnum, char *partnum, char *action) {
279 struct CtdlMessage *msg;
280 struct ical_respond_data ird;
283 (strcasecmp(action, "accept"))
284 && (strcasecmp(action, "decline"))
286 cprintf("%d Action must be 'accept' or 'decline'\n",
287 ERROR + ILLEGAL_VALUE
292 msg = CtdlFetchMessage(msgnum);
294 cprintf("%d Message %ld not found.\n",
301 memset(&ird, 0, sizeof ird);
302 strcpy(ird.desired_partnum, partnum);
303 mime_parser(msg->cm_fields['M'],
305 *ical_locate_part, /* callback function */
307 (void *) &ird, /* user data */
311 /* We're done with the incoming message, because we now have a
312 * calendar object in memory.
314 CtdlFreeMessage(msg);
317 * Here is the real meat of this function. Handle the event.
319 if (ird.cal != NULL) {
320 /* Save this in the user's calendar if necessary */
321 if (!strcasecmp(action, "accept")) {
322 ical_add(ird.cal, 0);
325 /* Send a reply if necessary */
326 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
327 ical_send_a_reply(ird.cal, action);
330 /* Now that we've processed this message, we don't need it
331 * anymore. So delete it.
333 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
335 /* Free the memory we allocated and return a response. */
336 icalcomponent_free(ird.cal);
338 cprintf("%d ok\n", CIT_OK);
342 cprintf("%d No calendar object found\n", ERROR);
346 /* should never get here */
351 * Figure out the UID of the calendar event being referred to in a
352 * REPLY object. This function is recursive.
354 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
355 icalcomponent *subcomponent;
358 /* If this object is a REPLY, then extract the UID. */
359 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
360 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
362 strcpy(uidbuf, icalproperty_get_comment(p));
366 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
367 * UID of the reply; we want the UID of the invitation being replied to.
369 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
370 subcomponent != NULL;
371 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
372 ical_learn_uid_of_reply(uidbuf, subcomponent);
376 void ical_do_FIXME(long msgnum, void *data) {
377 lprintf(9, "Found message <%ld> with correct UID\n", msgnum);
381 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
382 * calendar event. The object has already been deserialized for us; all
383 * we have to do here is hunt for the event in our calendar, merge in the
384 * updated attendee status, and save it again.
386 * This function returns 0 on success, 1 if the event was not found in the
387 * user's calendar, or 2 if an internal error occurred.
389 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
391 char hold_rm[ROOMNAMELEN];
392 int replaced_an_event = 0;
393 struct CtdlMessage *template = NULL;
395 /* Figure out just what event it is we're dealing with */
396 strcpy(uid, "--==<< InVaLiD uId >>==--");
397 ical_learn_uid_of_reply(uid, cal);
398 lprintf(9, "UID of event being replied to is <%s>\n", uid);
400 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
402 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
403 getroom(&CC->quickroom, hold_rm);
404 lprintf(3, "cannot get user calendar room\n");
409 * Pound through the user's calendar looking for a message with
410 * the Citadel EUID set to the value we're looking for. Since
411 * Citadel always sets the message EUID to the vCalendar UID of
412 * the event, this will work.
414 template = (struct CtdlMessage *)
415 mallok(sizeof(struct CtdlMessage));
416 memset(template, 0, sizeof(struct CtdlMessage));
417 template->cm_fields['E'] = strdoop(uid);
418 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
419 template, ical_do_FIXME, NULL);
420 CtdlFreeMessage(template);
421 getroom(&CC->quickroom, hold_rm); /* return to saved room */
423 if (replaced_an_event) {
433 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
434 * simply extracts the calendar object from the message, deserializes it, and
435 * passes it up to ical_update_my_calendar_with_reply() for processing.
437 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
438 struct CtdlMessage *msg;
439 struct ical_respond_data ird;
443 (strcasecmp(action, "update"))
444 && (strcasecmp(action, "ignore"))
446 cprintf("%d Action must be 'update' or 'ignore'\n",
447 ERROR + ILLEGAL_VALUE
452 msg = CtdlFetchMessage(msgnum);
454 cprintf("%d Message %ld not found.\n",
461 memset(&ird, 0, sizeof ird);
462 strcpy(ird.desired_partnum, partnum);
463 mime_parser(msg->cm_fields['M'],
465 *ical_locate_part, /* callback function */
467 (void *) &ird, /* user data */
471 /* We're done with the incoming message, because we now have a
472 * calendar object in memory.
474 CtdlFreeMessage(msg);
477 * Here is the real meat of this function. Handle the event.
479 if (ird.cal != NULL) {
480 /* Update the user's calendar if necessary */
481 if (!strcasecmp(action, "update")) {
482 ret = ical_update_my_calendar_with_reply(ird.cal);
484 cprintf("%d Your calendar has been updated with this reply.\n",
488 cprintf("%d This event does not exist in your calendar.\n",
489 ERROR + FILE_NOT_FOUND);
492 cprintf("%d An internal error occurred.\n",
493 ERROR + INTERNAL_ERROR);
497 cprintf("%d This reply has been ignored.\n", CIT_OK);
500 /* Now that we've processed this message, we don't need it
501 * anymore. So delete it. FIXME uncomment this when ready!
502 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
505 /* Free the memory we allocated and return a response. */
506 icalcomponent_free(ird.cal);
511 cprintf("%d No calendar object found\n", ERROR);
515 /* should never get here */
520 * Search for a property in both the top level and in a VEVENT subcomponent
522 icalproperty *ical_ctdl_get_subprop(
524 icalproperty_kind which_prop
529 p = icalcomponent_get_first_property(cal, which_prop);
531 c = icalcomponent_get_first_component(cal,
532 ICAL_VEVENT_COMPONENT);
534 p = icalcomponent_get_first_property(c, which_prop);
542 * Check to see if two events overlap. Returns nonzero if they do.
544 int ical_ctdl_is_overlap(
545 struct icaltimetype t1start,
546 struct icaltimetype t1end,
547 struct icaltimetype t2start,
548 struct icaltimetype t2end
551 if (icaltime_is_null_time(t1start)) return(0);
552 if (icaltime_is_null_time(t2start)) return(0);
554 /* First, check for all-day events */
555 if (t1start.is_date) {
556 if (!icaltime_compare_date_only(t1start, t2start)) {
559 if (!icaltime_is_null_time(t2end)) {
560 if (!icaltime_compare_date_only(t1start, t2end)) {
566 if (t2start.is_date) {
567 if (!icaltime_compare_date_only(t2start, t1start)) {
570 if (!icaltime_is_null_time(t1end)) {
571 if (!icaltime_compare_date_only(t2start, t1end)) {
577 /* Now check for overlaps using date *and* time. */
579 /* First, bail out if either event 1 or event 2 is missing end time. */
580 if (icaltime_is_null_time(t1end)) return(0);
581 if (icaltime_is_null_time(t2end)) return(0);
583 /* If event 1 ends before event 2 starts, we're in the clear. */
584 if (icaltime_compare(t1end, t2start) <= 0) return(0);
586 /* If event 2 ends before event 1 starts, we're also ok. */
587 if (icaltime_compare(t2end, t1start) <= 0) return(0);
589 /* Otherwise, they overlap. */
596 * Backend for ical_hunt_for_conflicts()
598 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
600 struct CtdlMessage *msg;
601 struct ical_respond_data ird;
602 struct icaltimetype t1start, t1end, t2start, t2end;
604 char conflict_event_uid[SIZ];
605 char conflict_event_summary[SIZ];
606 char compare_uid[SIZ];
608 cal = (icalcomponent *)data;
609 strcpy(compare_uid, "");
610 strcpy(conflict_event_uid, "");
611 strcpy(conflict_event_summary, "");
613 msg = CtdlFetchMessage(msgnum);
614 if (msg == NULL) return;
615 memset(&ird, 0, sizeof ird);
616 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
617 mime_parser(msg->cm_fields['M'],
619 *ical_locate_part, /* callback function */
621 (void *) &ird, /* user data */
624 CtdlFreeMessage(msg);
626 if (ird.cal == NULL) return;
628 t1start = icaltime_null_time();
629 t1end = icaltime_null_time();
630 t2start = icaltime_null_time();
631 t1end = icaltime_null_time();
633 /* Now compare cal to ird.cal */
634 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
635 if (p == NULL) return;
636 if (p != NULL) t2start = icalproperty_get_dtstart(p);
638 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
639 if (p != NULL) t2end = icalproperty_get_dtend(p);
641 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
642 if (p == NULL) return;
643 if (p != NULL) t1start = icalproperty_get_dtstart(p);
645 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
646 if (p != NULL) t1end = icalproperty_get_dtend(p);
648 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
650 strcpy(compare_uid, icalproperty_get_comment(p));
653 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
655 strcpy(conflict_event_uid, icalproperty_get_comment(p));
658 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
660 strcpy(conflict_event_summary, icalproperty_get_comment(p));
664 icalcomponent_free(ird.cal);
666 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
667 cprintf("%ld||%s|%s|%d|\n",
670 conflict_event_summary,
671 ( ((strlen(compare_uid)>0)
672 &&(!strcasecmp(compare_uid,
673 conflict_event_uid))) ? 1 : 0
682 * Phase 2 of "hunt for conflicts" operation.
683 * At this point we have a calendar object which represents the VEVENT that
684 * we're considering adding to the calendar. Now hunt through the user's
685 * calendar room, and output zero or more existing VEVENTs which conflict
688 void ical_hunt_for_conflicts(icalcomponent *cal) {
689 char hold_rm[ROOMNAMELEN];
691 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
693 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
694 getroom(&CC->quickroom, hold_rm);
695 cprintf("%d You do not have a calendar.\n", ERROR);
699 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
701 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
703 ical_hunt_for_conflicts_backend,
708 getroom(&CC->quickroom, hold_rm); /* return to saved room */
715 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
717 void ical_conflicts(long msgnum, char *partnum) {
718 struct CtdlMessage *msg;
719 struct ical_respond_data ird;
721 msg = CtdlFetchMessage(msgnum);
723 cprintf("%d Message %ld not found.\n",
730 memset(&ird, 0, sizeof ird);
731 strcpy(ird.desired_partnum, partnum);
732 mime_parser(msg->cm_fields['M'],
734 *ical_locate_part, /* callback function */
736 (void *) &ird, /* user data */
740 CtdlFreeMessage(msg);
742 if (ird.cal != NULL) {
743 ical_hunt_for_conflicts(ird.cal);
744 icalcomponent_free(ird.cal);
748 cprintf("%d No calendar object found\n", ERROR);
752 /* should never get here */
759 * All Citadel calendar commands from the client come through here.
761 void cmd_ical(char *argbuf)
768 if (CtdlAccessCheck(ac_logged_in)) return;
770 extract(subcmd, argbuf, 0);
772 if (!strcmp(subcmd, "test")) {
773 cprintf("%d This server supports calendaring\n", CIT_OK);
777 else if (!strcmp(subcmd, "respond")) {
778 msgnum = extract_long(argbuf, 1);
779 extract(partnum, argbuf, 2);
780 extract(action, argbuf, 3);
781 ical_respond(msgnum, partnum, action);
784 else if (!strcmp(subcmd, "handle_rsvp")) {
785 msgnum = extract_long(argbuf, 1);
786 extract(partnum, argbuf, 2);
787 extract(action, argbuf, 3);
788 ical_handle_rsvp(msgnum, partnum, action);
791 else if (!strcmp(subcmd, "conflicts")) {
792 msgnum = extract_long(argbuf, 1);
793 extract(partnum, argbuf, 2);
794 ical_conflicts(msgnum, partnum);
798 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
802 /* should never get here */
808 * We don't know if the calendar room exists so we just create it at login
810 void ical_create_room(void)
815 /* Create the calendar room if it doesn't already exist */
816 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
818 /* Set expiration policy to manual; otherwise objects will be lost! */
819 if (lgetroom(&qr, USERCALENDARROOM)) {
820 lprintf(3, "Couldn't get the user calendar room!\n");
823 qr.QRep.expire_mode = EXPIRE_MANUAL;
826 /* Set the view to a calendar view */
827 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
828 vbuf.v_view = 3; /* 3 = calendar */
829 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
831 /* Create the tasks list room if it doesn't already exist */
832 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
834 /* Set expiration policy to manual; otherwise objects will be lost! */
835 if (lgetroom(&qr, USERTASKSROOM)) {
836 lprintf(3, "Couldn't get the user calendar room!\n");
839 qr.QRep.expire_mode = EXPIRE_MANUAL;
842 /* Set the view to a task list view */
843 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
844 vbuf.v_view = 4; /* 4 = tasks */
845 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
852 * ical_send_out_invitations() is called by ical_saving_vevent() when it
853 * finds a VEVENT. FIXME ... finish implementing.
855 void ical_send_out_invitations(icalcomponent *cal) {
856 icalcomponent *the_request = NULL;
857 char *serialized_request = NULL;
858 char *request_message_text = NULL;
859 struct CtdlMessage *msg = NULL;
860 struct recptypes *valid = NULL;
861 char attendees_string[SIZ];
862 char this_attendee[SIZ];
863 icalproperty *attendee = NULL;
864 char summary_string[SIZ];
865 icalproperty *summary = NULL;
866 icalcomponent *encaps = NULL;
869 lprintf(3, "ERROR: trying to reply to NULL event?\n");
873 /* Clone the event */
874 the_request = icalcomponent_new_clone(cal);
875 if (the_request == NULL) {
876 lprintf(3, "ERROR: cannot clone calendar object\n");
880 /* Extract the summary string -- we'll use it as the
881 * message subject for the request
883 strcpy(summary_string, "Meeting request");
884 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
885 if (summary != NULL) {
886 if (icalproperty_get_summary(summary)) {
887 strcpy(summary_string,
888 icalproperty_get_summary(summary) );
892 /* Determine who the recipients of this message are (the attendees) */
893 strcpy(attendees_string, "");
894 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
895 if (icalproperty_get_attendee(attendee)) {
896 strcpy(this_attendee, icalproperty_get_attendee(attendee) );
897 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
898 strcpy(this_attendee, &this_attendee[7]);
899 snprintf(&attendees_string[strlen(attendees_string)],
900 sizeof(attendees_string) - strlen(attendees_string),
908 lprintf(9, "attendees_string: <%s>\n", attendees_string);
910 /* Encapsulate the VEVENT component into a complete VCALENDAR */
911 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
912 if (encaps == NULL) {
913 lprintf(3, "Error at %s:%d - could not allocate component!\n",
915 icalcomponent_free(the_request);
919 /* Set the Product ID */
920 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
922 /* Set the Version Number */
923 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
925 /* Set the method to REQUEST */
926 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
928 /* FIXME: here we need to insert a VTIMEZONE object. */
930 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
931 * are responsible for "the_request"'s memory -- it will be freed
932 * when we free "encaps".
934 icalcomponent_add_component(encaps, the_request);
937 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
938 icalcomponent_free(encaps); /* Don't need this anymore. */
939 if (serialized_request == NULL) return;
941 request_message_text = mallok(strlen(serialized_request) + SIZ);
942 if (request_message_text != NULL) {
943 sprintf(request_message_text,
944 "Content-type: text/calendar\r\n\r\n%s\r\n",
948 msg = CtdlMakeMessage(&CC->usersupp,
949 "", /* No single recipient here */
950 CC->quickroom.QRname, 0, FMT_RFC822,
952 summary_string, /* Use summary for subject */
953 request_message_text);
956 valid = validate_recipients(attendees_string);
957 CtdlSubmitMsg(msg, valid, "");
958 CtdlFreeMessage(msg);
961 phree(serialized_request);
966 * When a calendar object is being saved, determine whether it's a VEVENT
967 * and the user saving it is the organizer. If so, send out invitations
968 * to any listed attendees.
971 void ical_saving_vevent(icalcomponent *cal) {
973 icalproperty *organizer = NULL;
974 char organizer_string[SIZ];
976 strcpy(organizer_string, "");
978 * The VEVENT subcomponent is the one we're interested in.
979 * Send out invitations if, and only if, this user is the Organizer.
981 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
982 organizer = icalcomponent_get_first_property(cal,
983 ICAL_ORGANIZER_PROPERTY);
984 if (organizer != NULL) {
985 if (icalproperty_get_organizer(organizer)) {
986 strcpy(organizer_string,
987 icalproperty_get_organizer(organizer));
990 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
991 strcpy(organizer_string, &organizer_string[7]);
992 striplt(organizer_string);
994 * If the user saving the event is listed as the
995 * organizer, then send out invitations.
997 if (CtdlIsMe(organizer_string)) {
998 ical_send_out_invitations(cal);
1003 /* If the component has subcomponents, recurse through them. */
1004 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1006 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1007 /* Recursively process subcomponent */
1008 ical_saving_vevent(c);
1016 * Back end for ical_obj_beforesave()
1017 * This hunts for the UID of the calendar event.
1019 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
1020 char *disp, void *content, char *cbtype, size_t length,
1021 char *encoding, void *cbuserdata)
1026 /* If this is a text/calendar object, hunt for the UID and drop it in
1027 * the "user data" pointer for the MIME parser. When
1028 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1031 if (!strcasecmp(cbtype, "text/calendar")) {
1032 cal = icalcomponent_new_from_string(content);
1034 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1036 strcpy((char *)cbuserdata,
1037 icalproperty_get_comment(p)
1040 icalcomponent_free(cal);
1050 * See if we need to prevent the object from being saved (we don't allow
1051 * MIME types other than text/calendar in the Calendar> room). Also, when
1052 * saving an event to the calendar, set the message's Citadel extended message
1053 * ID to the UID of the object. This causes our replication checker to
1054 * automatically delete any existing instances of the same object. (Isn't
1057 int ical_obj_beforesave(struct CtdlMessage *msg)
1059 char roomname[ROOMNAMELEN];
1065 * Only messages with content-type text/calendar
1066 * may be saved to Calendar>. If the message is bound for
1067 * Calendar> but doesn't have this content-type, throw an error
1068 * so that the message may not be posted.
1071 /* First determine if this is our room */
1072 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1073 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1074 return 0; /* It's not the Calendar room. */
1077 /* Then determine content-type of the message */
1079 /* It must be an RFC822 message! */
1080 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1081 if (msg->cm_format_type != 4)
1082 return 1; /* You tried to save a non-RFC822 message! */
1084 /* Find the Content-Type: header */
1085 p = msg->cm_fields['M'];
1088 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1089 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1091 mime_parser(msg->cm_fields['M'],
1093 *ical_ctdl_set_extended_msgid,
1098 if (strlen(eidbuf) > 0) {
1099 if (msg->cm_fields['E'] != NULL) {
1100 phree(msg->cm_fields['E']);
1102 msg->cm_fields['E'] = strdoop(eidbuf);
1113 /* Oops! No Content-Type in this message! How'd that happen? */
1114 lprintf(7, "RFC822 message with no Content-Type header!\n");
1120 * Things we need to do after saving a calendar event.
1122 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1123 char *disp, void *content, char *cbtype, size_t length,
1124 char *encoding, void *cbuserdata)
1128 /* If this is a text/calendar object, hunt for the UID and drop it in
1129 * the "user data" pointer for the MIME parser. When
1130 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1133 if (!strcasecmp(cbtype, "text/calendar")) {
1134 cal = icalcomponent_new_from_string(content);
1136 ical_saving_vevent(cal);
1137 icalcomponent_free(cal);
1144 * Things we need to do after saving a calendar event.
1146 int ical_obj_aftersave(struct CtdlMessage *msg)
1148 char roomname[ROOMNAMELEN];
1153 * If this isn't the Calendar> room, no further action is necessary.
1156 /* First determine if this is our room */
1157 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1158 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1159 return 0; /* It's not the Calendar room. */
1162 /* Then determine content-type of the message */
1164 /* It must be an RFC822 message! */
1165 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1166 if (msg->cm_format_type != 4) return(1);
1168 /* Find the Content-Type: header */
1169 p = msg->cm_fields['M'];
1172 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1173 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1174 mime_parser(msg->cm_fields['M'],
1176 *ical_obj_aftersave_backend,
1190 /* Oops! No Content-Type in this message! How'd that happen? */
1191 lprintf(7, "RFC822 message with no Content-Type header!\n");
1196 #endif /* HAVE_ICAL_H */
1199 * Register this module with the Citadel server.
1201 char *Dynamic_Module_Init(void)
1204 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1205 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1206 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1207 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");