]> code.citadel.org Git - citadel.git/blob - citadel/serv_calendar.c
* Added support for a "New User Greetings" room. See docs/install.txt
[citadel.git] / citadel / serv_calendar.c
1 /* 
2  * $Id$ 
3  *
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.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <string.h>
16 #ifdef HAVE_STRINGS_H
17 #include <strings.h>
18 #endif
19 #include "serv_calendar.h"
20 #include "citadel.h"
21 #include "server.h"
22 #include "citserver.h"
23 #include "sysdep_decls.h"
24 #include "support.h"
25 #include "config.h"
26 #include "dynloader.h"
27 #include "user_ops.h"
28 #include "room_ops.h"
29 #include "tools.h"
30 #include "msgbase.h"
31 #include "mime_parser.h"
32
33
34 #ifdef HAVE_ICAL_H
35
36 #include <ical.h>
37
38 struct ical_respond_data {
39         char desired_partnum[SIZ];
40         icalcomponent *cal;
41 };
42
43
44 /*
45  * Write a calendar object into the specified user's calendar room.
46  */
47 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
48         char temp[PATH_MAX];
49         FILE *fp;
50         char *ser;
51
52         strcpy(temp, tmpnam(NULL));
53         ser = icalcomponent_as_ical_string(cal);
54         if (ser == NULL) return;
55
56         /* Make a temp file out of it */
57         fp = fopen(temp, "w");
58         if (fp == NULL) return;
59         fwrite(ser, strlen(ser), 1, fp);
60         fclose(fp);
61
62         /* This handy API function does all the work for us.
63          */
64         CtdlWriteObject(USERCALENDARROOM,       /* which room */
65                         "text/calendar",        /* MIME type */
66                         temp,                   /* temp file */
67                         u,                      /* which user */
68                         0,                      /* not binary */
69                         0,              /* don't delete others of this type */
70                         0);                     /* no flags */
71
72         unlink(temp);
73 }
74
75
76 /*
77  * Add a calendar object to the user's calendar
78  */
79 void ical_add(icalcomponent *cal, int recursion_level) {
80         icalcomponent *c;
81
82         /*
83          * The VEVENT subcomponent is the one we're interested in saving.
84          */
85         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
86         
87                 ical_write_to_cal(&CC->usersupp, cal);
88
89         }
90
91         /* If the component has subcomponents, recurse through them. */
92         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
93             (c != 0);
94             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
95                 /* Recursively process subcomponent */
96                 ical_add(c, recursion_level+1);
97         }
98
99 }
100
101
102
103 /*
104  * Send a reply to a meeting invitation.
105  *
106  * 'request' is the invitation to reply to.
107  * 'action' is the string "accept" or "decline".
108  *
109  * (Sorry about this being more than 80 columns ... there was just
110  * no easy way to break it down sensibly.)
111  */
112 void ical_send_a_reply(icalcomponent *request, char *action) {
113         icalcomponent *the_reply = NULL;
114         icalcomponent *vevent = NULL;
115         icalproperty *attendee = NULL;
116         char attendee_string[SIZ];
117         icalproperty *organizer = NULL;
118         char organizer_string[SIZ];
119         icalproperty *summary = NULL;
120         char summary_string[SIZ];
121         icalproperty *me_attend = NULL;
122         struct recptypes *recp = NULL;
123         icalparameter *partstat = NULL;
124         char *serialized_reply = NULL;
125         char *reply_message_text = NULL;
126         struct CtdlMessage *msg = NULL;
127         struct recptypes *valid = NULL;
128
129         strcpy(organizer_string, "");
130         strcpy(summary_string, "Calendar item");
131
132         if (request == NULL) {
133                 lprintf(3, "ERROR: trying to reply to NULL event?\n");
134                 return;
135         }
136
137         the_reply = icalcomponent_new_clone(request);
138         if (the_reply == NULL) {
139                 lprintf(3, "ERROR: cannot clone request\n");
140                 return;
141         }
142
143         /* Change the method from REQUEST to REPLY */
144         icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
145
146         vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
147         if (vevent != NULL) {
148                 /* Hunt for attendees, removing ones that aren't us.
149                  * (Actually, remove them all, cloning our own one so we can
150                  * re-insert it later)
151                  */
152                 while (attendee = icalcomponent_get_first_property(vevent,
153                     ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
154                 ) {
155                         if (icalproperty_get_attendee(attendee)) {
156                                 strcpy(attendee_string,
157                                         icalproperty_get_attendee(attendee) );
158                                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
159                                         strcpy(attendee_string, &attendee_string[7]);
160                                         striplt(attendee_string);
161                                         recp = validate_recipients(attendee_string);
162                                         if (recp != NULL) {
163                                                 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
164                                                         if (me_attend) icalproperty_free(me_attend);
165                                                         me_attend = icalproperty_new_clone(attendee);
166                                                 }
167                                                 phree(recp);
168                                         }
169                                 }
170                         }
171                         /* Remove it... */
172                         icalcomponent_remove_property(vevent, attendee);
173                 }
174
175                 /* We found our own address in the attendee list. */
176                 if (me_attend) {
177                         /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
178                         icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
179
180                         if (!strcasecmp(action, "accept")) {
181                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
182                         }
183                         else if (!strcasecmp(action, "decline")) {
184                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
185                         }
186                         else if (!strcasecmp(action, "tentative")) {
187                                 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
188                         }
189
190                         if (partstat) icalproperty_add_parameter(me_attend, partstat);
191
192                         /* Now insert it back into the vevent. */
193                         icalcomponent_add_property(vevent, me_attend);
194                 }
195
196                 /* Figure out who to send this thing to */
197                 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
198                 if (organizer != NULL) {
199                         if (icalproperty_get_organizer(organizer)) {
200                                 strcpy(organizer_string,
201                                         icalproperty_get_organizer(organizer) );
202                         }
203                 }
204                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
205                         strcpy(organizer_string, &organizer_string[7]);
206                         striplt(organizer_string);
207                 } else {
208                         strcpy(organizer_string, "");
209                 }
210
211                 /* Extract the summary string -- we'll use it as the
212                  * message subject for the reply
213                  */
214                 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
215                 if (summary != NULL) {
216                         if (icalproperty_get_summary(summary)) {
217                                 strcpy(summary_string,
218                                         icalproperty_get_summary(summary) );
219                         }
220                 }
221
222         }
223
224         /* Now generate the reply message and send it out. */
225         serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
226         icalcomponent_free(the_reply);  /* don't need this anymore */
227         if (serialized_reply == NULL) return;
228
229         reply_message_text = mallok(strlen(serialized_reply) + SIZ);
230         if (reply_message_text != NULL) {
231                 sprintf(reply_message_text,
232                         "Content-type: text/calendar\r\n\r\n%s\r\n",
233                         serialized_reply
234                 );
235
236                 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
237                         CC->quickroom.QRname, 0, FMT_RFC822,
238                         "",
239                         summary_string,         /* Use summary for subject */
240                         reply_message_text);
241         
242                 if (msg != NULL) {
243                         valid = validate_recipients(organizer_string);
244                         CtdlSubmitMsg(msg, valid, "");
245                         CtdlFreeMessage(msg);
246                 }
247         }
248         phree(serialized_reply);
249 }
250
251
252
253 /*
254  * Callback function for mime parser that hunts for calendar content types
255  * and turns them into calendar objects
256  */
257 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
258                 void *content, char *cbtype, size_t length, char *encoding,
259                 void *cbuserdata) {
260
261         struct ical_respond_data *ird = NULL;
262
263         ird = (struct ical_respond_data *) cbuserdata;
264         if (ird->cal != NULL) {
265                 icalcomponent_free(ird->cal);
266                 ird->cal = NULL;
267         }
268         if (strcasecmp(partnum, ird->desired_partnum)) return;
269         ird->cal = icalcomponent_new_from_string(content);
270 }
271
272
273 /*
274  * Respond to a meeting request.
275  */
276 void ical_respond(long msgnum, char *partnum, char *action) {
277         struct CtdlMessage *msg;
278         struct ical_respond_data ird;
279
280         if (
281            (strcasecmp(action, "accept"))
282            && (strcasecmp(action, "decline"))
283         ) {
284                 cprintf("%d Action must be 'accept' or 'decline'\n",
285                         ERROR + ILLEGAL_VALUE
286                 );
287                 return;
288         }
289
290         msg = CtdlFetchMessage(msgnum);
291         if (msg == NULL) {
292                 cprintf("%d Message %ld not found.\n",
293                         ERROR+ILLEGAL_VALUE,
294                         (long)msgnum
295                 );
296                 return;
297         }
298
299         memset(&ird, 0, sizeof ird);
300         strcpy(ird.desired_partnum, partnum);
301         mime_parser(msg->cm_fields['M'],
302                 NULL,
303                 *ical_locate_part,              /* callback function */
304                 NULL, NULL,
305                 (void *) &ird,                  /* user data */
306                 0
307         );
308
309         /* We're done with the incoming message, because we now have a
310          * calendar object in memory.
311          */
312         CtdlFreeMessage(msg);
313
314         /*
315          * Here is the real meat of this function.  Handle the event.
316          */
317         if (ird.cal != NULL) {
318                 /* Save this in the user's calendar if necessary */
319                 if (!strcasecmp(action, "accept")) {
320                         ical_add(ird.cal, 0);
321                 }
322
323                 /* Send a reply if necessary */
324                 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
325                         ical_send_a_reply(ird.cal, action);
326                 }
327
328                 /* Now that we've processed this message, we don't need it
329                  * anymore.  So delete it.
330                  */
331                 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
332
333                 /* Free the memory we allocated and return a response. */
334                 icalcomponent_free(ird.cal);
335                 ird.cal = NULL;
336                 cprintf("%d ok\n", CIT_OK);
337                 return;
338         }
339         else {
340                 cprintf("%d No calendar object found\n", ERROR);
341                 return;
342         }
343
344         /* should never get here */
345 }
346
347
348 /*
349  * Search for a property in both the top level and in a VEVENT subcomponent
350  */
351 icalproperty *ical_ctdl_get_subprop(
352                 icalcomponent *cal,
353                 icalproperty_kind which_prop
354 ) {
355         icalproperty *p;
356         icalcomponent *c;
357
358         p = icalcomponent_get_first_property(cal, which_prop);
359         if (p == NULL) {
360                 c = icalcomponent_get_first_component(cal,
361                                                         ICAL_VEVENT_COMPONENT);
362                 if (c != NULL) {
363                         p = icalcomponent_get_first_property(c, which_prop);
364                 }
365         }
366         return p;
367 }
368
369
370 /*
371  * Check to see if two events overlap.  Returns nonzero if they do.
372  */
373 int ical_ctdl_is_overlap(
374                         struct icaltimetype t1start,
375                         struct icaltimetype t1end,
376                         struct icaltimetype t2start,
377                         struct icaltimetype t2end
378 ) {
379
380         if (icaltime_is_null_time(t1start)) return(0);
381         if (icaltime_is_null_time(t2start)) return(0);
382
383         /* First, check for all-day events */
384         if (t1start.is_date) {
385                 if (!icaltime_compare_date_only(t1start, t2start)) {
386                         return(1);
387                 }
388                 if (!icaltime_is_null_time(t2end)) {
389                         if (!icaltime_compare_date_only(t1start, t2end)) {
390                                 return(1);
391                         }
392                 }
393         }
394
395         if (t2start.is_date) {
396                 if (!icaltime_compare_date_only(t2start, t1start)) {
397                         return(1);
398                 }
399                 if (!icaltime_is_null_time(t1end)) {
400                         if (!icaltime_compare_date_only(t2start, t1end)) {
401                                 return(1);
402                         }
403                 }
404         }
405
406         /* Now check for overlaps using date *and* time. */
407
408         /* First, bail out if either event 1 or event 2 is missing end time. */
409         if (icaltime_is_null_time(t1end)) return(0);
410         if (icaltime_is_null_time(t2end)) return(0);
411
412         /* If event 1 ends before event 2 starts, we're in the clear. */
413         if (icaltime_compare(t1end, t2start) <= 0) return(0);
414
415         /* If event 2 ends before event 1 starts, we're also ok. */
416         if (icaltime_compare(t2end, t1start) <= 0) return(0);
417
418         /* Otherwise, they overlap. */
419         return(1);
420 }
421
422
423
424 /*
425  * Backend for ical_hunt_for_conflicts()
426  */
427 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
428         icalcomponent *cal;
429         struct CtdlMessage *msg;
430         struct ical_respond_data ird;
431         struct icaltimetype t1start, t1end, t2start, t2end;
432         icalproperty *p;
433         char conflict_event_uid[SIZ];
434         char conflict_event_summary[SIZ];
435         char compare_uid[SIZ];
436
437         cal = (icalcomponent *)data;
438         strcpy(compare_uid, "");
439         strcpy(conflict_event_uid, "");
440         strcpy(conflict_event_summary, "");
441
442         msg = CtdlFetchMessage(msgnum);
443         if (msg == NULL) return;
444         memset(&ird, 0, sizeof ird);
445         strcpy(ird.desired_partnum, "1");       /* hopefully it's always 1 */
446         mime_parser(msg->cm_fields['M'],
447                 NULL,
448                 *ical_locate_part,              /* callback function */
449                 NULL, NULL,
450                 (void *) &ird,                  /* user data */
451                 0
452         );
453         CtdlFreeMessage(msg);
454
455         if (ird.cal == NULL) return;
456
457         t1start = icaltime_null_time();
458         t1end = icaltime_null_time();
459         t2start = icaltime_null_time();
460         t1end = icaltime_null_time();
461
462         /* Now compare cal to ird.cal */
463         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
464         if (p == NULL) return;
465         if (p != NULL) t2start = icalproperty_get_dtstart(p);
466         
467         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
468         if (p != NULL) t2end = icalproperty_get_dtend(p);
469
470         p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
471         if (p == NULL) return;
472         if (p != NULL) t1start = icalproperty_get_dtstart(p);
473         
474         p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
475         if (p != NULL) t1end = icalproperty_get_dtend(p);
476         
477         p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
478         if (p != NULL) {
479                 strcpy(compare_uid, icalproperty_get_comment(p));
480         }
481
482         p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
483         if (p != NULL) {
484                 strcpy(conflict_event_uid, icalproperty_get_comment(p));
485         }
486
487         p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
488         if (p != NULL) {
489                 strcpy(conflict_event_summary, icalproperty_get_comment(p));
490         }
491
492
493         icalcomponent_free(ird.cal);
494
495         if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
496                 cprintf("%ld||%s|%s|%d|\n",
497                         msgnum,
498                         conflict_event_uid,
499                         conflict_event_summary,
500                         (       ((strlen(compare_uid)>0)
501                                 &&(!strcasecmp(compare_uid,
502                                 conflict_event_uid))) ? 1 : 0
503                         )
504                 );
505         }
506 }
507
508
509
510 /* 
511  * Phase 2 of "hunt for conflicts" operation.
512  * At this point we have a calendar object which represents the VEVENT that
513  * we're considering adding to the calendar.  Now hunt through the user's
514  * calendar room, and output zero or more existing VEVENTs which conflict
515  * with this one.
516  */
517 void ical_hunt_for_conflicts(icalcomponent *cal) {
518         char hold_rm[ROOMNAMELEN];
519
520         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
521
522         if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
523                 getroom(&CC->quickroom, hold_rm);
524                 cprintf("%d You do not have a calendar.\n", ERROR);
525                 return;
526         }
527
528         cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
529
530         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
531                 NULL,
532                 ical_hunt_for_conflicts_backend,
533                 (void *) cal
534         );
535
536         cprintf("000\n");
537         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
538
539 }
540
541
542
543 /*
544  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
545  */
546 void ical_conflicts(long msgnum, char *partnum) {
547         struct CtdlMessage *msg;
548         struct ical_respond_data ird;
549
550         msg = CtdlFetchMessage(msgnum);
551         if (msg == NULL) {
552                 cprintf("%d Message %ld not found.\n",
553                         ERROR+ILLEGAL_VALUE,
554                         (long)msgnum
555                 );
556                 return;
557         }
558
559         memset(&ird, 0, sizeof ird);
560         strcpy(ird.desired_partnum, partnum);
561         mime_parser(msg->cm_fields['M'],
562                 NULL,
563                 *ical_locate_part,              /* callback function */
564                 NULL, NULL,
565                 (void *) &ird,                  /* user data */
566                 0
567         );
568
569         CtdlFreeMessage(msg);
570
571         if (ird.cal != NULL) {
572                 ical_hunt_for_conflicts(ird.cal);
573                 icalcomponent_free(ird.cal);
574                 return;
575         }
576         else {
577                 cprintf("%d No calendar object found\n", ERROR);
578                 return;
579         }
580
581         /* should never get here */
582 }
583
584
585
586
587 /*
588  * All Citadel calendar commands from the client come through here.
589  */
590 void cmd_ical(char *argbuf)
591 {
592         char subcmd[SIZ];
593         long msgnum;
594         char partnum[SIZ];
595         char action[SIZ];
596
597         if (CtdlAccessCheck(ac_logged_in)) return;
598
599         extract(subcmd, argbuf, 0);
600
601         if (!strcmp(subcmd, "test")) {
602                 cprintf("%d This server supports calendaring\n", CIT_OK);
603                 return;
604         }
605
606         else if (!strcmp(subcmd, "respond")) {
607                 msgnum = extract_long(argbuf, 1);
608                 extract(partnum, argbuf, 2);
609                 extract(action, argbuf, 3);
610                 ical_respond(msgnum, partnum, action);
611         }
612
613         else if (!strcmp(subcmd, "conflicts")) {
614                 msgnum = extract_long(argbuf, 1);
615                 extract(partnum, argbuf, 2);
616                 ical_conflicts(msgnum, partnum);
617         }
618
619         else {
620                 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
621                 return;
622         }
623
624         /* should never get here */
625 }
626
627
628
629 /*
630  * We don't know if the calendar room exists so we just create it at login
631  */
632 void ical_create_room(void)
633 {
634         struct quickroom qr;
635         struct visit vbuf;
636
637         /* Create the calendar room if it doesn't already exist */
638         create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
639
640         /* Set expiration policy to manual; otherwise objects will be lost! */
641         if (lgetroom(&qr, USERCALENDARROOM)) {
642                 lprintf(3, "Couldn't get the user calendar room!\n");
643                 return;
644         }
645         qr.QRep.expire_mode = EXPIRE_MANUAL;
646         lputroom(&qr);
647
648         /* Set the view to a calendar view */
649         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
650         vbuf.v_view = 3;        /* 3 = calendar */
651         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
652
653         /* Create the tasks list room if it doesn't already exist */
654         create_room(USERTASKSROOM, 4, "", 0, 1, 0);
655
656         /* Set expiration policy to manual; otherwise objects will be lost! */
657         if (lgetroom(&qr, USERTASKSROOM)) {
658                 lprintf(3, "Couldn't get the user calendar room!\n");
659                 return;
660         }
661         qr.QRep.expire_mode = EXPIRE_MANUAL;
662         lputroom(&qr);
663
664         /* Set the view to a task list view */
665         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
666         vbuf.v_view = 4;        /* 4 = tasks */
667         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
668
669         return;
670 }
671
672
673
674 /*
675  * Back end for ical_obj_beforesave()
676  * This hunts for the UID of the calendar event.
677  */
678 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
679                 char *disp, void *content, char *cbtype, size_t length,
680                 char *encoding, void *cbuserdata)
681 {
682         icalcomponent *cal;
683         icalproperty *p;
684
685         /* If this is a text/calendar object, hunt for the UID and drop it in
686          * the "user data" pointer for the MIME parser.  When
687          * ical_obj_beforesave() sees it there, it'll set the Extended msgid
688          * to that string.
689          */
690         if (!strcasecmp(cbtype, "text/calendar")) {
691                 cal = icalcomponent_new_from_string(content);
692                 if (cal != NULL) {
693                         p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
694                         if (p != NULL) {
695                                 strcpy((char *)cbuserdata,
696                                         icalproperty_get_comment(p)
697                                 );
698                         }
699                         icalcomponent_free(cal);
700                 }
701         }
702 }
703
704
705
706
707
708 /*
709  * See if we need to prevent the object from being saved (we don't allow
710  * MIME types other than text/calendar in the Calendar> room).  Also, when
711  * saving an event to the calendar, set the message's Citadel extended message
712  * ID to the UID of the object.  This causes our replication checker to
713  * automatically delete any existing instances of the same object.  (Isn't
714  * that cool?)
715  */
716 int ical_obj_beforesave(struct CtdlMessage *msg)
717 {
718         char roomname[ROOMNAMELEN];
719         char *p;
720         int a;
721         char eidbuf[SIZ];
722
723         /*
724          * Only messages with content-type text/calendar
725          * may be saved to Calendar>.  If the message is bound for
726          * Calendar> but doesn't have this content-type, throw an error
727          * so that the message may not be posted.
728          */
729
730         /* First determine if this is our room */
731         MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
732         if (strcasecmp(roomname, CC->quickroom.QRname)) {
733                 return 0;       /* It's not the Calendar room. */
734         }
735
736         /* Then determine content-type of the message */
737         
738         /* It must be an RFC822 message! */
739         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
740         if (msg->cm_format_type != 4)
741                 return 1;       /* You tried to save a non-RFC822 message! */
742         
743         /* Find the Content-Type: header */
744         p = msg->cm_fields['M'];
745         a = strlen(p);
746         while (--a > 0) {
747                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
748                         if (!strncasecmp(p + 14, "text/calendar", 13)) {
749                                 strcpy(eidbuf, "");
750                                 mime_parser(msg->cm_fields['M'],
751                                         NULL,
752                                         *ical_ctdl_set_extended_msgid,
753                                         NULL, NULL,
754                                         (void *)eidbuf,
755                                         0
756                                 );
757                                 if (strlen(eidbuf) > 0) {
758                                         if (msg->cm_fields['E'] != NULL) {
759                                                 phree(msg->cm_fields['E']);
760                                         }
761                                         msg->cm_fields['E'] = strdoop(eidbuf);
762                                 }
763                                 return 0;
764                         }
765                         else {
766                                 return 1;
767                         }
768                 }
769                 p++;
770         }
771         
772         /* Oops!  No Content-Type in this message!  How'd that happen? */
773         lprintf(7, "RFC822 message with no Content-Type header!\n");
774         return 1;
775 }
776
777
778 #endif  /* HAVE_ICAL_H */
779
780 /*
781  * Register this module with the Citadel server.
782  */
783 char *Dynamic_Module_Init(void)
784 {
785 #ifdef HAVE_ICAL_H
786         CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
787         CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
788         CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
789 #endif
790         return "$Id$";
791 }