4 * This module implements iCalendar object processing and the Calendar>
5 * room on a Citadel/UX server. It handles iCalendar objects using the
6 * iTIP protocol. See RFCs 2445 and 2446.
10 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
14 #include <sys/types.h>
21 #include "serv_calendar.h"
24 #include "citserver.h"
25 #include "sysdep_decls.h"
28 #include "dynloader.h"
33 #include "mime_parser.h"
39 struct ical_respond_data {
40 char desired_partnum[SIZ];
46 * Write a calendar object into the specified user's calendar room.
48 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
53 strcpy(temp, tmpnam(NULL));
54 ser = icalcomponent_as_ical_string(cal);
55 if (ser == NULL) return;
57 /* Make a temp file out of it */
58 fp = fopen(temp, "w");
59 if (fp == NULL) return;
60 fwrite(ser, strlen(ser), 1, fp);
63 /* This handy API function does all the work for us.
65 CtdlWriteObject(USERCALENDARROOM, /* which room */
66 "text/calendar", /* MIME type */
70 0, /* don't delete others of this type */
78 * Add a calendar object to the user's calendar
80 void ical_add(icalcomponent *cal, int recursion_level) {
84 * The VEVENT subcomponent is the one we're interested in saving.
86 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
88 ical_write_to_cal(&CC->usersupp, cal);
92 /* If the component has subcomponents, recurse through them. */
93 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
95 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
96 /* Recursively process subcomponent */
97 ical_add(c, recursion_level+1);
105 * Send a reply to a meeting invitation.
107 * 'request' is the invitation to reply to.
108 * 'action' is the string "accept" or "decline".
110 * (Sorry about this being more than 80 columns ... there was just
111 * no easy way to break it down sensibly.)
113 void ical_send_a_reply(icalcomponent *request, char *action) {
114 icalcomponent *the_reply = NULL;
115 icalcomponent *vevent = NULL;
116 icalproperty *attendee = NULL;
117 char attendee_string[SIZ];
118 icalproperty *organizer = NULL;
119 char organizer_string[SIZ];
120 icalproperty *summary = NULL;
121 char summary_string[SIZ];
122 icalproperty *me_attend = NULL;
123 struct recptypes *recp = NULL;
124 icalparameter *partstat = NULL;
125 char *serialized_reply = NULL;
126 char *reply_message_text = NULL;
127 struct CtdlMessage *msg = NULL;
128 struct recptypes *valid = NULL;
130 strcpy(organizer_string, "");
131 strcpy(summary_string, "Calendar item");
133 if (request == NULL) {
134 lprintf(3, "ERROR: trying to reply to NULL event?\n");
138 the_reply = icalcomponent_new_clone(request);
139 if (the_reply == NULL) {
140 lprintf(3, "ERROR: cannot clone request\n");
144 /* Change the method from REQUEST to REPLY */
145 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
147 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
148 if (vevent != NULL) {
149 /* Hunt for attendees, removing ones that aren't us.
150 * (Actually, remove them all, cloning our own one so we can
151 * re-insert it later)
153 while (attendee = icalcomponent_get_first_property(vevent,
154 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
156 if (icalproperty_get_attendee(attendee)) {
157 strcpy(attendee_string,
158 icalproperty_get_attendee(attendee) );
159 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
160 strcpy(attendee_string, &attendee_string[7]);
161 striplt(attendee_string);
162 recp = validate_recipients(attendee_string);
164 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
165 if (me_attend) icalproperty_free(me_attend);
166 me_attend = icalproperty_new_clone(attendee);
173 icalcomponent_remove_property(vevent, attendee);
174 icalproperty_free(attendee);
177 /* We found our own address in the attendee list. */
179 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
180 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
182 if (!strcasecmp(action, "accept")) {
183 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
185 else if (!strcasecmp(action, "decline")) {
186 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
188 else if (!strcasecmp(action, "tentative")) {
189 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
192 if (partstat) icalproperty_add_parameter(me_attend, partstat);
194 /* Now insert it back into the vevent. */
195 icalcomponent_add_property(vevent, me_attend);
198 /* Figure out who to send this thing to */
199 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
200 if (organizer != NULL) {
201 if (icalproperty_get_organizer(organizer)) {
202 strcpy(organizer_string,
203 icalproperty_get_organizer(organizer) );
206 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
207 strcpy(organizer_string, &organizer_string[7]);
208 striplt(organizer_string);
210 strcpy(organizer_string, "");
213 /* Extract the summary string -- we'll use it as the
214 * message subject for the reply
216 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
217 if (summary != NULL) {
218 if (icalproperty_get_summary(summary)) {
219 strcpy(summary_string,
220 icalproperty_get_summary(summary) );
226 /* Now generate the reply message and send it out. */
227 serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
228 icalcomponent_free(the_reply); /* don't need this anymore */
229 if (serialized_reply == NULL) return;
231 reply_message_text = mallok(strlen(serialized_reply) + SIZ);
232 if (reply_message_text != NULL) {
233 sprintf(reply_message_text,
234 "Content-type: text/calendar\r\n\r\n%s\r\n",
238 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
239 CC->quickroom.QRname, 0, FMT_RFC822,
241 summary_string, /* Use summary for subject */
245 valid = validate_recipients(organizer_string);
246 CtdlSubmitMsg(msg, valid, "");
247 CtdlFreeMessage(msg);
250 phree(serialized_reply);
256 * Callback function for mime parser that hunts for calendar content types
257 * and turns them into calendar objects
259 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
260 void *content, char *cbtype, size_t length, char *encoding,
263 struct ical_respond_data *ird = NULL;
265 ird = (struct ical_respond_data *) cbuserdata;
266 if (ird->cal != NULL) {
267 icalcomponent_free(ird->cal);
270 if (strcasecmp(partnum, ird->desired_partnum)) return;
271 ird->cal = icalcomponent_new_from_string(content);
276 * Respond to a meeting request.
278 void ical_respond(long msgnum, char *partnum, char *action) {
279 struct CtdlMessage *msg;
280 struct ical_respond_data ird;
283 (strcasecmp(action, "accept"))
284 && (strcasecmp(action, "decline"))
286 cprintf("%d Action must be 'accept' or 'decline'\n",
287 ERROR + ILLEGAL_VALUE
292 msg = CtdlFetchMessage(msgnum);
294 cprintf("%d Message %ld not found.\n",
301 memset(&ird, 0, sizeof ird);
302 strcpy(ird.desired_partnum, partnum);
303 mime_parser(msg->cm_fields['M'],
305 *ical_locate_part, /* callback function */
307 (void *) &ird, /* user data */
311 /* We're done with the incoming message, because we now have a
312 * calendar object in memory.
314 CtdlFreeMessage(msg);
317 * Here is the real meat of this function. Handle the event.
319 if (ird.cal != NULL) {
320 /* Save this in the user's calendar if necessary */
321 if (!strcasecmp(action, "accept")) {
322 ical_add(ird.cal, 0);
325 /* Send a reply if necessary */
326 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
327 ical_send_a_reply(ird.cal, action);
330 /* Now that we've processed this message, we don't need it
331 * anymore. So delete it.
333 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
335 /* Free the memory we allocated and return a response. */
336 icalcomponent_free(ird.cal);
338 cprintf("%d ok\n", CIT_OK);
342 cprintf("%d No calendar object found\n", ERROR);
346 /* should never get here */
351 * Search for a property in both the top level and in a VEVENT subcomponent
353 icalproperty *ical_ctdl_get_subprop(
355 icalproperty_kind which_prop
360 p = icalcomponent_get_first_property(cal, which_prop);
362 c = icalcomponent_get_first_component(cal,
363 ICAL_VEVENT_COMPONENT);
365 p = icalcomponent_get_first_property(c, which_prop);
373 * Check to see if two events overlap. Returns nonzero if they do.
375 int ical_ctdl_is_overlap(
376 struct icaltimetype t1start,
377 struct icaltimetype t1end,
378 struct icaltimetype t2start,
379 struct icaltimetype t2end
382 if (icaltime_is_null_time(t1start)) return(0);
383 if (icaltime_is_null_time(t2start)) return(0);
385 /* First, check for all-day events */
386 if (t1start.is_date) {
387 if (!icaltime_compare_date_only(t1start, t2start)) {
390 if (!icaltime_is_null_time(t2end)) {
391 if (!icaltime_compare_date_only(t1start, t2end)) {
397 if (t2start.is_date) {
398 if (!icaltime_compare_date_only(t2start, t1start)) {
401 if (!icaltime_is_null_time(t1end)) {
402 if (!icaltime_compare_date_only(t2start, t1end)) {
408 /* Now check for overlaps using date *and* time. */
410 /* First, bail out if either event 1 or event 2 is missing end time. */
411 if (icaltime_is_null_time(t1end)) return(0);
412 if (icaltime_is_null_time(t2end)) return(0);
414 /* If event 1 ends before event 2 starts, we're in the clear. */
415 if (icaltime_compare(t1end, t2start) <= 0) return(0);
417 /* If event 2 ends before event 1 starts, we're also ok. */
418 if (icaltime_compare(t2end, t1start) <= 0) return(0);
420 /* Otherwise, they overlap. */
427 * Backend for ical_hunt_for_conflicts()
429 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
431 struct CtdlMessage *msg;
432 struct ical_respond_data ird;
433 struct icaltimetype t1start, t1end, t2start, t2end;
435 char conflict_event_uid[SIZ];
436 char conflict_event_summary[SIZ];
437 char compare_uid[SIZ];
439 cal = (icalcomponent *)data;
440 strcpy(compare_uid, "");
441 strcpy(conflict_event_uid, "");
442 strcpy(conflict_event_summary, "");
444 msg = CtdlFetchMessage(msgnum);
445 if (msg == NULL) return;
446 memset(&ird, 0, sizeof ird);
447 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
448 mime_parser(msg->cm_fields['M'],
450 *ical_locate_part, /* callback function */
452 (void *) &ird, /* user data */
455 CtdlFreeMessage(msg);
457 if (ird.cal == NULL) return;
459 t1start = icaltime_null_time();
460 t1end = icaltime_null_time();
461 t2start = icaltime_null_time();
462 t1end = icaltime_null_time();
464 /* Now compare cal to ird.cal */
465 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
466 if (p == NULL) return;
467 if (p != NULL) t2start = icalproperty_get_dtstart(p);
469 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
470 if (p != NULL) t2end = icalproperty_get_dtend(p);
472 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
473 if (p == NULL) return;
474 if (p != NULL) t1start = icalproperty_get_dtstart(p);
476 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
477 if (p != NULL) t1end = icalproperty_get_dtend(p);
479 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
481 strcpy(compare_uid, icalproperty_get_comment(p));
484 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
486 strcpy(conflict_event_uid, icalproperty_get_comment(p));
489 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
491 strcpy(conflict_event_summary, icalproperty_get_comment(p));
495 icalcomponent_free(ird.cal);
497 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
498 cprintf("%ld||%s|%s|%d|\n",
501 conflict_event_summary,
502 ( ((strlen(compare_uid)>0)
503 &&(!strcasecmp(compare_uid,
504 conflict_event_uid))) ? 1 : 0
513 * Phase 2 of "hunt for conflicts" operation.
514 * At this point we have a calendar object which represents the VEVENT that
515 * we're considering adding to the calendar. Now hunt through the user's
516 * calendar room, and output zero or more existing VEVENTs which conflict
519 void ical_hunt_for_conflicts(icalcomponent *cal) {
520 char hold_rm[ROOMNAMELEN];
522 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
524 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
525 getroom(&CC->quickroom, hold_rm);
526 cprintf("%d You do not have a calendar.\n", ERROR);
530 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
532 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
534 ical_hunt_for_conflicts_backend,
539 getroom(&CC->quickroom, hold_rm); /* return to saved room */
546 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
548 void ical_conflicts(long msgnum, char *partnum) {
549 struct CtdlMessage *msg;
550 struct ical_respond_data ird;
552 msg = CtdlFetchMessage(msgnum);
554 cprintf("%d Message %ld not found.\n",
561 memset(&ird, 0, sizeof ird);
562 strcpy(ird.desired_partnum, partnum);
563 mime_parser(msg->cm_fields['M'],
565 *ical_locate_part, /* callback function */
567 (void *) &ird, /* user data */
571 CtdlFreeMessage(msg);
573 if (ird.cal != NULL) {
574 ical_hunt_for_conflicts(ird.cal);
575 icalcomponent_free(ird.cal);
579 cprintf("%d No calendar object found\n", ERROR);
583 /* should never get here */
590 * All Citadel calendar commands from the client come through here.
592 void cmd_ical(char *argbuf)
599 if (CtdlAccessCheck(ac_logged_in)) return;
601 extract(subcmd, argbuf, 0);
603 if (!strcmp(subcmd, "test")) {
604 cprintf("%d This server supports calendaring\n", CIT_OK);
608 else if (!strcmp(subcmd, "respond")) {
609 msgnum = extract_long(argbuf, 1);
610 extract(partnum, argbuf, 2);
611 extract(action, argbuf, 3);
612 ical_respond(msgnum, partnum, action);
615 else if (!strcmp(subcmd, "conflicts")) {
616 msgnum = extract_long(argbuf, 1);
617 extract(partnum, argbuf, 2);
618 ical_conflicts(msgnum, partnum);
622 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
626 /* should never get here */
632 * We don't know if the calendar room exists so we just create it at login
634 void ical_create_room(void)
639 /* Create the calendar room if it doesn't already exist */
640 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
642 /* Set expiration policy to manual; otherwise objects will be lost! */
643 if (lgetroom(&qr, USERCALENDARROOM)) {
644 lprintf(3, "Couldn't get the user calendar room!\n");
647 qr.QRep.expire_mode = EXPIRE_MANUAL;
650 /* Set the view to a calendar view */
651 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
652 vbuf.v_view = 3; /* 3 = calendar */
653 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
655 /* Create the tasks list room if it doesn't already exist */
656 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
658 /* Set expiration policy to manual; otherwise objects will be lost! */
659 if (lgetroom(&qr, USERTASKSROOM)) {
660 lprintf(3, "Couldn't get the user calendar room!\n");
663 qr.QRep.expire_mode = EXPIRE_MANUAL;
666 /* Set the view to a task list view */
667 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
668 vbuf.v_view = 4; /* 4 = tasks */
669 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
676 * ical_send_out_invitations() is called by ical_saving_vevent() when it
677 * finds a VEVENT. FIXME ... finish implementing.
679 void ical_send_out_invitations(icalcomponent *cal) {
680 icalcomponent *the_request = NULL;
681 char *serialized_request = NULL;
682 char *request_message_text = NULL;
683 struct CtdlMessage *msg = NULL;
684 struct recptypes *valid = NULL;
685 char attendees_string[SIZ];
686 char this_attendee[SIZ];
687 icalproperty *attendee = NULL;
688 char summary_string[SIZ];
689 icalproperty *summary = NULL;
690 icalcomponent *encaps = NULL;
693 lprintf(3, "ERROR: trying to reply to NULL event?\n");
697 /* Clone the event */
698 the_request = icalcomponent_new_clone(cal);
699 if (the_request == NULL) {
700 lprintf(3, "ERROR: cannot clone calendar object\n");
704 /* Extract the summary string -- we'll use it as the
705 * message subject for the request
707 strcpy(summary_string, "Meeting request");
708 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
709 if (summary != NULL) {
710 if (icalproperty_get_summary(summary)) {
711 strcpy(summary_string,
712 icalproperty_get_summary(summary) );
716 /* Determine who the recipients of this message are (the attendees) */
717 strcpy(attendees_string, "");
718 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
719 if (icalproperty_get_attendee(attendee)) {
720 strcpy(this_attendee, icalproperty_get_attendee(attendee) );
721 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
722 strcpy(this_attendee, &this_attendee[7]);
723 snprintf(&attendees_string[strlen(attendees_string)],
724 sizeof(attendees_string) - strlen(attendees_string),
732 lprintf(9, "attendees_string: <%s>\n", attendees_string);
734 /* Encapsulate the VEVENT component into a complete VCALENDAR */
735 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
736 if (encaps == NULL) {
737 lprintf(3, "Error at %s:%d - could not allocate component!\n",
739 icalcomponent_free(the_request);
743 /* Set the Product ID */
744 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
746 /* Set the Version Number */
747 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
749 /* Set the method to REQUEST */
750 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
752 /* FIXME: here we need to insert a VTIMEZONE object. */
754 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
755 * are responsible for "the_request"'s memory -- it will be freed
756 * when we free "encaps".
758 icalcomponent_add_component(encaps, the_request);
761 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
762 icalcomponent_free(encaps); /* Don't need this anymore. */
763 if (serialized_request == NULL) return;
765 request_message_text = mallok(strlen(serialized_request) + SIZ);
766 if (request_message_text != NULL) {
767 sprintf(request_message_text,
768 "Content-type: text/calendar\r\n\r\n%s\r\n",
772 msg = CtdlMakeMessage(&CC->usersupp,
773 "", /* No single recipient here */
774 CC->quickroom.QRname, 0, FMT_RFC822,
776 summary_string, /* Use summary for subject */
777 request_message_text);
780 valid = validate_recipients(attendees_string);
781 CtdlSubmitMsg(msg, valid, "");
782 CtdlFreeMessage(msg);
785 phree(serialized_request);
790 * When a calendar object is being saved, determine whether it's a VEVENT
791 * and the user saving it is the organizer. If so, send out invitations
792 * to any listed attendees.
795 void ical_saving_vevent(icalcomponent *cal) {
797 icalproperty *organizer = NULL;
798 char organizer_string[SIZ];
800 strcpy(organizer_string, "");
802 * The VEVENT subcomponent is the one we're interested in.
803 * Send out invitations if, and only if, this user is the Organizer.
805 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
806 organizer = icalcomponent_get_first_property(cal,
807 ICAL_ORGANIZER_PROPERTY);
808 if (organizer != NULL) {
809 if (icalproperty_get_organizer(organizer)) {
810 strcpy(organizer_string,
811 icalproperty_get_organizer(organizer));
814 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
815 strcpy(organizer_string, &organizer_string[7]);
816 striplt(organizer_string);
818 * If the user saving the event is listed as the
819 * organizer, then send out invitations.
821 if (CtdlIsMe(organizer_string)) {
822 ical_send_out_invitations(cal);
827 /* If the component has subcomponents, recurse through them. */
828 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
830 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
831 /* Recursively process subcomponent */
832 ical_saving_vevent(c);
840 * Back end for ical_obj_beforesave()
841 * This hunts for the UID of the calendar event.
843 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
844 char *disp, void *content, char *cbtype, size_t length,
845 char *encoding, void *cbuserdata)
850 /* If this is a text/calendar object, hunt for the UID and drop it in
851 * the "user data" pointer for the MIME parser. When
852 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
855 if (!strcasecmp(cbtype, "text/calendar")) {
856 cal = icalcomponent_new_from_string(content);
858 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
860 strcpy((char *)cbuserdata,
861 icalproperty_get_comment(p)
864 icalcomponent_free(cal);
874 * See if we need to prevent the object from being saved (we don't allow
875 * MIME types other than text/calendar in the Calendar> room). Also, when
876 * saving an event to the calendar, set the message's Citadel extended message
877 * ID to the UID of the object. This causes our replication checker to
878 * automatically delete any existing instances of the same object. (Isn't
881 int ical_obj_beforesave(struct CtdlMessage *msg)
883 char roomname[ROOMNAMELEN];
889 * Only messages with content-type text/calendar
890 * may be saved to Calendar>. If the message is bound for
891 * Calendar> but doesn't have this content-type, throw an error
892 * so that the message may not be posted.
895 /* First determine if this is our room */
896 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
897 if (strcasecmp(roomname, CC->quickroom.QRname)) {
898 return 0; /* It's not the Calendar room. */
901 /* Then determine content-type of the message */
903 /* It must be an RFC822 message! */
904 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
905 if (msg->cm_format_type != 4)
906 return 1; /* You tried to save a non-RFC822 message! */
908 /* Find the Content-Type: header */
909 p = msg->cm_fields['M'];
912 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
913 if (!strncasecmp(p + 14, "text/calendar", 13)) {
915 mime_parser(msg->cm_fields['M'],
917 *ical_ctdl_set_extended_msgid,
922 if (strlen(eidbuf) > 0) {
923 if (msg->cm_fields['E'] != NULL) {
924 phree(msg->cm_fields['E']);
926 msg->cm_fields['E'] = strdoop(eidbuf);
937 /* Oops! No Content-Type in this message! How'd that happen? */
938 lprintf(7, "RFC822 message with no Content-Type header!\n");
944 * Things we need to do after saving a calendar event.
946 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
947 char *disp, void *content, char *cbtype, size_t length,
948 char *encoding, void *cbuserdata)
952 /* If this is a text/calendar object, hunt for the UID and drop it in
953 * the "user data" pointer for the MIME parser. When
954 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
957 if (!strcasecmp(cbtype, "text/calendar")) {
958 cal = icalcomponent_new_from_string(content);
960 ical_saving_vevent(cal);
961 icalcomponent_free(cal);
968 * Things we need to do after saving a calendar event.
970 int ical_obj_aftersave(struct CtdlMessage *msg)
972 char roomname[ROOMNAMELEN];
977 * If this isn't the Calendar> room, no further action is necessary.
980 /* First determine if this is our room */
981 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
982 if (strcasecmp(roomname, CC->quickroom.QRname)) {
983 return 0; /* It's not the Calendar room. */
986 /* Then determine content-type of the message */
988 /* It must be an RFC822 message! */
989 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
990 if (msg->cm_format_type != 4) return(1);
992 /* Find the Content-Type: header */
993 p = msg->cm_fields['M'];
996 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
997 if (!strncasecmp(p + 14, "text/calendar", 13)) {
998 mime_parser(msg->cm_fields['M'],
1000 *ical_obj_aftersave_backend,
1014 /* Oops! No Content-Type in this message! How'd that happen? */
1015 lprintf(7, "RFC822 message with no Content-Type header!\n");
1020 #endif /* HAVE_ICAL_H */
1023 * Register this module with the Citadel server.
1025 char *Dynamic_Module_Init(void)
1028 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1029 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1030 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1031 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");