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