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"
15 char request_method[128];
18 * String to unset the cookie.
19 * Any date "in the past" will work, so I chose my birthday, right down to
20 * the exact minute. :)
22 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
24 void unescape_input(char *buf)
29 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
30 buf[strlen(buf) - 1] = 0;
32 for (a = 0; a < strlen(buf); ++a) {
40 sscanf(hex, "%02x", &b);
42 strcpy(&buf[a + 1], &buf[a + 3]);
49 void addurls(char *url)
57 while (strlen(up) > 0) {
59 /* locate the = sign */
60 safestrncpy(buf, up, sizeof buf);
62 for (a = 255; a >= 0; --a)
69 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
70 u->next = WC->urlstrings;
72 safestrncpy(u->url_key, buf, sizeof u->url_key);
74 /* now chop that part off */
75 for (a = 0; a <= b; ++a)
78 /* locate "&" and "?" delimiters */
81 for (a = 0; a < strlen(up); ++a) {
82 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
89 for (a = 0; a < b; ++a)
93 u->url_data = malloc(strlen(up) + 2);
94 safestrncpy(u->url_data, up, strlen(up) + 1);
96 unescape_input(u->url_data);
104 struct urlcontent *u;
106 while (WC->urlstrings != NULL) {
107 free(WC->urlstrings->url_data);
108 u = WC->urlstrings->next;
109 free(WC->urlstrings);
115 * Diagnostic function to display the contents of all variables
119 struct urlcontent *u;
121 for (u = WC->urlstrings; u != NULL; u = u->next) {
122 wprintf("%38s = %s\n", u->url_key, u->url_data);
126 char *bstr(char *key)
128 struct urlcontent *u;
130 for (u = WC->urlstrings; u != NULL; u = u->next) {
131 if (!strcasecmp(u->url_key, key))
132 return (u->url_data);
138 void wprintf(const char *format,...)
143 va_start(arg_ptr, format);
144 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
147 client_write(wbuf, strlen(wbuf));
152 * wDumpContent() wraps up an HTTP session, closes tags, etc.
154 * print_standard_html_footer should be set to 0 to transmit only, 1 to
155 * append the main menu and closing tags, or 2 to
156 * append the closing tags only.
158 void wDumpContent(int print_standard_html_footer)
160 if (print_standard_html_footer) {
161 wprintf("</div>\n"); /* end of "text" div */
162 do_template("trailing");
165 /* If we've been saving it all up for one big output burst,
166 * go ahead and do that now.
173 * Copy a string, escaping characters which have meaning in HTML. If
174 * nbsp is nonzero, spaces are converted to non-breaking spaces.
176 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
181 for (a = 0; a < strlen(strbuf); ++a) {
182 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] == '\'')
191 strcat(target, "'");
192 else if (strbuf[a] == LB)
194 else if (strbuf[a] == RB)
196 else if (strbuf[a] == QU)
197 strcat(target, "\"");
198 else if ((strbuf[a] == 32) && (nbsp == 1))
199 strcat(target, " ");
200 else if ((strbuf[a] == '\n') && (nolinebreaks))
201 strcat(target, ""); /* nothing */
202 else if ((strbuf[a] == '\r') && (nolinebreaks))
203 strcat(target, ""); /* nothing */
205 strncat(target, &strbuf[a], 1);
209 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
213 if (strbuf == NULL) return;
214 buf = malloc( (3 * strlen(strbuf)) + SIZ );
215 stresc(buf, strbuf, nbsp, nolinebreaks);
220 void escputs(char *strbuf)
222 escputs1(strbuf, 0, 0);
226 * Escape a string for feeding out as a URL.
227 * Returns a pointer to a buffer that must be freed by the caller!
229 void urlesc(char *outbuf, char *strbuf)
232 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
236 for (a = 0; a < strlen(strbuf); ++a) {
238 for (b = 0; b < strlen(ec); ++b) {
239 if (strbuf[a] == ec[b])
244 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
246 sprintf(&outbuf[b], "%c", strbuf[a]);
250 void urlescputs(char *strbuf)
254 urlesc(outbuf, strbuf);
255 wprintf("%s", outbuf);
260 * Copy a string, escaping characters for JavaScript strings.
262 void jsesc(char *target, char *strbuf)
267 for (a = 0; a < strlen(strbuf); ++a) {
268 if (strbuf[a] == '<')
270 else if (strbuf[a] == '>')
272 else if (strbuf[a] == '\"')
273 strcat(target, """);
274 else if (strbuf[a] == '&')
275 strcat(target, "&;");
276 else if (strbuf[a] == '\'')
277 strcat(target, "\\'");
279 strncat(target, &strbuf[a], 1);
284 void jsescputs(char *strbuf)
288 jsesc(outbuf, strbuf);
289 wprintf("%s", outbuf);
293 * Copy a string, escaping characters for message text hold
295 void msgesc(char *target, char *strbuf)
300 for (a = 0; a < strlen(strbuf); ++a) {
301 if (strbuf[a] == '\'')
302 strcat(target, "\\'");
303 else if (strbuf[a] == '\n')
305 else if (strbuf[a] == '\r')
308 strncat(target, &strbuf[a], 1);
313 void msgescputs(char *strbuf) {
316 if (strbuf == NULL) return;
317 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
318 msgesc(outbuf, strbuf);
319 wprintf("%s", outbuf);
327 * Output all that important stuff that the browser will want to see
329 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
330 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
332 int do_room_banner, /* 0=no, 1=yes, */
333 /* 2 = I'm going to embed my own, so don't open the */
334 /* <div id="content"> either. */
336 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
337 int refresh30, /* 1 = automatically refresh page every 30 seconds */
338 int suppress_check, /* 1 = suppress check for instant messages */
339 int cache /* 1 = allow browser to cache this page */
344 wprintf("HTTP/1.0 200 OK\n");
345 httpdate(httpnow, time(NULL));
347 if (do_httpheaders) {
348 wprintf("Content-type: text/html; charset=utf-8\r\n"
350 "Connection: close\r\n",
351 SERVER, serv_info.serv_software
356 wprintf("Pragma: public\r\n"
357 "Cache-Control: max-age=3600, must-revalidate\r\n"
358 "Last-modified: %s\r\n",
363 wprintf("Pragma: no-cache\r\n"
364 "Cache-Control: no-store\r\n"
368 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
369 WC->wc_password, WC->wc_roomname);
372 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
374 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
375 if (server_cookie != NULL) {
376 wprintf("%s\n", server_cookie);
385 svprintf("REFRESHTAG", WCS_STRING, "%s",
386 "<meta http-equiv=\"refresh\" content=\"30\" />\n");
389 svprintf("REFRESHTAG", WCS_STRING, "%s",
390 "<meta http-equiv=\"refresh\" content=\"500363689;\" />\n");
399 if (WC->HaveInstantMessages) {
400 wprintf("<div id=\"page_popup\">\n");
404 if (strlen(WC->ImportantMessage) > 0) {
405 wprintf("<div id=\"important_message\">\n");
406 wprintf("<SPAN CLASS=\"imsg\">"
407 "%s</SPAN><br />\n", WC->ImportantMessage);
409 wprintf("<script type=\"text/javascript\">\n"
410 " setTimeout('hide_imsg_popup()', 2000); \n"
412 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
414 if ( (WC->logged_in) && (!unset_cookies) ) {
415 wprintf("<div id=\"iconbar\">");
419 if (do_room_banner == 1) {
420 wprintf("<div id=\"banner\">\n");
421 embed_room_banner(NULL, navbar_default);
426 if (do_room_banner == 1) {
427 wprintf("<div id=\"content\">\n");
433 * Generic function to do an HTTP redirect. Easy and fun.
435 void http_redirect(char *whichpage) {
436 wprintf("HTTP/1.0 302 Moved Temporarily\n");
437 wprintf("Location: %s\r\n", whichpage);
438 wprintf("URI: %s\r\n", whichpage);
439 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
440 wprintf("<html><body>");
441 wprintf("Go <A HREF=\"%s\">here</A>.", whichpage);
442 wprintf("</body></html>\n");
447 void check_for_instant_messages()
452 serv_getln(buf, sizeof buf);
453 if (buf[3] == '*') WC->HaveInstantMessages = 1;
460 * Output a piece of content to the web browser
462 void http_transmit_thing(char *thing, size_t length, char *content_type,
465 output_headers(0, 0, 0, 0, 0, 0, is_static);
467 wprintf("Content-type: %s\r\n"
469 "Connection: close\r\n",
474 /* If we can send the data out compressed, please do so. */
476 char *compressed_data = NULL;
477 uLongf compressed_len;
479 compressed_len = (uLongf) ((length * 101) / 100) + 100;
480 compressed_data = malloc(compressed_len);
482 if (compress_gzip((Bytef *) compressed_data,
485 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
486 wprintf("Content-encoding: gzip\r\n"
487 "Content-length: %ld\r\n"
489 (long) compressed_len
491 client_write(compressed_data, (size_t)compressed_len);
492 free(compressed_data);
498 /* No compression ... just send it out as-is */
499 wprintf("Content-length: %ld\r\n"
503 client_write(thing, (size_t)length);
509 void output_static(char *what)
516 char content_type[128];
518 sprintf(buf, "static/%s", what);
519 fp = fopen(buf, "rb");
521 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
522 wprintf("Content-Type: text/plain\r\n");
524 wprintf("Cannot open %s: %s\n", what, strerror(errno));
526 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
527 safestrncpy(content_type, "image/gif", sizeof content_type);
528 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
529 safestrncpy(content_type, "text/plain", sizeof content_type);
530 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
531 safestrncpy(content_type, "text/css", sizeof content_type);
532 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
533 safestrncpy(content_type, "image/jpeg", sizeof content_type);
534 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
535 safestrncpy(content_type, "image/png", sizeof content_type);
536 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
537 safestrncpy(content_type, "image/x-icon", sizeof content_type);
538 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
539 safestrncpy(content_type, "text/html", sizeof content_type);
540 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
541 safestrncpy(content_type, "text/html", sizeof content_type);
542 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
543 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
544 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
545 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
546 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
547 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
548 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
549 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
550 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
551 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
552 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
553 safestrncpy(content_type, "text/javascript", sizeof content_type);
555 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
557 fstat(fileno(fp), &statbuf);
558 bytes = statbuf.st_size;
559 bigbuffer = malloc(bytes + 2);
560 fread(bigbuffer, bytes, 1, fp);
563 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
566 if (!strcasecmp(bstr("force_close_session"), "yes")) {
567 end_webcit_session();
573 * When the browser requests an image file from the Citadel server,
574 * this function is called to transmit it.
579 char *xferbuf = NULL;
582 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
583 serv_getln(buf, sizeof buf);
585 bytes = extract_long(&buf[4], 0);
586 xferbuf = malloc(bytes + 2);
588 /* Read it from the server */
589 read_server_binary(xferbuf, bytes);
591 serv_getln(buf, sizeof buf);
593 /* Write it to the browser */
594 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");
609 * Generic function to output an arbitrary MIME part from an arbitrary
610 * message number on the server.
612 void output_mimepart()
616 char content_type[SIZ];
617 char *content = NULL;
619 serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
620 serv_getln(buf, sizeof buf);
622 bytes = extract_long(&buf[4], 0);
623 content = malloc(bytes + 2);
624 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
625 output_headers(0, 0, 0, 0, 0, 0, 0);
626 read_server_binary(content, bytes);
628 serv_getln(buf, sizeof buf);
629 http_transmit_thing(content, bytes, content_type, 0);
632 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
633 output_headers(0, 0, 0, 0, 0, 0, 0);
634 wprintf("Content-Type: text/plain\r\n");
636 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
644 char *load_mimepart(long msgnum, char *partnum)
648 char content_type[SIZ];
651 serv_printf("OPNA %ld|%s", msgnum, partnum);
652 serv_getln(buf, sizeof buf);
654 bytes = extract_long(&buf[4], 0);
655 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
657 content = malloc(bytes + 2);
658 read_server_binary(content, bytes);
661 serv_getln(buf, sizeof buf);
662 content[bytes] = 0; /* null terminate for good measure */
673 * Convenience functions to display a page containing only a string
675 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
677 wprintf("HTTP/1.0 200 OK\n");
678 output_headers(1, 1, 2, 0, 0, 0, 0);
679 wprintf("<div id=\"banner\">\n");
680 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
681 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
682 wprintf("</TD></TR></TABLE>\n");
683 wprintf("</div>\n<div id=\"content\">\n");
684 escputs(messagetext);
692 * Display a blank page.
694 void blank_page(void) {
695 output_headers(1, 1, 0, 0, 1, 0, 0);
701 * A template has been requested
703 void url_do_template(void) {
704 do_template(bstr("template"));
710 * Offer to make any page the user's "start page."
712 void offer_start_page(void) {
713 wprintf("<A HREF=\"/change_start_page?startpage=");
714 urlescputs(WC->this_page);
715 wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
716 wprintf(_("Make this my start page"));
717 wprintf("</FONT></A>");
722 * Change the user's start page
724 void change_start_page(void) {
726 if (bstr("startpage") == NULL) {
727 safestrncpy(WC->ImportantMessage,
728 _("You no longer have a start page selected."),
729 sizeof WC->ImportantMessage);
734 set_preference("startpage", bstr("startpage"), 1);
736 output_headers(1, 1, 0, 0, 0, 0, 0);
737 do_template("newstartpage");
744 void display_success(char *successmessage)
746 convenience_page("007700", "OK", successmessage);
750 /* Authorization required page */
751 /* This is probably temporary and should be revisited */
752 void authorization_required(const char *message)
754 wprintf("HTTP/1.0 401 Authorization Required\r\n");
755 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
756 wprintf("Content-Type: text/html\r\n\r\n");
758 wprintf(_("Authorization Required"));
759 wprintf("</h1>\r\n");
760 wprintf(_("The resource you requested requires a valid username and password. "
761 "You could not be logged in: %s\n"), message);
766 void upload_handler(char *name, char *filename, char *partnum, char *disp,
767 void *content, char *cbtype, char *cbcharset,
768 size_t length, char *encoding, void *userdata)
770 struct urlcontent *u;
772 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
773 name, cbtype, length);
776 if ( (length > 0) && (strlen(cbtype) == 0) ) {
777 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
778 u->next = WC->urlstrings;
780 safestrncpy(u->url_key, name, sizeof(u->url_key));
781 u->url_data = malloc(length + 1);
782 memcpy(u->url_data, content, length);
783 u->url_data[length] = 0;
787 if ( (length > 0) && (strlen(cbtype) > 0) ) {
788 WC->upload = malloc(length);
789 if (WC->upload != NULL) {
790 WC->upload_length = length;
791 safestrncpy(WC->upload_filename, filename,
792 sizeof(WC->upload_filename));
793 safestrncpy(WC->upload_content_type, cbtype,
794 sizeof(WC->upload_content_type));
795 memcpy(WC->upload, content, length);
798 lprintf(3, "malloc() failed: %s\n", strerror(errno));
808 * Entry point for WebCit transaction
810 void session_loop(struct httprequest *req)
819 int ContentLength = 0;
821 char ContentType[512];
822 char *content = NULL;
823 char *content_end = NULL;
824 struct httprequest *hptr;
825 char browser_host[SIZ];
826 char user_agent[SIZ];
829 /* We stuff these with the values coming from the client cookies,
830 * so we can use them to reconnect a timed out session if we have to.
832 char c_username[SIZ];
833 char c_password[SIZ];
834 char c_roomname[SIZ];
835 char c_httpauth_string[SIZ];
836 char c_httpauth_user[SIZ];
837 char c_httpauth_pass[SIZ];
840 safestrncpy(c_username, "", sizeof c_username);
841 safestrncpy(c_password, "", sizeof c_password);
842 safestrncpy(c_roomname, "", sizeof c_roomname);
843 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
844 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
845 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
847 WC->upload_length = 0;
854 if (hptr == NULL) return;
856 safestrncpy(cmd, hptr->line, sizeof cmd);
858 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
860 /* Figure out the action */
861 extract_token(action, cmd, 1, '/', sizeof action);
862 if (strstr(action, "?")) *strstr(action, "?") = 0;
863 if (strstr(action, "&")) *strstr(action, "&") = 0;
864 if (strstr(action, " ")) *strstr(action, " ") = 0;
866 extract_token(arg1, cmd, 2, '/', sizeof arg1);
867 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
868 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
869 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
871 extract_token(arg2, cmd, 3, '/', sizeof arg2);
872 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
873 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
874 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
876 extract_token(arg3, cmd, 4, '/', sizeof arg3);
877 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
878 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
879 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
881 while (hptr != NULL) {
882 safestrncpy(buf, hptr->line, sizeof buf);
885 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
886 safestrncpy(cookie, &buf[15], sizeof cookie);
887 cookie_to_stuff(cookie, NULL,
888 c_username, sizeof c_username,
889 c_password, sizeof c_password,
890 c_roomname, sizeof c_roomname);
892 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
893 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
894 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
895 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
897 else if (!strncasecmp(buf, "Content-length: ", 16)) {
898 ContentLength = atoi(&buf[16]);
900 else if (!strncasecmp(buf, "Content-type: ", 14)) {
901 safestrncpy(ContentType, &buf[14], sizeof ContentType);
903 else if (!strncasecmp(buf, "User-agent: ", 12)) {
904 safestrncpy(user_agent, &buf[12], sizeof user_agent);
906 else if (!strncasecmp(buf, "Host: ", 6)) {
907 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
909 /* Only WAP gateways explicitly name this content-type */
910 else if (strstr(buf, "text/vnd.wap.wml")) {
915 if (ContentLength > 0) {
916 content = malloc(ContentLength + SIZ);
917 memset(content, 0, ContentLength + SIZ);
918 sprintf(content, "Content-type: %s\n"
919 "Content-length: %d\n\n",
920 ContentType, ContentLength);
921 body_start = strlen(content);
923 /* Read the entire input data at once. */
924 client_read(WC->http_sock, &content[BytesRead+body_start],
927 if (!strncasecmp(ContentType,
928 "application/x-www-form-urlencoded", 33)) {
929 addurls(&content[body_start]);
930 } else if (!strncasecmp(ContentType, "multipart", 9)) {
931 content_end = content + ContentLength + body_start;
932 mime_parser(content, content_end, *upload_handler,
933 NULL, NULL, NULL, 0);
939 /* make a note of where we are in case the user wants to save it */
940 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
941 remove_token(WC->this_page, 2, ' ');
942 remove_token(WC->this_page, 0, ' ');
944 /* If there are variables in the URL, we must grab them now */
945 for (a = 0; a < strlen(cmd); ++a) {
946 if ((cmd[a] == '?') || (cmd[a] == '&')) {
947 for (b = a; b < strlen(cmd); ++b)
950 addurls(&cmd[a + 1]);
956 /* Static content can be sent without connecting to Citadel. */
957 if (!strcasecmp(action, "static")) {
958 safestrncpy(buf, arg1, sizeof buf);
959 for (a = 0; a < strlen(buf); ++a)
963 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
967 * If we're not connected to a Citadel server, try to hook up the
970 if (!WC->connected) {
971 if (!strcasecmp(ctdlhost, "uds")) {
972 /* unix domain socket */
973 sprintf(buf, "%s/citadel.socket", ctdlport);
974 WC->serv_sock = uds_connectsock(buf);
978 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
981 if (WC->serv_sock < 0) {
983 goto SKIP_ALL_THIS_CRAP;
987 serv_getln(buf, sizeof buf); /* get the server welcome message */
988 locate_host(browser_host, WC->http_sock);
989 get_serv_info(browser_host, user_agent);
990 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
991 wprintf(_("You are connected to a Citadel "
992 "server running Citadel %d.%02d. \n"
993 "In order to run this version of WebCit "
994 "you must also have Citadel %d.%02d or"
996 serv_info.serv_rev_level / 100,
997 serv_info.serv_rev_level % 100,
998 MINIMUM_CIT_VERSION / 100,
999 MINIMUM_CIT_VERSION % 100
1001 end_webcit_session();
1002 goto SKIP_ALL_THIS_CRAP;
1008 * Functions which can be performed without logging in
1010 if (!strcasecmp(action, "listsub")) {
1012 goto SKIP_ALL_THIS_CRAP;
1014 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1015 if (!strcasecmp(action, "freebusy")) {
1017 goto SKIP_ALL_THIS_CRAP;
1022 * If we're not logged in, but we have HTTP Authentication data,
1023 * try logging in to Citadel using that.
1025 if ((!WC->logged_in)
1026 && (strlen(c_httpauth_user) > 0)
1027 && (strlen(c_httpauth_pass) > 0)) {
1028 serv_printf("USER %s", c_httpauth_user);
1029 serv_getln(buf, sizeof buf);
1030 if (buf[0] == '3') {
1031 serv_printf("PASS %s", c_httpauth_pass);
1032 serv_getln(buf, sizeof buf);
1033 if (buf[0] == '2') {
1034 become_logged_in(c_httpauth_user,
1035 c_httpauth_pass, buf);
1036 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1037 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1039 /* Should only display when password is wrong */
1040 authorization_required(&buf[4]);
1041 goto SKIP_ALL_THIS_CRAP;
1046 /* This needs to run early */
1047 if (!strcasecmp(action, "rss")) {
1048 display_rss(bstr("room"));
1049 goto SKIP_ALL_THIS_CRAP;
1053 * The GroupDAV stuff relies on HTTP authentication instead of
1054 * our session's authentication.
1056 if (!strncasecmp(action, "groupdav", 8)) {
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;
1067 * Automatically send requests with any method other than GET or
1068 * POST to the GroupDAV code as well.
1070 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1071 groupdav_main(req, ContentType, /* do GroupDAV methods */
1072 ContentLength, content+body_start);
1073 if (!WC->logged_in) {
1074 WC->killthis = 1; /* If not logged in, don't */
1075 } /* keep the session active */
1076 goto SKIP_ALL_THIS_CRAP;
1080 * If we're not logged in, but we have username and password cookies
1081 * supplied by the browser, try using them to log in.
1083 if ((!WC->logged_in)
1084 && (strlen(c_username) > 0)
1085 && (strlen(c_password) > 0)) {
1086 serv_printf("USER %s", c_username);
1087 serv_getln(buf, sizeof buf);
1088 if (buf[0] == '3') {
1089 serv_printf("PASS %s", c_password);
1090 serv_getln(buf, sizeof buf);
1091 if (buf[0] == '2') {
1092 become_logged_in(c_username, c_password, buf);
1097 * If we don't have a current room, but a cookie specifying the
1098 * current room is supplied, make an effort to go there.
1100 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1101 serv_printf("GOTO %s", c_roomname);
1102 serv_getln(buf, sizeof buf);
1103 if (buf[0] == '2') {
1104 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1109 * If there are instant messages waiting, retrieve them for display.
1111 check_for_instant_messages();
1113 if (!strcasecmp(action, "image")) {
1117 * All functions handled below this point ... make sure we log in
1118 * before doing anything else!
1120 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1122 } else if (!WC->logged_in) {
1123 display_login(NULL);
1127 * Various commands...
1130 else if (!strcasecmp(action, "do_welcome")) {
1132 } else if (!strcasecmp(action, "blank")) {
1134 } else if (!strcasecmp(action, "do_template")) {
1136 } else if (!strcasecmp(action, "display_aide_menu")) {
1137 display_aide_menu();
1138 } else if (!strcasecmp(action, "display_main_menu")) {
1139 display_main_menu();
1140 } else if (!strcasecmp(action, "who")) {
1142 } else if (!strcasecmp(action, "who_inner_html")) {
1144 } else if (!strcasecmp(action, "knrooms")) {
1146 } else if (!strcasecmp(action, "gotonext")) {
1149 } else if (!strcasecmp(action, "skip")) {
1151 } else if (!strcasecmp(action, "ungoto")) {
1153 } else if (!strcasecmp(action, "dotgoto")) {
1154 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1157 smart_goto(bstr("room"));
1158 } else if (!strcasecmp(action, "dotskip")) {
1159 smart_goto(bstr("room"));
1160 } else if (!strcasecmp(action, "termquit")) {
1162 } else if (!strcasecmp(action, "readnew")) {
1163 readloop("readnew");
1164 } else if (!strcasecmp(action, "readold")) {
1165 readloop("readold");
1166 } else if (!strcasecmp(action, "readfwd")) {
1167 readloop("readfwd");
1168 } else if (!strcasecmp(action, "headers")) {
1169 readloop("headers");
1170 } else if (!strcasecmp(action, "msg")) {
1172 } else if (!strcasecmp(action, "display_enter")) {
1174 } else if (!strcasecmp(action, "post")) {
1176 } else if (!strcasecmp(action, "move_msg")) {
1178 } else if (!strcasecmp(action, "delete_msg")) {
1180 } else if (!strcasecmp(action, "userlist")) {
1182 } else if (!strcasecmp(action, "showuser")) {
1184 } else if (!strcasecmp(action, "display_page")) {
1186 } else if (!strcasecmp(action, "page_user")) {
1188 } else if (!strcasecmp(action, "chat")) {
1190 } else if (!strcasecmp(action, "display_private")) {
1191 display_private("", 0);
1192 } else if (!strcasecmp(action, "goto_private")) {
1194 } else if (!strcasecmp(action, "zapped_list")) {
1196 } else if (!strcasecmp(action, "display_zap")) {
1198 } else if (!strcasecmp(action, "zap")) {
1200 } else if (!strcasecmp(action, "display_entroom")) {
1202 } else if (!strcasecmp(action, "entroom")) {
1204 } else if (!strcasecmp(action, "display_whok")) {
1206 } else if (!strcasecmp(action, "do_invt_kick")) {
1208 } else if (!strcasecmp(action, "display_editroom")) {
1210 } else if (!strcasecmp(action, "netedit")) {
1212 } else if (!strcasecmp(action, "editroom")) {
1214 } else if (!strcasecmp(action, "display_editinfo")) {
1215 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1216 } else if (!strcasecmp(action, "editinfo")) {
1217 save_edit(_("Room info"), "EINF 1", 1);
1218 } else if (!strcasecmp(action, "display_editbio")) {
1219 sprintf(buf, "RBIO %s", WC->wc_username);
1220 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1221 } else if (!strcasecmp(action, "editbio")) {
1222 save_edit(_("Your bio"), "EBIO", 0);
1223 } else if (!strcasecmp(action, "confirm_move_msg")) {
1225 } else if (!strcasecmp(action, "delete_room")) {
1227 } else if (!strcasecmp(action, "validate")) {
1229 } else if (!strcasecmp(action, "display_editpic")) {
1230 display_graphics_upload(_("your photo"),
1233 } else if (!strcasecmp(action, "editpic")) {
1234 do_graphics_upload("UIMG 1|_userpic_");
1235 } else if (!strcasecmp(action, "display_editroompic")) {
1236 display_graphics_upload(_("the icon for this room"),
1239 } else if (!strcasecmp(action, "editroompic")) {
1240 do_graphics_upload("UIMG 1|_roompic_");
1241 } else if (!strcasecmp(action, "delete_floor")) {
1243 } else if (!strcasecmp(action, "rename_floor")) {
1245 } else if (!strcasecmp(action, "create_floor")) {
1247 } else if (!strcasecmp(action, "display_editfloorpic")) {
1248 sprintf(buf, "UIMG 0|_floorpic_|%s",
1249 bstr("which_floor"));
1250 display_graphics_upload(_("the icon for this floor"),
1253 } else if (!strcasecmp(action, "editfloorpic")) {
1254 sprintf(buf, "UIMG 1|_floorpic_|%s",
1255 bstr("which_floor"));
1256 do_graphics_upload(buf);
1257 } else if (!strcasecmp(action, "display_reg")) {
1259 } else if (!strcasecmp(action, "display_changepw")) {
1261 } else if (!strcasecmp(action, "changepw")) {
1263 } else if (!strcasecmp(action, "display_edit_node")) {
1264 display_edit_node();
1265 } else if (!strcasecmp(action, "edit_node")) {
1267 } else if (!strcasecmp(action, "display_netconf")) {
1269 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1270 display_confirm_delete_node();
1271 } else if (!strcasecmp(action, "delete_node")) {
1273 } else if (!strcasecmp(action, "display_add_node")) {
1275 } else if (!strcasecmp(action, "add_node")) {
1277 } else if (!strcasecmp(action, "terminate_session")) {
1279 terminate_session();
1280 } else if (!strcasecmp(action, "edit_me")) {
1282 } else if (!strcasecmp(action, "display_siteconfig")) {
1283 display_siteconfig();
1284 } else if (!strcasecmp(action, "chat_recv")) {
1286 } else if (!strcasecmp(action, "chat_send")) {
1288 } else if (!strcasecmp(action, "siteconfig")) {
1290 } else if (!strcasecmp(action, "display_generic")) {
1292 } else if (!strcasecmp(action, "do_generic")) {
1294 } else if (!strcasecmp(action, "display_menubar")) {
1296 } else if (!strcasecmp(action, "output_mimepart")) {
1298 } else if (!strcasecmp(action, "edit_vcard")) {
1300 } else if (!strcasecmp(action, "submit_vcard")) {
1302 } else if (!strcasecmp(action, "select_user_to_edit")) {
1303 select_user_to_edit(NULL, NULL);
1304 } else if (!strcasecmp(action, "display_edituser")) {
1305 display_edituser(NULL, 0);
1306 } else if (!strcasecmp(action, "edituser")) {
1308 } else if (!strcasecmp(action, "create_user")) {
1310 } else if (!strcasecmp(action, "changeview")) {
1312 } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1314 } else if (!strcasecmp(action, "change_start_page")) {
1315 change_start_page();
1316 } else if (!strcasecmp(action, "display_floorconfig")) {
1317 display_floorconfig(NULL);
1318 } else if (!strcasecmp(action, "toggle_self_service")) {
1319 toggle_self_service();
1320 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1321 } else if (!strcasecmp(action, "display_edit_task")) {
1322 display_edit_task();
1323 } else if (!strcasecmp(action, "save_task")) {
1325 } else if (!strcasecmp(action, "display_edit_event")) {
1326 display_edit_event();
1327 } else if (!strcasecmp(action, "save_event")) {
1329 } else if (!strcasecmp(action, "respond_to_request")) {
1330 respond_to_request();
1331 } else if (!strcasecmp(action, "handle_rsvp")) {
1334 } else if (!strcasecmp(action, "summary")) {
1336 } else if (!strcasecmp(action, "iconbar")) {
1338 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1339 display_customize_iconbar();
1340 } else if (!strcasecmp(action, "commit_iconbar")) {
1342 } else if (!strcasecmp(action, "set_room_policy")) {
1344 } else if (!strcasecmp(action, "display_inetconf")) {
1346 } else if (!strcasecmp(action, "save_inetconf")) {
1348 } else if (!strcasecmp(action, "setup_wizard")) {
1350 } else if (!strcasecmp(action, "display_preferences")) {
1351 display_preferences();
1352 } else if (!strcasecmp(action, "set_preferences")) {
1354 } else if (!strcasecmp(action, "diagnostics")) {
1355 output_headers(1, 1, 1, 0, 0, 0, 0);
1356 wprintf("Session: %d<hr />\n", WC->wc_session);
1357 wprintf("Command: <br /><PRE>\n");
1359 wprintf("</PRE><hr />\n");
1360 wprintf("Variables: <br /><PRE>\n");
1362 wprintf("</PRE><hr />\n");
1365 /* When all else fais, display the main menu. */
1367 display_main_menu();
1372 if (content != NULL) {
1377 if (WC->upload_length > 0) {
1379 WC->upload_length = 0;