]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* mktime() modifies us, we mustn't call it twice on the same data, it doesn't work.
[citadel.git] / webcit / calendar.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup calav Functions which handle calendar objects and their processing/display.
6  * \ingroup Calendaring
7  */
8 /* @{ */
9
10 #include "webcit.h"
11 #include "webserver.h"
12
13 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
14
15 /**
16  * \brief get around non existing types
17  * Handler stubs for builds with no calendar library available
18  * \param part_source dummy pointer to the source
19  * \param msgnum number of the mesage in the db
20  * \param cal_partnum number of the calendar part
21  */
22 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
23
24         wprintf(_("<I>This message contains calendaring/scheduling information,"
25                 " but support for calendars is not available on this "
26                 "particular system.  Please ask your system administrator to "
27                 "install a new version of the Citadel web service with "
28                 "calendaring enabled.</I><br />\n")
29         );
30
31 }
32
33 /**
34  * \brief say we can't display calendar items
35  * \param msgnum number of the mesage in our db
36  */
37 void display_calendar(long msgnum) {
38         wprintf(_("<i>"
39                 "Cannot display calendar item.  You are seeing this error "
40                 "because your WebCit service has not been installed with "
41                 "calendar support.  Please contact your system administrator."
42                 "</i><br />\n"));
43 }
44
45 /**
46  * \brief say we can't display task items
47  * \param msgnum number of the mesage in our db
48  */
49 void display_task(long msgnum) {
50         wprintf(_("<i>"
51                 "Cannot display to-do item.  You are seeing this error "
52                 "because your WebCit service has not been installed with "
53                 "calendar support.  Please contact your system administrator."
54                 "</i><br />\n"));
55 }
56 /** ok, we have calendaring available */
57 #else /* WEBCIT_WITH_CALENDAR_SERVICE */
58
59
60 /******   End of handler stubs.  Everything below this line is real.   ******/
61
62
63
64
65 /**
66  * \brief Process a calendar object
67  * ...at this point it's already been deserialized by cal_process_attachment()
68  * \param cal the calendar object
69  * \param recursion_level call stack depth ??????
70  * \param msgnum number of the mesage in our db
71  * \param cal_partnum of the calendar object ???? 
72  */
73 void cal_process_object(icalcomponent *cal,
74                         int recursion_level,
75                         long msgnum,
76                         char *cal_partnum
77 ) {
78         icalcomponent *c;
79         icalproperty *method = NULL;
80         icalproperty_method the_method = ICAL_METHOD_NONE;
81         icalproperty *p;
82         struct icaltimetype t;
83         time_t tt;
84         char buf[256];
85         char conflict_name[256];
86         char conflict_message[256];
87         int is_update = 0;
88         char divname[32];
89         static int divcount = 0;
90
91         sprintf(divname, "rsvp%04x", ++divcount);
92
93         /** Leading HTML for the display of this object */
94         if (recursion_level == 0) {
95                 wprintf("<div class=\"mimepart\">\n");
96         }
97
98         /** Look for a method */
99         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
100
101         /** See what we need to do with this */
102         if (method != NULL) {
103                 the_method = icalproperty_get_method(method);
104                 char *title;
105
106                 wprintf("<div id=\"%s_title\">", divname);
107                 wprintf("<img src=\"static/calarea_48x.gif\">");
108                 wprintf("<span>");
109                 switch(the_method) {
110                     case ICAL_METHOD_REQUEST:
111                         title = _("Meeting invitation");
112                         break;
113                     case ICAL_METHOD_REPLY:
114                         title = _("Attendee's reply to your invitation");
115                         break;
116                     case ICAL_METHOD_PUBLISH:
117                         title = _("Published event");
118                         break;
119                     default:
120                         title = _("This is an unknown type of calendar item.");
121                         break;
122                 }
123                 wprintf("</span>");
124
125                 wprintf("&nbsp;&nbsp;%s",title);
126                 wprintf("</div>");
127         }
128
129         wprintf("<dl>");
130         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
131         if (p != NULL) {
132                 wprintf("<dt>");
133                 wprintf(_("Summary:"));
134                 wprintf("</dt><dd>");
135                 escputs((char *)icalproperty_get_comment(p));
136                 wprintf("</dd>\n");
137         }
138
139         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
140         if (p != NULL) {
141                 wprintf("<dt>");
142                 wprintf(_("Location:"));
143                 wprintf("</dt><dd>");
144                 escputs((char *)icalproperty_get_comment(p));
145                 wprintf("</dd>\n");
146         }
147
148         /**
149          * Only show start/end times if we're actually looking at the VEVENT
150          * component.  Otherwise it shows bogus dates for things like timezone.
151          */
152         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
153
154                 p = icalcomponent_get_first_property(cal,
155                                                 ICAL_DTSTART_PROPERTY);
156                 if (p != NULL) {
157                         t = icalproperty_get_dtstart(p);
158
159                         if (t.is_date) {
160                                 struct tm d_tm;
161                                 char d_str[32];
162                                 memset(&d_tm, 0, sizeof d_tm);
163                                 d_tm.tm_year = t.year - 1900;
164                                 d_tm.tm_mon = t.month - 1;
165                                 d_tm.tm_mday = t.day;
166                                 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
167                                 wprintf("<dt>");
168                                 wprintf(_("Date:"));
169                                 wprintf("</dt><dd>%s</dd>", d_str);
170                         }
171                         else {
172                                 tt = icaltime_as_timet(t);
173                                 fmt_date(buf, tt, 0);
174                                 wprintf("<dt>");
175                                 wprintf(_("Starting date/time:"));
176                                 wprintf("</dt><dd>%s</dd>", buf);
177                         }
178                 }
179         
180                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
181                 if (p != NULL) {
182                         t = icalproperty_get_dtend(p);
183                         tt = icaltime_as_timet(t);
184                         fmt_date(buf, tt, 0);
185                         wprintf("<dt>");
186                         wprintf(_("Ending date/time:"));
187                         wprintf("</dt><dd>%s</dd>", buf);
188                 }
189
190         }
191
192         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
193         if (p != NULL) {
194                 wprintf("<dt>");
195                 wprintf(_("Description:"));
196                 wprintf("</dt><dd>");
197                 escputs((char *)icalproperty_get_comment(p));
198                 wprintf("</dd>\n");
199         }
200
201         /** If the component has attendees, iterate through them. */
202         for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
203                 wprintf("<dt>");
204                 wprintf(_("Attendee:"));
205                 wprintf("</dt><dd>");
206                 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
207                 if (!strncasecmp(buf, "MAILTO:", 7)) {
208
209                         /** screen name or email address */
210                         strcpy(buf, &buf[7]);
211                         striplt(buf);
212                         escputs(buf);
213                         wprintf(" ");
214
215                         /** participant status */
216                         partstat_as_string(buf, p);
217                         escputs(buf);
218                 }
219                 wprintf("</dd>\n");
220         }
221
222         /** If the component has subcomponents, recurse through them. */
223         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
224             (c != 0);
225             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
226                 /* Recursively process subcomponent */
227                 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
228         }
229
230         /** If this is a REQUEST, display conflicts and buttons */
231         if (the_method == ICAL_METHOD_REQUEST) {
232
233                 /* Check for conflicts */
234                 lprintf(9, "Checking server calendar for conflicts...\n");
235                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
236                 serv_getln(buf, sizeof buf);
237                 if (buf[0] == '1') {
238                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
239                                 extract_token(conflict_name, buf, 3, '|', sizeof conflict_name);
240                                 is_update = extract_int(buf, 4);
241
242                                 if (is_update) {
243                                         snprintf(conflict_message, sizeof conflict_message,
244                                                 _("This is an update of '%s' which is already in your calendar."), conflict_name);
245                                 }
246                                 else {
247                                         snprintf(conflict_message, sizeof conflict_message,
248                                                 _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
249                                 }
250
251                                 wprintf("<dt>%s",
252                                         (is_update ?
253                                                 _("Update:") :
254                                                 _("CONFLICT:")
255                                         )
256                                 );
257                                 wprintf("</dt><dd>");
258                                 escputs(conflict_message);
259                                 wprintf("</dd>\n");
260                         }
261                 }
262                 lprintf(9, "...done.\n");
263
264                 wprintf("</dl>");
265
266                 /** Display the Accept/Decline buttons */
267                 wprintf("<p id=\"%s_question\" class=\"buttons\">"
268                         "%s "
269                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
270                         "<span> | </span>"
271                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
272                         "<span> | </span>"
273                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
274                         "</p>\n",
275                         divname,
276                         _("How would you like to respond to this invitation?"),
277                         divname, divname, msgnum, cal_partnum, _("Accept"),
278                         divname, divname, msgnum, cal_partnum, _("Tentative"),
279                         divname, divname, msgnum, cal_partnum, _("Decline")
280                 );
281
282         }
283
284         /** If this is a REPLY, display update button */
285         if (the_method == ICAL_METHOD_REPLY) {
286
287                 /** \todo  In the future, if we want to validate this object before \
288                  * continuing, we can do it this way:
289                 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
290                 serv_getln(buf, sizeof buf);
291                 }
292                  ***********/
293
294                 /** Display the update buttons */
295                 wprintf("<p id=\"%s_question\" class=\"buttons\">"
296                         "%s"
297                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
298                         "<span> | </span>"
299                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
300                         "</p>\n",
301                         divname,
302                         _("Click <i>Update</i> to accept this reply and update your calendar."),
303                         divname, divname, msgnum, cal_partnum, _("Update"),
304                         divname, divname, msgnum, cal_partnum, _("Ignore")
305                 );
306
307         }
308
309         /** Trailing HTML for the display of this object */
310         if (recursion_level == 0) {
311                 wprintf("<p>&nbsp;</p></div>\n");
312         }
313 }
314
315
316 /**
317  * \brief process calendar mail atachment
318  * Deserialize a calendar object in a message so it can be processed.
319  * (This is the main entry point for these things)
320  * \param part_source the part of the message we want to parse
321  * \param msgnum number of the mesage in our db
322  * \param cal_partnum the number of the calendar item
323  */
324 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
325         icalcomponent *cal;
326
327         cal = icalcomponent_new_from_string(part_source);
328
329         if (cal == NULL) {
330                 wprintf(_("There was an error parsing this calendar item."));
331                 wprintf("<br />\n");
332                 return;
333         }
334
335         ical_dezonify(cal);
336         cal_process_object(cal, 0, msgnum, cal_partnum);
337
338         /* Free the memory we obtained from libical's constructor */
339         icalcomponent_free(cal);
340 }
341
342
343
344
345 /**
346  * \brief accept/decline meeting
347  * Respond to a meeting request
348  */
349 void respond_to_request(void) {
350         char buf[1024];
351
352         begin_ajax_response();
353
354         serv_printf("ICAL respond|%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"), "accept")) {
364                         wprintf(_("You have accepted this meeting invitation.  "
365                                 "It has been entered into your calendar.")
366                         );
367                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
368                         wprintf(_("You have tentatively accepted this meeting invitation.  "
369                                 "It has been 'pencilled in' to your calendar.")
370                         );
371                 } else if (!strcasecmp(bstr("sc"), "decline")) {
372                         wprintf(_("You have declined this meeting invitation.  "
373                                 "It has <b>not</b> been entered into your calendar.")
374                         );
375                 }
376                 wprintf(" ");
377                 wprintf(_("A reply has been sent to the meeting organizer."));
378                 wprintf("</span>");
379         } else {
380                 wprintf("<img align=\"center\" src=\"static/error.gif\"><span>");
381                 wprintf("%s\n", &buf[4]);
382                 wprintf("</span>");
383         }
384
385         end_ajax_response();
386 }
387
388
389
390 /**
391  * \brief Handle an incoming RSVP
392  */
393 void handle_rsvp(void) {
394         char buf[1024];
395
396         begin_ajax_response();
397
398         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
399                 bstr("msgnum"),
400                 bstr("cal_partnum"),
401                 bstr("sc")
402         );
403         serv_getln(buf, sizeof buf);
404
405         if (buf[0] == '2') {
406                 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
407                 if (!strcasecmp(bstr("sc"), "update")) {
408                         wprintf(_("Your calendar has been updated to reflect this RSVP."));
409                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
410                         wprintf(_("You have chosen to ignore this RSVP. "
411                                 "Your calendar has <b>not</b> been updated.")
412                         );
413                 }
414                 wprintf("</span>");
415         } else {
416                 wprintf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
417                 wprintf("</span>");
418         }
419
420         end_ajax_response();
421
422 }
423
424
425
426 /*@}*/
427 /*-----------------------------------------------------------------------**/
428
429
430
431 /**
432  * \defgroup MsgDisplayHandlers Display handlers for message reading 
433  * \ingroup Calendaring
434  */
435
436 /*@{*/
437
438
439
440 /**
441  * \brief get items, keep them.
442  * If we're reading calendar items, just store them for now.  We have to
443  * sort and re-output them later when we draw the calendar.
444  * \param cal Our calendar to process
445  * \param msgnum number of the mesage in our db
446  */
447 void display_individual_cal(icalcomponent *cal, long msgnum) 
448 {
449         icalproperty *ps = NULL;
450         struct icaltimetype t;
451         struct wcsession *WCC;
452         struct disp_cal *Cal;
453         struct tm event;
454         struct tm event_hr;
455         time_t event_ts;
456
457         WCC = WC;
458
459         WCC->num_cal += 1;
460
461         WCC->disp_cal = realloc(WCC->disp_cal,
462                         (sizeof(struct disp_cal) * WCC->num_cal) );
463
464         Cal = &WCC->disp_cal[WCC->num_cal - 1];
465         Cal->cal = icalcomponent_new_clone(cal);
466
467         Cal->cal_msgnum = msgnum;
468
469         //! Precalculate some Values we can use for easy comparison later.
470         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
471         if (ps != NULL) {
472                 t = icalproperty_get_dtstart(ps);
473                 event_ts = icaltime_as_timet(t);
474                 if (t.is_date) { //! calculate whether we are a day event.
475                         Cal->start_hour = -1;
476                         Cal->end_hour = -1;
477                         Cal->end_day = -1;
478                         gmtime_r(&event_ts, &event); 
479                         event.tm_sec = 0;
480                         event.tm_min = 0;
481                         event.tm_hour = 0; 
482                         Cal->start_day = mktime (&event);
483                 }
484                 else { //! Precalc start day and start day + hour
485                         localtime_r(&event_ts, &event);
486                         event.tm_sec = 0;
487                         event.tm_min = 0;
488                         memcpy (&event_hr, &event, sizeof(struct tm));
489                         Cal->start_hour = mktime (&event_hr);
490                         event.tm_hour = 0;                      
491                         Cal->start_day = mktime (&event);
492         
493                         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
494                         if (ps != NULL) { //!  Precalc the end day and end day + hour
495                                 t = icalproperty_get_dtstart(ps);
496                                 event_ts = icaltime_as_timet(t);
497                                 localtime_r(&event_ts, &event);
498                                 event.tm_sec = 0;
499                                 event.tm_min = 0;
500                                 memcpy (&event_hr, &event, sizeof(struct tm));
501                                 Cal->end_hour = mktime (&event);
502                                 event.tm_hour = 0;                      
503                                 Cal->end_day = mktime (&event);
504                         }
505                         else
506                         {
507                                 Cal->end_hour = -1;
508                                 Cal->end_day = -1;
509                         }       
510                 }
511
512
513         }
514         Cal->multi_day_event = Cal->start_day != Cal->end_day;
515 }
516
517
518
519 /*
520  * \brief edit a task
521  * Display a task by itself (for editing)
522  * \param supplied_vtodo the todo item we want to edit
523  * \param msgnum number of the mesage in our db
524  */
525 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
526         icalcomponent *vtodo;
527         icalproperty *p;
528         struct icaltimetype t;
529         time_t now;
530         int created_new_vtodo = 0;
531
532         now = time(NULL);
533
534         if (supplied_vtodo != NULL) {
535                 vtodo = supplied_vtodo;
536
537                 /**
538                  * If we're looking at a fully encapsulated VCALENDAR
539                  * rather than a VTODO component, attempt to use the first
540                  * relevant VTODO subcomponent.  If there is none, the
541                  * NULL returned by icalcomponent_get_first_component() will
542                  * tell the next iteration of this function to create a
543                  * new one.
544                  */
545                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
546                         display_edit_individual_task(
547                                 icalcomponent_get_first_component(
548                                         vtodo, ICAL_VTODO_COMPONENT
549                                 ), msgnum
550                         );
551                         return;
552                 }
553         }
554         else {
555                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
556                 created_new_vtodo = 1;
557         }
558
559         output_headers(1, 1, 2, 0, 0, 0);
560         wprintf("<div id=\"banner\">\n");
561         wprintf("<img src=\"static/taskmanag_48x.gif\">");
562         wprintf("<h1>");
563         wprintf(_("Edit task"));
564         wprintf("</h1>");
565         wprintf("</div>\n");
566
567         wprintf("<div id=\"content\" class=\"service\">\n");
568
569         wprintf("<div class=\"fix_scrollbar_bug\">"
570                 "<table class=\"calendar_background\"><tr><td>");
571         
572         wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
573         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
574         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
575                 msgnum);
576
577         wprintf("<TABLE border=0>\n");
578
579         wprintf("<TR><TD>");
580         wprintf(_("Summary:"));
581         wprintf("</TD><TD>"
582                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
583                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
584         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
585         if (p != NULL) {
586                 escputs((char *)icalproperty_get_comment(p));
587         }
588         wprintf("\"></TD></TR>\n");
589
590         wprintf("<TR><TD>");
591         wprintf(_("Start date:"));
592         wprintf("</TD><TD>");
593         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
594         if (p != NULL) {
595                 t = icalproperty_get_dtstart(p);
596         }
597         else {
598                 t = icaltime_from_timet(now, 0);
599         }
600         display_icaltimetype_as_webform(&t, "dtstart");
601         wprintf("</TD></TR>\n");
602
603         wprintf("<TR><TD>");
604         wprintf(_("Due date:"));
605         wprintf("</TD><TD>");
606         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
607         if (p != NULL) {
608                 t = icalproperty_get_due(p);
609         }
610         else {
611                 t = icaltime_from_timet(now, 0);
612         }
613         display_icaltimetype_as_webform(&t, "due");
614         wprintf("</TD></TR>\n");
615         wprintf("<TR><TD>");
616         wprintf(_("Description:"));
617         wprintf("</TD><TD>");
618         wprintf("<TEXTAREA NAME=\"description\" wrap=soft "
619                 "ROWS=10 COLS=80 WIDTH=80>\n"
620         );
621         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
622         if (p != NULL) {
623                 escputs((char *)icalproperty_get_comment(p));
624         }
625         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
626
627         wprintf("<CENTER>"
628                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
629                 "&nbsp;&nbsp;"
630                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
631                 "&nbsp;&nbsp;"
632                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
633                 "</CENTER>\n",
634                 _("Save"),
635                 _("Delete"),
636                 _("Cancel")
637         );
638
639         wprintf("</FORM>\n");
640
641         wprintf("</td></tr></table></div>\n");
642         wDumpContent(1);
643
644         if (created_new_vtodo) {
645                 icalcomponent_free(vtodo);
646         }
647 }
648
649 /*
650  * \brief Save an edited task
651  * \param supplied_vtodo the task to save
652  * \param msgnum number of the mesage in our db
653  */
654 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
655         char buf[SIZ];
656         int delete_existing = 0;
657         icalproperty *prop;
658         icalcomponent *vtodo, *encaps;
659         int created_new_vtodo = 0;
660         int i;
661         int sequence = 0;
662         struct icaltimetype t;
663
664         if (supplied_vtodo != NULL) {
665                 vtodo = supplied_vtodo;
666                 /**
667                  * If we're looking at a fully encapsulated VCALENDAR
668                  * rather than a VTODO component, attempt to use the first
669                  * relevant VTODO subcomponent.  If there is none, the
670                  * NULL returned by icalcomponent_get_first_component() will
671                  * tell the next iteration of this function to create a
672                  * new one.
673                  */
674                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
675                         save_individual_task(
676                                 icalcomponent_get_first_component(
677                                         vtodo, ICAL_VTODO_COMPONENT
678                                 ), msgnum
679                         );
680                         return;
681                 }
682         }
683         else {
684                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
685                 created_new_vtodo = 1;
686         }
687
688         if (!IsEmptyStr(bstr("save_button"))) {
689
690                 /** Replace values in the component with ones from the form */
691
692                 while (prop = icalcomponent_get_first_property(vtodo,
693                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
694                         icalcomponent_remove_property(vtodo, prop);
695                         icalproperty_free(prop);
696                 }
697                 if (!IsEmptyStr(bstr("summary"))) {
698         
699                         icalcomponent_add_property(vtodo,
700                                         icalproperty_new_summary(bstr("summary")));
701                 } else {
702                         icalcomponent_add_property(vtodo,
703                                         icalproperty_new_summary("Untitled Task"));
704                 }
705         
706                 while (prop = icalcomponent_get_first_property(vtodo,
707                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
708                         icalcomponent_remove_property(vtodo, prop);
709                         icalproperty_free(prop);
710                 }
711                 icalcomponent_add_property(vtodo,
712                         icalproperty_new_description(bstr("description")));
713         
714                 while (prop = icalcomponent_get_first_property(vtodo,
715                       ICAL_DTSTART_PROPERTY), prop != NULL) {
716                         icalcomponent_remove_property(vtodo, prop);
717                         icalproperty_free(prop);
718                 }
719                 icaltime_from_webform(&t, "dtstart");
720                 icalcomponent_add_property(vtodo,
721                         icalproperty_new_dtstart(t)
722                 );
723         
724                 while (prop = icalcomponent_get_first_property(vtodo,
725                       ICAL_DUE_PROPERTY), prop != NULL) {
726                         icalcomponent_remove_property(vtodo, prop);
727                         icalproperty_free(prop);
728                 }
729                 icaltime_from_webform(&t, "due");
730                 icalcomponent_add_property(vtodo,
731                         icalproperty_new_due(t)
732                 );
733
734                 /** Give this task a UID if it doesn't have one. */
735                 lprintf(9, "Give this task a UID if it doesn't have one.\n");
736                 if (icalcomponent_get_first_property(vtodo,
737                    ICAL_UID_PROPERTY) == NULL) {
738                         generate_uuid(buf);
739                         icalcomponent_add_property(vtodo,
740                                 icalproperty_new_uid(buf)
741                         );
742                 }
743
744                 /** Increment the sequence ID */
745                 lprintf(9, "Increment the sequence ID\n");
746                 while (prop = icalcomponent_get_first_property(vtodo,
747                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
748                         i = icalproperty_get_sequence(prop);
749                         lprintf(9, "Sequence was %d\n", i);
750                         if (i > sequence) sequence = i;
751                         icalcomponent_remove_property(vtodo, prop);
752                         icalproperty_free(prop);
753                 }
754                 ++sequence;
755                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
756                 icalcomponent_add_property(vtodo,
757                         icalproperty_new_sequence(sequence)
758                 );
759
760                 /**
761                  * Encapsulate event into full VCALENDAR component.  Clone it first,
762                  * for two reasons: one, it's easier to just free the whole thing
763                  * when we're done instead of unbundling, but more importantly, we
764                  * can't encapsulate something that may already be encapsulated
765                  * somewhere else.
766                  */
767                 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
768                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
769
770                 /* Serialize it and save it to the message base */
771                 serv_puts("ENT0 1|||4");
772                 serv_getln(buf, sizeof buf);
773                 if (buf[0] == '4') {
774                         serv_puts("Content-type: text/calendar");
775                         serv_puts("");
776                         serv_puts(icalcomponent_as_ical_string(encaps));
777                         serv_puts("000");
778
779                         /**
780                          * Probably not necessary; the server will see the UID
781                          * of the object and delete the old one anyway, but
782                          * just in case...
783                          */
784                         delete_existing = 1;
785                 }
786                 icalcomponent_free(encaps);
787         }
788
789         /**
790          * If the user clicked 'Delete' then explicitly delete the message.
791          */
792         if (!IsEmptyStr(bstr("delete_button"))) {
793                 delete_existing = 1;
794         }
795
796         if ( (delete_existing) && (msgnum > 0L) ) {
797                 serv_printf("DELE %ld", atol(bstr("msgnum")));
798                 serv_getln(buf, sizeof buf);
799         }
800
801         if (created_new_vtodo) {
802                 icalcomponent_free(vtodo);
803         }
804
805         /** Go back to the task list */
806         readloop("readfwd");
807 }
808
809
810
811 /**
812  * \brief generic item handler
813  * Code common to all display handlers.  Given a message number and a MIME
814  * type, we load the message and hunt for that MIME type.  If found, we load
815  * the relevant part, deserialize it into a libical component, filter it for
816  * the requested object type, and feed it to the specified handler.
817  * \param mimetype mimetyp of our object
818  * \param which_kind sort of ical type
819  * \param msgnum number of the mesage in our db
820  * \param callback a funcion \todo
821  *
822  */
823 void display_using_handler(long msgnum,
824                         char *mimetype,
825                         icalcomponent_kind which_kind,
826                         void (*callback)(icalcomponent *, long)
827         ) {
828         char buf[1024];
829         char mime_partnum[256];
830         char mime_filename[256];
831         char mime_content_type[256];
832         char mime_disposition[256];
833         int mime_length;
834         char relevant_partnum[256];
835         char *relevant_source = NULL;
836         icalcomponent *cal, *c;
837
838         relevant_partnum[0] = '\0';
839         sprintf(buf, "MSG0 %ld|0", msgnum);     /* unfortunately we need the mime headers */
840         serv_puts(buf);
841         serv_getln(buf, sizeof buf);
842         if (buf[0] != '1') return;
843
844         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
845                 if (!strncasecmp(buf, "part=", 5)) {
846                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
847                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
848                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
849                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
850                         mime_length = extract_int(&buf[5], 5);
851
852                         if (!strcasecmp(mime_content_type, "text/calendar")) {
853                                 strcpy(relevant_partnum, mime_partnum);
854                         }
855                         else if (!strcasecmp(mime_content_type, "text/vtodo")) {
856                                 strcpy(relevant_partnum, mime_partnum);
857                         }
858
859                 }
860         }
861
862         if (!IsEmptyStr(relevant_partnum)) {
863                 relevant_source = load_mimepart(msgnum, relevant_partnum);
864                 if (relevant_source != NULL) {
865
866                         cal = icalcomponent_new_from_string(relevant_source);
867                         if (cal != NULL) {
868
869                                 ical_dezonify(cal);
870
871                                 /** Simple components of desired type */
872                                 if (icalcomponent_isa(cal) == which_kind) {
873                                         callback(cal, msgnum);
874                                 }
875
876                                 /** Subcomponents of desired type */
877                                 for (c = icalcomponent_get_first_component(cal,
878                                     which_kind);
879                                     (c != 0);
880                                     c = icalcomponent_get_next_component(cal,
881                                     which_kind)) {
882                                         callback(c, msgnum);
883                                 }
884                                 icalcomponent_free(cal);
885                         }
886                         free(relevant_source);
887                 }
888         }
889
890 }
891
892 /**
893  * \brief display whole calendar
894  * \param msgnum number of the mesage in our db
895  */
896 void display_calendar(long msgnum) {
897         display_using_handler(msgnum, "text/calendar",
898                                 ICAL_VEVENT_COMPONENT,
899                                 display_individual_cal);
900 }
901
902 /**
903  * \brief display whole taksview
904  * \param msgnum number of the mesage in our db
905  */
906 void display_task(long msgnum) {
907         display_using_handler(msgnum, "text/calendar",
908                                 ICAL_VTODO_COMPONENT,
909                                 display_individual_cal);
910 }
911
912 /**
913  * \brief display the editor component for a task
914  */
915 void display_edit_task(void) {
916         long msgnum = 0L;
917
918         /** Force change the room if we have to */
919         if (!IsEmptyStr(bstr("taskrm"))) {
920                 gotoroom(bstr("taskrm"));
921         }
922
923         msgnum = atol(bstr("msgnum"));
924         if (msgnum > 0L) {
925                 /** existing task */
926                 display_using_handler(msgnum, "text/calendar",
927                                 ICAL_VTODO_COMPONENT,
928                                 display_edit_individual_task);
929         }
930         else {
931                 /** new task */
932                 display_edit_individual_task(NULL, 0L);
933         }
934 }
935
936 /**
937  *\brief save an edited task
938  */
939 void save_task(void) {
940         long msgnum = 0L;
941
942         msgnum = atol(bstr("msgnum"));
943         if (msgnum > 0L) {
944                 display_using_handler(msgnum, "text/calendar",
945                                 ICAL_VTODO_COMPONENT,
946                                 save_individual_task);
947         }
948         else {
949                 save_individual_task(NULL, 0L);
950         }
951 }
952
953 /**
954  * \brief display the editor component for an event
955  */
956 void display_edit_event(void) {
957         long msgnum = 0L;
958
959         msgnum = atol(bstr("msgnum"));
960         if (msgnum > 0L) {
961                 /* existing event */
962                 display_using_handler(msgnum, "text/calendar",
963                                 ICAL_VEVENT_COMPONENT,
964                                 display_edit_individual_event);
965         }
966         else {
967                 /* new event */
968                 display_edit_individual_event(NULL, 0L);
969         }
970 }
971
972 /**
973  * \brief save an edited event
974  */
975 void save_event(void) {
976         long msgnum = 0L;
977
978         msgnum = atol(bstr("msgnum"));
979
980         if (msgnum > 0L) {
981                 display_using_handler(msgnum, "text/calendar",
982                                 ICAL_VEVENT_COMPONENT,
983                                 save_individual_event);
984         }
985         else {
986                 save_individual_event(NULL, 0L);
987         }
988 }
989
990
991
992
993
994 /**
995  * \brief freebusy display (for client software)
996  * \param req dunno. ?????
997  */
998 void do_freebusy(char *req) {
999         char who[SIZ];
1000         char buf[SIZ];
1001         char *fb;
1002         int len;
1003
1004         extract_token(who, req, 1, ' ', sizeof who);
1005         if (!strncasecmp(who, "/freebusy/", 10)) {
1006                 strcpy(who, &who[10]);
1007         }
1008         unescape_input(who);
1009
1010         len = strlen(who);
1011         if ( (!strcasecmp(&who[len-4], ".vcf"))
1012            || (!strcasecmp(&who[len-4], ".ifb"))
1013            || (!strcasecmp(&who[len-4], ".vfb")) ) {
1014                 who[len-4] = 0;
1015         }
1016
1017         lprintf(9, "freebusy requested for <%s>\n", who);
1018         serv_printf("ICAL freebusy|%s", who);
1019         serv_getln(buf, sizeof buf);
1020
1021         if (buf[0] != '1') {
1022                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
1023                 output_headers(0, 0, 0, 0, 0, 0);
1024                 wprintf("Content-Type: text/plain\r\n");
1025                 wprintf("\r\n");
1026                 wprintf("%s\n", &buf[4]);
1027                 return;
1028         }
1029
1030         fb = read_server_text();
1031         http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
1032         free(fb);
1033 }
1034
1035
1036
1037 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */
1038
1039
1040 /*@}*/