* Added utility functions for displaying vcalendar timestamps in web forms
[citadel.git] / webcit / calendar.c
1 /*
2  * $Id$
3  *
4  * Functions which handle calendar objects and their processing/display.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30 #ifndef HAVE_ICAL_H
31
32 /*
33  * Handler stubs for builds with no calendar library available
34  */
35 void cal_process_attachment(char *part_source) {
36
37         wprintf("<I>This message contains calendaring/scheduling information,"
38                 " but support for calendars is not available on this "
39                 "particular system.  Please ask your system administrator to "
40                 "install a new version of the Citadel web service with "
41                 "calendaring enabled.</I><BR>\n"
42         );
43
44 }
45
46 void display_calendar(long msgnum) {
47         wprintf("<i>Cannot display calendar item</i><br>\n");
48 }
49
50 void display_task(long msgnum) {
51         wprintf("<i>Cannot display item from task list</i><br>\n");
52 }
53
54 #else /* HAVE_ICAL_H */
55
56
57 /******   End of handler stubs.  Everything below this line is real.   ******/
58
59
60 /*
61  * Process a single calendar component.
62  * It won't be a compound component at this point because those have
63  * already been broken down by cal_process_object().
64  */
65 void cal_process_subcomponent(icalcomponent *cal) {
66         wprintf("cal_process_subcomponent() called<BR>\n");
67         wprintf("cal_process_subcomponent() exiting<BR>\n");
68 }
69
70
71
72
73
74 /*
75  * Process a calendar object
76  * ...at this point it's already been deserialized by cal_process_attachment()
77  */
78 void cal_process_object(icalcomponent *cal) {
79         icalcomponent *c;
80         int num_subcomponents = 0;
81
82         wprintf("cal_process_object() called<BR>\n");
83
84         /* Iterate through all subcomponents */
85         wprintf("Iterating through all sub-components<BR>\n");
86         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
87             (c != 0);
88             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
89                 cal_process_subcomponent(c);
90                 ++num_subcomponents;
91         }
92
93         /* Iterate through all subcomponents */
94         wprintf("Iterating through VEVENTs<BR>\n");
95         for (c = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
96             (c != 0);
97             c = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT)) {
98                 cal_process_subcomponent(c);
99                 --num_subcomponents;
100         }
101
102         /* Iterate through all subcomponents */
103         wprintf("Iterating through VTODOs<BR>\n");
104         for (c = icalcomponent_get_first_component(cal, ICAL_VTODO_COMPONENT);
105             (c != 0);
106             c = icalcomponent_get_next_component(cal, ICAL_VTODO_COMPONENT)) {
107                 cal_process_subcomponent(c);
108                 --num_subcomponents;
109         }
110
111         /* Iterate through all subcomponents */
112         wprintf("Iterating through VJOURNALs<BR>\n");
113         for (c = icalcomponent_get_first_component(cal, ICAL_VJOURNAL_COMPONENT);
114             (c != 0);
115             c = icalcomponent_get_next_component(cal, ICAL_VJOURNAL_COMPONENT)) {
116                 cal_process_subcomponent(c);
117                 --num_subcomponents;
118         }
119
120         /* Iterate through all subcomponents */
121         wprintf("Iterating through VCALENDARs<BR>\n");
122         for (c = icalcomponent_get_first_component(cal, ICAL_VCALENDAR_COMPONENT);
123             (c != 0);
124             c = icalcomponent_get_next_component(cal, ICAL_VCALENDAR_COMPONENT)) {
125                 cal_process_subcomponent(c);
126                 --num_subcomponents;
127         }
128
129         /* Iterate through all subcomponents */
130         wprintf("Iterating through VFREEBUSYs<BR>\n");
131         for (c = icalcomponent_get_first_component(cal, ICAL_VFREEBUSY_COMPONENT);
132             (c != 0);
133             c = icalcomponent_get_next_component(cal, ICAL_VFREEBUSY_COMPONENT)) {
134                 cal_process_subcomponent(c);
135                 --num_subcomponents;
136         }
137
138         /* Iterate through all subcomponents */
139         wprintf("Iterating through VALARMs<BR>\n");
140         for (c = icalcomponent_get_first_component(cal, ICAL_VALARM_COMPONENT);
141             (c != 0);
142             c = icalcomponent_get_next_component(cal, ICAL_VALARM_COMPONENT)) {
143                 cal_process_subcomponent(c);
144                 --num_subcomponents;
145         }
146
147         /* Iterate through all subcomponents */
148         wprintf("Iterating through VTIMEZONEs<BR>\n");
149         for (c = icalcomponent_get_first_component(cal, ICAL_VTIMEZONE_COMPONENT);
150             (c != 0);
151             c = icalcomponent_get_next_component(cal, ICAL_VTIMEZONE_COMPONENT)) {
152                 cal_process_subcomponent(c);
153                 --num_subcomponents;
154         }
155
156         /* Iterate through all subcomponents */
157         wprintf("Iterating through VSCHEDULEs<BR>\n");
158         for (c = icalcomponent_get_first_component(cal, ICAL_VSCHEDULE_COMPONENT);
159             (c != 0);
160             c = icalcomponent_get_next_component(cal, ICAL_VSCHEDULE_COMPONENT)) {
161                 cal_process_subcomponent(c);
162                 --num_subcomponents;
163         }
164
165         /* Iterate through all subcomponents */
166         wprintf("Iterating through VQUERYs<BR>\n");
167         for (c = icalcomponent_get_first_component(cal, ICAL_VQUERY_COMPONENT);
168             (c != 0);
169             c = icalcomponent_get_next_component(cal, ICAL_VQUERY_COMPONENT)) {
170                 cal_process_subcomponent(c);
171                 --num_subcomponents;
172         }
173
174         /* Iterate through all subcomponents */
175         wprintf("Iterating through VCARs<BR>\n");
176         for (c = icalcomponent_get_first_component(cal, ICAL_VCAR_COMPONENT);
177             (c != 0);
178             c = icalcomponent_get_next_component(cal, ICAL_VCAR_COMPONENT)) {
179                 cal_process_subcomponent(c);
180                 --num_subcomponents;
181         }
182
183         /* Iterate through all subcomponents */
184         wprintf("Iterating through VCOMMANDs<BR>\n");
185         for (c = icalcomponent_get_first_component(cal, ICAL_VCOMMAND_COMPONENT);
186             (c != 0);
187             c = icalcomponent_get_next_component(cal, ICAL_VCOMMAND_COMPONENT)) {
188                 cal_process_subcomponent(c);
189                 --num_subcomponents;
190         }
191
192         if (num_subcomponents != 0) {
193                 wprintf("Warning: %d subcomponents unhandled<BR>\n",
194                         num_subcomponents);
195         }
196
197         wprintf("cal_process_object() exiting<BR>\n");
198 }
199
200
201 /*
202  * Deserialize a calendar object in a message so it can be processed.
203  * (This is the main entry point for these things)
204  */
205 void cal_process_attachment(char *part_source) {
206         icalcomponent *cal;
207
208         wprintf("Processing calendar attachment<BR>\n");
209         cal = icalcomponent_new_from_string(part_source);
210
211         if (cal == NULL) {
212                 wprintf("Error parsing calendar object: %s<BR>\n",
213                         icalerror_strerror(icalerrno));
214                 return;
215         }
216
217         cal_process_object(cal);
218
219         /* Free the memory we obtained from libical's constructor */
220         icalcomponent_free(cal);
221 }
222
223 /*****************************************************************************/
224
225
226
227
228
229 /*
230  * Display handlers for message reading
231  */
232 void display_individual_cal(icalcomponent *cal, long msgnum) {
233         wprintf("display_individual_cal() called<BR>\n");
234 }
235
236
237
238 /*
239  * Display a task in the task list
240  */
241 void display_individual_task(icalcomponent *vtodo, long msgnum) {
242         icalproperty *p;
243
244         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
245         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
246         if (p != NULL) {
247                 escputs((char *)icalproperty_get_comment(p));
248         }
249         wprintf("</A>\n");
250 }
251
252
253 /*
254  * Display a task by itself (for editing)
255  */
256 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
257         icalcomponent *vtodo;
258         icalproperty *p;
259         struct icaltimetype t;
260         time_t now;
261         int created_new_vtodo = 0;
262
263         now = time(NULL);
264
265         if (supplied_vtodo != NULL) {
266                 vtodo = supplied_vtodo;
267         }
268         else {
269                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
270                 created_new_vtodo = 1;
271         }
272
273         output_headers(3);
274         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
275                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
276                 "<B>Edit task</B>"
277                 "</FONT></TD></TR></TABLE><BR>\n"
278         );
279
280         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
281         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
282                 msgnum);
283
284         wprintf("Summary: "
285                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
286                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
287         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
288         if (p != NULL) {
289                 escputs((char *)icalproperty_get_comment(p));
290         }
291         wprintf("\"><BR>\n");
292
293         wprintf("Start date: ");
294         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
295         if (p != NULL) {
296                 t = icalproperty_get_dtstart(p);
297         }
298         else {
299                 t = icaltime_from_timet(now, 0);
300         }
301         display_icaltimetype_as_webform(&t, "dtstart");
302         wprintf("<BR>\n");
303
304         wprintf("Due date: ");
305         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
306         if (p != NULL) {
307                 t = icalproperty_get_due(p);
308         }
309         else {
310                 t = icaltime_from_timet(now, 0);
311         }
312         display_icaltimetype_as_webform(&t, "due");
313         wprintf("<BR>\n");
314
315         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
316                 "ROWS=10 COLS=80 WIDTH=80>\n"
317         );
318         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
319         if (p != NULL) {
320                 escputs((char *)icalproperty_get_comment(p));
321         }
322         wprintf("</TEXTAREA><BR>\n");
323
324         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
325                 "&nbsp;&nbsp;"
326                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
327                 "&nbsp;&nbsp;"
328                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
329                 "</CENTER>\n"
330         );
331
332         wprintf("</FORM>\n");
333
334         wDumpContent(1);
335
336         if (created_new_vtodo) {
337                 icalcomponent_free(vtodo);
338         }
339 }
340
341 /*
342  * Save an edited task
343  */
344 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
345         char buf[SIZ];
346         int delete_existing = 0;
347         icalproperty *prop;
348         icalcomponent *vtodo;
349         int created_new_vtodo = 0;
350
351         if (supplied_vtodo != NULL) {
352                 vtodo = supplied_vtodo;
353         }
354         else {
355                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
356                 created_new_vtodo = 1;
357         }
358
359         if (!strcasecmp(bstr("sc"), "Save")) {
360
361                 /* Replace values in the component with ones from the form */
362
363                 while (prop = icalcomponent_get_first_property(vtodo,
364                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
365                         icalcomponent_remove_property(vtodo, prop);
366                 }
367                 icalcomponent_add_property(vtodo,
368                         icalproperty_new_summary(bstr("summary")));
369                 
370                 while (prop = icalcomponent_get_first_property(vtodo,
371                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
372                         icalcomponent_remove_property(vtodo, prop);
373                 }
374                 icalcomponent_add_property(vtodo,
375                         icalproperty_new_description(bstr("description")));
376         
377                 while (prop = icalcomponent_get_first_property(vtodo,
378                       ICAL_DTSTART_PROPERTY), prop != NULL) {
379                         icalcomponent_remove_property(vtodo, prop);
380                 }
381                 icalcomponent_add_property(vtodo,
382                         icalproperty_new_dtstart(
383                                 icaltime_from_webform("dtstart")
384                         )
385                 );
386         
387                 while (prop = icalcomponent_get_first_property(vtodo,
388                       ICAL_DUE_PROPERTY), prop != NULL) {
389                         icalcomponent_remove_property(vtodo, prop);
390                 }
391                 icalcomponent_add_property(vtodo,
392                         icalproperty_new_due(
393                                 icaltime_from_webform("due")
394                         )
395                 );
396         
397                 /* Serialize it and save it to the message base */
398                 serv_puts("ENT0 1|||4");
399                 serv_gets(buf);
400                 if (buf[0] == '4') {
401                         serv_puts("Content-type: text/calendar");
402                         serv_puts("");
403                         serv_puts(icalcomponent_as_ical_string(vtodo));
404                         serv_puts("000");
405                         delete_existing = 1;
406                 }
407         }
408
409         /*
410          * If the user clicked 'Delete' then delete it, period.
411          */
412         if (!strcasecmp(bstr("sc"), "Delete")) {
413                 delete_existing = 1;
414         }
415
416         if ( (delete_existing) && (msgnum > 0L) ) {
417                 serv_printf("DELE %ld", atol(bstr("msgnum")));
418                 serv_gets(buf);
419         }
420
421         if (created_new_vtodo) {
422                 icalcomponent_free(vtodo);
423         }
424
425         /* Go back to the task list */
426         readloop("readfwd");
427 }
428
429
430
431 /*
432  * Code common to all display handlers.  Given a message number and a MIME
433  * type, we load the message and hunt for that MIME type.  If found, we load
434  * the relevant part, deserialize it into a libical component, filter it for
435  * the requested object type, and feed it to the specified handler.
436  */
437 void display_using_handler(long msgnum,
438                         char *mimetype,
439                         icalcomponent_kind which_kind,
440                         void (*callback)(icalcomponent *, long)
441         ) {
442         char buf[SIZ];
443         char mime_partnum[SIZ];
444         char mime_filename[SIZ];
445         char mime_content_type[SIZ];
446         char mime_disposition[SIZ];
447         int mime_length;
448         char relevant_partnum[SIZ];
449         char *relevant_source = NULL;
450         icalcomponent *cal, *c;
451
452         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
453         serv_puts(buf);
454         serv_gets(buf);
455         if (buf[0] != '1') return;
456
457         while (serv_gets(buf), strcmp(buf, "000")) {
458                 if (!strncasecmp(buf, "part=", 5)) {
459                         extract(mime_filename, &buf[5], 1);
460                         extract(mime_partnum, &buf[5], 2);
461                         extract(mime_disposition, &buf[5], 3);
462                         extract(mime_content_type, &buf[5], 4);
463                         mime_length = extract_int(&buf[5], 5);
464
465                         if (!strcasecmp(mime_content_type, "text/calendar")) {
466                                 strcpy(relevant_partnum, mime_partnum);
467                         }
468
469                 }
470         }
471
472         if (strlen(relevant_partnum) > 0) {
473                 relevant_source = load_mimepart(msgnum, relevant_partnum);
474                 if (relevant_source != NULL) {
475
476                         cal = icalcomponent_new_from_string(relevant_source);
477                         if (cal != NULL) {
478
479                                 /* Simple components of desired type */
480                                 if (icalcomponent_isa(cal) == which_kind) {
481                                         callback(cal, msgnum);
482                                 }
483
484                                 /* Subcomponents of desired type */
485                                 for (c = icalcomponent_get_first_component(cal,
486                                     which_kind);
487                                     (c != 0);
488                                     c = icalcomponent_get_next_component(cal,
489                                     which_kind)) {
490                                         callback(c, msgnum);
491                                 }
492                                 icalcomponent_free(cal);
493                         }
494                         free(relevant_source);
495                 }
496         }
497
498 }
499
500 void display_calendar(long msgnum) {
501         display_using_handler(msgnum, "text/calendar",
502                                 ICAL_ANY_COMPONENT,
503                                 display_individual_cal);
504 }
505
506 void display_task(long msgnum) {
507         display_using_handler(msgnum, "text/calendar",
508                                 ICAL_VTODO_COMPONENT,
509                                 display_individual_task);
510 }
511
512 void display_edit_task(void) {
513         long msgnum = 0L;
514
515         msgnum = atol(bstr("msgnum"));
516         if (msgnum > 0L) {
517                 /* existing task */
518                 display_using_handler(msgnum, "text/calendar",
519                                 ICAL_VTODO_COMPONENT,
520                                 display_edit_individual_task);
521         }
522         else {
523                 /* new task */
524                 display_edit_individual_task(NULL, 0L);
525         }
526 }
527
528 void save_task(void) {
529         long msgnum = 0L;
530
531         msgnum = atol(bstr("msgnum"));
532         if (msgnum > 0L) {
533                 display_using_handler(msgnum, "text/calendar",
534                                 ICAL_VTODO_COMPONENT,
535                                 save_individual_task);
536         }
537         else {
538                 save_individual_task(NULL, 0L);
539         }
540 }
541
542 #endif /* HAVE_ICAL_H */