Remove $Id$ tags from most of webcit
[citadel.git] / webcit / calendar.c
1 /*
2  * Functions which handle calendar objects and their processing/display.
3  *
4  * Copyright (c) 1996-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "calendar.h"
24
25 /*
26  * Process a calendar object.  At this point it's already been deserialized by cal_process_attachment()
27  *
28  * cal:                 the calendar object
29  * recursion_level:     Number of times we've recursed into this function
30  * msgnum:              Message number on the Citadel server
31  * cal_partnum:         MIME part number within that message containing the calendar object
32  */
33 void cal_process_object(StrBuf *Target,
34                         icalcomponent *cal,
35                         int recursion_level,
36                         long msgnum,
37                         const char *cal_partnum) 
38 {
39         icalcomponent *c;
40         icalproperty *method = NULL;
41         icalproperty_method the_method = ICAL_METHOD_NONE;
42         icalproperty *p;
43         struct icaltimetype t;
44         time_t tt;
45         char buf[256];
46         char conflict_name[256];
47         char conflict_message[256];
48         int is_update = 0;
49         char divname[32];
50         static int divcount = 0;
51
52         sprintf(divname, "rsvp%04x", ++divcount);
53
54         /* Convert timezones to something easy to display.
55          * It's safe to do this in memory because we're only changing it on the
56          * display side -- when we tell the server to do something with the object,
57          * the server will be working with its original copy in the database.
58          */
59         if ((cal) && (recursion_level == 0)) {
60                 ical_dezonify(cal);
61         }
62
63         /* Leading HTML for the display of this object */
64         if (recursion_level == 0) {
65                 StrBufAppendPrintf(Target, "<div class=\"mimepart\">\n");
66         }
67
68         /* Look for a method */
69         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
70
71         /* See what we need to do with this */
72         if (method != NULL) {
73                 char *title;
74                 the_method = icalproperty_get_method(method);
75
76                 StrBufAppendPrintf(Target, "<div id=\"%s_title\">", divname);
77                 StrBufAppendPrintf(Target, "<img src=\"static/calarea_48x.gif\">");
78                 StrBufAppendPrintf(Target, "<span>");
79                 switch(the_method) {
80                 case ICAL_METHOD_REQUEST:
81                         title = _("Meeting invitation");
82                         break;
83                 case ICAL_METHOD_REPLY:
84                         title = _("Attendee's reply to your invitation");
85                         break;
86                 case ICAL_METHOD_PUBLISH:
87                         title = _("Published event");
88                         break;
89                 default:
90                         title = _("This is an unknown type of calendar item.");
91                         break;
92                 }
93                 StrBufAppendPrintf(Target, "</span>");
94
95                 StrBufAppendPrintf(Target, "&nbsp;&nbsp;%s",title);
96                 StrBufAppendPrintf(Target, "</div>");
97         }
98
99         StrBufAppendPrintf(Target, "<dl>");
100         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
101         if (p != NULL) {
102                 StrBufAppendPrintf(Target, "<dt>");
103                 StrBufAppendPrintf(Target, _("Summary:"));
104                 StrBufAppendPrintf(Target, "</dt><dd>");
105                 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
106                 StrBufAppendPrintf(Target, "</dd>\n");
107         }
108
109         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
110         if (p != NULL) {
111                 StrBufAppendPrintf(Target, "<dt>");
112                 StrBufAppendPrintf(Target, _("Location:"));
113                 StrBufAppendPrintf(Target, "</dt><dd>");
114                 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
115                 StrBufAppendPrintf(Target, "</dd>\n");
116         }
117
118         /*
119          * Only show start/end times if we're actually looking at the VEVENT
120          * component.  Otherwise it shows bogus dates for things like timezone.
121          */
122         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
123
124                 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
125                 if (p != NULL) {
126                         t = icalproperty_get_dtstart(p);
127
128                         if (t.is_date) {
129                                 struct tm d_tm;
130                                 char d_str[32];
131                                 memset(&d_tm, 0, sizeof d_tm);
132                                 d_tm.tm_year = t.year - 1900;
133                                 d_tm.tm_mon = t.month - 1;
134                                 d_tm.tm_mday = t.day;
135                                 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
136                                 StrBufAppendPrintf(Target, "<dt>");
137                                 StrBufAppendPrintf(Target, _("Date:"));
138                                 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", d_str);
139                         }
140                         else {
141                                 tt = icaltime_as_timet(t);
142                                 webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
143                                 StrBufAppendPrintf(Target, "<dt>");
144                                 StrBufAppendPrintf(Target, _("Starting date/time:"));
145                                 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
146                         }
147                 }
148         
149                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
150                 if (p != NULL) {
151                         t = icalproperty_get_dtend(p);
152                         tt = icaltime_as_timet(t);
153                         webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
154                         StrBufAppendPrintf(Target, "<dt>");
155                         StrBufAppendPrintf(Target, _("Ending date/time:"));
156                         StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
157                 }
158
159         }
160
161         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
162         if (p != NULL) {
163                 StrBufAppendPrintf(Target, "<dt>");
164                 StrBufAppendPrintf(Target, _("Description:"));
165                 StrBufAppendPrintf(Target, "</dt><dd>");
166                 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
167                 StrBufAppendPrintf(Target, "</dd>\n");
168         }
169
170         if (icalcomponent_get_first_property(cal, ICAL_RRULE_PROPERTY)) {
171                 /* Unusual string syntax used here in order to re-use existing translations */
172                 StrBufAppendPrintf(Target, "<dt>%s:</dt><dd>%s.</dd>\n",
173                         _("Recurrence"),
174                         _("This is a recurring event")
175                 );
176         }
177
178         /* If the component has attendees, iterate through them. */
179         for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); 
180              (p != NULL); 
181              p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
182                 StrBufAppendPrintf(Target, "<dt>");
183                 StrBufAppendPrintf(Target, _("Attendee:"));
184                 StrBufAppendPrintf(Target, "</dt><dd>");
185                 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
186                 if (!strncasecmp(buf, "MAILTO:", 7)) {
187
188                         /** screen name or email address */
189                         strcpy(buf, &buf[7]);
190                         striplt(buf);
191                         StrEscAppend(Target, NULL, buf, 0, 0);
192                         StrBufAppendPrintf(Target, " ");
193
194                         /** participant status */
195                         partstat_as_string(buf, p);
196                         StrEscAppend(Target, NULL, buf, 0, 0);
197                 }
198                 StrBufAppendPrintf(Target, "</dd>\n");
199         }
200
201         /* If the component has subcomponents, recurse through them. */
202         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
203              (c != 0);
204              c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
205                 /* Recursively process subcomponent */
206                 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
207         }
208
209         /* If this is a REQUEST, display conflicts and buttons */
210         if (the_method == ICAL_METHOD_REQUEST) {
211
212                 /* Check for conflicts */
213                 lprintf(9, "Checking server calendar for conflicts...\n");
214                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
215                 serv_getln(buf, sizeof buf);
216                 if (buf[0] == '1') {
217                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
218                                 extract_token(conflict_name, buf, 3, '|', sizeof conflict_name);
219                                 is_update = extract_int(buf, 4);
220
221                                 if (is_update) {
222                                         snprintf(conflict_message, sizeof conflict_message,
223                                                  _("This is an update of '%s' which is already in your calendar."), conflict_name);
224                                 }
225                                 else {
226                                         snprintf(conflict_message, sizeof conflict_message,
227                                                  _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
228                                 }
229
230                                 StrBufAppendPrintf(Target, "<dt>%s",
231                                         (is_update ?
232                                          _("Update:") :
233                                          _("CONFLICT:")
234                                                 )
235                                         );
236                                 StrBufAppendPrintf(Target, "</dt><dd>");
237                                 StrEscAppend(Target, NULL, conflict_message, 0, 0);
238                                 StrBufAppendPrintf(Target, "</dd>\n");
239                         }
240                 }
241                 lprintf(9, "...done.\n");
242
243                 StrBufAppendPrintf(Target, "</dl>");
244
245                 /* Display the Accept/Decline buttons */
246                 StrBufAppendPrintf(Target, "<p id=\"%s_question\">"
247                         "%s "
248                         "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
249                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
250                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
251                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
252                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
253                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
254                         "</span></p>\n",
255                         divname,
256                         _("How would you like to respond to this invitation?"),
257                         divname, divname, msgnum, cal_partnum, _("Accept"),
258                         divname, divname, msgnum, cal_partnum, _("Tentative"),
259                         divname, divname, msgnum, cal_partnum, _("Decline")
260                         );
261
262         }
263
264         /* If this is a REPLY, display update button */
265         if (the_method == ICAL_METHOD_REPLY) {
266
267                 /* Display the update buttons */
268                 StrBufAppendPrintf(Target, "<p id=\"%s_question\" >"
269                         "%s "
270                         "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
271                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
272                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
273                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
274                         "</span></p>\n",
275                         divname,
276                         _("Click <i>Update</i> to accept this reply and update your calendar."),
277                         divname, divname, msgnum, cal_partnum, _("Update"),
278                         divname, divname, msgnum, cal_partnum, _("Ignore")
279                         );
280         
281         }
282         
283         /* Trailing HTML for the display of this object */
284         if (recursion_level == 0) {
285                 StrBufAppendPrintf(Target, "<p>&nbsp;</p></div>\n");
286         }
287 }
288
289
290 /*
291  * Deserialize a calendar object in a message so it can be displayed.
292  */
293 void cal_process_attachment(wc_mime_attachment *Mime) 
294 {
295         icalcomponent *cal;
296
297         cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
298         FlushStrBuf(Mime->Data);
299         if (cal == NULL) {
300                 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
301                 StrBufAppendPrintf(Mime->Data, "<br />\n");
302                 return;
303         }
304
305         cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum));
306
307         /* Free the memory we obtained from libical's constructor */
308         icalcomponent_free(cal);
309 }
310
311
312
313
314 /*
315  * Respond to a meeting request - accept/decline meeting
316  */
317 void respond_to_request(void) 
318 {
319         char buf[1024];
320
321         begin_ajax_response();
322
323         serv_printf("ICAL respond|%s|%s|%s|",
324                 bstr("msgnum"),
325                 bstr("cal_partnum"),
326                 bstr("sc")
327         );
328         serv_getln(buf, sizeof buf);
329
330         if (buf[0] == '2') {
331                 wc_printf("<img src=\"static/calarea_48x.gif\"><span>");
332                 if (!strcasecmp(bstr("sc"), "accept")) {
333                         wc_printf(_("You have accepted this meeting invitation.  "
334                                 "It has been entered into your calendar.")
335                         );
336                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
337                         wc_printf(_("You have tentatively accepted this meeting invitation.  "
338                                 "It has been 'pencilled in' to your calendar.")
339                         );
340                 } else if (!strcasecmp(bstr("sc"), "decline")) {
341                         wc_printf(_("You have declined this meeting invitation.  "
342                                   "It has <b>not</b> been entered into your calendar.")
343                                 );
344                 }
345                 wc_printf(" ");
346                 wc_printf(_("A reply has been sent to the meeting organizer."));
347                 wc_printf("</span>");
348         } else {
349                 wc_printf("<img align=\"center\" src=\"static/error.gif\"><span>");
350                 wc_printf("%s\n", &buf[4]);
351                 wc_printf("</span>");
352         }
353
354         end_ajax_response();
355 }
356
357
358
359 /*
360  * Handle an incoming RSVP
361  */
362 void handle_rsvp(void) 
363 {
364         char buf[1024];
365
366         begin_ajax_response();
367
368         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
369                 bstr("msgnum"),
370                 bstr("cal_partnum"),
371                 bstr("sc")
372         );
373         serv_getln(buf, sizeof buf);
374
375         if (buf[0] == '2') {
376                 wc_printf("<img src=\"static/calarea_48x.gif\"><span>");
377                 if (!strcasecmp(bstr("sc"), "update")) {
378                         wc_printf(_("Your calendar has been updated to reflect this RSVP."));
379                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
380                         wc_printf(_("You have chosen to ignore this RSVP. "
381                                   "Your calendar has <b>not</b> been updated.")
382                                 );
383                 }
384                 wc_printf("</span>");
385         } else {
386                 wc_printf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
387                 wc_printf("</span>");
388         }
389
390         end_ajax_response();
391 }
392
393
394
395
396 /*
397  * free memory allocated using libical
398  */
399 void delete_cal(void *vCal)
400 {
401         disp_cal *Cal = (disp_cal*) vCal;
402         icalcomponent_free(Cal->cal);
403         free(Cal->from);
404         free(Cal);
405 }
406
407 /*
408  * This is the meat-and-bones of the first part of our two-phase calendar display.
409  * As we encounter calendar items in messages being read from the server, we break out
410  * any iCalendar objects and store them in a hash table.  Later on, the second phase will
411  * use this hash table to render the calendar for display.
412  */
413 void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unread, calview *calv)
414 {
415         icalproperty *ps = NULL;
416         struct icaltimetype dtstart, dtend;
417         struct icaldurationtype dur;
418         wcsession *WCC = WC;
419         disp_cal *Cal;
420         size_t len;
421         time_t final_recurrence = 0;
422         icalcomponent *cptr = NULL;
423
424         /* recur variables */
425         icalproperty *rrule = NULL;
426         struct icalrecurrencetype recur;
427         icalrecur_iterator *ritr = NULL;
428         struct icaltimetype next;
429         int num_recur = 0;
430         int stop_rr = 0;
431
432         dtstart = icaltime_null_time();
433         dtend = icaltime_null_time();
434         
435         if (WCC->disp_cal_items == NULL)
436                 WCC->disp_cal_items = NewHash(0, Flathash);
437
438         /* Note: anything we do here, we also have to do below for the recurrences. */
439         Cal = (disp_cal*) malloc(sizeof(disp_cal));
440         memset(Cal, 0, sizeof(disp_cal));
441         Cal->cal = icalcomponent_new_clone(cal);
442
443         /* Dezonify and decapsulate at the very last moment */
444         /* lprintf(9, "INITIAL: %s\n", icaltime_as_ical_string(icalproperty_get_dtstart(
445                 icalcomponent_get_first_property(icalcomponent_get_first_component(
446                 Cal->cal, ICAL_VEVENT_COMPONENT), ICAL_DTSTART_PROPERTY)))
447         ); */
448         ical_dezonify(Cal->cal);
449         if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
450                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
451                 if (cptr) {
452                         cptr = icalcomponent_new_clone(cptr);
453                         icalcomponent_free(Cal->cal);
454                         Cal->cal = cptr;
455                 }
456         }
457
458         Cal->unread = unread;
459         len = strlen(from);
460         Cal->from = (char*)malloc(len+ 1);
461         memcpy(Cal->from, from, len + 1);
462         Cal->cal_msgnum = msgnum;
463
464         /* Precalculate the starting date and time of this event, and store it in our top-level
465          * structure.  Later, when we are rendering the calendar, we can just peek at these values
466          * without having to break apart every calendar item.
467          */
468         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
469         if (ps != NULL) {
470                 dtstart = icalproperty_get_dtstart(ps);
471                 Cal->event_start = icaltime_as_timet(dtstart);
472         }
473
474         /* Do the same for the ending date and time.  It makes the day view much easier to render. */
475         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
476         if (ps != NULL) {
477                 dtend = icalproperty_get_dtend(ps);
478                 Cal->event_end = icaltime_as_timet(dtend);
479         }
480
481         /* Store it in the hash list. */
482         Put(WCC->disp_cal_items, 
483             (char*) &Cal->event_start,
484             sizeof(Cal->event_start), 
485             Cal, 
486             delete_cal);
487
488         /****************************** handle recurring events ******************************/
489
490         if (icaltime_is_null_time(dtstart)) return;     /* Can't recur without a start time */
491
492         if (!icaltime_is_null_time(dtend)) {            /* Need duration for recurrences */
493                 dur = icaltime_subtract(dtend, dtstart);
494         }
495
496         /*
497          * Just let libical iterate the recurrence, and keep looping back to the top of this function,
498          * adding new hash entries that all point back to the same msgnum, until either the iteration
499          * stops or some outer bound is reached.  The display code will automatically do the Right Thing.
500          */
501         cptr = cal;
502         if (icalcomponent_isa(cptr) != ICAL_VEVENT_COMPONENT) {
503                 cptr = icalcomponent_get_first_component(cptr, ICAL_VEVENT_COMPONENT);
504         }
505         if (!cptr) return;
506         ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
507         if (ps == NULL) return;
508         dtstart = icalproperty_get_dtstart(ps);
509         rrule = icalcomponent_get_first_property(cptr, ICAL_RRULE_PROPERTY);
510         if (!rrule) return;
511         recur = icalproperty_get_rrule(rrule);
512         ritr = icalrecur_iterator_new(recur, dtstart);
513         if (!ritr) return;
514
515         while (next = icalrecur_iterator_next(ritr), ((!icaltime_is_null_time(next))&&(!stop_rr)) ) {
516                 ++num_recur;
517                 if (num_recur > 1) {            /* Skip the first one.  We already did it at the root. */
518                         icalcomponent *cptr;
519                         /* lprintf(9, "REPEATS: %s\n", icaltime_as_ical_string(next)); */
520
521                         /* Note: anything we do here, we also have to do above for the root event. */
522                         Cal = (disp_cal*) malloc(sizeof(disp_cal));
523                         memset(Cal, 0, sizeof(disp_cal));
524                         Cal->cal = icalcomponent_new_clone(cal);
525                         Cal->unread = unread;
526                         len = strlen(from);
527                         Cal->from = (char*)malloc(len+ 1);
528                         memcpy(Cal->from, from, len + 1);
529                         Cal->cal_msgnum = msgnum;
530
531                         if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
532                                 cptr = Cal->cal;
533                         }
534                         else {
535                                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
536                         }
537                         if (cptr) {
538                                 ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
539                                 if (ps != NULL) {
540                                         icalcomponent_remove_property(cptr, ps);
541                                         ps = icalproperty_new_dtstart(next);
542                                         icalcomponent_add_property(cptr, ps);
543         
544                                         Cal->event_start = icaltime_as_timet(next);
545                                         final_recurrence = Cal->event_start;
546                                 }
547
548                                 ps = icalcomponent_get_first_property(cptr, ICAL_DTEND_PROPERTY);
549                                 if (ps != NULL) {
550                                         icalcomponent_remove_property(cptr, ps);
551         
552                                         /* Make a new dtend */
553                                         ps = icalproperty_new_dtend(icaltime_add(next, dur));
554                 
555                                         /* and stick it somewhere */
556                                         icalcomponent_add_property(cptr, ps);
557                                 }
558
559                         }
560
561                         /* Dezonify and decapsulate at the very last moment */
562                         ical_dezonify(Cal->cal);
563                         if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
564                                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
565                                 if (cptr) {
566                                         cptr = icalcomponent_new_clone(cptr);
567                                         icalcomponent_free(Cal->cal);
568                                         Cal->cal = cptr;
569                                 }
570                         }
571
572                         if ( (Cal->event_start > calv->lower_bound)
573                            && (Cal->event_start < calv->upper_bound) ) {
574                                 Put(WCC->disp_cal_items, 
575                                         (char*) &Cal->event_start,
576                                         sizeof(Cal->event_start), 
577                                         Cal, 
578                                         delete_cal
579                                 );
580                         }
581                         else {
582                                 delete_cal(Cal);
583                         }
584
585                         /* If an upper bound is set, stop when we go out of scope */
586                         if (final_recurrence > calv->upper_bound) stop_rr = 1;
587                 }
588         }
589         icalrecur_iterator_free(ritr);
590         /* lprintf(9, "Performed %d recurrences; final one is %s", num_recur, ctime(&final_recurrence)); */
591
592 }
593
594
595
596
597
598
599 void process_ical_object(long msgnum, int unread,
600                          char *from, 
601                          char *FlatIcal, 
602                          icalcomponent_kind which_kind,
603                          IcalCallbackFunc CallBack,
604                          calview *calv
605         ) 
606 {
607         icalcomponent *cal, *c;
608
609         cal = icalcomponent_new_from_string(FlatIcal);
610         if (cal != NULL) {
611
612                 /* A which_kind of (-1) means just load the whole thing */
613                 if (which_kind == (-1)) {
614                         CallBack(cal, msgnum, from, unread, calv);
615                 }
616                 
617                 /* Otherwise recurse and hunt */
618                 else {
619                         
620                         /* Simple components of desired type */
621                         if (icalcomponent_isa(cal) == which_kind) {
622                                 CallBack(cal, msgnum, from, unread, calv);
623                         }
624                         
625                         /* Subcomponents of desired type */
626                         for (c = icalcomponent_get_first_component(cal, which_kind);
627                              (c != 0);
628                              c = icalcomponent_get_next_component(cal, which_kind)) {
629                                 CallBack(c, msgnum, from, unread, calv);
630                         }
631                         
632                 }
633                 
634                 icalcomponent_free(cal);
635         }
636 }
637
638 /*
639  * Code common to all icalendar display handlers.  Given a message number and a MIME
640  * type, we load the message and hunt for that MIME type.  If found, we load
641  * the relevant part, deserialize it into a libical component, filter it for
642  * the requested object type, and feed it to the specified handler.
643  */
644 void load_ical_object(long msgnum, int unread,
645                       icalcomponent_kind which_kind,
646                       IcalCallbackFunc CallBack,
647                       calview *calv,
648                       int RenderAsync
649         ) 
650 {
651         StrBuf *Buf;
652         StrBuf *Data = NULL;
653         const char *bptr;
654         int Done = 0;
655         char from[128] = "";
656         char mime_partnum[256];
657         char mime_filename[256];
658         char mime_content_type[256];
659         char mime_disposition[256];
660         int mime_length;
661         char relevant_partnum[256];
662         char *relevant_source = NULL;
663         int phase = 0;                          /* 0 = citadel headers, 1 = mime headers, 2 = body */
664         char msg4_content_type[256] = "";
665         char msg4_content_encoding[256] = "";
666         int msg4_content_length = 0;
667
668         relevant_partnum[0] = '\0';
669         serv_printf("MSG4 %ld", msgnum);        /* we need the mime headers */
670         Buf = NewStrBuf();
671         StrBuf_ServGetln(Buf);
672         if (GetServerStatus(Buf, NULL) != 1) {
673                 FreeStrBuf (&Buf);
674                 return;
675         }
676         while (!Done && (StrBuf_ServGetln(Buf)>=0)) {
677                 if ( (StrLength(Buf)==3) && 
678                      !strcmp(ChrPtr(Buf), "000")) {
679                         Done = 1;
680                         break;
681                 }
682                 bptr = ChrPtr(Buf);
683                 switch (phase) {
684                 case 0:
685                         if (!strncasecmp(bptr, "part=", 5)) {
686                                 extract_token(mime_filename, &bptr[5], 1, '|', sizeof mime_filename);
687                                 extract_token(mime_partnum, &bptr[5], 2, '|', sizeof mime_partnum);
688                                 extract_token(mime_disposition, &bptr[5], 3, '|', sizeof mime_disposition);
689                                 extract_token(mime_content_type, &bptr[5], 4, '|', sizeof mime_content_type);
690                                 mime_length = extract_int(&bptr[5], 5);
691
692                                 if (  (!strcasecmp(mime_content_type, "text/calendar"))
693                                       || (!strcasecmp(mime_content_type, "application/ics"))
694                                       || (!strcasecmp(mime_content_type, "text/vtodo"))
695                                       || (!strcasecmp(mime_content_type, "text/todo"))
696                                         ) {
697                                         strcpy(relevant_partnum, mime_partnum);
698                                 }
699                         }
700                         else if (!strncasecmp(bptr, "from=", 4)) {
701                                 extract_token(from, bptr, 1, '=', sizeof(from));
702                         }
703                         else if ((phase == 0) && (!strncasecmp(bptr, "text", 4))) {
704                                 phase = 1;
705                         }
706                 break;
707                 case 1:
708                         if (!IsEmptyStr(bptr)) {
709                                 if (!strncasecmp(bptr, "Content-type: ", 14)) {
710                                         safestrncpy(msg4_content_type, &bptr[14], sizeof msg4_content_type);
711                                         striplt(msg4_content_type);
712                                 }
713                                 else if (!strncasecmp(bptr, "Content-transfer-encoding: ", 27)) {
714                                         safestrncpy(msg4_content_encoding, &bptr[27], sizeof msg4_content_encoding);
715                                         striplt(msg4_content_type);
716                                 }
717                                 else if ((!strncasecmp(bptr, "Content-length: ", 16))) {
718                                         msg4_content_length = atoi(&bptr[16]);
719                                 }
720                                 break;
721                         }
722                         else {
723                                 phase++;
724                                 
725                                 if ((msg4_content_length > 0)
726                                     && ( !strcasecmp(msg4_content_encoding, "7bit"))
727                                     && ((!strcasecmp(mime_content_type, "text/calendar"))
728                                         || (!strcasecmp(mime_content_type, "application/ics"))
729                                         || (!strcasecmp(mime_content_type, "text/vtodo"))
730                                         || (!strcasecmp(mime_content_type, "text/todo"))
731                                             )
732                                         ) 
733                                 {
734                                 }
735                         }
736                 case 2:
737                         if (Data == NULL)
738                                 Data = NewStrBufPlain(NULL, msg4_content_length * 2);
739                         if (msg4_content_length > 0) {
740                                 StrBuf_ServGetBLOBBuffered(Data, msg4_content_length);
741                                 phase ++;
742                         }
743                         else {
744                                 StrBufAppendBuf(Data, Buf, 0);
745                                 StrBufAppendBufPlain(Data, "\r\n", 1, 0);
746                         }
747                 case 3:
748                         StrBufAppendBuf(Data, Buf, 0);
749                 }
750         }
751         FreeStrBuf(&Buf);
752
753         /* If MSG4 didn't give us the part we wanted, but we know that we can find it
754          * as one of the other MIME parts, attempt to load it now.
755          */
756         if ((Data == NULL) && (!IsEmptyStr(relevant_partnum))) {
757                 Data = load_mimepart(msgnum, relevant_partnum);
758         }
759
760         if (Data != NULL) {
761                 relevant_source = (char*) ChrPtr(Data);
762                 process_ical_object(msgnum, unread,
763                                     from, 
764                                     relevant_source, 
765                                     which_kind,
766                                     CallBack,
767                                     calv);
768         }
769         FreeStrBuf (&Data);
770
771         icalmemory_free_ring();
772 }
773
774 /*
775  * Display a calendar item
776  */
777 int calendar_LoadMsgFromServer(SharedMessageStatus *Stat, 
778                                void **ViewSpecific, 
779                                message_summary* Msg, 
780                                int is_new, 
781                                int i)
782 {
783         calview *c = (calview*) *ViewSpecific;
784         load_ical_object(Msg->msgnum, is_new, (-1), display_individual_cal, c, 1);
785         return 0;
786 }
787
788 /*
789  * display the editor component for an event
790  */
791 void display_edit_event(void) {
792         long msgnum = 0L;
793
794         msgnum = lbstr("msgnum");
795         if (msgnum > 0L) {
796                 /* existing event */
797                 load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, display_edit_individual_event, NULL, 0);
798         }
799         else {
800                 /* new event */
801                 display_edit_individual_event(NULL, 0L, "", 0, NULL);
802         }
803 }
804
805 /*
806  * save an edited event
807  */
808 void save_event(void) {
809         long msgnum = 0L;
810
811         msgnum = lbstr("msgnum");
812
813         if (msgnum > 0L) {
814                 load_ical_object(msgnum, 0, (-1), save_individual_event, NULL, 0);
815         }
816         else {
817                 save_individual_event(NULL, 0L, "", 0, NULL);
818         }
819 }
820
821
822
823
824
825 /*
826  * Anonymous request of freebusy data for a user
827  */
828 void do_freebusy(void)
829 {
830         const char *req = ChrPtr(WC->Hdr->HR.ReqLine);
831         char who[SIZ];
832         char buf[SIZ];
833         int len;
834         long lines;
835
836         extract_token(who, req, 0, ' ', sizeof who);
837         if (!strncasecmp(who, "/freebusy/", 10)) {
838                 strcpy(who, &who[10]);
839         }
840         unescape_input(who);
841
842         len = strlen(who);
843         if ( (!strcasecmp(&who[len-4], ".vcf"))
844              || (!strcasecmp(&who[len-4], ".ifb"))
845              || (!strcasecmp(&who[len-4], ".vfb")) ) {
846                 who[len-4] = 0;
847         }
848
849         lprintf(9, "freebusy requested for <%s>\n", who);
850         serv_printf("ICAL freebusy|%s", who);
851         serv_getln(buf, sizeof buf);
852
853         if (buf[0] != '1') {
854                 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
855                 output_headers(0, 0, 0, 0, 0, 0);
856                 hprintf("Content-Type: text/plain\r\n");
857                 wc_printf("%s\n", &buf[4]);
858                 end_burst();
859                 return;
860         }
861
862         read_server_text(WC->WBuf, &lines);
863         http_transmit_thing("text/calendar", 0);
864 }
865
866
867
868 int calendar_Cleanup(void **ViewSpecific)
869 {
870         calview *c;
871         
872         c = (calview *) *ViewSpecific;
873
874         wDumpContent(1);
875         free (c);
876         *ViewSpecific = NULL;
877
878         return 0;
879 }
880
881 int __calendar_Cleanup(void **ViewSpecific)
882 {
883         calview *c;
884         
885         c = (calview *) *ViewSpecific;
886
887         free (c);
888         *ViewSpecific = NULL;
889
890         return 0;
891 }
892
893
894 void 
895 InitModule_CALENDAR
896 (void)
897 {
898         RegisterReadLoopHandlerset(
899                 VIEW_CALENDAR,
900                 calendar_GetParamsGetServerCall,
901                 NULL,
902                 NULL,
903                 calendar_LoadMsgFromServer,
904                 calendar_RenderView_or_Tail,
905                 calendar_Cleanup);
906
907         RegisterReadLoopHandlerset(
908                 VIEW_CALBRIEF,
909                 calendar_GetParamsGetServerCall,
910                 NULL,
911                 NULL,
912                 calendar_LoadMsgFromServer,
913                 calendar_RenderView_or_Tail,
914                 calendar_Cleanup);
915
916
917
918         RegisterPreference("daystart", _("Calendar day view begins at:"), PRF_INT, NULL);
919         RegisterPreference("dayend", _("Calendar day view ends at:"), PRF_INT, NULL);
920         RegisterPreference("weekstart", _("Week starts on:"), PRF_INT, NULL);
921
922         WebcitAddUrlHandler(HKEY("freebusy"), "", 0, do_freebusy, COOKIEUNNEEDED|ANONYMOUS|FORCE_SESSIONCLOSE);
923         WebcitAddUrlHandler(HKEY("display_edit_task"), "", 0, display_edit_task, 0);
924         WebcitAddUrlHandler(HKEY("display_edit_event"), "", 0, display_edit_event, 0);
925         WebcitAddUrlHandler(HKEY("save_event"), "", 0, save_event, 0);
926         WebcitAddUrlHandler(HKEY("respond_to_request"), "", 0, respond_to_request, 0);
927         WebcitAddUrlHandler(HKEY("handle_rsvp"), "", 0, handle_rsvp, 0);
928 }