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