]> code.citadel.org Git - citadel.git/blob - webcit/event.c
* cosmetics
[citadel.git] / webcit / event.c
1 /*
2  * $Id$
3  *
4  * Editing calendar events.
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
31 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
32
33 /*
34  * Display an event by itself (for editing)
35  */
36 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
37         icalcomponent *vevent;
38         icalproperty *p;
39         icalvalue *v;
40         struct icaltimetype t_start, t_end;
41         time_t now;
42         struct tm tm_now;
43         int created_new_vevent = 0;
44         icalproperty *organizer = NULL;
45         char organizer_string[SIZ];
46         icalproperty *attendee = NULL;
47         char attendee_string[SIZ];
48         char buf[SIZ];
49         int i;
50         int organizer_is_me = 0;
51         int sequence = 0;
52
53         now = time(NULL) % 60;          /* mod 60 to force :00 seconds */
54         strcpy(organizer_string, "");
55         strcpy(attendee_string, "");
56
57         if (supplied_vevent != NULL) {
58                 vevent = supplied_vevent;
59                 /* If we're looking at a fully encapsulated VCALENDAR
60                  * rather than a VTODO component, attempt to use the first
61                  * relevant VTODO subcomponent.  If there is none, the
62                  * NULL returned by icalcomponent_get_first_component() will
63                  * tell the next iteration of this function to create a
64                  * new one.
65                  */
66                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
67                         display_edit_individual_event(
68                                 icalcomponent_get_first_component(
69                                         vevent, ICAL_VEVENT_COMPONENT
70                                 ), msgnum
71                         );
72                         return;
73                 }
74         }
75         else {
76                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
77                 created_new_vevent = 1;
78         }
79
80         /* Learn the sequence */
81         p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
82         if (p != NULL) {
83                 sequence = icalproperty_get_sequence(p);
84         }
85
86         /* Begin output */
87         output_headers(3);
88         do_template("beginbox_nt");
89         wprintf("<h3>&nbsp;<IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
90                 "&nbsp;Add or edit an event</h3>\n");
91
92         /************************************************************
93          * Uncomment this to see the UID in calendar events for debugging
94         wprintf("UID == ");
95         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
96         if (p != NULL) {
97                 escputs((char *)icalproperty_get_comment(p));
98         }
99         wprintf("<BR>\n");
100         wprintf("SEQUENCE == %d<BR>\n", sequence);
101         *************************************************************/
102
103         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
104
105         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
106                 msgnum);
107         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
108                 bstr("calview"));
109         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
110                 bstr("year"));
111         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
112                 bstr("month"));
113         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
114                 bstr("day"));
115
116         /* Put it in a borderless table so it lines up nicely */
117         wprintf("<TABLE border=0 width=100%%>\n");
118
119         wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
120                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
121                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
122         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
123         if (p != NULL) {
124                 escputs((char *)icalproperty_get_comment(p));
125         }
126         wprintf("\"></TD></TR>\n");
127
128         wprintf("<TR><TD><B>Location</B></TD><TD>\n"
129                 "<INPUT TYPE=\"text\" NAME=\"location\" "
130                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
131         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
132         if (p != NULL) {
133                 escputs((char *)icalproperty_get_comment(p));
134         }
135         wprintf("\"></TD></TR>\n");
136
137         wprintf("<TR><TD><B>Start</B></TD><TD>\n");
138         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
139         if (p != NULL) {
140                 t_start = icalproperty_get_dtstart(p);
141                 if (t_start.is_date) {
142                         t_start.hour = 0;
143                         t_start.minute = 0;
144                         t_start.second = 0;
145                 }
146         }
147         else {
148                 memcpy(&tm_now, localtime(&now), sizeof(struct tm));
149                 tm_now.tm_year = atoi(bstr("year")) - 1900;
150                 tm_now.tm_mon = atoi(bstr("month")) - 1;
151                 tm_now.tm_mday = atoi(bstr("day"));
152                 if (strlen(bstr("hour")) > 0) {
153                         tm_now.tm_hour = atoi(bstr("hour"));
154                         tm_now.tm_min = atoi(bstr("minute"));
155                         tm_now.tm_sec = 0;
156                 }
157                 else {
158                         tm_now.tm_hour = 9;
159                         tm_now.tm_min = 0;
160                         tm_now.tm_sec = 0;
161                 }
162
163                 t_start = icaltime_from_timet_with_zone(
164                         mktime(&tm_now),
165                         ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
166                         icaltimezone_get_utc_timezone
167                 );
168                 t_start.is_utc = 1;
169
170         }
171         display_icaltimetype_as_webform(&t_start, "dtstart");
172
173         wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
174                 "VALUE=\"yes\" onClick=\"
175
176                         if (this.checked) {
177                                 this.form.dtstart_hour.value='0';
178                                 this.form.dtstart_hour.disabled = true;
179                                 this.form.dtstart_minute.value='0';
180                                 this.form.dtstart_minute.disabled = true;
181                                 this.form.dtend_hour.value='0';
182                                 this.form.dtend_hour.disabled = true;
183                                 this.form.dtend_minute.value='0';
184                                 this.form.dtend_minute.disabled = true;
185                                 this.form.dtend_month.disabled = true;
186                                 this.form.dtend_day.disabled = true;
187                                 this.form.dtend_year.disabled = true;
188                         }
189                         else {
190                                 this.form.dtstart_hour.disabled = false;
191                                 this.form.dtstart_minute.disabled = false;
192                                 this.form.dtend_hour.disabled = false;
193                                 this.form.dtend_minute.disabled = false;
194                                 this.form.dtend_month.disabled = false;
195                                 this.form.dtend_day.disabled = false;
196                                 this.form.dtend_year.disabled = false;
197                         }
198
199
200                 \" %s >All day event",
201                 (t_start.is_date ? "CHECKED" : "" )
202         );
203
204         wprintf("</TD></TR>\n");
205
206         /* If this is an all-day-event, set the end time to be identical to
207          * the start time (the hour/minute/second will be set to midnight).
208          * Otherwise extract or create it.
209          */
210         wprintf("<TR><TD><B>End</B></TD><TD>\n");
211         if (t_start.is_date) {
212                 t_end = t_start;
213         }
214         else {
215                 p = icalcomponent_get_first_property(vevent,
216                                                         ICAL_DTEND_PROPERTY);
217                 if (p != NULL) {
218                         t_end = icalproperty_get_dtend(p);
219                 }
220                 else {
221                         /* If this is not an all-day event and there is no
222                          * end time specified, make the default one hour
223                          * from the start time.
224                          */
225                         t_end = t_start;
226                         t_end.hour += 1;
227                         t_end.second = 0;
228                         t_end = icaltime_normalize(t_end);
229                         /* t_end = icaltime_from_timet(now, 0); */
230                 }
231         }
232         display_icaltimetype_as_webform(&t_end, "dtend");
233         wprintf("</TD></TR>\n");
234
235         wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
236                 "<TEXTAREA NAME=\"description\" wrap=soft "
237                 "ROWS=5 COLS=80 WIDTH=80>\n"
238         );
239         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
240         if (p != NULL) {
241                 escputs((char *)icalproperty_get_comment(p));
242         }
243         wprintf("</TEXTAREA></TD></TR>");
244
245         /* For a new event, the user creating the event should be the
246          * organizer.  Set this field accordingly.
247          */
248         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
249            == NULL) {
250                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
251                 icalcomponent_add_property(vevent,
252                         icalproperty_new_organizer(organizer_string)
253                 );
254         }
255
256         /* Determine who is the organizer of this event.
257          * We need to determine "me" or "not me."
258          */
259         organizer = icalcomponent_get_first_property(vevent,
260                                                 ICAL_ORGANIZER_PROPERTY);
261         if (organizer != NULL) {
262                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
263                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
264                         strcpy(organizer_string, &organizer_string[7]);
265                         striplt(organizer_string);
266                         lprintf(9, "ISME %s\n", organizer_string);
267                         serv_printf("ISME %s", organizer_string);
268                         serv_gets(buf);
269                         lprintf(9, "%s\n", buf);
270                         if (buf[0] == '2') {
271                                 organizer_is_me = 1;
272                         }
273                 }
274         }
275
276         wprintf("<TR><TD><B>Organizer</B></TD><TD>");
277         escputs(organizer_string);
278         if (organizer_is_me) {
279                 wprintf(" <FONT SIZE=-1><I>"
280                         "(you are the organizer)</I></FONT>\n");
281         }
282
283         /*
284          * Transmit the organizer as a hidden field.   We don't want the user
285          * to be able to change it, but we do want it fed back to the server,
286          * especially if this is a new event and there is no organizer already
287          * in the calendar object.
288          */
289         wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
290         escputs(organizer_string);
291         wprintf("\">");
292
293         wprintf("</TD></TR>\n");
294
295         /* Transparency */
296         wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
297
298         p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
299         if (p == NULL) {
300                 /* No transparency found.  Default to opaque (busy). */
301                 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
302                 if (p != NULL) {
303                         icalcomponent_add_property(vevent, p);
304                 }
305         }
306         if (p != NULL) {
307                 v = icalproperty_get_value(p);
308         }
309         else {
310                 v = NULL;
311         }
312
313         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
314         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
315                 wprintf(" CHECKED");
316         wprintf(">Free&nbsp;&nbsp;");
317
318         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
319         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
320                 wprintf(" CHECKED");
321         wprintf(">Busy");
322
323         wprintf("</TD></TR>\n");
324
325         /* Attendees */
326         wprintf("<TR><TD><B>Attendees</B><BR>"
327                 "<FONT SIZE=-2>(One per line)"
328                 "</FONT></TD><TD>"
329                 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
330                 "ROWS=3 COLS=80 WIDTH=80>\n",
331                 (organizer_is_me ? "" : "DISABLED ")
332         );
333         i = 0;
334         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
335             attendee != NULL;
336             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
337                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
338                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
339
340                         /* screen name or email address */
341                         strcpy(attendee_string, &attendee_string[7]);
342                         striplt(attendee_string);
343                         if (i++) wprintf("\n");
344                         escputs(attendee_string);
345                         wprintf(" ");
346
347                         /* participant status */
348                         partstat_as_string(buf, attendee);
349                         escputs(buf);
350                 }
351         }
352         wprintf("</TEXTAREA></TD></TR>\n");
353
354         /* Done with properties. */
355         wprintf("</TABLE>\n<CENTER>"
356                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
357                 "&nbsp;&nbsp;"
358                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
359                 "&nbsp;&nbsp;"
360                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
361                 "</CENTER>\n"
362         );
363
364         wprintf("</FORM>\n");
365         
366         wprintf("<SCRIPT language=\"javascript\">
367                 <!--
368                         if (document.EventForm.alldayevent.checked) {
369                                 document.EventForm.dtstart_hour.value='0';
370                                 document.EventForm.dtstart_hour.disabled = true;
371                                 document.EventForm.dtstart_minute.value='0';
372                                 document.EventForm.dtstart_minute.disabled = true;
373                                 document.EventForm.dtend_hour.value='0';
374                                 document.EventForm.dtend_hour.disabled = true;
375                                 document.EventForm.dtend_minute.value='0';
376                                 document.EventForm.dtend_minute.disabled = true;
377                                 document.EventForm.dtend_month.disabled = true;
378                                 document.EventForm.dtend_day.disabled = true;
379                                 document.EventForm.dtend_year.disabled = true;
380                         }
381                         else {
382                                 document.EventForm.dtstart_hour.disabled = false;
383                                 document.EventForm.dtstart_minute.disabled = false;
384                                 document.EventForm.dtend_hour.disabled = false;
385                                 document.EventForm.dtend_minute.disabled = false;
386                                 document.EventForm.dtend_month.disabled = false;
387                                 document.EventForm.dtend_day.disabled = false;
388                                 document.EventForm.dtend_year.disabled = false;
389                         }
390                 //-->
391                 </SCRIPT>
392         ");
393
394         do_template("endbox");
395         wDumpContent(1);
396
397         if (created_new_vevent) {
398                 icalcomponent_free(vevent);
399         }
400 }
401
402 /*
403  * Save an edited event
404  * 
405  */
406 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
407         char buf[SIZ];
408         icalproperty *prop;
409         icalcomponent *vevent, *encaps;
410         int created_new_vevent = 0;
411         int all_day_event = 0;
412         struct icaltimetype event_start;
413         icalproperty *attendee = NULL;
414         char attendee_string[SIZ];
415         int i;
416         int foundit;
417         char form_attendees[SIZ];
418         char organizer_string[SIZ];
419         int sequence = 0;
420         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
421
422         if (supplied_vevent != NULL) {
423                 vevent = supplied_vevent;
424                 /* If we're looking at a fully encapsulated VCALENDAR
425                  * rather than a VTODO component, attempt to use the first
426                  * relevant VTODO subcomponent.  If there is none, the
427                  * NULL returned by icalcomponent_get_first_component() will
428                  * tell the next iteration of this function to create a
429                  * new one.
430                  */
431                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
432                         save_individual_event(
433                                 icalcomponent_get_first_component(
434                                         vevent, ICAL_VTODO_COMPONENT
435                                 ), msgnum
436                         );
437                         return;
438                 }
439         }
440         else {
441                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
442                 created_new_vevent = 1;
443         }
444
445         if (!strcasecmp(bstr("sc"), "Save")) {
446
447                 /* Replace values in the component with ones from the form */
448
449                 while (prop = icalcomponent_get_first_property(vevent,
450                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
451                         icalcomponent_remove_property(vevent, prop);
452                         icalproperty_free(prop);
453                 }
454                 icalcomponent_add_property(vevent,
455                         icalproperty_new_summary(bstr("summary")));
456
457                 while (prop = icalcomponent_get_first_property(vevent,
458                       ICAL_LOCATION_PROPERTY), prop != NULL) {
459                         icalcomponent_remove_property(vevent, prop);
460                         icalproperty_free(prop);
461                 }
462                 icalcomponent_add_property(vevent,
463                         icalproperty_new_location(bstr("location")));
464                 
465                 while (prop = icalcomponent_get_first_property(vevent,
466                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
467                         icalcomponent_remove_property(vevent, prop);
468                         icalproperty_free(prop);
469                 }
470                 icalcomponent_add_property(vevent,
471                         icalproperty_new_description(bstr("description")));
472         
473                 while (prop = icalcomponent_get_first_property(vevent,
474                       ICAL_DTSTART_PROPERTY), prop != NULL) {
475                         icalcomponent_remove_property(vevent, prop);
476                         icalproperty_free(prop);
477                 }
478
479                 if (!strcmp(bstr("alldayevent"), "yes")) {
480                         all_day_event = 1;
481                 }
482                 else {
483                         all_day_event = 0;
484                 }
485
486                 event_start = icaltime_from_webform("dtstart");
487                 if (all_day_event) {
488                         event_start.is_date = 1;
489                         event_start.hour = 0;
490                         event_start.minute = 0;
491                         event_start.second = 0;
492                 }
493
494
495                 /* The following odd-looking snippet of code looks like it
496                  * takes some unnecessary steps.  It is done this way because
497                  * libical incorrectly turns an "all day event" into a normal
498                  * event starting at midnight (i.e. it serializes as date/time
499                  * instead of just date) unless icalvalue_new_date() is used.
500                  * So we force it, if this is an all day event.
501                  */
502                 prop = icalproperty_new_dtstart(event_start);
503                 if (all_day_event) {
504                         icalproperty_set_value(prop,
505                                 icalvalue_new_date(event_start)
506                         );
507                 }
508
509                 if (prop) icalcomponent_add_property(vevent, prop);
510                 else icalproperty_free(prop);
511
512                 while (prop = icalcomponent_get_first_property(vevent,
513                       ICAL_DTEND_PROPERTY), prop != NULL) {
514                         icalcomponent_remove_property(vevent, prop);
515                         icalproperty_free(prop);
516                 }
517                 while (prop = icalcomponent_get_first_property(vevent,
518                       ICAL_DURATION_PROPERTY), prop != NULL) {
519                         icalcomponent_remove_property(vevent, prop);
520                         icalproperty_free(prop);
521                 }
522
523                 if (all_day_event == 0) {
524                         icalcomponent_add_property(vevent,
525                                 icalproperty_new_dtend(icaltime_normalize(
526                                         icaltime_from_webform("dtend"))
527                                 )
528                         );
529                 }
530
531                 /* See if transparency is indicated */
532                 if (strlen(bstr("transp")) > 0) {
533                         if (!strcasecmp(bstr("transp"), "opaque")) {
534                                 formtransp = ICAL_TRANSP_OPAQUE;
535                         }
536                         else if (!strcasecmp(bstr("transp"), "transparent")) {
537                                 formtransp = ICAL_TRANSP_TRANSPARENT;
538                         }
539
540                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
541                               (prop != NULL)) {
542                                 icalcomponent_remove_property(vevent, prop);
543                                 icalproperty_free(prop);
544                         }
545
546                         lprintf(9, "adding new property...\n");
547                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
548                         lprintf(9, "...added it.\n");
549                 }
550
551                 /* Give this event a UID if it doesn't have one. */
552                 lprintf(9, "Give this event a UID if it doesn't have one.\n");
553                 if (icalcomponent_get_first_property(vevent,
554                    ICAL_UID_PROPERTY) == NULL) {
555                         generate_new_uid(buf);
556                         icalcomponent_add_property(vevent,
557                                 icalproperty_new_uid(buf)
558                         );
559                 }
560
561                 /* Increment the sequence ID */
562                 lprintf(9, "Increment the sequence ID\n");
563                 while (prop = icalcomponent_get_first_property(vevent,
564                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
565                         i = icalproperty_get_sequence(prop);
566                         lprintf(9, "Sequence was %d\n", i);
567                         if (i > sequence) sequence = i;
568                         icalcomponent_remove_property(vevent, prop);
569                         icalproperty_free(prop);
570                 }
571                 ++sequence;
572                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
573                 icalcomponent_add_property(vevent,
574                         icalproperty_new_sequence(sequence)
575                 );
576                 
577                 /* Set the organizer, only if one does not already exist *and*
578                  * the form is supplying one
579                  */
580                 lprintf(9, "Setting the organizer...\n");
581                 strcpy(buf, bstr("organizer"));
582                 if ( (icalcomponent_get_first_property(vevent,
583                    ICAL_ORGANIZER_PROPERTY) == NULL) 
584                    && (strlen(buf) > 0) ) {
585
586                         /* set new organizer */
587                         sprintf(organizer_string, "MAILTO:%s", buf);
588                         icalcomponent_add_property(vevent,
589                                 icalproperty_new_organizer(organizer_string)
590                         );
591
592                 }
593
594                 /*
595                  * Add any new attendees listed in the web form
596                  */
597                 lprintf(9, "Add any new attendees\n");
598
599                 /* First, strip out the parenthesized partstats.  */
600                 strcpy(form_attendees, bstr("attendees"));
601                 stripout(form_attendees, '(', ')');
602
603                 /* Now iterate! */
604                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
605                         extract_token(buf, form_attendees, i, '\n');
606                         striplt(buf);
607                         if (strlen(buf) > 0) {
608                                 lprintf(9, "Attendee: <%s>\n", buf);
609                                 sprintf(attendee_string, "MAILTO:%s", buf);
610                                 foundit = 0;
611
612                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
613                                         if (!strcasecmp(attendee_string,
614                                            icalproperty_get_attendee(attendee)))
615                                                 ++foundit;
616                                 }
617
618
619                                 if (foundit == 0) {
620                                         icalcomponent_add_property(vevent,
621                                                 icalproperty_new_attendee(attendee_string)
622                                         );
623                                 }
624                         }
625                 }
626
627                 /*
628                  * Remove any attendees *not* listed in the web form
629                  */
630 STARTOVER:      lprintf(9, "Remove unlisted attendees\n");
631                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
632                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
633                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
634                                 strcpy(attendee_string, &attendee_string[7]);
635                                 striplt(attendee_string);
636                                 foundit = 0;
637                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
638                                         extract_token(buf, form_attendees, i, '\n');
639                                         striplt(buf);
640                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
641                                 }
642                                 if (foundit == 0) {
643                                         icalcomponent_remove_property(vevent, attendee);
644                                         icalproperty_free(attendee);
645                                         goto STARTOVER;
646                                 }
647                         }
648                 }
649
650                 /*
651                  * Serialize it and save it to the message base.  We clone it first,
652                  * for two reasons: one, it's easier to just free the whole thing
653                  * when we're done instead of unbundling, but more importantly, we
654                  * can't encapsulate something that may already be encapsulated
655                  * somewhere else.
656                  */
657                 lprintf(9, "Encapsulating into full VCALENDAR component\n");
658                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
659                 lprintf(9, "Serializing it for saving\n");
660                 if (encaps != NULL) {
661                         serv_puts("ENT0 1|||4");
662                         serv_gets(buf);
663                         if (buf[0] == '4') {
664                                 serv_puts("Content-type: text/calendar");
665                                 serv_puts("");
666                                 serv_puts(icalcomponent_as_ical_string(encaps));
667                                 serv_puts("000");
668                         }
669                         icalcomponent_free(encaps);
670                 }
671         }
672
673         /*
674          * If the user clicked 'Delete' then delete it.
675          */
676         lprintf(9, "Checking to see if we have to delete an old event\n");
677         if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
678                 serv_printf("DELE %ld", atol(bstr("msgnum")));
679                 serv_gets(buf);
680         }
681
682         if (created_new_vevent) {
683                 icalcomponent_free(vevent);
684         }
685
686         /* Go back to the event list */
687         readloop("readfwd");
688 }
689
690
691 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */