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>
22 #include <libical/ical.h>
23 #include <libcitadel.h>
26 #include "citserver.h"
32 #include "internet_addressing.h"
33 #include "serv_calendar.h"
34 #include "euidindex.h"
35 #include "ctdl_module.h"
36 #include "ical_dezonify.h"
40 struct ical_respond_data {
41 char desired_partnum[SIZ];
47 * Utility function to create a new VCALENDAR component with some of the
48 * required fields already set the way we like them.
50 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
51 icalcomponent *encaps;
53 encaps = icalcomponent_new_vcalendar();
55 CtdlLogPrintf(CTDL_CRIT, "ERROR: could not allocate component!\n");
59 /* Set the Product ID */
60 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
62 /* Set the Version Number */
63 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
70 * Utility function to encapsulate a subcomponent into a full VCALENDAR
72 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
73 icalcomponent *encaps;
75 /* If we're already looking at a full VCALENDAR component,
76 * don't bother ... just return itself.
78 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
82 /* Encapsulate the VEVENT component into a complete VCALENDAR */
83 encaps = icalcomponent_new_citadel_vcalendar();
84 if (encaps == NULL) return NULL;
86 /* Encapsulate the subcomponent inside */
87 icalcomponent_add_component(encaps, subcomp);
89 /* Return the object we just created. */
97 * Write a calendar object into the specified user's calendar room.
98 * If the supplied user is NULL, this function writes the calendar object
99 * to the currently selected room.
101 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
103 icalcomponent *encaps = NULL;
104 struct CtdlMessage *msg = NULL;
105 icalcomponent *tmp=NULL;
107 if (cal == NULL) return;
109 /* If the supplied object is a subcomponent, encapsulate it in
110 * a full VCALENDAR component, and save that instead.
112 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
113 tmp = icalcomponent_new_clone(cal);
114 encaps = ical_encapsulate_subcomponent(tmp);
115 ical_write_to_cal(u, encaps);
116 icalcomponent_free(tmp);
117 icalcomponent_free(encaps);
121 ser = icalcomponent_as_ical_string_r(cal);
122 if (ser == NULL) return;
124 /* If the caller supplied a user, write to that user's default calendar room */
126 /* This handy API function does all the work for us. */
127 CtdlWriteObject(USERCALENDARROOM, /* which room */
128 "text/calendar", /* MIME type */
130 strlen(ser)+1, /* length */
133 0, /* don't delete others of this type */
138 /* If the caller did not supply a user, write to the currently selected room */
140 msg = malloc(sizeof(struct CtdlMessage));
141 memset(msg, 0, sizeof(struct CtdlMessage));
142 msg->cm_magic = CTDLMESSAGE_MAGIC;
143 msg->cm_anon_type = MES_NORMAL;
144 msg->cm_format_type = 4;
145 msg->cm_fields['A'] = strdup(CC->user.fullname);
146 msg->cm_fields['O'] = strdup(CC->room.QRname);
147 msg->cm_fields['N'] = strdup(config.c_nodename);
148 msg->cm_fields['H'] = strdup(config.c_humannode);
149 msg->cm_fields['M'] = malloc(strlen(ser) + 40);
150 strcpy(msg->cm_fields['M'], "Content-type: text/calendar\r\n\r\n");
151 strcat(msg->cm_fields['M'], ser);
153 /* Now write the data */
154 CtdlSubmitMsg(msg, NULL, "", QP_EADDR);
155 CtdlFreeMessage(msg);
158 /* In either case, now we can free the serialized calendar object */
164 * Send a reply to a meeting invitation.
166 * 'request' is the invitation to reply to.
167 * 'action' is the string "accept" or "decline" or "tentative".
170 void ical_send_a_reply(icalcomponent *request, char *action) {
171 icalcomponent *the_reply = NULL;
172 icalcomponent *vevent = NULL;
173 icalproperty *attendee = NULL;
174 char attendee_string[SIZ];
175 icalproperty *organizer = NULL;
176 char organizer_string[SIZ];
177 icalproperty *summary = NULL;
178 char summary_string[SIZ];
179 icalproperty *me_attend = NULL;
180 struct recptypes *recp = NULL;
181 icalparameter *partstat = NULL;
182 char *serialized_reply = NULL;
183 char *reply_message_text = NULL;
184 struct CtdlMessage *msg = NULL;
185 struct recptypes *valid = NULL;
187 strcpy(organizer_string, "");
188 strcpy(summary_string, "Calendar item");
190 if (request == NULL) {
191 CtdlLogPrintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
195 the_reply = icalcomponent_new_clone(request);
196 if (the_reply == NULL) {
197 CtdlLogPrintf(CTDL_ERR, "ERROR: cannot clone request\n");
201 /* Change the method from REQUEST to REPLY */
202 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
204 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
205 if (vevent != NULL) {
206 /* Hunt for attendees, removing ones that aren't us.
207 * (Actually, remove them all, cloning our own one so we can
208 * re-insert it later)
210 while (attendee = icalcomponent_get_first_property(vevent,
211 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
213 if (icalproperty_get_attendee(attendee)) {
214 strcpy(attendee_string,
215 icalproperty_get_attendee(attendee) );
216 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
217 strcpy(attendee_string, &attendee_string[7]);
218 striplt(attendee_string);
219 recp = validate_recipients(attendee_string, NULL, 0);
221 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
222 if (me_attend) icalproperty_free(me_attend);
223 me_attend = icalproperty_new_clone(attendee);
225 free_recipients(recp);
230 icalcomponent_remove_property(vevent, attendee);
231 icalproperty_free(attendee);
234 /* We found our own address in the attendee list. */
236 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
237 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
239 if (!strcasecmp(action, "accept")) {
240 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
242 else if (!strcasecmp(action, "decline")) {
243 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
245 else if (!strcasecmp(action, "tentative")) {
246 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
249 if (partstat) icalproperty_add_parameter(me_attend, partstat);
251 /* Now insert it back into the vevent. */
252 icalcomponent_add_property(vevent, me_attend);
255 /* Figure out who to send this thing to */
256 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
257 if (organizer != NULL) {
258 if (icalproperty_get_organizer(organizer)) {
259 strcpy(organizer_string,
260 icalproperty_get_organizer(organizer) );
263 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
264 strcpy(organizer_string, &organizer_string[7]);
265 striplt(organizer_string);
267 strcpy(organizer_string, "");
270 /* Extract the summary string -- we'll use it as the
271 * message subject for the reply
273 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
274 if (summary != NULL) {
275 if (icalproperty_get_summary(summary)) {
276 strcpy(summary_string,
277 icalproperty_get_summary(summary) );
282 /* Now generate the reply message and send it out. */
283 serialized_reply = icalcomponent_as_ical_string_r(the_reply);
284 icalcomponent_free(the_reply); /* don't need this anymore */
285 if (serialized_reply == NULL) return;
287 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
288 if (reply_message_text != NULL) {
289 sprintf(reply_message_text,
290 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
294 msg = CtdlMakeMessage(&CC->user,
295 organizer_string, /* to */
297 CC->room.QRname, 0, FMT_RFC822,
300 summary_string, /* Use summary for subject */
306 valid = validate_recipients(organizer_string, NULL, 0);
307 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
308 CtdlFreeMessage(msg);
309 free_recipients(valid);
312 free(serialized_reply);
318 * Callback function for mime parser that hunts for calendar content types
319 * and turns them into calendar objects
321 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
322 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
323 char *cbid, void *cbuserdata) {
325 struct ical_respond_data *ird = NULL;
327 ird = (struct ical_respond_data *) cbuserdata;
329 /* desired_partnum can be set to "_HUNT_" to have it just look for
330 * the first part with a content type of text/calendar. Otherwise
331 * we have to only process the right one.
333 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
334 if (strcasecmp(partnum, ird->desired_partnum)) {
339 if ( (strcasecmp(cbtype, "text/calendar"))
340 && (strcasecmp(cbtype, "application/ics")) ) {
344 if (ird->cal != NULL) {
345 icalcomponent_free(ird->cal);
349 ird->cal = icalcomponent_new_from_string(content);
354 * Respond to a meeting request.
356 void ical_respond(long msgnum, char *partnum, char *action) {
357 struct CtdlMessage *msg = NULL;
358 struct ical_respond_data ird;
361 (strcasecmp(action, "accept"))
362 && (strcasecmp(action, "decline"))
364 cprintf("%d Action must be 'accept' or 'decline'\n",
365 ERROR + ILLEGAL_VALUE
370 msg = CtdlFetchMessage(msgnum, 1);
372 cprintf("%d Message %ld not found.\n",
373 ERROR + ILLEGAL_VALUE,
379 memset(&ird, 0, sizeof ird);
380 strcpy(ird.desired_partnum, partnum);
381 mime_parser(msg->cm_fields['M'],
383 *ical_locate_part, /* callback function */
385 (void *) &ird, /* user data */
389 /* We're done with the incoming message, because we now have a
390 * calendar object in memory.
392 CtdlFreeMessage(msg);
395 * Here is the real meat of this function. Handle the event.
397 if (ird.cal != NULL) {
398 /* Save this in the user's calendar if necessary */
399 if (!strcasecmp(action, "accept")) {
400 ical_write_to_cal(&CC->user, ird.cal);
403 /* Send a reply if necessary */
404 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
405 ical_send_a_reply(ird.cal, action);
408 /* We used to delete the invitation after handling it.
409 * We don't do that anymore, but here is the code that handled it:
410 * CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
413 /* Free the memory we allocated and return a response. */
414 icalcomponent_free(ird.cal);
416 cprintf("%d ok\n", CIT_OK);
420 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
424 /* should never get here */
429 * Figure out the UID of the calendar event being referred to in a
430 * REPLY object. This function is recursive.
432 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
433 icalcomponent *subcomponent;
436 /* If this object is a REPLY, then extract the UID. */
437 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
438 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
440 strcpy(uidbuf, icalproperty_get_comment(p));
444 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
445 * UID of the reply; we want the UID of the invitation being replied to.
447 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
448 subcomponent != NULL;
449 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
450 ical_learn_uid_of_reply(uidbuf, subcomponent);
456 * ical_update_my_calendar_with_reply() refers to this callback function; when we
457 * locate the message containing the calendar event we're replying to, this function
458 * gets called. It basically just sticks the message number in a supplied buffer.
460 void ical_hunt_for_event_to_update(long msgnum, void *data) {
463 msgnumptr = (long *) data;
468 struct original_event_container {
473 * Callback function for mime parser that hunts for calendar content types
474 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
475 * to fetch the object being updated)
477 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
478 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
479 char *cbid, void *cbuserdata) {
481 struct original_event_container *oec = NULL;
483 if ( (strcasecmp(cbtype, "text/calendar"))
484 && (strcasecmp(cbtype, "application/ics")) ) {
487 oec = (struct original_event_container *) cbuserdata;
488 if (oec->c != NULL) {
489 icalcomponent_free(oec->c);
491 oec->c = icalcomponent_new_from_string(content);
496 * Merge updated attendee information from a REPLY into an existing event.
498 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
500 icalproperty *e_attendee, *r_attendee;
502 /* First things first. If we're not looking at a VEVENT component,
503 * recurse through subcomponents until we find one.
505 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
506 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
508 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
509 ical_merge_attendee_reply(c, reply);
514 /* Now do the same thing with the reply.
516 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
517 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
519 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
520 ical_merge_attendee_reply(event, c);
525 /* Clone the reply, because we're going to rip its guts out. */
526 reply = icalcomponent_new_clone(reply);
528 /* At this point we're looking at the correct subcomponents.
529 * Iterate through the attendees looking for a match.
532 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
534 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
536 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
538 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
540 /* Check to see if these two attendees match...
543 icalproperty_get_attendee(e_attendee),
544 icalproperty_get_attendee(r_attendee)
546 /* ...and if they do, remove the attendee from the event
547 * and replace it with the attendee from the reply. (The
548 * reply's copy will have the same address, but an updated
551 icalcomponent_remove_property(event, e_attendee);
552 icalproperty_free(e_attendee);
553 icalcomponent_remove_property(reply, r_attendee);
554 icalcomponent_add_property(event, r_attendee);
556 /* Since we diddled both sets of attendees, we have to start
557 * the iteration over again. This will not create an infinite
558 * loop because we removed the attendee from the reply. (That's
559 * why we cloned the reply, and that's what we mean by "ripping
568 /* Free the *clone* of the reply. */
569 icalcomponent_free(reply);
576 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
577 * calendar event. The object has already been deserialized for us; all
578 * we have to do here is hunt for the event in our calendar, merge in the
579 * updated attendee status, and save it again.
581 * This function returns 0 on success, 1 if the event was not found in the
582 * user's calendar, or 2 if an internal error occurred.
584 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
586 char hold_rm[ROOMNAMELEN];
587 long msgnum_being_replaced = 0;
588 struct CtdlMessage *msg = NULL;
589 struct original_event_container oec;
590 icalcomponent *original_event;
591 char *serialized_event = NULL;
592 char roomname[ROOMNAMELEN];
593 char *message_text = NULL;
595 /* Figure out just what event it is we're dealing with */
596 strcpy(uid, "--==<< InVaLiD uId >>==--");
597 ical_learn_uid_of_reply(uid, cal);
598 CtdlLogPrintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
600 strcpy(hold_rm, CC->room.QRname); /* save current room */
602 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
603 getroom(&CC->room, hold_rm);
604 CtdlLogPrintf(CTDL_CRIT, "cannot get user calendar room\n");
609 * Look in the EUID index for a message with
610 * the Citadel EUID set to the value we're looking for. Since
611 * Citadel always sets the message EUID to the iCalendar UID of
612 * the event, this will work.
614 msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
616 getroom(&CC->room, hold_rm); /* return to saved room */
618 CtdlLogPrintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
619 if (msgnum_being_replaced == 0) {
620 return(1); /* no calendar event found */
623 /* Now we know the ID of the message containing the event being updated.
624 * We don't actually have to delete it; that'll get taken care of by the
625 * server when we save another event with the same UID. This just gives
626 * us the ability to load the event into memory so we can diddle the
629 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
631 return(2); /* internal error */
634 mime_parser(msg->cm_fields['M'],
636 *ical_locate_original_event, /* callback function */
638 &oec, /* user data */
641 CtdlFreeMessage(msg);
643 original_event = oec.c;
644 if (original_event == NULL) {
645 CtdlLogPrintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
649 /* Merge the attendee's updated status into the event */
650 ical_merge_attendee_reply(original_event, cal);
653 serialized_event = icalcomponent_as_ical_string_r(original_event);
654 icalcomponent_free(original_event); /* Don't need this anymore. */
655 if (serialized_event == NULL) return(2);
657 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
659 message_text = malloc(strlen(serialized_event) + SIZ);
660 if (message_text != NULL) {
661 sprintf(message_text,
662 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
666 msg = CtdlMakeMessage(&CC->user,
667 "", /* No recipient */
668 "", /* No recipient */
679 CIT_ICAL->avoid_sending_invitations = 1;
680 CtdlSubmitMsg(msg, NULL, roomname, QP_EADDR);
681 CtdlFreeMessage(msg);
682 CIT_ICAL->avoid_sending_invitations = 0;
685 free(serialized_event);
691 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
692 * simply extracts the calendar object from the message, deserializes it, and
693 * passes it up to ical_update_my_calendar_with_reply() for processing.
695 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
696 struct CtdlMessage *msg = NULL;
697 struct ical_respond_data ird;
701 (strcasecmp(action, "update"))
702 && (strcasecmp(action, "ignore"))
704 cprintf("%d Action must be 'update' or 'ignore'\n",
705 ERROR + ILLEGAL_VALUE
710 msg = CtdlFetchMessage(msgnum, 1);
712 cprintf("%d Message %ld not found.\n",
713 ERROR + ILLEGAL_VALUE,
719 memset(&ird, 0, sizeof ird);
720 strcpy(ird.desired_partnum, partnum);
721 mime_parser(msg->cm_fields['M'],
723 *ical_locate_part, /* callback function */
725 (void *) &ird, /* user data */
729 /* We're done with the incoming message, because we now have a
730 * calendar object in memory.
732 CtdlFreeMessage(msg);
735 * Here is the real meat of this function. Handle the event.
737 if (ird.cal != NULL) {
738 /* Update the user's calendar if necessary */
739 if (!strcasecmp(action, "update")) {
740 ret = ical_update_my_calendar_with_reply(ird.cal);
742 cprintf("%d Your calendar has been updated with this reply.\n",
746 cprintf("%d This event does not exist in your calendar.\n",
747 ERROR + FILE_NOT_FOUND);
750 cprintf("%d An internal error occurred.\n",
751 ERROR + INTERNAL_ERROR);
755 cprintf("%d This reply has been ignored.\n", CIT_OK);
758 /* Now that we've processed this message, we don't need it
759 * anymore. So delete it. (Don't do this anymore.)
760 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
763 /* Free the memory we allocated and return a response. */
764 icalcomponent_free(ird.cal);
769 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
773 /* should never get here */
778 * Search for a property in both the top level and in a VEVENT subcomponent
780 icalproperty *ical_ctdl_get_subprop(
782 icalproperty_kind which_prop
787 p = icalcomponent_get_first_property(cal, which_prop);
789 c = icalcomponent_get_first_component(cal,
790 ICAL_VEVENT_COMPONENT);
792 p = icalcomponent_get_first_property(c, which_prop);
800 * Check to see if two events overlap. Returns nonzero if they do.
801 * (This function is used in both Citadel and WebCit. If you change it in
802 * one place, change it in the other. Better yet, put it in a library.)
804 int ical_ctdl_is_overlap(
805 struct icaltimetype t1start,
806 struct icaltimetype t1end,
807 struct icaltimetype t2start,
808 struct icaltimetype t2end
811 if (icaltime_is_null_time(t1start)) return(0);
812 if (icaltime_is_null_time(t2start)) return(0);
814 /* First, check for all-day events */
815 if (t1start.is_date) {
816 if (!icaltime_compare_date_only(t1start, t2start)) {
819 if (!icaltime_is_null_time(t2end)) {
820 if (!icaltime_compare_date_only(t1start, t2end)) {
826 if (t2start.is_date) {
827 if (!icaltime_compare_date_only(t2start, t1start)) {
830 if (!icaltime_is_null_time(t1end)) {
831 if (!icaltime_compare_date_only(t2start, t1end)) {
837 /* Now check for overlaps using date *and* time. */
839 /* First, bail out if either event 1 or event 2 is missing end time. */
840 if (icaltime_is_null_time(t1end)) return(0);
841 if (icaltime_is_null_time(t2end)) return(0);
843 /* If event 1 ends before event 2 starts, we're in the clear. */
844 if (icaltime_compare(t1end, t2start) <= 0) return(0);
846 /* If event 2 ends before event 1 starts, we're also ok. */
847 if (icaltime_compare(t2end, t1start) <= 0) return(0);
849 /* Otherwise, they overlap. */
856 * Phase 6 of "hunt for conflicts"
857 * called by ical_conflicts_phase5()
859 * Now both the proposed and existing events have been boiled down to start and end times.
860 * Check for overlap and output any conflicts.
862 * Returns nonzero if a conflict was reported. This allows the caller to stop iterating.
864 int ical_conflicts_phase6(struct icaltimetype t1start,
865 struct icaltimetype t1end,
866 struct icaltimetype t2start,
867 struct icaltimetype t2end,
868 long existing_msgnum,
869 char *conflict_event_uid,
870 char *conflict_event_summary,
873 int conflict_reported = 0;
877 tt = icaltime_as_timet_with_zone(t1start, t1start.zone);
878 CtdlLogPrintf(CTDL_DEBUG, "PROPOSED START: %s", ctime(&tt));
879 tt = icaltime_as_timet_with_zone(t1end, t1end.zone);
880 CtdlLogPrintf(CTDL_DEBUG, " PROPOSED END: %s", ctime(&tt));
881 tt = icaltime_as_timet_with_zone(t2start, t2start.zone);
882 CtdlLogPrintf(CTDL_DEBUG, "EXISTING START: %s", ctime(&tt));
883 tt = icaltime_as_timet_with_zone(t2end, t2end.zone);
884 CtdlLogPrintf(CTDL_DEBUG, " EXISTING END: %s", ctime(&tt));
887 /* compare and output */
889 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
890 cprintf("%ld||%s|%s|%d|\n",
893 conflict_event_summary,
894 ( ((strlen(compare_uid)>0)
895 &&(!strcasecmp(compare_uid,
896 conflict_event_uid))) ? 1 : 0
899 conflict_reported = 1;
902 return(conflict_reported);
908 * Phase 5 of "hunt for conflicts"
909 * Called by ical_conflicts_phase4()
911 * We have the proposed event boiled down to start and end times.
912 * Now check it against an existing event.
914 void ical_conflicts_phase5(struct icaltimetype t1start,
915 struct icaltimetype t1end,
916 icalcomponent *existing_event,
917 long existing_msgnum,
920 char conflict_event_uid[SIZ];
921 char conflict_event_summary[SIZ];
922 struct icaltimetype t2start, t2end;
925 /* recur variables */
926 icalproperty *rrule = NULL;
927 struct icalrecurrencetype recur;
928 icalrecur_iterator *ritr = NULL;
929 struct icaldurationtype dur;
933 strcpy(conflict_event_uid, "");
934 strcpy(conflict_event_summary, "");
935 t2start = icaltime_null_time();
936 t2end = icaltime_null_time();
938 /* existing event stuff */
939 p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
940 if (p == NULL) return;
941 if (p != NULL) t2start = icalproperty_get_dtstart(p);
942 if (icaltime_is_utc(t2start)) {
943 t2start.zone = icaltimezone_get_utc_timezone();
946 t2start.zone = icalcomponent_get_timezone(existing_event,
947 icalparameter_get_tzid(
948 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
952 t2start.zone = get_default_icaltimezone();
956 p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
958 t2end = icalproperty_get_dtend(p);
960 if (icaltime_is_utc(t2end)) {
961 t2end.zone = icaltimezone_get_utc_timezone();
964 t2end.zone = icalcomponent_get_timezone(existing_event,
965 icalparameter_get_tzid(
966 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
970 t2end.zone = get_default_icaltimezone();
973 dur = icaltime_subtract(t2end, t2start);
976 rrule = ical_ctdl_get_subprop(existing_event, ICAL_RRULE_PROPERTY);
978 recur = icalproperty_get_rrule(rrule);
979 ritr = icalrecur_iterator_new(recur, t2start);
983 p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
985 strcpy(conflict_event_uid, icalproperty_get_comment(p));
988 p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
990 strcpy(conflict_event_summary, icalproperty_get_comment(p));
993 if (ical_conflicts_phase6(t1start, t1end, t2start, t2end,
994 existing_msgnum, conflict_event_uid, conflict_event_summary, compare_uid))
996 num_recur = MAX_RECUR + 1; /* force it out of scope, no need to continue */
1000 t2start = icalrecur_iterator_next(ritr);
1001 if (!icaltime_is_null_time(t2end)) {
1002 const icaltimezone *hold_zone = t2end.zone;
1003 t2end = icaltime_add(t2start, dur);
1004 t2end.zone = hold_zone;
1009 if (icaltime_compare(t2start, t1end) < 0) {
1010 num_recur = MAX_RECUR + 1; /* force it out of scope */
1013 } while ( (rrule) && (!icaltime_is_null_time(t2start)) && (num_recur < MAX_RECUR) );
1014 icalrecur_iterator_free(ritr);
1021 * Phase 4 of "hunt for conflicts"
1022 * Called by ical_hunt_for_conflicts_backend()
1024 * At this point we've got it boiled down to two icalcomponent events in memory.
1025 * If they conflict, output something to the client.
1027 void ical_conflicts_phase4(icalcomponent *proposed_event,
1028 icalcomponent *existing_event,
1029 long existing_msgnum)
1031 struct icaltimetype t1start, t1end;
1032 t1start = icaltime_null_time();
1033 t1end = icaltime_null_time();
1035 char compare_uid[SIZ];
1037 /* recur variables */
1038 icalproperty *rrule = NULL;
1039 struct icalrecurrencetype recur;
1040 icalrecur_iterator *ritr = NULL;
1041 struct icaldurationtype dur;
1044 /* initialization */
1045 strcpy(compare_uid, "");
1047 /* proposed event stuff */
1049 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
1050 if (p == NULL) return;
1051 if (p != NULL) t1start = icalproperty_get_dtstart(p);
1052 if (icaltime_is_utc(t1start)) {
1053 t1start.zone = icaltimezone_get_utc_timezone();
1056 t1start.zone = icalcomponent_get_timezone(proposed_event,
1057 icalparameter_get_tzid(
1058 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1061 if (!t1start.zone) {
1062 t1start.zone = get_default_icaltimezone();
1066 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
1068 t1end = icalproperty_get_dtend(p);
1070 if (icaltime_is_utc(t1end)) {
1071 t1end.zone = icaltimezone_get_utc_timezone();
1074 t1end.zone = icalcomponent_get_timezone(proposed_event,
1075 icalparameter_get_tzid(
1076 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1080 t1end.zone = get_default_icaltimezone();
1084 dur = icaltime_subtract(t1end, t1start);
1087 rrule = ical_ctdl_get_subprop(proposed_event, ICAL_RRULE_PROPERTY);
1089 recur = icalproperty_get_rrule(rrule);
1090 ritr = icalrecur_iterator_new(recur, t1start);
1093 p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
1095 strcpy(compare_uid, icalproperty_get_comment(p));
1099 ical_conflicts_phase5(t1start, t1end, existing_event, existing_msgnum, compare_uid);
1102 t1start = icalrecur_iterator_next(ritr);
1103 if (!icaltime_is_null_time(t1end)) {
1104 const icaltimezone *hold_zone = t1end.zone;
1105 t1end = icaltime_add(t1start, dur);
1106 t1end.zone = hold_zone;
1111 } while ( (rrule) && (!icaltime_is_null_time(t1start)) && (num_recur < MAX_RECUR) );
1112 icalrecur_iterator_free(ritr);
1118 * Phase 3 of "hunt for conflicts"
1119 * Called by ical_hunt_for_conflicts()
1121 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
1122 icalcomponent *proposed_event;
1123 struct CtdlMessage *msg = NULL;
1124 struct ical_respond_data ird;
1126 proposed_event = (icalcomponent *)data;
1128 msg = CtdlFetchMessage(msgnum, 1);
1129 if (msg == NULL) return;
1130 memset(&ird, 0, sizeof ird);
1131 strcpy(ird.desired_partnum, "_HUNT_");
1132 mime_parser(msg->cm_fields['M'],
1134 *ical_locate_part, /* callback function */
1136 (void *) &ird, /* user data */
1139 CtdlFreeMessage(msg);
1141 if (ird.cal == NULL) return;
1143 ical_conflicts_phase4(proposed_event, ird.cal, msgnum);
1144 icalcomponent_free(ird.cal);
1150 * Phase 2 of "hunt for conflicts" operation.
1151 * At this point we have a calendar object which represents the VEVENT that
1152 * is proposed for addition to the calendar. Now hunt through the user's
1153 * calendar room, and output zero or more existing VEVENTs which conflict
1156 void ical_hunt_for_conflicts(icalcomponent *cal) {
1157 char hold_rm[ROOMNAMELEN];
1159 strcpy(hold_rm, CC->room.QRname); /* save current room */
1161 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
1162 getroom(&CC->room, hold_rm);
1163 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1167 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1169 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1172 ical_hunt_for_conflicts_backend,
1177 getroom(&CC->room, hold_rm); /* return to saved room */
1184 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1186 void ical_conflicts(long msgnum, char *partnum) {
1187 struct CtdlMessage *msg = NULL;
1188 struct ical_respond_data ird;
1190 msg = CtdlFetchMessage(msgnum, 1);
1192 cprintf("%d Message %ld not found\n",
1193 ERROR + ILLEGAL_VALUE,
1199 memset(&ird, 0, sizeof ird);
1200 strcpy(ird.desired_partnum, partnum);
1201 mime_parser(msg->cm_fields['M'],
1203 *ical_locate_part, /* callback function */
1205 (void *) &ird, /* user data */
1209 CtdlFreeMessage(msg);
1211 if (ird.cal != NULL) {
1212 ical_hunt_for_conflicts(ird.cal);
1213 icalcomponent_free(ird.cal);
1217 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1223 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1225 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1228 struct icalperiodtype my_period;
1230 if (cal == NULL) return;
1231 my_period = icalperiodtype_null_period();
1233 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1234 ical_add_to_freebusy(fb,
1235 icalcomponent_get_first_component(
1236 cal, ICAL_VEVENT_COMPONENT
1242 /* If this event is not opaque, the user isn't publishing it as
1243 * busy time, so don't bother doing anything else.
1245 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1247 v = icalproperty_get_value(p);
1249 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1255 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1256 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1258 my_period.start = icalproperty_get_dtstart(p);
1261 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1263 my_period.end = icalproperty_get_dtstart(p);
1267 icalcomponent_add_property(fb,
1268 icalproperty_new_freebusy(my_period)
1271 /* Make sure the DTSTART property of the freebusy *list* is set to
1272 * the DTSTART property of the *earliest event*.
1274 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1276 icalcomponent_set_dtstart(fb,
1277 icalcomponent_get_dtstart(cal) );
1280 if (icaltime_compare(
1281 icalcomponent_get_dtstart(cal),
1282 icalcomponent_get_dtstart(fb)
1284 icalcomponent_set_dtstart(fb,
1285 icalcomponent_get_dtstart(cal) );
1289 /* Make sure the DTEND property of the freebusy *list* is set to
1290 * the DTEND property of the *latest event*.
1292 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1294 icalcomponent_set_dtend(fb,
1295 icalcomponent_get_dtend(cal) );
1298 if (icaltime_compare(
1299 icalcomponent_get_dtend(cal),
1300 icalcomponent_get_dtend(fb)
1302 icalcomponent_set_dtend(fb,
1303 icalcomponent_get_dtend(cal) );
1312 * Backend for ical_freebusy()
1314 * This function simply loads the messages in the user's calendar room,
1315 * which contain VEVENTs, then strips them of all non-freebusy data, and
1316 * adds them to the supplied VCALENDAR.
1319 void ical_freebusy_backend(long msgnum, void *data) {
1321 struct CtdlMessage *msg = NULL;
1322 struct ical_respond_data ird;
1324 cal = (icalcomponent *)data;
1326 msg = CtdlFetchMessage(msgnum, 1);
1327 if (msg == NULL) return;
1328 memset(&ird, 0, sizeof ird);
1329 strcpy(ird.desired_partnum, "_HUNT_");
1330 mime_parser(msg->cm_fields['M'],
1332 *ical_locate_part, /* callback function */
1334 (void *) &ird, /* user data */
1337 CtdlFreeMessage(msg);
1339 if (ird.cal == NULL) return;
1341 ical_add_to_freebusy(cal, ird.cal);
1343 /* Now free the memory. */
1344 icalcomponent_free(ird.cal);
1350 * Grab another user's free/busy times
1352 void ical_freebusy(char *who) {
1353 struct ctdluser usbuf;
1354 char calendar_room_name[ROOMNAMELEN];
1355 char hold_rm[ROOMNAMELEN];
1356 char *serialized_request = NULL;
1357 icalcomponent *encaps = NULL;
1358 icalcomponent *fb = NULL;
1359 int found_user = (-1);
1360 struct recptypes *recp = NULL;
1365 int config_lines = 0;
1367 /* First try an exact match. */
1368 found_user = getuser(&usbuf, who);
1370 /* If not found, try it as an unqualified email address. */
1371 if (found_user != 0) {
1373 recp = validate_recipients(buf, NULL, 0);
1374 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1376 if (recp->num_local == 1) {
1377 found_user = getuser(&usbuf, recp->recp_local);
1379 free_recipients(recp);
1383 /* If still not found, try it as an address qualified with the
1384 * primary FQDN of this Citadel node.
1386 if (found_user != 0) {
1387 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1388 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1389 recp = validate_recipients(buf, NULL, 0);
1391 if (recp->num_local == 1) {
1392 found_user = getuser(&usbuf, recp->recp_local);
1394 free_recipients(recp);
1398 /* Still not found? Try qualifying it with every domain we
1399 * might have addresses in.
1401 if (found_user != 0) {
1402 config_lines = num_tokens(inetcfg, '\n');
1403 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1404 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1405 extract_token(host, buf, 0, '|', sizeof host);
1406 extract_token(type, buf, 1, '|', sizeof type);
1408 if ( (!strcasecmp(type, "localhost"))
1409 || (!strcasecmp(type, "directory")) ) {
1410 snprintf(buf, sizeof buf, "%s@%s", who, host);
1411 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1412 recp = validate_recipients(buf, NULL, 0);
1414 if (recp->num_local == 1) {
1415 found_user = getuser(&usbuf, recp->recp_local);
1417 free_recipients(recp);
1423 if (found_user != 0) {
1424 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1428 MailboxName(calendar_room_name, sizeof calendar_room_name,
1429 &usbuf, USERCALENDARROOM);
1431 strcpy(hold_rm, CC->room.QRname); /* save current room */
1433 if (getroom(&CC->room, calendar_room_name) != 0) {
1434 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1435 getroom(&CC->room, hold_rm);
1439 /* Create a VFREEBUSY subcomponent */
1440 CtdlLogPrintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1441 fb = icalcomponent_new_vfreebusy();
1443 cprintf("%d Internal error: cannot allocate memory.\n",
1444 ERROR + INTERNAL_ERROR);
1445 getroom(&CC->room, hold_rm);
1449 /* Set the method to PUBLISH */
1450 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1452 /* Set the DTSTAMP to right now. */
1453 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1455 /* Add the user's email address as ORGANIZER */
1456 sprintf(buf, "MAILTO:%s", who);
1457 if (strchr(buf, '@') == NULL) {
1459 strcat(buf, config.c_fqdn);
1461 for (i=0; buf[i]; ++i) {
1462 if (buf[i]==' ') buf[i] = '_';
1464 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1466 /* Add busy time from events */
1467 CtdlLogPrintf(CTDL_DEBUG, "Adding busy time from events\n");
1468 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1470 /* If values for DTSTART and DTEND are still not present, set them
1471 * to yesterday and tomorrow as default values.
1473 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1474 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1476 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1477 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1480 /* Put the freebusy component into the calendar component */
1481 CtdlLogPrintf(CTDL_DEBUG, "Encapsulating\n");
1482 encaps = ical_encapsulate_subcomponent(fb);
1483 if (encaps == NULL) {
1484 icalcomponent_free(fb);
1485 cprintf("%d Internal error: cannot allocate memory.\n",
1486 ERROR + INTERNAL_ERROR);
1487 getroom(&CC->room, hold_rm);
1491 /* Set the method to PUBLISH */
1492 CtdlLogPrintf(CTDL_DEBUG, "Setting method\n");
1493 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1496 CtdlLogPrintf(CTDL_DEBUG, "Serializing\n");
1497 serialized_request = icalcomponent_as_ical_string_r(encaps);
1498 icalcomponent_free(encaps); /* Don't need this anymore. */
1500 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1501 if (serialized_request != NULL) {
1502 client_write(serialized_request, strlen(serialized_request));
1503 free(serialized_request);
1507 /* Go back to the room from which we came... */
1508 getroom(&CC->room, hold_rm);
1514 * Backend for ical_getics()
1516 * This is a ForEachMessage() callback function that searches the current room
1517 * for calendar events and adds them each into one big calendar component.
1519 void ical_getics_backend(long msgnum, void *data) {
1520 icalcomponent *encaps, *c;
1521 struct CtdlMessage *msg = NULL;
1522 struct ical_respond_data ird;
1524 encaps = (icalcomponent *)data;
1525 if (encaps == NULL) return;
1527 /* Look for the calendar event... */
1529 msg = CtdlFetchMessage(msgnum, 1);
1530 if (msg == NULL) return;
1531 memset(&ird, 0, sizeof ird);
1532 strcpy(ird.desired_partnum, "_HUNT_");
1533 mime_parser(msg->cm_fields['M'],
1535 *ical_locate_part, /* callback function */
1537 (void *) &ird, /* user data */
1540 CtdlFreeMessage(msg);
1542 if (ird.cal == NULL) return;
1544 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1545 * are responsible for "the_request"'s memory -- it will be freed
1546 * when we free "encaps".
1549 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1550 * in. This will almost never happen.
1552 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1553 icalcomponent_add_component(encaps, ird.cal);
1556 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1557 * and other components encapsulated inside, we have to extract them.
1560 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1562 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1564 /* For VTIMEZONE components, suppress duplicates of the same tzid */
1566 if (icalcomponent_isa(c) == ICAL_VTIMEZONE_COMPONENT) {
1567 icalproperty *p = icalcomponent_get_first_property(c, ICAL_TZID_PROPERTY);
1569 const char *tzid = icalproperty_get_tzid(p);
1570 if (!icalcomponent_get_timezone(encaps, tzid)) {
1571 icalcomponent_add_component(encaps,
1572 icalcomponent_new_clone(c));
1577 /* All other types of components can go in verbatim */
1579 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1582 icalcomponent_free(ird.cal);
1589 * Retrieve all of the calendar items in the current room, and output them
1590 * as a single icalendar object.
1592 void ical_getics(void)
1594 icalcomponent *encaps = NULL;
1597 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1598 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1599 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1600 return; /* Not an iCalendar-centric room */
1603 encaps = icalcomponent_new_vcalendar();
1604 if (encaps == NULL) {
1605 CtdlLogPrintf(CTDL_DEBUG, "ERROR: could not allocate component!\n");
1606 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1610 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1612 /* Set the Product ID */
1613 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1615 /* Set the Version Number */
1616 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1618 /* Set the method to PUBLISH */
1619 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1621 /* Now go through the room encapsulating all calendar items. */
1622 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1625 ical_getics_backend,
1629 ser = icalcomponent_as_ical_string_r(encaps);
1630 icalcomponent_free(encaps); /* Don't need this anymore. */
1631 client_write(ser, strlen(ser));
1638 * Helper callback function for ical_putics() to discover which TZID's we need.
1639 * Simply put the tzid name string into a hash table. After the callbacks are
1640 * done we'll go through them and attach the ones that we have.
1642 void ical_putics_grabtzids(icalparameter *param, void *data)
1644 const char *tzid = icalparameter_get_tzid(param);
1645 HashList *keys = (HashList *) data;
1647 if ( (keys) && (tzid) && (!IsEmptyStr(tzid)) ) {
1648 Put(keys, tzid, strlen(tzid), strdup(tzid), generic_free_handler);
1654 * Delete all of the calendar items in the current room, and replace them
1655 * with calendar items from a client-supplied data stream.
1657 void ical_putics(void)
1659 char *calstream = NULL;
1662 icalcomponent *encaps = NULL;
1663 HashList *tzidlist = NULL;
1669 /* Only allow this operation if we're in a room containing a calendar or tasks view */
1670 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1671 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1672 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1676 /* Only allow this operation if we have permission to overwrite the existing calendar */
1677 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1678 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1682 cprintf("%d Transmit data now\n", SEND_LISTING);
1683 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1684 if (calstream == NULL) {
1688 cal = icalcomponent_new_from_string(calstream);
1691 /* We got our data stream -- now do something with it. */
1693 /* Delete the existing messages in the room, because we are overwriting
1694 * the entire calendar with an entire new (or updated) calendar.
1695 * (Careful: this opens an S_ROOMS critical section!)
1697 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1699 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1700 * in. This will almost never happen.
1702 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1703 ical_write_to_cal(NULL, cal);
1706 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1707 * and other components encapsulated inside, we have to extract them.
1710 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1712 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1714 /* Non-VTIMEZONE components each get written as individual messages.
1715 * But we also need to attach the relevant VTIMEZONE components to them.
1717 if ( (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT)
1718 && (encaps = icalcomponent_new_vcalendar()) ) {
1719 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1720 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1721 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1723 /* Attach any needed timezones here */
1724 tzidlist = NewHash(1, NULL);
1726 icalcomponent_foreach_tzid(c, ical_putics_grabtzids, tzidlist);
1728 HashPos = GetNewHashPos(tzidlist, 0);
1730 while (GetNextHashPos(tzidlist, HashPos, &len, &Key, &Value)) {
1731 CtdlLogPrintf(CTDL_DEBUG, "Attaching timezone '%s'\n", Value);
1732 icaltimezone *t = NULL;
1734 /* First look for a timezone attached to the original calendar */
1735 t = icalcomponent_get_timezone(cal, Value);
1737 /* Try built-in tzdata if the right one wasn't attached */
1739 t = icaltimezone_get_builtin_timezone(Value);
1742 /* I've got a valid timezone to attach. */
1744 icalcomponent_add_component(encaps,
1745 icalcomponent_new_clone(
1746 icaltimezone_get_component(t)
1752 DeleteHashPos(&HashPos);
1753 DeleteHash(&tzidlist);
1755 /* Now attach the component itself (usually a VEVENT or VTODO) */
1756 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1758 /* Write it to the message store */
1759 ical_write_to_cal(NULL, encaps);
1760 icalcomponent_free(encaps);
1765 icalcomponent_free(cal);
1770 * All Citadel calendar commands from the client come through here.
1772 void cmd_ical(char *argbuf)
1780 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1782 /* Allow "test" and "freebusy" subcommands without logging in. */
1784 if (!strcasecmp(subcmd, "test")) {
1785 cprintf("%d This server supports calendaring\n", CIT_OK);
1789 if (!strcasecmp(subcmd, "freebusy")) {
1790 extract_token(who, argbuf, 1, '|', sizeof who);
1795 if (!strcasecmp(subcmd, "sgi")) {
1796 CIT_ICAL->server_generated_invitations =
1797 (extract_int(argbuf, 1) ? 1 : 0) ;
1799 CIT_OK, CIT_ICAL->server_generated_invitations);
1803 if (CtdlAccessCheck(ac_logged_in)) return;
1805 if (!strcasecmp(subcmd, "respond")) {
1806 msgnum = extract_long(argbuf, 1);
1807 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1808 extract_token(action, argbuf, 3, '|', sizeof action);
1809 ical_respond(msgnum, partnum, action);
1813 if (!strcasecmp(subcmd, "handle_rsvp")) {
1814 msgnum = extract_long(argbuf, 1);
1815 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1816 extract_token(action, argbuf, 3, '|', sizeof action);
1817 ical_handle_rsvp(msgnum, partnum, action);
1821 if (!strcasecmp(subcmd, "conflicts")) {
1822 msgnum = extract_long(argbuf, 1);
1823 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1824 ical_conflicts(msgnum, partnum);
1828 if (!strcasecmp(subcmd, "getics")) {
1833 if (!strcasecmp(subcmd, "putics")) {
1838 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1844 * We don't know if the calendar room exists so we just create it at login
1846 void ical_create_room(void)
1851 /* Create the calendar room if it doesn't already exist */
1852 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1854 /* Set expiration policy to manual; otherwise objects will be lost! */
1855 if (lgetroom(&qr, USERCALENDARROOM)) {
1856 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1859 qr.QRep.expire_mode = EXPIRE_MANUAL;
1860 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1863 /* Set the view to a calendar view */
1864 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1865 vbuf.v_view = VIEW_CALENDAR;
1866 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1868 /* Create the tasks list room if it doesn't already exist */
1869 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1871 /* Set expiration policy to manual; otherwise objects will be lost! */
1872 if (lgetroom(&qr, USERTASKSROOM)) {
1873 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1876 qr.QRep.expire_mode = EXPIRE_MANUAL;
1877 qr.QRdefaultview = VIEW_TASKS;
1880 /* Set the view to a task list view */
1881 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1882 vbuf.v_view = VIEW_TASKS;
1883 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1885 /* Create the notes room if it doesn't already exist */
1886 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1888 /* Set expiration policy to manual; otherwise objects will be lost! */
1889 if (lgetroom(&qr, USERNOTESROOM)) {
1890 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1893 qr.QRep.expire_mode = EXPIRE_MANUAL;
1894 qr.QRdefaultview = VIEW_NOTES;
1897 /* Set the view to a notes view */
1898 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1899 vbuf.v_view = VIEW_NOTES;
1900 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1907 * ical_send_out_invitations() is called by ical_saving_vevent() when it finds a VEVENT.
1909 * top_level_cal is the highest available level calendar object.
1910 * cal is the subcomponent containing the VEVENT.
1912 * Note: if you change the encapsulation code here, change it in WebCit's ical_encapsulate_subcomponent()
1914 void ical_send_out_invitations(icalcomponent *top_level_cal, icalcomponent *cal) {
1915 icalcomponent *the_request = NULL;
1916 char *serialized_request = NULL;
1917 icalcomponent *encaps = NULL;
1918 char *request_message_text = NULL;
1919 struct CtdlMessage *msg = NULL;
1920 struct recptypes *valid = NULL;
1921 char attendees_string[SIZ];
1922 int num_attendees = 0;
1923 char this_attendee[256];
1924 icalproperty *attendee = NULL;
1925 char summary_string[SIZ];
1926 icalproperty *summary = NULL;
1929 struct icaltimetype t;
1930 icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
1933 int num_zones_attached = 0;
1934 int zone_already_attached;
1937 CtdlLogPrintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1942 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1943 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1944 ical_send_out_invitations(top_level_cal,
1945 icalcomponent_get_first_component(
1946 cal, ICAL_VEVENT_COMPONENT
1952 /* Clone the event */
1953 the_request = icalcomponent_new_clone(cal);
1954 if (the_request == NULL) {
1955 CtdlLogPrintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1959 /* Extract the summary string -- we'll use it as the
1960 * message subject for the request
1962 strcpy(summary_string, "Meeting request");
1963 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1964 if (summary != NULL) {
1965 if (icalproperty_get_summary(summary)) {
1966 strcpy(summary_string,
1967 icalproperty_get_summary(summary) );
1971 /* Determine who the recipients of this message are (the attendees) */
1972 strcpy(attendees_string, "");
1973 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1974 if (icalproperty_get_attendee(attendee)) {
1975 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1976 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1977 strcpy(this_attendee, &this_attendee[7]);
1979 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1980 snprintf(&attendees_string[strlen(attendees_string)],
1981 sizeof(attendees_string) - strlen(attendees_string),
1991 CtdlLogPrintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1993 /* If there are no attendees, there are no invitations to send, so...
1994 * don't bother putting one together! Punch out, Maverick!
1996 if (num_attendees == 0) {
1997 icalcomponent_free(the_request);
2001 /* Encapsulate the VEVENT component into a complete VCALENDAR */
2002 encaps = icalcomponent_new_vcalendar();
2003 if (encaps == NULL) {
2004 CtdlLogPrintf(CTDL_DEBUG, "ERROR: could not allocate component!\n");
2005 icalcomponent_free(the_request);
2009 /* Set the Product ID */
2010 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
2012 /* Set the Version Number */
2013 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
2015 /* Set the method to REQUEST */
2016 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
2018 /* Look for properties containing timezone parameters, to see if we need to attach VTIMEZONEs */
2019 for (p = icalcomponent_get_first_property(the_request, ICAL_ANY_PROPERTY);
2021 p = icalcomponent_get_next_property(the_request, ICAL_ANY_PROPERTY))
2023 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
2024 || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
2025 || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
2026 || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
2027 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
2028 || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
2029 || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
2030 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
2031 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
2032 || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
2033 || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
2034 || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
2035 || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
2037 t = icalproperty_get_dtstart(p); // it's safe to use dtstart for all of them
2038 CtdlLogPrintf(CTDL_DEBUG, "Found an icaltimetype: %s\n",
2039 icaltime_as_ical_string(t)
2042 /* First see if there's a timezone attached to the data structure itself */
2043 if (icaltime_is_utc(t)) {
2044 z = icaltimezone_get_utc_timezone();
2047 z = icaltime_get_timezone(t);
2049 if (z) CtdlLogPrintf(CTDL_DEBUG, "Timezone is present in data structure\n");
2051 /* If not, try to determine the tzid from the parameter using attached zones */
2053 z = icalcomponent_get_timezone(top_level_cal,
2054 icalparameter_get_tzid(
2055 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
2058 if (z) CtdlLogPrintf(CTDL_DEBUG, "Timezone was found in attached zones\n");
2061 /* Still no good? Try our internal database */
2063 z = icaltimezone_get_builtin_timezone_from_tzid(
2064 icalparameter_get_tzid(
2065 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
2068 if (z) CtdlLogPrintf(CTDL_DEBUG, "Timezone was found in internal db\n");
2072 CtdlLogPrintf(CTDL_DEBUG, "Have valid timezone, need to attach it.\n");
2074 zone_already_attached = 0;
2075 for (i=0; i<5; ++i) {
2076 if (z == attached_zones[i]) {
2077 ++zone_already_attached;
2078 CtdlLogPrintf(CTDL_DEBUG, "zone already attached!!\n");
2081 if ((!zone_already_attached) && (num_zones_attached < 5)) {
2082 CtdlLogPrintf(CTDL_DEBUG, "attach zone %d\n", num_zones_attached);
2083 attached_zones[num_zones_attached++] = z;
2086 icalproperty_set_parameter(p,
2087 icalparameter_new_tzid(icaltimezone_get_tzid(z))
2093 /* Encapsulate any timezones we need */
2094 if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
2096 zc = icalcomponent_new_clone(icaltimezone_get_component(attached_zones[i]));
2097 icalcomponent_add_component(encaps, zc);
2100 /* Here we go: encapsulate the VEVENT into the VCALENDAR. We now no longer
2101 * are responsible for "the_request"'s memory -- it will be freed
2102 * when we free "encaps".
2104 icalcomponent_add_component(encaps, the_request);
2107 serialized_request = icalcomponent_as_ical_string_r(encaps);
2108 icalcomponent_free(encaps); /* Don't need this anymore. */
2109 if (serialized_request == NULL) return;
2111 CtdlLogPrintf(CTDL_DEBUG, "SENDING INVITATIONS:\n%s\n", serialized_request);
2113 reqsize = strlen(serialized_request) + SIZ;
2114 request_message_text = malloc(reqsize);
2115 if (request_message_text != NULL) {
2116 snprintf(request_message_text, reqsize,
2117 "Content-type: text/calendar\r\n\r\n%s\r\n",
2121 msg = CtdlMakeMessage(&CC->user,
2122 "", /* No single recipient here */
2123 "", /* No single recipient here */
2124 CC->room.QRname, 0, FMT_RFC822,
2127 summary_string, /* Use summary for subject */
2129 request_message_text,
2133 valid = validate_recipients(attendees_string, NULL, 0);
2134 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
2135 CtdlFreeMessage(msg);
2136 free_recipients(valid);
2139 free(serialized_request);
2144 * When a calendar object is being saved, determine whether it's a VEVENT
2145 * and the user saving it is the organizer. If so, send out invitations
2146 * to any listed attendees.
2148 * This function is recursive. The caller can simply supply the same object
2149 * as both arguments. When it recurses it will alter the second argument
2150 * while holding on to the top level object. This allows us to go back and
2151 * grab things like time zones which might be attached.
2154 void ical_saving_vevent(icalcomponent *top_level_cal, icalcomponent *cal) {
2156 icalproperty *organizer = NULL;
2157 char organizer_string[SIZ];
2159 CtdlLogPrintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
2161 /* Don't send out invitations unless the client wants us to. */
2162 if (CIT_ICAL->server_generated_invitations == 0) {
2166 /* Don't send out invitations if we've been asked not to. */
2167 if (CIT_ICAL->avoid_sending_invitations > 0) {
2171 strcpy(organizer_string, "");
2173 * The VEVENT subcomponent is the one we're interested in.
2174 * Send out invitations if, and only if, this user is the Organizer.
2176 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
2177 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
2178 if (organizer != NULL) {
2179 if (icalproperty_get_organizer(organizer)) {
2180 strcpy(organizer_string,
2181 icalproperty_get_organizer(organizer));
2184 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
2185 strcpy(organizer_string, &organizer_string[7]);
2186 striplt(organizer_string);
2188 * If the user saving the event is listed as the
2189 * organizer, then send out invitations.
2191 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
2192 ical_send_out_invitations(top_level_cal, cal);
2197 /* If the component has subcomponents, recurse through them. */
2198 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2200 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2201 /* Recursively process subcomponent */
2202 ical_saving_vevent(top_level_cal, c);
2210 * Back end for ical_obj_beforesave()
2211 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
2212 * the summary of the event (becomes message subject),
2213 * and the start time (becomes message date/time).
2215 void ical_obj_beforesave_backend(char *name, char *filename, char *partnum,
2216 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2217 char *encoding, char *cbid, void *cbuserdata)
2219 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
2221 char new_uid[256] = "";
2222 char buf[1024] = "";
2223 struct CtdlMessage *msg = (struct CtdlMessage *) cbuserdata;
2227 /* We're only interested in calendar data. */
2228 if ( (strcasecmp(cbtype, "text/calendar"))
2229 && (strcasecmp(cbtype, "application/ics")) ) {
2233 /* Hunt for the UID and drop it in
2234 * the "user data" pointer for the MIME parser. When
2235 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2238 whole_cal = icalcomponent_new_from_string(content);
2241 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2242 nested_event = icalcomponent_get_first_component(
2243 cal, ICAL_VEVENT_COMPONENT);
2244 if (nested_event != NULL) {
2248 nested_todo = icalcomponent_get_first_component(
2249 cal, ICAL_VTODO_COMPONENT);
2250 if (nested_todo != NULL) {
2258 /* Set the message EUID to the iCalendar UID */
2260 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2262 /* If there's no uid we must generate one */
2263 generate_uuid(new_uid);
2264 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
2265 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2268 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
2269 if (!IsEmptyStr(buf)) {
2270 if (msg->cm_fields['E'] != NULL) {
2271 free(msg->cm_fields['E']);
2273 msg->cm_fields['E'] = strdup(buf);
2274 CtdlLogPrintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", buf);
2278 /* Set the message subject to the iCalendar summary */
2280 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
2282 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
2283 if (!IsEmptyStr(buf)) {
2284 if (msg->cm_fields['U'] != NULL) {
2285 free(msg->cm_fields['U']);
2287 msg->cm_fields['U'] = strdup(buf);
2291 /* Set the message date/time to the iCalendar start time */
2293 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
2296 idtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
2298 if (msg->cm_fields['T'] != NULL) {
2299 free(msg->cm_fields['T']);
2301 msg->cm_fields['T'] = strdup("000000000000000000");
2302 sprintf(msg->cm_fields['T'], "%ld", idtstart);
2307 icalcomponent_free(cal);
2308 if (whole_cal != cal) {
2309 icalcomponent_free(whole_cal);
2318 * See if we need to prevent the object from being saved (we don't allow
2319 * MIME types other than text/calendar in "calendar" or "tasks" rooms).
2321 * If the message is being saved, we also set various message header fields
2322 * using data found in the iCalendar object.
2324 int ical_obj_beforesave(struct CtdlMessage *msg)
2326 /* First determine if this is a calendar or tasks room */
2327 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
2328 && (CC->room.QRdefaultview != VIEW_TASKS)
2330 return(0); /* Not an iCalendar-centric room */
2333 /* It must be an RFC822 message! */
2334 if (msg->cm_format_type != 4) {
2335 CtdlLogPrintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
2336 return(1); /* You tried to save a non-RFC822 message! */
2339 if (msg->cm_fields['M'] == NULL) {
2340 return(1); /* You tried to save a null message! */
2343 /* Do all of our lovely back-end parsing */
2344 mime_parser(msg->cm_fields['M'],
2346 *ical_obj_beforesave_backend,
2357 * Things we need to do after saving a calendar event.
2359 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2360 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2361 char *encoding, char *cbid, void *cbuserdata)
2365 /* We're only interested in calendar items here. */
2366 if ( (strcasecmp(cbtype, "text/calendar"))
2367 && (strcasecmp(cbtype, "application/ics")) ) {
2371 /* Hunt for the UID and drop it in
2372 * the "user data" pointer for the MIME parser. When
2373 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2376 if ( (!strcasecmp(cbtype, "text/calendar"))
2377 || (!strcasecmp(cbtype, "application/ics")) ) {
2378 cal = icalcomponent_new_from_string(content);
2380 ical_saving_vevent(cal, cal);
2381 icalcomponent_free(cal);
2388 * Things we need to do after saving a calendar event.
2389 * (This will start back end tasks such as automatic generation of invitations,
2390 * if such actions are appropriate.)
2392 int ical_obj_aftersave(struct CtdlMessage *msg)
2394 char roomname[ROOMNAMELEN];
2397 * If this isn't the Calendar> room, no further action is necessary.
2400 /* First determine if this is our room */
2401 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2402 if (strcasecmp(roomname, CC->room.QRname)) {
2403 return(0); /* Not the Calendar room -- don't do anything. */
2406 /* It must be an RFC822 message! */
2407 if (msg->cm_format_type != 4) return(1);
2409 /* Reject null messages */
2410 if (msg->cm_fields['M'] == NULL) return(1);
2412 /* Now recurse through it looking for our icalendar data */
2413 mime_parser(msg->cm_fields['M'],
2415 *ical_obj_aftersave_backend,
2425 void ical_session_startup(void) {
2426 CIT_ICAL = malloc(sizeof(struct cit_ical));
2427 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2430 void ical_session_shutdown(void) {
2436 * Back end for ical_fixed_output()
2438 void ical_fixed_output_backend(icalcomponent *cal,
2445 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2447 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2450 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2452 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2455 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2457 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2460 /* If the component has attendees, iterate through them. */
2461 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2462 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2463 if (!strncasecmp(buf, "MAILTO:", 7)) {
2465 /* screen name or email address */
2466 strcpy(buf, &buf[7]);
2468 cprintf("%s ", buf);
2473 /* If the component has subcomponents, recurse through them. */
2474 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2476 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2477 /* Recursively process subcomponent */
2478 ical_fixed_output_backend(c, recursion_level+1);
2485 * Function to output iCalendar data as plain text. Nobody uses MSG0
2486 * anymore, so really this is just so we expose the vCard data to the full
2489 void ical_fixed_output(char *ptr, int len) {
2493 stringy_cal = malloc(len + 1);
2494 safestrncpy(stringy_cal, ptr, len + 1);
2495 cal = icalcomponent_new_from_string(stringy_cal);
2502 ical_fixed_output_backend(cal, 0);
2504 /* Free the memory we obtained from libical's constructor */
2505 icalcomponent_free(cal);
2510 void serv_calendar_destroy(void)
2512 icaltimezone_free_builtin_timezones();
2516 * Register this module with the Citadel server.
2518 CTDL_MODULE_INIT(calendar)
2523 /* Tell libical to return errors instead of aborting if it gets bad data */
2524 icalerror_errors_are_fatal = 0;
2526 /* Use our own application prefix in tzid's generated from system tzdata */
2527 icaltimezone_set_tzid_prefix("/citadel.org/");
2529 /* Initialize our hook functions */
2530 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2531 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2532 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2533 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2534 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2535 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2536 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2537 CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
2538 CtdlRegisterCleanupHook(serv_calendar_destroy);
2541 /* return our Subversion id for the Log */