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"
34 #include "euidindex.h"
35 #include "ical_dezonify.h"
39 struct ical_respond_data {
40 char desired_partnum[SIZ];
46 * Utility function to create a new VCALENDAR component with some of the
47 * required fields already set the way we like them.
49 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
50 icalcomponent *encaps;
52 encaps = icalcomponent_new_vcalendar();
54 syslog(LOG_CRIT, "ERROR: could not allocate component!\n");
58 /* Set the Product ID */
59 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
61 /* Set the Version Number */
62 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
69 * Utility function to encapsulate a subcomponent into a full VCALENDAR
71 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
72 icalcomponent *encaps;
74 /* If we're already looking at a full VCALENDAR component,
75 * don't bother ... just return itself.
77 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
81 /* Encapsulate the VEVENT component into a complete VCALENDAR */
82 encaps = icalcomponent_new_citadel_vcalendar();
83 if (encaps == NULL) return NULL;
85 /* Encapsulate the subcomponent inside */
86 icalcomponent_add_component(encaps, subcomp);
88 /* Return the object we just created. */
96 * Write a calendar object into the specified user's calendar room.
97 * If the supplied user is NULL, this function writes the calendar object
98 * to the currently selected room.
100 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 serlen = strlen(ser);
126 /* If the caller supplied a user, write to that user's default calendar room */
128 /* This handy API function does all the work for us. */
129 CtdlWriteObject(USERCALENDARROOM, /* which room */
130 "text/calendar", /* MIME type */
132 serlen + 1, /* length */
135 0, /* don't delete others of this type */
140 /* If the caller did not supply a user, write to the currently selected room */
142 struct CitContext *CCC = CC;
145 msg = malloc(sizeof(struct CtdlMessage));
146 memset(msg, 0, sizeof(struct CtdlMessage));
147 msg->cm_magic = CTDLMESSAGE_MAGIC;
148 msg->cm_anon_type = MES_NORMAL;
149 msg->cm_format_type = 4;
150 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
151 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
152 CM_SetField(msg, eNodeName, config.c_nodename, strlen(config.c_nodename));
153 CM_SetField(msg, eHumanNode, config.c_humannode, strlen(config.c_humannode));
155 MsgBody = NewStrBufPlain(NULL, serlen + 100);
156 StrBufAppendBufPlain(MsgBody, HKEY("Content-type: text/calendar\r\n\r\n"), 0);
157 StrBufAppendBufPlain(MsgBody, ser, serlen, 0);
159 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
161 /* Now write the data */
162 CtdlSubmitMsg(msg, NULL, "", QP_EADDR);
166 /* In either case, now we can free the serialized calendar object */
172 * Send a reply to a meeting invitation.
174 * 'request' is the invitation to reply to.
175 * 'action' is the string "accept" or "decline" or "tentative".
178 void ical_send_a_reply(icalcomponent *request, char *action) {
179 icalcomponent *the_reply = NULL;
180 icalcomponent *vevent = NULL;
181 icalproperty *attendee = NULL;
182 char attendee_string[SIZ];
183 icalproperty *organizer = NULL;
184 char organizer_string[SIZ];
185 icalproperty *summary = NULL;
186 char summary_string[SIZ];
187 icalproperty *me_attend = NULL;
188 recptypes *recp = NULL;
189 icalparameter *partstat = NULL;
190 char *serialized_reply = NULL;
191 char *reply_message_text = NULL;
193 struct CtdlMessage *msg = NULL;
194 recptypes *valid = NULL;
196 *organizer_string = '\0';
197 strcpy(summary_string, "Calendar item");
199 if (request == NULL) {
200 syslog(LOG_ERR, "ERROR: trying to reply to NULL event?\n");
204 the_reply = icalcomponent_new_clone(request);
205 if (the_reply == NULL) {
206 syslog(LOG_ERR, "ERROR: cannot clone request\n");
210 /* Change the method from REQUEST to REPLY */
211 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
213 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
214 if (vevent != NULL) {
215 /* Hunt for attendees, removing ones that aren't us.
216 * (Actually, remove them all, cloning our own one so we can
217 * re-insert it later)
219 while (attendee = icalcomponent_get_first_property(vevent,
220 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
222 ch = icalproperty_get_attendee(attendee);
223 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
224 safestrncpy(attendee_string, ch + 7, sizeof (attendee_string));
225 striplt(attendee_string);
226 recp = validate_recipients(attendee_string, NULL, 0);
228 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
229 if (me_attend) icalproperty_free(me_attend);
230 me_attend = icalproperty_new_clone(attendee);
232 free_recipients(recp);
237 icalcomponent_remove_property(vevent, attendee);
238 icalproperty_free(attendee);
241 /* We found our own address in the attendee list. */
243 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
244 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
246 if (!strcasecmp(action, "accept")) {
247 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
249 else if (!strcasecmp(action, "decline")) {
250 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
252 else if (!strcasecmp(action, "tentative")) {
253 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
256 if (partstat) icalproperty_add_parameter(me_attend, partstat);
258 /* Now insert it back into the vevent. */
259 icalcomponent_add_property(vevent, me_attend);
262 /* Figure out who to send this thing to */
263 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
264 if (organizer != NULL) {
265 if (icalproperty_get_organizer(organizer)) {
266 strcpy(organizer_string,
267 icalproperty_get_organizer(organizer) );
270 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
271 strcpy(organizer_string, &organizer_string[7]);
272 striplt(organizer_string);
274 strcpy(organizer_string, "");
277 /* Extract the summary string -- we'll use it as the
278 * message subject for the reply
280 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
281 if (summary != NULL) {
282 if (icalproperty_get_summary(summary)) {
283 strcpy(summary_string,
284 icalproperty_get_summary(summary) );
289 /* Now generate the reply message and send it out. */
290 serialized_reply = icalcomponent_as_ical_string_r(the_reply);
291 icalcomponent_free(the_reply); /* don't need this anymore */
292 if (serialized_reply == NULL) return;
294 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
295 if (reply_message_text != NULL) {
296 sprintf(reply_message_text,
297 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
301 msg = CtdlMakeMessage(&CC->user,
302 organizer_string, /* to */
304 CC->room.QRname, 0, FMT_RFC822,
307 summary_string, /* Use summary for subject */
313 valid = validate_recipients(organizer_string, NULL, 0);
314 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
316 free_recipients(valid);
319 free(serialized_reply);
325 * Callback function for mime parser that hunts for calendar content types
326 * and turns them into calendar objects. If something is found, it is placed
327 * in ird->cal, and the caller now owns that memory and is responsible for freeing it.
329 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
330 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
331 char *cbid, void *cbuserdata) {
333 struct ical_respond_data *ird = NULL;
335 ird = (struct ical_respond_data *) cbuserdata;
337 /* desired_partnum can be set to "_HUNT_" to have it just look for
338 * the first part with a content type of text/calendar. Otherwise
339 * we have to only process the right one.
341 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
342 if (strcasecmp(partnum, ird->desired_partnum)) {
347 if ( (strcasecmp(cbtype, "text/calendar"))
348 && (strcasecmp(cbtype, "application/ics")) ) {
352 if (ird->cal != NULL) {
353 icalcomponent_free(ird->cal);
357 ird->cal = icalcomponent_new_from_string(content);
362 * Respond to a meeting request.
364 void ical_respond(long msgnum, char *partnum, char *action) {
365 struct CtdlMessage *msg = NULL;
366 struct ical_respond_data ird;
369 (strcasecmp(action, "accept"))
370 && (strcasecmp(action, "decline"))
372 cprintf("%d Action must be 'accept' or 'decline'\n",
373 ERROR + ILLEGAL_VALUE
378 msg = CtdlFetchMessage(msgnum, 1);
380 cprintf("%d Message %ld not found.\n",
381 ERROR + ILLEGAL_VALUE,
387 memset(&ird, 0, sizeof ird);
388 strcpy(ird.desired_partnum, partnum);
389 mime_parser(msg->cm_fields[eMesageText],
391 *ical_locate_part, /* callback function */
393 (void *) &ird, /* user data */
397 /* We're done with the incoming message, because we now have a
398 * calendar object in memory.
403 * Here is the real meat of this function. Handle the event.
405 if (ird.cal != NULL) {
406 /* Save this in the user's calendar if necessary */
407 if (!strcasecmp(action, "accept")) {
408 ical_write_to_cal(&CC->user, ird.cal);
411 /* Send a reply if necessary */
412 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
413 ical_send_a_reply(ird.cal, action);
416 /* We used to delete the invitation after handling it.
417 * We don't do that anymore, but here is the code that handled it:
418 * CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
421 /* Free the memory we allocated and return a response. */
422 icalcomponent_free(ird.cal);
424 cprintf("%d ok\n", CIT_OK);
428 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
432 /* should never get here */
437 * Figure out the UID of the calendar event being referred to in a
438 * REPLY object. This function is recursive.
440 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
441 icalcomponent *subcomponent;
444 /* If this object is a REPLY, then extract the UID. */
445 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
446 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
448 strcpy(uidbuf, icalproperty_get_comment(p));
452 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
453 * UID of the reply; we want the UID of the invitation being replied to.
455 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
456 subcomponent != NULL;
457 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
458 ical_learn_uid_of_reply(uidbuf, subcomponent);
464 * ical_update_my_calendar_with_reply() refers to this callback function; when we
465 * locate the message containing the calendar event we're replying to, this function
466 * gets called. It basically just sticks the message number in a supplied buffer.
468 void ical_hunt_for_event_to_update(long msgnum, void *data) {
471 msgnumptr = (long *) data;
476 struct original_event_container {
481 * Callback function for mime parser that hunts for calendar content types
482 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
483 * to fetch the object being updated)
485 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
486 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
487 char *cbid, void *cbuserdata) {
489 struct original_event_container *oec = NULL;
491 if ( (strcasecmp(cbtype, "text/calendar"))
492 && (strcasecmp(cbtype, "application/ics")) ) {
495 oec = (struct original_event_container *) cbuserdata;
496 if (oec->c != NULL) {
497 icalcomponent_free(oec->c);
499 oec->c = icalcomponent_new_from_string(content);
504 * Merge updated attendee information from a REPLY into an existing event.
506 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
508 icalproperty *e_attendee, *r_attendee;
510 /* First things first. If we're not looking at a VEVENT component,
511 * recurse through subcomponents until we find one.
513 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
514 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
516 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
517 ical_merge_attendee_reply(c, reply);
522 /* Now do the same thing with the reply.
524 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
525 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
527 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
528 ical_merge_attendee_reply(event, c);
533 /* Clone the reply, because we're going to rip its guts out. */
534 reply = icalcomponent_new_clone(reply);
536 /* At this point we're looking at the correct subcomponents.
537 * Iterate through the attendees looking for a match.
540 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
542 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
544 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
546 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
548 /* Check to see if these two attendees match...
551 e = icalproperty_get_attendee(e_attendee);
552 r = icalproperty_get_attendee(r_attendee);
557 /* ...and if they do, remove the attendee from the event
558 * and replace it with the attendee from the reply. (The
559 * reply's copy will have the same address, but an updated
562 icalcomponent_remove_property(event, e_attendee);
563 icalproperty_free(e_attendee);
564 icalcomponent_remove_property(reply, r_attendee);
565 icalcomponent_add_property(event, r_attendee);
567 /* Since we diddled both sets of attendees, we have to start
568 * the iteration over again. This will not create an infinite
569 * loop because we removed the attendee from the reply. (That's
570 * why we cloned the reply, and that's what we mean by "ripping
579 /* Free the *clone* of the reply. */
580 icalcomponent_free(reply);
587 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
588 * calendar event. The object has already been deserialized for us; all
589 * we have to do here is hunt for the event in our calendar, merge in the
590 * updated attendee status, and save it again.
592 * This function returns 0 on success, 1 if the event was not found in the
593 * user's calendar, or 2 if an internal error occurred.
595 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
597 char hold_rm[ROOMNAMELEN];
598 long msgnum_being_replaced = 0;
599 struct CtdlMessage *msg = NULL;
600 struct original_event_container oec;
601 icalcomponent *original_event;
602 char *serialized_event = NULL;
603 char roomname[ROOMNAMELEN];
604 char *message_text = NULL;
606 /* Figure out just what event it is we're dealing with */
607 strcpy(uid, "--==<< InVaLiD uId >>==--");
608 ical_learn_uid_of_reply(uid, cal);
609 syslog(LOG_DEBUG, "UID of event being replied to is <%s>\n", uid);
611 strcpy(hold_rm, CC->room.QRname); /* save current room */
613 if (CtdlGetRoom(&CC->room, USERCALENDARROOM) != 0) {
614 CtdlGetRoom(&CC->room, hold_rm);
615 syslog(LOG_CRIT, "cannot get user calendar room\n");
620 * Look in the EUID index for a message with
621 * the Citadel EUID set to the value we're looking for. Since
622 * Citadel always sets the message EUID to the iCalendar UID of
623 * the event, this will work.
625 msgnum_being_replaced = CtdlLocateMessageByEuid(uid, &CC->room);
627 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
629 syslog(LOG_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
630 if (msgnum_being_replaced == 0) {
631 return(1); /* no calendar event found */
634 /* Now we know the ID of the message containing the event being updated.
635 * We don't actually have to delete it; that'll get taken care of by the
636 * server when we save another event with the same UID. This just gives
637 * us the ability to load the event into memory so we can diddle the
640 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
642 return(2); /* internal error */
645 mime_parser(msg->cm_fields[eMesageText],
647 *ical_locate_original_event, /* callback function */
649 &oec, /* user data */
654 original_event = oec.c;
655 if (original_event == NULL) {
656 syslog(LOG_ERR, "ERROR: Original_component is NULL.\n");
660 /* Merge the attendee's updated status into the event */
661 ical_merge_attendee_reply(original_event, cal);
664 serialized_event = icalcomponent_as_ical_string_r(original_event);
665 icalcomponent_free(original_event); /* Don't need this anymore. */
666 if (serialized_event == NULL) return(2);
668 CtdlMailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
670 message_text = malloc(strlen(serialized_event) + SIZ);
671 if (message_text != NULL) {
672 sprintf(message_text,
673 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
677 msg = CtdlMakeMessage(&CC->user,
678 "", /* No recipient */
679 "", /* No recipient */
690 CIT_ICAL->avoid_sending_invitations = 1;
691 CtdlSubmitMsg(msg, NULL, roomname, QP_EADDR);
693 CIT_ICAL->avoid_sending_invitations = 0;
696 free(serialized_event);
702 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
703 * simply extracts the calendar object from the message, deserializes it, and
704 * passes it up to ical_update_my_calendar_with_reply() for processing.
706 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
707 struct CtdlMessage *msg = NULL;
708 struct ical_respond_data ird;
712 (strcasecmp(action, "update"))
713 && (strcasecmp(action, "ignore"))
715 cprintf("%d Action must be 'update' or 'ignore'\n",
716 ERROR + ILLEGAL_VALUE
721 msg = CtdlFetchMessage(msgnum, 1);
723 cprintf("%d Message %ld not found.\n",
724 ERROR + ILLEGAL_VALUE,
730 memset(&ird, 0, sizeof ird);
731 strcpy(ird.desired_partnum, partnum);
732 mime_parser(msg->cm_fields[eMesageText],
734 *ical_locate_part, /* callback function */
736 (void *) &ird, /* user data */
740 /* We're done with the incoming message, because we now have a
741 * calendar object in memory.
746 * Here is the real meat of this function. Handle the event.
748 if (ird.cal != NULL) {
749 /* Update the user's calendar if necessary */
750 if (!strcasecmp(action, "update")) {
751 ret = ical_update_my_calendar_with_reply(ird.cal);
753 cprintf("%d Your calendar has been updated with this reply.\n",
757 cprintf("%d This event does not exist in your calendar.\n",
758 ERROR + FILE_NOT_FOUND);
761 cprintf("%d An internal error occurred.\n",
762 ERROR + INTERNAL_ERROR);
766 cprintf("%d This reply has been ignored.\n", CIT_OK);
769 /* Now that we've processed this message, we don't need it
770 * anymore. So delete it. (Don't do this anymore.)
771 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
774 /* Free the memory we allocated and return a response. */
775 icalcomponent_free(ird.cal);
780 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
784 /* should never get here */
789 * Search for a property in both the top level and in a VEVENT subcomponent
791 icalproperty *ical_ctdl_get_subprop(
793 icalproperty_kind which_prop
798 p = icalcomponent_get_first_property(cal, which_prop);
800 c = icalcomponent_get_first_component(cal,
801 ICAL_VEVENT_COMPONENT);
803 p = icalcomponent_get_first_property(c, which_prop);
811 * Check to see if two events overlap. Returns nonzero if they do.
812 * (This function is used in both Citadel and WebCit. If you change it in
813 * one place, change it in the other. Better yet, put it in a library.)
815 int ical_ctdl_is_overlap(
816 struct icaltimetype t1start,
817 struct icaltimetype t1end,
818 struct icaltimetype t2start,
819 struct icaltimetype t2end
821 if (icaltime_is_null_time(t1start)) return(0);
822 if (icaltime_is_null_time(t2start)) return(0);
824 /* if either event lacks end time, assume end = start */
825 if (icaltime_is_null_time(t1end))
826 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
828 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
830 * the end date is non-inclusive so adjust it by one
831 * day because our test is inclusive, note that a day is
832 * not too much because we are talking about all day
834 * if start = end we assume that nevertheless the whole
837 icaltime_adjust(&t1end, -1, 0, 0, 0);
841 if (icaltime_is_null_time(t2end))
842 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
844 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
845 icaltime_adjust(&t2end, -1, 0, 0, 0);
849 /* First, check for all-day events */
850 if (t1start.is_date || t2start.is_date) {
851 /* If event 1 ends before event 2 starts, we're in the clear. */
852 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
854 /* If event 2 ends before event 1 starts, we're also ok. */
855 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
860 /* syslog(LOG_DEBUG, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
861 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
862 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
865 /* Now check for overlaps using date *and* time. */
867 /* If event 1 ends before event 2 starts, we're in the clear. */
868 if (icaltime_compare(t1end, t2start) <= 0) return(0);
869 /* syslog(LOG_DEBUG, "first passed\n"); */
871 /* If event 2 ends before event 1 starts, we're also ok. */
872 if (icaltime_compare(t2end, t1start) <= 0) return(0);
873 /* syslog(LOG_DEBUG, "second passed\n"); */
875 /* Otherwise, they overlap. */
880 * Phase 6 of "hunt for conflicts"
881 * called by ical_conflicts_phase5()
883 * Now both the proposed and existing events have been boiled down to start and end times.
884 * Check for overlap and output any conflicts.
886 * Returns nonzero if a conflict was reported. This allows the caller to stop iterating.
888 int ical_conflicts_phase6(struct icaltimetype t1start,
889 struct icaltimetype t1end,
890 struct icaltimetype t2start,
891 struct icaltimetype t2end,
892 long existing_msgnum,
893 char *conflict_event_uid,
894 char *conflict_event_summary,
897 int conflict_reported = 0;
901 tt = icaltime_as_timet_with_zone(t1start, t1start.zone);
902 syslog(LOG_DEBUG, "PROPOSED START: %s", ctime(&tt));
903 tt = icaltime_as_timet_with_zone(t1end, t1end.zone);
904 syslog(LOG_DEBUG, " PROPOSED END: %s", ctime(&tt));
905 tt = icaltime_as_timet_with_zone(t2start, t2start.zone);
906 syslog(LOG_DEBUG, "EXISTING START: %s", ctime(&tt));
907 tt = icaltime_as_timet_with_zone(t2end, t2end.zone);
908 syslog(LOG_DEBUG, " EXISTING END: %s", ctime(&tt));
911 /* compare and output */
913 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
914 cprintf("%ld||%s|%s|%d|\n",
917 conflict_event_summary,
918 ( ((strlen(compare_uid)>0)
919 &&(!strcasecmp(compare_uid,
920 conflict_event_uid))) ? 1 : 0
923 conflict_reported = 1;
926 return(conflict_reported);
932 * Phase 5 of "hunt for conflicts"
933 * Called by ical_conflicts_phase4()
935 * We have the proposed event boiled down to start and end times.
936 * Now check it against an existing event.
938 void ical_conflicts_phase5(struct icaltimetype t1start,
939 struct icaltimetype t1end,
940 icalcomponent *existing_event,
941 long existing_msgnum,
944 char conflict_event_uid[SIZ];
945 char conflict_event_summary[SIZ];
946 struct icaltimetype t2start, t2end;
949 /* recur variables */
950 icalproperty *rrule = NULL;
951 struct icalrecurrencetype recur;
952 icalrecur_iterator *ritr = NULL;
953 struct icaldurationtype dur;
957 strcpy(conflict_event_uid, "");
958 strcpy(conflict_event_summary, "");
959 t2start = icaltime_null_time();
960 t2end = icaltime_null_time();
962 /* existing event stuff */
963 p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
964 if (p == NULL) return;
965 if (p != NULL) t2start = icalproperty_get_dtstart(p);
966 if (icaltime_is_utc(t2start)) {
967 t2start.zone = icaltimezone_get_utc_timezone();
970 t2start.zone = icalcomponent_get_timezone(existing_event,
971 icalparameter_get_tzid(
972 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
976 t2start.zone = get_default_icaltimezone();
980 p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
982 t2end = icalproperty_get_dtend(p);
984 if (icaltime_is_utc(t2end)) {
985 t2end.zone = icaltimezone_get_utc_timezone();
988 t2end.zone = icalcomponent_get_timezone(existing_event,
989 icalparameter_get_tzid(
990 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
994 t2end.zone = get_default_icaltimezone();
997 dur = icaltime_subtract(t2end, t2start);
1000 memset (&dur, 0, sizeof(struct icaldurationtype));
1003 rrule = ical_ctdl_get_subprop(existing_event, ICAL_RRULE_PROPERTY);
1005 recur = icalproperty_get_rrule(rrule);
1006 ritr = icalrecur_iterator_new(recur, t2start);
1010 p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
1012 strcpy(conflict_event_uid, icalproperty_get_comment(p));
1015 p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
1017 strcpy(conflict_event_summary, icalproperty_get_comment(p));
1020 if (ical_conflicts_phase6(t1start, t1end, t2start, t2end,
1021 existing_msgnum, conflict_event_uid, conflict_event_summary, compare_uid))
1023 num_recur = MAX_RECUR + 1; /* force it out of scope, no need to continue */
1027 t2start = icalrecur_iterator_next(ritr);
1028 if (!icaltime_is_null_time(t2end)) {
1029 const icaltimezone *hold_zone = t2end.zone;
1030 t2end = icaltime_add(t2start, dur);
1031 t2end.zone = hold_zone;
1036 if (icaltime_compare(t2start, t1end) < 0) {
1037 num_recur = MAX_RECUR + 1; /* force it out of scope */
1040 } while ( (rrule) && (!icaltime_is_null_time(t2start)) && (num_recur < MAX_RECUR) );
1041 icalrecur_iterator_free(ritr);
1048 * Phase 4 of "hunt for conflicts"
1049 * Called by ical_hunt_for_conflicts_backend()
1051 * At this point we've got it boiled down to two icalcomponent events in memory.
1052 * If they conflict, output something to the client.
1054 void ical_conflicts_phase4(icalcomponent *proposed_event,
1055 icalcomponent *existing_event,
1056 long existing_msgnum)
1058 struct icaltimetype t1start, t1end;
1060 char compare_uid[SIZ];
1062 /* recur variables */
1063 icalproperty *rrule = NULL;
1064 struct icalrecurrencetype recur;
1065 icalrecur_iterator *ritr = NULL;
1066 struct icaldurationtype dur;
1069 /* initialization */
1070 t1end = icaltime_null_time();
1071 *compare_uid = '\0';
1073 /* proposed event stuff */
1075 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
1079 t1start = icalproperty_get_dtstart(p);
1081 if (icaltime_is_utc(t1start)) {
1082 t1start.zone = icaltimezone_get_utc_timezone();
1085 t1start.zone = icalcomponent_get_timezone(proposed_event,
1086 icalparameter_get_tzid(
1087 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1090 if (!t1start.zone) {
1091 t1start.zone = get_default_icaltimezone();
1095 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
1097 t1end = icalproperty_get_dtend(p);
1099 if (icaltime_is_utc(t1end)) {
1100 t1end.zone = icaltimezone_get_utc_timezone();
1103 t1end.zone = icalcomponent_get_timezone(proposed_event,
1104 icalparameter_get_tzid(
1105 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1109 t1end.zone = get_default_icaltimezone();
1113 dur = icaltime_subtract(t1end, t1start);
1116 memset (&dur, 0, sizeof(struct icaldurationtype));
1119 rrule = ical_ctdl_get_subprop(proposed_event, ICAL_RRULE_PROPERTY);
1121 recur = icalproperty_get_rrule(rrule);
1122 ritr = icalrecur_iterator_new(recur, t1start);
1125 p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
1127 strcpy(compare_uid, icalproperty_get_comment(p));
1131 ical_conflicts_phase5(t1start, t1end, existing_event, existing_msgnum, compare_uid);
1134 t1start = icalrecur_iterator_next(ritr);
1135 if (!icaltime_is_null_time(t1end)) {
1136 const icaltimezone *hold_zone = t1end.zone;
1137 t1end = icaltime_add(t1start, dur);
1138 t1end.zone = hold_zone;
1143 } while ( (rrule) && (!icaltime_is_null_time(t1start)) && (num_recur < MAX_RECUR) );
1144 icalrecur_iterator_free(ritr);
1150 * Phase 3 of "hunt for conflicts"
1151 * Called by ical_hunt_for_conflicts()
1153 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
1154 icalcomponent *proposed_event;
1155 struct CtdlMessage *msg = NULL;
1156 struct ical_respond_data ird;
1158 proposed_event = (icalcomponent *)data;
1160 msg = CtdlFetchMessage(msgnum, 1);
1161 if (msg == NULL) return;
1162 memset(&ird, 0, sizeof ird);
1163 strcpy(ird.desired_partnum, "_HUNT_");
1164 mime_parser(msg->cm_fields[eMesageText],
1166 *ical_locate_part, /* callback function */
1168 (void *) &ird, /* user data */
1173 if (ird.cal == NULL) return;
1175 ical_conflicts_phase4(proposed_event, ird.cal, msgnum);
1176 icalcomponent_free(ird.cal);
1182 * Phase 2 of "hunt for conflicts" operation.
1183 * At this point we have a calendar object which represents the VEVENT that
1184 * is proposed for addition to the calendar. Now hunt through the user's
1185 * calendar room, and output zero or more existing VEVENTs which conflict
1188 void ical_hunt_for_conflicts(icalcomponent *cal) {
1189 char hold_rm[ROOMNAMELEN];
1191 strcpy(hold_rm, CC->room.QRname); /* save current room */
1193 if (CtdlGetRoom(&CC->room, USERCALENDARROOM) != 0) {
1194 CtdlGetRoom(&CC->room, hold_rm);
1195 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1199 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1201 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1204 ical_hunt_for_conflicts_backend,
1209 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
1216 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1218 void ical_conflicts(long msgnum, char *partnum) {
1219 struct CtdlMessage *msg = NULL;
1220 struct ical_respond_data ird;
1222 msg = CtdlFetchMessage(msgnum, 1);
1224 cprintf("%d Message %ld not found\n",
1225 ERROR + ILLEGAL_VALUE,
1231 memset(&ird, 0, sizeof ird);
1232 strcpy(ird.desired_partnum, partnum);
1233 mime_parser(msg->cm_fields[eMesageText],
1235 *ical_locate_part, /* callback function */
1237 (void *) &ird, /* user data */
1243 if (ird.cal != NULL) {
1244 ical_hunt_for_conflicts(ird.cal);
1245 icalcomponent_free(ird.cal);
1249 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1255 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1257 * fb The VFREEBUSY component to which we are appending
1258 * top_level_cal The top-level VCALENDAR component which contains a VEVENT to be added
1260 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal) {
1264 struct icalperiodtype this_event_period = icalperiodtype_null_period();
1265 icaltimetype dtstart;
1268 /* recur variables */
1269 icalproperty *rrule = NULL;
1270 struct icalrecurrencetype recur;
1271 icalrecur_iterator *ritr = NULL;
1272 struct icaldurationtype dur;
1275 if (!top_level_cal) return;
1277 /* Find the VEVENT component containing an event */
1278 cal = icalcomponent_get_first_component(top_level_cal, ICAL_VEVENT_COMPONENT);
1281 /* If this event is not opaque, the user isn't publishing it as
1282 * busy time, so don't bother doing anything else.
1284 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1286 v = icalproperty_get_value(p);
1288 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1295 * Now begin calculating the event start and end times.
1297 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1299 dtstart = icalproperty_get_dtstart(p);
1301 if (icaltime_is_utc(dtstart)) {
1302 dtstart.zone = icaltimezone_get_utc_timezone();
1305 dtstart.zone = icalcomponent_get_timezone(top_level_cal,
1306 icalparameter_get_tzid(
1307 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1310 if (!dtstart.zone) {
1311 dtstart.zone = get_default_icaltimezone();
1315 dtend = icalcomponent_get_dtend(cal);
1316 if (!icaltime_is_null_time(dtend)) {
1317 dur = icaltime_subtract(dtend, dtstart);
1320 memset (&dur, 0, sizeof(struct icaldurationtype));
1323 /* Is a recurrence specified? If so, get ready to process it... */
1324 rrule = ical_ctdl_get_subprop(cal, ICAL_RRULE_PROPERTY);
1326 recur = icalproperty_get_rrule(rrule);
1327 ritr = icalrecur_iterator_new(recur, dtstart);
1331 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1332 this_event_period.start = dtstart;
1334 if (!icaltime_is_null_time(dtend)) {
1335 this_event_period.end = dtend;
1338 /* Convert the timestamps to UTC. It's ok to do this because we've already expanded
1339 * recurrences and this data is never going to get used again.
1341 this_event_period.start = icaltime_convert_to_zone(
1342 this_event_period.start,
1343 icaltimezone_get_utc_timezone()
1345 this_event_period.end = icaltime_convert_to_zone(
1346 this_event_period.end,
1347 icaltimezone_get_utc_timezone()
1351 icalcomponent_add_property(fb, icalproperty_new_freebusy(this_event_period));
1353 /* Make sure the DTSTART property of the freebusy *list* is set to
1354 * the DTSTART property of the *earliest event*.
1356 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1358 icalcomponent_set_dtstart(fb, this_event_period.start);
1361 if (icaltime_compare(this_event_period.start, icalcomponent_get_dtstart(fb)) < 0) {
1362 icalcomponent_set_dtstart(fb, this_event_period.start);
1366 /* Make sure the DTEND property of the freebusy *list* is set to
1367 * the DTEND property of the *latest event*.
1369 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1371 icalcomponent_set_dtend(fb, this_event_period.end);
1374 if (icaltime_compare(this_event_period.end, icalcomponent_get_dtend(fb)) > 0) {
1375 icalcomponent_set_dtend(fb, this_event_period.end);
1380 dtstart = icalrecur_iterator_next(ritr);
1381 if (!icaltime_is_null_time(dtend)) {
1382 dtend = icaltime_add(dtstart, dur);
1383 dtend.zone = dtstart.zone;
1384 dtend.is_utc = dtstart.is_utc;
1389 } while ( (rrule) && (!icaltime_is_null_time(dtstart)) && (num_recur < MAX_RECUR) ) ;
1390 icalrecur_iterator_free(ritr);
1396 * Backend for ical_freebusy()
1398 * This function simply loads the messages in the user's calendar room,
1399 * which contain VEVENTs, then strips them of all non-freebusy data, and
1400 * adds them to the supplied VCALENDAR.
1403 void ical_freebusy_backend(long msgnum, void *data) {
1405 struct CtdlMessage *msg = NULL;
1406 struct ical_respond_data ird;
1408 fb = (icalcomponent *)data; /* User-supplied data will be the VFREEBUSY component */
1410 msg = CtdlFetchMessage(msgnum, 1);
1411 if (msg == NULL) return;
1412 memset(&ird, 0, sizeof ird);
1413 strcpy(ird.desired_partnum, "_HUNT_");
1414 mime_parser(msg->cm_fields[eMesageText],
1416 *ical_locate_part, /* callback function */
1418 (void *) &ird, /* user data */
1424 ical_add_to_freebusy(fb, ird.cal); /* Add VEVENT times to VFREEBUSY */
1425 icalcomponent_free(ird.cal);
1432 * Grab another user's free/busy times
1434 void ical_freebusy(char *who) {
1435 struct ctdluser usbuf;
1436 char calendar_room_name[ROOMNAMELEN];
1437 char hold_rm[ROOMNAMELEN];
1438 char *serialized_request = NULL;
1439 icalcomponent *encaps = NULL;
1440 icalcomponent *fb = NULL;
1441 int found_user = (-1);
1442 recptypes *recp = NULL;
1447 int config_lines = 0;
1449 /* First try an exact match. */
1450 found_user = CtdlGetUser(&usbuf, who);
1452 /* If not found, try it as an unqualified email address. */
1453 if (found_user != 0) {
1455 recp = validate_recipients(buf, NULL, 0);
1456 syslog(LOG_DEBUG, "Trying <%s>\n", buf);
1458 if (recp->num_local == 1) {
1459 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1461 free_recipients(recp);
1465 /* If still not found, try it as an address qualified with the
1466 * primary FQDN of this Citadel node.
1468 if (found_user != 0) {
1469 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1470 syslog(LOG_DEBUG, "Trying <%s>\n", buf);
1471 recp = validate_recipients(buf, NULL, 0);
1473 if (recp->num_local == 1) {
1474 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1476 free_recipients(recp);
1480 /* Still not found? Try qualifying it with every domain we
1481 * might have addresses in.
1483 if (found_user != 0) {
1484 config_lines = num_tokens(inetcfg, '\n');
1485 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1486 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1487 extract_token(host, buf, 0, '|', sizeof host);
1488 extract_token(type, buf, 1, '|', sizeof type);
1490 if ( (!strcasecmp(type, "localhost"))
1491 || (!strcasecmp(type, "directory")) ) {
1492 snprintf(buf, sizeof buf, "%s@%s", who, host);
1493 syslog(LOG_DEBUG, "Trying <%s>\n", buf);
1494 recp = validate_recipients(buf, NULL, 0);
1496 if (recp->num_local == 1) {
1497 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1499 free_recipients(recp);
1505 if (found_user != 0) {
1506 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1510 CtdlMailboxName(calendar_room_name, sizeof calendar_room_name,
1511 &usbuf, USERCALENDARROOM);
1513 strcpy(hold_rm, CC->room.QRname); /* save current room */
1515 if (CtdlGetRoom(&CC->room, calendar_room_name) != 0) {
1516 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1517 CtdlGetRoom(&CC->room, hold_rm);
1521 /* Create a VFREEBUSY subcomponent */
1522 syslog(LOG_DEBUG, "Creating VFREEBUSY component\n");
1523 fb = icalcomponent_new_vfreebusy();
1525 cprintf("%d Internal error: cannot allocate memory.\n",
1526 ERROR + INTERNAL_ERROR);
1527 CtdlGetRoom(&CC->room, hold_rm);
1531 /* Set the method to PUBLISH */
1532 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1534 /* Set the DTSTAMP to right now. */
1535 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1537 /* Add the user's email address as ORGANIZER */
1538 sprintf(buf, "MAILTO:%s", who);
1539 if (strchr(buf, '@') == NULL) {
1541 strcat(buf, config.c_fqdn);
1543 for (i=0; buf[i]; ++i) {
1544 if (buf[i]==' ') buf[i] = '_';
1546 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1548 /* Add busy time from events */
1549 syslog(LOG_DEBUG, "Adding busy time from events\n");
1550 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1552 /* If values for DTSTART and DTEND are still not present, set them
1553 * to yesterday and tomorrow as default values.
1555 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1556 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1558 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1559 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1562 /* Put the freebusy component into the calendar component */
1563 syslog(LOG_DEBUG, "Encapsulating\n");
1564 encaps = ical_encapsulate_subcomponent(fb);
1565 if (encaps == NULL) {
1566 icalcomponent_free(fb);
1567 cprintf("%d Internal error: cannot allocate memory.\n",
1568 ERROR + INTERNAL_ERROR);
1569 CtdlGetRoom(&CC->room, hold_rm);
1573 /* Set the method to PUBLISH */
1574 syslog(LOG_DEBUG, "Setting method\n");
1575 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1578 syslog(LOG_DEBUG, "Serializing\n");
1579 serialized_request = icalcomponent_as_ical_string_r(encaps);
1580 icalcomponent_free(encaps); /* Don't need this anymore. */
1582 cprintf("%d Free/busy for %s\n", LISTING_FOLLOWS, usbuf.fullname);
1583 if (serialized_request != NULL) {
1584 client_write(serialized_request, strlen(serialized_request));
1585 free(serialized_request);
1589 /* Go back to the room from which we came... */
1590 CtdlGetRoom(&CC->room, hold_rm);
1596 * Backend for ical_getics()
1598 * This is a ForEachMessage() callback function that searches the current room
1599 * for calendar events and adds them each into one big calendar component.
1601 void ical_getics_backend(long msgnum, void *data) {
1602 icalcomponent *encaps, *c;
1603 struct CtdlMessage *msg = NULL;
1604 struct ical_respond_data ird;
1606 encaps = (icalcomponent *)data;
1607 if (encaps == NULL) return;
1609 /* Look for the calendar event... */
1611 msg = CtdlFetchMessage(msgnum, 1);
1612 if (msg == NULL) return;
1613 memset(&ird, 0, sizeof ird);
1614 strcpy(ird.desired_partnum, "_HUNT_");
1615 mime_parser(msg->cm_fields[eMesageText],
1617 *ical_locate_part, /* callback function */
1619 (void *) &ird, /* user data */
1624 if (ird.cal == NULL) return;
1626 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1627 * are responsible for "the_request"'s memory -- it will be freed
1628 * when we free "encaps".
1631 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1632 * in. This will almost never happen.
1634 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1635 icalcomponent_add_component(encaps, ird.cal);
1638 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1639 * and other components encapsulated inside, we have to extract them.
1642 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1644 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1646 /* For VTIMEZONE components, suppress duplicates of the same tzid */
1648 if (icalcomponent_isa(c) == ICAL_VTIMEZONE_COMPONENT) {
1649 icalproperty *p = icalcomponent_get_first_property(c, ICAL_TZID_PROPERTY);
1651 const char *tzid = icalproperty_get_tzid(p);
1652 if (!icalcomponent_get_timezone(encaps, tzid)) {
1653 icalcomponent_add_component(encaps,
1654 icalcomponent_new_clone(c));
1659 /* All other types of components can go in verbatim */
1661 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1664 icalcomponent_free(ird.cal);
1671 * Retrieve all of the calendar items in the current room, and output them
1672 * as a single icalendar object.
1674 void ical_getics(void)
1676 icalcomponent *encaps = NULL;
1679 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1680 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1681 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1682 return; /* Not an iCalendar-centric room */
1685 encaps = icalcomponent_new_vcalendar();
1686 if (encaps == NULL) {
1687 syslog(LOG_ALERT, "ERROR: could not allocate component!\n");
1688 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1692 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1694 /* Set the Product ID */
1695 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1697 /* Set the Version Number */
1698 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1700 /* Set the method to PUBLISH */
1701 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1703 /* Now go through the room encapsulating all calendar items. */
1704 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1707 ical_getics_backend,
1711 ser = icalcomponent_as_ical_string_r(encaps);
1712 icalcomponent_free(encaps); /* Don't need this anymore. */
1713 client_write(ser, strlen(ser));
1720 * Helper callback function for ical_putics() to discover which TZID's we need.
1721 * Simply put the tzid name string into a hash table. After the callbacks are
1722 * done we'll go through them and attach the ones that we have.
1724 void ical_putics_grabtzids(icalparameter *param, void *data)
1726 const char *tzid = icalparameter_get_tzid(param);
1727 HashList *keys = (HashList *) data;
1729 if ( (keys) && (tzid) && (!IsEmptyStr(tzid)) ) {
1730 Put(keys, tzid, strlen(tzid), strdup(tzid), NULL);
1736 * Delete all of the calendar items in the current room, and replace them
1737 * with calendar items from a client-supplied data stream.
1739 void ical_putics(void)
1741 char *calstream = NULL;
1744 icalcomponent *encaps = NULL;
1745 HashList *tzidlist = NULL;
1751 /* Only allow this operation if we're in a room containing a calendar or tasks view */
1752 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1753 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1754 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1758 /* Only allow this operation if we have permission to overwrite the existing calendar */
1759 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1760 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1764 cprintf("%d Transmit data now\n", SEND_LISTING);
1765 calstream = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
1766 if (calstream == NULL) {
1770 cal = icalcomponent_new_from_string(calstream);
1773 /* We got our data stream -- now do something with it. */
1775 /* Delete the existing messages in the room, because we are overwriting
1776 * the entire calendar with an entire new (or updated) calendar.
1777 * (Careful: this opens an S_ROOMS critical section!)
1779 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1781 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1782 * in. This will almost never happen.
1784 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1785 ical_write_to_cal(NULL, cal);
1788 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1789 * and other components encapsulated inside, we have to extract them.
1792 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1794 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1796 /* Non-VTIMEZONE components each get written as individual messages.
1797 * But we also need to attach the relevant VTIMEZONE components to them.
1799 if ( (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT)
1800 && (encaps = icalcomponent_new_vcalendar()) ) {
1801 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1802 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1803 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1805 /* Attach any needed timezones here */
1806 tzidlist = NewHash(1, NULL);
1808 icalcomponent_foreach_tzid(c, ical_putics_grabtzids, tzidlist);
1810 HashPos = GetNewHashPos(tzidlist, 0);
1812 while (GetNextHashPos(tzidlist, HashPos, &len, &Key, &Value)) {
1813 syslog(LOG_DEBUG, "Attaching timezone '%s'\n", (char*) Value);
1814 icaltimezone *t = NULL;
1816 /* First look for a timezone attached to the original calendar */
1817 t = icalcomponent_get_timezone(cal, Value);
1819 /* Try built-in tzdata if the right one wasn't attached */
1821 t = icaltimezone_get_builtin_timezone(Value);
1824 /* I've got a valid timezone to attach. */
1826 icalcomponent_add_component(encaps,
1827 icalcomponent_new_clone(
1828 icaltimezone_get_component(t)
1834 DeleteHashPos(&HashPos);
1835 DeleteHash(&tzidlist);
1837 /* Now attach the component itself (usually a VEVENT or VTODO) */
1838 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1840 /* Write it to the message store */
1841 ical_write_to_cal(NULL, encaps);
1842 icalcomponent_free(encaps);
1847 icalcomponent_free(cal);
1852 * All Citadel calendar commands from the client come through here.
1854 void cmd_ical(char *argbuf)
1862 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1864 /* Allow "test" and "freebusy" subcommands without logging in. */
1866 if (!strcasecmp(subcmd, "test")) {
1867 cprintf("%d This server supports calendaring\n", CIT_OK);
1871 if (!strcasecmp(subcmd, "freebusy")) {
1872 extract_token(who, argbuf, 1, '|', sizeof who);
1877 if (!strcasecmp(subcmd, "sgi")) {
1878 CIT_ICAL->server_generated_invitations =
1879 (extract_int(argbuf, 1) ? 1 : 0) ;
1881 CIT_OK, CIT_ICAL->server_generated_invitations);
1885 if (CtdlAccessCheck(ac_logged_in)) return;
1887 if (!strcasecmp(subcmd, "respond")) {
1888 msgnum = extract_long(argbuf, 1);
1889 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1890 extract_token(action, argbuf, 3, '|', sizeof action);
1891 ical_respond(msgnum, partnum, action);
1895 if (!strcasecmp(subcmd, "handle_rsvp")) {
1896 msgnum = extract_long(argbuf, 1);
1897 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1898 extract_token(action, argbuf, 3, '|', sizeof action);
1899 ical_handle_rsvp(msgnum, partnum, action);
1903 if (!strcasecmp(subcmd, "conflicts")) {
1904 msgnum = extract_long(argbuf, 1);
1905 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1906 ical_conflicts(msgnum, partnum);
1910 if (!strcasecmp(subcmd, "getics")) {
1915 if (!strcasecmp(subcmd, "putics")) {
1920 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1926 * We don't know if the calendar room exists so we just create it at login
1928 void ical_CtdlCreateRoom(void)
1933 /* Create the calendar room if it doesn't already exist */
1934 CtdlCreateRoom(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1936 /* Set expiration policy to manual; otherwise objects will be lost! */
1937 if (CtdlGetRoomLock(&qr, USERCALENDARROOM)) {
1938 syslog(LOG_CRIT, "Couldn't get the user calendar room!\n");
1941 qr.QRep.expire_mode = EXPIRE_MANUAL;
1942 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1943 CtdlPutRoomLock(&qr);
1945 /* Set the view to a calendar view */
1946 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1947 vbuf.v_view = VIEW_CALENDAR;
1948 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1950 /* Create the tasks list room if it doesn't already exist */
1951 CtdlCreateRoom(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1953 /* Set expiration policy to manual; otherwise objects will be lost! */
1954 if (CtdlGetRoomLock(&qr, USERTASKSROOM)) {
1955 syslog(LOG_CRIT, "Couldn't get the user calendar room!\n");
1958 qr.QRep.expire_mode = EXPIRE_MANUAL;
1959 qr.QRdefaultview = VIEW_TASKS;
1960 CtdlPutRoomLock(&qr);
1962 /* Set the view to a task list view */
1963 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1964 vbuf.v_view = VIEW_TASKS;
1965 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1967 /* Create the notes room if it doesn't already exist */
1968 CtdlCreateRoom(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1970 /* Set expiration policy to manual; otherwise objects will be lost! */
1971 if (CtdlGetRoomLock(&qr, USERNOTESROOM)) {
1972 syslog(LOG_CRIT, "Couldn't get the user calendar room!\n");
1975 qr.QRep.expire_mode = EXPIRE_MANUAL;
1976 qr.QRdefaultview = VIEW_NOTES;
1977 CtdlPutRoomLock(&qr);
1979 /* Set the view to a notes view */
1980 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1981 vbuf.v_view = VIEW_NOTES;
1982 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1989 * ical_send_out_invitations() is called by ical_saving_vevent() when it finds a VEVENT.
1991 * top_level_cal is the highest available level calendar object.
1992 * cal is the subcomponent containing the VEVENT.
1994 * Note: if you change the encapsulation code here, change it in WebCit's ical_encapsulate_subcomponent()
1996 void ical_send_out_invitations(icalcomponent *top_level_cal, icalcomponent *cal) {
1997 icalcomponent *the_request = NULL;
1998 char *serialized_request = NULL;
1999 icalcomponent *encaps = NULL;
2000 char *request_message_text = NULL;
2001 struct CtdlMessage *msg = NULL;
2002 recptypes *valid = NULL;
2003 char attendees_string[SIZ];
2004 int num_attendees = 0;
2005 char this_attendee[256];
2006 icalproperty *attendee = NULL;
2007 char summary_string[SIZ];
2008 icalproperty *summary = NULL;
2011 struct icaltimetype t;
2012 const icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
2014 const icaltimezone *z;
2015 int num_zones_attached = 0;
2016 int zone_already_attached;
2017 icalparameter *tzidp = NULL;
2018 const char *tzidc = NULL;
2021 syslog(LOG_ERR, "ERROR: trying to reply to NULL event?\n");
2026 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
2027 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2028 ical_send_out_invitations(top_level_cal,
2029 icalcomponent_get_first_component(
2030 cal, ICAL_VEVENT_COMPONENT
2036 /* Clone the event */
2037 the_request = icalcomponent_new_clone(cal);
2038 if (the_request == NULL) {
2039 syslog(LOG_ERR, "ERROR: cannot clone calendar object\n");
2043 /* Extract the summary string -- we'll use it as the
2044 * message subject for the request
2046 strcpy(summary_string, "Meeting request");
2047 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
2048 if (summary != NULL) {
2049 if (icalproperty_get_summary(summary)) {
2050 strcpy(summary_string,
2051 icalproperty_get_summary(summary) );
2055 /* Determine who the recipients of this message are (the attendees) */
2056 strcpy(attendees_string, "");
2057 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
2058 const char *ch = icalproperty_get_attendee(attendee);
2059 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
2060 safestrncpy(this_attendee, ch + 7, sizeof(this_attendee));
2062 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
2063 snprintf(&attendees_string[strlen(attendees_string)],
2064 sizeof(attendees_string) - strlen(attendees_string),
2073 syslog(LOG_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
2075 /* If there are no attendees, there are no invitations to send, so...
2076 * don't bother putting one together! Punch out, Maverick!
2078 if (num_attendees == 0) {
2079 icalcomponent_free(the_request);
2083 /* Encapsulate the VEVENT component into a complete VCALENDAR */
2084 encaps = icalcomponent_new_vcalendar();
2085 if (encaps == NULL) {
2086 syslog(LOG_ALERT, "ERROR: could not allocate component!\n");
2087 icalcomponent_free(the_request);
2091 /* Set the Product ID */
2092 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
2094 /* Set the Version Number */
2095 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
2097 /* Set the method to REQUEST */
2098 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
2100 /* Look for properties containing timezone parameters, to see if we need to attach VTIMEZONEs */
2101 for (p = icalcomponent_get_first_property(the_request, ICAL_ANY_PROPERTY);
2103 p = icalcomponent_get_next_property(the_request, ICAL_ANY_PROPERTY))
2105 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
2106 || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
2107 || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
2108 || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
2109 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
2110 || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
2111 || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
2112 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
2113 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
2114 || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
2115 || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
2116 || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
2117 || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
2119 t = icalproperty_get_dtstart(p); // it's safe to use dtstart for all of them
2121 /* Determine the tzid in order for some of the conditions below to work */
2122 tzidp = icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER);
2124 tzidc = icalparameter_get_tzid(tzidp);
2130 /* First see if there's a timezone attached to the data structure itself */
2131 if (icaltime_is_utc(t)) {
2132 z = icaltimezone_get_utc_timezone();
2135 z = icaltime_get_timezone(t);
2138 /* If not, try to determine the tzid from the parameter using attached zones */
2139 if ((!z) && (tzidc)) {
2140 z = icalcomponent_get_timezone(top_level_cal, tzidc);
2143 /* Still no good? Try our internal database */
2144 if ((!z) && (tzidc)) {
2145 z = icaltimezone_get_builtin_timezone_from_tzid(tzidc);
2149 /* We have a valid timezone. Good. Now we need to attach it. */
2151 zone_already_attached = 0;
2152 for (i=0; i<5; ++i) {
2153 if (z == attached_zones[i]) {
2154 /* We've already got this one, no need to attach another. */
2155 ++zone_already_attached;
2158 if ((!zone_already_attached) && (num_zones_attached < 5)) {
2159 /* This is a new one, so attach it. */
2160 attached_zones[num_zones_attached++] = z;
2163 icalproperty_set_parameter(p,
2164 icalparameter_new_tzid(icaltimezone_get_tzid(z))
2170 /* Encapsulate any timezones we need */
2171 if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
2173 zc = icalcomponent_new_clone(icaltimezone_get_component(attached_zones[i]));
2174 icalcomponent_add_component(encaps, zc);
2177 /* Here we go: encapsulate the VEVENT into the VCALENDAR. We now no longer
2178 * are responsible for "the_request"'s memory -- it will be freed
2179 * when we free "encaps".
2181 icalcomponent_add_component(encaps, the_request);
2184 serialized_request = icalcomponent_as_ical_string_r(encaps);
2185 icalcomponent_free(encaps); /* Don't need this anymore. */
2186 if (serialized_request == NULL) return;
2188 reqsize = strlen(serialized_request) + SIZ;
2189 request_message_text = malloc(reqsize);
2190 if (request_message_text != NULL) {
2191 snprintf(request_message_text, reqsize,
2192 "Content-type: text/calendar\r\n\r\n%s\r\n",
2196 msg = CtdlMakeMessage(
2198 NULL, /* No single recipient here */
2199 NULL, /* No single recipient here */
2205 summary_string, /* Use summary for subject */
2207 request_message_text,
2212 valid = validate_recipients(attendees_string, NULL, 0);
2213 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
2215 free_recipients(valid);
2218 free(serialized_request);
2223 * When a calendar object is being saved, determine whether it's a VEVENT
2224 * and the user saving it is the organizer. If so, send out invitations
2225 * to any listed attendees.
2227 * This function is recursive. The caller can simply supply the same object
2228 * as both arguments. When it recurses it will alter the second argument
2229 * while holding on to the top level object. This allows us to go back and
2230 * grab things like time zones which might be attached.
2233 void ical_saving_vevent(icalcomponent *top_level_cal, icalcomponent *cal) {
2235 icalproperty *organizer = NULL;
2236 char organizer_string[SIZ];
2238 syslog(LOG_DEBUG, "ical_saving_vevent() has been called!\n");
2240 /* Don't send out invitations unless the client wants us to. */
2241 if (CIT_ICAL->server_generated_invitations == 0) {
2245 /* Don't send out invitations if we've been asked not to. */
2246 if (CIT_ICAL->avoid_sending_invitations > 0) {
2250 strcpy(organizer_string, "");
2252 * The VEVENT subcomponent is the one we're interested in.
2253 * Send out invitations if, and only if, this user is the Organizer.
2255 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
2256 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
2257 if (organizer != NULL) {
2258 if (icalproperty_get_organizer(organizer)) {
2259 strcpy(organizer_string,
2260 icalproperty_get_organizer(organizer));
2263 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
2264 strcpy(organizer_string, &organizer_string[7]);
2265 striplt(organizer_string);
2267 * If the user saving the event is listed as the
2268 * organizer, then send out invitations.
2270 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
2271 ical_send_out_invitations(top_level_cal, cal);
2276 /* If the component has subcomponents, recurse through them. */
2277 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2279 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2280 /* Recursively process subcomponent */
2281 ical_saving_vevent(top_level_cal, c);
2289 * Back end for ical_obj_beforesave()
2290 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
2291 * the summary of the event (becomes message subject),
2292 * and the start time (becomes message date/time).
2294 void ical_obj_beforesave_backend(char *name, char *filename, char *partnum,
2295 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2296 char *encoding, char *cbid, void *cbuserdata)
2299 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
2301 char new_uid[256] = "";
2302 struct CtdlMessage *msg = (struct CtdlMessage *) cbuserdata;
2306 /* We're only interested in calendar data. */
2307 if ( (strcasecmp(cbtype, "text/calendar"))
2308 && (strcasecmp(cbtype, "application/ics")) ) {
2312 /* Hunt for the UID and drop it in
2313 * the "user data" pointer for the MIME parser. When
2314 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2317 whole_cal = icalcomponent_new_from_string(content);
2320 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2321 nested_event = icalcomponent_get_first_component(
2322 cal, ICAL_VEVENT_COMPONENT);
2323 if (nested_event != NULL) {
2327 nested_todo = icalcomponent_get_first_component(
2328 cal, ICAL_VTODO_COMPONENT);
2329 if (nested_todo != NULL) {
2337 /* Set the message EUID to the iCalendar UID */
2339 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2341 /* If there's no uid we must generate one */
2342 generate_uuid(new_uid);
2343 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
2344 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2347 pch = icalproperty_get_comment(p);
2348 if (!IsEmptyStr(pch)) {
2349 CM_SetField(msg, eExclusiveID, pch, strlen(pch));
2350 syslog(LOG_DEBUG, "Saving calendar UID <%s>\n", pch);
2354 /* Set the message subject to the iCalendar summary */
2356 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
2358 pch = icalproperty_get_comment(p);
2359 if (!IsEmptyStr(pch)) {
2362 subj = rfc2047encode(pch, strlen(pch));
2363 CM_SetAsField(msg, eMsgSubject, &subj, strlen(subj));
2367 /* Set the message date/time to the iCalendar start time */
2369 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
2372 idtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
2374 CM_SetFieldLONG(msg, eTimestamp, idtstart);
2379 icalcomponent_free(cal);
2380 if (whole_cal != cal) {
2381 icalcomponent_free(whole_cal);
2390 * See if we need to prevent the object from being saved (we don't allow
2391 * MIME types other than text/calendar in "calendar" or "tasks" rooms).
2393 * If the message is being saved, we also set various message header fields
2394 * using data found in the iCalendar object.
2396 int ical_obj_beforesave(struct CtdlMessage *msg, recptypes *recp)
2398 /* First determine if this is a calendar or tasks room */
2399 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
2400 && (CC->room.QRdefaultview != VIEW_TASKS)
2402 return(0); /* Not an iCalendar-centric room */
2405 /* It must be an RFC822 message! */
2406 if (msg->cm_format_type != 4) {
2407 syslog(LOG_DEBUG, "Rejecting non-RFC822 message\n");
2408 return(1); /* You tried to save a non-RFC822 message! */
2411 if (CM_IsEmpty(msg, eMesageText)) {
2412 return(1); /* You tried to save a null message! */
2415 /* Do all of our lovely back-end parsing */
2416 mime_parser(msg->cm_fields[eMesageText],
2418 *ical_obj_beforesave_backend,
2429 * Things we need to do after saving a calendar event.
2431 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2432 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2433 char *encoding, char *cbid, void *cbuserdata)
2437 /* We're only interested in calendar items here. */
2438 if ( (strcasecmp(cbtype, "text/calendar"))
2439 && (strcasecmp(cbtype, "application/ics")) ) {
2443 /* Hunt for the UID and drop it in
2444 * the "user data" pointer for the MIME parser. When
2445 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2448 if ( (!strcasecmp(cbtype, "text/calendar"))
2449 || (!strcasecmp(cbtype, "application/ics")) ) {
2450 cal = icalcomponent_new_from_string(content);
2452 ical_saving_vevent(cal, cal);
2453 icalcomponent_free(cal);
2460 * Things we need to do after saving a calendar event.
2461 * (This will start back end tasks such as automatic generation of invitations,
2462 * if such actions are appropriate.)
2464 int ical_obj_aftersave(struct CtdlMessage *msg, recptypes *recp)
2466 char roomname[ROOMNAMELEN];
2469 * If this isn't the Calendar> room, no further action is necessary.
2472 /* First determine if this is our room */
2473 CtdlMailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2474 if (strcasecmp(roomname, CC->room.QRname)) {
2475 return(0); /* Not the Calendar room -- don't do anything. */
2478 /* It must be an RFC822 message! */
2479 if (msg->cm_format_type != 4) return(1);
2481 /* Reject null messages */
2482 if (CM_IsEmpty(msg, eMesageText)) return(1);
2484 /* Now recurse through it looking for our icalendar data */
2485 mime_parser(msg->cm_fields[eMesageText],
2487 *ical_obj_aftersave_backend,
2497 void ical_session_startup(void) {
2498 CIT_ICAL = malloc(sizeof(struct cit_ical));
2499 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2502 void ical_session_shutdown(void) {
2508 * Back end for ical_fixed_output()
2510 void ical_fixed_output_backend(icalcomponent *cal,
2518 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2520 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2523 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2525 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2528 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2530 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2533 /* If the component has attendees, iterate through them. */
2534 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2535 ch = icalproperty_get_attendee(p);
2537 !strncasecmp(ch, "MAILTO:", 7)) {
2539 /* screen name or email address */
2540 safestrncpy(buf, ch + 7, sizeof(buf));
2542 cprintf("%s ", buf);
2547 /* If the component has subcomponents, recurse through them. */
2548 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2550 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2551 /* Recursively process subcomponent */
2552 ical_fixed_output_backend(c, recursion_level+1);
2559 * Function to output iCalendar data as plain text. Nobody uses MSG0
2560 * anymore, so really this is just so we expose the vCard data to the full
2563 void ical_fixed_output(char *ptr, int len) {
2567 stringy_cal = malloc(len + 1);
2568 safestrncpy(stringy_cal, ptr, len + 1);
2569 cal = icalcomponent_new_from_string(stringy_cal);
2576 ical_fixed_output_backend(cal, 0);
2578 /* Free the memory we obtained from libical's constructor */
2579 icalcomponent_free(cal);
2584 void serv_calendar_destroy(void)
2586 icaltimezone_free_builtin_timezones();
2590 * Register this module with the Citadel server.
2592 CTDL_MODULE_INIT(calendar)
2597 /* Tell libical to return errors instead of aborting if it gets bad data */
2598 icalerror_errors_are_fatal = 0;
2600 /* Use our own application prefix in tzid's generated from system tzdata */
2601 icaltimezone_set_tzid_prefix("/citadel.org/");
2603 /* Initialize our hook functions */
2604 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2605 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2606 CtdlRegisterSessionHook(ical_CtdlCreateRoom, EVT_LOGIN, PRIO_LOGIN + 1);
2607 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2608 CtdlRegisterSessionHook(ical_session_startup, EVT_START, PRIO_START + 1);
2609 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP, PRIO_STOP + 80);
2610 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2611 CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
2612 CtdlRegisterCleanupHook(serv_calendar_destroy);
2615 /* return our module name for the log */