]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Display the "Tasks" view as a bulleted list with each task clickable
[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 "webcit.h"
27 #include "webserver.h"
28
29 #ifdef HAVE_ICAL_H
30 #include <ical.h>
31 #endif
32
33
34 #ifndef HAVE_ICAL_H
35
36 /*
37  * Handler stubs for builds with no calendar library available
38  */
39 void cal_process_attachment(char *part_source) {
40
41         wprintf("<I>This message contains calendaring/scheduling information,"
42                 " but support for calendars is not available on this "
43                 "particular system.  Please ask your system administrator to "
44                 "install a new version of the Citadel web service with "
45                 "calendaring enabled.</I><BR>\n"
46         );
47
48 }
49
50 void display_calendar(long msgnum) {
51         wprintf("<i>Cannot display calendar item</i><br>\n");
52 }
53
54 void display_task(long msgnum) {
55         wprintf("<i>Cannot display item from task list</i><br>\n");
56 }
57
58 #else /* HAVE_ICAL_H */
59
60
61
62 /*
63  * Process a single calendar component.
64  * It won't be a compound component at this point because those have
65  * already been broken down by cal_process_object().
66  */
67 void cal_process_subcomponent(icalcomponent *cal) {
68         wprintf("cal_process_subcomponent() called<BR>\n");
69         wprintf("cal_process_subcomponent() exiting<BR>\n");
70 }
71
72
73
74
75
76 /*
77  * Process a calendar object
78  * ...at this point it's already been deserialized by cal_process_attachment()
79  */
80 void cal_process_object(icalcomponent *cal) {
81         icalcomponent *c;
82         int num_subcomponents = 0;
83
84         wprintf("cal_process_object() called<BR>\n");
85
86         /* Iterate through all subcomponents */
87         wprintf("Iterating through all sub-components<BR>\n");
88         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
89             (c != 0);
90             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
91                 cal_process_subcomponent(c);
92                 ++num_subcomponents;
93         }
94
95         /* Iterate through all subcomponents */
96         wprintf("Iterating through VEVENTs<BR>\n");
97         for (c = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
98             (c != 0);
99             c = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT)) {
100                 cal_process_subcomponent(c);
101                 --num_subcomponents;
102         }
103
104         /* Iterate through all subcomponents */
105         wprintf("Iterating through VTODOs<BR>\n");
106         for (c = icalcomponent_get_first_component(cal, ICAL_VTODO_COMPONENT);
107             (c != 0);
108             c = icalcomponent_get_next_component(cal, ICAL_VTODO_COMPONENT)) {
109                 cal_process_subcomponent(c);
110                 --num_subcomponents;
111         }
112
113         /* Iterate through all subcomponents */
114         wprintf("Iterating through VJOURNALs<BR>\n");
115         for (c = icalcomponent_get_first_component(cal, ICAL_VJOURNAL_COMPONENT);
116             (c != 0);
117             c = icalcomponent_get_next_component(cal, ICAL_VJOURNAL_COMPONENT)) {
118                 cal_process_subcomponent(c);
119                 --num_subcomponents;
120         }
121
122         /* Iterate through all subcomponents */
123         wprintf("Iterating through VCALENDARs<BR>\n");
124         for (c = icalcomponent_get_first_component(cal, ICAL_VCALENDAR_COMPONENT);
125             (c != 0);
126             c = icalcomponent_get_next_component(cal, ICAL_VCALENDAR_COMPONENT)) {
127                 cal_process_subcomponent(c);
128                 --num_subcomponents;
129         }
130
131         /* Iterate through all subcomponents */
132         wprintf("Iterating through VFREEBUSYs<BR>\n");
133         for (c = icalcomponent_get_first_component(cal, ICAL_VFREEBUSY_COMPONENT);
134             (c != 0);
135             c = icalcomponent_get_next_component(cal, ICAL_VFREEBUSY_COMPONENT)) {
136                 cal_process_subcomponent(c);
137                 --num_subcomponents;
138         }
139
140         /* Iterate through all subcomponents */
141         wprintf("Iterating through VALARMs<BR>\n");
142         for (c = icalcomponent_get_first_component(cal, ICAL_VALARM_COMPONENT);
143             (c != 0);
144             c = icalcomponent_get_next_component(cal, ICAL_VALARM_COMPONENT)) {
145                 cal_process_subcomponent(c);
146                 --num_subcomponents;
147         }
148
149         /* Iterate through all subcomponents */
150         wprintf("Iterating through VTIMEZONEs<BR>\n");
151         for (c = icalcomponent_get_first_component(cal, ICAL_VTIMEZONE_COMPONENT);
152             (c != 0);
153             c = icalcomponent_get_next_component(cal, ICAL_VTIMEZONE_COMPONENT)) {
154                 cal_process_subcomponent(c);
155                 --num_subcomponents;
156         }
157
158         /* Iterate through all subcomponents */
159         wprintf("Iterating through VSCHEDULEs<BR>\n");
160         for (c = icalcomponent_get_first_component(cal, ICAL_VSCHEDULE_COMPONENT);
161             (c != 0);
162             c = icalcomponent_get_next_component(cal, ICAL_VSCHEDULE_COMPONENT)) {
163                 cal_process_subcomponent(c);
164                 --num_subcomponents;
165         }
166
167         /* Iterate through all subcomponents */
168         wprintf("Iterating through VQUERYs<BR>\n");
169         for (c = icalcomponent_get_first_component(cal, ICAL_VQUERY_COMPONENT);
170             (c != 0);
171             c = icalcomponent_get_next_component(cal, ICAL_VQUERY_COMPONENT)) {
172                 cal_process_subcomponent(c);
173                 --num_subcomponents;
174         }
175
176         /* Iterate through all subcomponents */
177         wprintf("Iterating through VCARs<BR>\n");
178         for (c = icalcomponent_get_first_component(cal, ICAL_VCAR_COMPONENT);
179             (c != 0);
180             c = icalcomponent_get_next_component(cal, ICAL_VCAR_COMPONENT)) {
181                 cal_process_subcomponent(c);
182                 --num_subcomponents;
183         }
184
185         /* Iterate through all subcomponents */
186         wprintf("Iterating through VCOMMANDs<BR>\n");
187         for (c = icalcomponent_get_first_component(cal, ICAL_VCOMMAND_COMPONENT);
188             (c != 0);
189             c = icalcomponent_get_next_component(cal, ICAL_VCOMMAND_COMPONENT)) {
190                 cal_process_subcomponent(c);
191                 --num_subcomponents;
192         }
193
194         if (num_subcomponents != 0) {
195                 wprintf("Warning: %d subcomponents unhandled<BR>\n",
196                         num_subcomponents);
197         }
198
199         wprintf("cal_process_object() exiting<BR>\n");
200 }
201
202
203 /*
204  * Deserialize a calendar object in a message so it can be processed.
205  * (This is the main entry point for these things)
206  */
207 void cal_process_attachment(char *part_source) {
208         icalcomponent *cal;
209
210         wprintf("Processing calendar attachment<BR>\n");
211         cal = icalcomponent_new_from_string(part_source);
212
213         if (cal == NULL) {
214                 wprintf("Error parsing calendar object: %s<BR>\n",
215                         icalerror_strerror(icalerrno));
216                 return;
217         }
218
219         cal_process_object(cal);
220
221         /* Free the memory we obtained from libical's constructor */
222         icalcomponent_free(cal);
223 }
224
225 /*****************************************************************************/
226
227
228
229
230
231 /*
232  * Display handlers for message reading
233  */
234 void display_individual_cal(icalcomponent *cal, long msgnum) {
235         wprintf("display_individual_cal() called<BR>\n");
236 }
237
238
239
240 /*
241  * Display a task in the task list
242  */
243 void display_individual_task(icalcomponent *vtodo, long msgnum) {
244         icalproperty *p;
245
246         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
247         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
248         if (p != NULL) escputs(icalproperty_get_comment(p));
249         wprintf("</A>\n");
250         icalproperty_free(p);
251 }
252
253
254 /*
255  * Display/edit a task by itself FIXME
256  */
257 void display_edit_individual_task(icalcomponent *vtodo, long msgnum) {
258         icalproperty *p;
259
260         output_headers(3);
261         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
262                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
263                 "<B>Edit task</B>"
264                 "</FONT></TD></TR></TABLE><BR>\n"
265         );
266
267         wprintf("<FORM METHOD=\"POST\" ACTION=\"/edit_task\">\n");
268         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
269                 msgnum);
270
271         wprintf("Summary: "
272                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
273                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
274         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
275         if (p != NULL) escputs(icalproperty_get_comment(p));
276         icalproperty_free(p);
277         wprintf("\">\n");
278
279         wprintf("</FORM>\n");
280
281         wprintf("<BR><BR>\n"
282                 "<A HREF=\"/readfwd\">"
283                 "Back to task list</A>\n"
284         );
285         wDumpContent(1);
286 }
287
288 /*
289  * Code common to all display handlers.  Given a message number and a MIME
290  * type, we load the message and hunt for that MIME type.  If found, we load
291  * the relevant part, deserialize it into a libical component, filter it for
292  * the requested object type, and feed it to the specified handler.
293  */
294 void display_using_handler(long msgnum,
295                         char *mimetype,
296                         icalcomponent_kind which_kind,
297                         void (*callback)(icalcomponent *, long)
298         ) {
299         char buf[SIZ];
300         char mime_partnum[SIZ];
301         char mime_filename[SIZ];
302         char mime_content_type[SIZ];
303         char mime_disposition[SIZ];
304         int mime_length;
305         char relevant_partnum[SIZ];
306         char *relevant_source = NULL;
307         icalcomponent *cal, *c;
308
309         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
310         serv_puts(buf);
311         serv_gets(buf);
312         if (buf[0] != '1') return;
313
314         while (serv_gets(buf), strcmp(buf, "000")) {
315                 if (!strncasecmp(buf, "part=", 5)) {
316                         extract(mime_filename, &buf[5], 1);
317                         extract(mime_partnum, &buf[5], 2);
318                         extract(mime_disposition, &buf[5], 3);
319                         extract(mime_content_type, &buf[5], 4);
320                         mime_length = extract_int(&buf[5], 5);
321
322                         if (!strcasecmp(mime_content_type, "text/calendar")) {
323                                 strcpy(relevant_partnum, mime_partnum);
324                         }
325
326                 }
327         }
328
329         if (strlen(relevant_partnum) > 0) {
330                 relevant_source = load_mimepart(msgnum, relevant_partnum);
331                 if (relevant_source != NULL) {
332
333                         /* Display the task */
334                         cal = icalcomponent_new_from_string(relevant_source);
335                         if (cal != NULL) {
336                                 for (c = icalcomponent_get_first_component(cal,
337                                     which_kind);
338                                     (c != 0);
339                                     c = icalcomponent_get_next_component(cal,
340                                     which_kind)) {
341                                         callback(c, msgnum);
342                                 }
343                                 icalcomponent_free(cal);
344                         }
345                         free(relevant_source);
346                 }
347         }
348
349 }
350
351 void display_calendar(long msgnum) {
352         display_using_handler(msgnum, "text/calendar",
353                                 ICAL_ANY_COMPONENT,
354                                 display_individual_cal);
355 }
356
357 void display_task(long msgnum) {
358         display_using_handler(msgnum, "text/calendar",
359                                 ICAL_VTODO_COMPONENT,
360                                 display_individual_task);
361 }
362
363 void display_edit_task(void) {
364         long msgnum = 0L;
365
366         msgnum = atol(bstr("msgnum"));
367         display_using_handler(msgnum, "text/calendar",
368                                 ICAL_VTODO_COMPONENT,
369                                 display_edit_individual_task);
370 }
371
372 #endif /* HAVE_ICAL_H */