HUGE PATCH. This moves all of mime_parser.c and all
[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                                 webcit_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                         webcit_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\">"
268                         "%s "
269                         "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
270                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
271                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
272                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
273                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
274                         "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
275                         "</span></p>\n",
276                         divname,
277                         _("How would you like to respond to this invitation?"),
278                         divname, divname, msgnum, cal_partnum, _("Accept"),
279                         divname, divname, msgnum, cal_partnum, _("Tentative"),
280                         divname, divname, msgnum, cal_partnum, _("Decline")
281                 );
282
283         }
284
285         /** If this is a REPLY, display update button */
286         if (the_method == ICAL_METHOD_REPLY) {
287
288                 /** \todo  In the future, if we want to validate this object before \
289                  * continuing, we can do it this way:
290                 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
291                 serv_getln(buf, sizeof buf);
292                 }
293                  ***********/
294
295                 /** Display the update buttons */
296                 wprintf("<p id=\"%s_question\" >"
297                         "%s "
298                         "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
299                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
300                         "</span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
301                         "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
302                         "</span></p>\n",
303                         divname,
304                         _("Click <i>Update</i> to accept this reply and update your calendar."),
305                         divname, divname, msgnum, cal_partnum, _("Update"),
306                         divname, divname, msgnum, cal_partnum, _("Ignore")
307                 );
308
309         }
310
311         /** Trailing HTML for the display of this object */
312         if (recursion_level == 0) {
313                 wprintf("<p>&nbsp;</p></div>\n");
314         }
315 }
316
317
318 /**
319  * \brief process calendar mail atachment
320  * Deserialize a calendar object in a message so it can be processed.
321  * (This is the main entry point for these things)
322  * \param part_source the part of the message we want to parse
323  * \param msgnum number of the mesage in our db
324  * \param cal_partnum the number of the calendar item
325  */
326 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
327         icalcomponent *cal;
328
329         cal = icalcomponent_new_from_string(part_source);
330
331         if (cal == NULL) {
332                 wprintf(_("There was an error parsing this calendar item."));
333                 wprintf("<br />\n");
334                 return;
335         }
336
337         ical_dezonify(cal);
338         cal_process_object(cal, 0, msgnum, cal_partnum);
339
340         /* Free the memory we obtained from libical's constructor */
341         icalcomponent_free(cal);
342 }
343
344
345
346
347 /**
348  * \brief accept/decline meeting
349  * Respond to a meeting request
350  */
351 void respond_to_request(void) {
352         char buf[1024];
353
354         begin_ajax_response();
355
356         serv_printf("ICAL respond|%s|%s|%s|",
357                 bstr("msgnum"),
358                 bstr("cal_partnum"),
359                 bstr("sc")
360         );
361         serv_getln(buf, sizeof buf);
362
363         if (buf[0] == '2') {
364                 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
365                 if (!strcasecmp(bstr("sc"), "accept")) {
366                         wprintf(_("You have accepted this meeting invitation.  "
367                                 "It has been entered into your calendar.")
368                         );
369                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
370                         wprintf(_("You have tentatively accepted this meeting invitation.  "
371                                 "It has been 'pencilled in' to your calendar.")
372                         );
373                 } else if (!strcasecmp(bstr("sc"), "decline")) {
374                         wprintf(_("You have declined this meeting invitation.  "
375                                 "It has <b>not</b> been entered into your calendar.")
376                         );
377                 }
378                 wprintf(" ");
379                 wprintf(_("A reply has been sent to the meeting organizer."));
380                 wprintf("</span>");
381         } else {
382                 wprintf("<img align=\"center\" src=\"static/error.gif\"><span>");
383                 wprintf("%s\n", &buf[4]);
384                 wprintf("</span>");
385         }
386
387         end_ajax_response();
388 }
389
390
391
392 /**
393  * \brief Handle an incoming RSVP
394  */
395 void handle_rsvp(void) {
396         char buf[1024];
397
398         begin_ajax_response();
399
400         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
401                 bstr("msgnum"),
402                 bstr("cal_partnum"),
403                 bstr("sc")
404         );
405         serv_getln(buf, sizeof buf);
406
407         if (buf[0] == '2') {
408                 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
409                 if (!strcasecmp(bstr("sc"), "update")) {
410                         wprintf(_("Your calendar has been updated to reflect this RSVP."));
411                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
412                         wprintf(_("You have chosen to ignore this RSVP. "
413                                 "Your calendar has <b>not</b> been updated.")
414                         );
415                 }
416                 wprintf("</span>");
417         } else {
418                 wprintf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
419                 wprintf("</span>");
420         }
421
422         end_ajax_response();
423
424 }
425
426
427
428 /*@}*/
429 /*-----------------------------------------------------------------------**/
430
431
432
433 /**
434  * \defgroup MsgDisplayHandlers Display handlers for message reading 
435  * \ingroup Calendaring
436  */
437
438 /*@{*/
439
440
441
442 /**
443  * \brief get items, keep them.
444  * If we're reading calendar items, just store them for now.  We have to
445  * sort and re-output them later when we draw the calendar.
446  * \param cal Our calendar to process
447  * \param msgnum number of the mesage in our db
448  */
449 void display_individual_cal(icalcomponent *cal, long msgnum)
450 {
451         struct wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */
452
453         WCC->num_cal += 1;
454         WCC->disp_cal = realloc(WC->disp_cal, (sizeof(struct disp_cal) * WCC->num_cal) );
455         WCC->disp_cal[WCC->num_cal - 1].cal = icalcomponent_new_clone(cal);
456         ical_dezonify(WCC->disp_cal[WCC->num_cal - 1].cal);
457         WCC->disp_cal[WCC->num_cal - 1].cal_msgnum = msgnum;
458 }
459
460
461
462 /*
463  * \brief edit a task
464  * Display a task by itself (for editing)
465  * \param supplied_vtodo the todo item we want to edit
466  * \param msgnum number of the mesage in our db
467  */
468 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
469         icalcomponent *vtodo;
470         icalproperty *p;
471         struct icaltimetype t;
472         time_t now;
473         int created_new_vtodo = 0;
474
475         now = time(NULL);
476
477         if (supplied_vtodo != NULL) {
478                 vtodo = supplied_vtodo;
479
480                 /**
481                  * If we're looking at a fully encapsulated VCALENDAR
482                  * rather than a VTODO component, attempt to use the first
483                  * relevant VTODO subcomponent.  If there is none, the
484                  * NULL returned by icalcomponent_get_first_component() will
485                  * tell the next iteration of this function to create a
486                  * new one.
487                  */
488                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
489                         display_edit_individual_task(
490                                 icalcomponent_get_first_component(
491                                         vtodo, ICAL_VTODO_COMPONENT
492                                 ), msgnum
493                         );
494                         return;
495                 }
496         }
497         else {
498                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
499                 created_new_vtodo = 1;
500         }
501
502         output_headers(1, 1, 2, 0, 0, 0);
503         wprintf("<div id=\"banner\">\n");
504         wprintf("<img src=\"static/taskmanag_48x.gif\">");
505         wprintf("<h1>");
506         wprintf(_("Edit task"));
507         wprintf("</h1>");
508         wprintf("</div>\n");
509
510         wprintf("<div id=\"content\" class=\"service\">\n");
511
512         wprintf("<div class=\"fix_scrollbar_bug\">"
513                 "<table class=\"calendar_background\"><tr><td>");
514         
515         wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
516         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
517         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
518                 msgnum);
519
520         wprintf("<TABLE border=0>\n");
521
522         wprintf("<TR><TD>");
523         wprintf(_("Summary:"));
524         wprintf("</TD><TD>"
525                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
526                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
527         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
528         if (p != NULL) {
529                 escputs((char *)icalproperty_get_comment(p));
530         }
531         wprintf("\"></TD></TR>\n");
532
533         wprintf("<TR><TD>");
534         wprintf(_("Start date:"));
535         wprintf("</TD><TD>");
536         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
537         if (p != NULL) {
538                 t = icalproperty_get_dtstart(p);
539         }
540         else {
541                 t = icaltime_from_timet(now, 0);
542         }
543         display_icaltimetype_as_webform(&t, "dtstart");
544         wprintf("</TD></TR>\n");
545
546         wprintf("<TR><TD>");
547         wprintf(_("Due date:"));
548         wprintf("</TD><TD>");
549         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
550         if (p != NULL) {
551                 t = icalproperty_get_due(p);
552         }
553         else {
554                 t = icaltime_from_timet(now, 0);
555         }
556         display_icaltimetype_as_webform(&t, "due");
557         wprintf("</TD></TR>\n");
558         wprintf("<TR><TD>");
559         wprintf(_("Description:"));
560         wprintf("</TD><TD>");
561         wprintf("<TEXTAREA NAME=\"description\" wrap=soft "
562                 "ROWS=10 COLS=80 WIDTH=80>\n"
563         );
564         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
565         if (p != NULL) {
566                 escputs((char *)icalproperty_get_comment(p));
567         }
568         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
569
570         wprintf("<CENTER>"
571                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
572                 "&nbsp;&nbsp;"
573                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
574                 "&nbsp;&nbsp;"
575                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
576                 "</CENTER>\n",
577                 _("Save"),
578                 _("Delete"),
579                 _("Cancel")
580         );
581
582         wprintf("</FORM>\n");
583
584         wprintf("</td></tr></table></div>\n");
585         wDumpContent(1);
586
587         if (created_new_vtodo) {
588                 icalcomponent_free(vtodo);
589         }
590 }
591
592 /*
593  * \brief Save an edited task
594  * \param supplied_vtodo the task to save
595  * \param msgnum number of the mesage in our db
596  */
597 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
598         char buf[SIZ];
599         int delete_existing = 0;
600         icalproperty *prop;
601         icalcomponent *vtodo, *encaps;
602         int created_new_vtodo = 0;
603         int i;
604         int sequence = 0;
605         struct icaltimetype t;
606
607         if (supplied_vtodo != NULL) {
608                 vtodo = supplied_vtodo;
609                 /**
610                  * If we're looking at a fully encapsulated VCALENDAR
611                  * rather than a VTODO component, attempt to use the first
612                  * relevant VTODO subcomponent.  If there is none, the
613                  * NULL returned by icalcomponent_get_first_component() will
614                  * tell the next iteration of this function to create a
615                  * new one.
616                  */
617                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
618                         save_individual_task(
619                                 icalcomponent_get_first_component(
620                                         vtodo, ICAL_VTODO_COMPONENT
621                                 ), msgnum
622                         );
623                         return;
624                 }
625         }
626         else {
627                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
628                 created_new_vtodo = 1;
629         }
630
631         if (!IsEmptyStr(bstr("save_button"))) {
632
633                 /** Replace values in the component with ones from the form */
634
635                 while (prop = icalcomponent_get_first_property(vtodo,
636                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
637                         icalcomponent_remove_property(vtodo, prop);
638                         icalproperty_free(prop);
639                 }
640                 if (!IsEmptyStr(bstr("summary"))) {
641         
642                         icalcomponent_add_property(vtodo,
643                                         icalproperty_new_summary(bstr("summary")));
644                 } else {
645                         icalcomponent_add_property(vtodo,
646                                         icalproperty_new_summary("Untitled Task"));
647                 }
648         
649                 while (prop = icalcomponent_get_first_property(vtodo,
650                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
651                         icalcomponent_remove_property(vtodo, prop);
652                         icalproperty_free(prop);
653                 }
654                 icalcomponent_add_property(vtodo,
655                         icalproperty_new_description(bstr("description")));
656         
657                 while (prop = icalcomponent_get_first_property(vtodo,
658                       ICAL_DTSTART_PROPERTY), prop != NULL) {
659                         icalcomponent_remove_property(vtodo, prop);
660                         icalproperty_free(prop);
661                 }
662                 icaltime_from_webform(&t, "dtstart");
663                 icalcomponent_add_property(vtodo,
664                         icalproperty_new_dtstart(t)
665                 );
666         
667                 while (prop = icalcomponent_get_first_property(vtodo,
668                       ICAL_DUE_PROPERTY), prop != NULL) {
669                         icalcomponent_remove_property(vtodo, prop);
670                         icalproperty_free(prop);
671                 }
672                 icaltime_from_webform(&t, "due");
673                 icalcomponent_add_property(vtodo,
674                         icalproperty_new_due(t)
675                 );
676
677                 /** Give this task a UID if it doesn't have one. */
678                 lprintf(9, "Give this task a UID if it doesn't have one.\n");
679                 if (icalcomponent_get_first_property(vtodo,
680                    ICAL_UID_PROPERTY) == NULL) {
681                         generate_uuid(buf);
682                         icalcomponent_add_property(vtodo,
683                                 icalproperty_new_uid(buf)
684                         );
685                 }
686
687                 /** Increment the sequence ID */
688                 lprintf(9, "Increment the sequence ID\n");
689                 while (prop = icalcomponent_get_first_property(vtodo,
690                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
691                         i = icalproperty_get_sequence(prop);
692                         lprintf(9, "Sequence was %d\n", i);
693                         if (i > sequence) sequence = i;
694                         icalcomponent_remove_property(vtodo, prop);
695                         icalproperty_free(prop);
696                 }
697                 ++sequence;
698                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
699                 icalcomponent_add_property(vtodo,
700                         icalproperty_new_sequence(sequence)
701                 );
702
703                 /**
704                  * Encapsulate event into full VCALENDAR component.  Clone it first,
705                  * for two reasons: one, it's easier to just free the whole thing
706                  * when we're done instead of unbundling, but more importantly, we
707                  * can't encapsulate something that may already be encapsulated
708                  * somewhere else.
709                  */
710                 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
711                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
712
713                 /* Serialize it and save it to the message base */
714                 serv_puts("ENT0 1|||4");
715                 serv_getln(buf, sizeof buf);
716                 if (buf[0] == '4') {
717                         serv_puts("Content-type: text/calendar");
718                         serv_puts("");
719                         serv_puts(icalcomponent_as_ical_string(encaps));
720                         serv_puts("000");
721
722                         /**
723                          * Probably not necessary; the server will see the UID
724                          * of the object and delete the old one anyway, but
725                          * just in case...
726                          */
727                         delete_existing = 1;
728                 }
729                 icalcomponent_free(encaps);
730         }
731
732         /**
733          * If the user clicked 'Delete' then explicitly delete the message.
734          */
735         if (!IsEmptyStr(bstr("delete_button"))) {
736                 delete_existing = 1;
737         }
738
739         if ( (delete_existing) && (msgnum > 0L) ) {
740                 serv_printf("DELE %ld", atol(bstr("msgnum")));
741                 serv_getln(buf, sizeof buf);
742         }
743
744         if (created_new_vtodo) {
745                 icalcomponent_free(vtodo);
746         }
747
748         /** Go back to the task list */
749         readloop("readfwd");
750 }
751
752
753
754 /**
755  * \brief generic item handler
756  * Code common to all display handlers.  Given a message number and a MIME
757  * type, we load the message and hunt for that MIME type.  If found, we load
758  * the relevant part, deserialize it into a libical component, filter it for
759  * the requested object type, and feed it to the specified handler.
760  * \param mimetype mimetyp of our object
761  * \param which_kind sort of ical type
762  * \param msgnum number of the mesage in our db
763  * \param callback a funcion \todo
764  *
765  */
766 void display_using_handler(long msgnum,
767                         char *mimetype,
768                         icalcomponent_kind which_kind,
769                         void (*callback)(icalcomponent *, long)
770         ) {
771         char buf[1024];
772         char mime_partnum[256];
773         char mime_filename[256];
774         char mime_content_type[256];
775         char mime_disposition[256];
776         int mime_length;
777         char relevant_partnum[256];
778         char *relevant_source = NULL;
779         icalcomponent *cal, *c;
780
781         relevant_partnum[0] = '\0';
782         sprintf(buf, "MSG4 %ld", msgnum);       /* we need the mime headers */
783         serv_puts(buf);
784         serv_getln(buf, sizeof buf);
785         if (buf[0] != '1') return;
786
787         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
788                 if (!strncasecmp(buf, "part=", 5)) {
789                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
790                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
791                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
792                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
793                         mime_length = extract_int(&buf[5], 5);
794
795                         if (!strcasecmp(mime_content_type, "text/calendar")) {
796                                 strcpy(relevant_partnum, mime_partnum);
797                         }
798                         else if (!strcasecmp(mime_content_type, "text/vtodo")) {
799                                 strcpy(relevant_partnum, mime_partnum);
800                         }
801
802                 }
803         }
804
805         if (!IsEmptyStr(relevant_partnum)) {
806                 relevant_source = load_mimepart(msgnum, relevant_partnum);
807                 if (relevant_source != NULL) {
808
809                         cal = icalcomponent_new_from_string(relevant_source);
810                         if (cal != NULL) {
811
812                                 ical_dezonify(cal);
813
814                                 /** Simple components of desired type */
815                                 if (icalcomponent_isa(cal) == which_kind) {
816                                         callback(cal, msgnum);
817                                 }
818
819                                 /** Subcomponents of desired type */
820                                 for (c = icalcomponent_get_first_component(cal,
821                                     which_kind);
822                                     (c != 0);
823                                     c = icalcomponent_get_next_component(cal,
824                                     which_kind)) {
825                                         callback(c, msgnum);
826                                 }
827                                 icalcomponent_free(cal);
828                         }
829                         free(relevant_source);
830                 }
831         }
832
833 }
834
835 /**
836  * \brief display whole calendar
837  * \param msgnum number of the mesage in our db
838  */
839 void display_calendar(long msgnum) {
840         display_using_handler(msgnum, "text/calendar",
841                                 ICAL_VEVENT_COMPONENT,
842                                 display_individual_cal);
843 }
844
845 /**
846  * \brief display whole taksview
847  * \param msgnum number of the mesage in our db
848  */
849 void display_task(long msgnum) {
850         display_using_handler(msgnum, "text/calendar",
851                                 ICAL_VTODO_COMPONENT,
852                                 display_individual_cal);
853 }
854
855 /**
856  * \brief display the editor component for a task
857  */
858 void display_edit_task(void) {
859         long msgnum = 0L;
860
861         /** Force change the room if we have to */
862         if (!IsEmptyStr(bstr("taskrm"))) {
863                 gotoroom(bstr("taskrm"));
864         }
865
866         msgnum = atol(bstr("msgnum"));
867         if (msgnum > 0L) {
868                 /** existing task */
869                 display_using_handler(msgnum, "text/calendar",
870                                 ICAL_VTODO_COMPONENT,
871                                 display_edit_individual_task);
872         }
873         else {
874                 /** new task */
875                 display_edit_individual_task(NULL, 0L);
876         }
877 }
878
879 /**
880  *\brief save an edited task
881  */
882 void save_task(void) {
883         long msgnum = 0L;
884
885         msgnum = atol(bstr("msgnum"));
886         if (msgnum > 0L) {
887                 display_using_handler(msgnum, "text/calendar",
888                                 ICAL_VTODO_COMPONENT,
889                                 save_individual_task);
890         }
891         else {
892                 save_individual_task(NULL, 0L);
893         }
894 }
895
896 /**
897  * \brief display the editor component for an event
898  */
899 void display_edit_event(void) {
900         long msgnum = 0L;
901
902         msgnum = atol(bstr("msgnum"));
903         if (msgnum > 0L) {
904                 /* existing event */
905                 display_using_handler(msgnum, "text/calendar",
906                                 ICAL_VEVENT_COMPONENT,
907                                 display_edit_individual_event);
908         }
909         else {
910                 /* new event */
911                 display_edit_individual_event(NULL, 0L);
912         }
913 }
914
915 /**
916  * \brief save an edited event
917  */
918 void save_event(void) {
919         long msgnum = 0L;
920
921         msgnum = atol(bstr("msgnum"));
922
923         if (msgnum > 0L) {
924                 display_using_handler(msgnum, "text/calendar",
925                                 ICAL_VEVENT_COMPONENT,
926                                 save_individual_event);
927         }
928         else {
929                 save_individual_event(NULL, 0L);
930         }
931 }
932
933
934
935
936
937 /**
938  * \brief freebusy display (for client software)
939  * \param req dunno. ?????
940  */
941 void do_freebusy(char *req) {
942         char who[SIZ];
943         char buf[SIZ];
944         char *fb;
945         int len;
946
947         extract_token(who, req, 1, ' ', sizeof who);
948         if (!strncasecmp(who, "/freebusy/", 10)) {
949                 strcpy(who, &who[10]);
950         }
951         unescape_input(who);
952
953         len = strlen(who);
954         if ( (!strcasecmp(&who[len-4], ".vcf"))
955            || (!strcasecmp(&who[len-4], ".ifb"))
956            || (!strcasecmp(&who[len-4], ".vfb")) ) {
957                 who[len-4] = 0;
958         }
959
960         lprintf(9, "freebusy requested for <%s>\n", who);
961         serv_printf("ICAL freebusy|%s", who);
962         serv_getln(buf, sizeof buf);
963
964         if (buf[0] != '1') {
965                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
966                 output_headers(0, 0, 0, 0, 0, 0);
967                 wprintf("Content-Type: text/plain\r\n");
968                 wprintf("\r\n");
969                 wprintf("%s\n", &buf[4]);
970                 return;
971         }
972
973         fb = read_server_text();
974         http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
975         free(fb);
976 }
977
978
979
980 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */
981
982
983 /*@}*/