4 * This is the main transaction loop of the web service. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
12 #include "webserver.h"
13 #include "mime_parser.h"
16 * String to unset the cookie.
17 * Any date "in the past" will work, so I chose my birthday, right down to
18 * the exact minute. :)
20 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
22 void unescape_input(char *buf)
27 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
28 buf[strlen(buf) - 1] = 0;
30 for (a = 0; a < strlen(buf); ++a) {
38 sscanf(hex, "%02x", &b);
40 strcpy(&buf[a + 1], &buf[a + 3]);
47 void addurls(char *url)
55 while (strlen(up) > 0) {
57 /* locate the = sign */
58 safestrncpy(buf, up, sizeof buf);
60 for (a = 255; a >= 0; --a)
67 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
68 u->next = WC->urlstrings;
70 safestrncpy(u->url_key, buf, sizeof u->url_key);
72 /* now chop that part off */
73 for (a = 0; a <= b; ++a)
76 /* locate "&" and "?" delimiters */
79 for (a = 0; a < strlen(up); ++a) {
80 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
87 for (a = 0; a < b; ++a)
91 u->url_data = malloc(strlen(up) + 2);
92 safestrncpy(u->url_data, up, strlen(up) + 1);
94 unescape_input(u->url_data);
102 struct urlcontent *u;
104 while (WC->urlstrings != NULL) {
105 free(WC->urlstrings->url_data);
106 u = WC->urlstrings->next;
107 free(WC->urlstrings);
113 * Diagnostic function to display the contents of all variables
117 struct urlcontent *u;
119 for (u = WC->urlstrings; u != NULL; u = u->next) {
120 wprintf("%38s = %s\n", u->url_key, u->url_data);
124 char *bstr(char *key)
126 struct urlcontent *u;
128 for (u = WC->urlstrings; u != NULL; u = u->next) {
129 if (!strcasecmp(u->url_key, key))
130 return (u->url_data);
136 void wprintf(const char *format,...)
141 va_start(arg_ptr, format);
142 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
145 client_write(wbuf, strlen(wbuf));
150 * wDumpContent() wraps up an HTTP session, closes tags, etc.
152 * print_standard_html_footer should be set to 0 to transmit only, 1 to
153 * append the main menu and closing tags, or 2 to
154 * append the closing tags only.
156 void wDumpContent(int print_standard_html_footer)
158 if (print_standard_html_footer) {
159 wprintf("</div>\n"); /* end of "text" div */
160 do_template("trailing");
163 /* If we've been saving it all up for one big output burst,
164 * go ahead and do that now.
171 * Copy a string, escaping characters which have meaning in HTML. If
172 * nbsp is nonzero, spaces are converted to non-breaking spaces.
174 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
179 for (a = 0; a < strlen(strbuf); ++a) {
180 if (strbuf[a] == '<')
181 strcat(target, "<");
182 else if (strbuf[a] == '>')
183 strcat(target, ">");
184 else if (strbuf[a] == '&')
185 strcat(target, "&");
186 else if (strbuf[a] == '\"')
187 strcat(target, """);
188 else if (strbuf[a] == '\'')
189 strcat(target, "'");
190 else if (strbuf[a] == LB)
192 else if (strbuf[a] == RB)
194 else if (strbuf[a] == QU)
195 strcat(target, "\"");
196 else if ((strbuf[a] == 32) && (nbsp == 1))
197 strcat(target, " ");
198 else if ((strbuf[a] == '\n') && (nolinebreaks))
199 strcat(target, ""); /* nothing */
200 else if ((strbuf[a] == '\r') && (nolinebreaks))
201 strcat(target, ""); /* nothing */
203 strncat(target, &strbuf[a], 1);
207 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
211 if (strbuf == NULL) return;
212 buf = malloc( (3 * strlen(strbuf)) + SIZ );
213 stresc(buf, strbuf, nbsp, nolinebreaks);
218 void escputs(char *strbuf)
220 escputs1(strbuf, 0, 0);
224 * Escape a string for feeding out as a URL.
225 * Returns a pointer to a buffer that must be freed by the caller!
227 void urlesc(char *outbuf, char *strbuf)
230 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
234 for (a = 0; a < strlen(strbuf); ++a) {
236 for (b = 0; b < strlen(ec); ++b) {
237 if (strbuf[a] == ec[b])
242 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
244 sprintf(&outbuf[b], "%c", strbuf[a]);
248 void urlescputs(char *strbuf)
252 urlesc(outbuf, strbuf);
253 wprintf("%s", outbuf);
258 * Copy a string, escaping characters for JavaScript strings.
260 void jsesc(char *target, char *strbuf)
265 for (a = 0; a < strlen(strbuf); ++a) {
266 if (strbuf[a] == '<')
268 else if (strbuf[a] == '>')
270 else if (strbuf[a] == '\"')
271 strcat(target, """);
272 else if (strbuf[a] == '&')
273 strcat(target, "&;");
274 else if (strbuf[a] == '\'')
275 strcat(target, "\\'");
277 strncat(target, &strbuf[a], 1);
282 void jsescputs(char *strbuf)
286 jsesc(outbuf, strbuf);
287 wprintf("%s", outbuf);
291 * Copy a string, escaping characters for message text hold
293 void msgesc(char *target, char *strbuf)
298 for (a = 0; a < strlen(strbuf); ++a) {
299 if (strbuf[a] == '\'')
300 strcat(target, "\\'");
301 else if (strbuf[a] == '\n')
303 else if (strbuf[a] == '\r')
306 strncat(target, &strbuf[a], 1);
311 void msgescputs(char *strbuf) {
314 if (strbuf == NULL) return;
315 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
316 msgesc(outbuf, strbuf);
317 wprintf("%s", outbuf);
325 * Output all that important stuff that the browser will want to see
327 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
328 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
330 int do_room_banner, /* 0=no, 1=yes, */
331 /* 2 = I'm going to embed my own, so don't open the */
332 /* <div id="content"> either. */
334 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
335 int refresh30, /* 1 = automatically refresh page every 30 seconds */
336 int suppress_check, /* 1 = suppress check for instant messages */
337 int cache /* 1 = allow browser to cache this page */
342 wprintf("HTTP/1.0 200 OK\n");
343 httpdate(httpnow, time(NULL));
345 if (do_httpheaders) {
346 wprintf("Content-type: text/html; charset=utf-8\r\n"
348 "Connection: close\r\n",
349 SERVER, serv_info.serv_software
354 wprintf("Pragma: public\r\n"
355 "Cache-Control: max-age=3600, must-revalidate\r\n"
356 "Last-modified: %s\r\n",
361 wprintf("Pragma: no-cache\r\n"
362 "Cache-Control: no-store\r\n"
366 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
367 WC->wc_password, WC->wc_roomname);
370 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
372 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
373 if (server_cookie != NULL) {
374 wprintf("%s\n", server_cookie);
383 svprintf("REFRESHTAG", WCS_STRING, "%s",
384 "<meta http-equiv=\"refresh\" content=\"30\" />\n");
387 svprintf("REFRESHTAG", WCS_STRING, "%s",
388 "<meta http-equiv=\"refresh\" content=\"500363689;\" />\n");
397 if (WC->HaveInstantMessages) {
398 wprintf("<div id=\"page_popup\">\n");
402 if (strlen(WC->ImportantMessage) > 0) {
403 wprintf("<div id=\"important_message\">\n");
404 wprintf("<SPAN CLASS=\"imsg\">"
405 "%s</SPAN><br />\n", WC->ImportantMessage);
407 wprintf("<script type=\"text/javascript\">\n"
408 " setTimeout('hide_imsg_popup()', 2000); \n"
410 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
412 if ( (WC->logged_in) && (!unset_cookies) ) {
413 wprintf("<div id=\"iconbar\">");
417 if (do_room_banner == 1) {
418 wprintf("<div id=\"banner\">\n");
419 embed_room_banner(NULL, navbar_default);
424 if (do_room_banner == 1) {
425 wprintf("<div id=\"content\">\n");
431 * Generic function to do an HTTP redirect. Easy and fun.
433 void http_redirect(char *whichpage) {
434 wprintf("HTTP/1.0 302 Moved Temporarily\n");
435 wprintf("Location: %s\r\n", whichpage);
436 wprintf("URI: %s\r\n", whichpage);
437 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
438 wprintf("<html><body>\n");
439 wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
441 wprintf("</body></html>\n");
446 void check_for_instant_messages()
451 serv_getln(buf, sizeof buf);
452 if (buf[3] == '*') WC->HaveInstantMessages = 1;
459 * Output a piece of content to the web browser
461 void http_transmit_thing(char *thing, size_t length, char *content_type,
464 output_headers(0, 0, 0, 0, 0, 0, is_static);
466 wprintf("Content-type: %s\r\n"
468 "Connection: close\r\n",
473 /* If we can send the data out compressed, please do so. */
475 char *compressed_data = NULL;
476 uLongf compressed_len;
478 compressed_len = (uLongf) ((length * 101) / 100) + 100;
479 compressed_data = malloc(compressed_len);
481 if (compress_gzip((Bytef *) compressed_data,
484 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
485 wprintf("Content-encoding: gzip\r\n"
486 "Content-length: %ld\r\n"
488 (long) compressed_len
490 client_write(compressed_data, (size_t)compressed_len);
491 free(compressed_data);
497 /* No compression ... just send it out as-is */
498 wprintf("Content-length: %ld\r\n"
502 client_write(thing, (size_t)length);
508 void output_static(char *what)
515 char content_type[128];
517 sprintf(buf, "static/%s", what);
518 fp = fopen(buf, "rb");
520 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
521 wprintf("Content-Type: text/plain\r\n");
523 wprintf("Cannot open %s: %s\n", what, strerror(errno));
525 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
526 safestrncpy(content_type, "image/gif", sizeof content_type);
527 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
528 safestrncpy(content_type, "text/plain", sizeof content_type);
529 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
530 safestrncpy(content_type, "text/css", sizeof content_type);
531 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
532 safestrncpy(content_type, "image/jpeg", sizeof content_type);
533 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
534 safestrncpy(content_type, "image/png", sizeof content_type);
535 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
536 safestrncpy(content_type, "image/x-icon", sizeof content_type);
537 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
538 safestrncpy(content_type, "text/html", sizeof content_type);
539 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
540 safestrncpy(content_type, "text/html", sizeof content_type);
541 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
542 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
543 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
544 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
545 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
546 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
547 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
548 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
549 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
550 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
551 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
552 safestrncpy(content_type, "text/javascript", sizeof content_type);
554 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
556 fstat(fileno(fp), &statbuf);
557 bytes = statbuf.st_size;
558 bigbuffer = malloc(bytes + 2);
559 fread(bigbuffer, bytes, 1, fp);
562 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
565 if (!strcasecmp(bstr("force_close_session"), "yes")) {
566 end_webcit_session();
572 * When the browser requests an image file from the Citadel server,
573 * this function is called to transmit it.
578 char *xferbuf = NULL;
581 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
582 serv_getln(buf, sizeof buf);
584 bytes = extract_long(&buf[4], 0);
585 xferbuf = malloc(bytes + 2);
587 /* Read it from the server */
588 read_server_binary(xferbuf, bytes);
590 serv_getln(buf, sizeof buf);
592 /* Write it to the browser */
593 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
598 /* Instead of an ugly 404, send a 1x1 transparent GIF
599 * when there's no such image on the server.
601 output_static("blank.gif");
604 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
605 output_headers(0, 0, 0, 0, 0, 0, 0);
606 wprintf("Content-Type: text/plain\r\n"
608 "Error retrieving image: %s\n",
621 void output_mimepart()
625 char content_type[SIZ];
626 char *content = NULL;
628 serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
629 serv_getln(buf, sizeof buf);
631 bytes = extract_long(&buf[4], 0);
632 content = malloc(bytes + 2);
633 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
634 output_headers(0, 0, 0, 0, 0, 0, 0);
635 read_server_binary(content, bytes);
637 serv_getln(buf, sizeof buf);
638 http_transmit_thing(content, bytes, content_type, 0);
641 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
642 output_headers(0, 0, 0, 0, 0, 0, 0);
643 wprintf("Content-Type: text/plain\r\n");
645 wprintf("Error retrieving part: %s\n", &buf[4]);
653 char *load_mimepart(long msgnum, char *partnum)
657 char content_type[SIZ];
660 serv_printf("OPNA %ld|%s", msgnum, partnum);
661 serv_getln(buf, sizeof buf);
663 bytes = extract_long(&buf[4], 0);
664 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
666 content = malloc(bytes + 2);
667 read_server_binary(content, bytes);
670 serv_getln(buf, sizeof buf);
671 content[bytes] = 0; /* null terminate for good measure */
682 * Convenience functions to display a page containing only a string
684 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
686 wprintf("HTTP/1.0 200 OK\n");
687 output_headers(1, 1, 2, 0, 0, 0, 0);
688 wprintf("<div id=\"banner\">\n");
689 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
690 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
691 wprintf("</TD></TR></TABLE>\n");
692 wprintf("</div>\n<div id=\"content\">\n");
693 escputs(messagetext);
701 * Display a blank page.
703 void blank_page(void) {
704 output_headers(1, 1, 0, 0, 1, 0, 0);
710 * A template has been requested
712 void url_do_template(void) {
713 do_template(bstr("template"));
719 * Offer to make any page the user's "start page."
721 void offer_start_page(void) {
722 wprintf("<A HREF=\"/change_start_page?startpage=");
723 urlescputs(WC->this_page);
725 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
732 * Change the user's start page
734 void change_start_page(void) {
736 if (bstr("startpage") == NULL) {
737 safestrncpy(WC->ImportantMessage,
738 "startpage set to null",
739 sizeof WC->ImportantMessage);
744 set_preference("startpage", bstr("startpage"), 1);
746 output_headers(1, 1, 0, 0, 0, 0, 0);
747 do_template("newstartpage");
754 void display_success(char *successmessage)
756 convenience_page("007700", "OK", successmessage);
761 void upload_handler(char *name, char *filename, char *partnum, char *disp,
762 void *content, char *cbtype, char *cbcharset,
763 size_t length, char *encoding, void *userdata)
765 struct urlcontent *u;
767 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
768 name, cbtype, length);
771 if ( (length > 0) && (strlen(cbtype) == 0) ) {
772 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
773 u->next = WC->urlstrings;
775 safestrncpy(u->url_key, name, sizeof(u->url_key));
776 u->url_data = malloc(length + 1);
777 memcpy(u->url_data, content, length);
778 u->url_data[length] = 0;
782 if ( (length > 0) && (strlen(cbtype) > 0) ) {
783 WC->upload = malloc(length);
784 if (WC->upload != NULL) {
785 WC->upload_length = length;
786 safestrncpy(WC->upload_filename, filename,
787 sizeof(WC->upload_filename));
788 safestrncpy(WC->upload_content_type, cbtype,
789 sizeof(WC->upload_content_type));
790 memcpy(WC->upload, content, length);
793 lprintf(3, "malloc() failed: %s\n", strerror(errno));
803 * Entry point for WebCit transaction
805 void session_loop(struct httprequest *req)
815 int ContentLength = 0;
817 char ContentType[512];
818 char *content = NULL;
819 char *content_end = NULL;
820 struct httprequest *hptr;
821 char browser_host[SIZ];
822 char user_agent[SIZ];
825 /* We stuff these with the values coming from the client cookies,
826 * so we can use them to reconnect a timed out session if we have to.
828 char c_username[SIZ];
829 char c_password[SIZ];
830 char c_roomname[SIZ];
831 char c_httpauth_string[SIZ];
832 char c_httpauth_user[SIZ];
833 char c_httpauth_pass[SIZ];
836 safestrncpy(c_username, "", sizeof c_username);
837 safestrncpy(c_password, "", sizeof c_password);
838 safestrncpy(c_roomname, "", sizeof c_roomname);
839 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
840 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
841 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
843 WC->upload_length = 0;
850 if (hptr == NULL) return;
852 safestrncpy(cmd, hptr->line, sizeof cmd);
854 extract_token(method, cmd, 0, ' ', sizeof method);
856 /* Figure out the action */
857 extract_token(action, cmd, 1, '/', sizeof action);
858 if (strstr(action, "?")) *strstr(action, "?") = 0;
859 if (strstr(action, "&")) *strstr(action, "&") = 0;
860 if (strstr(action, " ")) *strstr(action, " ") = 0;
862 extract_token(arg1, cmd, 2, '/', sizeof arg1);
863 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
864 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
865 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
867 extract_token(arg2, cmd, 3, '/', sizeof arg2);
868 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
869 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
870 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
872 extract_token(arg3, cmd, 4, '/', sizeof arg3);
873 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
874 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
875 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
877 while (hptr != NULL) {
878 safestrncpy(buf, hptr->line, sizeof buf);
881 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
882 safestrncpy(cookie, &buf[15], sizeof cookie);
883 cookie_to_stuff(cookie, NULL,
884 c_username, sizeof c_username,
885 c_password, sizeof c_password,
886 c_roomname, sizeof c_roomname);
888 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
889 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
890 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
891 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
893 else if (!strncasecmp(buf, "Content-length: ", 16)) {
894 ContentLength = atoi(&buf[16]);
896 else if (!strncasecmp(buf, "Content-type: ", 14)) {
897 safestrncpy(ContentType, &buf[14], sizeof ContentType);
899 else if (!strncasecmp(buf, "User-agent: ", 12)) {
900 safestrncpy(user_agent, &buf[12], sizeof user_agent);
902 else if (!strncasecmp(buf, "Host: ", 6)) {
903 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
905 /* Only WAP gateways explicitly name this content-type */
906 else if (strstr(buf, "text/vnd.wap.wml")) {
911 if (ContentLength > 0) {
912 content = malloc(ContentLength + SIZ);
913 memset(content, 0, ContentLength + SIZ);
914 sprintf(content, "Content-type: %s\n"
915 "Content-length: %d\n\n",
916 ContentType, ContentLength);
917 body_start = strlen(content);
919 /* Read the entire input data at once. */
920 client_read(WC->http_sock, &content[BytesRead+body_start],
923 if (!strncasecmp(ContentType,
924 "application/x-www-form-urlencoded", 33)) {
925 addurls(&content[body_start]);
926 } else if (!strncasecmp(ContentType, "multipart", 9)) {
927 content_end = content + ContentLength + body_start;
928 mime_parser(content, content_end, *upload_handler,
929 NULL, NULL, NULL, 0);
935 /* make a note of where we are in case the user wants to save it */
936 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
937 remove_token(WC->this_page, 2, ' ');
938 remove_token(WC->this_page, 0, ' ');
940 /* If there are variables in the URL, we must grab them now */
941 for (a = 0; a < strlen(cmd); ++a) {
942 if ((cmd[a] == '?') || (cmd[a] == '&')) {
943 for (b = a; b < strlen(cmd); ++b)
946 addurls(&cmd[a + 1]);
952 /* Static content can be sent without connecting to Citadel. */
953 if (!strcasecmp(action, "static")) {
954 safestrncpy(buf, arg1, sizeof buf);
955 for (a = 0; a < strlen(buf); ++a)
959 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
963 * If we're not connected to a Citadel server, try to hook up the
966 if (!WC->connected) {
967 if (!strcasecmp(ctdlhost, "uds")) {
968 /* unix domain socket */
969 sprintf(buf, "%s/citadel.socket", ctdlport);
970 WC->serv_sock = uds_connectsock(buf);
974 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
977 if (WC->serv_sock < 0) {
979 goto SKIP_ALL_THIS_CRAP;
983 serv_getln(buf, sizeof buf); /* get the server welcome message */
984 locate_host(browser_host, WC->http_sock);
985 get_serv_info(browser_host, user_agent);
986 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
987 wprintf("You are connected to a Citadel "
988 "server running Citadel %d.%02d;\nin "
989 "order to run this version of WebCit "
990 "you must also have Citadel %d.%02d or"
992 serv_info.serv_rev_level / 100,
993 serv_info.serv_rev_level % 100,
994 MINIMUM_CIT_VERSION / 100,
995 MINIMUM_CIT_VERSION % 100
997 end_webcit_session();
998 goto SKIP_ALL_THIS_CRAP;
1004 * Functions which can be performed without logging in
1006 if (!strcasecmp(action, "listsub")) {
1008 goto SKIP_ALL_THIS_CRAP;
1010 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1011 if (!strcasecmp(action, "freebusy")) {
1013 goto SKIP_ALL_THIS_CRAP;
1018 * If we're not logged in, but we have HTTP Authentication data,
1019 * try logging in to Citadel using that.
1021 if ((!WC->logged_in)
1022 && (strlen(c_httpauth_user) > 0)
1023 && (strlen(c_httpauth_pass) > 0)) {
1024 serv_printf("USER %s", c_httpauth_user);
1025 serv_getln(buf, sizeof buf);
1026 if (buf[0] == '3') {
1027 serv_printf("PASS %s", c_httpauth_pass);
1028 serv_getln(buf, sizeof buf);
1029 if (buf[0] == '2') {
1030 become_logged_in(c_httpauth_user,
1031 c_httpauth_pass, buf);
1032 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1033 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1039 * The GroupDAV stuff relies on HTTP authentication instead of
1040 * our session's authentication.
1042 if (!strncasecmp(action, "groupdav", 8)) {
1043 groupdav_main(req, ContentType, /* do GroupDAV methods */
1044 ContentLength, content+body_start);
1045 if (!WC->logged_in) {
1046 WC->killthis = 1; /* If not logged in, don't */
1047 } /* keep the session active */
1048 goto SKIP_ALL_THIS_CRAP;
1053 * Automatically send requests with any method other than GET or
1054 * POST to the GroupDAV code as well.
1056 if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1057 groupdav_main(req, ContentType, /* do GroupDAV methods */
1058 ContentLength, content+body_start);
1059 if (!WC->logged_in) {
1060 WC->killthis = 1; /* If not logged in, don't */
1061 } /* keep the session active */
1062 goto SKIP_ALL_THIS_CRAP;
1066 * If we're not logged in, but we have username and password cookies
1067 * supplied by the browser, try using them to log in.
1069 if ((!WC->logged_in)
1070 && (strlen(c_username) > 0)
1071 && (strlen(c_password) > 0)) {
1072 serv_printf("USER %s", c_username);
1073 serv_getln(buf, sizeof buf);
1074 if (buf[0] == '3') {
1075 serv_printf("PASS %s", c_password);
1076 serv_getln(buf, sizeof buf);
1077 if (buf[0] == '2') {
1078 become_logged_in(c_username, c_password, buf);
1083 * If we don't have a current room, but a cookie specifying the
1084 * current room is supplied, make an effort to go there.
1086 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1087 serv_printf("GOTO %s", c_roomname);
1088 serv_getln(buf, sizeof buf);
1089 if (buf[0] == '2') {
1090 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1095 * If there are instant messages waiting, retrieve them for display.
1097 check_for_instant_messages();
1099 if (!strcasecmp(action, "image")) {
1103 * All functions handled below this point ... make sure we log in
1104 * before doing anything else!
1106 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1108 } else if (!WC->logged_in) {
1109 display_login(NULL);
1113 * Various commands...
1116 else if (!strcasecmp(action, "do_welcome")) {
1118 } else if (!strcasecmp(action, "blank")) {
1120 } else if (!strcasecmp(action, "do_template")) {
1122 } else if (!strcasecmp(action, "display_aide_menu")) {
1123 display_aide_menu();
1124 } else if (!strcasecmp(action, "display_main_menu")) {
1125 display_main_menu();
1126 } else if (!strcasecmp(action, "who")) {
1128 } else if (!strcasecmp(action, "who_inner_html")) {
1130 } else if (!strcasecmp(action, "knrooms")) {
1132 } else if (!strcasecmp(action, "gotonext")) {
1135 } else if (!strcasecmp(action, "skip")) {
1137 } else if (!strcasecmp(action, "ungoto")) {
1139 } else if (!strcasecmp(action, "dotgoto")) {
1140 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1143 smart_goto(bstr("room"));
1144 } else if (!strcasecmp(action, "dotskip")) {
1145 smart_goto(bstr("room"));
1146 } else if (!strcasecmp(action, "termquit")) {
1148 } else if (!strcasecmp(action, "readnew")) {
1149 readloop("readnew");
1150 } else if (!strcasecmp(action, "readold")) {
1151 readloop("readold");
1152 } else if (!strcasecmp(action, "readfwd")) {
1153 readloop("readfwd");
1154 } else if (!strcasecmp(action, "headers")) {
1155 readloop("headers");
1156 } else if (!strcasecmp(action, "msg")) {
1158 } else if (!strcasecmp(action, "display_enter")) {
1160 } else if (!strcasecmp(action, "post")) {
1162 } else if (!strcasecmp(action, "move_msg")) {
1164 } else if (!strcasecmp(action, "delete_msg")) {
1166 } else if (!strcasecmp(action, "userlist")) {
1168 } else if (!strcasecmp(action, "showuser")) {
1170 } else if (!strcasecmp(action, "display_page")) {
1172 } else if (!strcasecmp(action, "page_user")) {
1174 } else if (!strcasecmp(action, "chat")) {
1176 } else if (!strcasecmp(action, "display_private")) {
1177 display_private("", 0);
1178 } else if (!strcasecmp(action, "goto_private")) {
1180 } else if (!strcasecmp(action, "zapped_list")) {
1182 } else if (!strcasecmp(action, "display_zap")) {
1184 } else if (!strcasecmp(action, "zap")) {
1186 } else if (!strcasecmp(action, "display_entroom")) {
1188 } else if (!strcasecmp(action, "entroom")) {
1190 } else if (!strcasecmp(action, "display_whok")) {
1192 } else if (!strcasecmp(action, "do_invt_kick")) {
1194 } else if (!strcasecmp(action, "display_editroom")) {
1196 } else if (!strcasecmp(action, "netedit")) {
1198 } else if (!strcasecmp(action, "editroom")) {
1200 } else if (!strcasecmp(action, "display_editinfo")) {
1201 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1202 } else if (!strcasecmp(action, "editinfo")) {
1203 save_edit("Room info", "EINF 1", 1);
1204 } else if (!strcasecmp(action, "display_editbio")) {
1205 sprintf(buf, "RBIO %s", WC->wc_username);
1206 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1207 } else if (!strcasecmp(action, "editbio")) {
1208 save_edit("Your bio", "EBIO", 0);
1209 } else if (!strcasecmp(action, "confirm_move_msg")) {
1211 } else if (!strcasecmp(action, "delete_room")) {
1213 } else if (!strcasecmp(action, "validate")) {
1215 } else if (!strcasecmp(action, "display_editpic")) {
1216 display_graphics_upload("your photo",
1219 } else if (!strcasecmp(action, "editpic")) {
1220 do_graphics_upload("UIMG 1|_userpic_");
1221 } else if (!strcasecmp(action, "display_editroompic")) {
1222 display_graphics_upload("the icon for this room",
1225 } else if (!strcasecmp(action, "editroompic")) {
1226 do_graphics_upload("UIMG 1|_roompic_");
1227 } else if (!strcasecmp(action, "delete_floor")) {
1229 } else if (!strcasecmp(action, "rename_floor")) {
1231 } else if (!strcasecmp(action, "create_floor")) {
1233 } else if (!strcasecmp(action, "display_editfloorpic")) {
1234 sprintf(buf, "UIMG 0|_floorpic_|%s",
1235 bstr("which_floor"));
1236 display_graphics_upload("the icon for this floor",
1239 } else if (!strcasecmp(action, "editfloorpic")) {
1240 sprintf(buf, "UIMG 1|_floorpic_|%s",
1241 bstr("which_floor"));
1242 do_graphics_upload(buf);
1243 } else if (!strcasecmp(action, "display_reg")) {
1245 } else if (!strcasecmp(action, "display_changepw")) {
1247 } else if (!strcasecmp(action, "changepw")) {
1249 } else if (!strcasecmp(action, "display_edit_node")) {
1250 display_edit_node();
1251 } else if (!strcasecmp(action, "edit_node")) {
1253 } else if (!strcasecmp(action, "display_netconf")) {
1255 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1256 display_confirm_delete_node();
1257 } else if (!strcasecmp(action, "delete_node")) {
1259 } else if (!strcasecmp(action, "display_add_node")) {
1261 } else if (!strcasecmp(action, "add_node")) {
1263 } else if (!strcasecmp(action, "terminate_session")) {
1265 terminate_session();
1266 } else if (!strcasecmp(action, "edit_me")) {
1268 } else if (!strcasecmp(action, "display_siteconfig")) {
1269 display_siteconfig();
1270 } else if (!strcasecmp(action, "chat_recv")) {
1272 } else if (!strcasecmp(action, "chat_send")) {
1274 } else if (!strcasecmp(action, "siteconfig")) {
1276 } else if (!strcasecmp(action, "display_generic")) {
1278 } else if (!strcasecmp(action, "do_generic")) {
1280 } else if (!strcasecmp(action, "display_menubar")) {
1282 } else if (!strcasecmp(action, "output_mimepart")) {
1284 } else if (!strcasecmp(action, "edit_vcard")) {
1286 } else if (!strcasecmp(action, "submit_vcard")) {
1288 } else if (!strcasecmp(action, "select_user_to_edit")) {
1289 select_user_to_edit(NULL, NULL);
1290 } else if (!strcasecmp(action, "display_edituser")) {
1291 display_edituser(NULL, 0);
1292 } else if (!strcasecmp(action, "edituser")) {
1294 } else if (!strcasecmp(action, "create_user")) {
1296 } else if (!strcasecmp(action, "changeview")) {
1298 } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1300 } else if (!strcasecmp(action, "change_start_page")) {
1301 change_start_page();
1302 } else if (!strcasecmp(action, "display_floorconfig")) {
1303 display_floorconfig(NULL);
1304 } else if (!strcasecmp(action, "toggle_self_service")) {
1305 toggle_self_service();
1306 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1307 } else if (!strcasecmp(action, "display_edit_task")) {
1308 display_edit_task();
1309 } else if (!strcasecmp(action, "save_task")) {
1311 } else if (!strcasecmp(action, "display_edit_event")) {
1312 display_edit_event();
1313 } else if (!strcasecmp(action, "save_event")) {
1315 } else if (!strcasecmp(action, "respond_to_request")) {
1316 respond_to_request();
1317 } else if (!strcasecmp(action, "handle_rsvp")) {
1320 } else if (!strcasecmp(action, "summary")) {
1322 } else if (!strcasecmp(action, "iconbar")) {
1324 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1325 display_customize_iconbar();
1326 } else if (!strcasecmp(action, "commit_iconbar")) {
1328 } else if (!strcasecmp(action, "set_room_policy")) {
1330 } else if (!strcasecmp(action, "display_inetconf")) {
1332 } else if (!strcasecmp(action, "save_inetconf")) {
1334 } else if (!strcasecmp(action, "setup_wizard")) {
1336 } else if (!strcasecmp(action, "display_preferences")) {
1337 display_preferences();
1338 } else if (!strcasecmp(action, "set_preferences")) {
1340 } else if (!strcasecmp(action, "diagnostics")) {
1341 output_headers(1, 1, 1, 0, 0, 0, 0);
1343 wprintf("You're in session %d<hr />\n", WC->wc_session);
1344 wprintf("Command: <br /><PRE>\n");
1346 wprintf("</PRE><hr />\n");
1347 wprintf("Variables: <br /><PRE>\n");
1349 wprintf("</PRE><hr />\n");
1352 /* When all else fais, display the main menu. */
1354 display_main_menu();
1359 if (content != NULL) {
1364 if (WC->upload_length > 0) {
1366 WC->upload_length = 0;