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.
6 * Copyright (c) 1987-2018 by the citadel.org team
8 * This program is open source software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
19 #include "ctdl_module.h"
21 #include <libical/ical.h>
24 #include "internet_addressing.h"
25 #include "serv_calendar.h"
27 #include "euidindex.h"
28 #include "default_timezone.h"
32 struct ical_respond_data {
33 char desired_partnum[SIZ];
39 * Utility function to create a new VCALENDAR component with some of the
40 * required fields already set the way we like them.
42 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
43 icalcomponent *encaps;
45 encaps = icalcomponent_new_vcalendar();
47 syslog(LOG_ERR, "calendar: could not allocate component");
51 /* Set the Product ID */
52 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
54 /* Set the Version Number */
55 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
62 * Utility function to encapsulate a subcomponent into a full VCALENDAR
64 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
65 icalcomponent *encaps;
67 /* If we're already looking at a full VCALENDAR component,
68 * don't bother ... just return itself.
70 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
74 /* Encapsulate the VEVENT component into a complete VCALENDAR */
75 encaps = icalcomponent_new_citadel_vcalendar();
76 if (encaps == NULL) return NULL;
78 /* Encapsulate the subcomponent inside */
79 icalcomponent_add_component(encaps, subcomp);
81 /* Return the object we just created. */
87 * Write a calendar object into the specified user's calendar room.
88 * If the supplied user is NULL, this function writes the calendar object
89 * to the currently selected room.
91 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
94 icalcomponent *encaps = NULL;
95 struct CtdlMessage *msg = NULL;
96 icalcomponent *tmp=NULL;
98 if (cal == NULL) return;
100 /* If the supplied object is a subcomponent, encapsulate it in
101 * a full VCALENDAR component, and save that instead.
103 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
104 tmp = icalcomponent_new_clone(cal);
105 encaps = ical_encapsulate_subcomponent(tmp);
106 ical_write_to_cal(u, encaps);
107 icalcomponent_free(tmp);
108 icalcomponent_free(encaps);
112 ser = icalcomponent_as_ical_string_r(cal);
113 if (ser == NULL) return;
115 serlen = strlen(ser);
117 /* If the caller supplied a user, write to that user's default calendar room */
119 /* This handy API function does all the work for us. */
120 CtdlWriteObject(USERCALENDARROOM, /* which room */
121 "text/calendar", /* MIME type */
123 serlen + 1, /* length */
126 0, /* don't delete others of this type */
131 /* If the caller did not supply a user, write to the currently selected room */
133 struct CitContext *CCC = CC;
136 msg = malloc(sizeof(struct CtdlMessage));
137 memset(msg, 0, sizeof(struct CtdlMessage));
138 msg->cm_magic = CTDLMESSAGE_MAGIC;
139 msg->cm_anon_type = MES_NORMAL;
140 msg->cm_format_type = 4;
141 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
142 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
143 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
145 MsgBody = NewStrBufPlain(NULL, serlen + 100);
146 StrBufAppendBufPlain(MsgBody, HKEY("Content-type: text/calendar\r\n\r\n"), 0);
147 StrBufAppendBufPlain(MsgBody, ser, serlen, 0);
149 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
151 /* Now write the data */
152 CtdlSubmitMsg(msg, NULL, "", QP_EADDR);
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 recptypes *recp = NULL;
179 icalparameter *partstat = NULL;
180 char *serialized_reply = NULL;
181 char *reply_message_text = NULL;
183 struct CtdlMessage *msg = NULL;
184 recptypes *valid = NULL;
186 *organizer_string = '\0';
187 strcpy(summary_string, "Calendar item");
189 if (request == NULL) {
190 syslog(LOG_ERR, "calendar: trying to reply to NULL event");
194 the_reply = icalcomponent_new_clone(request);
195 if (the_reply == NULL) {
196 syslog(LOG_ERR, "calendar: cannot clone request");
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_by_kind(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);
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, 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(CM_RANGE(msg, eMesageText),
380 *ical_locate_part, /* callback function */
382 (void *) &ird, /* user data */
386 /* We're done with the incoming message, because we now have a
387 * calendar object in memory.
392 * Here is the real meat of this function. Handle the event.
394 if (ird.cal != NULL) {
395 /* Save this in the user's calendar if necessary */
396 if (!strcasecmp(action, "accept")) {
397 ical_write_to_cal(&CC->user, ird.cal);
400 /* Send a reply if necessary */
401 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
402 ical_send_a_reply(ird.cal, action);
405 /* We used to delete the invitation after handling it.
406 * We don't do that anymore, but here is the code that handled it:
407 * CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
410 /* Free the memory we allocated and return a response. */
411 icalcomponent_free(ird.cal);
413 cprintf("%d ok\n", CIT_OK);
417 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
421 /* should never get here */
426 * Figure out the UID of the calendar event being referred to in a
427 * REPLY object. This function is recursive.
429 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
430 icalcomponent *subcomponent;
433 /* If this object is a REPLY, then extract the UID. */
434 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
435 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
437 strcpy(uidbuf, icalproperty_get_comment(p));
441 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
442 * UID of the reply; we want the UID of the invitation being replied to.
444 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
445 subcomponent != NULL;
446 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
447 ical_learn_uid_of_reply(uidbuf, subcomponent);
453 * ical_update_my_calendar_with_reply() refers to this callback function; when we
454 * locate the message containing the calendar event we're replying to, this function
455 * gets called. It basically just sticks the message number in a supplied buffer.
457 void ical_hunt_for_event_to_update(long msgnum, void *data) {
460 msgnumptr = (long *) data;
465 struct original_event_container {
470 * Callback function for mime parser that hunts for calendar content types
471 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
472 * to fetch the object being updated)
474 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
475 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
476 char *cbid, void *cbuserdata) {
478 struct original_event_container *oec = NULL;
480 if ( (strcasecmp(cbtype, "text/calendar"))
481 && (strcasecmp(cbtype, "application/ics")) ) {
484 oec = (struct original_event_container *) cbuserdata;
485 if (oec->c != NULL) {
486 icalcomponent_free(oec->c);
488 oec->c = icalcomponent_new_from_string(content);
493 * Merge updated attendee information from a REPLY into an existing event.
495 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
497 icalproperty *e_attendee, *r_attendee;
499 /* First things first. If we're not looking at a VEVENT component,
500 * recurse through subcomponents until we find one.
502 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
503 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
505 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
506 ical_merge_attendee_reply(c, reply);
511 /* Now do the same thing with the reply.
513 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
514 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
516 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
517 ical_merge_attendee_reply(event, c);
522 /* Clone the reply, because we're going to rip its guts out. */
523 reply = icalcomponent_new_clone(reply);
525 /* At this point we're looking at the correct subcomponents.
526 * Iterate through the attendees looking for a match.
529 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
531 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
533 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
535 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
537 /* Check to see if these two attendees match...
540 e = icalproperty_get_attendee(e_attendee);
541 r = icalproperty_get_attendee(r_attendee);
546 /* ...and if they do, remove the attendee from the event
547 * and replace it with the attendee from the reply. (The
548 * reply's copy will have the same address, but an updated
551 icalcomponent_remove_property(event, e_attendee);
552 icalproperty_free(e_attendee);
553 icalcomponent_remove_property(reply, r_attendee);
554 icalcomponent_add_property(event, r_attendee);
556 /* Since we diddled both sets of attendees, we have to start
557 * the iteration over again. This will not create an infinite
558 * loop because we removed the attendee from the reply. (That's
559 * why we cloned the reply, and that's what we mean by "ripping
568 /* Free the *clone* of the reply. */
569 icalcomponent_free(reply);
576 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
577 * calendar event. The object has already been deserialized for us; all
578 * we have to do here is hunt for the event in our calendar, merge in the
579 * updated attendee status, and save it again.
581 * This function returns 0 on success, 1 if the event was not found in the
582 * user's calendar, or 2 if an internal error occurred.
584 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
586 char hold_rm[ROOMNAMELEN];
587 long msgnum_being_replaced = 0;
588 struct CtdlMessage *msg = NULL;
589 struct original_event_container oec;
590 icalcomponent *original_event;
591 char *serialized_event = NULL;
592 char roomname[ROOMNAMELEN];
593 char *message_text = NULL;
595 /* Figure out just what event it is we're dealing with */
596 strcpy(uid, "--==<< InVaLiD uId >>==--");
597 ical_learn_uid_of_reply(uid, cal);
598 syslog(LOG_DEBUG, "calendar: UID of event being replied to is <%s>", uid);
600 strcpy(hold_rm, CC->room.QRname); /* save current room */
602 if (CtdlGetRoom(&CC->room, USERCALENDARROOM) != 0) {
603 CtdlGetRoom(&CC->room, hold_rm);
604 syslog(LOG_ERR, "calendar: cannot get user calendar room");
609 * Look in the EUID index for a message with
610 * the Citadel EUID set to the value we're looking for. Since
611 * Citadel always sets the message EUID to the iCalendar UID of
612 * the event, this will work.
614 msgnum_being_replaced = CtdlLocateMessageByEuid(uid, &CC->room);
616 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
618 syslog(LOG_DEBUG, "calendar: msgnum_being_replaced == %ld", msgnum_being_replaced);
619 if (msgnum_being_replaced == 0) {
620 return(1); /* no calendar event found */
623 /* Now we know the ID of the message containing the event being updated.
624 * We don't actually have to delete it; that'll get taken care of by the
625 * server when we save another event with the same UID. This just gives
626 * us the ability to load the event into memory so we can diddle the
629 msg = CtdlFetchMessage(msgnum_being_replaced, 1, 1);
631 return(2); /* internal error */
634 mime_parser(CM_RANGE(msg, eMesageText),
635 *ical_locate_original_event, /* callback function */
637 &oec, /* user data */
642 original_event = oec.c;
643 if (original_event == NULL) {
644 syslog(LOG_ERR, "calendar: original_component is NULL");
648 /* Merge the attendee's updated status into the event */
649 ical_merge_attendee_reply(original_event, cal);
652 serialized_event = icalcomponent_as_ical_string_r(original_event);
653 icalcomponent_free(original_event); /* Don't need this anymore. */
654 if (serialized_event == NULL) return(2);
656 CtdlMailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
658 message_text = malloc(strlen(serialized_event) + SIZ);
659 if (message_text != NULL) {
660 sprintf(message_text,
661 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
665 msg = CtdlMakeMessage(&CC->user,
666 "", /* No recipient */
667 "", /* No recipient */
678 CIT_ICAL->avoid_sending_invitations = 1;
679 CtdlSubmitMsg(msg, NULL, roomname, QP_EADDR);
681 CIT_ICAL->avoid_sending_invitations = 0;
684 free(serialized_event);
690 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
691 * simply extracts the calendar object from the message, deserializes it, and
692 * passes it up to ical_update_my_calendar_with_reply() for processing.
694 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
695 struct CtdlMessage *msg = NULL;
696 struct ical_respond_data ird;
700 (strcasecmp(action, "update"))
701 && (strcasecmp(action, "ignore"))
703 cprintf("%d Action must be 'update' or 'ignore'\n",
704 ERROR + ILLEGAL_VALUE
709 msg = CtdlFetchMessage(msgnum, 1, 1);
711 cprintf("%d Message %ld not found.\n",
712 ERROR + ILLEGAL_VALUE,
718 memset(&ird, 0, sizeof ird);
719 strcpy(ird.desired_partnum, partnum);
720 mime_parser(CM_RANGE(msg, eMesageText),
721 *ical_locate_part, /* callback function */
723 (void *) &ird, /* user data */
727 /* We're done with the incoming message, because we now have a
728 * calendar object in memory.
733 * Here is the real meat of this function. Handle the event.
735 if (ird.cal != NULL) {
736 /* Update the user's calendar if necessary */
737 if (!strcasecmp(action, "update")) {
738 ret = ical_update_my_calendar_with_reply(ird.cal);
740 cprintf("%d Your calendar has been updated with this reply.\n",
744 cprintf("%d This event does not exist in your calendar.\n",
745 ERROR + FILE_NOT_FOUND);
748 cprintf("%d An internal error occurred.\n",
749 ERROR + INTERNAL_ERROR);
753 cprintf("%d This reply has been ignored.\n", CIT_OK);
756 /* Now that we've processed this message, we don't need it
757 * anymore. So delete it. (Don't do this anymore.)
758 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
761 /* Free the memory we allocated and return a response. */
762 icalcomponent_free(ird.cal);
767 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
771 /* should never get here */
776 * Search for a property in both the top level and in a VEVENT subcomponent
778 icalproperty *ical_ctdl_get_subprop(
780 icalproperty_kind which_prop
785 p = icalcomponent_get_first_property(cal, which_prop);
787 c = icalcomponent_get_first_component(cal,
788 ICAL_VEVENT_COMPONENT);
790 p = icalcomponent_get_first_property(c, which_prop);
798 * Check to see if two events overlap. Returns nonzero if they do.
799 * (This function is used in both Citadel and WebCit. If you change it in
800 * one place, change it in the other. Better yet, put it in a library.)
802 int ical_ctdl_is_overlap(
803 struct icaltimetype t1start,
804 struct icaltimetype t1end,
805 struct icaltimetype t2start,
806 struct icaltimetype t2end
808 if (icaltime_is_null_time(t1start)) return(0);
809 if (icaltime_is_null_time(t2start)) return(0);
811 /* if either event lacks end time, assume end = start */
812 if (icaltime_is_null_time(t1end))
813 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
815 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
817 * the end date is non-inclusive so adjust it by one
818 * day because our test is inclusive, note that a day is
819 * not too much because we are talking about all day
821 * if start = end we assume that nevertheless the whole
824 icaltime_adjust(&t1end, -1, 0, 0, 0);
828 if (icaltime_is_null_time(t2end))
829 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
831 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
832 icaltime_adjust(&t2end, -1, 0, 0, 0);
836 /* First, check for all-day events */
837 if (t1start.is_date || t2start.is_date) {
838 /* If event 1 ends before event 2 starts, we're in the clear. */
839 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
841 /* If event 2 ends before event 1 starts, we're also ok. */
842 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
847 /* syslog(LOG_DEBUG, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d",
848 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
849 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
852 /* Now check for overlaps using date *and* time. */
854 /* If event 1 ends before event 2 starts, we're in the clear. */
855 if (icaltime_compare(t1end, t2start) <= 0) return(0);
856 /* syslog(LOG_DEBUG, "calendar: first passed"); */
858 /* If event 2 ends before event 1 starts, we're also ok. */
859 if (icaltime_compare(t2end, t1start) <= 0) return(0);
860 /* syslog(LOG_DEBUG, "calendar: second passed"); */
862 /* Otherwise, they overlap. */
867 * Phase 6 of "hunt for conflicts"
868 * called by ical_conflicts_phase5()
870 * Now both the proposed and existing events have been boiled down to start and end times.
871 * Check for overlap and output any conflicts.
873 * Returns nonzero if a conflict was reported. This allows the caller to stop iterating.
875 int ical_conflicts_phase6(struct icaltimetype t1start,
876 struct icaltimetype t1end,
877 struct icaltimetype t2start,
878 struct icaltimetype t2end,
879 long existing_msgnum,
880 char *conflict_event_uid,
881 char *conflict_event_summary,
884 int conflict_reported = 0;
888 tt = icaltime_as_timet_with_zone(t1start, t1start.zone);
889 syslog(LOG_DEBUG, "PROPOSED START: %s", ctime(&tt));
890 tt = icaltime_as_timet_with_zone(t1end, t1end.zone);
891 syslog(LOG_DEBUG, " PROPOSED END: %s", ctime(&tt));
892 tt = icaltime_as_timet_with_zone(t2start, t2start.zone);
893 syslog(LOG_DEBUG, "EXISTING START: %s", ctime(&tt));
894 tt = icaltime_as_timet_with_zone(t2end, t2end.zone);
895 syslog(LOG_DEBUG, " EXISTING END: %s", ctime(&tt));
898 /* compare and output */
900 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
901 cprintf("%ld||%s|%s|%d|\n",
904 conflict_event_summary,
905 ( (!IsEmptyStr(compare_uid)
906 &&(!strcasecmp(compare_uid,
907 conflict_event_uid))) ? 1 : 0
910 conflict_reported = 1;
913 return(conflict_reported);
919 * Phase 5 of "hunt for conflicts"
920 * Called by ical_conflicts_phase4()
922 * We have the proposed event boiled down to start and end times.
923 * Now check it against an existing event.
925 void ical_conflicts_phase5(struct icaltimetype t1start,
926 struct icaltimetype t1end,
927 icalcomponent *existing_event,
928 long existing_msgnum,
931 char conflict_event_uid[SIZ];
932 char conflict_event_summary[SIZ];
933 struct icaltimetype t2start, t2end;
936 /* recur variables */
937 icalproperty *rrule = NULL;
938 struct icalrecurrencetype recur;
939 icalrecur_iterator *ritr = NULL;
940 struct icaldurationtype dur;
944 strcpy(conflict_event_uid, "");
945 strcpy(conflict_event_summary, "");
946 t2start = icaltime_null_time();
947 t2end = icaltime_null_time();
949 /* existing event stuff */
950 p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
951 if (p == NULL) return;
952 if (p != NULL) t2start = icalproperty_get_dtstart(p);
953 if (icaltime_is_utc(t2start)) {
954 t2start.zone = icaltimezone_get_utc_timezone();
957 t2start.zone = icalcomponent_get_timezone(existing_event,
958 icalparameter_get_tzid(
959 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
963 t2start.zone = get_default_icaltimezone();
967 p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
969 t2end = icalproperty_get_dtend(p);
971 if (icaltime_is_utc(t2end)) {
972 t2end.zone = icaltimezone_get_utc_timezone();
975 t2end.zone = icalcomponent_get_timezone(existing_event,
976 icalparameter_get_tzid(
977 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
981 t2end.zone = get_default_icaltimezone();
984 dur = icaltime_subtract(t2end, t2start);
987 memset (&dur, 0, sizeof(struct icaldurationtype));
990 rrule = ical_ctdl_get_subprop(existing_event, ICAL_RRULE_PROPERTY);
992 recur = icalproperty_get_rrule(rrule);
993 ritr = icalrecur_iterator_new(recur, t2start);
997 p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
999 strcpy(conflict_event_uid, icalproperty_get_comment(p));
1002 p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
1004 strcpy(conflict_event_summary, icalproperty_get_comment(p));
1007 if (ical_conflicts_phase6(t1start, t1end, t2start, t2end,
1008 existing_msgnum, conflict_event_uid, conflict_event_summary, compare_uid))
1010 num_recur = MAX_RECUR + 1; /* force it out of scope, no need to continue */
1014 t2start = icalrecur_iterator_next(ritr);
1015 if (!icaltime_is_null_time(t2end)) {
1016 const icaltimezone *hold_zone = t2end.zone;
1017 t2end = icaltime_add(t2start, dur);
1018 t2end.zone = hold_zone;
1023 if (icaltime_compare(t2start, t1end) < 0) {
1024 num_recur = MAX_RECUR + 1; /* force it out of scope */
1027 } while ( (rrule) && (!icaltime_is_null_time(t2start)) && (num_recur < MAX_RECUR) );
1028 icalrecur_iterator_free(ritr);
1035 * Phase 4 of "hunt for conflicts"
1036 * Called by ical_hunt_for_conflicts_backend()
1038 * At this point we've got it boiled down to two icalcomponent events in memory.
1039 * If they conflict, output something to the client.
1041 void ical_conflicts_phase4(icalcomponent *proposed_event,
1042 icalcomponent *existing_event,
1043 long existing_msgnum)
1045 struct icaltimetype t1start, t1end;
1047 char compare_uid[SIZ];
1049 /* recur variables */
1050 icalproperty *rrule = NULL;
1051 struct icalrecurrencetype recur;
1052 icalrecur_iterator *ritr = NULL;
1053 struct icaldurationtype dur;
1056 /* initialization */
1057 t1end = icaltime_null_time();
1058 *compare_uid = '\0';
1060 /* proposed event stuff */
1062 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
1066 t1start = icalproperty_get_dtstart(p);
1068 if (icaltime_is_utc(t1start)) {
1069 t1start.zone = icaltimezone_get_utc_timezone();
1072 t1start.zone = icalcomponent_get_timezone(proposed_event,
1073 icalparameter_get_tzid(
1074 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1077 if (!t1start.zone) {
1078 t1start.zone = get_default_icaltimezone();
1082 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
1084 t1end = icalproperty_get_dtend(p);
1086 if (icaltime_is_utc(t1end)) {
1087 t1end.zone = icaltimezone_get_utc_timezone();
1090 t1end.zone = icalcomponent_get_timezone(proposed_event,
1091 icalparameter_get_tzid(
1092 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1096 t1end.zone = get_default_icaltimezone();
1100 dur = icaltime_subtract(t1end, t1start);
1103 memset (&dur, 0, sizeof(struct icaldurationtype));
1106 rrule = ical_ctdl_get_subprop(proposed_event, ICAL_RRULE_PROPERTY);
1108 recur = icalproperty_get_rrule(rrule);
1109 ritr = icalrecur_iterator_new(recur, t1start);
1112 p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
1114 strcpy(compare_uid, icalproperty_get_comment(p));
1118 ical_conflicts_phase5(t1start, t1end, existing_event, existing_msgnum, compare_uid);
1121 t1start = icalrecur_iterator_next(ritr);
1122 if (!icaltime_is_null_time(t1end)) {
1123 const icaltimezone *hold_zone = t1end.zone;
1124 t1end = icaltime_add(t1start, dur);
1125 t1end.zone = hold_zone;
1130 } while ( (rrule) && (!icaltime_is_null_time(t1start)) && (num_recur < MAX_RECUR) );
1131 icalrecur_iterator_free(ritr);
1137 * Phase 3 of "hunt for conflicts"
1138 * Called by ical_hunt_for_conflicts()
1140 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
1141 icalcomponent *proposed_event;
1142 struct CtdlMessage *msg = NULL;
1143 struct ical_respond_data ird;
1145 proposed_event = (icalcomponent *)data;
1147 msg = CtdlFetchMessage(msgnum, 1, 1);
1148 if (msg == NULL) return;
1149 memset(&ird, 0, sizeof ird);
1150 strcpy(ird.desired_partnum, "_HUNT_");
1151 mime_parser(CM_RANGE(msg, eMesageText),
1152 *ical_locate_part, /* callback function */
1154 (void *) &ird, /* user data */
1159 if (ird.cal == NULL) return;
1161 ical_conflicts_phase4(proposed_event, ird.cal, msgnum);
1162 icalcomponent_free(ird.cal);
1168 * Phase 2 of "hunt for conflicts" operation.
1169 * At this point we have a calendar object which represents the VEVENT that
1170 * is proposed for addition to the calendar. Now hunt through the user's
1171 * calendar room, and output zero or more existing VEVENTs which conflict
1174 void ical_hunt_for_conflicts(icalcomponent *cal) {
1175 char hold_rm[ROOMNAMELEN];
1177 strcpy(hold_rm, CC->room.QRname); /* save current room */
1179 if (CtdlGetRoom(&CC->room, USERCALENDARROOM) != 0) {
1180 CtdlGetRoom(&CC->room, hold_rm);
1181 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1185 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1187 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1190 ical_hunt_for_conflicts_backend,
1195 CtdlGetRoom(&CC->room, hold_rm); /* return to saved room */
1202 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1204 void ical_conflicts(long msgnum, char *partnum) {
1205 struct CtdlMessage *msg = NULL;
1206 struct ical_respond_data ird;
1208 msg = CtdlFetchMessage(msgnum, 1, 1);
1210 cprintf("%d Message %ld not found\n",
1211 ERROR + ILLEGAL_VALUE,
1217 memset(&ird, 0, sizeof ird);
1218 strcpy(ird.desired_partnum, partnum);
1219 mime_parser(CM_RANGE(msg, eMesageText),
1220 *ical_locate_part, /* callback function */
1222 (void *) &ird, /* user data */
1228 if (ird.cal != NULL) {
1229 ical_hunt_for_conflicts(ird.cal);
1230 icalcomponent_free(ird.cal);
1234 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1240 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1242 * fb The VFREEBUSY component to which we are appending
1243 * top_level_cal The top-level VCALENDAR component which contains a VEVENT to be added
1245 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal) {
1249 struct icalperiodtype this_event_period = icalperiodtype_null_period();
1250 icaltimetype dtstart;
1253 /* recur variables */
1254 icalproperty *rrule = NULL;
1255 struct icalrecurrencetype recur;
1256 icalrecur_iterator *ritr = NULL;
1257 struct icaldurationtype dur;
1260 if (!top_level_cal) return;
1262 /* Find the VEVENT component containing an event */
1263 cal = icalcomponent_get_first_component(top_level_cal, ICAL_VEVENT_COMPONENT);
1266 /* If this event is not opaque, the user isn't publishing it as
1267 * busy time, so don't bother doing anything else.
1269 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1271 v = icalproperty_get_value(p);
1273 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1280 * Now begin calculating the event start and end times.
1282 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1284 dtstart = icalproperty_get_dtstart(p);
1286 if (icaltime_is_utc(dtstart)) {
1287 dtstart.zone = icaltimezone_get_utc_timezone();
1290 dtstart.zone = icalcomponent_get_timezone(top_level_cal,
1291 icalparameter_get_tzid(
1292 icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER)
1295 if (!dtstart.zone) {
1296 dtstart.zone = get_default_icaltimezone();
1300 dtend = icalcomponent_get_dtend(cal);
1301 if (!icaltime_is_null_time(dtend)) {
1302 dur = icaltime_subtract(dtend, dtstart);
1305 memset (&dur, 0, sizeof(struct icaldurationtype));
1308 /* Is a recurrence specified? If so, get ready to process it... */
1309 rrule = ical_ctdl_get_subprop(cal, ICAL_RRULE_PROPERTY);
1311 recur = icalproperty_get_rrule(rrule);
1312 ritr = icalrecur_iterator_new(recur, dtstart);
1316 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1317 this_event_period.start = dtstart;
1319 if (!icaltime_is_null_time(dtend)) {
1320 this_event_period.end = dtend;
1323 /* Convert the timestamps to UTC. It's ok to do this because we've already expanded
1324 * recurrences and this data is never going to get used again.
1326 this_event_period.start = icaltime_convert_to_zone(
1327 this_event_period.start,
1328 icaltimezone_get_utc_timezone()
1330 this_event_period.end = icaltime_convert_to_zone(
1331 this_event_period.end,
1332 icaltimezone_get_utc_timezone()
1336 icalcomponent_add_property(fb, icalproperty_new_freebusy(this_event_period));
1338 /* Make sure the DTSTART property of the freebusy *list* is set to
1339 * the DTSTART property of the *earliest event*.
1341 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1343 icalcomponent_set_dtstart(fb, this_event_period.start);
1346 if (icaltime_compare(this_event_period.start, icalcomponent_get_dtstart(fb)) < 0) {
1347 icalcomponent_set_dtstart(fb, this_event_period.start);
1351 /* Make sure the DTEND property of the freebusy *list* is set to
1352 * the DTEND property of the *latest event*.
1354 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1356 icalcomponent_set_dtend(fb, this_event_period.end);
1359 if (icaltime_compare(this_event_period.end, icalcomponent_get_dtend(fb)) > 0) {
1360 icalcomponent_set_dtend(fb, this_event_period.end);
1365 dtstart = icalrecur_iterator_next(ritr);
1366 if (!icaltime_is_null_time(dtend)) {
1367 dtend = icaltime_add(dtstart, dur);
1368 dtend.zone = dtstart.zone;
1373 } while ( (rrule) && (!icaltime_is_null_time(dtstart)) && (num_recur < MAX_RECUR) ) ;
1374 icalrecur_iterator_free(ritr);
1380 * Backend for ical_freebusy()
1382 * This function simply loads the messages in the user's calendar room,
1383 * which contain VEVENTs, then strips them of all non-freebusy data, and
1384 * adds them to the supplied VCALENDAR.
1387 void ical_freebusy_backend(long msgnum, void *data) {
1389 struct CtdlMessage *msg = NULL;
1390 struct ical_respond_data ird;
1392 fb = (icalcomponent *)data; /* User-supplied data will be the VFREEBUSY component */
1394 msg = CtdlFetchMessage(msgnum, 1, 1);
1395 if (msg == NULL) return;
1396 memset(&ird, 0, sizeof ird);
1397 strcpy(ird.desired_partnum, "_HUNT_");
1398 mime_parser(CM_RANGE(msg, eMesageText),
1399 *ical_locate_part, /* callback function */
1401 (void *) &ird, /* user data */
1407 ical_add_to_freebusy(fb, ird.cal); /* Add VEVENT times to VFREEBUSY */
1408 icalcomponent_free(ird.cal);
1415 * Grab another user's free/busy times
1417 void ical_freebusy(char *who) {
1418 struct ctdluser usbuf;
1419 char calendar_room_name[ROOMNAMELEN];
1420 char hold_rm[ROOMNAMELEN];
1421 char *serialized_request = NULL;
1422 icalcomponent *encaps = NULL;
1423 icalcomponent *fb = NULL;
1424 int found_user = (-1);
1425 recptypes *recp = NULL;
1430 int config_lines = 0;
1432 /* First try an exact match. */
1433 found_user = CtdlGetUser(&usbuf, who);
1435 /* If not found, try it as an unqualified email address. */
1436 if (found_user != 0) {
1438 recp = validate_recipients(buf, NULL, 0);
1439 syslog(LOG_DEBUG, "calendar: trying <%s>", buf);
1441 if (recp->num_local == 1) {
1442 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1444 free_recipients(recp);
1448 /* If still not found, try it as an address qualified with the
1449 * primary FQDN of this Citadel node.
1451 if (found_user != 0) {
1452 snprintf(buf, sizeof buf, "%s@%s", who, CtdlGetConfigStr("c_fqdn"));
1453 syslog(LOG_DEBUG, "calendar: trying <%s>", buf);
1454 recp = validate_recipients(buf, NULL, 0);
1456 if (recp->num_local == 1) {
1457 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1459 free_recipients(recp);
1463 /* Still not found? Try qualifying it with every domain we
1464 * might have addresses in.
1466 if (found_user != 0) {
1467 config_lines = num_tokens(inetcfg, '\n');
1468 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1469 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1470 extract_token(host, buf, 0, '|', sizeof host);
1471 extract_token(type, buf, 1, '|', sizeof type);
1473 if ( (!strcasecmp(type, "localhost"))
1474 || (!strcasecmp(type, "directory")) ) {
1475 snprintf(buf, sizeof buf, "%s@%s", who, host);
1476 syslog(LOG_DEBUG, "calendar: trying <%s>", buf);
1477 recp = validate_recipients(buf, NULL, 0);
1479 if (recp->num_local == 1) {
1480 found_user = CtdlGetUser(&usbuf, recp->recp_local);
1482 free_recipients(recp);
1488 if (found_user != 0) {
1489 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1493 CtdlMailboxName(calendar_room_name, sizeof calendar_room_name,
1494 &usbuf, USERCALENDARROOM);
1496 strcpy(hold_rm, CC->room.QRname); /* save current room */
1498 if (CtdlGetRoom(&CC->room, calendar_room_name) != 0) {
1499 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1500 CtdlGetRoom(&CC->room, hold_rm);
1504 /* Create a VFREEBUSY subcomponent */
1505 syslog(LOG_DEBUG, "calendar: creating VFREEBUSY component");
1506 fb = icalcomponent_new_vfreebusy();
1508 cprintf("%d Internal error: cannot allocate memory.\n", ERROR + INTERNAL_ERROR);
1509 CtdlGetRoom(&CC->room, hold_rm);
1513 /* Set the method to PUBLISH */
1514 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1516 /* Set the DTSTAMP to right now. */
1517 icalcomponent_set_dtstamp(fb, icaltime_from_timet_with_zone(time(NULL), 0, icaltimezone_get_utc_timezone()));
1519 /* Add the user's email address as ORGANIZER */
1520 sprintf(buf, "MAILTO:%s", who);
1521 if (strchr(buf, '@') == NULL) {
1523 strcat(buf, CtdlGetConfigStr("c_fqdn"));
1525 for (i=0; buf[i]; ++i) {
1526 if (buf[i]==' ') buf[i] = '_';
1528 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1530 /* Add busy time from events */
1531 syslog(LOG_DEBUG, "calendar: adding busy time from events");
1532 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1534 /* If values for DTSTART and DTEND are still not present, set them
1535 * to yesterday and tomorrow as default values.
1537 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1538 icalcomponent_set_dtstart(fb, icaltime_from_timet_with_zone(time(NULL)-86400L, 0, icaltimezone_get_utc_timezone()));
1540 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1541 icalcomponent_set_dtend(fb, icaltime_from_timet_with_zone(time(NULL)+86400L, 0, icaltimezone_get_utc_timezone()));
1544 /* Put the freebusy component into the calendar component */
1545 syslog(LOG_DEBUG, "calendar: encapsulating");
1546 encaps = ical_encapsulate_subcomponent(fb);
1547 if (encaps == NULL) {
1548 icalcomponent_free(fb);
1549 cprintf("%d Internal error: cannot allocate memory.\n",
1550 ERROR + INTERNAL_ERROR);
1551 CtdlGetRoom(&CC->room, hold_rm);
1555 /* Set the method to PUBLISH */
1556 syslog(LOG_DEBUG, "calendar: setting method");
1557 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1560 syslog(LOG_DEBUG, "calendar: serializing");
1561 serialized_request = icalcomponent_as_ical_string_r(encaps);
1562 icalcomponent_free(encaps); /* Don't need this anymore. */
1564 cprintf("%d Free/busy for %s\n", LISTING_FOLLOWS, usbuf.fullname);
1565 if (serialized_request != NULL) {
1566 client_write(serialized_request, strlen(serialized_request));
1567 free(serialized_request);
1571 /* Go back to the room from which we came... */
1572 CtdlGetRoom(&CC->room, hold_rm);
1578 * Backend for ical_getics()
1580 * This is a ForEachMessage() callback function that searches the current room
1581 * for calendar events and adds them each into one big calendar component.
1583 void ical_getics_backend(long msgnum, void *data) {
1584 icalcomponent *encaps, *c;
1585 struct CtdlMessage *msg = NULL;
1586 struct ical_respond_data ird;
1588 encaps = (icalcomponent *)data;
1589 if (encaps == NULL) return;
1591 /* Look for the calendar event... */
1593 msg = CtdlFetchMessage(msgnum, 1, 1);
1594 if (msg == NULL) return;
1595 memset(&ird, 0, sizeof ird);
1596 strcpy(ird.desired_partnum, "_HUNT_");
1597 mime_parser(CM_RANGE(msg, eMesageText),
1598 *ical_locate_part, /* callback function */
1600 (void *) &ird, /* user data */
1605 if (ird.cal == NULL) return;
1607 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1608 * are responsible for "the_request"'s memory -- it will be freed
1609 * when we free "encaps".
1612 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1613 * in. This will almost never happen.
1615 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1616 icalcomponent_add_component(encaps, ird.cal);
1619 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1620 * and other components encapsulated inside, we have to extract them.
1623 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1625 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1627 /* For VTIMEZONE components, suppress duplicates of the same tzid */
1629 if (icalcomponent_isa(c) == ICAL_VTIMEZONE_COMPONENT) {
1630 icalproperty *p = icalcomponent_get_first_property(c, ICAL_TZID_PROPERTY);
1632 const char *tzid = icalproperty_get_tzid(p);
1633 if (!icalcomponent_get_timezone(encaps, tzid)) {
1634 icalcomponent_add_component(encaps,
1635 icalcomponent_new_clone(c));
1640 /* All other types of components can go in verbatim */
1642 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1645 icalcomponent_free(ird.cal);
1652 * Retrieve all of the calendar items in the current room, and output them
1653 * as a single icalendar object.
1655 void ical_getics(void)
1657 icalcomponent *encaps = NULL;
1660 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1661 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1662 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1663 return; /* Not an iCalendar-centric room */
1666 encaps = icalcomponent_new_vcalendar();
1667 if (encaps == NULL) {
1668 syslog(LOG_ERR, "calendar: could not allocate component!");
1669 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1673 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1675 /* Set the Product ID */
1676 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1678 /* Set the Version Number */
1679 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1681 /* Set the method to PUBLISH */
1682 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1684 /* Now go through the room encapsulating all calendar items. */
1685 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1688 ical_getics_backend,
1692 ser = icalcomponent_as_ical_string_r(encaps);
1693 icalcomponent_free(encaps); /* Don't need this anymore. */
1694 client_write(ser, strlen(ser));
1701 * Helper callback function for ical_putics() to discover which TZID's we need.
1702 * Simply put the tzid name string into a hash table. After the callbacks are
1703 * done we'll go through them and attach the ones that we have.
1705 void ical_putics_grabtzids(icalparameter *param, void *data)
1707 const char *tzid = icalparameter_get_tzid(param);
1708 HashList *keys = (HashList *) data;
1710 if ( (keys) && (tzid) && (!IsEmptyStr(tzid)) ) {
1711 Put(keys, tzid, strlen(tzid), strdup(tzid), NULL);
1717 * Delete all of the calendar items in the current room, and replace them
1718 * with calendar items from a client-supplied data stream.
1720 void ical_putics(void)
1722 char *calstream = NULL;
1725 icalcomponent *encaps = NULL;
1726 HashList *tzidlist = NULL;
1732 /* Only allow this operation if we're in a room containing a calendar or tasks view */
1733 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1734 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1735 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1739 /* Only allow this operation if we have permission to overwrite the existing calendar */
1740 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1741 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1745 cprintf("%d Transmit data now\n", SEND_LISTING);
1746 calstream = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
1747 if (calstream == NULL) {
1751 cal = icalcomponent_new_from_string(calstream);
1754 /* We got our data stream -- now do something with it. */
1756 /* Delete the existing messages in the room, because we are overwriting
1757 * the entire calendar with an entire new (or updated) calendar.
1758 * (Careful: this opens an S_ROOMS critical section!)
1760 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1762 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1763 * in. This will almost never happen.
1765 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1766 ical_write_to_cal(NULL, cal);
1769 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1770 * and other components encapsulated inside, we have to extract them.
1773 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1775 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1777 /* Non-VTIMEZONE components each get written as individual messages.
1778 * But we also need to attach the relevant VTIMEZONE components to them.
1780 if ( (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT)
1781 && (encaps = icalcomponent_new_vcalendar()) ) {
1782 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1783 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1784 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1786 /* Attach any needed timezones here */
1787 tzidlist = NewHash(1, NULL);
1789 icalcomponent_foreach_tzid(c, ical_putics_grabtzids, tzidlist);
1791 HashPos = GetNewHashPos(tzidlist, 0);
1793 while (GetNextHashPos(tzidlist, HashPos, &len, &Key, &Value)) {
1794 syslog(LOG_DEBUG, "calendar: attaching timezone '%s'", (char*) Value);
1795 icaltimezone *t = NULL;
1797 /* First look for a timezone attached to the original calendar */
1798 t = icalcomponent_get_timezone(cal, Value);
1800 /* Try built-in tzdata if the right one wasn't attached */
1802 t = icaltimezone_get_builtin_timezone(Value);
1805 /* I've got a valid timezone to attach. */
1807 icalcomponent_add_component(encaps,
1808 icalcomponent_new_clone(
1809 icaltimezone_get_component(t)
1815 DeleteHashPos(&HashPos);
1816 DeleteHash(&tzidlist);
1818 /* Now attach the component itself (usually a VEVENT or VTODO) */
1819 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1821 /* Write it to the message store */
1822 ical_write_to_cal(NULL, encaps);
1823 icalcomponent_free(encaps);
1828 icalcomponent_free(cal);
1833 * All Citadel calendar commands from the client come through here.
1835 void cmd_ical(char *argbuf)
1843 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1845 /* Allow "test" and "freebusy" subcommands without logging in. */
1847 if (!strcasecmp(subcmd, "test")) {
1848 cprintf("%d This server supports calendaring\n", CIT_OK);
1852 if (!strcasecmp(subcmd, "freebusy")) {
1853 extract_token(who, argbuf, 1, '|', sizeof who);
1858 if (!strcasecmp(subcmd, "sgi")) {
1859 CIT_ICAL->server_generated_invitations =
1860 (extract_int(argbuf, 1) ? 1 : 0) ;
1862 CIT_OK, CIT_ICAL->server_generated_invitations);
1866 if (CtdlAccessCheck(ac_logged_in)) return;
1868 if (!strcasecmp(subcmd, "respond")) {
1869 msgnum = extract_long(argbuf, 1);
1870 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1871 extract_token(action, argbuf, 3, '|', sizeof action);
1872 ical_respond(msgnum, partnum, action);
1876 if (!strcasecmp(subcmd, "handle_rsvp")) {
1877 msgnum = extract_long(argbuf, 1);
1878 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1879 extract_token(action, argbuf, 3, '|', sizeof action);
1880 ical_handle_rsvp(msgnum, partnum, action);
1884 if (!strcasecmp(subcmd, "conflicts")) {
1885 msgnum = extract_long(argbuf, 1);
1886 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1887 ical_conflicts(msgnum, partnum);
1891 if (!strcasecmp(subcmd, "getics")) {
1896 if (!strcasecmp(subcmd, "putics")) {
1901 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1907 * We don't know if the calendar room exists so we just create it at login
1909 void ical_CtdlCreateRoom(void)
1914 /* Create the calendar room if it doesn't already exist */
1915 CtdlCreateRoom(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1917 /* Set expiration policy to manual; otherwise objects will be lost! */
1918 if (CtdlGetRoomLock(&qr, USERCALENDARROOM)) {
1919 syslog(LOG_ERR, "calendar: couldn't get the user calendar room");
1922 qr.QRep.expire_mode = EXPIRE_MANUAL;
1923 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1924 CtdlPutRoomLock(&qr);
1926 /* Set the view to a calendar view */
1927 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1928 vbuf.v_view = VIEW_CALENDAR;
1929 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1931 /* Create the tasks list room if it doesn't already exist */
1932 CtdlCreateRoom(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1934 /* Set expiration policy to manual; otherwise objects will be lost! */
1935 if (CtdlGetRoomLock(&qr, USERTASKSROOM)) {
1936 syslog(LOG_ERR, "calendar: couldn't get the user calendar room!");
1939 qr.QRep.expire_mode = EXPIRE_MANUAL;
1940 qr.QRdefaultview = VIEW_TASKS;
1941 CtdlPutRoomLock(&qr);
1943 /* Set the view to a task list view */
1944 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1945 vbuf.v_view = VIEW_TASKS;
1946 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1948 /* Create the notes room if it doesn't already exist */
1949 CtdlCreateRoom(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1951 /* Set expiration policy to manual; otherwise objects will be lost! */
1952 if (CtdlGetRoomLock(&qr, USERNOTESROOM)) {
1953 syslog(LOG_ERR, "calendar: couldn't get the user calendar room!");
1956 qr.QRep.expire_mode = EXPIRE_MANUAL;
1957 qr.QRdefaultview = VIEW_NOTES;
1958 CtdlPutRoomLock(&qr);
1960 /* Set the view to a notes view */
1961 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1962 vbuf.v_view = VIEW_NOTES;
1963 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1970 * ical_send_out_invitations() is called by ical_saving_vevent() when it finds a VEVENT.
1972 * top_level_cal is the highest available level calendar object.
1973 * cal is the subcomponent containing the VEVENT.
1975 * Note: if you change the encapsulation code here, change it in WebCit's ical_encapsulate_subcomponent()
1977 void ical_send_out_invitations(icalcomponent *top_level_cal, icalcomponent *cal) {
1978 icalcomponent *the_request = NULL;
1979 char *serialized_request = NULL;
1980 icalcomponent *encaps = NULL;
1981 char *request_message_text = NULL;
1982 struct CtdlMessage *msg = NULL;
1983 recptypes *valid = NULL;
1984 char attendees_string[SIZ];
1985 int num_attendees = 0;
1986 char this_attendee[256];
1987 icalproperty *attendee = NULL;
1988 char summary_string[SIZ];
1989 icalproperty *summary = NULL;
1992 struct icaltimetype t;
1993 const icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
1995 const icaltimezone *z;
1996 int num_zones_attached = 0;
1997 int zone_already_attached;
1998 icalparameter *tzidp = NULL;
1999 const char *tzidc = NULL;
2002 syslog(LOG_ERR, "calendar: trying to reply to NULL event?");
2007 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
2008 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2009 ical_send_out_invitations(top_level_cal,
2010 icalcomponent_get_first_component(
2011 cal, ICAL_VEVENT_COMPONENT
2017 /* Clone the event */
2018 the_request = icalcomponent_new_clone(cal);
2019 if (the_request == NULL) {
2020 syslog(LOG_ERR, "calendar: cannot clone calendar object");
2024 /* Extract the summary string -- we'll use it as the
2025 * message subject for the request
2027 strcpy(summary_string, "Meeting request");
2028 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
2029 if (summary != NULL) {
2030 if (icalproperty_get_summary(summary)) {
2031 strcpy(summary_string,
2032 icalproperty_get_summary(summary) );
2036 /* Determine who the recipients of this message are (the attendees) */
2037 strcpy(attendees_string, "");
2038 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
2039 const char *ch = icalproperty_get_attendee(attendee);
2040 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
2041 safestrncpy(this_attendee, ch + 7, sizeof(this_attendee));
2043 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
2044 snprintf(&attendees_string[strlen(attendees_string)],
2045 sizeof(attendees_string) - strlen(attendees_string),
2054 syslog(LOG_DEBUG, "calendar: <%d> attendees: <%s>", num_attendees, attendees_string);
2056 /* If there are no attendees, there are no invitations to send, so...
2057 * don't bother putting one together! Punch out, Maverick!
2059 if (num_attendees == 0) {
2060 icalcomponent_free(the_request);
2064 /* Encapsulate the VEVENT component into a complete VCALENDAR */
2065 encaps = icalcomponent_new_vcalendar();
2066 if (encaps == NULL) {
2067 syslog(LOG_ERR, "calendar: could not allocate component!");
2068 icalcomponent_free(the_request);
2072 /* Set the Product ID */
2073 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
2075 /* Set the Version Number */
2076 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
2078 /* Set the method to REQUEST */
2079 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
2081 /* Look for properties containing timezone parameters, to see if we need to attach VTIMEZONEs */
2082 for (p = icalcomponent_get_first_property(the_request, ICAL_ANY_PROPERTY);
2084 p = icalcomponent_get_next_property(the_request, ICAL_ANY_PROPERTY))
2086 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
2087 || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
2088 || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
2089 || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
2090 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
2091 || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
2092 || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
2093 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
2094 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
2095 || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
2096 || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
2097 || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
2098 || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
2100 t = icalproperty_get_dtstart(p); // it's safe to use dtstart for all of them
2102 /* Determine the tzid in order for some of the conditions below to work */
2103 tzidp = icalproperty_get_first_parameter(p, ICAL_TZID_PARAMETER);
2105 tzidc = icalparameter_get_tzid(tzidp);
2111 /* First see if there's a timezone attached to the data structure itself */
2112 if (icaltime_is_utc(t)) {
2113 z = icaltimezone_get_utc_timezone();
2116 z = icaltime_get_timezone(t);
2119 /* If not, try to determine the tzid from the parameter using attached zones */
2120 if ((!z) && (tzidc)) {
2121 z = icalcomponent_get_timezone(top_level_cal, tzidc);
2124 /* Still no good? Try our internal database */
2125 if ((!z) && (tzidc)) {
2126 z = icaltimezone_get_builtin_timezone_from_tzid(tzidc);
2130 /* We have a valid timezone. Good. Now we need to attach it. */
2132 zone_already_attached = 0;
2133 for (i=0; i<5; ++i) {
2134 if (z == attached_zones[i]) {
2135 /* We've already got this one, no need to attach another. */
2136 ++zone_already_attached;
2139 if ((!zone_already_attached) && (num_zones_attached < 5)) {
2140 /* This is a new one, so attach it. */
2141 attached_zones[num_zones_attached++] = z;
2144 icalproperty_set_parameter(p,
2145 icalparameter_new_tzid(icaltimezone_get_tzid(z))
2151 /* Encapsulate any timezones we need */
2152 if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
2154 zc = icalcomponent_new_clone(icaltimezone_get_component(attached_zones[i]));
2155 icalcomponent_add_component(encaps, zc);
2158 /* Here we go: encapsulate the VEVENT into the VCALENDAR. We now no longer
2159 * are responsible for "the_request"'s memory -- it will be freed
2160 * when we free "encaps".
2162 icalcomponent_add_component(encaps, the_request);
2165 serialized_request = icalcomponent_as_ical_string_r(encaps);
2166 icalcomponent_free(encaps); /* Don't need this anymore. */
2167 if (serialized_request == NULL) return;
2169 reqsize = strlen(serialized_request) + SIZ;
2170 request_message_text = malloc(reqsize);
2171 if (request_message_text != NULL) {
2172 snprintf(request_message_text, reqsize,
2173 "Content-type: text/calendar\r\n\r\n%s\r\n",
2177 msg = CtdlMakeMessage(
2179 NULL, /* No single recipient here */
2180 NULL, /* No single recipient here */
2186 summary_string, /* Use summary for subject */
2188 request_message_text,
2193 valid = validate_recipients(attendees_string, NULL, 0);
2194 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
2196 free_recipients(valid);
2199 free(serialized_request);
2204 * When a calendar object is being saved, determine whether it's a VEVENT
2205 * and the user saving it is the organizer. If so, send out invitations
2206 * to any listed attendees.
2208 * This function is recursive. The caller can simply supply the same object
2209 * as both arguments. When it recurses it will alter the second argument
2210 * while holding on to the top level object. This allows us to go back and
2211 * grab things like time zones which might be attached.
2214 void ical_saving_vevent(icalcomponent *top_level_cal, icalcomponent *cal) {
2216 icalproperty *organizer = NULL;
2217 char organizer_string[SIZ];
2219 syslog(LOG_DEBUG, "calendar: ical_saving_vevent() has been called");
2221 /* Don't send out invitations unless the client wants us to. */
2222 if (CIT_ICAL->server_generated_invitations == 0) {
2226 /* Don't send out invitations if we've been asked not to. */
2227 if (CIT_ICAL->avoid_sending_invitations > 0) {
2231 strcpy(organizer_string, "");
2233 * The VEVENT subcomponent is the one we're interested in.
2234 * Send out invitations if, and only if, this user is the Organizer.
2236 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
2237 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
2238 if (organizer != NULL) {
2239 if (icalproperty_get_organizer(organizer)) {
2240 strcpy(organizer_string,
2241 icalproperty_get_organizer(organizer));
2244 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
2245 strcpy(organizer_string, &organizer_string[7]);
2246 striplt(organizer_string);
2248 * If the user saving the event is listed as the
2249 * organizer, then send out invitations.
2251 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
2252 ical_send_out_invitations(top_level_cal, cal);
2257 /* If the component has subcomponents, recurse through them. */
2258 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2260 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2261 /* Recursively process subcomponent */
2262 ical_saving_vevent(top_level_cal, c);
2270 * Back end for ical_obj_beforesave()
2271 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
2272 * the summary of the event (becomes message subject),
2273 * and the start time (becomes message date/time).
2275 void ical_obj_beforesave_backend(char *name, char *filename, char *partnum,
2276 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2277 char *encoding, char *cbid, void *cbuserdata)
2280 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
2282 char new_uid[256] = "";
2283 struct CtdlMessage *msg = (struct CtdlMessage *) cbuserdata;
2287 /* We're only interested in calendar data. */
2288 if ( (strcasecmp(cbtype, "text/calendar"))
2289 && (strcasecmp(cbtype, "application/ics")) ) {
2293 /* Hunt for the UID and drop it in
2294 * the "user data" pointer for the MIME parser. When
2295 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2298 whole_cal = icalcomponent_new_from_string(content);
2301 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2302 nested_event = icalcomponent_get_first_component(
2303 cal, ICAL_VEVENT_COMPONENT);
2304 if (nested_event != NULL) {
2308 nested_todo = icalcomponent_get_first_component(
2309 cal, ICAL_VTODO_COMPONENT);
2310 if (nested_todo != NULL) {
2318 /* Set the message EUID to the iCalendar UID */
2320 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2322 /* If there's no uid we must generate one */
2323 generate_uuid(new_uid);
2324 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
2325 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2328 pch = icalproperty_get_comment(p);
2329 if (!IsEmptyStr(pch)) {
2330 CM_SetField(msg, eExclusiveID, pch, strlen(pch));
2331 syslog(LOG_DEBUG, "calendar: saving calendar UID <%s>", pch);
2335 /* Set the message subject to the iCalendar summary */
2337 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
2339 pch = icalproperty_get_comment(p);
2340 if (!IsEmptyStr(pch)) {
2343 subj = rfc2047encode(pch, strlen(pch));
2344 CM_SetAsField(msg, eMsgSubject, &subj, strlen(subj));
2348 /* Set the message date/time to the iCalendar start time */
2350 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
2353 idtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
2355 CM_SetFieldLONG(msg, eTimestamp, idtstart);
2360 icalcomponent_free(cal);
2361 if (whole_cal != cal) {
2362 icalcomponent_free(whole_cal);
2371 * See if we need to prevent the object from being saved (we don't allow
2372 * MIME types other than text/calendar in "calendar" or "tasks" rooms).
2374 * If the message is being saved, we also set various message header fields
2375 * using data found in the iCalendar object.
2377 int ical_obj_beforesave(struct CtdlMessage *msg, recptypes *recp)
2379 /* First determine if this is a calendar or tasks room */
2380 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
2381 && (CC->room.QRdefaultview != VIEW_TASKS)
2383 return(0); /* Not an iCalendar-centric room */
2386 /* It must be an RFC822 message! */
2387 if (msg->cm_format_type != 4) {
2388 syslog(LOG_DEBUG, "calendar: rejecting non-RFC822 message");
2389 return(1); /* You tried to save a non-RFC822 message! */
2392 if (CM_IsEmpty(msg, eMesageText)) {
2393 return(1); /* You tried to save a null message! */
2396 /* Do all of our lovely back-end parsing */
2397 mime_parser(CM_RANGE(msg, eMesageText),
2398 *ical_obj_beforesave_backend,
2409 * Things we need to do after saving a calendar event.
2411 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2412 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2413 char *encoding, char *cbid, void *cbuserdata)
2417 /* We're only interested in calendar items here. */
2418 if ( (strcasecmp(cbtype, "text/calendar"))
2419 && (strcasecmp(cbtype, "application/ics")) ) {
2423 /* Hunt for the UID and drop it in
2424 * the "user data" pointer for the MIME parser. When
2425 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2428 if ( (!strcasecmp(cbtype, "text/calendar"))
2429 || (!strcasecmp(cbtype, "application/ics")) ) {
2430 cal = icalcomponent_new_from_string(content);
2432 ical_saving_vevent(cal, cal);
2433 icalcomponent_free(cal);
2440 * Things we need to do after saving a calendar event.
2441 * (This will start back end tasks such as automatic generation of invitations,
2442 * if such actions are appropriate.)
2444 int ical_obj_aftersave(struct CtdlMessage *msg, recptypes *recp)
2446 char roomname[ROOMNAMELEN];
2449 * If this isn't the Calendar> room, no further action is necessary.
2452 /* First determine if this is our room */
2453 CtdlMailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2454 if (strcasecmp(roomname, CC->room.QRname)) {
2455 return(0); /* Not the Calendar room -- don't do anything. */
2458 /* It must be an RFC822 message! */
2459 if (msg->cm_format_type != 4) return(1);
2461 /* Reject null messages */
2462 if (CM_IsEmpty(msg, eMesageText)) return(1);
2464 /* Now recurse through it looking for our icalendar data */
2465 mime_parser(CM_RANGE(msg, eMesageText),
2466 *ical_obj_aftersave_backend,
2476 void ical_session_startup(void) {
2477 CIT_ICAL = malloc(sizeof(struct cit_ical));
2478 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2481 void ical_session_shutdown(void) {
2487 * Back end for ical_fixed_output()
2489 void ical_fixed_output_backend(icalcomponent *cal,
2497 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2499 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2502 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2504 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2507 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2509 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2512 /* If the component has attendees, iterate through them. */
2513 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2514 ch = icalproperty_get_attendee(p);
2516 !strncasecmp(ch, "MAILTO:", 7)) {
2518 /* screen name or email address */
2519 safestrncpy(buf, ch + 7, sizeof(buf));
2521 cprintf("%s ", buf);
2526 /* If the component has subcomponents, recurse through them. */
2527 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2529 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2530 /* Recursively process subcomponent */
2531 ical_fixed_output_backend(c, recursion_level+1);
2538 * Function to output iCalendar data as plain text. Nobody uses MSG0
2539 * anymore, so really this is just so we expose the vCard data to the full
2542 void ical_fixed_output(char *ptr, int len) {
2546 stringy_cal = malloc(len + 1);
2547 safestrncpy(stringy_cal, ptr, len + 1);
2548 cal = icalcomponent_new_from_string(stringy_cal);
2555 ical_fixed_output_backend(cal, 0);
2557 /* Free the memory we obtained from libical's constructor */
2558 icalcomponent_free(cal);
2562 void serv_calendar_destroy(void)
2564 icaltimezone_free_builtin_timezones();
2569 * Register this module with the Citadel server.
2571 CTDL_MODULE_INIT(calendar)
2576 /* Tell libical to return errors instead of aborting if it gets bad data */
2578 #ifdef LIBICAL_ICAL_EXPORT // cheap and sleazy way to detect libical >=2.0
2579 icalerror_set_errors_are_fatal(0);
2581 icalerror_errors_are_fatal = 0;
2584 /* Use our own application prefix in tzid's generated from system tzdata */
2585 icaltimezone_set_tzid_prefix("/citadel.org/");
2587 /* Initialize our hook functions */
2588 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2589 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2590 CtdlRegisterSessionHook(ical_CtdlCreateRoom, EVT_LOGIN, PRIO_LOGIN + 1);
2591 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2592 CtdlRegisterSessionHook(ical_session_startup, EVT_START, PRIO_START + 1);
2593 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP, PRIO_STOP + 80);
2594 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2595 CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
2596 CtdlRegisterCleanupHook(serv_calendar_destroy);
2599 /* return our module name for the log */