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