4 * This module implements iCalendar object processing and the Calendar>
5 * room on a Citadel server. It handles iCalendar objects using the
6 * iTIP protocol. See RFCs 2445 and 2446.
10 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
15 #include <sys/types.h>
24 #include "citserver.h"
31 #include "mime_parser.h"
32 #include "internet_addressing.h"
33 #include "serv_calendar.h"
34 #include "euidindex.h"
35 #include "ctdl_module.h"
37 #ifdef CITADEL_WITH_CALENDAR_SERVICE
40 #include "ical_dezonify.h"
44 struct ical_respond_data {
45 char desired_partnum[SIZ];
51 * Utility function to create a new VCALENDAR component with some of the
52 * required fields already set the way we like them.
54 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
55 icalcomponent *encaps;
57 encaps = icalcomponent_new_vcalendar();
59 lprintf(CTDL_CRIT, "Error at %s:%d - could not allocate component!\n",
64 /* Set the Product ID */
65 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
67 /* Set the Version Number */
68 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
75 * Utility function to encapsulate a subcomponent into a full VCALENDAR
77 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
78 icalcomponent *encaps;
80 /* If we're already looking at a full VCALENDAR component,
81 * don't bother ... just return itself.
83 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
87 /* Encapsulate the VEVENT component into a complete VCALENDAR */
88 encaps = icalcomponent_new_citadel_vcalendar();
89 if (encaps == NULL) return NULL;
91 /* Encapsulate the subcomponent inside */
92 icalcomponent_add_component(encaps, subcomp);
94 /* Convert all timestamps to UTC so we don't have to deal with
95 * stupid VTIMEZONE crap.
97 ical_dezonify(encaps);
99 /* Return the object we just created. */
107 * Write a calendar object into the specified user's calendar room.
108 * If the supplied user is NULL, this function writes the calendar object
109 * to the currently selected room.
111 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
115 icalcomponent *encaps = NULL;
116 struct CtdlMessage *msg = NULL;
118 if (cal == NULL) return;
120 /* If the supplied object is a subcomponent, encapsulate it in
121 * a full VCALENDAR component, and save that instead.
123 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
124 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(cal));
125 ical_write_to_cal(u, encaps);
126 icalcomponent_free(encaps);
130 ser = icalcomponent_as_ical_string(cal);
131 if (ser == NULL) return;
133 /* If the caller supplied a user, write to that user's default calendar room */
135 /* Make a temp file out of it */
136 CtdlMakeTempFileName(temp, sizeof temp);
137 fp = fopen(temp, "w");
139 fwrite(ser, strlen(ser), 1, fp);
142 /* This handy API function does all the work for us. */
143 CtdlWriteObject(USERCALENDARROOM, /* which room */
144 "text/calendar", /* MIME type */
145 temp, /* temp file */
148 0, /* don't delete others of this type */
155 /* If the caller did not supply a user, write to the currently selected room */
157 msg = malloc(sizeof(struct CtdlMessage));
158 memset(msg, 0, sizeof(struct CtdlMessage));
159 msg->cm_magic = CTDLMESSAGE_MAGIC;
160 msg->cm_anon_type = MES_NORMAL;
161 msg->cm_format_type = 4;
162 msg->cm_fields['A'] = strdup(CC->user.fullname);
163 msg->cm_fields['O'] = strdup(CC->room.QRname);
164 msg->cm_fields['N'] = strdup(config.c_nodename);
165 msg->cm_fields['H'] = strdup(config.c_humannode);
166 msg->cm_fields['M'] = malloc(strlen(ser) + 40);
167 strcpy(msg->cm_fields['M'], "Content-type: text/calendar\r\n\r\n");
168 strcat(msg->cm_fields['M'], ser);
170 /* Now write the data */
171 CtdlSubmitMsg(msg, NULL, "");
172 CtdlFreeMessage(msg);
175 /* In either case, now we can free the serialized calendar object */
181 * Add a calendar object to the user's calendar
183 * ok because it uses ical_write_to_cal()
185 void ical_add(icalcomponent *cal, int recursion_level) {
189 * The VEVENT subcomponent is the one we're interested in saving.
191 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
193 ical_write_to_cal(&CC->user, cal);
197 /* If the component has subcomponents, recurse through them. */
198 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
200 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
201 /* Recursively process subcomponent */
202 ical_add(c, recursion_level+1);
210 * Send a reply to a meeting invitation.
212 * 'request' is the invitation to reply to.
213 * 'action' is the string "accept" or "decline" or "tentative".
216 void ical_send_a_reply(icalcomponent *request, char *action) {
217 icalcomponent *the_reply = NULL;
218 icalcomponent *vevent = NULL;
219 icalproperty *attendee = NULL;
220 char attendee_string[SIZ];
221 icalproperty *organizer = NULL;
222 char organizer_string[SIZ];
223 icalproperty *summary = NULL;
224 char summary_string[SIZ];
225 icalproperty *me_attend = NULL;
226 struct recptypes *recp = NULL;
227 icalparameter *partstat = NULL;
228 char *serialized_reply = NULL;
229 char *reply_message_text = NULL;
230 struct CtdlMessage *msg = NULL;
231 struct recptypes *valid = NULL;
233 strcpy(organizer_string, "");
234 strcpy(summary_string, "Calendar item");
236 if (request == NULL) {
237 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
241 the_reply = icalcomponent_new_clone(request);
242 if (the_reply == NULL) {
243 lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
247 /* Change the method from REQUEST to REPLY */
248 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
250 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
251 if (vevent != NULL) {
252 /* Hunt for attendees, removing ones that aren't us.
253 * (Actually, remove them all, cloning our own one so we can
254 * re-insert it later)
256 while (attendee = icalcomponent_get_first_property(vevent,
257 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
259 if (icalproperty_get_attendee(attendee)) {
260 strcpy(attendee_string,
261 icalproperty_get_attendee(attendee) );
262 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
263 strcpy(attendee_string, &attendee_string[7]);
264 striplt(attendee_string);
265 recp = validate_recipients(attendee_string);
267 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
268 if (me_attend) icalproperty_free(me_attend);
269 me_attend = icalproperty_new_clone(attendee);
271 free_recipients(recp);
276 icalcomponent_remove_property(vevent, attendee);
277 icalproperty_free(attendee);
280 /* We found our own address in the attendee list. */
282 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
283 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
285 if (!strcasecmp(action, "accept")) {
286 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
288 else if (!strcasecmp(action, "decline")) {
289 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
291 else if (!strcasecmp(action, "tentative")) {
292 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
295 if (partstat) icalproperty_add_parameter(me_attend, partstat);
297 /* Now insert it back into the vevent. */
298 icalcomponent_add_property(vevent, me_attend);
301 /* Figure out who to send this thing to */
302 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
303 if (organizer != NULL) {
304 if (icalproperty_get_organizer(organizer)) {
305 strcpy(organizer_string,
306 icalproperty_get_organizer(organizer) );
309 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
310 strcpy(organizer_string, &organizer_string[7]);
311 striplt(organizer_string);
313 strcpy(organizer_string, "");
316 /* Extract the summary string -- we'll use it as the
317 * message subject for the reply
319 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
320 if (summary != NULL) {
321 if (icalproperty_get_summary(summary)) {
322 strcpy(summary_string,
323 icalproperty_get_summary(summary) );
328 /* Now generate the reply message and send it out. */
329 serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
330 icalcomponent_free(the_reply); /* don't need this anymore */
331 if (serialized_reply == NULL) return;
333 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
334 if (reply_message_text != NULL) {
335 sprintf(reply_message_text,
336 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
340 msg = CtdlMakeMessage(&CC->user,
341 organizer_string, /* to */
343 CC->room.QRname, 0, FMT_RFC822,
346 summary_string, /* Use summary for subject */
351 valid = validate_recipients(organizer_string);
352 CtdlSubmitMsg(msg, valid, "");
353 CtdlFreeMessage(msg);
354 free_recipients(valid);
357 free(serialized_reply);
363 * Callback function for mime parser that hunts for calendar content types
364 * and turns them into calendar objects
366 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
367 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
370 struct ical_respond_data *ird = NULL;
372 ird = (struct ical_respond_data *) cbuserdata;
374 /* desired_partnum can be set to "_HUNT_" to have it just look for
375 * the first part with a content type of text/calendar. Otherwise
376 * we have to only process the right one.
378 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
379 if (strcasecmp(partnum, ird->desired_partnum)) {
384 if (strcasecmp(cbtype, "text/calendar")) {
388 if (ird->cal != NULL) {
389 icalcomponent_free(ird->cal);
393 ird->cal = icalcomponent_new_from_string(content);
394 if (ird->cal != NULL) {
395 ical_dezonify(ird->cal);
401 * Respond to a meeting request.
403 void ical_respond(long msgnum, char *partnum, char *action) {
404 struct CtdlMessage *msg = NULL;
405 struct ical_respond_data ird;
408 (strcasecmp(action, "accept"))
409 && (strcasecmp(action, "decline"))
411 cprintf("%d Action must be 'accept' or 'decline'\n",
412 ERROR + ILLEGAL_VALUE
417 msg = CtdlFetchMessage(msgnum, 1);
419 cprintf("%d Message %ld not found.\n",
420 ERROR + ILLEGAL_VALUE,
426 memset(&ird, 0, sizeof ird);
427 strcpy(ird.desired_partnum, partnum);
428 mime_parser(msg->cm_fields['M'],
430 *ical_locate_part, /* callback function */
432 (void *) &ird, /* user data */
436 /* We're done with the incoming message, because we now have a
437 * calendar object in memory.
439 CtdlFreeMessage(msg);
442 * Here is the real meat of this function. Handle the event.
444 if (ird.cal != NULL) {
445 /* Save this in the user's calendar if necessary */
446 if (!strcasecmp(action, "accept")) {
447 ical_add(ird.cal, 0);
450 /* Send a reply if necessary */
451 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
452 ical_send_a_reply(ird.cal, action);
455 /* Now that we've processed this message, we don't need it
456 * anymore. So delete it. (NOTE we don't do this anymore.)
457 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
460 /* Free the memory we allocated and return a response. */
461 icalcomponent_free(ird.cal);
463 cprintf("%d ok\n", CIT_OK);
467 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
471 /* should never get here */
476 * Figure out the UID of the calendar event being referred to in a
477 * REPLY object. This function is recursive.
479 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
480 icalcomponent *subcomponent;
483 /* If this object is a REPLY, then extract the UID. */
484 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
485 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
487 strcpy(uidbuf, icalproperty_get_comment(p));
491 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
492 * UID of the reply; we want the UID of the invitation being replied to.
494 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
495 subcomponent != NULL;
496 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
497 ical_learn_uid_of_reply(uidbuf, subcomponent);
503 * ical_update_my_calendar_with_reply() refers to this callback function; when we
504 * locate the message containing the calendar event we're replying to, this function
505 * gets called. It basically just sticks the message number in a supplied buffer.
507 void ical_hunt_for_event_to_update(long msgnum, void *data) {
510 msgnumptr = (long *) data;
515 struct original_event_container {
520 * Callback function for mime parser that hunts for calendar content types
521 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
522 * to fetch the object being updated)
524 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
525 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
528 struct original_event_container *oec = NULL;
530 if (strcasecmp(cbtype, "text/calendar")) {
533 oec = (struct original_event_container *) cbuserdata;
534 if (oec->c != NULL) {
535 icalcomponent_free(oec->c);
537 oec->c = icalcomponent_new_from_string(content);
542 * Merge updated attendee information from a REPLY into an existing event.
544 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
546 icalproperty *e_attendee, *r_attendee;
548 /* First things first. If we're not looking at a VEVENT component,
549 * recurse through subcomponents until we find one.
551 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
552 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
554 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
555 ical_merge_attendee_reply(c, reply);
560 /* Now do the same thing with the reply.
562 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
563 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
565 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
566 ical_merge_attendee_reply(event, c);
571 /* Clone the reply, because we're going to rip its guts out. */
572 reply = icalcomponent_new_clone(reply);
574 /* At this point we're looking at the correct subcomponents.
575 * Iterate through the attendees looking for a match.
578 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
580 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
582 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
584 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
586 /* Check to see if these two attendees match...
589 icalproperty_get_attendee(e_attendee),
590 icalproperty_get_attendee(r_attendee)
592 /* ...and if they do, remove the attendee from the event
593 * and replace it with the attendee from the reply. (The
594 * reply's copy will have the same address, but an updated
597 icalcomponent_remove_property(event, e_attendee);
598 icalproperty_free(e_attendee);
599 icalcomponent_remove_property(reply, r_attendee);
600 icalcomponent_add_property(event, r_attendee);
602 /* Since we diddled both sets of attendees, we have to start
603 * the iteration over again. This will not create an infinite
604 * loop because we removed the attendee from the reply. (That's
605 * why we cloned the reply, and that's what we mean by "ripping
614 /* Free the *clone* of the reply. */
615 icalcomponent_free(reply);
622 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
623 * calendar event. The object has already been deserialized for us; all
624 * we have to do here is hunt for the event in our calendar, merge in the
625 * updated attendee status, and save it again.
627 * This function returns 0 on success, 1 if the event was not found in the
628 * user's calendar, or 2 if an internal error occurred.
630 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
632 char hold_rm[ROOMNAMELEN];
633 long msgnum_being_replaced = 0;
634 struct CtdlMessage *msg = NULL;
635 struct original_event_container oec;
636 icalcomponent *original_event;
637 char *serialized_event = NULL;
638 char roomname[ROOMNAMELEN];
639 char *message_text = NULL;
641 /* Figure out just what event it is we're dealing with */
642 strcpy(uid, "--==<< InVaLiD uId >>==--");
643 ical_learn_uid_of_reply(uid, cal);
644 lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
646 strcpy(hold_rm, CC->room.QRname); /* save current room */
648 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
649 getroom(&CC->room, hold_rm);
650 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
655 * Look in the EUID index for a message with
656 * the Citadel EUID set to the value we're looking for. Since
657 * Citadel always sets the message EUID to the vCalendar UID of
658 * the event, this will work.
660 msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
662 getroom(&CC->room, hold_rm); /* return to saved room */
664 lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
665 if (msgnum_being_replaced == 0) {
666 return(1); /* no calendar event found */
669 /* Now we know the ID of the message containing the event being updated.
670 * We don't actually have to delete it; that'll get taken care of by the
671 * server when we save another event with the same UID. This just gives
672 * us the ability to load the event into memory so we can diddle the
675 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
677 return(2); /* internal error */
680 mime_parser(msg->cm_fields['M'],
682 *ical_locate_original_event, /* callback function */
684 &oec, /* user data */
687 CtdlFreeMessage(msg);
689 original_event = oec.c;
690 if (original_event == NULL) {
691 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
695 /* Merge the attendee's updated status into the event */
696 ical_merge_attendee_reply(original_event, cal);
699 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
700 icalcomponent_free(original_event); /* Don't need this anymore. */
701 if (serialized_event == NULL) return(2);
703 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
705 message_text = malloc(strlen(serialized_event) + SIZ);
706 if (message_text != NULL) {
707 sprintf(message_text,
708 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
712 msg = CtdlMakeMessage(&CC->user,
713 "", /* No recipient */
714 "", /* No recipient */
724 CIT_ICAL->avoid_sending_invitations = 1;
725 CtdlSubmitMsg(msg, NULL, roomname);
726 CtdlFreeMessage(msg);
727 CIT_ICAL->avoid_sending_invitations = 0;
730 free(serialized_event);
736 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
737 * simply extracts the calendar object from the message, deserializes it, and
738 * passes it up to ical_update_my_calendar_with_reply() for processing.
740 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
741 struct CtdlMessage *msg = NULL;
742 struct ical_respond_data ird;
746 (strcasecmp(action, "update"))
747 && (strcasecmp(action, "ignore"))
749 cprintf("%d Action must be 'update' or 'ignore'\n",
750 ERROR + ILLEGAL_VALUE
755 msg = CtdlFetchMessage(msgnum, 1);
757 cprintf("%d Message %ld not found.\n",
758 ERROR + ILLEGAL_VALUE,
764 memset(&ird, 0, sizeof ird);
765 strcpy(ird.desired_partnum, partnum);
766 mime_parser(msg->cm_fields['M'],
768 *ical_locate_part, /* callback function */
770 (void *) &ird, /* user data */
774 /* We're done with the incoming message, because we now have a
775 * calendar object in memory.
777 CtdlFreeMessage(msg);
780 * Here is the real meat of this function. Handle the event.
782 if (ird.cal != NULL) {
783 /* Update the user's calendar if necessary */
784 if (!strcasecmp(action, "update")) {
785 ret = ical_update_my_calendar_with_reply(ird.cal);
787 cprintf("%d Your calendar has been updated with this reply.\n",
791 cprintf("%d This event does not exist in your calendar.\n",
792 ERROR + FILE_NOT_FOUND);
795 cprintf("%d An internal error occurred.\n",
796 ERROR + INTERNAL_ERROR);
800 cprintf("%d This reply has been ignored.\n", CIT_OK);
803 /* Now that we've processed this message, we don't need it
804 * anymore. So delete it. (Don't do this anymore.)
805 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
808 /* Free the memory we allocated and return a response. */
809 icalcomponent_free(ird.cal);
814 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
818 /* should never get here */
823 * Search for a property in both the top level and in a VEVENT subcomponent
825 icalproperty *ical_ctdl_get_subprop(
827 icalproperty_kind which_prop
832 p = icalcomponent_get_first_property(cal, which_prop);
834 c = icalcomponent_get_first_component(cal,
835 ICAL_VEVENT_COMPONENT);
837 p = icalcomponent_get_first_property(c, which_prop);
845 * Check to see if two events overlap. Returns nonzero if they do.
846 * (This function is used in both Citadel and WebCit. If you change it in
847 * one place, change it in the other. Better yet, put it in a library.)
849 int ical_ctdl_is_overlap(
850 struct icaltimetype t1start,
851 struct icaltimetype t1end,
852 struct icaltimetype t2start,
853 struct icaltimetype t2end
856 if (icaltime_is_null_time(t1start)) return(0);
857 if (icaltime_is_null_time(t2start)) return(0);
859 /* First, check for all-day events */
860 if (t1start.is_date) {
861 if (!icaltime_compare_date_only(t1start, t2start)) {
864 if (!icaltime_is_null_time(t2end)) {
865 if (!icaltime_compare_date_only(t1start, t2end)) {
871 if (t2start.is_date) {
872 if (!icaltime_compare_date_only(t2start, t1start)) {
875 if (!icaltime_is_null_time(t1end)) {
876 if (!icaltime_compare_date_only(t2start, t1end)) {
882 /* Now check for overlaps using date *and* time. */
884 /* First, bail out if either event 1 or event 2 is missing end time. */
885 if (icaltime_is_null_time(t1end)) return(0);
886 if (icaltime_is_null_time(t2end)) return(0);
888 /* If event 1 ends before event 2 starts, we're in the clear. */
889 if (icaltime_compare(t1end, t2start) <= 0) return(0);
891 /* If event 2 ends before event 1 starts, we're also ok. */
892 if (icaltime_compare(t2end, t1start) <= 0) return(0);
894 /* Otherwise, they overlap. */
901 * Backend for ical_hunt_for_conflicts()
903 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
905 struct CtdlMessage *msg = NULL;
906 struct ical_respond_data ird;
907 struct icaltimetype t1start, t1end, t2start, t2end;
909 char conflict_event_uid[SIZ];
910 char conflict_event_summary[SIZ];
911 char compare_uid[SIZ];
913 cal = (icalcomponent *)data;
914 strcpy(compare_uid, "");
915 strcpy(conflict_event_uid, "");
916 strcpy(conflict_event_summary, "");
918 msg = CtdlFetchMessage(msgnum, 1);
919 if (msg == NULL) return;
920 memset(&ird, 0, sizeof ird);
921 strcpy(ird.desired_partnum, "_HUNT_");
922 mime_parser(msg->cm_fields['M'],
924 *ical_locate_part, /* callback function */
926 (void *) &ird, /* user data */
929 CtdlFreeMessage(msg);
931 if (ird.cal == NULL) return;
933 t1start = icaltime_null_time();
934 t1end = icaltime_null_time();
935 t2start = icaltime_null_time();
936 t1end = icaltime_null_time();
938 /* Now compare cal to ird.cal */
939 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
940 if (p == NULL) return;
941 if (p != NULL) t2start = icalproperty_get_dtstart(p);
943 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
944 if (p != NULL) t2end = icalproperty_get_dtend(p);
946 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
947 if (p == NULL) return;
948 if (p != NULL) t1start = icalproperty_get_dtstart(p);
950 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
951 if (p != NULL) t1end = icalproperty_get_dtend(p);
953 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
955 strcpy(compare_uid, icalproperty_get_comment(p));
958 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
960 strcpy(conflict_event_uid, icalproperty_get_comment(p));
963 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
965 strcpy(conflict_event_summary, icalproperty_get_comment(p));
968 icalcomponent_free(ird.cal);
970 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
971 cprintf("%ld||%s|%s|%d|\n",
974 conflict_event_summary,
975 ( ((strlen(compare_uid)>0)
976 &&(!strcasecmp(compare_uid,
977 conflict_event_uid))) ? 1 : 0
986 * Phase 2 of "hunt for conflicts" operation.
987 * At this point we have a calendar object which represents the VEVENT that
988 * we're considering adding to the calendar. Now hunt through the user's
989 * calendar room, and output zero or more existing VEVENTs which conflict
992 void ical_hunt_for_conflicts(icalcomponent *cal) {
993 char hold_rm[ROOMNAMELEN];
995 strcpy(hold_rm, CC->room.QRname); /* save current room */
997 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
998 getroom(&CC->room, hold_rm);
999 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1003 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1005 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1008 ical_hunt_for_conflicts_backend,
1013 getroom(&CC->room, hold_rm); /* return to saved room */
1020 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1022 void ical_conflicts(long msgnum, char *partnum) {
1023 struct CtdlMessage *msg = NULL;
1024 struct ical_respond_data ird;
1026 msg = CtdlFetchMessage(msgnum, 1);
1028 cprintf("%d Message %ld not found.\n",
1029 ERROR + ILLEGAL_VALUE,
1035 memset(&ird, 0, sizeof ird);
1036 strcpy(ird.desired_partnum, partnum);
1037 mime_parser(msg->cm_fields['M'],
1039 *ical_locate_part, /* callback function */
1041 (void *) &ird, /* user data */
1045 CtdlFreeMessage(msg);
1047 if (ird.cal != NULL) {
1048 ical_hunt_for_conflicts(ird.cal);
1049 icalcomponent_free(ird.cal);
1053 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1057 /* should never get here */
1063 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1065 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1068 struct icalperiodtype my_period;
1070 if (cal == NULL) return;
1071 my_period = icalperiodtype_null_period();
1073 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1074 ical_add_to_freebusy(fb,
1075 icalcomponent_get_first_component(
1076 cal, ICAL_VEVENT_COMPONENT
1084 /* If this event is not opaque, the user isn't publishing it as
1085 * busy time, so don't bother doing anything else.
1087 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1089 v = icalproperty_get_value(p);
1091 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1097 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1098 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1100 my_period.start = icalproperty_get_dtstart(p);
1103 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1105 my_period.end = icalproperty_get_dtstart(p);
1109 icalcomponent_add_property(fb,
1110 icalproperty_new_freebusy(my_period)
1113 /* Make sure the DTSTART property of the freebusy *list* is set to
1114 * the DTSTART property of the *earliest event*.
1116 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1118 icalcomponent_set_dtstart(fb,
1119 icalcomponent_get_dtstart(cal) );
1122 if (icaltime_compare(
1123 icalcomponent_get_dtstart(cal),
1124 icalcomponent_get_dtstart(fb)
1126 icalcomponent_set_dtstart(fb,
1127 icalcomponent_get_dtstart(cal) );
1131 /* Make sure the DTEND property of the freebusy *list* is set to
1132 * the DTEND property of the *latest event*.
1134 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1136 icalcomponent_set_dtend(fb,
1137 icalcomponent_get_dtend(cal) );
1140 if (icaltime_compare(
1141 icalcomponent_get_dtend(cal),
1142 icalcomponent_get_dtend(fb)
1144 icalcomponent_set_dtend(fb,
1145 icalcomponent_get_dtend(cal) );
1154 * Backend for ical_freebusy()
1156 * This function simply loads the messages in the user's calendar room,
1157 * which contain VEVENTs, then strips them of all non-freebusy data, and
1158 * adds them to the supplied VCALENDAR.
1161 void ical_freebusy_backend(long msgnum, void *data) {
1163 struct CtdlMessage *msg = NULL;
1164 struct ical_respond_data ird;
1166 cal = (icalcomponent *)data;
1168 msg = CtdlFetchMessage(msgnum, 1);
1169 if (msg == NULL) return;
1170 memset(&ird, 0, sizeof ird);
1171 strcpy(ird.desired_partnum, "_HUNT_");
1172 mime_parser(msg->cm_fields['M'],
1174 *ical_locate_part, /* callback function */
1176 (void *) &ird, /* user data */
1179 CtdlFreeMessage(msg);
1181 if (ird.cal == NULL) return;
1183 ical_add_to_freebusy(cal, ird.cal);
1185 /* Now free the memory. */
1186 icalcomponent_free(ird.cal);
1192 * Grab another user's free/busy times
1194 void ical_freebusy(char *who) {
1195 struct ctdluser usbuf;
1196 char calendar_room_name[ROOMNAMELEN];
1197 char hold_rm[ROOMNAMELEN];
1198 char *serialized_request = NULL;
1199 icalcomponent *encaps = NULL;
1200 icalcomponent *fb = NULL;
1201 int found_user = (-1);
1202 struct recptypes *recp = NULL;
1207 int config_lines = 0;
1209 /* First try an exact match. */
1210 found_user = getuser(&usbuf, who);
1212 /* If not found, try it as an unqualified email address. */
1213 if (found_user != 0) {
1215 recp = validate_recipients(buf);
1216 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1218 if (recp->num_local == 1) {
1219 found_user = getuser(&usbuf, recp->recp_local);
1221 free_recipients(recp);
1225 /* If still not found, try it as an address qualified with the
1226 * primary FQDN of this Citadel node.
1228 if (found_user != 0) {
1229 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1230 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1231 recp = validate_recipients(buf);
1233 if (recp->num_local == 1) {
1234 found_user = getuser(&usbuf, recp->recp_local);
1236 free_recipients(recp);
1240 /* Still not found? Try qualifying it with every domain we
1241 * might have addresses in.
1243 if (found_user != 0) {
1244 config_lines = num_tokens(inetcfg, '\n');
1245 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1246 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1247 extract_token(host, buf, 0, '|', sizeof host);
1248 extract_token(type, buf, 1, '|', sizeof type);
1250 if ( (!strcasecmp(type, "localhost"))
1251 || (!strcasecmp(type, "directory")) ) {
1252 snprintf(buf, sizeof buf, "%s@%s", who, host);
1253 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1254 recp = validate_recipients(buf);
1256 if (recp->num_local == 1) {
1257 found_user = getuser(&usbuf, recp->recp_local);
1259 free_recipients(recp);
1265 if (found_user != 0) {
1266 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1270 MailboxName(calendar_room_name, sizeof calendar_room_name,
1271 &usbuf, USERCALENDARROOM);
1273 strcpy(hold_rm, CC->room.QRname); /* save current room */
1275 if (getroom(&CC->room, calendar_room_name) != 0) {
1276 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1277 getroom(&CC->room, hold_rm);
1281 /* Create a VFREEBUSY subcomponent */
1282 lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1283 fb = icalcomponent_new_vfreebusy();
1285 cprintf("%d Internal error: cannot allocate memory.\n",
1286 ERROR + INTERNAL_ERROR);
1287 icalcomponent_free(encaps);
1288 getroom(&CC->room, hold_rm);
1292 /* Set the method to PUBLISH */
1293 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1295 /* Set the DTSTAMP to right now. */
1296 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1298 /* Add the user's email address as ORGANIZER */
1299 sprintf(buf, "MAILTO:%s", who);
1300 if (strchr(buf, '@') == NULL) {
1302 strcat(buf, config.c_fqdn);
1304 for (i=0; buf[i]; ++i) {
1305 if (buf[i]==' ') buf[i] = '_';
1307 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1309 /* Add busy time from events */
1310 lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1311 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1313 /* If values for DTSTART and DTEND are still not present, set them
1314 * to yesterday and tomorrow as default values.
1316 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1317 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1319 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1320 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1323 /* Put the freebusy component into the calendar component */
1324 lprintf(CTDL_DEBUG, "Encapsulating\n");
1325 encaps = ical_encapsulate_subcomponent(fb);
1326 if (encaps == NULL) {
1327 icalcomponent_free(fb);
1328 cprintf("%d Internal error: cannot allocate memory.\n",
1329 ERROR + INTERNAL_ERROR);
1330 getroom(&CC->room, hold_rm);
1334 /* Set the method to PUBLISH */
1335 lprintf(CTDL_DEBUG, "Setting method\n");
1336 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1339 lprintf(CTDL_DEBUG, "Serializing\n");
1340 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1341 icalcomponent_free(encaps); /* Don't need this anymore. */
1343 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1344 if (serialized_request != NULL) {
1345 client_write(serialized_request, strlen(serialized_request));
1346 free(serialized_request);
1350 /* Go back to the room from which we came... */
1351 getroom(&CC->room, hold_rm);
1357 * Backend for ical_getics()
1359 * This is a ForEachMessage() callback function that searches the current room
1360 * for calendar events and adds them each into one big calendar component.
1362 void ical_getics_backend(long msgnum, void *data) {
1363 icalcomponent *encaps, *c;
1364 struct CtdlMessage *msg = NULL;
1365 struct ical_respond_data ird;
1367 encaps = (icalcomponent *)data;
1368 if (encaps == NULL) return;
1370 /* Look for the calendar event... */
1372 msg = CtdlFetchMessage(msgnum, 1);
1373 if (msg == NULL) return;
1374 memset(&ird, 0, sizeof ird);
1375 strcpy(ird.desired_partnum, "_HUNT_");
1376 mime_parser(msg->cm_fields['M'],
1378 *ical_locate_part, /* callback function */
1380 (void *) &ird, /* user data */
1383 CtdlFreeMessage(msg);
1385 if (ird.cal == NULL) return;
1387 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1388 * are responsible for "the_request"'s memory -- it will be freed
1389 * when we free "encaps".
1392 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1393 * in. This will almost never happen.
1395 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1396 icalcomponent_add_component(encaps, ird.cal);
1399 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1400 * and other components encapsulated inside, we have to extract them.
1403 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1405 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1406 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1408 icalcomponent_free(ird.cal);
1415 * Retrieve all of the calendar items in the current room, and output them
1416 * as a single icalendar object.
1418 void ical_getics(void)
1420 icalcomponent *encaps = NULL;
1423 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1424 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1425 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1426 return; /* Not a vCalendar-centric room */
1429 encaps = icalcomponent_new_vcalendar();
1430 if (encaps == NULL) {
1431 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1432 __FILE__, __LINE__);
1433 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1437 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1439 /* Set the Product ID */
1440 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1442 /* Set the Version Number */
1443 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1445 /* Set the method to PUBLISH */
1446 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1448 /* Now go through the room encapsulating all calendar items. */
1449 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1452 ical_getics_backend,
1456 ser = strdup(icalcomponent_as_ical_string(encaps));
1457 client_write(ser, strlen(ser));
1460 icalcomponent_free(encaps); /* Don't need this anymore. */
1466 * Delete all of the calendar items in the current room, and replace them
1467 * with calendar items from a client-supplied data stream.
1469 void ical_putics(void)
1471 char *calstream = NULL;
1475 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1476 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1477 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1478 return; /* Not a vCalendar-centric room */
1481 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1482 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1486 cprintf("%d Transmit data now\n", SEND_LISTING);
1487 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1488 if (calstream == NULL) {
1492 cal = icalcomponent_new_from_string(calstream);
1496 /* We got our data stream -- now do something with it. */
1498 /* Delete the existing messages in the room, because we are replacing
1499 * the entire calendar with an entire new (or updated) calendar.
1500 * (Careful: this opens an S_ROOMS critical section!)
1502 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1504 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1505 * in. This will almost never happen.
1507 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1508 ical_write_to_cal(NULL, cal);
1511 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1512 * and other components encapsulated inside, we have to extract them.
1515 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1517 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1518 ical_write_to_cal(NULL, c);
1522 icalcomponent_free(cal);
1527 * All Citadel calendar commands from the client come through here.
1529 void cmd_ical(char *argbuf)
1537 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1539 /* Allow "test" and "freebusy" subcommands without logging in. */
1541 if (!strcasecmp(subcmd, "test")) {
1542 cprintf("%d This server supports calendaring\n", CIT_OK);
1546 if (!strcasecmp(subcmd, "freebusy")) {
1547 extract_token(who, argbuf, 1, '|', sizeof who);
1552 if (!strcasecmp(subcmd, "sgi")) {
1553 CIT_ICAL->server_generated_invitations =
1554 (extract_int(argbuf, 1) ? 1 : 0) ;
1556 CIT_OK, CIT_ICAL->server_generated_invitations);
1560 if (CtdlAccessCheck(ac_logged_in)) return;
1562 if (!strcasecmp(subcmd, "respond")) {
1563 msgnum = extract_long(argbuf, 1);
1564 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1565 extract_token(action, argbuf, 3, '|', sizeof action);
1566 ical_respond(msgnum, partnum, action);
1570 if (!strcasecmp(subcmd, "handle_rsvp")) {
1571 msgnum = extract_long(argbuf, 1);
1572 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1573 extract_token(action, argbuf, 3, '|', sizeof action);
1574 ical_handle_rsvp(msgnum, partnum, action);
1578 if (!strcasecmp(subcmd, "conflicts")) {
1579 msgnum = extract_long(argbuf, 1);
1580 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1581 ical_conflicts(msgnum, partnum);
1585 if (!strcasecmp(subcmd, "getics")) {
1590 if (!strcasecmp(subcmd, "putics")) {
1595 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1601 * We don't know if the calendar room exists so we just create it at login
1603 void ical_create_room(void)
1608 /* Create the calendar room if it doesn't already exist */
1609 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1611 /* Set expiration policy to manual; otherwise objects will be lost! */
1612 if (lgetroom(&qr, USERCALENDARROOM)) {
1613 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1616 qr.QRep.expire_mode = EXPIRE_MANUAL;
1617 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1620 /* Set the view to a calendar view */
1621 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1622 vbuf.v_view = VIEW_CALENDAR;
1623 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1625 /* Create the tasks list room if it doesn't already exist */
1626 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1628 /* Set expiration policy to manual; otherwise objects will be lost! */
1629 if (lgetroom(&qr, USERTASKSROOM)) {
1630 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1633 qr.QRep.expire_mode = EXPIRE_MANUAL;
1634 qr.QRdefaultview = VIEW_TASKS;
1637 /* Set the view to a task list view */
1638 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1639 vbuf.v_view = VIEW_TASKS;
1640 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1642 /* Create the notes room if it doesn't already exist */
1643 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1645 /* Set expiration policy to manual; otherwise objects will be lost! */
1646 if (lgetroom(&qr, USERNOTESROOM)) {
1647 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1650 qr.QRep.expire_mode = EXPIRE_MANUAL;
1651 qr.QRdefaultview = VIEW_NOTES;
1654 /* Set the view to a notes view */
1655 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1656 vbuf.v_view = VIEW_NOTES;
1657 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1664 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1667 void ical_send_out_invitations(icalcomponent *cal) {
1668 icalcomponent *the_request = NULL;
1669 char *serialized_request = NULL;
1670 icalcomponent *encaps = NULL;
1671 char *request_message_text = NULL;
1672 struct CtdlMessage *msg = NULL;
1673 struct recptypes *valid = NULL;
1674 char attendees_string[SIZ];
1675 int num_attendees = 0;
1676 char this_attendee[256];
1677 icalproperty *attendee = NULL;
1678 char summary_string[SIZ];
1679 icalproperty *summary = NULL;
1682 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1687 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1688 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1689 ical_send_out_invitations(
1690 icalcomponent_get_first_component(
1691 cal, ICAL_VEVENT_COMPONENT
1697 /* Clone the event */
1698 the_request = icalcomponent_new_clone(cal);
1699 if (the_request == NULL) {
1700 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1704 /* Extract the summary string -- we'll use it as the
1705 * message subject for the request
1707 strcpy(summary_string, "Meeting request");
1708 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1709 if (summary != NULL) {
1710 if (icalproperty_get_summary(summary)) {
1711 strcpy(summary_string,
1712 icalproperty_get_summary(summary) );
1716 /* Determine who the recipients of this message are (the attendees) */
1717 strcpy(attendees_string, "");
1718 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1719 if (icalproperty_get_attendee(attendee)) {
1720 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1721 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1722 strcpy(this_attendee, &this_attendee[7]);
1724 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1725 snprintf(&attendees_string[strlen(attendees_string)],
1726 sizeof(attendees_string) - strlen(attendees_string),
1736 lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1738 /* If there are no attendees, there are no invitations to send, so...
1739 * don't bother putting one together! Punch out, Maverick!
1741 if (num_attendees == 0) {
1742 icalcomponent_free(the_request);
1746 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1747 encaps = icalcomponent_new_vcalendar();
1748 if (encaps == NULL) {
1749 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1750 __FILE__, __LINE__);
1751 icalcomponent_free(the_request);
1755 /* Set the Product ID */
1756 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1758 /* Set the Version Number */
1759 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1761 /* Set the method to REQUEST */
1762 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1764 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1765 ical_dezonify(the_request);
1767 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1768 * are responsible for "the_request"'s memory -- it will be freed
1769 * when we free "encaps".
1771 icalcomponent_add_component(encaps, the_request);
1774 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1775 icalcomponent_free(encaps); /* Don't need this anymore. */
1776 if (serialized_request == NULL) return;
1778 request_message_text = malloc(strlen(serialized_request) + SIZ);
1779 if (request_message_text != NULL) {
1780 sprintf(request_message_text,
1781 "Content-type: text/calendar\r\n\r\n%s\r\n",
1785 msg = CtdlMakeMessage(&CC->user,
1786 "", /* No single recipient here */
1787 "", /* No single recipient here */
1788 CC->room.QRname, 0, FMT_RFC822,
1791 summary_string, /* Use summary for subject */
1793 request_message_text);
1796 valid = validate_recipients(attendees_string);
1797 CtdlSubmitMsg(msg, valid, "");
1798 CtdlFreeMessage(msg);
1799 free_recipients(valid);
1802 free(serialized_request);
1807 * When a calendar object is being saved, determine whether it's a VEVENT
1808 * and the user saving it is the organizer. If so, send out invitations
1809 * to any listed attendees.
1812 void ical_saving_vevent(icalcomponent *cal) {
1814 icalproperty *organizer = NULL;
1815 char organizer_string[SIZ];
1817 lprintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
1819 /* Don't send out invitations unless the client wants us to. */
1820 if (CIT_ICAL->server_generated_invitations == 0) {
1824 /* Don't send out invitations if we've been asked not to. */
1825 if (CIT_ICAL->avoid_sending_invitations > 0) {
1829 strcpy(organizer_string, "");
1831 * The VEVENT subcomponent is the one we're interested in.
1832 * Send out invitations if, and only if, this user is the Organizer.
1834 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1835 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
1836 if (organizer != NULL) {
1837 if (icalproperty_get_organizer(organizer)) {
1838 strcpy(organizer_string,
1839 icalproperty_get_organizer(organizer));
1842 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1843 strcpy(organizer_string, &organizer_string[7]);
1844 striplt(organizer_string);
1846 * If the user saving the event is listed as the
1847 * organizer, then send out invitations.
1849 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1850 ical_send_out_invitations(cal);
1855 /* If the component has subcomponents, recurse through them. */
1856 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1858 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1859 /* Recursively process subcomponent */
1860 ical_saving_vevent(c);
1868 * Back end for ical_obj_beforesave()
1869 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1870 * the summary of the event (becomes message subject),
1871 * and the start time (becomes message date/time).
1873 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1874 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1875 char *encoding, void *cbuserdata)
1877 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
1879 struct icalmessagemod *imm;
1882 imm = (struct icalmessagemod *)cbuserdata;
1884 /* We're only interested in calendar data. */
1885 if (strcasecmp(cbtype, "text/calendar")) {
1889 /* Hunt for the UID and drop it in
1890 * the "user data" pointer for the MIME parser. When
1891 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1894 whole_cal = icalcomponent_new_from_string(content);
1897 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1898 nested_event = icalcomponent_get_first_component(
1899 cal, ICAL_VEVENT_COMPONENT);
1900 if (nested_event != NULL) {
1904 nested_todo = icalcomponent_get_first_component(
1905 cal, ICAL_VTODO_COMPONENT);
1906 if (nested_todo != NULL) {
1913 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1915 /* If there's no uid we must generate one */
1916 generate_uuid(new_uid);
1917 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1918 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1921 strcpy(imm->uid, icalproperty_get_comment(p));
1923 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1925 strcpy(imm->subject, icalproperty_get_comment(p));
1927 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1929 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1932 icalcomponent_free(cal);
1933 if (whole_cal != cal) {
1934 icalcomponent_free(whole_cal);
1943 * See if we need to prevent the object from being saved (we don't allow
1944 * MIME types other than text/calendar in "calendar" or "tasks" rooms). Also,
1945 * when saving an event to the calendar, set the message's Citadel exclusive
1946 * message ID to the UID of the object. This causes our replication checker to
1947 * automatically delete any existing instances of the same object. (Isn't
1950 * We also set the message's Subject to the event summary, and the Date/time to
1951 * the event start time.
1953 int ical_obj_beforesave(struct CtdlMessage *msg)
1955 struct icalmessagemod imm;
1957 /* First determine if this is a calendar or tasks room */
1958 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1959 && (CC->room.QRdefaultview != VIEW_TASKS)
1961 return(0); /* Not a vCalendar-centric room */
1964 /* It must be an RFC822 message! */
1965 if (msg->cm_format_type != 4) {
1966 lprintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
1967 return(1); /* You tried to save a non-RFC822 message! */
1970 if (msg->cm_fields['M'] == NULL) {
1971 return(1); /* You tried to save a null message! */
1974 memset(&imm, 0, sizeof(struct icalmessagemod));
1976 /* Do all of our lovely back-end parsing */
1977 mime_parser(msg->cm_fields['M'],
1979 *ical_ctdl_set_exclusive_msgid,
1985 if (!IsEmptyStr(imm.uid)) {
1986 if (msg->cm_fields['E'] != NULL) {
1987 free(msg->cm_fields['E']);
1989 msg->cm_fields['E'] = strdup(imm.uid);
1990 lprintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", msg->cm_fields['E']);
1992 if (!IsEmptyStr(imm.subject)) {
1993 if (msg->cm_fields['U'] != NULL) {
1994 free(msg->cm_fields['U']);
1996 msg->cm_fields['U'] = strdup(imm.subject);
1998 if (imm.dtstart > 0) {
1999 if (msg->cm_fields['T'] != NULL) {
2000 free(msg->cm_fields['T']);
2002 msg->cm_fields['T'] = strdup("000000000000000000");
2003 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
2011 * Things we need to do after saving a calendar event.
2013 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2014 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2015 char *encoding, void *cbuserdata)
2019 /* We're only interested in calendar items here. */
2020 if (strcasecmp(cbtype, "text/calendar")) {
2024 /* Hunt for the UID and drop it in
2025 * the "user data" pointer for the MIME parser. When
2026 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2029 if (!strcasecmp(cbtype, "text/calendar")) {
2030 cal = icalcomponent_new_from_string(content);
2032 ical_saving_vevent(cal);
2033 icalcomponent_free(cal);
2040 * Things we need to do after saving a calendar event.
2041 * (This will start back end tasks such as automatic generation of invitations,
2042 * if such actions are appropriate.)
2044 int ical_obj_aftersave(struct CtdlMessage *msg)
2046 char roomname[ROOMNAMELEN];
2049 * If this isn't the Calendar> room, no further action is necessary.
2052 /* First determine if this is our room */
2053 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2054 if (strcasecmp(roomname, CC->room.QRname)) {
2055 return(0); /* Not the Calendar room -- don't do anything. */
2058 /* It must be an RFC822 message! */
2059 if (msg->cm_format_type != 4) return(1);
2061 /* Reject null messages */
2062 if (msg->cm_fields['M'] == NULL) return(1);
2064 /* Now recurse through it looking for our icalendar data */
2065 mime_parser(msg->cm_fields['M'],
2067 *ical_obj_aftersave_backend,
2077 void ical_session_startup(void) {
2078 CIT_ICAL = malloc(sizeof(struct cit_ical));
2079 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2082 void ical_session_shutdown(void) {
2088 * Back end for ical_fixed_output()
2090 void ical_fixed_output_backend(icalcomponent *cal,
2097 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2099 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2102 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2104 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2107 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2109 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2112 /* If the component has attendees, iterate through them. */
2113 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2114 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2115 if (!strncasecmp(buf, "MAILTO:", 7)) {
2117 /* screen name or email address */
2118 strcpy(buf, &buf[7]);
2120 cprintf("%s ", buf);
2125 /* If the component has subcomponents, recurse through them. */
2126 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2128 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2129 /* Recursively process subcomponent */
2130 ical_fixed_output_backend(c, recursion_level+1);
2137 * Function to output vcalendar data as plain text. Nobody uses MSG0
2138 * anymore, so really this is just so we expose the vCard data to the full
2141 void ical_fixed_output(char *ptr, int len) {
2145 stringy_cal = malloc(len + 1);
2146 safestrncpy(stringy_cal, ptr, len + 1);
2147 cal = icalcomponent_new_from_string(stringy_cal);
2155 ical_fixed_output_backend(cal, 0);
2157 /* Free the memory we obtained from libical's constructor */
2158 icalcomponent_free(cal);
2162 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
2165 * Register this module with the Citadel server.
2167 CTDL_MODULE_INIT(calendar)
2169 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2170 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2171 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2172 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2173 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2174 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2175 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2176 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2179 /* return our Subversion id for the Log */
2185 void serv_calendar_destroy(void)
2187 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2188 icaltimezone_free_builtin_timezones();