3a78ac3d880e8b910afd5748c36ee5681bc048a6
[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, char *from, int unread)
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         WCC->disp_cal[WCC->num_cal - 1].unread = unread;
457         WCC->disp_cal[WCC->num_cal - 1].from = malloc (strlen(from) + 1);
458         strcpy (WCC->disp_cal[WCC->num_cal - 1].from, from);
459         ical_dezonify(WCC->disp_cal[WCC->num_cal - 1].cal);
460         WCC->disp_cal[WCC->num_cal - 1].cal_msgnum = msgnum;
461 }
462
463
464
465 /*
466  * \brief edit a task
467  * Display a task by itself (for editing)
468  * \param supplied_vtodo the todo item we want to edit
469  * \param msgnum number of the mesage in our db
470  */
471 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from, int unread) {
472         icalcomponent *vtodo;
473         icalproperty *p;
474         struct icaltimetype t;
475         time_t now;
476         int created_new_vtodo = 0;
477
478         now = time(NULL);
479
480         if (supplied_vtodo != NULL) {
481                 vtodo = supplied_vtodo;
482
483                 /**
484                  * If we're looking at a fully encapsulated VCALENDAR
485                  * rather than a VTODO component, attempt to use the first
486                  * relevant VTODO subcomponent.  If there is none, the
487                  * NULL returned by icalcomponent_get_first_component() will
488                  * tell the next iteration of this function to create a
489                  * new one.
490                  */
491                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
492                         display_edit_individual_task(
493                                 icalcomponent_get_first_component(
494                                         vtodo, ICAL_VTODO_COMPONENT
495                                         ), 
496                                 msgnum,
497                                 from, unread
498                         );
499                         return;
500                 }
501         }
502         else {
503                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
504                 created_new_vtodo = 1;
505         }
506
507         output_headers(1, 1, 2, 0, 0, 0);
508         wprintf("<div id=\"banner\">\n");
509         wprintf("<img src=\"static/taskmanag_48x.gif\">");
510         wprintf("<h1>");
511         wprintf(_("Edit task"));
512         wprintf("</h1>");
513         wprintf("</div>\n");
514
515         wprintf("<div id=\"content\" class=\"service\">\n");
516
517         wprintf("<div class=\"fix_scrollbar_bug\">"
518                 "<table class=\"calendar_background\"><tr><td>");
519         
520         wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
521         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
522         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
523                 msgnum);
524
525         wprintf("<TABLE border=0>\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         if (p != NULL) {
543                 t = icalproperty_get_dtstart(p);
544         }
545         else {
546                 t = icaltime_from_timet(now, 0);
547         }
548         display_icaltimetype_as_webform(&t, "dtstart");
549         wprintf("</TD></TR>\n");
550
551         wprintf("<TR><TD>");
552         wprintf(_("Due date:"));
553         wprintf("</TD><TD>");
554         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
555         if (p != NULL) {
556                 t = icalproperty_get_due(p);
557         }
558         else {
559                 t = icaltime_from_timet(now, 0);
560         }
561         display_icaltimetype_as_webform(&t, "due");
562         wprintf("</TD></TR>\n");
563         wprintf("<TR><TD>");
564         wprintf(_("Description:"));
565         wprintf("</TD><TD>");
566         wprintf("<TEXTAREA NAME=\"description\" wrap=soft "
567                 "ROWS=10 COLS=80 WIDTH=80>\n"
568         );
569         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
570         if (p != NULL) {
571                 escputs((char *)icalproperty_get_comment(p));
572         }
573         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
574
575         wprintf("<CENTER>"
576                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
577                 "&nbsp;&nbsp;"
578                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
579                 "&nbsp;&nbsp;"
580                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
581                 "</CENTER>\n",
582                 _("Save"),
583                 _("Delete"),
584                 _("Cancel")
585         );
586
587         wprintf("</FORM>\n");
588
589         wprintf("</td></tr></table></div>\n");
590         wDumpContent(1);
591
592         if (created_new_vtodo) {
593                 icalcomponent_free(vtodo);
594         }
595 }
596
597 /*
598  * \brief Save an edited task
599  * \param supplied_vtodo the task to save
600  * \param msgnum number of the mesage in our db
601  */
602 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread) {
603         char buf[SIZ];
604         int delete_existing = 0;
605         icalproperty *prop;
606         icalcomponent *vtodo, *encaps;
607         int created_new_vtodo = 0;
608         int i;
609         int sequence = 0;
610         struct icaltimetype t;
611
612         if (supplied_vtodo != NULL) {
613                 vtodo = supplied_vtodo;
614                 /**
615                  * If we're looking at a fully encapsulated VCALENDAR
616                  * rather than a VTODO component, attempt to use the first
617                  * relevant VTODO subcomponent.  If there is none, the
618                  * NULL returned by icalcomponent_get_first_component() will
619                  * tell the next iteration of this function to create a
620                  * new one.
621                  */
622                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
623                         save_individual_task(
624                                 icalcomponent_get_first_component(
625                                         vtodo, ICAL_VTODO_COMPONENT), 
626                                 msgnum, from, unread
627                         );
628                         return;
629                 }
630         }
631         else {
632                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
633                 created_new_vtodo = 1;
634         }
635
636         if (!IsEmptyStr(bstr("save_button"))) {
637
638                 /** Replace values in the component with ones from the form */
639
640                 while (prop = icalcomponent_get_first_property(vtodo,
641                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
642                         icalcomponent_remove_property(vtodo, prop);
643                         icalproperty_free(prop);
644                 }
645                 if (!IsEmptyStr(bstr("summary"))) {
646         
647                         icalcomponent_add_property(vtodo,
648                                         icalproperty_new_summary(bstr("summary")));
649                 } else {
650                         icalcomponent_add_property(vtodo,
651                                         icalproperty_new_summary("Untitled Task"));
652                 }
653         
654                 while (prop = icalcomponent_get_first_property(vtodo,
655                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
656                         icalcomponent_remove_property(vtodo, prop);
657                         icalproperty_free(prop);
658                 }
659                 icalcomponent_add_property(vtodo,
660                         icalproperty_new_description(bstr("description")));
661         
662                 while (prop = icalcomponent_get_first_property(vtodo,
663                       ICAL_DTSTART_PROPERTY), prop != NULL) {
664                         icalcomponent_remove_property(vtodo, prop);
665                         icalproperty_free(prop);
666                 }
667                 icaltime_from_webform(&t, "dtstart");
668                 icalcomponent_add_property(vtodo,
669                         icalproperty_new_dtstart(t)
670                 );
671         
672                 while (prop = icalcomponent_get_first_property(vtodo,
673                       ICAL_DUE_PROPERTY), prop != NULL) {
674                         icalcomponent_remove_property(vtodo, prop);
675                         icalproperty_free(prop);
676                 }
677                 icaltime_from_webform(&t, "due");
678                 icalcomponent_add_property(vtodo,
679                         icalproperty_new_due(t)
680                 );
681
682                 /** Give this task a UID if it doesn't have one. */
683                 lprintf(9, "Give this task a UID if it doesn't have one.\n");
684                 if (icalcomponent_get_first_property(vtodo,
685                    ICAL_UID_PROPERTY) == NULL) {
686                         generate_uuid(buf);
687                         icalcomponent_add_property(vtodo,
688                                 icalproperty_new_uid(buf)
689                         );
690                 }
691
692                 /** Increment the sequence ID */
693                 lprintf(9, "Increment the sequence ID\n");
694                 while (prop = icalcomponent_get_first_property(vtodo,
695                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
696                         i = icalproperty_get_sequence(prop);
697                         lprintf(9, "Sequence was %d\n", i);
698                         if (i > sequence) sequence = i;
699                         icalcomponent_remove_property(vtodo, prop);
700                         icalproperty_free(prop);
701                 }
702                 ++sequence;
703                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
704                 icalcomponent_add_property(vtodo,
705                         icalproperty_new_sequence(sequence)
706                 );
707
708                 /**
709                  * Encapsulate event into full VCALENDAR component.  Clone it first,
710                  * for two reasons: one, it's easier to just free the whole thing
711                  * when we're done instead of unbundling, but more importantly, we
712                  * can't encapsulate something that may already be encapsulated
713                  * somewhere else.
714                  */
715                 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
716                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
717
718                 /* Serialize it and save it to the message base */
719                 serv_puts("ENT0 1|||4");
720                 serv_getln(buf, sizeof buf);
721                 if (buf[0] == '4') {
722                         serv_puts("Content-type: text/calendar");
723                         serv_puts("");
724                         serv_puts(icalcomponent_as_ical_string(encaps));
725                         serv_puts("000");
726
727                         /**
728                          * Probably not necessary; the server will see the UID
729                          * of the object and delete the old one anyway, but
730                          * just in case...
731                          */
732                         delete_existing = 1;
733                 }
734                 icalcomponent_free(encaps);
735         }
736
737         /**
738          * If the user clicked 'Delete' then explicitly delete the message.
739          */
740         if (!IsEmptyStr(bstr("delete_button"))) {
741                 delete_existing = 1;
742         }
743
744         if ( (delete_existing) && (msgnum > 0L) ) {
745                 serv_printf("DELE %ld", atol(bstr("msgnum")));
746                 serv_getln(buf, sizeof buf);
747         }
748
749         if (created_new_vtodo) {
750                 icalcomponent_free(vtodo);
751         }
752
753         /** Go back to the task list */
754         readloop("readfwd");
755 }
756
757
758
759 /**
760  * \brief generic item handler
761  * Code common to all display handlers.  Given a message number and a MIME
762  * type, we load the message and hunt for that MIME type.  If found, we load
763  * the relevant part, deserialize it into a libical component, filter it for
764  * the requested object type, and feed it to the specified handler.
765  * \param mimetype mimetyp of our object
766  * \param which_kind sort of ical type
767  * \param msgnum number of the mesage in our db
768  * \param callback a funcion \todo
769  *
770  */
771 void display_using_handler(long msgnum, int unread,
772                            icalcomponent_kind which_kind,
773                            void (*callback)(icalcomponent *, long, char*, int)
774         ) {
775         char buf[1024];
776         char from[128] = "";
777         char mime_partnum[256];
778         char mime_filename[256];
779         char mime_content_type[256];
780         char mime_disposition[256];
781         int mime_length;
782         char relevant_partnum[256];
783         char *relevant_source = NULL;
784         icalcomponent *cal, *c;
785
786         relevant_partnum[0] = '\0';
787         sprintf(buf, "MSG4 %ld", msgnum);       /* we need the mime headers */
788         serv_puts(buf);
789         serv_getln(buf, sizeof buf);
790         if (buf[0] != '1') return;
791
792         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
793                 if (!strncasecmp(buf, "part=", 5)) {
794                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
795                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
796                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
797                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
798                         mime_length = extract_int(&buf[5], 5);
799
800                         if (  (!strcasecmp(mime_content_type, "text/calendar"))
801                            || (!strcasecmp(mime_content_type, "application/ics"))
802                            || (!strcasecmp(mime_content_type, "text/vtodo"))
803                         ) {
804                                 strcpy(relevant_partnum, mime_partnum);
805                         }
806                 }
807                 else if (!strncasecmp(buf, "from=", 4)) {
808                         extract_token(from, buf, 1, '=', sizeof(from));
809                 }
810         }
811
812         if (!IsEmptyStr(relevant_partnum)) {
813                 relevant_source = load_mimepart(msgnum, relevant_partnum);
814                 if (relevant_source != NULL) {
815
816                         cal = icalcomponent_new_from_string(relevant_source);
817                         if (cal != NULL) {
818
819                                 ical_dezonify(cal);
820
821                                 /** Simple components of desired type */
822                                 if (icalcomponent_isa(cal) == which_kind) {
823                                         callback(cal, msgnum, from, unread);
824                                 }
825
826                                 /** Subcomponents of desired type */
827                                 for (c = icalcomponent_get_first_component(cal,
828                                     which_kind);
829                                     (c != 0);
830                                     c = icalcomponent_get_next_component(cal,
831                                     which_kind)) {
832                                         callback(c, msgnum, from, unread);
833                                 }
834                                 icalcomponent_free(cal);
835                         }
836                         free(relevant_source);
837                 }
838         }
839         icalmemory_free_ring();
840 }
841
842 /**
843  * \brief display whole calendar
844  * \param msgnum number of the mesage in our db
845  */
846 void display_calendar(long msgnum, int unread) {
847         display_using_handler(msgnum, unread,
848                                 ICAL_VEVENT_COMPONENT,
849                                 display_individual_cal);
850 }
851
852 /**
853  * \brief display whole taksview
854  * \param msgnum number of the mesage in our db
855  */
856 void display_task(long msgnum, int unread) {
857         display_using_handler(msgnum, unread,
858                                 ICAL_VTODO_COMPONENT,
859                                 display_individual_cal);
860 }
861
862 /**
863  * \brief display the editor component for a task
864  */
865 void display_edit_task(void) {
866         long msgnum = 0L;
867
868         /** Force change the room if we have to */
869         if (!IsEmptyStr(bstr("taskrm"))) {
870                 gotoroom(bstr("taskrm"));
871         }
872
873         msgnum = atol(bstr("msgnum"));
874         if (msgnum > 0L) {
875                 /** existing task */
876                 display_using_handler(msgnum, 0,
877                                 ICAL_VTODO_COMPONENT,
878                                 display_edit_individual_task);
879         }
880         else {
881                 /** new task */
882                 display_edit_individual_task(NULL, 0L, "", 0);
883         }
884 }
885
886 /**
887  *\brief save an edited task
888  */
889 void save_task(void) {
890         long msgnum = 0L;
891
892         msgnum = atol(bstr("msgnum"));
893         if (msgnum > 0L) {
894                 display_using_handler(msgnum, 0,
895                                 ICAL_VTODO_COMPONENT,
896                                 save_individual_task);
897         }
898         else {
899                 save_individual_task(NULL, 0L, "", 0);
900         }
901 }
902
903 /**
904  * \brief display the editor component for an event
905  */
906 void display_edit_event(void) {
907         long msgnum = 0L;
908
909         msgnum = atol(bstr("msgnum"));
910         if (msgnum > 0L) {
911                 /* existing event */
912                 display_using_handler(msgnum, 0,
913                                 ICAL_VEVENT_COMPONENT,
914                                 display_edit_individual_event);
915         }
916         else {
917                 /* new event */
918                 display_edit_individual_event(NULL, 0L, "", 0);
919         }
920 }
921
922 /**
923  * \brief save an edited event
924  */
925 void save_event(void) {
926         long msgnum = 0L;
927
928         msgnum = atol(bstr("msgnum"));
929
930         if (msgnum > 0L) {
931                 display_using_handler(msgnum, 0,
932                                 ICAL_VEVENT_COMPONENT,
933                                 save_individual_event);
934         }
935         else {
936                 save_individual_event(NULL, 0L, "", 0);
937         }
938 }
939
940
941
942
943
944 /**
945  * \brief freebusy display (for client software)
946  * \param req dunno. ?????
947  */
948 void do_freebusy(char *req) {
949         char who[SIZ];
950         char buf[SIZ];
951         char *fb;
952         int len;
953
954         extract_token(who, req, 1, ' ', sizeof who);
955         if (!strncasecmp(who, "/freebusy/", 10)) {
956                 strcpy(who, &who[10]);
957         }
958         unescape_input(who);
959
960         len = strlen(who);
961         if ( (!strcasecmp(&who[len-4], ".vcf"))
962            || (!strcasecmp(&who[len-4], ".ifb"))
963            || (!strcasecmp(&who[len-4], ".vfb")) ) {
964                 who[len-4] = 0;
965         }
966
967         lprintf(9, "freebusy requested for <%s>\n", who);
968         serv_printf("ICAL freebusy|%s", who);
969         serv_getln(buf, sizeof buf);
970
971         if (buf[0] != '1') {
972                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
973                 output_headers(0, 0, 0, 0, 0, 0);
974                 wprintf("Content-Type: text/plain\r\n");
975                 wprintf("\r\n");
976                 wprintf("%s\n", &buf[4]);
977                 return;
978         }
979
980         fb = read_server_text();
981         http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
982         free(fb);
983 }
984
985
986
987 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */
988