* Replaced serv_gets() with serv_getln() - which now requires the caller
[citadel.git] / webcit / event.c
index e8521d67dd30c22efa2d13ba97b45963b078e21a..e3185e515c959d24409bb428d9ed2be9e370d213 100644 (file)
@@ -32,8 +32,6 @@
 
 /*
  * Display an event by itself (for editing)
- *
- * ok
  */
 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
        icalcomponent *vevent;
@@ -48,19 +46,19 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        icalproperty *attendee = NULL;
        char attendee_string[SIZ];
        char buf[SIZ];
-       int i;
        int organizer_is_me = 0;
+       int i;
        int sequence = 0;
 
-       now = time(NULL) % 60;  /* mod 60 to force :00 seconds */
+       now = time(NULL);
        strcpy(organizer_string, "");
        strcpy(attendee_string, "");
 
        if (supplied_vevent != NULL) {
                vevent = supplied_vevent;
                /* If we're looking at a fully encapsulated VCALENDAR
-                * rather than a VTODO component, attempt to use the first
-                * relevant VTODO subcomponent.  If there is none, the
+                * rather than a VEVENT component, attempt to use the first
+                * relevant VEVENT subcomponent.  If there is none, the
                 * NULL returned by icalcomponent_get_first_component() will
                 * tell the next iteration of this function to create a
                 * new one.
@@ -68,7 +66,7 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
                        display_edit_individual_event(
                                icalcomponent_get_first_component(
-                                       vevent, ICAL_VTODO_COMPONENT
+                                       vevent, ICAL_VEVENT_COMPONENT
                                ), msgnum
                        );
                        return;
@@ -86,10 +84,16 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        }
 
        /* Begin output */
-       output_headers(3);
-       do_template("beginbox_nt");
-       wprintf("<h3>&nbsp;<IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
-               "&nbsp;Add or edit an event</h3>\n");
+       output_headers(1, 1, 2, 0, 0, 0, 0);
+       wprintf("<div id=\"banner\">\n"
+               "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
+               "<SPAN CLASS=\"titlebar\">Add or edit an event</SPAN>"
+               "</TD></TR></TABLE>\n"
+               "</div>\n<div id=\"content\">\n"
+       );
+
+       wprintf("<div id=\"fix_scrollbar_bug\">"
+               "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
 
        /************************************************************
         * Uncomment this to see the UID in calendar events for debugging
@@ -98,8 +102,8 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        if (p != NULL) {
                escputs((char *)icalproperty_get_comment(p));
        }
-       wprintf("<BR>\n");
-       wprintf("SEQUENCE == %d<BR>\n", sequence);
+       wprintf("<br />\n");
+       wprintf("SEQUENCE == %d<br />\n", sequence);
        *************************************************************/
 
        wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
@@ -147,10 +151,12 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                }
        }
        else {
-               memcpy(&tm_now, localtime(&now), sizeof(struct tm));
-               tm_now.tm_year = atoi(bstr("year")) - 1900;
-               tm_now.tm_mon = atoi(bstr("month")) - 1;
-               tm_now.tm_mday = atoi(bstr("day"));
+               localtime_r(&now, &tm_now);
+               if (strlen(bstr("year")) > 0) {
+                       tm_now.tm_year = atoi(bstr("year")) - 1900;
+                       tm_now.tm_mon = atoi(bstr("month")) - 1;
+                       tm_now.tm_mday = atoi(bstr("day"));
+               }
                if (strlen(bstr("hour")) > 0) {
                        tm_now.tm_hour = atoi(bstr("hour"));
                        tm_now.tm_min = atoi(bstr("minute"));
@@ -165,7 +171,7 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                t_start = icaltime_from_timet_with_zone(
                        mktime(&tm_now),
                        ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
-                       icaltimezone_get_utc_timezone
+                       icaltimezone_get_utc_timezone()
                );
                t_start.is_utc = 1;
 
@@ -173,33 +179,33 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        display_icaltimetype_as_webform(&t_start, "dtstart");
 
        wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
-               "VALUE=\"yes\" onClick=\"
-
-                       if (this.checked) {
-                               this.form.dtstart_hour.value='0';
-                               this.form.dtstart_hour.disabled = true;
-                               this.form.dtstart_minute.value='0';
-                               this.form.dtstart_minute.disabled = true;
-                               this.form.dtend_hour.value='0';
-                               this.form.dtend_hour.disabled = true;
-                               this.form.dtend_minute.value='0';
-                               this.form.dtend_minute.disabled = true;
-                               this.form.dtend_month.disabled = true;
-                               this.form.dtend_day.disabled = true;
-                               this.form.dtend_year.disabled = true;
-                       }
-                       else {
-                               this.form.dtstart_hour.disabled = false;
-                               this.form.dtstart_minute.disabled = false;
-                               this.form.dtend_hour.disabled = false;
-                               this.form.dtend_minute.disabled = false;
-                               this.form.dtend_month.disabled = false;
-                               this.form.dtend_day.disabled = false;
-                               this.form.dtend_year.disabled = false;
-                       }
-
-
-               \" %s >All day event",
+               "VALUE=\"yes\" onClick=\""
+""
+"                      if (this.checked) { "
+"                              this.form.dtstart_hour.value='0'; "
+"                              this.form.dtstart_hour.disabled = true; "
+"                              this.form.dtstart_minute.value='0'; "
+"                              this.form.dtstart_minute.disabled = true; "
+"                              this.form.dtend_hour.value='0'; "
+"                              this.form.dtend_hour.disabled = true; "
+"                              this.form.dtend_minute.value='0'; "
+"                              this.form.dtend_minute.disabled = true; "
+"                              this.form.dtend_month.disabled = true; "
+"                              this.form.dtend_day.disabled = true; "
+"                              this.form.dtend_year.disabled = true; "
+"                      } "
+"                      else { "
+"                              this.form.dtstart_hour.disabled = false; "
+"                              this.form.dtstart_minute.disabled = false; "
+"                              this.form.dtend_hour.disabled = false; "
+"                              this.form.dtend_minute.disabled = false; "
+"                              this.form.dtend_month.disabled = false; "
+"                              this.form.dtend_day.disabled = false; "
+"                              this.form.dtend_year.disabled = false; "
+"                      } "
+" "
+" "
+"              \" %s >All day event",
                (t_start.is_date ? "CHECKED" : "" )
        );
 
@@ -267,7 +273,7 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                        striplt(organizer_string);
                        lprintf(9, "ISME %s\n", organizer_string);
                        serv_printf("ISME %s", organizer_string);
-                       serv_gets(buf);
+                       serv_getln(buf, sizeof buf);
                        lprintf(9, "%s\n", buf);
                        if (buf[0] == '2') {
                                organizer_is_me = 1;
@@ -325,7 +331,7 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        wprintf("</TD></TR>\n");
 
        /* Attendees */
-       wprintf("<TR><TD><B>Attendees</B><BR>"
+       wprintf("<TR><TD><B>Attendees</B><br />"
                "<FONT SIZE=-2>(One per line)"
                "</FONT></TD><TD>"
                "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
@@ -333,7 +339,9 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                (organizer_is_me ? "" : "DISABLED ")
        );
        i = 0;
-       for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
+       for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
+           attendee != NULL;
+           attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
                strcpy(attendee_string, icalproperty_get_attendee(attendee));
                if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
 
@@ -357,42 +365,44 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                "&nbsp;&nbsp;"
                "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
                "&nbsp;&nbsp;"
+               "<INPUT TYPE=\"submit\" NAME=\"sc\" "
+                               "VALUE=\"Check attendee availability\">\n"
+               "&nbsp;&nbsp;"
                "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
                "</CENTER>\n"
        );
 
        wprintf("</FORM>\n");
        
-       wprintf("<SCRIPT language=\"javascript\">
-               <!--
-
-                       if (document.EventForm.alldayevent.checked) {
-                               document.EventForm.dtstart_hour.value='0';
-                               document.EventForm.dtstart_hour.disabled = true;
-                               document.EventForm.dtstart_minute.value='0';
-                               document.EventForm.dtstart_minute.disabled = true;
-                               document.EventForm.dtend_hour.value='0';
-                               document.EventForm.dtend_hour.disabled = true;
-                               document.EventForm.dtend_minute.value='0';
-                               document.EventForm.dtend_minute.disabled = true;
-                               document.EventForm.dtend_month.disabled = true;
-                               document.EventForm.dtend_day.disabled = true;
-                               document.EventForm.dtend_year.disabled = true;
-                       }
-                       else {
-                               document.EventForm.dtstart_hour.disabled = false;
-                               document.EventForm.dtstart_minute.disabled = false;
-                               document.EventForm.dtend_hour.disabled = false;
-                               document.EventForm.dtend_minute.disabled = false;
-                               document.EventForm.dtend_month.disabled = false;
-                               document.EventForm.dtend_day.disabled = false;
-                               document.EventForm.dtend_year.disabled = false;
-                       }
-               //-->
-               </SCRIPT>
-       ");
+       wprintf("<script type=\"text/javascript\">"
+               "<!--"
+                       "if (document.EventForm.alldayevent.checked) {"
+                               "document.EventForm.dtstart_hour.value='0';"
+                               "document.EventForm.dtstart_hour.disabled = true;"
+                               "document.EventForm.dtstart_minute.value='0';"
+                               "document.EventForm.dtstart_minute.disabled = true;"
+                               "document.EventForm.dtend_hour.value='0';"
+                               "document.EventForm.dtend_hour.disabled = true;"
+                               "document.EventForm.dtend_minute.value='0';"
+                               "document.EventForm.dtend_minute.disabled = true;"
+                               "document.EventForm.dtend_month.disabled = true;"
+                               "document.EventForm.dtend_day.disabled = true;"
+                               "document.EventForm.dtend_year.disabled = true;"
+                       "}"
+                       "else {"
+                               "document.EventForm.dtstart_hour.disabled = false;"
+                               "document.EventForm.dtstart_minute.disabled = false;"
+                               "document.EventForm.dtend_hour.disabled = false;"
+                               "document.EventForm.dtend_minute.disabled = false;"
+                               "document.EventForm.dtend_month.disabled = false;"
+                               "document.EventForm.dtend_day.disabled = false;"
+                               "document.EventForm.dtend_year.disabled = false;"
+                       "}"
+               "//-->"
+               "</script>\n"
+       );
 
-       do_template("endbox");
+       wprintf("</td></tr></table></div>\n");
        wDumpContent(1);
 
        if (created_new_vevent) {
@@ -423,8 +433,8 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
        if (supplied_vevent != NULL) {
                vevent = supplied_vevent;
                /* If we're looking at a fully encapsulated VCALENDAR
-                * rather than a VTODO component, attempt to use the first
-                * relevant VTODO subcomponent.  If there is none, the
+                * rather than a VEVENT component, attempt to use the first
+                * relevant VEVENT subcomponent.  If there is none, the
                 * NULL returned by icalcomponent_get_first_component() will
                 * tell the next iteration of this function to create a
                 * new one.
@@ -432,7 +442,7 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
                if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
                        save_individual_event(
                                icalcomponent_get_first_component(
-                                       vevent, ICAL_VTODO_COMPONENT
+                                       vevent, ICAL_VEVENT_COMPONENT
                                ), msgnum
                        );
                        return;
@@ -443,7 +453,8 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
                created_new_vevent = 1;
        }
 
-       if (!strcasecmp(bstr("sc"), "Save")) {
+       if ( (!strcasecmp(bstr("sc"), "Save"))
+          || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
 
                /* Replace values in the component with ones from the form */
 
@@ -553,7 +564,7 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
                lprintf(9, "Give this event a UID if it doesn't have one.\n");
                if (icalcomponent_get_first_property(vevent,
                   ICAL_UID_PROPERTY) == NULL) {
-                       generate_new_uid(buf);
+                       generate_uuid(buf);
                        icalcomponent_add_property(vevent,
                                icalproperty_new_uid(buf)
                        );
@@ -603,7 +614,7 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
 
                /* Now iterate! */
                for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
-                       extract_token(buf, form_attendees, i, '\n');
+                       extract_token(buf, form_attendees, i, '\n', sizeof buf);
                        striplt(buf);
                        if (strlen(buf) > 0) {
                                lprintf(9, "Attendee: <%s>\n", buf);
@@ -636,7 +647,7 @@ STARTOVER:  lprintf(9, "Remove unlisted attendees\n");
                                striplt(attendee_string);
                                foundit = 0;
                                for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
-                                       extract_token(buf, form_attendees, i, '\n');
+                                       extract_token(buf, form_attendees, i, '\n', sizeof buf);
                                        striplt(buf);
                                        if (!strcasecmp(buf, attendee_string)) ++foundit;
                                }
@@ -649,7 +660,7 @@ STARTOVER:  lprintf(9, "Remove unlisted attendees\n");
                }
 
                /*
-                * Serialize it and save it to the message base.  We clone it first,
+                * Encapsulate event into full VCALENDAR component.  Clone it first,
                 * for two reasons: one, it's easier to just free the whole thing
                 * when we're done instead of unbundling, but more importantly, we
                 * can't encapsulate something that may already be encapsulated
@@ -657,18 +668,36 @@ STARTOVER:        lprintf(9, "Remove unlisted attendees\n");
                 */
                lprintf(9, "Encapsulating into full VCALENDAR component\n");
                encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
+
+               /* If the user clicked 'Save' then save it to the server. */
                lprintf(9, "Serializing it for saving\n");
-               if (encaps != NULL) {
-                       serv_puts("ENT0 1|||4");
-                       serv_gets(buf);
-                       if (buf[0] == '4') {
+               if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
+                       serv_puts("ENT0 1|||4|||1|");
+                       serv_getln(buf, sizeof buf);
+                       if (buf[0] == '8') {
                                serv_puts("Content-type: text/calendar");
                                serv_puts("");
                                serv_puts(icalcomponent_as_ical_string(encaps));
                                serv_puts("000");
                        }
+                       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                               lprintf(9, "ENT0 REPLY: %s\n", buf);
+                       }
+                       icalcomponent_free(encaps);
+               }
+
+               /* Or, check attendee availability if the user asked for that. */
+               if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
+
+                       /* Call this function, which does the real work */
+                       check_attendee_availability(encaps);
+
+                       /* This displays the form again, with our annotations */
+                       display_edit_individual_event(encaps, msgnum);
+
                        icalcomponent_free(encaps);
                }
+
        }
 
        /*
@@ -677,15 +706,17 @@ STARTOVER:        lprintf(9, "Remove unlisted attendees\n");
        lprintf(9, "Checking to see if we have to delete an old event\n");
        if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
                serv_printf("DELE %ld", atol(bstr("msgnum")));
-               serv_gets(buf);
+               serv_getln(buf, sizeof buf);
        }
 
        if (created_new_vevent) {
                icalcomponent_free(vevent);
        }
 
-       /* Go back to the event list */
-       readloop("readfwd");
+       /* If this was a save or deelete, go back to the calendar view. */
+       if (strcasecmp(bstr("sc"), "Check attendee availability")) {
+               readloop("readfwd");
+       }
 }