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