2 * This module implements iCalendar object processing and the Calendar>
3 * room on a Citadel server. It handles iCalendar objects using the
4 * iTIP protocol. See RFCs 2445 and 2446.
7 * Copyright (c) 1987-2011 by the citadel.org team
9 * This program is open source software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
26 #include "ctdl_module.h"
28 #include <libical/ical.h>
31 #include "internet_addressing.h"
32 #include "serv_calendar.h"
33 #include "euidindex.h"
34 #include "ical_dezonify.h"
38 struct ical_respond_data {
39 char desired_partnum[SIZ];
45 * Utility function to create a new VCALENDAR component with some of the
46 * required fields already set the way we like them.
48 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
49 icalcomponent *encaps;
51 encaps = icalcomponent_new_vcalendar();
53 syslog(LOG_CRIT, "ERROR: could not allocate component!\n");
57 /* Set the Product ID */
58 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
60 /* Set the Version Number */
61 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
68 * Utility function to encapsulate a subcomponent into a full VCALENDAR
70 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
71 icalcomponent *encaps;
73 /* If we're already looking at a full VCALENDAR component,
74 * don't bother ... just return itself.
76 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
80 /* Encapsulate the VEVENT component into a complete VCALENDAR */
81 encaps = icalcomponent_new_citadel_vcalendar();
82 if (encaps == NULL) return NULL;
84 /* Encapsulate the subcomponent inside */
85 icalcomponent_add_component(encaps, subcomp);
87 /* Return the object we just created. */
95 * Write a calendar object into the specified user's calendar room.
96 * If the supplied user is NULL, this function writes the calendar object
97 * to the currently selected room.
99 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
101 icalcomponent *encaps = NULL;
102 struct CtdlMessage *msg = NULL;
103 icalcomponent *tmp=NULL;
105 if (cal == NULL) return;
107 /* If the supplied object is a subcomponent, encapsulate it in
108 * a full VCALENDAR component, and save that instead.
110 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
111 tmp = icalcomponent_new_clone(cal);
112 encaps = ical_encapsulate_subcomponent(tmp);
113 ical_write_to_cal(u, encaps);
114 icalcomponent_free(tmp);
115 icalcomponent_free(encaps);
119 ser = icalcomponent_as_ical_string_r(cal);
120 if (ser == NULL) return;
122 /* If the caller supplied a user, write to that user's default calendar room */
124 /* This handy API function does all the work for us. */
125 CtdlWriteObject(USERCALENDARROOM, /* which room */
126 "text/calendar", /* MIME type */
128 strlen(ser)+1, /* length */
131 0, /* don't delete others of this type */
136 /* If the caller did not supply a user, write to the currently selected room */
138 msg = malloc(sizeof(struct CtdlMessage));
139 memset(msg, 0, sizeof(struct CtdlMessage));
140 msg->cm_magic = CTDLMESSAGE_MAGIC;
141 msg->cm_anon_type = MES_NORMAL;
142 msg->cm_format_type = 4;
143 msg->cm_fields['A'] = strdup(CC->user.fullname);
144 msg->cm_fields['O'] = strdup(CC->room.QRname);
145 msg->cm_fields['N'] = strdup(config.c_nodename);
146 msg->cm_fields['H'] = strdup(config.c_humannode);
147 msg->cm_fields['M'] = malloc(strlen(ser) + 40);
148 strcpy(msg->cm_fields['M'], "Content-type: text/calendar\r\n\r\n");
149 strcat(msg->cm_fields['M'], ser);
151 /* Now write the data */
152 CtdlSubmitMsg(msg, NULL, "", QP_EADDR);
153 CtdlFreeMessage(msg);
156 /* In either case, now we can free the serialized calendar object */
162 * Send a reply to a meeting invitation.
164 * 'request' is the invitation to reply to.
165 * 'action' is the string "accept" or "decline" or "tentative".
168 void ical_send_a_reply(icalcomponent *request, char *action) {
169 icalcomponent *the_reply = NULL;
170 icalcomponent *vevent = NULL;
171 icalproperty *attendee = NULL;
172 char attendee_string[SIZ];
173 icalproperty *organizer = NULL;
174 char organizer_string[SIZ];
175 icalproperty *summary = NULL;
176 char summary_string[SIZ];
177 icalproperty *me_attend = NULL;
178 struct recptypes *recp = NULL;
179 icalparameter *partstat = NULL;
180 char *serialized_reply = NULL;
181 char *reply_message_text = NULL;
183 struct CtdlMessage *msg = NULL;
184 struct recptypes *valid = NULL;
186 strcpy(organizer_string, "");
187 strcpy(summary_string, "Calendar item");
189 if (request == NULL) {
190 syslog(LOG_ERR, "ERROR: trying to reply to NULL event?\n");
194 the_reply = icalcomponent_new_clone(request);
195 if (the_reply == NULL) {
196 syslog(LOG_ERR, "ERROR: cannot clone request\n");
200 /* Change the method from REQUEST to REPLY */
201 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
203 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
204 if (vevent != NULL) {
205 /* Hunt for attendees, removing ones that aren't us.
206 * (Actually, remove them all, cloning our own one so we can
207 * re-insert it later)
209 while (attendee = icalcomponent_get_first_property(vevent,
210 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
212 ch = icalproperty_get_attendee(attendee);
213 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
214 safestrncpy(attendee_string, ch + 7, sizeof (attendee_string));
215 striplt(attendee_string);
216 recp = validate_recipients(attendee_string, NULL, 0);
218 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
219 if (me_attend) icalproperty_free(me_attend);
220 me_attend = icalproperty_new_clone(attendee);
222 free_recipients(recp);
227 icalcomponent_remove_property(vevent, attendee);
228 icalproperty_free(attendee);
231 /* We found our own address in the attendee list. */
233 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
234 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
236 if (!strcasecmp(action, "accept")) {
237 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
239 else if (!strcasecmp(action, "decline")) {
240 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
242 else if (!strcasecmp(action, "tentative")) {
243 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
246 if (partstat) icalproperty_add_parameter(me_attend, partstat);
248 /* Now insert it back into the vevent. */
249 icalcomponent_add_property(vevent, me_attend);
252 /* Figure out who to send this thing to */
253 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
254 if (organizer != NULL) {
255 if (icalproperty_get_organizer(organizer)) {
256 strcpy(organizer_string,
257 icalproperty_get_organizer(organizer) );
260 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
261 strcpy(organizer_string, &organizer_string[7]);
262 striplt(organizer_string);
264 strcpy(organizer_string, "");
267 /* Extract the summary string -- we'll use it as the
268 * message subject for the reply
270 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
271 if (summary != NULL) {
272 if (icalproperty_get_summary(summary)) {
273 strcpy(summary_string,
274 icalproperty_get_summary(summary) );
279 /* Now generate the reply message and send it out. */
280 serialized_reply = icalcomponent_as_ical_string_r(the_reply);
281 icalcomponent_free(the_reply); /* don't need this anymore */
282 if (serialized_reply == NULL) return;
284 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
285 if (reply_message_text != NULL) {
286 sprintf(reply_message_text,
287 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
291 msg = CtdlMakeMessage(&CC->user,
292 organizer_string, /* to */
294 CC->room.QRname, 0, FMT_RFC822,
297 summary_string, /* Use summary for subject */
303 valid = validate_recipients(organizer_string, NULL, 0);
304 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
305 CtdlFreeMessage(msg);
306 free_recipients(valid);
309 free(serialized_reply);
315 * Callback function for mime parser that hunts for calendar content types
316 * and turns them into calendar objects. If something is found, it is placed
317 * in ird->cal, and the caller now owns that memory and is responsible for freeing it.
319 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
320 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
321 char *cbid, void *cbuserdata) {
323 struct ical_respond_data *ird = NULL;
325 ird = (struct ical_respond_data *) cbuserdata;
327 /* desired_partnum can be set to "_HUNT_" to have it just look for
328 * the first part with a content type of text/calendar. Otherwise
329 * we have to only process the right one.
331 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
332 if (strcasecmp(partnum, ird->desired_partnum)) {
337 if ( (strcasecmp(cbtype, "text/calendar"))
338 && (strcasecmp(cbtype, "application/ics")) ) {
342 if (ird->cal != NULL) {
343 icalcomponent_free(ird->cal);
347 ird->cal = icalcomponent_new_from_string(content);
352 * Respond to a meeting request.
354 void ical_respond(long msgnum, char *partnum, char *action) {
355 struct CtdlMessage *msg = NULL;
356 struct ical_respond_data ird;
359 (strcasecmp(action, "accept"))
360 && (strcasecmp(action, "decline"))
362 cprintf("%d Action must be 'accept' or 'decline'\n",
363 ERROR + ILLEGAL_VALUE
368 msg = CtdlFetchMessage(msgnum, 1);
370 cprintf("%d Message %ld not found.\n",
371 ERROR + ILLEGAL_VALUE,
377 memset(&ird, 0, sizeof ird);
378 strcpy(ird.desired_partnum, partnum);
379 mime_parser(msg->cm_fields['M'],
381 *ical_locate_part, /* callback function */
383 (void *) &ird, /* user data */
387 /* We're done with the incoming message, because we now have a
388 * calendar object in memory.
390 CtdlFreeMessage(msg);
393 * Here is the real meat of this function. Handle the event.
395 if (ird.cal != NULL) {
396 /* Save this in the user's calendar if necessary */
397 if (!strcasecmp(action, "accept")) {
398 ical_write_to_cal(&CC->user, ird.cal);
401 /* Send a reply if necessary */
402 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
403 ical_send_a_reply(ird.cal, action);
406 /* We used to delete the invitation after handling it.
407 * We don't do that anymore, but here is the code that handled it:
408 * CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
411 /* Free the memory we allocated and return a response. */
412 icalcomponent_free(ird.cal);
414 cprintf("%d ok\n", CIT_OK);
418 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
422 /* should never get here */
427 * Figure out the UID of the calendar event being referred to in a
428 * REPLY object. This function is recursive.
430 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
431 icalcomponent *subcomponent;
434 /* If this object is a REPLY, then extract the UID. */
435 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
436 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
438 strcpy(uidbuf, icalproperty_get_comment(p));
442 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
443 * UID of the reply; we want the UID of the invitation being replied to.
445 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
446 subcomponent != NULL;
447 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
448 ical_learn_uid_of_reply(uidbuf, subcomponent);
454 * ical_update_my_calendar_with_reply() refers to this callback function; when we
455 * locate the message containing the calendar event we're replying to, this function
456 * gets called. It basically just sticks the message number in a supplied buffer.
458 void ical_hunt_for_event_to_update(long msgnum, void *data) {
461 msgnumptr = (long *) data;
466 struct original_event_container {
471 * Callback function for mime parser that hunts for calendar content types
472 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
473 * to fetch the object being updated)
475 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
476 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
477 char *cbid, void *cbuserdata) {
479 struct original_event_container *oec = NULL;
481 if ( (strcasecmp(cbtype, "text/calendar"))
482 && (strcasecmp(cbtype, "application/ics")) ) {
485 oec = (struct original_event_container *) cbuserdata;
486 if (oec->c != NULL) {
487 icalcomponent_free(oec->c);
489 oec->c = icalcomponent_new_from_string(content);
494 * Merge updated attendee information from a REPLY into an existing event.
496 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
498 icalproperty *e_attendee, *r_attendee;
500 /* First things first. If we're not looking at a VEVENT component,
501 * recurse through subcomponents until we find one.
503 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
504 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
506 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
507 ical_merge_attendee_reply(c, reply);
512 /* Now do the same thing with the reply.
514 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
515 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
517 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
518 ical_merge_attendee_reply(event, c);
523 /* Clone the reply, because we're going to rip its guts out. */
524 reply = icalcomponent_new_clone(reply);
526 /* At this point we're looking at the correct subcomponents.
527 * Iterate through the attendees looking for a match.
530 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
532 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
534 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
536 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
538 /* Check to see if these two attendees match...
541 e = icalproperty_get_attendee(e_attendee);
542 r = icalproperty_get_attendee(r_attendee);
547 /* ...and if they do, remove the attendee from the event
548 * and replace it with the attendee from the reply. (The
549 * reply's copy will have the same address, but an updated
552 icalcomponent_remove_property(event, e_attendee);
553 icalproperty_free(e_attendee);
554 icalcomponent_remove_property(reply, r_attendee);
555 icalcomponent_add_property(event, r_attendee);
557 /* Since we diddled both sets of attendees, we have to start
558 * the iteration over again. This will not create an infinite
559 * loop because we removed the attendee from the reply. (That's
560 * why we cloned the reply, and that's what we mean by "ripping
569 /* Free the *clone* of the reply. */
570 icalcomponent_free(reply);
577 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
578 * calendar event. The object has already been deserialized for us; all
579 * we have to do here is hunt for the event in our calendar, merge in the
580 * updated attendee status, and save it again.
582 * This function returns 0 on success, 1 if the event was not found in the
583 * user's calendar, or 2 if an internal error occurred.
585 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
587 char hold_rm[ROOMNAMELEN];
588 long msgnum_being_replaced = 0;
589 struct CtdlMessage *msg = NULL;
590 struct original_event_container oec;
591 icalcomponent *original_event;
592 char *serialized_event = NULL;
593 char roomname[ROOMNAMELEN];
594 char *message_text = NULL;
596 /* Figure out just what event it is we're dealing with */
597 strcpy(uid, "--==<< InVaLiD uId >>==--");
598 ical_learn_uid_of_reply(uid, cal);
599 syslog(LOG_DEBUG, "UID of event being replied to is <%s>\n", uid);
601 strcpy(hold_rm, CC->room.QRname); /* save current room */
603 if (CtdlGetRoom(&CC->room, USERCALENDARROOM) != 0) {
604 CtdlGetRoom(&CC->room, hold_rm);
605 syslog(LOG_CRIT, "cannot get user calendar room\n");
610 * Look in the EUID index for a message with
611 * the Citadel EUID set to the value we're looking for. Since
612 * Citadel always sets the message EUID to the iCalendar UID of
613 * the event, this will work.
615 msgnum_being_replaced = CtdlLocateMessageByEuid(uid, &CC->room);
617 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
619 syslog(LOG_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
620 if (msgnum_being_replaced == 0) {
621 return(1); /* no calendar event found */
624 /* Now we know the ID of the message containing the event being updated.
625 * We don't actually have to delete it; that'll get taken care of by the
626 * server when we save another event with the same UID. This just gives
627 * us the ability to load the event into memory so we can diddle the
630 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
632 return(2); /* internal error */
635 mime_parser(msg->cm_fields['M'],
637 *ical_locate_original_event, /* callback function */
639 &oec, /* user data */
642 CtdlFreeMessage(msg);
644 original_event = oec.c;
645 if (original_event == NULL) {
646 syslog(LOG_ERR, "ERROR: Original_component is NULL.\n");
650 /* Merge the attendee's updated status into the event */
651 ical_merge_attendee_reply(original_event, cal);
654 serialized_event = icalcomponent_as_ical_string_r(original_event);
655 icalcomponent_free(original_event); /* Don't need this anymore. */
656 if (serialized_event == NULL) return(2);
658 CtdlMailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
660 message_text = malloc(strlen(serialized_event) + SIZ);
661 if (message_text != NULL) {
662 sprintf(message_text,
663 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
667 msg = CtdlMakeMessage(&CC->user,
668 "", /* No recipient */
669 "", /* No recipient */
680 CIT_ICAL->avoid_sending_invitations = 1;
681 CtdlSubmitMsg(msg, NULL, roomname, QP_EADDR);
682 CtdlFreeMessage(msg);
683 CIT_ICAL->avoid_sending_invitations = 0;
686 free(serialized_event);
692 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
693 * simply extracts the calendar object from the message, deserializes it, and
694 * passes it up to ical_update_my_calendar_with_reply() for processing.
696 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
697 struct CtdlMessage *msg = NULL;
698 struct ical_respond_data ird;
702 (strcasecmp(action, "update"))
703 && (strcasecmp(action, "ignore"))
705 cprintf("%d Action must be 'update' or 'ignore'\n",
706 ERROR + ILLEGAL_VALUE
711 msg = CtdlFetchMessage(msgnum, 1);
713 cprintf("%d Message %ld not found.\n",
714 ERROR + ILLEGAL_VALUE,
720 memset(&ird, 0, sizeof ird);
721 strcpy(ird.desired_partnum, partnum);
722 mime_parser(msg->cm_fields['M'],
724 *ical_locate_part, /* callback function */
726 (void *) &ird, /* user data */
730 /* We're done with the incoming message, because we now have a
731 * calendar object in memory.
733 CtdlFreeMessage(msg);
736 * Here is the real meat of this function. Handle the event.
738 if (ird.cal != NULL) {
739 /* Update the user's calendar if necessary */
740 if (!strcasecmp(action, "update")) {
741 ret = ical_update_my_calendar_with_reply(ird.cal);
743 cprintf("%d Your calendar has been updated with this reply.\n",
747 cprintf("%d This event does not exist in your calendar.\n",
748 ERROR + FILE_NOT_FOUND);
751 cprintf("%d An internal error occurred.\n",
752 ERROR + INTERNAL_ERROR);
756 cprintf("%d This reply has been ignored.\n", CIT_OK);
759 /* Now that we've processed this message, we don't need it
760 * anymore. So delete it. (Don't do this anymore.)
761 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
764 /* Free the memory we allocated and return a response. */
765 icalcomponent_free(ird.cal);
770 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
774 /* should never get here */
779 * Search for a property in both the top level and in a VEVENT subcomponent
781 icalproperty *ical_ctdl_get_subprop(
783 icalproperty_kind which_prop
788 p = icalcomponent_get_first_property(cal, which_prop);
790 c = icalcomponent_get_first_component(cal,
791 ICAL_VEVENT_COMPONENT);
793 p = icalcomponent_get_first_property(c, which_prop);
801 * Check to see if two events overlap. Returns nonzero if they do.
802 * (This function is used in both Citadel and WebCit. If you change it in
803 * one place, change it in the other. Better yet, put it in a library.)
805 int ical_ctdl_is_overlap(
806 struct icaltimetype t1start,
807 struct icaltimetype t1end,
808 struct icaltimetype t2start,
809 struct icaltimetype t2end
811 if (icaltime_is_null_time(t1start)) return(0);
812 if (icaltime_is_null_time(t2start)) return(0);
814 /* if either event lacks end time, assume end = start */
815 if (icaltime_is_null_time(t1end))
816 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
818 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
820 * the end date is non-inclusive so adjust it by one
821 * day because our test is inclusive, note that a day is
822 * not too much because we are talking about all day
824 * if start = end we assume that nevertheless the whole
827 icaltime_adjust(&t1end, -1, 0, 0, 0);
831 if (icaltime_is_null_time(t2end))
832 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
834 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
835 icaltime_adjust(&t2end, -1, 0, 0, 0);
839 /* First, check for all-day events */
840 if (t1start.is_date || t2start.is_date) {
841 /* If event 1 ends before event 2 starts, we're in the clear. */
842 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
844 /* If event 2 ends before event 1 starts, we're also ok. */
845 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
850 /* syslog(LOG_DEBUG, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
851 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
852 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
855 /* Now check for overlaps using date *and* time. */
857 /* If event 1 ends before event 2 starts, we're in the clear. */
858 if (icaltime_compare(t1end, t2start) <= 0) return(0);
859 /* syslog(LOG_DEBUG, "first passed\n"); */
861 /* If event 2 ends before event 1 starts, we're also ok. */
862 if (icaltime_compare(t2end, t1start) <= 0) return(0);
863 /* syslog(LOG_DEBUG, "second passed\n"); */
865 /* Otherwise, they overlap. */
870 * Phase 6 of "hunt for conflicts"
871 * called by ical_conflicts_phase5()
873 * Now both the proposed and existing events have been boiled down to start and end times.
874 * Check for overlap and output any conflicts.
876 * Returns nonzero if a conflict was reported. This allows the caller to stop iterating.
878 int ical_conflicts_phase6(struct icaltimetype t1start,
879 struct icaltimetype t1end,
880 struct icaltimetype t2start,
881 struct icaltimetype t2end,
882 long existing_msgnum,
883 char *conflict_event_uid,
884 char *conflict_event_summary,
887 int conflict_reported = 0;
891 tt = icaltime_as_timet_with_zone(t1start, t1start.zone);
892 syslog(LOG_DEBUG, "PROPOSED START: %s", ctime(&tt));
893 tt = icaltime_as_timet_with_zone(t1end, t1end.zone);
894 syslog(LOG_DEBUG, " PROPOSED END: %s", ctime(&tt));
895 tt = icaltime_as_timet_with_zone(t2start, t2start.zone);
896 syslog(LOG_DEBUG, "EXISTING START: %s", ctime(&tt));
897 tt = icaltime_as_timet_with_zone(t2end, t2end.zone);
898 syslog(LOG_DEBUG, " EXISTING END: %s", ctime(&tt));
901 /* compare and output */
903 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
904 cprintf("%ld||%s|%s|%d|\n",
907 conflict_event_summary,
908 ( ((strlen(compare_uid)>0)
909 &&(!strcasecmp(compare_uid,
910 conflict_event_uid))) ? 1 : 0
913 conflict_reported = 1;
916 return(conflict_reported);
922 * Phase 5 of "hunt for conflicts"
923 * Called by ical_conflicts_phase4()
925 * We have the proposed event boiled down to start and end times.
926 * Now check it against an existing event.
928 void ical_conflicts_phase5(struct icaltimetype t1start,
929 struct icaltimetype t1end,
930 icalcomponent *existing_event,
931 long existing_msgnum,
934 char conflict_event_uid[SIZ];
935 char conflict_event_summary[SIZ];
936 struct icaltimetype t2start, t2end;
939 /* recur variables */
940 icalproperty *rrule = NULL;
941 struct icalrecurrencetype recur;
942 icalrecur_iterator *ritr = NULL;
943 struct icaldurationtype dur;
947 strcpy(conflict_event_uid, "");
948 strcpy(conflict_event_summary, "");
949 t2start = icaltime_null_time();
950 t2end = icaltime_null_time();
952 /* existing event stuff */
953 p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
954 if (p == NULL) return;
955 if (p != NULL) t2start = icalproperty_get_dtstart(p);
956 if (icaltime_is_utc(t2start)) {
957 t2start.zone = icaltimezone_get_utc_timezone();
960 t2start.zone = icalcomponent_get_timezone(existing_event,
961 icalparameter_get_tzid(
962 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
966 t2start.zone = get_default_icaltimezone();
970 p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
972 t2end = icalproperty_get_dtend(p);
974 if (icaltime_is_utc(t2end)) {
975 t2end.zone = icaltimezone_get_utc_timezone();
978 t2end.zone = icalcomponent_get_timezone(existing_event,
979 icalparameter_get_tzid(
980 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
984 t2end.zone = get_default_icaltimezone();
987 dur = icaltime_subtract(t2end, t2start);
990 memset (&dur, 0, sizeof(struct icaldurationtype));
993 rrule = ical_ctdl_get_subprop(existing_event, ICAL_RRULE_PROPERTY);
995 recur = icalproperty_get_rrule(rrule);
996 ritr = icalrecur_iterator_new(recur, t2start);
1000 p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
1002 strcpy(conflict_event_uid, icalproperty_get_comment(p));
1005 p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
1007 strcpy(conflict_event_summary, icalproperty_get_comment(p));
1010 if (ical_conflicts_phase6(t1start, t1end, t2start, t2end,
1011 existing_msgnum, conflict_event_uid, conflict_event_summary, compare_uid))
1013 num_recur = MAX_RECUR + 1; /* force it out of scope, no need to continue */
1017 t2start = icalrecur_iterator_next(ritr);
1018 if (!icaltime_is_null_time(t2end)) {
1019 const icaltimezone *hold_zone = t2end.zone;
1020 t2end = icaltime_add(t2start, dur);
1021 t2end.zone = hold_zone;
1026 if (icaltime_compare(t2start, t1end) < 0) {
1027 num_recur = MAX_RECUR + 1; /* force it out of scope */
1030 } while ( (rrule) && (!icaltime_is_null_time(t2start)) && (num_recur < MAX_RECUR) );
1031 icalrecur_iterator_free(ritr);
1038 * Phase 4 of "hunt for conflicts"
1039 * Called by ical_hunt_for_conflicts_backend()
1041 * At this point we've got it boiled down to two icalcomponent events in memory.
1042 * If they conflict, output something to the client.
1044 void ical_conflicts_phase4(icalcomponent *proposed_event,
1045 icalcomponent *existing_event,
1046 long existing_msgnum)
1048 struct icaltimetype t1start, t1end;
1049 t1start = icaltime_null_time();
1050 t1end = icaltime_null_time();
1052 char compare_uid[SIZ];
1054 /* recur variables */
1055 icalproperty *rrule = NULL;
1056 struct icalrecurrencetype recur;
1057 icalrecur_iterator *ritr = NULL;
1058 struct icaldurationtype dur;
1061 /* initialization */
1062 *compare_uid = '\0';
1064 /* proposed event stuff */
1066 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
1070 t1start = icalproperty_get_dtstart(p);
1072 if (icaltime_is_utc(t1start)) {
1073 t1start.zone = icaltimezone_get_utc_timezone();
1076 t1start.zone = icalcomponent_get_timezone(proposed_event,
1077 icalparameter_get_tzid(
1078 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1081 if (!t1start.zone) {
1082 t1start.zone = get_default_icaltimezone();
1086 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
1088 t1end = icalproperty_get_dtend(p);
1090 if (icaltime_is_utc(t1end)) {
1091 t1end.zone = icaltimezone_get_utc_timezone();
1094 t1end.zone = icalcomponent_get_timezone(proposed_event,
1095 icalparameter_get_tzid(
1096 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1100 t1end.zone = get_default_icaltimezone();
1104 dur = icaltime_subtract(t1end, t1start);
1107 memset (&dur, 0, sizeof(struct icaldurationtype));
1110 rrule = ical_ctdl_get_subprop(proposed_event, ICAL_RRULE_PROPERTY);
1112 recur = icalproperty_get_rrule(rrule);
1113 ritr = icalrecur_iterator_new(recur, t1start);
1116 p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
1118 strcpy(compare_uid, icalproperty_get_comment(p));
1122 ical_conflicts_phase5(t1start, t1end, existing_event, existing_msgnum, compare_uid);
1125 t1start = icalrecur_iterator_next(ritr);
1126 if (!icaltime_is_null_time(t1end)) {
1127 const icaltimezone *hold_zone = t1end.zone;
1128 t1end = icaltime_add(t1start, dur);
1129 t1end.zone = hold_zone;
1134 } while ( (rrule) && (!icaltime_is_null_time(t1start)) && (num_recur < MAX_RECUR) );
1135 icalrecur_iterator_free(ritr);
1141 * Phase 3 of "hunt for conflicts"
1142 * Called by ical_hunt_for_conflicts()
1144 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
1145 icalcomponent *proposed_event;
1146 struct CtdlMessage *msg = NULL;
1147 struct ical_respond_data ird;
1149 proposed_event = (icalcomponent *)data;
1151 msg = CtdlFetchMessage(msgnum, 1);
1152 if (msg == NULL) return;
1153 memset(&ird, 0, sizeof ird);
1154 strcpy(ird.desired_partnum, "_HUNT_");
1155 mime_parser(msg->cm_fields['M'],
1157 *ical_locate_part, /* callback function */
1159 (void *) &ird, /* user data */
1162 CtdlFreeMessage(msg);
1164 if (ird.cal == NULL) return;
1166 ical_conflicts_phase4(proposed_event, ird.cal, msgnum);
1167 icalcomponent_free(ird.cal);
1173 * Phase 2 of "hunt for conflicts" operation.
1174 * At this point we have a calendar object which represents the VEVENT that
1175 * is proposed for addition to the calendar. Now hunt through the user's
1176 * calendar room, and output zero or more existing VEVENTs which conflict
1179 void ical_hunt_for_conflicts(icalcomponent *cal) {
1180 char hold_rm[ROOMNAMELEN];
1182 strcpy(hold_rm, CC->room.QRname); /* save current room */
1184 if (CtdlGetRoom(&CC->room, USERCALENDARROOM) != 0) {
1185 CtdlGetRoom(&CC->room, hold_rm);
1186 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1190 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1192 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1195 ical_hunt_for_conflicts_backend,
1200 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
1207 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1209 void ical_conflicts(long msgnum, char *partnum) {
1210 struct CtdlMessage *msg = NULL;
1211 struct ical_respond_data ird;
1213 msg = CtdlFetchMessage(msgnum, 1);
1215 cprintf("%d Message %ld not found\n",
1216 ERROR + ILLEGAL_VALUE,
1222 memset(&ird, 0, sizeof ird);
1223 strcpy(ird.desired_partnum, partnum);
1224 mime_parser(msg->cm_fields['M'],
1226 *ical_locate_part, /* callback function */
1228 (void *) &ird, /* user data */
1232 CtdlFreeMessage(msg);
1234 if (ird.cal != NULL) {
1235 ical_hunt_for_conflicts(ird.cal);
1236 icalcomponent_free(ird.cal);
1240 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1246 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1248 * fb The VFREEBUSY component to which we are appending
1249 * top_level_cal The top-level VCALENDAR component which contains a VEVENT to be added
1251 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal) {
1255 struct icalperiodtype this_event_period = icalperiodtype_null_period();
1256 icaltimetype dtstart;
1259 /* recur variables */
1260 icalproperty *rrule = NULL;
1261 struct icalrecurrencetype recur;
1262 icalrecur_iterator *ritr = NULL;
1263 struct icaldurationtype dur;
1266 if (!top_level_cal) return;
1268 /* Find the VEVENT component containing an event */
1269 cal = icalcomponent_get_first_component(top_level_cal, ICAL_VEVENT_COMPONENT);
1272 /* If this event is not opaque, the user isn't publishing it as
1273 * busy time, so don't bother doing anything else.
1275 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1277 v = icalproperty_get_value(p);
1279 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1286 * Now begin calculating the event start and end times.
1288 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1290 dtstart = icalproperty_get_dtstart(p);
1292 if (icaltime_is_utc(dtstart)) {
1293 dtstart.zone = icaltimezone_get_utc_timezone();
1296 dtstart.zone = icalcomponent_get_timezone(top_level_cal,
1297 icalparameter_get_tzid(
1298 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1301 if (!dtstart.zone) {
1302 dtstart.zone = get_default_icaltimezone();
1306 dtend = icalcomponent_get_dtend(cal);
1307 if (!icaltime_is_null_time(dtend)) {
1308 dur = icaltime_subtract(dtend, dtstart);
1311 memset (&dur, 0, sizeof(struct icaldurationtype));
1314 /* Is a recurrence specified? If so, get ready to process it... */
1315 rrule = ical_ctdl_get_subprop(cal, ICAL_RRULE_PROPERTY);
1317 recur = icalproperty_get_rrule(rrule);
1318 ritr = icalrecur_iterator_new(recur, dtstart);
1322 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1323 this_event_period.start = dtstart;
1325 if (!icaltime_is_null_time(dtend)) {
1326 this_event_period.end = dtend;
1329 /* Convert the timestamps to UTC. It's ok to do this because we've already expanded
1330 * recurrences and this data is never going to get used again.
1332 this_event_period.start = icaltime_convert_to_zone(
1333 this_event_period.start,
1334 icaltimezone_get_utc_timezone()
1336 this_event_period.end = icaltime_convert_to_zone(
1337 this_event_period.end,
1338 icaltimezone_get_utc_timezone()
1342 icalcomponent_add_property(fb, icalproperty_new_freebusy(this_event_period));
1344 /* Make sure the DTSTART property of the freebusy *list* is set to
1345 * the DTSTART property of the *earliest event*.
1347 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1349 icalcomponent_set_dtstart(fb, this_event_period.start);
1352 if (icaltime_compare(this_event_period.start, icalcomponent_get_dtstart(fb)) < 0) {
1353 icalcomponent_set_dtstart(fb, this_event_period.start);
1357 /* Make sure the DTEND property of the freebusy *list* is set to
1358 * the DTEND property of the *latest event*.
1360 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1362 icalcomponent_set_dtend(fb, this_event_period.end);
1365 if (icaltime_compare(this_event_period.end, icalcomponent_get_dtend(fb)) > 0) {
1366 icalcomponent_set_dtend(fb, this_event_period.end);
1371 dtstart = icalrecur_iterator_next(ritr);
1372 if (!icaltime_is_null_time(dtend)) {
1373 dtend = icaltime_add(dtstart, dur);
1374 dtend.zone = dtstart.zone;
1375 dtend.is_utc = dtstart.is_utc;
1380 } while ( (rrule) && (!icaltime_is_null_time(dtstart)) && (num_recur < MAX_RECUR) ) ;
1381 icalrecur_iterator_free(ritr);
1387 * Backend for ical_freebusy()
1389 * This function simply loads the messages in the user's calendar room,
1390 * which contain VEVENTs, then strips them of all non-freebusy data, and
1391 * adds them to the supplied VCALENDAR.
1394 void ical_freebusy_backend(long msgnum, void *data) {
1396 struct CtdlMessage *msg = NULL;
1397 struct ical_respond_data ird;
1399 fb = (icalcomponent *)data; /* User-supplied data will be the VFREEBUSY component */
1401 msg = CtdlFetchMessage(msgnum, 1);
1402 if (msg == NULL) return;
1403 memset(&ird, 0, sizeof ird);
1404 strcpy(ird.desired_partnum, "_HUNT_");
1405 mime_parser(msg->cm_fields['M'],
1407 *ical_locate_part, /* callback function */
1409 (void *) &ird, /* user data */
1412 CtdlFreeMessage(msg);
1415 ical_add_to_freebusy(fb, ird.cal); /* Add VEVENT times to VFREEBUSY */
1416 icalcomponent_free(ird.cal);
1423 * Grab another user's free/busy times
1425 void ical_freebusy(char *who) {
1426 struct ctdluser usbuf;
1427 char calendar_room_name[ROOMNAMELEN];
1428 char hold_rm[ROOMNAMELEN];
1429 char *serialized_request = NULL;
1430 icalcomponent *encaps = NULL;
1431 icalcomponent *fb = NULL;
1432 int found_user = (-1);
1433 struct recptypes *recp = NULL;
1438 int config_lines = 0;
1440 /* First try an exact match. */
1441 found_user = CtdlGetUser(&usbuf, who);
1443 /* If not found, try it as an unqualified email address. */
1444 if (found_user != 0) {
1446 recp = validate_recipients(buf, NULL, 0);
1447 syslog(LOG_DEBUG, "Trying <%s>\n", buf);
1449 if (recp->num_local == 1) {
1450 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1452 free_recipients(recp);
1456 /* If still not found, try it as an address qualified with the
1457 * primary FQDN of this Citadel node.
1459 if (found_user != 0) {
1460 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1461 syslog(LOG_DEBUG, "Trying <%s>\n", buf);
1462 recp = validate_recipients(buf, NULL, 0);
1464 if (recp->num_local == 1) {
1465 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1467 free_recipients(recp);
1471 /* Still not found? Try qualifying it with every domain we
1472 * might have addresses in.
1474 if (found_user != 0) {
1475 config_lines = num_tokens(inetcfg, '\n');
1476 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1477 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1478 extract_token(host, buf, 0, '|', sizeof host);
1479 extract_token(type, buf, 1, '|', sizeof type);
1481 if ( (!strcasecmp(type, "localhost"))
1482 || (!strcasecmp(type, "directory")) ) {
1483 snprintf(buf, sizeof buf, "%s@%s", who, host);
1484 syslog(LOG_DEBUG, "Trying <%s>\n", buf);
1485 recp = validate_recipients(buf, NULL, 0);
1487 if (recp->num_local == 1) {
1488 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1490 free_recipients(recp);
1496 if (found_user != 0) {
1497 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1501 CtdlMailboxName(calendar_room_name, sizeof calendar_room_name,
1502 &usbuf, USERCALENDARROOM);
1504 strcpy(hold_rm, CC->room.QRname); /* save current room */
1506 if (CtdlGetRoom(&CC->room, calendar_room_name) != 0) {
1507 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1508 CtdlGetRoom(&CC->room, hold_rm);
1512 /* Create a VFREEBUSY subcomponent */
1513 syslog(LOG_DEBUG, "Creating VFREEBUSY component\n");
1514 fb = icalcomponent_new_vfreebusy();
1516 cprintf("%d Internal error: cannot allocate memory.\n",
1517 ERROR + INTERNAL_ERROR);
1518 CtdlGetRoom(&CC->room, hold_rm);
1522 /* Set the method to PUBLISH */
1523 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1525 /* Set the DTSTAMP to right now. */
1526 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1528 /* Add the user's email address as ORGANIZER */
1529 sprintf(buf, "MAILTO:%s", who);
1530 if (strchr(buf, '@') == NULL) {
1532 strcat(buf, config.c_fqdn);
1534 for (i=0; buf[i]; ++i) {
1535 if (buf[i]==' ') buf[i] = '_';
1537 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1539 /* Add busy time from events */
1540 syslog(LOG_DEBUG, "Adding busy time from events\n");
1541 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1543 /* If values for DTSTART and DTEND are still not present, set them
1544 * to yesterday and tomorrow as default values.
1546 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1547 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1549 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1550 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1553 /* Put the freebusy component into the calendar component */
1554 syslog(LOG_DEBUG, "Encapsulating\n");
1555 encaps = ical_encapsulate_subcomponent(fb);
1556 if (encaps == NULL) {
1557 icalcomponent_free(fb);
1558 cprintf("%d Internal error: cannot allocate memory.\n",
1559 ERROR + INTERNAL_ERROR);
1560 CtdlGetRoom(&CC->room, hold_rm);
1564 /* Set the method to PUBLISH */
1565 syslog(LOG_DEBUG, "Setting method\n");
1566 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1569 syslog(LOG_DEBUG, "Serializing\n");
1570 serialized_request = icalcomponent_as_ical_string_r(encaps);
1571 icalcomponent_free(encaps); /* Don't need this anymore. */
1573 cprintf("%d Free/busy for %s\n", LISTING_FOLLOWS, usbuf.fullname);
1574 if (serialized_request != NULL) {
1575 client_write(serialized_request, strlen(serialized_request));
1576 free(serialized_request);
1580 /* Go back to the room from which we came... */
1581 CtdlGetRoom(&CC->room, hold_rm);
1587 * Backend for ical_getics()
1589 * This is a ForEachMessage() callback function that searches the current room
1590 * for calendar events and adds them each into one big calendar component.
1592 void ical_getics_backend(long msgnum, void *data) {
1593 icalcomponent *encaps, *c;
1594 struct CtdlMessage *msg = NULL;
1595 struct ical_respond_data ird;
1597 encaps = (icalcomponent *)data;
1598 if (encaps == NULL) return;
1600 /* Look for the calendar event... */
1602 msg = CtdlFetchMessage(msgnum, 1);
1603 if (msg == NULL) return;
1604 memset(&ird, 0, sizeof ird);
1605 strcpy(ird.desired_partnum, "_HUNT_");
1606 mime_parser(msg->cm_fields['M'],
1608 *ical_locate_part, /* callback function */
1610 (void *) &ird, /* user data */
1613 CtdlFreeMessage(msg);
1615 if (ird.cal == NULL) return;
1617 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1618 * are responsible for "the_request"'s memory -- it will be freed
1619 * when we free "encaps".
1622 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1623 * in. This will almost never happen.
1625 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1626 icalcomponent_add_component(encaps, ird.cal);
1629 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1630 * and other components encapsulated inside, we have to extract them.
1633 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1635 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1637 /* For VTIMEZONE components, suppress duplicates of the same tzid */
1639 if (icalcomponent_isa(c) == ICAL_VTIMEZONE_COMPONENT) {
1640 icalproperty *p = icalcomponent_get_first_property(c, ICAL_TZID_PROPERTY);
1642 const char *tzid = icalproperty_get_tzid(p);
1643 if (!icalcomponent_get_timezone(encaps, tzid)) {
1644 icalcomponent_add_component(encaps,
1645 icalcomponent_new_clone(c));
1650 /* All other types of components can go in verbatim */
1652 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1655 icalcomponent_free(ird.cal);
1662 * Retrieve all of the calendar items in the current room, and output them
1663 * as a single icalendar object.
1665 void ical_getics(void)
1667 icalcomponent *encaps = NULL;
1670 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1671 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1672 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1673 return; /* Not an iCalendar-centric room */
1676 encaps = icalcomponent_new_vcalendar();
1677 if (encaps == NULL) {
1678 syslog(LOG_ALERT, "ERROR: could not allocate component!\n");
1679 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1683 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1685 /* Set the Product ID */
1686 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1688 /* Set the Version Number */
1689 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1691 /* Set the method to PUBLISH */
1692 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1694 /* Now go through the room encapsulating all calendar items. */
1695 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1698 ical_getics_backend,
1702 ser = icalcomponent_as_ical_string_r(encaps);
1703 icalcomponent_free(encaps); /* Don't need this anymore. */
1704 client_write(ser, strlen(ser));
1711 * Helper callback function for ical_putics() to discover which TZID's we need.
1712 * Simply put the tzid name string into a hash table. After the callbacks are
1713 * done we'll go through them and attach the ones that we have.
1715 void ical_putics_grabtzids(icalparameter *param, void *data)
1717 const char *tzid = icalparameter_get_tzid(param);
1718 HashList *keys = (HashList *) data;
1720 if ( (keys) && (tzid) && (!IsEmptyStr(tzid)) ) {
1721 Put(keys, tzid, strlen(tzid), strdup(tzid), NULL);
1727 * Delete all of the calendar items in the current room, and replace them
1728 * with calendar items from a client-supplied data stream.
1730 void ical_putics(void)
1732 char *calstream = NULL;
1735 icalcomponent *encaps = NULL;
1736 HashList *tzidlist = NULL;
1742 /* Only allow this operation if we're in a room containing a calendar or tasks view */
1743 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1744 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1745 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1749 /* Only allow this operation if we have permission to overwrite the existing calendar */
1750 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1751 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1755 cprintf("%d Transmit data now\n", SEND_LISTING);
1756 calstream = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
1757 if (calstream == NULL) {
1761 cal = icalcomponent_new_from_string(calstream);
1764 /* We got our data stream -- now do something with it. */
1766 /* Delete the existing messages in the room, because we are overwriting
1767 * the entire calendar with an entire new (or updated) calendar.
1768 * (Careful: this opens an S_ROOMS critical section!)
1770 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1772 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1773 * in. This will almost never happen.
1775 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1776 ical_write_to_cal(NULL, cal);
1779 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1780 * and other components encapsulated inside, we have to extract them.
1783 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1785 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1787 /* Non-VTIMEZONE components each get written as individual messages.
1788 * But we also need to attach the relevant VTIMEZONE components to them.
1790 if ( (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT)
1791 && (encaps = icalcomponent_new_vcalendar()) ) {
1792 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1793 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1794 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1796 /* Attach any needed timezones here */
1797 tzidlist = NewHash(1, NULL);
1799 icalcomponent_foreach_tzid(c, ical_putics_grabtzids, tzidlist);
1801 HashPos = GetNewHashPos(tzidlist, 0);
1803 while (GetNextHashPos(tzidlist, HashPos, &len, &Key, &Value)) {
1804 syslog(LOG_DEBUG, "Attaching timezone '%s'\n", (char*) Value);
1805 icaltimezone *t = NULL;
1807 /* First look for a timezone attached to the original calendar */
1808 t = icalcomponent_get_timezone(cal, Value);
1810 /* Try built-in tzdata if the right one wasn't attached */
1812 t = icaltimezone_get_builtin_timezone(Value);
1815 /* I've got a valid timezone to attach. */
1817 icalcomponent_add_component(encaps,
1818 icalcomponent_new_clone(
1819 icaltimezone_get_component(t)
1825 DeleteHashPos(&HashPos);
1826 DeleteHash(&tzidlist);
1828 /* Now attach the component itself (usually a VEVENT or VTODO) */
1829 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1831 /* Write it to the message store */
1832 ical_write_to_cal(NULL, encaps);
1833 icalcomponent_free(encaps);
1838 icalcomponent_free(cal);
1843 * All Citadel calendar commands from the client come through here.
1845 void cmd_ical(char *argbuf)
1853 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1855 /* Allow "test" and "freebusy" subcommands without logging in. */
1857 if (!strcasecmp(subcmd, "test")) {
1858 cprintf("%d This server supports calendaring\n", CIT_OK);
1862 if (!strcasecmp(subcmd, "freebusy")) {
1863 extract_token(who, argbuf, 1, '|', sizeof who);
1868 if (!strcasecmp(subcmd, "sgi")) {
1869 CIT_ICAL->server_generated_invitations =
1870 (extract_int(argbuf, 1) ? 1 : 0) ;
1872 CIT_OK, CIT_ICAL->server_generated_invitations);
1876 if (CtdlAccessCheck(ac_logged_in)) return;
1878 if (!strcasecmp(subcmd, "respond")) {
1879 msgnum = extract_long(argbuf, 1);
1880 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1881 extract_token(action, argbuf, 3, '|', sizeof action);
1882 ical_respond(msgnum, partnum, action);
1886 if (!strcasecmp(subcmd, "handle_rsvp")) {
1887 msgnum = extract_long(argbuf, 1);
1888 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1889 extract_token(action, argbuf, 3, '|', sizeof action);
1890 ical_handle_rsvp(msgnum, partnum, action);
1894 if (!strcasecmp(subcmd, "conflicts")) {
1895 msgnum = extract_long(argbuf, 1);
1896 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1897 ical_conflicts(msgnum, partnum);
1901 if (!strcasecmp(subcmd, "getics")) {
1906 if (!strcasecmp(subcmd, "putics")) {
1911 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1917 * We don't know if the calendar room exists so we just create it at login
1919 void ical_CtdlCreateRoom(void)
1924 /* Create the calendar room if it doesn't already exist */
1925 CtdlCreateRoom(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1927 /* Set expiration policy to manual; otherwise objects will be lost! */
1928 if (CtdlGetRoomLock(&qr, USERCALENDARROOM)) {
1929 syslog(LOG_CRIT, "Couldn't get the user calendar room!\n");
1932 qr.QRep.expire_mode = EXPIRE_MANUAL;
1933 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1934 CtdlPutRoomLock(&qr);
1936 /* Set the view to a calendar view */
1937 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1938 vbuf.v_view = VIEW_CALENDAR;
1939 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1941 /* Create the tasks list room if it doesn't already exist */
1942 CtdlCreateRoom(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1944 /* Set expiration policy to manual; otherwise objects will be lost! */
1945 if (CtdlGetRoomLock(&qr, USERTASKSROOM)) {
1946 syslog(LOG_CRIT, "Couldn't get the user calendar room!\n");
1949 qr.QRep.expire_mode = EXPIRE_MANUAL;
1950 qr.QRdefaultview = VIEW_TASKS;
1951 CtdlPutRoomLock(&qr);
1953 /* Set the view to a task list view */
1954 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1955 vbuf.v_view = VIEW_TASKS;
1956 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1958 /* Create the notes room if it doesn't already exist */
1959 CtdlCreateRoom(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1961 /* Set expiration policy to manual; otherwise objects will be lost! */
1962 if (CtdlGetRoomLock(&qr, USERNOTESROOM)) {
1963 syslog(LOG_CRIT, "Couldn't get the user calendar room!\n");
1966 qr.QRep.expire_mode = EXPIRE_MANUAL;
1967 qr.QRdefaultview = VIEW_NOTES;
1968 CtdlPutRoomLock(&qr);
1970 /* Set the view to a notes view */
1971 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1972 vbuf.v_view = VIEW_NOTES;
1973 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1980 * ical_send_out_invitations() is called by ical_saving_vevent() when it finds a VEVENT.
1982 * top_level_cal is the highest available level calendar object.
1983 * cal is the subcomponent containing the VEVENT.
1985 * Note: if you change the encapsulation code here, change it in WebCit's ical_encapsulate_subcomponent()
1987 void ical_send_out_invitations(icalcomponent *top_level_cal, icalcomponent *cal) {
1988 icalcomponent *the_request = NULL;
1989 char *serialized_request = NULL;
1990 icalcomponent *encaps = NULL;
1991 char *request_message_text = NULL;
1992 struct CtdlMessage *msg = NULL;
1993 struct recptypes *valid = NULL;
1994 char attendees_string[SIZ];
1995 int num_attendees = 0;
1996 char this_attendee[256];
1997 icalproperty *attendee = NULL;
1998 char summary_string[SIZ];
1999 icalproperty *summary = NULL;
2002 struct icaltimetype t;
2003 const icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
2005 const icaltimezone *z;
2006 int num_zones_attached = 0;
2007 int zone_already_attached;
2008 icalparameter *tzidp = NULL;
2009 const char *tzidc = NULL;
2012 syslog(LOG_ERR, "ERROR: trying to reply to NULL event?\n");
2017 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
2018 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2019 ical_send_out_invitations(top_level_cal,
2020 icalcomponent_get_first_component(
2021 cal, ICAL_VEVENT_COMPONENT
2027 /* Clone the event */
2028 the_request = icalcomponent_new_clone(cal);
2029 if (the_request == NULL) {
2030 syslog(LOG_ERR, "ERROR: cannot clone calendar object\n");
2034 /* Extract the summary string -- we'll use it as the
2035 * message subject for the request
2037 strcpy(summary_string, "Meeting request");
2038 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
2039 if (summary != NULL) {
2040 if (icalproperty_get_summary(summary)) {
2041 strcpy(summary_string,
2042 icalproperty_get_summary(summary) );
2046 /* Determine who the recipients of this message are (the attendees) */
2047 strcpy(attendees_string, "");
2048 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
2049 const char *ch = icalproperty_get_attendee(attendee);
2050 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
2051 safestrncpy(this_attendee, ch + 7, sizeof(this_attendee));
2053 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
2054 snprintf(&attendees_string[strlen(attendees_string)],
2055 sizeof(attendees_string) - strlen(attendees_string),
2064 syslog(LOG_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
2066 /* If there are no attendees, there are no invitations to send, so...
2067 * don't bother putting one together! Punch out, Maverick!
2069 if (num_attendees == 0) {
2070 icalcomponent_free(the_request);
2074 /* Encapsulate the VEVENT component into a complete VCALENDAR */
2075 encaps = icalcomponent_new_vcalendar();
2076 if (encaps == NULL) {
2077 syslog(LOG_ALERT, "ERROR: could not allocate component!\n");
2078 icalcomponent_free(the_request);
2082 /* Set the Product ID */
2083 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
2085 /* Set the Version Number */
2086 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
2088 /* Set the method to REQUEST */
2089 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
2091 /* Look for properties containing timezone parameters, to see if we need to attach VTIMEZONEs */
2092 for (p = icalcomponent_get_first_property(the_request, ICAL_ANY_PROPERTY);
2094 p = icalcomponent_get_next_property(the_request, ICAL_ANY_PROPERTY))
2096 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
2097 || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
2098 || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
2099 || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
2100 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
2101 || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
2102 || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
2103 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
2104 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
2105 || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
2106 || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
2107 || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
2108 || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
2110 t = icalproperty_get_dtstart(p); // it's safe to use dtstart for all of them
2112 /* Determine the tzid in order for some of the conditions below to work */
2113 tzidp = icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER);
2115 tzidc = icalparameter_get_tzid(tzidp);
2121 /* First see if there's a timezone attached to the data structure itself */
2122 if (icaltime_is_utc(t)) {
2123 z = icaltimezone_get_utc_timezone();
2126 z = icaltime_get_timezone(t);
2129 /* If not, try to determine the tzid from the parameter using attached zones */
2130 if ((!z) && (tzidc)) {
2131 z = icalcomponent_get_timezone(top_level_cal, tzidc);
2134 /* Still no good? Try our internal database */
2135 if ((!z) && (tzidc)) {
2136 z = icaltimezone_get_builtin_timezone_from_tzid(tzidc);
2140 /* We have a valid timezone. Good. Now we need to attach it. */
2142 zone_already_attached = 0;
2143 for (i=0; i<5; ++i) {
2144 if (z == attached_zones[i]) {
2145 /* We've already got this one, no need to attach another. */
2146 ++zone_already_attached;
2149 if ((!zone_already_attached) && (num_zones_attached < 5)) {
2150 /* This is a new one, so attach it. */
2151 attached_zones[num_zones_attached++] = z;
2154 icalproperty_set_parameter(p,
2155 icalparameter_new_tzid(icaltimezone_get_tzid(z))
2161 /* Encapsulate any timezones we need */
2162 if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
2164 zc = icalcomponent_new_clone(icaltimezone_get_component(attached_zones[i]));
2165 icalcomponent_add_component(encaps, zc);
2168 /* Here we go: encapsulate the VEVENT into the VCALENDAR. We now no longer
2169 * are responsible for "the_request"'s memory -- it will be freed
2170 * when we free "encaps".
2172 icalcomponent_add_component(encaps, the_request);
2175 serialized_request = icalcomponent_as_ical_string_r(encaps);
2176 icalcomponent_free(encaps); /* Don't need this anymore. */
2177 if (serialized_request == NULL) return;
2179 reqsize = strlen(serialized_request) + SIZ;
2180 request_message_text = malloc(reqsize);
2181 if (request_message_text != NULL) {
2182 snprintf(request_message_text, reqsize,
2183 "Content-type: text/calendar\r\n\r\n%s\r\n",
2187 msg = CtdlMakeMessage(
2189 NULL, /* No single recipient here */
2190 NULL, /* No single recipient here */
2196 summary_string, /* Use summary for subject */
2198 request_message_text,
2203 valid = validate_recipients(attendees_string, NULL, 0);
2204 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
2205 CtdlFreeMessage(msg);
2206 free_recipients(valid);
2209 free(serialized_request);
2214 * When a calendar object is being saved, determine whether it's a VEVENT
2215 * and the user saving it is the organizer. If so, send out invitations
2216 * to any listed attendees.
2218 * This function is recursive. The caller can simply supply the same object
2219 * as both arguments. When it recurses it will alter the second argument
2220 * while holding on to the top level object. This allows us to go back and
2221 * grab things like time zones which might be attached.
2224 void ical_saving_vevent(icalcomponent *top_level_cal, icalcomponent *cal) {
2226 icalproperty *organizer = NULL;
2227 char organizer_string[SIZ];
2229 syslog(LOG_DEBUG, "ical_saving_vevent() has been called!\n");
2231 /* Don't send out invitations unless the client wants us to. */
2232 if (CIT_ICAL->server_generated_invitations == 0) {
2236 /* Don't send out invitations if we've been asked not to. */
2237 if (CIT_ICAL->avoid_sending_invitations > 0) {
2241 strcpy(organizer_string, "");
2243 * The VEVENT subcomponent is the one we're interested in.
2244 * Send out invitations if, and only if, this user is the Organizer.
2246 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
2247 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
2248 if (organizer != NULL) {
2249 if (icalproperty_get_organizer(organizer)) {
2250 strcpy(organizer_string,
2251 icalproperty_get_organizer(organizer));
2254 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
2255 strcpy(organizer_string, &organizer_string[7]);
2256 striplt(organizer_string);
2258 * If the user saving the event is listed as the
2259 * organizer, then send out invitations.
2261 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
2262 ical_send_out_invitations(top_level_cal, cal);
2267 /* If the component has subcomponents, recurse through them. */
2268 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2270 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2271 /* Recursively process subcomponent */
2272 ical_saving_vevent(top_level_cal, c);
2280 * Back end for ical_obj_beforesave()
2281 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
2282 * the summary of the event (becomes message subject),
2283 * and the start time (becomes message date/time).
2285 void ical_obj_beforesave_backend(char *name, char *filename, char *partnum,
2286 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2287 char *encoding, char *cbid, void *cbuserdata)
2289 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
2291 char new_uid[256] = "";
2292 char buf[1024] = "";
2293 struct CtdlMessage *msg = (struct CtdlMessage *) cbuserdata;
2297 /* We're only interested in calendar data. */
2298 if ( (strcasecmp(cbtype, "text/calendar"))
2299 && (strcasecmp(cbtype, "application/ics")) ) {
2303 /* Hunt for the UID and drop it in
2304 * the "user data" pointer for the MIME parser. When
2305 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2308 whole_cal = icalcomponent_new_from_string(content);
2311 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2312 nested_event = icalcomponent_get_first_component(
2313 cal, ICAL_VEVENT_COMPONENT);
2314 if (nested_event != NULL) {
2318 nested_todo = icalcomponent_get_first_component(
2319 cal, ICAL_VTODO_COMPONENT);
2320 if (nested_todo != NULL) {
2328 /* Set the message EUID to the iCalendar UID */
2330 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2332 /* If there's no uid we must generate one */
2333 generate_uuid(new_uid);
2334 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
2335 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2338 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
2339 if (!IsEmptyStr(buf)) {
2340 if (msg->cm_fields['E'] != NULL) {
2341 free(msg->cm_fields['E']);
2343 msg->cm_fields['E'] = strdup(buf);
2344 syslog(LOG_DEBUG, "Saving calendar UID <%s>\n", buf);
2348 /* Set the message subject to the iCalendar summary */
2350 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
2352 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
2353 if (!IsEmptyStr(buf)) {
2354 if (msg->cm_fields['U'] != NULL) {
2355 free(msg->cm_fields['U']);
2357 msg->cm_fields['U'] = rfc2047encode(buf, strlen(buf));
2361 /* Set the message date/time to the iCalendar start time */
2363 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
2366 idtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
2368 if (msg->cm_fields['T'] != NULL) {
2369 free(msg->cm_fields['T']);
2371 msg->cm_fields['T'] = strdup("000000000000000000");
2372 sprintf(msg->cm_fields['T'], "%ld", idtstart);
2377 icalcomponent_free(cal);
2378 if (whole_cal != cal) {
2379 icalcomponent_free(whole_cal);
2388 * See if we need to prevent the object from being saved (we don't allow
2389 * MIME types other than text/calendar in "calendar" or "tasks" rooms).
2391 * If the message is being saved, we also set various message header fields
2392 * using data found in the iCalendar object.
2394 int ical_obj_beforesave(struct CtdlMessage *msg)
2396 /* First determine if this is a calendar or tasks room */
2397 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
2398 && (CC->room.QRdefaultview != VIEW_TASKS)
2400 return(0); /* Not an iCalendar-centric room */
2403 /* It must be an RFC822 message! */
2404 if (msg->cm_format_type != 4) {
2405 syslog(LOG_DEBUG, "Rejecting non-RFC822 message\n");
2406 return(1); /* You tried to save a non-RFC822 message! */
2409 if (msg->cm_fields['M'] == NULL) {
2410 return(1); /* You tried to save a null message! */
2413 /* Do all of our lovely back-end parsing */
2414 mime_parser(msg->cm_fields['M'],
2416 *ical_obj_beforesave_backend,
2427 * Things we need to do after saving a calendar event.
2429 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2430 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2431 char *encoding, char *cbid, void *cbuserdata)
2435 /* We're only interested in calendar items here. */
2436 if ( (strcasecmp(cbtype, "text/calendar"))
2437 && (strcasecmp(cbtype, "application/ics")) ) {
2441 /* Hunt for the UID and drop it in
2442 * the "user data" pointer for the MIME parser. When
2443 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2446 if ( (!strcasecmp(cbtype, "text/calendar"))
2447 || (!strcasecmp(cbtype, "application/ics")) ) {
2448 cal = icalcomponent_new_from_string(content);
2450 ical_saving_vevent(cal, cal);
2451 icalcomponent_free(cal);
2458 * Things we need to do after saving a calendar event.
2459 * (This will start back end tasks such as automatic generation of invitations,
2460 * if such actions are appropriate.)
2462 int ical_obj_aftersave(struct CtdlMessage *msg)
2464 char roomname[ROOMNAMELEN];
2467 * If this isn't the Calendar> room, no further action is necessary.
2470 /* First determine if this is our room */
2471 CtdlMailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2472 if (strcasecmp(roomname, CC->room.QRname)) {
2473 return(0); /* Not the Calendar room -- don't do anything. */
2476 /* It must be an RFC822 message! */
2477 if (msg->cm_format_type != 4) return(1);
2479 /* Reject null messages */
2480 if (msg->cm_fields['M'] == NULL) return(1);
2482 /* Now recurse through it looking for our icalendar data */
2483 mime_parser(msg->cm_fields['M'],
2485 *ical_obj_aftersave_backend,
2495 void ical_session_startup(void) {
2496 CIT_ICAL = malloc(sizeof(struct cit_ical));
2497 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2500 void ical_session_shutdown(void) {
2506 * Back end for ical_fixed_output()
2508 void ical_fixed_output_backend(icalcomponent *cal,
2516 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2518 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2521 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2523 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2526 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2528 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2531 /* If the component has attendees, iterate through them. */
2532 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2533 ch = icalproperty_get_attendee(p);
2535 !strncasecmp(ch, "MAILTO:", 7)) {
2537 /* screen name or email address */
2538 safestrncpy(buf, ch + 7, sizeof(buf));
2540 cprintf("%s ", buf);
2545 /* If the component has subcomponents, recurse through them. */
2546 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2548 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2549 /* Recursively process subcomponent */
2550 ical_fixed_output_backend(c, recursion_level+1);
2557 * Function to output iCalendar data as plain text. Nobody uses MSG0
2558 * anymore, so really this is just so we expose the vCard data to the full
2561 void ical_fixed_output(char *ptr, int len) {
2565 stringy_cal = malloc(len + 1);
2566 safestrncpy(stringy_cal, ptr, len + 1);
2567 cal = icalcomponent_new_from_string(stringy_cal);
2574 ical_fixed_output_backend(cal, 0);
2576 /* Free the memory we obtained from libical's constructor */
2577 icalcomponent_free(cal);
2582 void serv_calendar_destroy(void)
2584 icaltimezone_free_builtin_timezones();
2588 * Register this module with the Citadel server.
2590 CTDL_MODULE_INIT(calendar)
2595 /* Tell libical to return errors instead of aborting if it gets bad data */
2596 icalerror_errors_are_fatal = 0;
2598 /* Use our own application prefix in tzid's generated from system tzdata */
2599 icaltimezone_set_tzid_prefix("/citadel.org/");
2601 /* Initialize our hook functions */
2602 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2603 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2604 CtdlRegisterSessionHook(ical_CtdlCreateRoom, EVT_LOGIN);
2605 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2606 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2607 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2608 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2609 CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
2610 CtdlRegisterCleanupHook(serv_calendar_destroy);
2613 /* return our module name for the log */