* Worked on meeting accept/decline a little bit
[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>"
48                 "Cannot display calendar item.  You are seeing this error "
49                 "because your WebCit service has not been installed with "
50                 "calendar support.  Please contact your system administrator."
51                 "</i><br>\n");
52 }
53
54 void display_task(long msgnum) {
55         wprintf("<i>"
56                 "Cannot display to-do item.  You are seeing this error "
57                 "because your WebCit service has not been installed with "
58                 "calendar support.  Please contact your system administrator."
59                 "</i><br>\n");
60 }
61
62 #else /* HAVE_ICAL_H */
63
64
65 /******   End of handler stubs.  Everything below this line is real.   ******/
66
67
68
69
70 /*
71  * Process a calendar object
72  * ...at this point it's already been deserialized by cal_process_attachment()
73  */
74 void cal_process_object(icalcomponent *cal,
75                         int recursion_level
76 ) {
77         icalcomponent *c;
78         icalproperty *method = NULL;
79         icalproperty_method the_method;
80         icalproperty *p;
81         struct icaltimetype t;
82         time_t tt;
83
84         /* Look for a method */
85         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
86
87         /* See what we need to do with this */
88         if (method != NULL) {
89                 the_method = icalproperty_get_method(method);
90                 switch(the_method) {
91                     case ICAL_METHOD_REQUEST:
92                         wprintf("<CENTER><IMG ALIGN=CENTER "
93                                 "SRC=\"/static/vcalendar.gif\">"
94                                 "&nbsp;&nbsp;"  
95                                 "<B>Meeting invitation</B></CENTER><BR>\n"
96                         );
97                         break;
98                     default:
99                         wprintf("I don't know what to do with this.<BR>\n");
100                         break;
101                 }
102         }
103
104         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
105         if (p != NULL) {
106                 wprintf("<B>Summary:</B> ");
107                 escputs((char *)icalproperty_get_comment(p));
108                 wprintf("<BR>\n");
109         }
110
111         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
112         if (p != NULL) {
113                 wprintf("<B>Location:</B> ");
114                 escputs((char *)icalproperty_get_comment(p));
115                 wprintf("<BR>\n");
116         }
117
118         p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
119         if (p != NULL) {
120                 t = icalproperty_get_dtstart(p);
121                 tt = icaltime_as_timet(t);
122                 wprintf("<B>Starting date/time:</B> %s<BR>",
123                         asctime(localtime(&tt))
124                 );
125         }
126
127         p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
128         if (p != NULL) {
129                 t = icalproperty_get_dtstart(p);
130                 tt = icaltime_as_timet(t);
131                 wprintf("<B>Ending date/time:</B> %s<BR>",
132                         asctime(localtime(&tt))
133                 );
134         }
135
136         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
137         if (p != NULL) {
138                 wprintf("<B>Description:</B> ");
139                 escputs((char *)icalproperty_get_comment(p));
140                 wprintf("<BR>\n");
141         }
142
143         /* If the component has subcomponents, recurse through them. */
144         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
145             (c != 0);
146             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
147                 /* Recursively process subcomponent */
148                 cal_process_object(c, recursion_level+1);
149         }
150
151         if (recursion_level == 0) {
152                 wprintf("<CENTER><FORM METHOD=\"GET\" "
153                         "ACTION=\"/respond_to_request\">\n"
154                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
155                                 "VALUE=\"Accept\">"
156                         "&nbsp;&nbsp;"
157                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
158                                 "VALUE=\"Decline\">"
159                         "</FORM></CENTER>\n"
160                 );
161         }
162 }
163
164
165 /*
166  * Deserialize a calendar object in a message so it can be processed.
167  * (This is the main entry point for these things)
168  */
169 void cal_process_attachment(char *part_source) {
170         icalcomponent *cal;
171
172         cal = icalcomponent_new_from_string(part_source);
173
174         if (cal == NULL) {
175                 wprintf("Error parsing calendar object: %s<BR>\n",
176                         icalerror_strerror(icalerrno));
177                 return;
178         }
179
180         cal_process_object(cal, 0);
181
182         /* Free the memory we obtained from libical's constructor */
183         icalcomponent_free(cal);
184 }
185
186 /*****************************************************************************/
187
188
189
190 /*
191  * Display handlers for message reading
192  */
193
194
195
196 /*
197  * If we're reading calendar items, just store them for now.  We have to
198  * sort and re-output them later when we draw the calendar.
199  */
200 void display_individual_cal(icalcomponent *cal, long msgnum) {
201
202         WC->num_cal += 1;
203
204         WC->disp_cal = realloc(WC->disp_cal,
205                         (sizeof(icalcomponent *) * WC->num_cal) );
206         WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
207
208         WC->cal_msgnum = realloc(WC->cal_msgnum,
209                         (sizeof(long) * WC->num_cal) );
210         WC->cal_msgnum[WC->num_cal - 1] = msgnum;
211 }
212
213
214
215 /*
216  * Display a task in the task list
217  */
218 void display_individual_task(icalcomponent *vtodo, long msgnum) {
219         icalproperty *p;
220
221         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
222         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
223         if (p != NULL) {
224                 escputs((char *)icalproperty_get_comment(p));
225         }
226         wprintf("</A>\n");
227 }
228
229
230 /*
231  * Display a task by itself (for editing)
232  */
233 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
234         icalcomponent *vtodo;
235         icalproperty *p;
236         struct icaltimetype t;
237         time_t now;
238         int created_new_vtodo = 0;
239
240         now = time(NULL);
241
242         if (supplied_vtodo != NULL) {
243                 vtodo = supplied_vtodo;
244         }
245         else {
246                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
247                 created_new_vtodo = 1;
248         }
249
250         output_headers(3);
251         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
252                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
253                 "<B>Edit task</B>"
254                 "</FONT></TD></TR></TABLE><BR>\n"
255         );
256
257         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
258         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
259                 msgnum);
260
261         wprintf("Summary: "
262                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
263                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
264         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
265         if (p != NULL) {
266                 escputs((char *)icalproperty_get_comment(p));
267         }
268         wprintf("\"><BR>\n");
269
270         wprintf("Start date: ");
271         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
272         if (p != NULL) {
273                 t = icalproperty_get_dtstart(p);
274         }
275         else {
276                 t = icaltime_from_timet(now, 0);
277         }
278         display_icaltimetype_as_webform(&t, "dtstart");
279         wprintf("<BR>\n");
280
281         wprintf("Due date: ");
282         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
283         if (p != NULL) {
284                 t = icalproperty_get_due(p);
285         }
286         else {
287                 t = icaltime_from_timet(now, 0);
288         }
289         display_icaltimetype_as_webform(&t, "due");
290         wprintf("<BR>\n");
291
292         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
293                 "ROWS=10 COLS=80 WIDTH=80>\n"
294         );
295         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
296         if (p != NULL) {
297                 escputs((char *)icalproperty_get_comment(p));
298         }
299         wprintf("</TEXTAREA><BR>\n");
300
301         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
302                 "&nbsp;&nbsp;"
303                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
304                 "&nbsp;&nbsp;"
305                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
306                 "</CENTER>\n"
307         );
308
309         wprintf("</FORM>\n");
310
311         wDumpContent(1);
312
313         if (created_new_vtodo) {
314                 icalcomponent_free(vtodo);
315         }
316 }
317
318 /*
319  * Save an edited task
320  */
321 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
322         char buf[SIZ];
323         int delete_existing = 0;
324         icalproperty *prop;
325         icalcomponent *vtodo;
326         int created_new_vtodo = 0;
327
328         if (supplied_vtodo != NULL) {
329                 vtodo = supplied_vtodo;
330         }
331         else {
332                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
333                 created_new_vtodo = 1;
334         }
335
336         if (!strcasecmp(bstr("sc"), "Save")) {
337
338                 /* Replace values in the component with ones from the form */
339
340                 while (prop = icalcomponent_get_first_property(vtodo,
341                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
342                         icalcomponent_remove_property(vtodo, prop);
343                 }
344                 icalcomponent_add_property(vtodo,
345                         icalproperty_new_summary(bstr("summary")));
346                 
347                 while (prop = icalcomponent_get_first_property(vtodo,
348                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
349                         icalcomponent_remove_property(vtodo, prop);
350                 }
351                 icalcomponent_add_property(vtodo,
352                         icalproperty_new_description(bstr("description")));
353         
354                 while (prop = icalcomponent_get_first_property(vtodo,
355                       ICAL_DTSTART_PROPERTY), prop != NULL) {
356                         icalcomponent_remove_property(vtodo, prop);
357                 }
358                 icalcomponent_add_property(vtodo,
359                         icalproperty_new_dtstart(
360                                 icaltime_from_webform("dtstart")
361                         )
362                 );
363         
364                 while (prop = icalcomponent_get_first_property(vtodo,
365                       ICAL_DUE_PROPERTY), prop != NULL) {
366                         icalcomponent_remove_property(vtodo, prop);
367                 }
368                 icalcomponent_add_property(vtodo,
369                         icalproperty_new_due(
370                                 icaltime_from_webform("due")
371                         )
372                 );
373         
374                 /* Serialize it and save it to the message base */
375                 serv_puts("ENT0 1|||4");
376                 serv_gets(buf);
377                 if (buf[0] == '4') {
378                         serv_puts("Content-type: text/calendar");
379                         serv_puts("");
380                         serv_puts(icalcomponent_as_ical_string(vtodo));
381                         serv_puts("000");
382                         delete_existing = 1;
383                 }
384         }
385
386         /*
387          * If the user clicked 'Delete' then delete it, period.
388          */
389         if (!strcasecmp(bstr("sc"), "Delete")) {
390                 delete_existing = 1;
391         }
392
393         if ( (delete_existing) && (msgnum > 0L) ) {
394                 serv_printf("DELE %ld", atol(bstr("msgnum")));
395                 serv_gets(buf);
396         }
397
398         if (created_new_vtodo) {
399                 icalcomponent_free(vtodo);
400         }
401
402         /* Go back to the task list */
403         readloop("readfwd");
404 }
405
406
407
408 /*
409  * Code common to all display handlers.  Given a message number and a MIME
410  * type, we load the message and hunt for that MIME type.  If found, we load
411  * the relevant part, deserialize it into a libical component, filter it for
412  * the requested object type, and feed it to the specified handler.
413  */
414 void display_using_handler(long msgnum,
415                         char *mimetype,
416                         icalcomponent_kind which_kind,
417                         void (*callback)(icalcomponent *, long)
418         ) {
419         char buf[SIZ];
420         char mime_partnum[SIZ];
421         char mime_filename[SIZ];
422         char mime_content_type[SIZ];
423         char mime_disposition[SIZ];
424         int mime_length;
425         char relevant_partnum[SIZ];
426         char *relevant_source = NULL;
427         icalcomponent *cal, *c;
428
429         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
430         serv_puts(buf);
431         serv_gets(buf);
432         if (buf[0] != '1') return;
433
434         while (serv_gets(buf), strcmp(buf, "000")) {
435                 if (!strncasecmp(buf, "part=", 5)) {
436                         extract(mime_filename, &buf[5], 1);
437                         extract(mime_partnum, &buf[5], 2);
438                         extract(mime_disposition, &buf[5], 3);
439                         extract(mime_content_type, &buf[5], 4);
440                         mime_length = extract_int(&buf[5], 5);
441
442                         if (!strcasecmp(mime_content_type, "text/calendar")) {
443                                 strcpy(relevant_partnum, mime_partnum);
444                         }
445
446                 }
447         }
448
449         if (strlen(relevant_partnum) > 0) {
450                 relevant_source = load_mimepart(msgnum, relevant_partnum);
451                 if (relevant_source != NULL) {
452
453                         cal = icalcomponent_new_from_string(relevant_source);
454                         if (cal != NULL) {
455
456                                 /* Simple components of desired type */
457                                 if (icalcomponent_isa(cal) == which_kind) {
458                                         callback(cal, msgnum);
459                                 }
460
461                                 /* Subcomponents of desired type */
462                                 for (c = icalcomponent_get_first_component(cal,
463                                     which_kind);
464                                     (c != 0);
465                                     c = icalcomponent_get_next_component(cal,
466                                     which_kind)) {
467                                         callback(c, msgnum);
468                                 }
469                                 icalcomponent_free(cal);
470                         }
471                         free(relevant_source);
472                 }
473         }
474
475 }
476
477 void display_calendar(long msgnum) {
478         display_using_handler(msgnum, "text/calendar",
479                                 ICAL_VEVENT_COMPONENT,
480                                 display_individual_cal);
481 }
482
483 void display_task(long msgnum) {
484         display_using_handler(msgnum, "text/calendar",
485                                 ICAL_VTODO_COMPONENT,
486                                 display_individual_task);
487 }
488
489 void display_edit_task(void) {
490         long msgnum = 0L;
491
492         msgnum = atol(bstr("msgnum"));
493         if (msgnum > 0L) {
494                 /* existing task */
495                 display_using_handler(msgnum, "text/calendar",
496                                 ICAL_VTODO_COMPONENT,
497                                 display_edit_individual_task);
498         }
499         else {
500                 /* new task */
501                 display_edit_individual_task(NULL, 0L);
502         }
503 }
504
505 void save_task(void) {
506         long msgnum = 0L;
507
508         msgnum = atol(bstr("msgnum"));
509         if (msgnum > 0L) {
510                 display_using_handler(msgnum, "text/calendar",
511                                 ICAL_VTODO_COMPONENT,
512                                 save_individual_task);
513         }
514         else {
515                 save_individual_task(NULL, 0L);
516         }
517 }
518
519 void display_edit_event(void) {
520         long msgnum = 0L;
521
522         msgnum = atol(bstr("msgnum"));
523         if (msgnum > 0L) {
524                 /* existing event */
525                 display_using_handler(msgnum, "text/calendar",
526                                 ICAL_VEVENT_COMPONENT,
527                                 display_edit_individual_event);
528         }
529         else {
530                 /* new event */
531                 display_edit_individual_event(NULL, 0L);
532         }
533 }
534
535 void save_event(void) {
536         long msgnum = 0L;
537
538         msgnum = atol(bstr("msgnum"));
539
540         if (msgnum > 0L) {
541                 display_using_handler(msgnum, "text/calendar",
542                                 ICAL_VEVENT_COMPONENT,
543                                 save_individual_event);
544         }
545         else {
546                 save_individual_event(NULL, 0L);
547         }
548 }
549
550 #endif /* HAVE_ICAL_H */