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);
378 * ical_update_my_calendar_with_reply() refers to this callback function; when we
379 * locate the message containing the calendar event we're replying to, this function
380 * gets called. It basically just sticks the message number in a supplied buffer.
382 void ical_hunt_for_event_to_update(long msgnum, void *data) {
385 msgnumptr = (long *) data;
390 struct original_event_container {
395 * Callback function for mime parser that hunts for calendar content types
396 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
397 * to fetch the object being updated)
399 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
400 void *content, char *cbtype, size_t length, char *encoding,
403 struct original_event_container *oec = NULL;
405 if (strcasecmp(cbtype, "text/calendar")) {
408 oec = (struct original_event_container *) cbuserdata;
409 if (oec->c != NULL) {
410 icalcomponent_free(oec->c);
412 oec->c = icalcomponent_new_from_string(content);
417 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
418 * calendar event. The object has already been deserialized for us; all
419 * we have to do here is hunt for the event in our calendar, merge in the
420 * updated attendee status, and save it again.
422 * This function returns 0 on success, 1 if the event was not found in the
423 * user's calendar, or 2 if an internal error occurred.
425 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
427 char hold_rm[ROOMNAMELEN];
428 long msgnum_being_replaced = 0;
429 struct CtdlMessage *template = NULL;
430 struct CtdlMessage *msg;
431 struct original_event_container oec;
432 icalcomponent *original_event;
434 /* Figure out just what event it is we're dealing with */
435 strcpy(uid, "--==<< InVaLiD uId >>==--");
436 ical_learn_uid_of_reply(uid, cal);
437 lprintf(9, "UID of event being replied to is <%s>\n", uid);
439 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
441 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
442 getroom(&CC->quickroom, hold_rm);
443 lprintf(3, "cannot get user calendar room\n");
448 * Pound through the user's calendar looking for a message with
449 * the Citadel EUID set to the value we're looking for. Since
450 * Citadel always sets the message EUID to the vCalendar UID of
451 * the event, this will work.
453 template = (struct CtdlMessage *)
454 mallok(sizeof(struct CtdlMessage));
455 memset(template, 0, sizeof(struct CtdlMessage));
456 template->cm_fields['E'] = strdoop(uid);
457 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
458 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
459 CtdlFreeMessage(template);
460 getroom(&CC->quickroom, hold_rm); /* return to saved room */
462 lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
463 if (msgnum_being_replaced == 0) {
464 return(1); /* no calendar event found */
467 /* Now we know the ID of the message containing the event being updated.
468 * We don't actually have to delete it; that'll get taken care of by the
469 * server when we save another event with the same UID. This just gives
470 * us the ability to load the event into memory so we can diddle the
473 msg = CtdlFetchMessage(msgnum_being_replaced);
475 return(2); /* internal error */
478 mime_parser(msg->cm_fields['M'],
480 *ical_locate_original_event, /* callback function */
482 &oec, /* user data */
485 CtdlFreeMessage(msg);
487 original_event = oec.c;
488 if (original_event == NULL) {
489 lprintf(3, "ERROR: Original_component is NULL.\n");
493 /* FIXME finish this */
494 /* merge "cal" into "original_event" */
495 /* reserialize "original_event" and save to disk */
497 icalcomponent_free(original_event);
503 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
504 * simply extracts the calendar object from the message, deserializes it, and
505 * passes it up to ical_update_my_calendar_with_reply() for processing.
507 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
508 struct CtdlMessage *msg;
509 struct ical_respond_data ird;
513 (strcasecmp(action, "update"))
514 && (strcasecmp(action, "ignore"))
516 cprintf("%d Action must be 'update' or 'ignore'\n",
517 ERROR + ILLEGAL_VALUE
522 msg = CtdlFetchMessage(msgnum);
524 cprintf("%d Message %ld not found.\n",
531 memset(&ird, 0, sizeof ird);
532 strcpy(ird.desired_partnum, partnum);
533 mime_parser(msg->cm_fields['M'],
535 *ical_locate_part, /* callback function */
537 (void *) &ird, /* user data */
541 /* We're done with the incoming message, because we now have a
542 * calendar object in memory.
544 CtdlFreeMessage(msg);
547 * Here is the real meat of this function. Handle the event.
549 if (ird.cal != NULL) {
550 /* Update the user's calendar if necessary */
551 if (!strcasecmp(action, "update")) {
552 ret = ical_update_my_calendar_with_reply(ird.cal);
554 cprintf("%d Your calendar has been updated with this reply.\n",
558 cprintf("%d This event does not exist in your calendar.\n",
559 ERROR + FILE_NOT_FOUND);
562 cprintf("%d An internal error occurred.\n",
563 ERROR + INTERNAL_ERROR);
567 cprintf("%d This reply has been ignored.\n", CIT_OK);
570 /* Now that we've processed this message, we don't need it
571 * anymore. So delete it. FIXME uncomment this when ready!
572 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
575 /* Free the memory we allocated and return a response. */
576 icalcomponent_free(ird.cal);
581 cprintf("%d No calendar object found\n", ERROR);
585 /* should never get here */
590 * Search for a property in both the top level and in a VEVENT subcomponent
592 icalproperty *ical_ctdl_get_subprop(
594 icalproperty_kind which_prop
599 p = icalcomponent_get_first_property(cal, which_prop);
601 c = icalcomponent_get_first_component(cal,
602 ICAL_VEVENT_COMPONENT);
604 p = icalcomponent_get_first_property(c, which_prop);
612 * Check to see if two events overlap. Returns nonzero if they do.
614 int ical_ctdl_is_overlap(
615 struct icaltimetype t1start,
616 struct icaltimetype t1end,
617 struct icaltimetype t2start,
618 struct icaltimetype t2end
621 if (icaltime_is_null_time(t1start)) return(0);
622 if (icaltime_is_null_time(t2start)) return(0);
624 /* First, check for all-day events */
625 if (t1start.is_date) {
626 if (!icaltime_compare_date_only(t1start, t2start)) {
629 if (!icaltime_is_null_time(t2end)) {
630 if (!icaltime_compare_date_only(t1start, t2end)) {
636 if (t2start.is_date) {
637 if (!icaltime_compare_date_only(t2start, t1start)) {
640 if (!icaltime_is_null_time(t1end)) {
641 if (!icaltime_compare_date_only(t2start, t1end)) {
647 /* Now check for overlaps using date *and* time. */
649 /* First, bail out if either event 1 or event 2 is missing end time. */
650 if (icaltime_is_null_time(t1end)) return(0);
651 if (icaltime_is_null_time(t2end)) return(0);
653 /* If event 1 ends before event 2 starts, we're in the clear. */
654 if (icaltime_compare(t1end, t2start) <= 0) return(0);
656 /* If event 2 ends before event 1 starts, we're also ok. */
657 if (icaltime_compare(t2end, t1start) <= 0) return(0);
659 /* Otherwise, they overlap. */
666 * Backend for ical_hunt_for_conflicts()
668 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
670 struct CtdlMessage *msg;
671 struct ical_respond_data ird;
672 struct icaltimetype t1start, t1end, t2start, t2end;
674 char conflict_event_uid[SIZ];
675 char conflict_event_summary[SIZ];
676 char compare_uid[SIZ];
678 cal = (icalcomponent *)data;
679 strcpy(compare_uid, "");
680 strcpy(conflict_event_uid, "");
681 strcpy(conflict_event_summary, "");
683 msg = CtdlFetchMessage(msgnum);
684 if (msg == NULL) return;
685 memset(&ird, 0, sizeof ird);
686 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
687 mime_parser(msg->cm_fields['M'],
689 *ical_locate_part, /* callback function */
691 (void *) &ird, /* user data */
694 CtdlFreeMessage(msg);
696 if (ird.cal == NULL) return;
698 t1start = icaltime_null_time();
699 t1end = icaltime_null_time();
700 t2start = icaltime_null_time();
701 t1end = icaltime_null_time();
703 /* Now compare cal to ird.cal */
704 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
705 if (p == NULL) return;
706 if (p != NULL) t2start = icalproperty_get_dtstart(p);
708 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
709 if (p != NULL) t2end = icalproperty_get_dtend(p);
711 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
712 if (p == NULL) return;
713 if (p != NULL) t1start = icalproperty_get_dtstart(p);
715 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
716 if (p != NULL) t1end = icalproperty_get_dtend(p);
718 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
720 strcpy(compare_uid, icalproperty_get_comment(p));
723 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
725 strcpy(conflict_event_uid, icalproperty_get_comment(p));
728 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
730 strcpy(conflict_event_summary, icalproperty_get_comment(p));
734 icalcomponent_free(ird.cal);
736 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
737 cprintf("%ld||%s|%s|%d|\n",
740 conflict_event_summary,
741 ( ((strlen(compare_uid)>0)
742 &&(!strcasecmp(compare_uid,
743 conflict_event_uid))) ? 1 : 0
752 * Phase 2 of "hunt for conflicts" operation.
753 * At this point we have a calendar object which represents the VEVENT that
754 * we're considering adding to the calendar. Now hunt through the user's
755 * calendar room, and output zero or more existing VEVENTs which conflict
758 void ical_hunt_for_conflicts(icalcomponent *cal) {
759 char hold_rm[ROOMNAMELEN];
761 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
763 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
764 getroom(&CC->quickroom, hold_rm);
765 cprintf("%d You do not have a calendar.\n", ERROR);
769 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
771 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
773 ical_hunt_for_conflicts_backend,
778 getroom(&CC->quickroom, hold_rm); /* return to saved room */
785 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
787 void ical_conflicts(long msgnum, char *partnum) {
788 struct CtdlMessage *msg;
789 struct ical_respond_data ird;
791 msg = CtdlFetchMessage(msgnum);
793 cprintf("%d Message %ld not found.\n",
800 memset(&ird, 0, sizeof ird);
801 strcpy(ird.desired_partnum, partnum);
802 mime_parser(msg->cm_fields['M'],
804 *ical_locate_part, /* callback function */
806 (void *) &ird, /* user data */
810 CtdlFreeMessage(msg);
812 if (ird.cal != NULL) {
813 ical_hunt_for_conflicts(ird.cal);
814 icalcomponent_free(ird.cal);
818 cprintf("%d No calendar object found\n", ERROR);
822 /* should never get here */
829 * All Citadel calendar commands from the client come through here.
831 void cmd_ical(char *argbuf)
838 if (CtdlAccessCheck(ac_logged_in)) return;
840 extract(subcmd, argbuf, 0);
842 if (!strcmp(subcmd, "test")) {
843 cprintf("%d This server supports calendaring\n", CIT_OK);
847 else if (!strcmp(subcmd, "respond")) {
848 msgnum = extract_long(argbuf, 1);
849 extract(partnum, argbuf, 2);
850 extract(action, argbuf, 3);
851 ical_respond(msgnum, partnum, action);
854 else if (!strcmp(subcmd, "handle_rsvp")) {
855 msgnum = extract_long(argbuf, 1);
856 extract(partnum, argbuf, 2);
857 extract(action, argbuf, 3);
858 ical_handle_rsvp(msgnum, partnum, action);
861 else if (!strcmp(subcmd, "conflicts")) {
862 msgnum = extract_long(argbuf, 1);
863 extract(partnum, argbuf, 2);
864 ical_conflicts(msgnum, partnum);
868 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
872 /* should never get here */
878 * We don't know if the calendar room exists so we just create it at login
880 void ical_create_room(void)
885 /* Create the calendar room if it doesn't already exist */
886 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
888 /* Set expiration policy to manual; otherwise objects will be lost! */
889 if (lgetroom(&qr, USERCALENDARROOM)) {
890 lprintf(3, "Couldn't get the user calendar room!\n");
893 qr.QRep.expire_mode = EXPIRE_MANUAL;
896 /* Set the view to a calendar view */
897 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
898 vbuf.v_view = 3; /* 3 = calendar */
899 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
901 /* Create the tasks list room if it doesn't already exist */
902 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
904 /* Set expiration policy to manual; otherwise objects will be lost! */
905 if (lgetroom(&qr, USERTASKSROOM)) {
906 lprintf(3, "Couldn't get the user calendar room!\n");
909 qr.QRep.expire_mode = EXPIRE_MANUAL;
912 /* Set the view to a task list view */
913 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
914 vbuf.v_view = 4; /* 4 = tasks */
915 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
922 * ical_send_out_invitations() is called by ical_saving_vevent() when it
923 * finds a VEVENT. FIXME ... finish implementing.
925 void ical_send_out_invitations(icalcomponent *cal) {
926 icalcomponent *the_request = NULL;
927 char *serialized_request = NULL;
928 char *request_message_text = NULL;
929 struct CtdlMessage *msg = NULL;
930 struct recptypes *valid = NULL;
931 char attendees_string[SIZ];
932 char this_attendee[SIZ];
933 icalproperty *attendee = NULL;
934 char summary_string[SIZ];
935 icalproperty *summary = NULL;
936 icalcomponent *encaps = NULL;
939 lprintf(3, "ERROR: trying to reply to NULL event?\n");
943 /* Clone the event */
944 the_request = icalcomponent_new_clone(cal);
945 if (the_request == NULL) {
946 lprintf(3, "ERROR: cannot clone calendar object\n");
950 /* Extract the summary string -- we'll use it as the
951 * message subject for the request
953 strcpy(summary_string, "Meeting request");
954 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
955 if (summary != NULL) {
956 if (icalproperty_get_summary(summary)) {
957 strcpy(summary_string,
958 icalproperty_get_summary(summary) );
962 /* Determine who the recipients of this message are (the attendees) */
963 strcpy(attendees_string, "");
964 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
965 if (icalproperty_get_attendee(attendee)) {
966 strcpy(this_attendee, icalproperty_get_attendee(attendee) );
967 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
968 strcpy(this_attendee, &this_attendee[7]);
969 snprintf(&attendees_string[strlen(attendees_string)],
970 sizeof(attendees_string) - strlen(attendees_string),
978 lprintf(9, "attendees_string: <%s>\n", attendees_string);
980 /* Encapsulate the VEVENT component into a complete VCALENDAR */
981 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
982 if (encaps == NULL) {
983 lprintf(3, "Error at %s:%d - could not allocate component!\n",
985 icalcomponent_free(the_request);
989 /* Set the Product ID */
990 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
992 /* Set the Version Number */
993 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
995 /* Set the method to REQUEST */
996 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
998 /* FIXME: here we need to insert a VTIMEZONE object. */
1000 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1001 * are responsible for "the_request"'s memory -- it will be freed
1002 * when we free "encaps".
1004 icalcomponent_add_component(encaps, the_request);
1007 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
1008 icalcomponent_free(encaps); /* Don't need this anymore. */
1009 if (serialized_request == NULL) return;
1011 request_message_text = mallok(strlen(serialized_request) + SIZ);
1012 if (request_message_text != NULL) {
1013 sprintf(request_message_text,
1014 "Content-type: text/calendar\r\n\r\n%s\r\n",
1018 msg = CtdlMakeMessage(&CC->usersupp,
1019 "", /* No single recipient here */
1020 CC->quickroom.QRname, 0, FMT_RFC822,
1022 summary_string, /* Use summary for subject */
1023 request_message_text);
1026 valid = validate_recipients(attendees_string);
1027 CtdlSubmitMsg(msg, valid, "");
1028 CtdlFreeMessage(msg);
1031 phree(serialized_request);
1036 * When a calendar object is being saved, determine whether it's a VEVENT
1037 * and the user saving it is the organizer. If so, send out invitations
1038 * to any listed attendees.
1041 void ical_saving_vevent(icalcomponent *cal) {
1043 icalproperty *organizer = NULL;
1044 char organizer_string[SIZ];
1046 strcpy(organizer_string, "");
1048 * The VEVENT subcomponent is the one we're interested in.
1049 * Send out invitations if, and only if, this user is the Organizer.
1051 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1052 organizer = icalcomponent_get_first_property(cal,
1053 ICAL_ORGANIZER_PROPERTY);
1054 if (organizer != NULL) {
1055 if (icalproperty_get_organizer(organizer)) {
1056 strcpy(organizer_string,
1057 icalproperty_get_organizer(organizer));
1060 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1061 strcpy(organizer_string, &organizer_string[7]);
1062 striplt(organizer_string);
1064 * If the user saving the event is listed as the
1065 * organizer, then send out invitations.
1067 if (CtdlIsMe(organizer_string)) {
1068 ical_send_out_invitations(cal);
1073 /* If the component has subcomponents, recurse through them. */
1074 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1076 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1077 /* Recursively process subcomponent */
1078 ical_saving_vevent(c);
1086 * Back end for ical_obj_beforesave()
1087 * This hunts for the UID of the calendar event.
1089 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
1090 char *disp, void *content, char *cbtype, size_t length,
1091 char *encoding, void *cbuserdata)
1096 /* If this is a text/calendar object, hunt for the UID and drop it in
1097 * the "user data" pointer for the MIME parser. When
1098 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1101 if (!strcasecmp(cbtype, "text/calendar")) {
1102 cal = icalcomponent_new_from_string(content);
1104 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1106 strcpy((char *)cbuserdata,
1107 icalproperty_get_comment(p)
1110 icalcomponent_free(cal);
1120 * See if we need to prevent the object from being saved (we don't allow
1121 * MIME types other than text/calendar in the Calendar> room). Also, when
1122 * saving an event to the calendar, set the message's Citadel extended message
1123 * ID to the UID of the object. This causes our replication checker to
1124 * automatically delete any existing instances of the same object. (Isn't
1127 int ical_obj_beforesave(struct CtdlMessage *msg)
1129 char roomname[ROOMNAMELEN];
1135 * Only messages with content-type text/calendar
1136 * may be saved to Calendar>. If the message is bound for
1137 * Calendar> but doesn't have this content-type, throw an error
1138 * so that the message may not be posted.
1141 /* First determine if this is our room */
1142 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1143 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1144 return 0; /* It's not the Calendar room. */
1147 /* Then determine content-type of the message */
1149 /* It must be an RFC822 message! */
1150 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1151 if (msg->cm_format_type != 4)
1152 return 1; /* You tried to save a non-RFC822 message! */
1154 /* Find the Content-Type: header */
1155 p = msg->cm_fields['M'];
1158 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1159 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1161 mime_parser(msg->cm_fields['M'],
1163 *ical_ctdl_set_extended_msgid,
1168 if (strlen(eidbuf) > 0) {
1169 if (msg->cm_fields['E'] != NULL) {
1170 phree(msg->cm_fields['E']);
1172 msg->cm_fields['E'] = strdoop(eidbuf);
1183 /* Oops! No Content-Type in this message! How'd that happen? */
1184 lprintf(7, "RFC822 message with no Content-Type header!\n");
1190 * Things we need to do after saving a calendar event.
1192 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1193 char *disp, void *content, char *cbtype, size_t length,
1194 char *encoding, void *cbuserdata)
1198 /* If this is a text/calendar object, hunt for the UID and drop it in
1199 * the "user data" pointer for the MIME parser. When
1200 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1203 if (!strcasecmp(cbtype, "text/calendar")) {
1204 cal = icalcomponent_new_from_string(content);
1206 ical_saving_vevent(cal);
1207 icalcomponent_free(cal);
1214 * Things we need to do after saving a calendar event.
1216 int ical_obj_aftersave(struct CtdlMessage *msg)
1218 char roomname[ROOMNAMELEN];
1223 * If this isn't the Calendar> room, no further action is necessary.
1226 /* First determine if this is our room */
1227 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1228 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1229 return 0; /* It's not the Calendar room. */
1232 /* Then determine content-type of the message */
1234 /* It must be an RFC822 message! */
1235 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1236 if (msg->cm_format_type != 4) return(1);
1238 /* Find the Content-Type: header */
1239 p = msg->cm_fields['M'];
1242 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1243 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1244 mime_parser(msg->cm_fields['M'],
1246 *ical_obj_aftersave_backend,
1260 /* Oops! No Content-Type in this message! How'd that happen? */
1261 lprintf(7, "RFC822 message with no Content-Type header!\n");
1266 #endif /* HAVE_ICAL_H */
1269 * Register this module with the Citadel server.
1271 char *Dynamic_Module_Init(void)
1274 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1275 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1276 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1277 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");