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);
760 /* Authorization required page */
761 /* This is probably temporary and should be revisited */
762 void authorization_required(const char *message)
764 wprintf("HTTP/1.0 401 Authorization Required\r\n");
765 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
766 wprintf("Content-Type: text/html\r\n\r\n");
767 wprintf("<h1>Authorization Required</h1>\r\n");
768 wprintf("The resource you requested requires a valid username and password.");
769 wprintf("I could not log you in: %s\n", message);
774 void upload_handler(char *name, char *filename, char *partnum, char *disp,
775 void *content, char *cbtype, char *cbcharset,
776 size_t length, char *encoding, void *userdata)
778 struct urlcontent *u;
780 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
781 name, cbtype, length);
784 if ( (length > 0) && (strlen(cbtype) == 0) ) {
785 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
786 u->next = WC->urlstrings;
788 safestrncpy(u->url_key, name, sizeof(u->url_key));
789 u->url_data = malloc(length + 1);
790 memcpy(u->url_data, content, length);
791 u->url_data[length] = 0;
795 if ( (length > 0) && (strlen(cbtype) > 0) ) {
796 WC->upload = malloc(length);
797 if (WC->upload != NULL) {
798 WC->upload_length = length;
799 safestrncpy(WC->upload_filename, filename,
800 sizeof(WC->upload_filename));
801 safestrncpy(WC->upload_content_type, cbtype,
802 sizeof(WC->upload_content_type));
803 memcpy(WC->upload, content, length);
806 lprintf(3, "malloc() failed: %s\n", strerror(errno));
816 * Entry point for WebCit transaction
818 void session_loop(struct httprequest *req)
828 int ContentLength = 0;
830 char ContentType[512];
831 char *content = NULL;
832 char *content_end = NULL;
833 struct httprequest *hptr;
834 char browser_host[SIZ];
835 char user_agent[SIZ];
838 /* We stuff these with the values coming from the client cookies,
839 * so we can use them to reconnect a timed out session if we have to.
841 char c_username[SIZ];
842 char c_password[SIZ];
843 char c_roomname[SIZ];
844 char c_httpauth_string[SIZ];
845 char c_httpauth_user[SIZ];
846 char c_httpauth_pass[SIZ];
849 safestrncpy(c_username, "", sizeof c_username);
850 safestrncpy(c_password, "", sizeof c_password);
851 safestrncpy(c_roomname, "", sizeof c_roomname);
852 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
853 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
854 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
856 WC->upload_length = 0;
863 if (hptr == NULL) return;
865 safestrncpy(cmd, hptr->line, sizeof cmd);
867 extract_token(method, cmd, 0, ' ', sizeof method);
869 /* Figure out the action */
870 extract_token(action, cmd, 1, '/', sizeof action);
871 if (strstr(action, "?")) *strstr(action, "?") = 0;
872 if (strstr(action, "&")) *strstr(action, "&") = 0;
873 if (strstr(action, " ")) *strstr(action, " ") = 0;
875 extract_token(arg1, cmd, 2, '/', sizeof arg1);
876 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
877 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
878 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
880 extract_token(arg2, cmd, 3, '/', sizeof arg2);
881 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
882 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
883 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
885 extract_token(arg3, cmd, 4, '/', sizeof arg3);
886 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
887 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
888 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
890 while (hptr != NULL) {
891 safestrncpy(buf, hptr->line, sizeof buf);
894 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
895 safestrncpy(cookie, &buf[15], sizeof cookie);
896 cookie_to_stuff(cookie, NULL,
897 c_username, sizeof c_username,
898 c_password, sizeof c_password,
899 c_roomname, sizeof c_roomname);
901 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
902 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
903 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
904 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
906 else if (!strncasecmp(buf, "Content-length: ", 16)) {
907 ContentLength = atoi(&buf[16]);
909 else if (!strncasecmp(buf, "Content-type: ", 14)) {
910 safestrncpy(ContentType, &buf[14], sizeof ContentType);
912 else if (!strncasecmp(buf, "User-agent: ", 12)) {
913 safestrncpy(user_agent, &buf[12], sizeof user_agent);
915 else if (!strncasecmp(buf, "Host: ", 6)) {
916 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
918 /* Only WAP gateways explicitly name this content-type */
919 else if (strstr(buf, "text/vnd.wap.wml")) {
924 if (ContentLength > 0) {
925 content = malloc(ContentLength + SIZ);
926 memset(content, 0, ContentLength + SIZ);
927 sprintf(content, "Content-type: %s\n"
928 "Content-length: %d\n\n",
929 ContentType, ContentLength);
930 body_start = strlen(content);
932 /* Read the entire input data at once. */
933 client_read(WC->http_sock, &content[BytesRead+body_start],
936 if (!strncasecmp(ContentType,
937 "application/x-www-form-urlencoded", 33)) {
938 addurls(&content[body_start]);
939 } else if (!strncasecmp(ContentType, "multipart", 9)) {
940 content_end = content + ContentLength + body_start;
941 mime_parser(content, content_end, *upload_handler,
942 NULL, NULL, NULL, 0);
948 /* make a note of where we are in case the user wants to save it */
949 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
950 remove_token(WC->this_page, 2, ' ');
951 remove_token(WC->this_page, 0, ' ');
953 /* If there are variables in the URL, we must grab them now */
954 for (a = 0; a < strlen(cmd); ++a) {
955 if ((cmd[a] == '?') || (cmd[a] == '&')) {
956 for (b = a; b < strlen(cmd); ++b)
959 addurls(&cmd[a + 1]);
965 /* Static content can be sent without connecting to Citadel. */
966 if (!strcasecmp(action, "static")) {
967 safestrncpy(buf, arg1, sizeof buf);
968 for (a = 0; a < strlen(buf); ++a)
972 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
976 * If we're not connected to a Citadel server, try to hook up the
979 if (!WC->connected) {
980 if (!strcasecmp(ctdlhost, "uds")) {
981 /* unix domain socket */
982 sprintf(buf, "%s/citadel.socket", ctdlport);
983 WC->serv_sock = uds_connectsock(buf);
987 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
990 if (WC->serv_sock < 0) {
992 goto SKIP_ALL_THIS_CRAP;
996 serv_getln(buf, sizeof buf); /* get the server welcome message */
997 locate_host(browser_host, WC->http_sock);
998 get_serv_info(browser_host, user_agent);
999 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1000 wprintf("You are connected to a Citadel "
1001 "server running Citadel %d.%02d;\nin "
1002 "order to run this version of WebCit "
1003 "you must also have Citadel %d.%02d or"
1005 serv_info.serv_rev_level / 100,
1006 serv_info.serv_rev_level % 100,
1007 MINIMUM_CIT_VERSION / 100,
1008 MINIMUM_CIT_VERSION % 100
1010 end_webcit_session();
1011 goto SKIP_ALL_THIS_CRAP;
1017 * Functions which can be performed without logging in
1019 if (!strcasecmp(action, "listsub")) {
1021 goto SKIP_ALL_THIS_CRAP;
1023 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1024 if (!strcasecmp(action, "freebusy")) {
1026 goto SKIP_ALL_THIS_CRAP;
1031 * If we're not logged in, but we have HTTP Authentication data,
1032 * try logging in to Citadel using that.
1034 if ((!WC->logged_in)
1035 && (strlen(c_httpauth_user) > 0)
1036 && (strlen(c_httpauth_pass) > 0)) {
1037 serv_printf("USER %s", c_httpauth_user);
1038 serv_getln(buf, sizeof buf);
1039 if (buf[0] == '3') {
1040 serv_printf("PASS %s", c_httpauth_pass);
1041 serv_getln(buf, sizeof buf);
1042 if (buf[0] == '2') {
1043 become_logged_in(c_httpauth_user,
1044 c_httpauth_pass, buf);
1045 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1046 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1048 /* Should only display when password is wrong */
1049 authorization_required(&buf[4]);
1050 goto SKIP_ALL_THIS_CRAP;
1055 /* This needs to run early */
1056 if (!strcasecmp(action, "rss")) {
1057 display_rss(bstr("room"));
1058 goto SKIP_ALL_THIS_CRAP;
1062 * The GroupDAV stuff relies on HTTP authentication instead of
1063 * our session's authentication.
1065 if (!strncasecmp(action, "groupdav", 8)) {
1066 groupdav_main(req, ContentType, /* do GroupDAV methods */
1067 ContentLength, content+body_start);
1068 if (!WC->logged_in) {
1069 WC->killthis = 1; /* If not logged in, don't */
1070 } /* keep the session active */
1071 goto SKIP_ALL_THIS_CRAP;
1076 * Automatically send requests with any method other than GET or
1077 * POST to the GroupDAV code as well.
1079 if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1080 groupdav_main(req, ContentType, /* do GroupDAV methods */
1081 ContentLength, content+body_start);
1082 if (!WC->logged_in) {
1083 WC->killthis = 1; /* If not logged in, don't */
1084 } /* keep the session active */
1085 goto SKIP_ALL_THIS_CRAP;
1089 * If we're not logged in, but we have username and password cookies
1090 * supplied by the browser, try using them to log in.
1092 if ((!WC->logged_in)
1093 && (strlen(c_username) > 0)
1094 && (strlen(c_password) > 0)) {
1095 serv_printf("USER %s", c_username);
1096 serv_getln(buf, sizeof buf);
1097 if (buf[0] == '3') {
1098 serv_printf("PASS %s", c_password);
1099 serv_getln(buf, sizeof buf);
1100 if (buf[0] == '2') {
1101 become_logged_in(c_username, c_password, buf);
1106 * If we don't have a current room, but a cookie specifying the
1107 * current room is supplied, make an effort to go there.
1109 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1110 serv_printf("GOTO %s", c_roomname);
1111 serv_getln(buf, sizeof buf);
1112 if (buf[0] == '2') {
1113 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1118 * If there are instant messages waiting, retrieve them for display.
1120 check_for_instant_messages();
1122 if (!strcasecmp(action, "image")) {
1126 * All functions handled below this point ... make sure we log in
1127 * before doing anything else!
1129 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1131 } else if (!WC->logged_in) {
1132 display_login(NULL);
1136 * Various commands...
1139 else if (!strcasecmp(action, "do_welcome")) {
1141 } else if (!strcasecmp(action, "blank")) {
1143 } else if (!strcasecmp(action, "do_template")) {
1145 } else if (!strcasecmp(action, "display_aide_menu")) {
1146 display_aide_menu();
1147 } else if (!strcasecmp(action, "display_main_menu")) {
1148 display_main_menu();
1149 } else if (!strcasecmp(action, "who")) {
1151 } else if (!strcasecmp(action, "who_inner_html")) {
1153 } else if (!strcasecmp(action, "knrooms")) {
1155 } else if (!strcasecmp(action, "gotonext")) {
1158 } else if (!strcasecmp(action, "skip")) {
1160 } else if (!strcasecmp(action, "ungoto")) {
1162 } else if (!strcasecmp(action, "dotgoto")) {
1163 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1166 smart_goto(bstr("room"));
1167 } else if (!strcasecmp(action, "dotskip")) {
1168 smart_goto(bstr("room"));
1169 } else if (!strcasecmp(action, "termquit")) {
1171 } else if (!strcasecmp(action, "readnew")) {
1172 readloop("readnew");
1173 } else if (!strcasecmp(action, "readold")) {
1174 readloop("readold");
1175 } else if (!strcasecmp(action, "readfwd")) {
1176 readloop("readfwd");
1177 } else if (!strcasecmp(action, "headers")) {
1178 readloop("headers");
1179 } else if (!strcasecmp(action, "msg")) {
1181 } else if (!strcasecmp(action, "display_enter")) {
1183 } else if (!strcasecmp(action, "post")) {
1185 } else if (!strcasecmp(action, "move_msg")) {
1187 } else if (!strcasecmp(action, "delete_msg")) {
1189 } else if (!strcasecmp(action, "userlist")) {
1191 } else if (!strcasecmp(action, "showuser")) {
1193 } else if (!strcasecmp(action, "display_page")) {
1195 } else if (!strcasecmp(action, "page_user")) {
1197 } else if (!strcasecmp(action, "chat")) {
1199 } else if (!strcasecmp(action, "display_private")) {
1200 display_private("", 0);
1201 } else if (!strcasecmp(action, "goto_private")) {
1203 } else if (!strcasecmp(action, "zapped_list")) {
1205 } else if (!strcasecmp(action, "display_zap")) {
1207 } else if (!strcasecmp(action, "zap")) {
1209 } else if (!strcasecmp(action, "display_entroom")) {
1211 } else if (!strcasecmp(action, "entroom")) {
1213 } else if (!strcasecmp(action, "display_whok")) {
1215 } else if (!strcasecmp(action, "do_invt_kick")) {
1217 } else if (!strcasecmp(action, "display_editroom")) {
1219 } else if (!strcasecmp(action, "netedit")) {
1221 } else if (!strcasecmp(action, "editroom")) {
1223 } else if (!strcasecmp(action, "display_editinfo")) {
1224 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1225 } else if (!strcasecmp(action, "editinfo")) {
1226 save_edit("Room info", "EINF 1", 1);
1227 } else if (!strcasecmp(action, "display_editbio")) {
1228 sprintf(buf, "RBIO %s", WC->wc_username);
1229 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1230 } else if (!strcasecmp(action, "editbio")) {
1231 save_edit("Your bio", "EBIO", 0);
1232 } else if (!strcasecmp(action, "confirm_move_msg")) {
1234 } else if (!strcasecmp(action, "delete_room")) {
1236 } else if (!strcasecmp(action, "validate")) {
1238 } else if (!strcasecmp(action, "display_editpic")) {
1239 display_graphics_upload("your photo",
1242 } else if (!strcasecmp(action, "editpic")) {
1243 do_graphics_upload("UIMG 1|_userpic_");
1244 } else if (!strcasecmp(action, "display_editroompic")) {
1245 display_graphics_upload("the icon for this room",
1248 } else if (!strcasecmp(action, "editroompic")) {
1249 do_graphics_upload("UIMG 1|_roompic_");
1250 } else if (!strcasecmp(action, "delete_floor")) {
1252 } else if (!strcasecmp(action, "rename_floor")) {
1254 } else if (!strcasecmp(action, "create_floor")) {
1256 } else if (!strcasecmp(action, "display_editfloorpic")) {
1257 sprintf(buf, "UIMG 0|_floorpic_|%s",
1258 bstr("which_floor"));
1259 display_graphics_upload("the icon for this floor",
1262 } else if (!strcasecmp(action, "editfloorpic")) {
1263 sprintf(buf, "UIMG 1|_floorpic_|%s",
1264 bstr("which_floor"));
1265 do_graphics_upload(buf);
1266 } else if (!strcasecmp(action, "display_reg")) {
1268 } else if (!strcasecmp(action, "display_changepw")) {
1270 } else if (!strcasecmp(action, "changepw")) {
1272 } else if (!strcasecmp(action, "display_edit_node")) {
1273 display_edit_node();
1274 } else if (!strcasecmp(action, "edit_node")) {
1276 } else if (!strcasecmp(action, "display_netconf")) {
1278 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1279 display_confirm_delete_node();
1280 } else if (!strcasecmp(action, "delete_node")) {
1282 } else if (!strcasecmp(action, "display_add_node")) {
1284 } else if (!strcasecmp(action, "add_node")) {
1286 } else if (!strcasecmp(action, "terminate_session")) {
1288 terminate_session();
1289 } else if (!strcasecmp(action, "edit_me")) {
1291 } else if (!strcasecmp(action, "display_siteconfig")) {
1292 display_siteconfig();
1293 } else if (!strcasecmp(action, "chat_recv")) {
1295 } else if (!strcasecmp(action, "chat_send")) {
1297 } else if (!strcasecmp(action, "siteconfig")) {
1299 } else if (!strcasecmp(action, "display_generic")) {
1301 } else if (!strcasecmp(action, "do_generic")) {
1303 } else if (!strcasecmp(action, "display_menubar")) {
1305 } else if (!strcasecmp(action, "output_mimepart")) {
1307 } else if (!strcasecmp(action, "edit_vcard")) {
1309 } else if (!strcasecmp(action, "submit_vcard")) {
1311 } else if (!strcasecmp(action, "select_user_to_edit")) {
1312 select_user_to_edit(NULL, NULL);
1313 } else if (!strcasecmp(action, "display_edituser")) {
1314 display_edituser(NULL, 0);
1315 } else if (!strcasecmp(action, "edituser")) {
1317 } else if (!strcasecmp(action, "create_user")) {
1319 } else if (!strcasecmp(action, "changeview")) {
1321 } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1323 } else if (!strcasecmp(action, "change_start_page")) {
1324 change_start_page();
1325 } else if (!strcasecmp(action, "display_floorconfig")) {
1326 display_floorconfig(NULL);
1327 } else if (!strcasecmp(action, "toggle_self_service")) {
1328 toggle_self_service();
1329 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1330 } else if (!strcasecmp(action, "display_edit_task")) {
1331 display_edit_task();
1332 } else if (!strcasecmp(action, "save_task")) {
1334 } else if (!strcasecmp(action, "display_edit_event")) {
1335 display_edit_event();
1336 } else if (!strcasecmp(action, "save_event")) {
1338 } else if (!strcasecmp(action, "respond_to_request")) {
1339 respond_to_request();
1340 } else if (!strcasecmp(action, "handle_rsvp")) {
1343 } else if (!strcasecmp(action, "summary")) {
1345 } else if (!strcasecmp(action, "iconbar")) {
1347 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1348 display_customize_iconbar();
1349 } else if (!strcasecmp(action, "commit_iconbar")) {
1351 } else if (!strcasecmp(action, "set_room_policy")) {
1353 } else if (!strcasecmp(action, "display_inetconf")) {
1355 } else if (!strcasecmp(action, "save_inetconf")) {
1357 } else if (!strcasecmp(action, "setup_wizard")) {
1359 } else if (!strcasecmp(action, "display_preferences")) {
1360 display_preferences();
1361 } else if (!strcasecmp(action, "set_preferences")) {
1363 } else if (!strcasecmp(action, "diagnostics")) {
1364 output_headers(1, 1, 1, 0, 0, 0, 0);
1366 wprintf("You're in session %d<hr />\n", WC->wc_session);
1367 wprintf("Command: <br /><PRE>\n");
1369 wprintf("</PRE><hr />\n");
1370 wprintf("Variables: <br /><PRE>\n");
1372 wprintf("</PRE><hr />\n");
1375 /* When all else fais, display the main menu. */
1377 display_main_menu();
1382 if (content != NULL) {
1387 if (WC->upload_length > 0) {
1389 WC->upload_length = 0;