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