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')
307 else if (strbuf[a] == '\'')
308 strcat(target, "'");
310 strncat(target, &strbuf[a], 1);
315 void msgescputs(char *strbuf) {
318 if (strbuf == NULL) return;
319 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
320 msgesc(outbuf, strbuf);
321 wprintf("%s", outbuf);
329 * Output all that important stuff that the browser will want to see
331 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
332 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
334 int do_room_banner, /* 0=no, 1=yes, */
335 /* 2 = I'm going to embed my own, so don't open the */
336 /* <div id="content"> either. */
338 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
339 int refresh30, /* 1 = automatically refresh page every 30 seconds */
340 int suppress_check, /* 1 = suppress check for instant messages */
341 int cache /* 1 = allow browser to cache this page */
346 wprintf("HTTP/1.0 200 OK\n");
347 httpdate(httpnow, time(NULL));
349 if (do_httpheaders) {
350 wprintf("Content-type: text/html; charset=utf-8\r\n"
352 "Connection: close\r\n",
353 SERVER, serv_info.serv_software
358 wprintf("Pragma: public\r\n"
359 "Cache-Control: max-age=3600, must-revalidate\r\n"
360 "Last-modified: %s\r\n",
365 wprintf("Pragma: no-cache\r\n"
366 "Cache-Control: no-store\r\n"
370 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
371 WC->wc_password, WC->wc_roomname);
374 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
376 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
377 if (server_cookie != NULL) {
378 wprintf("%s\n", server_cookie);
387 svprintf("REFRESHTAG", WCS_STRING, "%s",
388 "<meta http-equiv=\"refresh\" content=\"30\" />\n");
391 svprintf("REFRESHTAG", WCS_STRING, "%s",
392 "<meta http-equiv=\"refresh\" content=\"500363689;\" />\n");
401 if (WC->HaveInstantMessages) {
402 wprintf("<div id=\"page_popup\">\n");
406 if (strlen(WC->ImportantMessage) > 0) {
407 wprintf("<div id=\"important_message\">\n");
408 wprintf("<SPAN CLASS=\"imsg\">"
409 "%s</SPAN><br />\n", WC->ImportantMessage);
411 wprintf("<script type=\"text/javascript\">\n"
412 " setTimeout('hide_imsg_popup()', 3000); \n"
414 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
416 if ( (WC->logged_in) && (!unset_cookies) ) {
417 wprintf("<div id=\"iconbar\">");
421 if (do_room_banner == 1) {
422 wprintf("<div id=\"banner\">\n");
423 embed_room_banner(NULL, navbar_default);
428 if (do_room_banner == 1) {
429 wprintf("<div id=\"content\">\n");
435 * Generic function to do an HTTP redirect. Easy and fun.
437 void http_redirect(char *whichpage) {
438 wprintf("HTTP/1.0 302 Moved Temporarily\n");
439 wprintf("Location: %s\r\n", whichpage);
440 wprintf("URI: %s\r\n", whichpage);
441 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
442 wprintf("<html><body>");
443 wprintf("Go <A HREF=\"%s\">here</A>.", whichpage);
444 wprintf("</body></html>\n");
449 void check_for_instant_messages()
454 serv_getln(buf, sizeof buf);
455 if (buf[3] == '*') WC->HaveInstantMessages = 1;
462 * Output a piece of content to the web browser
464 void http_transmit_thing(char *thing, size_t length, char *content_type,
467 output_headers(0, 0, 0, 0, 0, 0, is_static);
469 wprintf("Content-type: %s\r\n"
471 "Connection: close\r\n",
476 /* If we can send the data out compressed, please do so. */
478 char *compressed_data = NULL;
479 uLongf compressed_len;
481 compressed_len = (uLongf) ((length * 101) / 100) + 100;
482 compressed_data = malloc(compressed_len);
484 if (compress_gzip((Bytef *) compressed_data,
487 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
488 wprintf("Content-encoding: gzip\r\n"
489 "Content-length: %ld\r\n"
491 (long) compressed_len
493 client_write(compressed_data, (size_t)compressed_len);
494 free(compressed_data);
500 /* No compression ... just send it out as-is */
501 wprintf("Content-length: %ld\r\n"
505 client_write(thing, (size_t)length);
511 void output_static(char *what)
518 char content_type[128];
520 sprintf(buf, "static/%s", what);
521 fp = fopen(buf, "rb");
523 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
524 wprintf("Content-Type: text/plain\r\n");
526 wprintf("Cannot open %s: %s\n", what, strerror(errno));
528 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
529 safestrncpy(content_type, "image/gif", sizeof content_type);
530 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
531 safestrncpy(content_type, "text/plain", sizeof content_type);
532 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
533 safestrncpy(content_type, "text/css", sizeof content_type);
534 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
535 safestrncpy(content_type, "image/jpeg", sizeof content_type);
536 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
537 safestrncpy(content_type, "image/png", sizeof content_type);
538 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
539 safestrncpy(content_type, "image/x-icon", sizeof content_type);
540 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
541 safestrncpy(content_type, "text/html", sizeof content_type);
542 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
543 safestrncpy(content_type, "text/html", sizeof content_type);
544 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
545 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
546 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
547 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
548 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
549 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
550 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
551 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
552 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
553 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
554 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
555 safestrncpy(content_type, "text/javascript", sizeof content_type);
557 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
559 fstat(fileno(fp), &statbuf);
560 bytes = statbuf.st_size;
561 bigbuffer = malloc(bytes + 2);
562 fread(bigbuffer, bytes, 1, fp);
565 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
568 if (!strcasecmp(bstr("force_close_session"), "yes")) {
569 end_webcit_session();
575 * When the browser requests an image file from the Citadel server,
576 * this function is called to transmit it.
581 char *xferbuf = NULL;
584 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
585 serv_getln(buf, sizeof buf);
587 bytes = extract_long(&buf[4], 0);
588 xferbuf = malloc(bytes + 2);
590 /* Read it from the server */
591 read_server_binary(xferbuf, bytes);
593 serv_getln(buf, sizeof buf);
595 /* Write it to the browser */
596 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
600 /* Instead of an ugly 404, send a 1x1 transparent GIF
601 * when there's no such image on the server.
603 output_static("blank.gif");
611 * Generic function to output an arbitrary MIME part from an arbitrary
612 * message number on the server.
614 void output_mimepart()
618 char content_type[SIZ];
619 char *content = NULL;
621 serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
622 serv_getln(buf, sizeof buf);
624 bytes = extract_long(&buf[4], 0);
625 content = malloc(bytes + 2);
626 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
627 output_headers(0, 0, 0, 0, 0, 0, 0);
628 read_server_binary(content, bytes);
630 serv_getln(buf, sizeof buf);
631 http_transmit_thing(content, bytes, content_type, 0);
634 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
635 output_headers(0, 0, 0, 0, 0, 0, 0);
636 wprintf("Content-Type: text/plain\r\n");
638 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
645 * Read any MIME part of a message, from the server, into memory.
647 char *load_mimepart(long msgnum, char *partnum)
651 char content_type[SIZ];
654 serv_printf("OPNA %ld|%s", msgnum, partnum);
655 serv_getln(buf, sizeof buf);
657 bytes = extract_long(&buf[4], 0);
658 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
660 content = malloc(bytes + 2);
661 read_server_binary(content, bytes);
664 serv_getln(buf, sizeof buf);
665 content[bytes] = 0; /* null terminate for good measure */
676 * Convenience functions to display a page containing only a string
678 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
680 wprintf("HTTP/1.0 200 OK\n");
681 output_headers(1, 1, 2, 0, 0, 0, 0);
682 wprintf("<div id=\"banner\">\n");
683 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
684 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
685 wprintf("</TD></TR></TABLE>\n");
686 wprintf("</div>\n<div id=\"content\">\n");
687 escputs(messagetext);
695 * Display a blank page.
697 void blank_page(void) {
698 output_headers(1, 1, 0, 0, 1, 0, 0);
704 * A template has been requested
706 void url_do_template(void) {
707 do_template(bstr("template"));
713 * Offer to make any page the user's "start page."
715 void offer_start_page(void) {
716 wprintf("<A HREF=\"/change_start_page?startpage=");
717 urlescputs(WC->this_page);
718 wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
719 wprintf(_("Make this my start page"));
720 wprintf("</FONT></A>");
721 wprintf("<br/><a href=\"/rss?room=");
722 urlescputs(WC->wc_roomname);
723 wprintf("\" title=\"RSS 2.0 feed for ");
724 escputs(WC->wc_roomname);
725 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"/static/xml_button.gif\"/></a>\n");
730 * Change the user's start page
732 void change_start_page(void) {
734 if (bstr("startpage") == NULL) {
735 safestrncpy(WC->ImportantMessage,
736 _("You no longer have a start page selected."),
737 sizeof WC->ImportantMessage);
742 set_preference("startpage", bstr("startpage"), 1);
744 output_headers(1, 1, 0, 0, 0, 0, 0);
745 do_template("newstartpage");
752 void display_success(char *successmessage)
754 convenience_page("007700", "OK", successmessage);
758 /* Authorization required page */
759 /* This is probably temporary and should be revisited */
760 void authorization_required(const char *message)
762 wprintf("HTTP/1.0 401 Authorization Required\r\n");
763 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
764 wprintf("Content-Type: text/html\r\n\r\n");
766 wprintf(_("Authorization Required"));
767 wprintf("</h1>\r\n");
768 wprintf(_("The resource you requested requires a valid username and password. "
769 "You could not be logged 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)
827 int ContentLength = 0;
829 char ContentType[512];
830 char *content = NULL;
831 char *content_end = NULL;
832 struct httprequest *hptr;
833 char browser_host[SIZ];
834 char user_agent[SIZ];
837 /* We stuff these with the values coming from the client cookies,
838 * so we can use them to reconnect a timed out session if we have to.
840 char c_username[SIZ];
841 char c_password[SIZ];
842 char c_roomname[SIZ];
843 char c_httpauth_string[SIZ];
844 char c_httpauth_user[SIZ];
845 char c_httpauth_pass[SIZ];
848 safestrncpy(c_username, "", sizeof c_username);
849 safestrncpy(c_password, "", sizeof c_password);
850 safestrncpy(c_roomname, "", sizeof c_roomname);
851 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
852 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
853 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
855 WC->upload_length = 0;
862 if (hptr == NULL) return;
864 safestrncpy(cmd, hptr->line, sizeof cmd);
866 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
868 /* Figure out the action */
869 extract_token(action, cmd, 1, '/', sizeof action);
870 if (strstr(action, "?")) *strstr(action, "?") = 0;
871 if (strstr(action, "&")) *strstr(action, "&") = 0;
872 if (strstr(action, " ")) *strstr(action, " ") = 0;
874 extract_token(arg1, cmd, 2, '/', sizeof arg1);
875 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
876 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
877 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
879 extract_token(arg2, cmd, 3, '/', sizeof arg2);
880 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
881 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
882 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
884 extract_token(arg3, cmd, 4, '/', sizeof arg3);
885 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
886 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
887 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
889 while (hptr != NULL) {
890 safestrncpy(buf, hptr->line, sizeof buf);
893 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
894 safestrncpy(cookie, &buf[15], sizeof cookie);
895 cookie_to_stuff(cookie, NULL,
896 c_username, sizeof c_username,
897 c_password, sizeof c_password,
898 c_roomname, sizeof c_roomname);
900 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
901 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
902 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
903 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
905 else if (!strncasecmp(buf, "Content-length: ", 16)) {
906 ContentLength = atoi(&buf[16]);
908 else if (!strncasecmp(buf, "Content-type: ", 14)) {
909 safestrncpy(ContentType, &buf[14], sizeof ContentType);
911 else if (!strncasecmp(buf, "User-agent: ", 12)) {
912 safestrncpy(user_agent, &buf[12], sizeof user_agent);
914 else if (!strncasecmp(buf, "Host: ", 6)) {
915 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
917 /* Only WAP gateways explicitly name this content-type */
918 else if (strstr(buf, "text/vnd.wap.wml")) {
923 if (ContentLength > 0) {
924 content = malloc(ContentLength + SIZ);
925 memset(content, 0, ContentLength + SIZ);
926 sprintf(content, "Content-type: %s\n"
927 "Content-length: %d\n\n",
928 ContentType, ContentLength);
929 body_start = strlen(content);
931 /* Read the entire input data at once. */
932 client_read(WC->http_sock, &content[BytesRead+body_start],
935 if (!strncasecmp(ContentType,
936 "application/x-www-form-urlencoded", 33)) {
937 addurls(&content[body_start]);
938 } else if (!strncasecmp(ContentType, "multipart", 9)) {
939 content_end = content + ContentLength + body_start;
940 mime_parser(content, content_end, *upload_handler,
941 NULL, NULL, NULL, 0);
947 /* make a note of where we are in case the user wants to save it */
948 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
949 remove_token(WC->this_page, 2, ' ');
950 remove_token(WC->this_page, 0, ' ');
952 /* If there are variables in the URL, we must grab them now */
953 for (a = 0; a < strlen(cmd); ++a) {
954 if ((cmd[a] == '?') || (cmd[a] == '&')) {
955 for (b = a; b < strlen(cmd); ++b)
958 addurls(&cmd[a + 1]);
964 /* Static content can be sent without connecting to Citadel. */
965 if (!strcasecmp(action, "static")) {
966 safestrncpy(buf, arg1, sizeof buf);
967 for (a = 0; a < strlen(buf); ++a)
971 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
975 * If we're not connected to a Citadel server, try to hook up the
978 if (!WC->connected) {
979 if (!strcasecmp(ctdlhost, "uds")) {
980 /* unix domain socket */
981 sprintf(buf, "%s/citadel.socket", ctdlport);
982 WC->serv_sock = uds_connectsock(buf);
986 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
989 if (WC->serv_sock < 0) {
991 goto SKIP_ALL_THIS_CRAP;
995 serv_getln(buf, sizeof buf); /* get the server welcome message */
996 locate_host(browser_host, WC->http_sock);
997 get_serv_info(browser_host, user_agent);
998 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
999 wprintf(_("You are connected to a Citadel "
1000 "server running Citadel %d.%02d. \n"
1001 "In order to run this version of WebCit "
1002 "you must also have Citadel %d.%02d or"
1004 serv_info.serv_rev_level / 100,
1005 serv_info.serv_rev_level % 100,
1006 MINIMUM_CIT_VERSION / 100,
1007 MINIMUM_CIT_VERSION % 100
1009 end_webcit_session();
1010 goto SKIP_ALL_THIS_CRAP;
1016 * Functions which can be performed without logging in
1018 if (!strcasecmp(action, "listsub")) {
1020 goto SKIP_ALL_THIS_CRAP;
1022 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1023 if (!strcasecmp(action, "freebusy")) {
1025 goto SKIP_ALL_THIS_CRAP;
1030 * If we're not logged in, but we have HTTP Authentication data,
1031 * try logging in to Citadel using that.
1033 if ((!WC->logged_in)
1034 && (strlen(c_httpauth_user) > 0)
1035 && (strlen(c_httpauth_pass) > 0)) {
1036 serv_printf("USER %s", c_httpauth_user);
1037 serv_getln(buf, sizeof buf);
1038 if (buf[0] == '3') {
1039 serv_printf("PASS %s", c_httpauth_pass);
1040 serv_getln(buf, sizeof buf);
1041 if (buf[0] == '2') {
1042 become_logged_in(c_httpauth_user,
1043 c_httpauth_pass, buf);
1044 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1045 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1047 /* Should only display when password is wrong */
1048 authorization_required(&buf[4]);
1049 goto SKIP_ALL_THIS_CRAP;
1054 /* This needs to run early */
1055 if (!strcasecmp(action, "rss")) {
1056 display_rss(bstr("room"));
1057 goto SKIP_ALL_THIS_CRAP;
1061 * The GroupDAV stuff relies on HTTP authentication instead of
1062 * our session's authentication.
1064 if (!strncasecmp(action, "groupdav", 8)) {
1065 groupdav_main(req, ContentType, /* do GroupDAV methods */
1066 ContentLength, content+body_start);
1067 if (!WC->logged_in) {
1068 WC->killthis = 1; /* If not logged in, don't */
1069 } /* keep the session active */
1070 goto SKIP_ALL_THIS_CRAP;
1075 * Automatically send requests with any method other than GET or
1076 * POST to the GroupDAV code as well.
1078 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1079 groupdav_main(req, ContentType, /* do GroupDAV methods */
1080 ContentLength, content+body_start);
1081 if (!WC->logged_in) {
1082 WC->killthis = 1; /* If not logged in, don't */
1083 } /* keep the session active */
1084 goto SKIP_ALL_THIS_CRAP;
1088 * If we're not logged in, but we have username and password cookies
1089 * supplied by the browser, try using them to log in.
1091 if ((!WC->logged_in)
1092 && (strlen(c_username) > 0)
1093 && (strlen(c_password) > 0)) {
1094 serv_printf("USER %s", c_username);
1095 serv_getln(buf, sizeof buf);
1096 if (buf[0] == '3') {
1097 serv_printf("PASS %s", c_password);
1098 serv_getln(buf, sizeof buf);
1099 if (buf[0] == '2') {
1100 become_logged_in(c_username, c_password, buf);
1105 * If we don't have a current room, but a cookie specifying the
1106 * current room is supplied, make an effort to go there.
1108 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1109 serv_printf("GOTO %s", c_roomname);
1110 serv_getln(buf, sizeof buf);
1111 if (buf[0] == '2') {
1112 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1117 * If there are instant messages waiting, retrieve them for display.
1119 check_for_instant_messages();
1121 if (!strcasecmp(action, "image")) {
1125 * All functions handled below this point ... make sure we log in
1126 * before doing anything else!
1128 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1130 } else if (!WC->logged_in) {
1131 display_login(NULL);
1135 * Various commands...
1138 else if (!strcasecmp(action, "do_welcome")) {
1140 } else if (!strcasecmp(action, "blank")) {
1142 } else if (!strcasecmp(action, "do_template")) {
1144 } else if (!strcasecmp(action, "display_aide_menu")) {
1145 display_aide_menu();
1146 } else if (!strcasecmp(action, "display_main_menu")) {
1147 display_main_menu();
1148 } else if (!strcasecmp(action, "who")) {
1150 } else if (!strcasecmp(action, "who_inner_html")) {
1152 } else if (!strcasecmp(action, "knrooms")) {
1154 } else if (!strcasecmp(action, "gotonext")) {
1157 } else if (!strcasecmp(action, "skip")) {
1159 } else if (!strcasecmp(action, "ungoto")) {
1161 } else if (!strcasecmp(action, "dotgoto")) {
1162 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1165 smart_goto(bstr("room"));
1166 } else if (!strcasecmp(action, "dotskip")) {
1167 smart_goto(bstr("room"));
1168 } else if (!strcasecmp(action, "termquit")) {
1170 } else if (!strcasecmp(action, "readnew")) {
1171 readloop("readnew");
1172 } else if (!strcasecmp(action, "readold")) {
1173 readloop("readold");
1174 } else if (!strcasecmp(action, "readfwd")) {
1175 readloop("readfwd");
1176 } else if (!strcasecmp(action, "headers")) {
1177 readloop("headers");
1178 } else if (!strcasecmp(action, "msg")) {
1180 } else if (!strcasecmp(action, "display_enter")) {
1182 } else if (!strcasecmp(action, "post")) {
1184 } else if (!strcasecmp(action, "move_msg")) {
1186 } else if (!strcasecmp(action, "delete_msg")) {
1188 } else if (!strcasecmp(action, "userlist")) {
1190 } else if (!strcasecmp(action, "showuser")) {
1192 } else if (!strcasecmp(action, "display_page")) {
1194 } else if (!strcasecmp(action, "page_user")) {
1196 } else if (!strcasecmp(action, "chat")) {
1198 } else if (!strcasecmp(action, "display_private")) {
1199 display_private("", 0);
1200 } else if (!strcasecmp(action, "goto_private")) {
1202 } else if (!strcasecmp(action, "zapped_list")) {
1204 } else if (!strcasecmp(action, "display_zap")) {
1206 } else if (!strcasecmp(action, "zap")) {
1208 } else if (!strcasecmp(action, "display_entroom")) {
1210 } else if (!strcasecmp(action, "entroom")) {
1212 } else if (!strcasecmp(action, "display_whok")) {
1214 } else if (!strcasecmp(action, "do_invt_kick")) {
1216 } else if (!strcasecmp(action, "display_editroom")) {
1218 } else if (!strcasecmp(action, "netedit")) {
1220 } else if (!strcasecmp(action, "editroom")) {
1222 } else if (!strcasecmp(action, "display_editinfo")) {
1223 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1224 } else if (!strcasecmp(action, "editinfo")) {
1225 save_edit(_("Room info"), "EINF 1", 1);
1226 } else if (!strcasecmp(action, "display_editbio")) {
1227 sprintf(buf, "RBIO %s", WC->wc_username);
1228 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1229 } else if (!strcasecmp(action, "editbio")) {
1230 save_edit(_("Your bio"), "EBIO", 0);
1231 } else if (!strcasecmp(action, "confirm_move_msg")) {
1233 } else if (!strcasecmp(action, "delete_room")) {
1235 } else if (!strcasecmp(action, "validate")) {
1237 } else if (!strcasecmp(action, "display_editpic")) {
1238 display_graphics_upload(_("your photo"),
1241 } else if (!strcasecmp(action, "editpic")) {
1242 do_graphics_upload("UIMG 1|_userpic_");
1243 } else if (!strcasecmp(action, "display_editroompic")) {
1244 display_graphics_upload(_("the icon for this room"),
1247 } else if (!strcasecmp(action, "editroompic")) {
1248 do_graphics_upload("UIMG 1|_roompic_");
1249 } else if (!strcasecmp(action, "delete_floor")) {
1251 } else if (!strcasecmp(action, "rename_floor")) {
1253 } else if (!strcasecmp(action, "create_floor")) {
1255 } else if (!strcasecmp(action, "display_editfloorpic")) {
1256 sprintf(buf, "UIMG 0|_floorpic_|%s",
1257 bstr("which_floor"));
1258 display_graphics_upload(_("the icon for this floor"),
1261 } else if (!strcasecmp(action, "editfloorpic")) {
1262 sprintf(buf, "UIMG 1|_floorpic_|%s",
1263 bstr("which_floor"));
1264 do_graphics_upload(buf);
1265 } else if (!strcasecmp(action, "display_reg")) {
1267 } else if (!strcasecmp(action, "display_changepw")) {
1269 } else if (!strcasecmp(action, "changepw")) {
1271 } else if (!strcasecmp(action, "display_edit_node")) {
1272 display_edit_node();
1273 } else if (!strcasecmp(action, "edit_node")) {
1275 } else if (!strcasecmp(action, "display_netconf")) {
1277 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1278 display_confirm_delete_node();
1279 } else if (!strcasecmp(action, "delete_node")) {
1281 } else if (!strcasecmp(action, "display_add_node")) {
1283 } else if (!strcasecmp(action, "add_node")) {
1285 } else if (!strcasecmp(action, "terminate_session")) {
1287 terminate_session();
1288 } else if (!strcasecmp(action, "edit_me")) {
1290 } else if (!strcasecmp(action, "display_siteconfig")) {
1291 display_siteconfig();
1292 } else if (!strcasecmp(action, "chat_recv")) {
1294 } else if (!strcasecmp(action, "chat_send")) {
1296 } else if (!strcasecmp(action, "siteconfig")) {
1298 } else if (!strcasecmp(action, "display_generic")) {
1300 } else if (!strcasecmp(action, "do_generic")) {
1302 } else if (!strcasecmp(action, "display_menubar")) {
1304 } else if (!strcasecmp(action, "output_mimepart")) {
1306 } else if (!strcasecmp(action, "edit_vcard")) {
1308 } else if (!strcasecmp(action, "submit_vcard")) {
1310 } else if (!strcasecmp(action, "select_user_to_edit")) {
1311 select_user_to_edit(NULL, NULL);
1312 } else if (!strcasecmp(action, "display_edituser")) {
1313 display_edituser(NULL, 0);
1314 } else if (!strcasecmp(action, "edituser")) {
1316 } else if (!strcasecmp(action, "create_user")) {
1318 } else if (!strcasecmp(action, "changeview")) {
1320 } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1322 } else if (!strcasecmp(action, "change_start_page")) {
1323 change_start_page();
1324 } else if (!strcasecmp(action, "display_floorconfig")) {
1325 display_floorconfig(NULL);
1326 } else if (!strcasecmp(action, "toggle_self_service")) {
1327 toggle_self_service();
1328 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1329 } else if (!strcasecmp(action, "display_edit_task")) {
1330 display_edit_task();
1331 } else if (!strcasecmp(action, "save_task")) {
1333 } else if (!strcasecmp(action, "display_edit_event")) {
1334 display_edit_event();
1335 } else if (!strcasecmp(action, "save_event")) {
1337 } else if (!strcasecmp(action, "respond_to_request")) {
1338 respond_to_request();
1339 } else if (!strcasecmp(action, "handle_rsvp")) {
1342 } else if (!strcasecmp(action, "summary")) {
1344 } else if (!strcasecmp(action, "iconbar")) {
1346 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1347 display_customize_iconbar();
1348 } else if (!strcasecmp(action, "commit_iconbar")) {
1350 } else if (!strcasecmp(action, "set_room_policy")) {
1352 } else if (!strcasecmp(action, "display_inetconf")) {
1354 } else if (!strcasecmp(action, "save_inetconf")) {
1356 } else if (!strcasecmp(action, "setup_wizard")) {
1358 } else if (!strcasecmp(action, "display_preferences")) {
1359 display_preferences();
1360 } else if (!strcasecmp(action, "set_preferences")) {
1362 } else if (!strcasecmp(action, "recp_autocomplete")) {
1363 recp_autocomplete(bstr("recp"));
1364 } else if (!strcasecmp(action, "cc_autocomplete")) {
1365 recp_autocomplete(bstr("cc"));
1366 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1367 recp_autocomplete(bstr("bcc"));
1368 } else if (!strcasecmp(action, "diagnostics")) {
1369 output_headers(1, 1, 1, 0, 0, 0, 0);
1370 wprintf("Session: %d<hr />\n", WC->wc_session);
1371 wprintf("Command: <br /><PRE>\n");
1373 wprintf("</PRE><hr />\n");
1374 wprintf("Variables: <br /><PRE>\n");
1376 wprintf("</PRE><hr />\n");
1380 /* When all else fais, display the main menu. */
1382 display_main_menu();
1387 if (content != NULL) {
1392 if (WC->upload_length > 0) {
1394 WC->upload_length = 0;