4 * Functions which handle calendar objects and their processing/display.
12 * Process a calendar object. At this point it's already been deserialized by cal_process_attachment()
14 * cal: the calendar object
15 * recursion_level: Number of times we've recursed into this function
16 * msgnum: Message number on the Citadel server
17 * cal_partnum: MIME part number within that message containing the calendar object
19 void cal_process_object(StrBuf *Target,
23 const char *cal_partnum)
26 icalproperty *method = NULL;
27 icalproperty_method the_method = ICAL_METHOD_NONE;
29 struct icaltimetype t;
32 char conflict_name[256];
33 char conflict_message[256];
36 static int divcount = 0;
38 sprintf(divname, "rsvp%04x", ++divcount);
40 /* Leading HTML for the display of this object */
41 if (recursion_level == 0) {
42 StrBufAppendPrintf(Target, "<div class=\"mimepart\">\n");
45 /* Look for a method */
46 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
48 /* See what we need to do with this */
50 the_method = icalproperty_get_method(method);
53 StrBufAppendPrintf(Target, "<div id=\"%s_title\">", divname);
54 StrBufAppendPrintf(Target, "<img src=\"static/calarea_48x.gif\">");
55 StrBufAppendPrintf(Target, "<span>");
57 case ICAL_METHOD_REQUEST:
58 title = _("Meeting invitation");
60 case ICAL_METHOD_REPLY:
61 title = _("Attendee's reply to your invitation");
63 case ICAL_METHOD_PUBLISH:
64 title = _("Published event");
67 title = _("This is an unknown type of calendar item.");
70 StrBufAppendPrintf(Target, "</span>");
72 StrBufAppendPrintf(Target, " %s",title);
73 StrBufAppendPrintf(Target, "</div>");
76 StrBufAppendPrintf(Target, "<dl>");
77 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
79 StrBufAppendPrintf(Target, "<dt>");
80 StrBufAppendPrintf(Target, _("Summary:"));
81 StrBufAppendPrintf(Target, "</dt><dd>");
82 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
83 StrBufAppendPrintf(Target, "</dd>\n");
86 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
88 StrBufAppendPrintf(Target, "<dt>");
89 StrBufAppendPrintf(Target, _("Location:"));
90 StrBufAppendPrintf(Target, "</dt><dd>");
91 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
92 StrBufAppendPrintf(Target, "</dd>\n");
96 * Only show start/end times if we're actually looking at the VEVENT
97 * component. Otherwise it shows bogus dates for things like timezone.
99 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
101 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
103 t = icalproperty_get_dtstart(p);
108 memset(&d_tm, 0, sizeof d_tm);
109 d_tm.tm_year = t.year - 1900;
110 d_tm.tm_mon = t.month - 1;
111 d_tm.tm_mday = t.day;
112 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
113 StrBufAppendPrintf(Target, "<dt>");
114 StrBufAppendPrintf(Target, _("Date:"));
115 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", d_str);
118 tt = icaltime_as_timet(t);
119 webcit_fmt_date(buf, tt, 0);
120 StrBufAppendPrintf(Target, "<dt>");
121 StrBufAppendPrintf(Target, _("Starting date/time:"));
122 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
126 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
128 t = icalproperty_get_dtend(p);
129 tt = icaltime_as_timet(t);
130 webcit_fmt_date(buf, tt, 0);
131 StrBufAppendPrintf(Target, "<dt>");
132 StrBufAppendPrintf(Target, _("Ending date/time:"));
133 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
138 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
140 StrBufAppendPrintf(Target, "<dt>");
141 StrBufAppendPrintf(Target, _("Description:"));
142 StrBufAppendPrintf(Target, "</dt><dd>");
143 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
144 StrBufAppendPrintf(Target, "</dd>\n");
147 /* If the component has attendees, iterate through them. */
148 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY);
150 p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
151 StrBufAppendPrintf(Target, "<dt>");
152 StrBufAppendPrintf(Target, _("Attendee:"));
153 StrBufAppendPrintf(Target, "</dt><dd>");
154 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
155 if (!strncasecmp(buf, "MAILTO:", 7)) {
157 /** screen name or email address */
158 strcpy(buf, &buf[7]);
160 StrEscAppend(Target, NULL, buf, 0, 0);
161 StrBufAppendPrintf(Target, " ");
163 /** participant status */
164 partstat_as_string(buf, p);
165 StrEscAppend(Target, NULL, buf, 0, 0);
167 StrBufAppendPrintf(Target, "</dd>\n");
170 /* If the component has subcomponents, recurse through them. */
171 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
173 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
174 /* Recursively process subcomponent */
175 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
178 /* If this is a REQUEST, display conflicts and buttons */
179 if (the_method == ICAL_METHOD_REQUEST) {
181 /* Check for conflicts */
182 lprintf(9, "Checking server calendar for conflicts...\n");
183 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
184 serv_getln(buf, sizeof buf);
186 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
187 extract_token(conflict_name, buf, 3, '|', sizeof conflict_name);
188 is_update = extract_int(buf, 4);
191 snprintf(conflict_message, sizeof conflict_message,
192 _("This is an update of '%s' which is already in your calendar."), conflict_name);
195 snprintf(conflict_message, sizeof conflict_message,
196 _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
199 StrBufAppendPrintf(Target, "<dt>%s",
205 StrBufAppendPrintf(Target, "</dt><dd>");
206 StrEscAppend(Target, NULL, conflict_message, 0, 0);
207 StrBufAppendPrintf(Target, "</dd>\n");
210 lprintf(9, "...done.\n");
212 StrBufAppendPrintf(Target, "</dl>");
214 /* Display the Accept/Decline buttons */
215 StrBufAppendPrintf(Target, "<p id=\"%s_question\">"
217 " <span class=\"button_link\"> "
218 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
219 "</span> <span class=\"button_link\">"
220 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
221 "</span> <span class=\"button_link\">"
222 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
225 _("How would you like to respond to this invitation?"),
226 divname, divname, msgnum, cal_partnum, _("Accept"),
227 divname, divname, msgnum, cal_partnum, _("Tentative"),
228 divname, divname, msgnum, cal_partnum, _("Decline")
233 /* If this is a REPLY, display update button */
234 if (the_method == ICAL_METHOD_REPLY) {
236 /* In the future, if we want to validate this object before
237 * continuing, we can do it this way:
238 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
239 serv_getln(buf, sizeof buf);
243 /* Display the update buttons */
244 StrBufAppendPrintf(Target, "<p id=\"%s_question\" >"
246 " <span class=\"button_link\"> "
247 "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
248 "</span> <span class=\"button_link\">"
249 "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
252 _("Click <i>Update</i> to accept this reply and update your calendar."),
253 divname, divname, msgnum, cal_partnum, _("Update"),
254 divname, divname, msgnum, cal_partnum, _("Ignore")
259 /* Trailing HTML for the display of this object */
260 if (recursion_level == 0) {
261 StrBufAppendPrintf(Target, "<p> </p></div>\n");
267 * \brief process calendar mail atachment
268 * Deserialize a calendar object in a message so it can be processed.
269 * (This is the main entry point for these things)
270 * \param part_source the part of the message we want to parse
271 * \param msgnum number of the mesage in our db
272 * \param cal_partnum the number of the calendar item
274 void cal_process_attachment(wc_mime_attachment *Mime)
278 cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
279 FlushStrBuf(Mime->Data);
281 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
282 StrBufAppendPrintf(Mime->Data, "<br />\n");
286 cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum));
288 /* Free the memory we obtained from libical's constructor */
289 icalcomponent_free(cal);
296 * \brief accept/decline meeting
297 * Respond to a meeting request
299 void respond_to_request(void)
303 begin_ajax_response();
305 serv_printf("ICAL respond|%s|%s|%s|",
310 serv_getln(buf, sizeof buf);
313 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
314 if (!strcasecmp(bstr("sc"), "accept")) {
315 wprintf(_("You have accepted this meeting invitation. "
316 "It has been entered into your calendar.")
318 } else if (!strcasecmp(bstr("sc"), "tentative")) {
319 wprintf(_("You have tentatively accepted this meeting invitation. "
320 "It has been 'pencilled in' to your calendar.")
322 } else if (!strcasecmp(bstr("sc"), "decline")) {
323 wprintf(_("You have declined this meeting invitation. "
324 "It has <b>not</b> been entered into your calendar.")
328 wprintf(_("A reply has been sent to the meeting organizer."));
331 wprintf("<img align=\"center\" src=\"static/error.gif\"><span>");
332 wprintf("%s\n", &buf[4]);
342 * \brief Handle an incoming RSVP
344 void handle_rsvp(void)
348 begin_ajax_response();
350 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
355 serv_getln(buf, sizeof buf);
358 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
359 if (!strcasecmp(bstr("sc"), "update")) {
360 wprintf(_("Your calendar has been updated to reflect this RSVP."));
361 } else if (!strcasecmp(bstr("sc"), "ignore")) {
362 wprintf(_("You have chosen to ignore this RSVP. "
363 "Your calendar has <b>not</b> been updated.")
368 wprintf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
379 * free memory allocated using libical
381 void delete_cal(void *vCal)
383 disp_cal *Cal = (disp_cal*) vCal;
384 icalcomponent_free(Cal->cal);
390 * This is the meat-and-bones of the first part of our two-phase calendar display.
391 * As we encounter calendar items in messages being read from the server, we break out
392 * any iCalendar objects and store them in a hash table. Later on, the second phase will
393 * use this hash table to render the calendar for display.
395 void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unread, struct calview *calv)
397 icalproperty *ps = NULL;
398 struct icaltimetype dtstart, dtend;
399 struct icaldurationtype dur;
400 struct wcsession *WCC = WC;
403 time_t final_recurrence = 0;
404 icalcomponent *cptr = NULL;
406 /* recur variables */
407 icalproperty *rrule = NULL;
408 struct icalrecurrencetype recur;
409 icalrecur_iterator *ritr = NULL;
410 struct icaltimetype next;
413 dtstart = icaltime_null_time();
414 dtend = icaltime_null_time();
416 if (WCC->disp_cal_items == NULL)
417 WCC->disp_cal_items = NewHash(0, Flathash);
419 /* Note: anything we do here, we also have to do below for the recurrences. */
420 Cal = (disp_cal*) malloc(sizeof(disp_cal));
421 memset(Cal, 0, sizeof(disp_cal));
422 Cal->cal = icalcomponent_new_clone(cal);
424 /* Dezonify and decapsulate at the very last moment */
425 /* lprintf(9, "INITIAL: %s\n", icaltime_as_ical_string(icalproperty_get_dtstart(
426 icalcomponent_get_first_property(icalcomponent_get_first_component(
427 Cal->cal, ICAL_VEVENT_COMPONENT), ICAL_DTSTART_PROPERTY)))
429 ical_dezonify(Cal->cal);
430 if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
431 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
433 cptr = icalcomponent_new_clone(cptr);
434 icalcomponent_free(Cal->cal);
439 Cal->unread = unread;
441 Cal->from = (char*)malloc(len+ 1);
442 memcpy(Cal->from, from, len + 1);
443 Cal->cal_msgnum = msgnum;
445 /* Precalculate the starting date and time of this event, and store it in our top-level
446 * structure. Later, when we are rendering the calendar, we can just peek at these values
447 * without having to break apart every calendar item.
449 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
451 dtstart = icalproperty_get_dtstart(ps);
452 Cal->event_start = icaltime_as_timet(dtstart);
455 /* Do the same for the ending date and time. It makes the day view much easier to render. */
456 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
458 dtend = icalproperty_get_dtend(ps);
459 Cal->event_end = icaltime_as_timet(dtend);
462 /* Store it in the hash list. */
463 Put(WCC->disp_cal_items,
464 (char*) &Cal->event_start,
465 sizeof(Cal->event_start),
469 /****************************** handle recurring events ******************************/
471 if (icaltime_is_null_time(dtstart)) return; /* Can't recur without a start time */
473 if (!icaltime_is_null_time(dtend)) { /* Need duration for recurrences */
474 dur = icaltime_subtract(dtend, dtstart);
478 * Just let libical iterate the recurrence, and keep looping back to the top of this function,
479 * adding new hash entries that all point back to the same msgnum, until either the iteration
480 * stops or some outer bound is reached. The display code will automatically do the Right Thing.
483 if (icalcomponent_isa(cptr) != ICAL_VEVENT_COMPONENT) {
484 cptr = icalcomponent_get_first_component(cptr, ICAL_VEVENT_COMPONENT);
487 ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
488 if (ps == NULL) return;
489 dtstart = icalproperty_get_dtstart(ps);
490 rrule = icalcomponent_get_first_property(cptr, ICAL_RRULE_PROPERTY);
492 recur = icalproperty_get_rrule(rrule);
493 ritr = icalrecur_iterator_new(recur, dtstart);
497 while (next = icalrecur_iterator_next(ritr), ((!icaltime_is_null_time(next))&&(!stop_rr)) ) {
499 if (num_recur > 1) { /* Skip the first one. We already did it at the root. */
500 /* lprintf(9, "REPEATS: %s\n", icaltime_as_ical_string(next)); */
502 /* Note: anything we do here, we also have to do above for the root event. */
503 Cal = (disp_cal*) malloc(sizeof(disp_cal));
504 memset(Cal, 0, sizeof(disp_cal));
505 Cal->cal = icalcomponent_new_clone(cal);
506 Cal->unread = unread;
508 Cal->from = (char*)malloc(len+ 1);
509 memcpy(Cal->from, from, len + 1);
510 Cal->cal_msgnum = msgnum;
513 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
517 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
520 ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
522 icalcomponent_remove_property(cptr, ps);
523 ps = icalproperty_new_dtstart(next);
524 icalcomponent_add_property(cptr, ps);
526 Cal->event_start = icaltime_as_timet(next);
527 final_recurrence = Cal->event_start;
530 ps = icalcomponent_get_first_property(cptr, ICAL_DTEND_PROPERTY);
532 icalcomponent_remove_property(cptr, ps);
534 /* Make a new dtend */
535 ps = icalproperty_new_dtend(icaltime_add(next, dur));
537 /* and stick it somewhere */
538 icalcomponent_add_property(cptr, ps);
543 /* Dezonify and decapsulate at the very last moment */
544 ical_dezonify(Cal->cal);
545 if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
546 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
548 cptr = icalcomponent_new_clone(cptr);
549 icalcomponent_free(Cal->cal);
554 if ( (Cal->event_start > calv->lower_bound)
555 && (Cal->event_start < calv->upper_bound) ) {
556 Put(WCC->disp_cal_items,
557 (char*) &Cal->event_start,
558 sizeof(Cal->event_start),
567 /* If an upper bound is set, stop when we go out of scope */
568 if (final_recurrence > calv->upper_bound) stop_rr = 1;
571 icalrecur_iterator_free(ritr);
572 /* lprintf(9, "Performed %d recurrences; final one is %s", num_recur, ctime(&final_recurrence)); */
579 * Display a task by itself (for editing)
581 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
582 int unread, struct calview *calv)
584 icalcomponent *vtodo;
586 struct icaltimetype IcalTime;
588 int created_new_vtodo = 0;
589 icalproperty_status todoStatus;
593 if (supplied_vtodo != NULL) {
594 vtodo = supplied_vtodo;
597 * If we're looking at a fully encapsulated VCALENDAR
598 * rather than a VTODO component, attempt to use the first
599 * relevant VTODO subcomponent. If there is none, the
600 * NULL returned by icalcomponent_get_first_component() will
601 * tell the next iteration of this function to create a
604 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
605 display_edit_individual_task(
606 icalcomponent_get_first_component(
607 vtodo, ICAL_VTODO_COMPONENT
609 msgnum, from, unread, calv
615 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
616 created_new_vtodo = 1;
619 // TODO: Can we take all this and move it into a template?
620 output_headers(1, 1, 1, 0, 0, 0);
621 wprintf("<!-- start task edit form -->");
622 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
623 // Get summary early for title
624 wprintf("<div class=\"box\">\n");
625 wprintf("<div class=\"boxlabel\">");
626 wprintf(_("Edit task"));
629 escputs((char *)icalproperty_get_comment(p));
633 wprintf("<div class=\"boxcontent\">\n");
634 wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
635 wprintf("<div style=\"display: none;\">\n ");
636 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
637 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
640 wprintf("<table class=\"calendar_background\"><tr><td>");
641 wprintf("<TABLE STYLE=\"border: none;\">\n");
644 wprintf(_("Summary:"));
646 "<INPUT TYPE=\"text\" NAME=\"summary\" "
647 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
648 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
650 escputs((char *)icalproperty_get_comment(p));
652 wprintf("\"></TD></TR>\n");
655 wprintf(_("Start date:"));
656 wprintf("</TD><TD>");
657 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
658 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
660 wprintf("CHECKED=\"CHECKED\"");
663 wprintf(_("No date"));
669 IcalTime = icalproperty_get_dtstart(p);
672 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
673 display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
674 wprintf("</TD></TR>\n");
677 wprintf(_("Due date:"));
678 wprintf("</TD><TD>");
679 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
680 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
682 wprintf("CHECKED=\"CHECKED\"");
685 wprintf(_("No date"));
690 IcalTime = icalproperty_get_due(p);
693 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
694 display_icaltimetype_as_webform(&IcalTime, "due", 0);
696 wprintf("</TD></TR>\n");
697 todoStatus = icalcomponent_get_status(vtodo);
698 wprintf("<TR><TD>\n");
699 wprintf(_("Completed:"));
700 wprintf("</TD><TD>");
701 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
702 if (todoStatus == ICAL_STATUS_COMPLETED) {
703 wprintf(" CHECKED=\"CHECKED\"");
706 wprintf("</TD></TR>");
707 // start category field
708 p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
710 wprintf(_("Category:"));
711 wprintf("</TD><TD>");
712 wprintf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
714 escputs((char *)icalproperty_get_categories(p));
717 wprintf("</TD></TR>\n ");
718 // end category field
720 wprintf(_("Description:"));
721 wprintf("</TD><TD>");
722 wprintf("<TEXTAREA NAME=\"description\" "
723 "ROWS=\"10\" COLS=\"80\">\n"
725 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
727 escputs((char *)icalproperty_get_comment(p));
729 wprintf("</TEXTAREA></TD></TR></TABLE>\n");
731 wprintf("<SPAN STYLE=\"text-align: center;\">"
732 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
734 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
736 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
742 wprintf("</td></tr></table>");
743 wprintf("</FORM>\n");
744 wprintf("</div></div></div>\n");
745 wprintf("<!-- end task edit form -->");
748 if (created_new_vtodo) {
749 icalcomponent_free(vtodo);
754 * \brief Save an edited task
755 * \param supplied_vtodo the task to save
756 * \param msgnum number of the mesage in our db
758 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
759 struct calview *calv)
762 int delete_existing = 0;
764 icalcomponent *vtodo, *encaps;
765 int created_new_vtodo = 0;
768 struct icaltimetype t;
770 if (supplied_vtodo != NULL) {
771 vtodo = supplied_vtodo;
773 * If we're looking at a fully encapsulated VCALENDAR
774 * rather than a VTODO component, attempt to use the first
775 * relevant VTODO subcomponent. If there is none, the
776 * NULL returned by icalcomponent_get_first_component() will
777 * tell the next iteration of this function to create a
780 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
781 save_individual_task(
782 icalcomponent_get_first_component(
783 vtodo, ICAL_VTODO_COMPONENT),
784 msgnum, from, unread, calv
790 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
791 created_new_vtodo = 1;
794 if (havebstr("save_button")) {
796 /** Replace values in the component with ones from the form */
798 while (prop = icalcomponent_get_first_property(vtodo,
799 ICAL_SUMMARY_PROPERTY), prop != NULL) {
800 icalcomponent_remove_property(vtodo, prop);
801 icalproperty_free(prop);
803 if (havebstr("summary")) {
805 icalcomponent_add_property(vtodo,
806 icalproperty_new_summary(bstr("summary")));
808 icalcomponent_add_property(vtodo,
809 icalproperty_new_summary("Untitled Task"));
812 while (prop = icalcomponent_get_first_property(vtodo,
813 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
814 icalcomponent_remove_property(vtodo, prop);
815 icalproperty_free(prop);
817 if (havebstr("description")) {
818 icalcomponent_add_property(vtodo,
819 icalproperty_new_description(bstr("description")));
822 while (prop = icalcomponent_get_first_property(vtodo,
823 ICAL_DTSTART_PROPERTY), prop != NULL) {
824 icalcomponent_remove_property(vtodo, prop);
825 icalproperty_free(prop);
827 if (IsEmptyStr(bstr("nodtstart"))) {
828 icaltime_from_webform(&t, "dtstart");
829 icalcomponent_add_property(vtodo,
830 icalproperty_new_dtstart(t)
833 while(prop = icalcomponent_get_first_property(vtodo,
834 ICAL_STATUS_PROPERTY), prop != NULL) {
835 icalcomponent_remove_property(vtodo,prop);
836 icalproperty_free(prop);
838 if (havebstr("status")) {
839 icalproperty_status taskStatus = icalproperty_string_to_status(
841 icalcomponent_set_status(vtodo, taskStatus);
843 while (prop = icalcomponent_get_first_property(vtodo,
844 ICAL_CATEGORIES_PROPERTY), prop != NULL) {
845 icalcomponent_remove_property(vtodo,prop);
846 icalproperty_free(prop);
848 if (!IsEmptyStr(bstr("category"))) {
849 prop = icalproperty_new_categories(bstr("category"));
850 icalcomponent_add_property(vtodo,prop);
852 while (prop = icalcomponent_get_first_property(vtodo,
853 ICAL_DUE_PROPERTY), prop != NULL) {
854 icalcomponent_remove_property(vtodo, prop);
855 icalproperty_free(prop);
857 if (IsEmptyStr(bstr("nodue"))) {
858 icaltime_from_webform(&t, "due");
859 icalcomponent_add_property(vtodo,
860 icalproperty_new_due(t)
863 /** Give this task a UID if it doesn't have one. */
864 lprintf(9, "Give this task a UID if it doesn't have one.\n");
865 if (icalcomponent_get_first_property(vtodo,
866 ICAL_UID_PROPERTY) == NULL) {
868 icalcomponent_add_property(vtodo,
869 icalproperty_new_uid(buf)
873 /** Increment the sequence ID */
874 lprintf(9, "Increment the sequence ID\n");
875 while (prop = icalcomponent_get_first_property(vtodo,
876 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
877 i = icalproperty_get_sequence(prop);
878 lprintf(9, "Sequence was %d\n", i);
879 if (i > sequence) sequence = i;
880 icalcomponent_remove_property(vtodo, prop);
881 icalproperty_free(prop);
884 lprintf(9, "New sequence is %d. Adding...\n", sequence);
885 icalcomponent_add_property(vtodo,
886 icalproperty_new_sequence(sequence)
890 * Encapsulate event into full VCALENDAR component. Clone it first,
891 * for two reasons: one, it's easier to just free the whole thing
892 * when we're done instead of unbundling, but more importantly, we
893 * can't encapsulate something that may already be encapsulated
896 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
897 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
899 /* Serialize it and save it to the message base */
900 serv_puts("ENT0 1|||4");
901 serv_getln(buf, sizeof buf);
903 serv_puts("Content-type: text/calendar");
905 serv_puts(icalcomponent_as_ical_string(encaps));
909 * Probably not necessary; the server will see the UID
910 * of the object and delete the old one anyway, but
915 icalcomponent_free(encaps);
919 * If the user clicked 'Delete' then explicitly delete the message.
921 if (havebstr("delete_button")) {
925 if ( (delete_existing) && (msgnum > 0L) ) {
926 serv_printf("DELE %ld", lbstr("msgnum"));
927 serv_getln(buf, sizeof buf);
930 if (created_new_vtodo) {
931 icalcomponent_free(vtodo);
934 /** Go back to the task list */
941 * Code common to all icalendar display handlers. Given a message number and a MIME
942 * type, we load the message and hunt for that MIME type. If found, we load
943 * the relevant part, deserialize it into a libical component, filter it for
944 * the requested object type, and feed it to the specified handler.
946 void load_ical_object(long msgnum, int unread,
947 icalcomponent_kind which_kind,
948 void (*callback)(icalcomponent *, long, char*, int, struct calview *),
954 char mime_partnum[256];
955 char mime_filename[256];
956 char mime_content_type[256];
957 char mime_disposition[256];
959 char relevant_partnum[256];
960 char *relevant_source = NULL;
961 icalcomponent *cal, *c;
963 relevant_partnum[0] = '\0';
964 sprintf(buf, "MSG4 %ld", msgnum); /* we need the mime headers */
966 serv_getln(buf, sizeof buf);
967 if (buf[0] != '1') return;
969 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
970 if (!strncasecmp(buf, "part=", 5)) {
971 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
972 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
973 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
974 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
975 mime_length = extract_int(&buf[5], 5);
977 if ( (!strcasecmp(mime_content_type, "text/calendar"))
978 || (!strcasecmp(mime_content_type, "application/ics"))
979 || (!strcasecmp(mime_content_type, "text/vtodo"))
981 strcpy(relevant_partnum, mime_partnum);
984 else if (!strncasecmp(buf, "from=", 4)) {
985 extract_token(from, buf, 1, '=', sizeof(from));
989 if (!IsEmptyStr(relevant_partnum)) {
990 relevant_source = load_mimepart(msgnum, relevant_partnum);
991 if (relevant_source != NULL) {
993 cal = icalcomponent_new_from_string(relevant_source);
996 /* A which_kind of (-1) means just load the whole thing */
997 if (which_kind == (-1)) {
998 callback(cal, msgnum, from, unread, calv);
1001 /* Otherwise recurse and hunt */
1004 /* Simple components of desired type */
1005 if (icalcomponent_isa(cal) == which_kind) {
1006 callback(cal, msgnum, from, unread, calv);
1009 /* Subcomponents of desired type */
1010 for (c = icalcomponent_get_first_component(cal, which_kind);
1012 c = icalcomponent_get_next_component(cal, which_kind)) {
1013 callback(c, msgnum, from, unread, calv);
1018 icalcomponent_free(cal);
1020 free(relevant_source);
1023 icalmemory_free_ring();
1027 * Display a calendar item
1029 void load_calendar_item(message_summary *Msg, int unread, struct calview *c) {
1030 /*load_ical_object(Msg->msgnum, unread, ICAL_VEVENT_COMPONENT, display_individual_cal, c);*/
1031 load_ical_object(Msg->msgnum, unread, (-1), display_individual_cal, c);
1037 void display_task(message_summary *Msg, int unread) {
1038 load_ical_object(Msg->msgnum, unread, ICAL_VTODO_COMPONENT, display_individual_cal, NULL);
1042 * Display the editor component for a task
1044 void display_edit_task(void) {
1047 /* Force change the room if we have to */
1048 if (havebstr("taskrm")) {
1049 gotoroom((char *)bstr("taskrm"));
1052 msgnum = lbstr("msgnum");
1055 load_ical_object(msgnum, 0,
1056 ICAL_VTODO_COMPONENT,
1057 display_edit_individual_task,
1063 display_edit_individual_task(NULL, 0L, "", 0, NULL);
1068 * save an edited task
1070 void save_task(void) {
1072 msgnum = lbstr("msgnum");
1074 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL);
1077 save_individual_task(NULL, 0L, "", 0, NULL);
1082 * display the editor component for an event
1084 void display_edit_event(void) {
1087 msgnum = lbstr("msgnum");
1089 /* existing event */
1090 load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, display_edit_individual_event, NULL);
1094 display_edit_individual_event(NULL, 0L, "", 0, NULL);
1099 * save an edited event
1101 void save_event(void) {
1104 msgnum = lbstr("msgnum");
1107 /* load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, save_individual_event, NULL); */
1108 load_ical_object(msgnum, 0, (-1), save_individual_event, NULL);
1111 save_individual_event(NULL, 0L, "", 0, NULL);
1120 * Anonymous request of freebusy data for a user
1122 void do_freebusy(const char *req) {
1128 extract_token(who, req, 1, ' ', sizeof who);
1129 if (!strncasecmp(who, "/freebusy/", 10)) {
1130 strcpy(who, &who[10]);
1132 unescape_input(who);
1135 if ( (!strcasecmp(&who[len-4], ".vcf"))
1136 || (!strcasecmp(&who[len-4], ".ifb"))
1137 || (!strcasecmp(&who[len-4], ".vfb")) ) {
1141 lprintf(9, "freebusy requested for <%s>\n", who);
1142 serv_printf("ICAL freebusy|%s", who);
1143 serv_getln(buf, sizeof buf);
1145 if (buf[0] != '1') {
1146 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1147 output_headers(0, 0, 0, 0, 0, 0);
1148 hprintf("Content-Type: text/plain\r\n");
1149 wprintf("%s\n", &buf[4]);
1154 read_server_text(WC->WBuf, &lines);
1155 http_transmit_thing("text/calendar", 0);
1166 WebcitAddUrlHandler(HKEY("display_edit_task"), display_edit_task, 0);
1167 WebcitAddUrlHandler(HKEY("save_task"), save_task, 0);
1168 WebcitAddUrlHandler(HKEY("display_edit_event"), display_edit_event, 0);
1169 WebcitAddUrlHandler(HKEY("save_event"), save_event, 0);
1170 WebcitAddUrlHandler(HKEY("respond_to_request"), respond_to_request, 0);
1171 WebcitAddUrlHandler(HKEY("handle_rsvp"), handle_rsvp, 0);