* serv_getln now is a wrapper around existing functionality. a new temporary var...
[citadel.git] / webcit / calendar.c
1 /*
2  * $Id$
3  *
4  * Functions which handle calendar objects and their processing/display.
5  */
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10
11 /*
12  * Process a calendar object.  At this point it's already been deserialized by cal_process_attachment()
13  *
14  * cal:                 the calendar object
15  * recursion_level:     Number of times we've recursed into this function
16  * msgnum:              Message number on the Citadel server
17  * cal_partnum:         MIME part number within that message containing the calendar object
18  */
19 void cal_process_object(StrBuf *Target,
20                         icalcomponent *cal,
21                         int recursion_level,
22                         long msgnum,
23                         const char *cal_partnum) 
24 {
25         icalcomponent *c;
26         icalproperty *method = NULL;
27         icalproperty_method the_method = ICAL_METHOD_NONE;
28         icalproperty *p;
29         struct icaltimetype t;
30         time_t tt;
31         char buf[256];
32         char conflict_name[256];
33         char conflict_message[256];
34         int is_update = 0;
35         char divname[32];
36         static int divcount = 0;
37
38         sprintf(divname, "rsvp%04x", ++divcount);
39
40         /* Convert timezones to something easy to display.
41          * It's safe to do this in memory because we're only changing it on the
42          * display side -- when we tell the server to do something with the object,
43          * the server will be working with its original copy in the database.
44          */
45         if ((cal) && (recursion_level == 0)) {
46                 ical_dezonify(cal);
47         }
48
49         /* Leading HTML for the display of this object */
50         if (recursion_level == 0) {
51                 StrBufAppendPrintf(Target, "<div class=\"mimepart\">\n");
52         }
53
54         /* Look for a method */
55         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
56
57         /* See what we need to do with this */
58         if (method != NULL) {
59                 char *title;
60                 the_method = icalproperty_get_method(method);
61
62                 StrBufAppendPrintf(Target, "<div id=\"%s_title\">", divname);
63                 StrBufAppendPrintf(Target, "<img src=\"static/calarea_48x.gif\">");
64                 StrBufAppendPrintf(Target, "<span>");
65                 switch(the_method) {
66                 case ICAL_METHOD_REQUEST:
67                         title = _("Meeting invitation");
68                         break;
69                 case ICAL_METHOD_REPLY:
70                         title = _("Attendee's reply to your invitation");
71                         break;
72                 case ICAL_METHOD_PUBLISH:
73                         title = _("Published event");
74                         break;
75                 default:
76                         title = _("This is an unknown type of calendar item.");
77                         break;
78                 }
79                 StrBufAppendPrintf(Target, "</span>");
80
81                 StrBufAppendPrintf(Target, "&nbsp;&nbsp;%s",title);
82                 StrBufAppendPrintf(Target, "</div>");
83         }
84
85         StrBufAppendPrintf(Target, "<dl>");
86         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
87         if (p != NULL) {
88                 StrBufAppendPrintf(Target, "<dt>");
89                 StrBufAppendPrintf(Target, _("Summary:"));
90                 StrBufAppendPrintf(Target, "</dt><dd>");
91                 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
92                 StrBufAppendPrintf(Target, "</dd>\n");
93         }
94
95         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
96         if (p != NULL) {
97                 StrBufAppendPrintf(Target, "<dt>");
98                 StrBufAppendPrintf(Target, _("Location:"));
99                 StrBufAppendPrintf(Target, "</dt><dd>");
100                 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
101                 StrBufAppendPrintf(Target, "</dd>\n");
102         }
103
104         /*
105          * Only show start/end times if we're actually looking at the VEVENT
106          * component.  Otherwise it shows bogus dates for things like timezone.
107          */
108         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
109
110                 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
111                 if (p != NULL) {
112                         t = icalproperty_get_dtstart(p);
113
114                         if (t.is_date) {
115                                 struct tm d_tm;
116                                 char d_str[32];
117                                 memset(&d_tm, 0, sizeof d_tm);
118                                 d_tm.tm_year = t.year - 1900;
119                                 d_tm.tm_mon = t.month - 1;
120                                 d_tm.tm_mday = t.day;
121                                 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
122                                 StrBufAppendPrintf(Target, "<dt>");
123                                 StrBufAppendPrintf(Target, _("Date:"));
124                                 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", d_str);
125                         }
126                         else {
127                                 tt = icaltime_as_timet(t);
128                                 webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
129                                 StrBufAppendPrintf(Target, "<dt>");
130                                 StrBufAppendPrintf(Target, _("Starting date/time:"));
131                                 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
132                         }
133                 }
134         
135                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
136                 if (p != NULL) {
137                         t = icalproperty_get_dtend(p);
138                         tt = icaltime_as_timet(t);
139                         webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
140                         StrBufAppendPrintf(Target, "<dt>");
141                         StrBufAppendPrintf(Target, _("Ending date/time:"));
142                         StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
143                 }
144
145         }
146
147         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
148         if (p != NULL) {
149                 StrBufAppendPrintf(Target, "<dt>");
150                 StrBufAppendPrintf(Target, _("Description:"));
151                 StrBufAppendPrintf(Target, "</dt><dd>");
152                 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
153                 StrBufAppendPrintf(Target, "</dd>\n");
154         }
155
156         if (icalcomponent_get_first_property(cal, ICAL_RRULE_PROPERTY)) {
157                 /* Unusual string syntax used here in order to re-use existing translations */
158                 StrBufAppendPrintf(Target, "<dt>%s:</dt><dd>%s.</dd>\n",
159                         _("Recurrence"),
160                         _("This is a recurring event")
161                 );
162         }
163
164         /* If the component has attendees, iterate through them. */
165         for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); 
166              (p != NULL); 
167              p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
168                 StrBufAppendPrintf(Target, "<dt>");
169                 StrBufAppendPrintf(Target, _("Attendee:"));
170                 StrBufAppendPrintf(Target, "</dt><dd>");
171                 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
172                 if (!strncasecmp(buf, "MAILTO:", 7)) {
173
174                         /** screen name or email address */
175                         strcpy(buf, &buf[7]);
176                         striplt(buf);
177                         StrEscAppend(Target, NULL, buf, 0, 0);
178                         StrBufAppendPrintf(Target, " ");
179
180                         /** participant status */
181                         partstat_as_string(buf, p);
182                         StrEscAppend(Target, NULL, buf, 0, 0);
183                 }
184                 StrBufAppendPrintf(Target, "</dd>\n");
185         }
186
187         /* If the component has subcomponents, recurse through them. */
188         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
189              (c != 0);
190              c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
191                 /* Recursively process subcomponent */
192                 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
193         }
194
195         /* If this is a REQUEST, display conflicts and buttons */
196         if (the_method == ICAL_METHOD_REQUEST) {
197
198                 /* Check for conflicts */
199                 lprintf(9, "Checking server calendar for conflicts...\n");
200                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
201                 serv_getln(buf, sizeof buf);
202                 if (buf[0] == '1') {
203                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
204                                 extract_token(conflict_name, buf, 3, '|', sizeof conflict_name);
205                                 is_update = extract_int(buf, 4);
206
207                                 if (is_update) {
208                                         snprintf(conflict_message, sizeof conflict_message,
209                                                  _("This is an update of '%s' which is already in your calendar."), conflict_name);
210                                 }
211                                 else {
212                                         snprintf(conflict_message, sizeof conflict_message,
213                                                  _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
214                                 }
215
216                                 StrBufAppendPrintf(Target, "<dt>%s",
217                                         (is_update ?
218                                          _("Update:") :
219                                          _("CONFLICT:")
220                                                 )
221                                         );
222                                 StrBufAppendPrintf(Target, "</dt><dd>");
223                                 StrEscAppend(Target, NULL, conflict_message, 0, 0);
224                                 StrBufAppendPrintf(Target, "</dd>\n");
225                         }
226                 }
227                 lprintf(9, "...done.\n");
228
229                 StrBufAppendPrintf(Target, "</dl>");
230
231                 /* Display the Accept/Decline buttons */
232                 StrBufAppendPrintf(Target, "<p id=\"%s_question\">"
233                         "%s "
234                         "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
235                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
236                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
237                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
238                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
239                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
240                         "</span></p>\n",
241                         divname,
242                         _("How would you like to respond to this invitation?"),
243                         divname, divname, msgnum, cal_partnum, _("Accept"),
244                         divname, divname, msgnum, cal_partnum, _("Tentative"),
245                         divname, divname, msgnum, cal_partnum, _("Decline")
246                         );
247
248         }
249
250         /* If this is a REPLY, display update button */
251         if (the_method == ICAL_METHOD_REPLY) {
252
253                 /* Display the update buttons */
254                 StrBufAppendPrintf(Target, "<p id=\"%s_question\" >"
255                         "%s "
256                         "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
257                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
258                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
259                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
260                         "</span></p>\n",
261                         divname,
262                         _("Click <i>Update</i> to accept this reply and update your calendar."),
263                         divname, divname, msgnum, cal_partnum, _("Update"),
264                         divname, divname, msgnum, cal_partnum, _("Ignore")
265                         );
266         
267         }
268         
269         /* Trailing HTML for the display of this object */
270         if (recursion_level == 0) {
271                 StrBufAppendPrintf(Target, "<p>&nbsp;</p></div>\n");
272         }
273 }
274
275
276 /*
277  * Deserialize a calendar object in a message so it can be displayed.
278  */
279 void cal_process_attachment(wc_mime_attachment *Mime) 
280 {
281         icalcomponent *cal;
282
283         cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
284         FlushStrBuf(Mime->Data);
285         if (cal == NULL) {
286                 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
287                 StrBufAppendPrintf(Mime->Data, "<br />\n");
288                 return;
289         }
290
291         cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum));
292
293         /* Free the memory we obtained from libical's constructor */
294         icalcomponent_free(cal);
295 }
296
297
298
299
300 /*
301  * Respond to a meeting request - accept/decline meeting
302  */
303 void respond_to_request(void) 
304 {
305         char buf[1024];
306
307         begin_ajax_response();
308
309         serv_printf("ICAL respond|%s|%s|%s|",
310                 bstr("msgnum"),
311                 bstr("cal_partnum"),
312                 bstr("sc")
313         );
314         serv_getln(buf, sizeof buf);
315
316         if (buf[0] == '2') {
317                 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
318                 if (!strcasecmp(bstr("sc"), "accept")) {
319                         wprintf(_("You have accepted this meeting invitation.  "
320                                 "It has been entered into your calendar.")
321                         );
322                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
323                         wprintf(_("You have tentatively accepted this meeting invitation.  "
324                                 "It has been 'pencilled in' to your calendar.")
325                         );
326                 } else if (!strcasecmp(bstr("sc"), "decline")) {
327                         wprintf(_("You have declined this meeting invitation.  "
328                                   "It has <b>not</b> been entered into your calendar.")
329                                 );
330                 }
331                 wprintf(" ");
332                 wprintf(_("A reply has been sent to the meeting organizer."));
333                 wprintf("</span>");
334         } else {
335                 wprintf("<img align=\"center\" src=\"static/error.gif\"><span>");
336                 wprintf("%s\n", &buf[4]);
337                 wprintf("</span>");
338         }
339
340         end_ajax_response();
341 }
342
343
344
345 /*
346  * Handle an incoming RSVP
347  */
348 void handle_rsvp(void) 
349 {
350         char buf[1024];
351
352         begin_ajax_response();
353
354         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
355                 bstr("msgnum"),
356                 bstr("cal_partnum"),
357                 bstr("sc")
358         );
359         serv_getln(buf, sizeof buf);
360
361         if (buf[0] == '2') {
362                 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
363                 if (!strcasecmp(bstr("sc"), "update")) {
364                         wprintf(_("Your calendar has been updated to reflect this RSVP."));
365                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
366                         wprintf(_("You have chosen to ignore this RSVP. "
367                                   "Your calendar has <b>not</b> been updated.")
368                                 );
369                 }
370                 wprintf("</span>");
371         } else {
372                 wprintf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
373                 wprintf("</span>");
374         }
375
376         end_ajax_response();
377 }
378
379
380
381
382 /*
383  * free memory allocated using libical
384  */
385 void delete_cal(void *vCal)
386 {
387         disp_cal *Cal = (disp_cal*) vCal;
388         icalcomponent_free(Cal->cal);
389         free(Cal->from);
390         free(Cal);
391 }
392
393 /*
394  * This is the meat-and-bones of the first part of our two-phase calendar display.
395  * As we encounter calendar items in messages being read from the server, we break out
396  * any iCalendar objects and store them in a hash table.  Later on, the second phase will
397  * use this hash table to render the calendar for display.
398  */
399 void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unread, struct calview *calv)
400 {
401         icalproperty *ps = NULL;
402         struct icaltimetype dtstart, dtend;
403         struct icaldurationtype dur;
404         wcsession *WCC = WC;
405         disp_cal *Cal;
406         size_t len;
407         time_t final_recurrence = 0;
408         icalcomponent *cptr = NULL;
409
410         /* recur variables */
411         icalproperty *rrule = NULL;
412         struct icalrecurrencetype recur;
413         icalrecur_iterator *ritr = NULL;
414         struct icaltimetype next;
415         int num_recur = 0;
416         int stop_rr = 0;
417
418         dtstart = icaltime_null_time();
419         dtend = icaltime_null_time();
420         
421         if (WCC->disp_cal_items == NULL)
422                 WCC->disp_cal_items = NewHash(0, Flathash);
423
424         /* Note: anything we do here, we also have to do below for the recurrences. */
425         Cal = (disp_cal*) malloc(sizeof(disp_cal));
426         memset(Cal, 0, sizeof(disp_cal));
427         Cal->cal = icalcomponent_new_clone(cal);
428
429         /* Dezonify and decapsulate at the very last moment */
430         /* lprintf(9, "INITIAL: %s\n", icaltime_as_ical_string(icalproperty_get_dtstart(
431                 icalcomponent_get_first_property(icalcomponent_get_first_component(
432                 Cal->cal, ICAL_VEVENT_COMPONENT), ICAL_DTSTART_PROPERTY)))
433         ); */
434         ical_dezonify(Cal->cal);
435         if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
436                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
437                 if (cptr) {
438                         cptr = icalcomponent_new_clone(cptr);
439                         icalcomponent_free(Cal->cal);
440                         Cal->cal = cptr;
441                 }
442         }
443
444         Cal->unread = unread;
445         len = strlen(from);
446         Cal->from = (char*)malloc(len+ 1);
447         memcpy(Cal->from, from, len + 1);
448         Cal->cal_msgnum = msgnum;
449
450         /* Precalculate the starting date and time of this event, and store it in our top-level
451          * structure.  Later, when we are rendering the calendar, we can just peek at these values
452          * without having to break apart every calendar item.
453          */
454         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
455         if (ps != NULL) {
456                 dtstart = icalproperty_get_dtstart(ps);
457                 Cal->event_start = icaltime_as_timet(dtstart);
458         }
459
460         /* Do the same for the ending date and time.  It makes the day view much easier to render. */
461         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
462         if (ps != NULL) {
463                 dtend = icalproperty_get_dtend(ps);
464                 Cal->event_end = icaltime_as_timet(dtend);
465         }
466
467         /* Store it in the hash list. */
468         Put(WCC->disp_cal_items, 
469             (char*) &Cal->event_start,
470             sizeof(Cal->event_start), 
471             Cal, 
472             delete_cal);
473
474         /****************************** handle recurring events ******************************/
475
476         if (icaltime_is_null_time(dtstart)) return;     /* Can't recur without a start time */
477
478         if (!icaltime_is_null_time(dtend)) {            /* Need duration for recurrences */
479                 dur = icaltime_subtract(dtend, dtstart);
480         }
481
482         /*
483          * Just let libical iterate the recurrence, and keep looping back to the top of this function,
484          * adding new hash entries that all point back to the same msgnum, until either the iteration
485          * stops or some outer bound is reached.  The display code will automatically do the Right Thing.
486          */
487         cptr = cal;
488         if (icalcomponent_isa(cptr) != ICAL_VEVENT_COMPONENT) {
489                 cptr = icalcomponent_get_first_component(cptr, ICAL_VEVENT_COMPONENT);
490         }
491         if (!cptr) return;
492         ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
493         if (ps == NULL) return;
494         dtstart = icalproperty_get_dtstart(ps);
495         rrule = icalcomponent_get_first_property(cptr, ICAL_RRULE_PROPERTY);
496         if (!rrule) return;
497         recur = icalproperty_get_rrule(rrule);
498         ritr = icalrecur_iterator_new(recur, dtstart);
499         if (!ritr) return;
500
501         while (next = icalrecur_iterator_next(ritr), ((!icaltime_is_null_time(next))&&(!stop_rr)) ) {
502                 ++num_recur;
503                 if (num_recur > 1) {            /* Skip the first one.  We already did it at the root. */
504                         icalcomponent *cptr;
505                         /* lprintf(9, "REPEATS: %s\n", icaltime_as_ical_string(next)); */
506
507                         /* Note: anything we do here, we also have to do above for the root event. */
508                         Cal = (disp_cal*) malloc(sizeof(disp_cal));
509                         memset(Cal, 0, sizeof(disp_cal));
510                         Cal->cal = icalcomponent_new_clone(cal);
511                         Cal->unread = unread;
512                         len = strlen(from);
513                         Cal->from = (char*)malloc(len+ 1);
514                         memcpy(Cal->from, from, len + 1);
515                         Cal->cal_msgnum = msgnum;
516
517                         if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
518                                 cptr = Cal->cal;
519                         }
520                         else {
521                                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
522                         }
523                         if (cptr) {
524                                 ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
525                                 if (ps != NULL) {
526                                         icalcomponent_remove_property(cptr, ps);
527                                         ps = icalproperty_new_dtstart(next);
528                                         icalcomponent_add_property(cptr, ps);
529         
530                                         Cal->event_start = icaltime_as_timet(next);
531                                         final_recurrence = Cal->event_start;
532                                 }
533
534                                 ps = icalcomponent_get_first_property(cptr, ICAL_DTEND_PROPERTY);
535                                 if (ps != NULL) {
536                                         icalcomponent_remove_property(cptr, ps);
537         
538                                         /* Make a new dtend */
539                                         ps = icalproperty_new_dtend(icaltime_add(next, dur));
540                 
541                                         /* and stick it somewhere */
542                                         icalcomponent_add_property(cptr, ps);
543                                 }
544
545                         }
546
547                         /* Dezonify and decapsulate at the very last moment */
548                         ical_dezonify(Cal->cal);
549                         if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
550                                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
551                                 if (cptr) {
552                                         cptr = icalcomponent_new_clone(cptr);
553                                         icalcomponent_free(Cal->cal);
554                                         Cal->cal = cptr;
555                                 }
556                         }
557
558                         if ( (Cal->event_start > calv->lower_bound)
559                            && (Cal->event_start < calv->upper_bound) ) {
560                                 Put(WCC->disp_cal_items, 
561                                         (char*) &Cal->event_start,
562                                         sizeof(Cal->event_start), 
563                                         Cal, 
564                                         delete_cal
565                                 );
566                         }
567                         else {
568                                 delete_cal(Cal);
569                         }
570
571                         /* If an upper bound is set, stop when we go out of scope */
572                         if (final_recurrence > calv->upper_bound) stop_rr = 1;
573                 }
574         }
575         icalrecur_iterator_free(ritr);
576         /* lprintf(9, "Performed %d recurrences; final one is %s", num_recur, ctime(&final_recurrence)); */
577
578 }
579
580
581
582 /*
583  * Display a task by itself (for editing)
584  */
585 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
586                         int unread, struct calview *calv)
587 {
588         icalcomponent *vtodo;
589         icalproperty *p;
590         struct icaltimetype IcalTime;
591         time_t now;
592         int created_new_vtodo = 0;
593         icalproperty_status todoStatus;
594
595         now = time(NULL);
596
597         if (supplied_vtodo != NULL) {
598                 vtodo = supplied_vtodo;
599
600                 /*
601                  * It's safe to convert to UTC here because there are no recurrences to worry about.
602                  */
603                 ical_dezonify(vtodo);
604
605                 /*
606                  * If we're looking at a fully encapsulated VCALENDAR
607                  * rather than a VTODO component, attempt to use the first
608                  * relevant VTODO subcomponent.  If there is none, the
609                  * NULL returned by icalcomponent_get_first_component() will
610                  * tell the next iteration of this function to create a
611                  * new one.
612                  */
613                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
614                         display_edit_individual_task(
615                                 icalcomponent_get_first_component(
616                                         vtodo, ICAL_VTODO_COMPONENT
617                                         ), 
618                                 msgnum, from, unread, calv
619                                 );
620                         return;
621                 }
622         }
623         else {
624                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
625                 created_new_vtodo = 1;
626         }
627         
628         /* TODO: Can we take all this and move it into a template?       */
629         output_headers(1, 1, 1, 0, 0, 0);
630         wprintf("<!-- start task edit form -->");
631         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
632         /* Get summary early for title */
633         wprintf("<div class=\"box\">\n");
634         wprintf("<div class=\"boxlabel\">");
635         wprintf(_("Edit task"));
636         wprintf("- ");
637         if (p != NULL) {
638                 escputs((char *)icalproperty_get_comment(p));
639         }
640         wprintf("</div>");
641         
642         wprintf("<div class=\"boxcontent\">\n");
643         wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
644         wprintf("<div style=\"display: none;\">\n       ");
645         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
646         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
647         wprintf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
648                 ibstr("return_to_summary"));
649         wprintf("</div>");
650         wprintf("<table class=\"calendar_background\"><tr><td>");
651         wprintf("<TABLE STYLE=\"border: none;\">\n");
652
653         wprintf("<TR><TD>");
654         wprintf(_("Summary:"));
655         wprintf("</TD><TD>"
656                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
657                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
658         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
659         if (p != NULL) {
660                 escputs((char *)icalproperty_get_comment(p));
661         }
662         wprintf("\"></TD></TR>\n");
663
664         wprintf("<TR><TD>");
665         wprintf(_("Start date:"));
666         wprintf("</TD><TD>");
667         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
668         wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
669         if (p == NULL) {
670                 wprintf("CHECKED=\"CHECKED\"");
671         }
672         wprintf(">");
673         wprintf(_("No date"));
674         
675         wprintf(" ");
676         wprintf("<span ID=\"dtstart_date\">");
677         wprintf(_("or"));
678         wprintf(" ");
679         if (p != NULL) {
680                 IcalTime = icalproperty_get_dtstart(p);
681         }
682         else
683                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
684         display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
685
686         wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"dtstart_time_assoc\" ID=\"dtstart_time_assoc\" VALUE=\"yes\"");
687         if (!IcalTime.is_date) {
688                 wprintf("CHECKED=\"CHECKED\"");
689         }
690         wprintf(">");
691         wprintf(_("Time associated"));
692         wprintf("</span></TD></TR>\n");
693
694         wprintf("<TR><TD>");
695         wprintf(_("Due date:"));
696         wprintf("</TD><TD>");
697         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
698         wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
699         if (p == NULL) {
700                 wprintf("CHECKED=\"CHECKED\"");
701         }
702         wprintf(">");
703         wprintf(_("No date"));
704         wprintf(" ");
705         wprintf("<span ID=\"due_date\">\n");
706         wprintf(_("or"));
707         wprintf(" ");
708         if (p != NULL) {
709                 IcalTime = icalproperty_get_due(p);
710         }
711         else
712                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
713         display_icaltimetype_as_webform(&IcalTime, "due", 0);
714
715         wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"due_time_assoc\" ID=\"due_time_assoc\" VALUE=\"yes\"");
716         if (!IcalTime.is_date) {
717                 wprintf("CHECKED=\"CHECKED\"");
718         }
719         wprintf(">");
720         wprintf(_("Time associated"));
721         wprintf("</span></TD></TR>\n");
722         todoStatus = icalcomponent_get_status(vtodo);
723         wprintf("<TR><TD>\n");
724         wprintf(_("Completed:"));
725         wprintf("</TD><TD>");
726         wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
727         if (todoStatus == ICAL_STATUS_COMPLETED) {
728                 wprintf(" CHECKED=\"CHECKED\"");
729         } 
730         wprintf(" >");
731         wprintf("</TD></TR>");
732         /* start category field */
733         p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
734         wprintf("<TR><TD>");
735         wprintf(_("Category:"));
736         wprintf("</TD><TD>");
737         wprintf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
738         if (p != NULL) {
739                 escputs((char *)icalproperty_get_categories(p));
740         }
741         wprintf("\">");
742         wprintf("</TD></TR>\n   ");
743         /* end category field */
744         wprintf("<TR><TD>");
745         wprintf(_("Description:"));
746         wprintf("</TD><TD>");
747         wprintf("<TEXTAREA NAME=\"description\" "
748                 "ROWS=\"10\" COLS=\"80\">\n"
749                 );
750         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
751         if (p != NULL) {
752                 escputs((char *)icalproperty_get_comment(p));
753         }
754         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
755
756         wprintf("<SPAN STYLE=\"text-align: center;\">"
757                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
758                 "&nbsp;&nbsp;"
759                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
760                 "&nbsp;&nbsp;"
761                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
762                 "</SPAN>\n",
763                 _("Save"),
764                 _("Delete"),
765                 _("Cancel")
766                 );
767         wprintf("</td></tr></table>");
768         wprintf("</FORM>\n");
769         wprintf("</div></div></div>\n");
770         wprintf("<!-- end task edit form -->");
771         wDumpContent(1);
772
773         if (created_new_vtodo) {
774                 icalcomponent_free(vtodo);
775         }
776 }
777
778 /*
779  * Save an edited task
780  *
781  * supplied_vtodo       the task to save
782  * msgnum               number of the mesage in our db
783  */
784 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
785                                 struct calview *calv)
786 {
787         char buf[SIZ];
788         int delete_existing = 0;
789         icalproperty *prop;
790         icalcomponent *vtodo, *encaps;
791         int created_new_vtodo = 0;
792         int i;
793         int sequence = 0;
794         struct icaltimetype t;
795
796         if (supplied_vtodo != NULL) {
797                 vtodo = supplied_vtodo;
798                 /**
799                  * If we're looking at a fully encapsulated VCALENDAR
800                  * rather than a VTODO component, attempt to use the first
801                  * relevant VTODO subcomponent.  If there is none, the
802                  * NULL returned by icalcomponent_get_first_component() will
803                  * tell the next iteration of this function to create a
804                  * new one.
805                  */
806                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
807                         save_individual_task(
808                                 icalcomponent_get_first_component(
809                                         vtodo, ICAL_VTODO_COMPONENT), 
810                                 msgnum, from, unread, calv
811                                 );
812                         return;
813                 }
814         }
815         else {
816                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
817                 created_new_vtodo = 1;
818         }
819
820         if (havebstr("save_button")) {
821
822                 /** Replace values in the component with ones from the form */
823
824                 while (prop = icalcomponent_get_first_property(vtodo,
825                                                                ICAL_SUMMARY_PROPERTY), prop != NULL) {
826                         icalcomponent_remove_property(vtodo, prop);
827                         icalproperty_free(prop);
828                 }
829                 if (havebstr("summary")) {
830
831                         icalcomponent_add_property(vtodo,
832                                                    icalproperty_new_summary(bstr("summary")));
833                 } else {
834                         icalcomponent_add_property(vtodo,
835                                                    icalproperty_new_summary(_("Untitled Task")));
836                 }
837         
838                 while (prop = icalcomponent_get_first_property(vtodo,
839                                                                ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
840                         icalcomponent_remove_property(vtodo, prop);
841                         icalproperty_free(prop);
842                 }
843                 if (havebstr("description")) {
844                         icalcomponent_add_property(vtodo,
845                                                    icalproperty_new_description(bstr("description")));
846                 }
847         
848                 while (prop = icalcomponent_get_first_property(vtodo,
849                                                                ICAL_DTSTART_PROPERTY), prop != NULL) {
850                         icalcomponent_remove_property(vtodo, prop);
851                         icalproperty_free(prop);
852                 }
853                 if (IsEmptyStr(bstr("nodtstart"))) {
854                         if (yesbstr("dtstart_time")) {
855                                 icaltime_from_webform(&t, "dtstart");
856                         }
857                         else {
858                                 icaltime_from_webform_dateonly(&t, "dtstart");
859                         }
860                         icalcomponent_add_property(vtodo,
861                                                    icalproperty_new_dtstart(t)
862                                 );
863                 }
864                 while(prop = icalcomponent_get_first_property(vtodo,
865                                                               ICAL_STATUS_PROPERTY), prop != NULL) {
866                         icalcomponent_remove_property(vtodo,prop);
867                         icalproperty_free(prop);
868                 }
869                 while(prop = icalcomponent_get_first_property(vtodo,
870                                                               ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
871                         icalcomponent_remove_property(vtodo,prop);
872                         icalproperty_free(prop);
873                 }
874
875                 if (havebstr("status")) {
876                         icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
877                         icalcomponent_set_status(vtodo, taskStatus);
878                         icalcomponent_add_property(vtodo,
879                                 icalproperty_new_percentcomplete(
880                                         (strcasecmp(bstr("status"), "completed") ? 0 : 100)
881                                 )
882                         );
883                 }
884                 else {
885                         icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
886                 }
887                 while (prop = icalcomponent_get_first_property(vtodo,
888                                                                ICAL_CATEGORIES_PROPERTY), prop != NULL) {
889                         icalcomponent_remove_property(vtodo,prop);
890                         icalproperty_free(prop);
891                 }
892                 if (!IsEmptyStr(bstr("category"))) {
893                         prop = icalproperty_new_categories(bstr("category"));
894                         icalcomponent_add_property(vtodo,prop);
895                 }
896                 while (prop = icalcomponent_get_first_property(vtodo,
897                                                                ICAL_DUE_PROPERTY), prop != NULL) {
898                         icalcomponent_remove_property(vtodo, prop);
899                         icalproperty_free(prop);
900                 }
901                 if (IsEmptyStr(bstr("nodue"))) {
902                         if (yesbstr("due_time")) {
903                                 icaltime_from_webform(&t, "due");
904                         }
905                         else {
906                                 icaltime_from_webform_dateonly(&t, "due");
907                         }
908                         icalcomponent_add_property(vtodo,
909                                                    icalproperty_new_due(t)
910                                 );
911                 }
912                 /** Give this task a UID if it doesn't have one. */
913                 lprintf(9, "Give this task a UID if it doesn't have one.\n");
914                 if (icalcomponent_get_first_property(vtodo,
915                                                      ICAL_UID_PROPERTY) == NULL) {
916                         generate_uuid(buf);
917                         icalcomponent_add_property(vtodo,
918                                                    icalproperty_new_uid(buf)
919                                 );
920                 }
921
922                 /* Increment the sequence ID */
923                 lprintf(9, "Increment the sequence ID\n");
924                 while (prop = icalcomponent_get_first_property(vtodo,
925                                                                ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
926                         i = icalproperty_get_sequence(prop);
927                         lprintf(9, "Sequence was %d\n", i);
928                         if (i > sequence) sequence = i;
929                         icalcomponent_remove_property(vtodo, prop);
930                         icalproperty_free(prop);
931                 }
932                 ++sequence;
933                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
934                 icalcomponent_add_property(vtodo,
935                                            icalproperty_new_sequence(sequence)
936                         );
937
938                 /*
939                  * Encapsulate event into full VCALENDAR component.  Clone it first,
940                  * for two reasons: one, it's easier to just free the whole thing
941                  * when we're done instead of unbundling, but more importantly, we
942                  * can't encapsulate something that may already be encapsulated
943                  * somewhere else.
944                  */
945                 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
946                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
947
948                 /* Serialize it and save it to the message base */
949                 serv_puts("ENT0 1|||4");
950                 serv_getln(buf, sizeof buf);
951                 if (buf[0] == '4') {
952                         serv_puts("Content-type: text/calendar");
953                         serv_puts("");
954                         serv_puts(icalcomponent_as_ical_string(encaps));
955                         serv_puts("000");
956
957                         /*
958                          * Probably not necessary; the server will see the UID
959                          * of the object and delete the old one anyway, but
960                          * just in case...
961                          */
962                         delete_existing = 1;
963                 }
964                 icalcomponent_free(encaps);
965         }
966
967         /**
968          * If the user clicked 'Delete' then explicitly delete the message.
969          */
970         if (havebstr("delete_button")) {
971                 delete_existing = 1;
972         }
973
974         if ( (delete_existing) && (msgnum > 0L) ) {
975                 serv_printf("DELE %ld", lbstr("msgnum"));
976                 serv_getln(buf, sizeof buf);
977         }
978
979         if (created_new_vtodo) {
980                 icalcomponent_free(vtodo);
981         }
982
983         /* Go back to wherever we came from */
984         if (ibstr("return_to_summary") == 1) {
985                 summary();
986         }
987         else {
988                 readloop(readfwd);
989         }
990 }
991
992
993
994 void process_ical_object(long msgnum, int unread,
995                          char *from, 
996                          char *FlatIcal, 
997                          icalcomponent_kind which_kind,
998                          IcalCallbackFunc CallBack,
999                          struct calview *calv
1000         ) 
1001 {
1002         icalcomponent *cal, *c;
1003
1004         cal = icalcomponent_new_from_string(FlatIcal);
1005         if (cal != NULL) {
1006
1007                 /* A which_kind of (-1) means just load the whole thing */
1008                 if (which_kind == (-1)) {
1009                         CallBack(cal, msgnum, from, unread, calv);
1010                 }
1011                 
1012                 /* Otherwise recurse and hunt */
1013                 else {
1014                         
1015                         /* Simple components of desired type */
1016                         if (icalcomponent_isa(cal) == which_kind) {
1017                                 CallBack(cal, msgnum, from, unread, calv);
1018                         }
1019                         
1020                         /* Subcomponents of desired type */
1021                         for (c = icalcomponent_get_first_component(cal, which_kind);
1022                              (c != 0);
1023                              c = icalcomponent_get_next_component(cal, which_kind)) {
1024                                 CallBack(c, msgnum, from, unread, calv);
1025                         }
1026                         
1027                 }
1028                 
1029                 icalcomponent_free(cal);
1030         }
1031 }
1032
1033 /*
1034  * Code common to all icalendar display handlers.  Given a message number and a MIME
1035  * type, we load the message and hunt for that MIME type.  If found, we load
1036  * the relevant part, deserialize it into a libical component, filter it for
1037  * the requested object type, and feed it to the specified handler.
1038  */
1039 void load_ical_object(long msgnum, int unread,
1040                       icalcomponent_kind which_kind,
1041                       IcalCallbackFunc CallBack,
1042                       struct calview *calv,
1043                       int RenderAsync
1044         ) 
1045 {
1046         StrBuf *Buf;
1047         StrBuf *Data;
1048         const char *bptr;
1049         int Done = 0;
1050         char from[128] = "";
1051         char mime_partnum[256];
1052         char mime_filename[256];
1053         char mime_content_type[256];
1054         char mime_disposition[256];
1055         int mime_length;
1056         char relevant_partnum[256];
1057         char *relevant_source = NULL;
1058         int phase = 0;                          /* 0 = citadel headers, 1 = mime headers, 2 = body */
1059         char msg4_content_type[256] = "";
1060         char msg4_content_encoding[256] = "";
1061         int msg4_content_length = 0;
1062
1063         relevant_partnum[0] = '\0';
1064         serv_printf("MSG4 %ld", msgnum);        /* we need the mime headers */
1065         Buf = NewStrBuf();
1066         StrBuf_ServGetln(Buf);
1067         if (GetServerStatus(Buf, NULL) != 1) {
1068                 FreeStrBuf (&Buf);
1069                 return;
1070         }
1071         while (!Done && (StrBuf_ServGetln(Buf)>=0)) {
1072                 if ( (StrLength(Buf)==3) && 
1073                      !strcmp(ChrPtr(Buf), "000")) {
1074                         Done = 1;
1075                         break;
1076                 }
1077                 bptr = ChrPtr(Buf);
1078                 switch (phase) {
1079                 case 0:
1080                         if (!strncasecmp(bptr, "part=", 5)) {
1081                                 extract_token(mime_filename, &bptr[5], 1, '|', sizeof mime_filename);
1082                                 extract_token(mime_partnum, &bptr[5], 2, '|', sizeof mime_partnum);
1083                                 extract_token(mime_disposition, &bptr[5], 3, '|', sizeof mime_disposition);
1084                                 extract_token(mime_content_type, &bptr[5], 4, '|', sizeof mime_content_type);
1085                                 mime_length = extract_int(&bptr[5], 5);
1086
1087                                 if (  (!strcasecmp(mime_content_type, "text/calendar"))
1088                                       || (!strcasecmp(mime_content_type, "application/ics"))
1089                                       || (!strcasecmp(mime_content_type, "text/vtodo"))
1090                                         ) {
1091                                         strcpy(relevant_partnum, mime_partnum);
1092                                 }
1093                         }
1094                         else if (!strncasecmp(bptr, "from=", 4)) {
1095                                 extract_token(from, bptr, 1, '=', sizeof(from));
1096                         }
1097                         else if ((phase == 0) && (!strncasecmp(bptr, "text", 4))) {
1098                                 phase = 1;
1099                         }
1100                 break;
1101                 case 1:
1102                         if (!IsEmptyStr(bptr)) {
1103                                 if (!strncasecmp(bptr, "Content-type: ", 14)) {
1104                                         safestrncpy(msg4_content_type, &bptr[14], sizeof msg4_content_type);
1105                                         striplt(msg4_content_type);
1106                                 }
1107                                 else if (!strncasecmp(bptr, "Content-transfer-encoding: ", 27)) {
1108                                         safestrncpy(msg4_content_encoding, &bptr[27], sizeof msg4_content_encoding);
1109                                         striplt(msg4_content_type);
1110                                 }
1111                                 else if ((!strncasecmp(bptr, "Content-length: ", 16))) {
1112                                         msg4_content_length = atoi(&bptr[16]);
1113                                 }
1114                                 break;
1115                         }
1116                         else {
1117                                 phase++;
1118                                 
1119                                 if ((msg4_content_length > 0)
1120                                     && ( !strcasecmp(msg4_content_encoding, "7bit"))
1121                                     && ((!strcasecmp(mime_content_type, "text/calendar"))
1122                                         || (!strcasecmp(mime_content_type, "application/ics"))
1123                                         || (!strcasecmp(mime_content_type, "text/vtodo"))
1124                                             )
1125                                         ) 
1126                                 {
1127                                 }
1128                         }
1129                 case 2:
1130                         Data = NewStrBufPlain(NULL, msg4_content_length * 2);
1131                         if (msg4_content_length > 0) {
1132                                 StrBuf_ServGetBLOBBuffered(Data, msg4_content_length);
1133                                 phase ++;
1134                         }
1135                         else {
1136                                 StrBufAppendBuf(Data, Buf, 0);
1137                                 StrBufAppendBufPlain(Data, "\r\n", 1, 0);
1138                         }
1139                 case 3:
1140                         StrBufAppendBuf(Data, Buf, 0);
1141                 }
1142         }
1143         FreeStrBuf(&Buf);
1144
1145         /* If MSG4 didn't give us the part we wanted, but we know that we can find it
1146          * as one of the other MIME parts, attempt to load it now.
1147          */
1148         if ((Data == NULL) && (!IsEmptyStr(relevant_partnum))) {
1149                 Data = load_mimepart(msgnum, relevant_partnum);
1150         }
1151
1152         if (Data != NULL) {
1153                 relevant_source = (char*) ChrPtr(Data);
1154                 process_ical_object(msgnum, unread,
1155                                     from, 
1156                                     relevant_source, 
1157                                     which_kind,
1158                                     CallBack,
1159                                     calv);
1160                 FreeStrBuf (&Data);
1161         }
1162
1163         icalmemory_free_ring();
1164 }
1165
1166 /*
1167  * Display a calendar item
1168  */
1169 void load_calendar_item(message_summary *Msg, int unread, struct calview *c) {
1170         load_ical_object(Msg->msgnum, unread, (-1), display_individual_cal, c, 1);
1171 }
1172
1173 /*
1174  * Display task view
1175  */
1176 void display_task(message_summary *Msg, int unread) {
1177         load_ical_object(Msg->msgnum, unread, ICAL_VTODO_COMPONENT, display_individual_cal, NULL, 0);
1178 }
1179
1180 /*
1181  * Display the editor component for a task
1182  */
1183 void display_edit_task(void) {
1184         long msgnum = 0L;
1185                         
1186         /* Force change the room if we have to */
1187         if (havebstr("taskrm")) {
1188                 gotoroom(sbstr("taskrm"));
1189         }
1190
1191         msgnum = lbstr("msgnum");
1192         if (msgnum > 0L) {
1193                 /* existing task */
1194                 load_ical_object(msgnum, 0,
1195                                  ICAL_VTODO_COMPONENT,
1196                                  display_edit_individual_task,
1197                                  NULL, 0
1198                 );
1199         }
1200         else {
1201                 /* new task */
1202                 display_edit_individual_task(NULL, 0L, "", 0, NULL);
1203         }
1204 }
1205
1206 /*
1207  * save an edited task
1208  */
1209 void save_task(void) {
1210         long msgnum = 0L;
1211         msgnum = lbstr("msgnum");
1212         if (msgnum > 0L) {
1213                 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL, 0);
1214         }
1215         else {
1216                 save_individual_task(NULL, 0L, "", 0, NULL);
1217         }
1218 }
1219
1220 /*
1221  * display the editor component for an event
1222  */
1223 void display_edit_event(void) {
1224         long msgnum = 0L;
1225
1226         msgnum = lbstr("msgnum");
1227         if (msgnum > 0L) {
1228                 /* existing event */
1229                 load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, display_edit_individual_event, NULL, 0);
1230         }
1231         else {
1232                 /* new event */
1233                 display_edit_individual_event(NULL, 0L, "", 0, NULL);
1234         }
1235 }
1236
1237 /*
1238  * save an edited event
1239  */
1240 void save_event(void) {
1241         long msgnum = 0L;
1242
1243         msgnum = lbstr("msgnum");
1244
1245         if (msgnum > 0L) {
1246                 load_ical_object(msgnum, 0, (-1), save_individual_event, NULL, 0);
1247         }
1248         else {
1249                 save_individual_event(NULL, 0L, "", 0, NULL);
1250         }
1251 }
1252
1253
1254
1255
1256
1257 /*
1258  * Anonymous request of freebusy data for a user
1259  */
1260 void do_freebusy(const char *req) {
1261         char who[SIZ];
1262         char buf[SIZ];
1263         int len;
1264         long lines;
1265
1266         extract_token(who, req, 1, ' ', sizeof who);
1267         if (!strncasecmp(who, "/freebusy/", 10)) {
1268                 strcpy(who, &who[10]);
1269         }
1270         unescape_input(who);
1271
1272         len = strlen(who);
1273         if ( (!strcasecmp(&who[len-4], ".vcf"))
1274              || (!strcasecmp(&who[len-4], ".ifb"))
1275              || (!strcasecmp(&who[len-4], ".vfb")) ) {
1276                 who[len-4] = 0;
1277         }
1278
1279         lprintf(9, "freebusy requested for <%s>\n", who);
1280         serv_printf("ICAL freebusy|%s", who);
1281         serv_getln(buf, sizeof buf);
1282
1283         if (buf[0] != '1') {
1284                 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1285                 output_headers(0, 0, 0, 0, 0, 0);
1286                 hprintf("Content-Type: text/plain\r\n");
1287                 wprintf("%s\n", &buf[4]);
1288                 end_burst();
1289                 return;
1290         }
1291
1292         read_server_text(WC->WBuf, &lines);
1293         http_transmit_thing("text/calendar", 0);
1294 }
1295
1296
1297
1298
1299
1300 void 
1301 InitModule_CALENDAR
1302 (void)
1303 {
1304         RegisterPreference("daystart", _("Calendar day view begins at:"), PRF_INT, NULL);
1305         RegisterPreference("dayend", _("Calendar day view ends at:"), PRF_INT, NULL);
1306         RegisterPreference("weekstart", _("Week starts on:"), PRF_INT, NULL);
1307
1308         WebcitAddUrlHandler(HKEY("display_edit_task"), display_edit_task, 0);
1309         WebcitAddUrlHandler(HKEY("save_task"), save_task, 0);
1310         WebcitAddUrlHandler(HKEY("display_edit_event"), display_edit_event, 0);
1311         WebcitAddUrlHandler(HKEY("save_event"), save_event, 0);
1312         WebcitAddUrlHandler(HKEY("respond_to_request"), respond_to_request, 0);
1313         WebcitAddUrlHandler(HKEY("handle_rsvp"), handle_rsvp, 0);
1314 }