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.
8 #define SHOW_ME_VAPPEND_PRINTF
13 #include "webserver.h"
17 * String to unset the cookie.
18 * Any date "in the past" will work, so I chose my birthday, right down to
19 * the exact minute. :)
21 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
23 HashList *HandlerHash = NULL;
25 void WebcitAddUrlHandler(const char * UrlString,
30 WebcitHandler *NewHandler;
32 if (HandlerHash == NULL)
33 HandlerHash = NewHash(1, NULL);
35 NewHandler = (WebcitHandler*) malloc(sizeof(WebcitHandler));
37 NewHandler->Flags = Flags;
38 Put(HandlerHash, UrlString, UrlSLen, NewHandler, NULL);
43 * web-printing funcion. uses our vsnprintf wrapper
45 void wprintf(const char *format,...)
50 if (WCC->WBuf == NULL)
51 WCC->WBuf = NewStrBuf();
53 va_start(arg_ptr, format);
54 StrBufVAppendPrintf(WCC->WBuf, format, arg_ptr);
59 * http-header-printing funcion. uses our vsnprintf wrapper
61 void hprintf(const char *format,...)
66 va_start(arg_ptr, format);
67 StrBufVAppendPrintf(WCC->HBuf, format, arg_ptr);
74 * wrap up an HTTP session, closes tags, etc.
76 * print_standard_html_footer should be set to:
77 * 0 - to transmit only,
78 * nonzero - to append the closing tags
80 void wDumpContent(int print_standard_html_footer)
82 if (print_standard_html_footer) {
83 wprintf("</div> <!-- end of 'content' div -->\n");
84 do_template("trailing", NULL);
87 /* If we've been saving it all up for one big output burst,
88 * go ahead and do that now.
97 * Output HTTP headers and leading HTML for a page
99 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
100 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
102 int do_room_banner, /* 0=no, 1=yes,
103 * 2 = I'm going to embed my own, so don't open the
104 * <div id="content"> either.
107 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
108 int suppress_check, /* 1 = suppress check for instant messages */
109 int cache /* 1 = allow browser to cache this page */
114 hprintf("HTTP/1.1 200 OK\n");
115 http_datestring(httpnow, sizeof httpnow, time(NULL));
117 if (do_httpheaders) {
118 hprintf("Content-type: text/html; charset=utf-8\r\n"
120 "Connection: close\r\n",
121 PACKAGE_STRING, serv_info.serv_software
126 char httpTomorow[128];
128 http_datestring(httpTomorow, sizeof httpTomorow,
129 time(NULL) + 60 * 60 * 24 * 2);
131 hprintf("Pragma: public\r\n"
132 "Cache-Control: max-age=3600, must-revalidate\r\n"
133 "Last-modified: %s\r\n"
140 hprintf("Pragma: no-cache\r\n"
141 "Cache-Control: no-store\r\n"
146 stuff_to_cookie(cookie, 1024, WC->wc_session, WC->wc_username,
147 WC->wc_password, WC->wc_roomname);
150 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
152 hprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
153 if (server_cookie != NULL) {
154 hprintf("%s\n", server_cookie);
160 do_template("head", NULL);
167 /* check for ImportantMessages (these display in a div overlaying the main screen) */
168 if (!IsEmptyStr(WC->ImportantMessage)) {
169 wprintf("<div id=\"important_message\">\n"
170 "<span class=\"imsg\">");
171 escputs(WC->ImportantMessage);
172 wprintf("</span><br />\n"
174 "<script type=\"text/javascript\">\n"
175 " setTimeout('hide_imsg_popup()', 5000); \n"
177 WC->ImportantMessage[0] = 0;
180 if ( (WC->logged_in) && (!unset_cookies) ) {
181 wprintf("<div id=\"iconbar\">");
182 do_selected_iconbar();
183 /** check for instant messages (these display in a new window) */
188 if (do_room_banner == 1) {
189 wprintf("<div id=\"banner\">\n");
190 embed_room_banner(NULL, navbar_default);
195 if (do_room_banner == 1) {
196 wprintf("<div id=\"content\">\n");
202 * Generic function to do an HTTP redirect. Easy and fun.
204 void http_redirect(const char *whichpage) {
205 hprintf("HTTP/1.1 302 Moved Temporarily\n");
206 hprintf("Location: %s\r\n", whichpage);
207 hprintf("URI: %s\r\n", whichpage);
208 hprintf("Content-type: text/html; charset=utf-8\r\n");
209 wprintf("<html><body>");
210 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
211 wprintf("</body></html>\n");
218 * Output a piece of content to the web browser using conformant HTTP and MIME semantics
220 void http_transmit_thing(const char *content_type,
224 lprintf(9, "http_transmit_thing(%s)%s\n",
226 (is_static ? " (static)" : "")
229 output_headers(0, 0, 0, 0, 0, is_static);
231 hprintf("Content-type: %s\r\n"
233 "Connection: close\r\n",
241 * print menu box like used in the floor view or admin interface.
242 * This function takes pair of strings as va_args,
243 * Title Title string of the box
244 * Class CSS Class for the box
245 * nLines How many string pairs should we print? (URL, UrlText)
246 * ... Pairs of URL Strings and their Names
248 void print_menu_box(char* Title, char *Class, int nLines, ...)
253 svput("BOXTITLE", WCS_STRING, Title);
254 do_template("beginboxx", NULL);
256 wprintf("<ul class=\"%s\">", Class);
258 va_start(arg_list, nLines);
259 for (i = 0; i < nLines; ++i)
261 wprintf("<li><a href=\"%s\">", va_arg(arg_list, char *));
262 wprintf((char *) va_arg(arg_list, char *));
263 wprintf("</a></li>\n");
267 wprintf("</a></li>\n");
271 do_template("endbox", NULL);
276 * dump out static pages from disk
278 void output_static(char *what)
284 const char *content_type;
288 fd = open(what, O_RDONLY);
290 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
291 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
292 hprintf("Content-Type: text/plain\r\n");
293 wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
297 content_type = GuessMimeByFilename(what, len);
299 if (fstat(fd, &statbuf) == -1) {
300 lprintf(9, "output_static('%s') -- FSTAT FAILED --\n", what);
301 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
302 hprintf("Content-Type: text/plain\r\n");
303 wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
309 bytes = statbuf.st_size;
311 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
313 if (fd > 0) close(fd);
314 lprintf(9, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
315 hprintf("HTTP/1.1 500 internal server error \r\n");
316 hprintf("Content-Type: text/plain\r\n");
324 lprintf(9, "output_static('%s') %s\n", what, content_type);
326 http_transmit_thing(content_type, 1);
328 if (yesbstr("force_close_session")) {
329 end_webcit_session();
335 * Convenience functions to display a page containing only a string
337 * titlebarcolor color of the titlebar of the frame
338 * titlebarmsg text to display in the title bar
339 * messagetext body of the box
341 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
343 hprintf("HTTP/1.1 200 OK\n");
344 output_headers(1, 1, 2, 0, 0, 0);
345 wprintf("<div id=\"banner\">\n");
346 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
347 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
348 wprintf("</td></tr></table>\n");
349 wprintf("</div>\n<div id=\"content\">\n");
350 escputs(messagetext);
358 * Display a blank page.
360 void blank_page(void) {
361 output_headers(1, 1, 0, 0, 0, 0);
367 * A template has been requested
369 void url_do_template(void) {
370 const StrBuf *Tmpl = sbstr("template");
372 output_headers(1, 0, 0, 0, 1, 0);
373 DoTemplate(ChrPtr(Tmpl), StrLength(Tmpl), NULL, NULL, 0);
380 * convenience function to indicate success
382 void display_success(char *successmessage)
384 convenience_page("007700", "OK", successmessage);
389 * Authorization required page
390 * This is probably temporary and should be revisited
392 void authorization_required(const char *message)
394 hprintf("HTTP/1.1 401 Authorization Required\r\n");
395 hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", serv_info.serv_humannode);
396 hprintf("Content-Type: text/html\r\n");
398 wprintf(_("Authorization Required"));
399 wprintf("</h1>\r\n");
400 wprintf(_("The resource you requested requires a valid username and password. "
401 "You could not be logged in: %s\n"), message);
407 * Convenience functions to wrap around asynchronous ajax responses
409 void begin_ajax_response(void) {
412 FlushStrBuf(WCC->HBuf);
413 output_headers(0, 0, 0, 0, 0, 0);
415 hprintf("Content-type: text/html; charset=UTF-8\r\n"
417 "Connection: close\r\n"
424 * print ajax response footer
426 void end_ajax_response(void) {
431 * Wraps a Citadel server command in an AJAX transaction.
433 void ajax_servcmd(void)
440 begin_ajax_response();
442 serv_printf("%s", bstr("g_cmd"));
443 serv_getln(buf, sizeof buf);
444 wprintf("%s\n", buf);
447 serv_printf("\n\n000");
449 if ((buf[0] == '1') || (buf[0] == '8')) {
450 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
451 wprintf("%s\n", gcontent);
456 text_to_server(bstr("g_input"));
462 serv_read(junk, len);
468 memset(junk, 0, len);
469 serv_write(junk, len);
476 * This is kind of an ugly hack, but this is the only place it can go.
477 * If the command was GEXP, then the instant messenger window must be
478 * running, so reset the "last_pager_check" watchdog timer so
479 * that page_popup() doesn't try to open it a second time.
481 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
482 WC->last_pager_check = time(NULL);
488 * Helper function for the asynchronous check to see if we need
489 * to open the instant messenger window.
491 void seconds_since_last_gexp(void)
495 if ( (time(NULL) - WC->last_pager_check) < 30) {
501 serv_getln(buf, sizeof buf);
512 * \brief Detects a 'mobile' user agent
514 int is_mobile_ua(char *user_agent) {
515 if (strstr(user_agent,"iPhone OS") != NULL) {
517 } else if (strstr(user_agent,"Windows CE") != NULL) {
519 } else if (strstr(user_agent,"SymbianOS") != NULL) {
521 } else if (strstr(user_agent, "Opera Mobi") != NULL) {
523 } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
524 // For some reason a new install of Opera 9.51beta decided to spoof.
532 * Entry point for WebCit transaction
534 void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method, StrBuf *ReadBuf)
536 const char *pch, *pchs, *pche;
543 int a, nBackDots, nEmpty;
544 int ContentLength = 0;
545 StrBuf *ContentType = NULL;
546 StrBuf *UrlLine = NULL;
547 StrBuf *content = NULL;
548 const char *content_end = NULL;
549 char browser_host[256];
550 char user_agent[256];
556 WebcitHandler *Handler;
559 * We stuff these with the values coming from the client cookies,
560 * so we can use them to reconnect a timed out session if we have to.
562 char c_username[SIZ];
563 char c_password[SIZ];
564 char c_roomname[SIZ];
565 char c_httpauth_string[SIZ];
566 char c_httpauth_user[SIZ];
567 char c_httpauth_pass[SIZ];
570 safestrncpy(c_username, "", sizeof c_username);
571 safestrncpy(c_password, "", sizeof c_password);
572 safestrncpy(c_roomname, "", sizeof c_roomname);
573 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
574 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
575 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
576 strcpy(browser_host, "");
579 if (WCC->WBuf == NULL)
580 WC->WBuf = NewStrBufPlain(NULL, 32768);
581 FlushStrBuf(WCC->WBuf);
583 if (WCC->HBuf == NULL)
584 WCC->HBuf = NewStrBuf();
585 FlushStrBuf(WCC->HBuf);
587 WCC->upload_length = 0;
590 WCC->trailing_javascript = NewStrBuf();
592 /** Figure out the action */
594 sizes[0] = sizeof action;
598 sizes[a] = sizeof arg[a-1];
600 //// index[9] = &foo; todo
603 for ( a = 0; a < 9; ++a)
605 extract_token(index[a], ChrPtr(ReqLine), a + 1, '/', sizes[a]);
606 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
607 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
608 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
609 if ((index[a][0] == '.') && (index[a][1] == '.'))
611 if (index[a][0] == '\0')
616 if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) &&
618 cookie_to_stuff((StrBuf *)vLine, NULL,
619 c_username, sizeof c_username,
620 c_password, sizeof c_password,
621 c_roomname, sizeof c_roomname);
623 if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) &&
625 CtdlDecodeBase64(c_httpauth_string, ChrPtr((StrBuf*)vLine), StrLength((StrBuf*)vLine));
626 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
627 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
629 if (GetHash(HTTPHeaders, HKEY("CONTENT-LENGTH"), &vLine) &&
631 ContentLength = StrToi((StrBuf*)vLine);
633 if (GetHash(HTTPHeaders, HKEY("CONTENT-TYPE"), &vLine) &&
635 ContentType = (StrBuf*)vLine;
637 if (GetHash(HTTPHeaders, HKEY("USER-AGENT"), &vLine) &&
639 safestrncpy(user_agent, ChrPtr((StrBuf*)vLine), sizeof user_agent);
641 if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {
650 GetHash(HTTPHeaders, HKEY("X-FORWARDED-HOST"), &vLine) &&
652 safestrncpy(WCC->http_host,
653 ChrPtr((StrBuf*)vLine),
654 sizeof WCC->http_host);
656 if (IsEmptyStr(WCC->http_host) &&
657 GetHash(HTTPHeaders, HKEY("HOST"), &vLine) &&
659 safestrncpy(WCC->http_host,
660 ChrPtr((StrBuf*)vLine),
661 sizeof WCC->http_host);
664 if (GetHash(HTTPHeaders, HKEY("X-FORWARDED-FOR"), &vLine) &&
666 safestrncpy(browser_host,
667 ChrPtr((StrBuf*) vLine),
668 sizeof browser_host);
669 while (num_tokens(browser_host, ',') > 1) {
670 remove_token(browser_host, 0, ',');
672 striplt(browser_host);
675 if (ContentLength > 0) {
676 content = NewStrBuf();
677 StrBufPrintf(content, "Content-type: %s\n"
678 "Content-length: %d\n\n",
679 ChrPtr(ContentType), ContentLength);
681 hprintf("Content-type: %s\n"
682 "Content-length: %d\n\n",
683 ContentType, ContentLength);
685 body_start = StrLength(content);
687 /** Read the entire input data at once. */
688 client_read(&WCC->http_sock, content, ReadBuf, ContentLength + body_start);
690 if (!strncasecmp(ChrPtr(ContentType), "application/x-www-form-urlencoded", 33)) {
691 StrBufCutLeft(content, body_start);
692 ParseURLParams(content);
693 } else if (!strncasecmp(ChrPtr(ContentType), "multipart", 9)) {
694 content_end = ChrPtr(content) + ContentLength + body_start;
695 mime_parser(ChrPtr(content), content_end, *upload_handler, NULL, NULL, NULL, 0);
701 /* make a note of where we are in case the user wants to save it */
702 safestrncpy(WCC->this_page, ChrPtr(ReqLine), sizeof(WCC->this_page));
703 remove_token(WCC->this_page, 2, ' ');
704 remove_token(WCC->this_page, 0, ' ');
706 /* If there are variables in the URL, we must grab them now */
707 UrlLine = NewStrBufDup(ReqLine);
708 len = StrLength(UrlLine);
709 pch = pchs = ChrPtr(UrlLine);
712 if ((*pch == '?') || (*pch == '&')) {
713 StrBufCutLeft(UrlLine, pch - pchs + 1);
714 ParseURLParams(UrlLine);
719 FreeStrBuf(&UrlLine);
721 /* If it's a "force 404" situation then display the error and bail. */
722 if (!strcmp(action, "404")) {
723 hprintf("HTTP/1.1 404 Not found\r\n");
724 hprintf("Content-Type: text/plain\r\n");
725 wprintf("Not found\r\n");
727 goto SKIP_ALL_THIS_CRAP;
730 /* Static content can be sent without connecting to Citadel. */
732 for (a=0; a<ndirs && ! is_static; ++a) {
733 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
741 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
742 static_dirs[n_static],
743 index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
744 for (a=0; a<8; ++a) {
745 if (buf[strlen(buf)-1] == '/') {
746 buf[strlen(buf)-1] = 0;
749 for (a = 0; a < strlen(buf); ++a) {
750 if (isspace(buf[a])) {
758 lprintf(9, "Suspicious request. Ignoring.");
759 hprintf("HTTP/1.1 404 Security check failed\r\n");
760 hprintf("Content-Type: text/plain\r\n");
761 wprintf("You have sent a malformed or invalid request.\r\n");
764 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
767 /* If the client sent a nonce that is incorrect, kill the request. */
768 if (strlen(bstr("nonce")) > 0) {
769 lprintf(9, "Comparing supplied nonce %s to session nonce %ld\n",
770 bstr("nonce"), WCC->nonce);
771 if (ibstr("nonce") != WCC->nonce) {
772 lprintf(9, "Ignoring request with mismatched nonce.\n");
773 hprintf("HTTP/1.1 404 Security check failed\r\n");
774 hprintf("Content-Type: text/plain\r\n");
775 wprintf("Security check failed.\r\n");
777 goto SKIP_ALL_THIS_CRAP;
782 * If we're not connected to a Citadel server, try to hook up the
785 if (!WCC->connected) {
786 if (!strcasecmp(ctdlhost, "uds")) {
787 /* unix domain socket */
788 snprintf(buf, SIZ, "%s/citadel.socket", ctdlport);
789 WCC->serv_sock = uds_connectsock(buf);
793 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
796 if (WCC->serv_sock < 0) {
798 goto SKIP_ALL_THIS_CRAP;
802 serv_getln(buf, sizeof buf); /** get the server welcome message */
805 * From what host is our user connecting? Go with
806 * the host at the other end of the HTTP socket,
807 * unless we are following X-Forwarded-For: headers
808 * and such a header has already turned up something.
810 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
811 locate_host(browser_host, WCC->http_sock);
814 get_serv_info(browser_host, user_agent);
815 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
817 wprintf(_("You are connected to a Citadel "
818 "server running Citadel %d.%02d. \n"
819 "In order to run this version of WebCit "
820 "you must also have Citadel %d.%02d or"
822 serv_info.serv_rev_level / 100,
823 serv_info.serv_rev_level % 100,
824 MINIMUM_CIT_VERSION / 100,
825 MINIMUM_CIT_VERSION % 100
828 end_webcit_session();
829 goto SKIP_ALL_THIS_CRAP;
833 ////////todo: restore language in this case
835 * Functions which can be performed without logging in
837 if (!strcasecmp(action, "listsub")) {
839 goto SKIP_ALL_THIS_CRAP;
841 if (!strcasecmp(action, "freebusy")) {
842 do_freebusy(ChrPtr(ReqLine));
843 goto SKIP_ALL_THIS_CRAP;
847 * If we're not logged in, but we have HTTP Authentication data,
848 * try logging in to Citadel using that.
850 if ((!WCC->logged_in)
851 && (strlen(c_httpauth_user) > 0)
852 && (strlen(c_httpauth_pass) > 0)) {
853 serv_printf("USER %s", c_httpauth_user);
854 serv_getln(buf, sizeof buf);
856 serv_printf("PASS %s", c_httpauth_pass);
857 serv_getln(buf, sizeof buf);
859 become_logged_in(c_httpauth_user,
860 c_httpauth_pass, buf);
861 safestrncpy(WCC->httpauth_user, c_httpauth_user, sizeof WCC->httpauth_user);
862 safestrncpy(WCC->httpauth_pass, c_httpauth_pass, sizeof WCC->httpauth_pass);
864 /* Should only display when password is wrong */
865 authorization_required(&buf[4]);
866 goto SKIP_ALL_THIS_CRAP;
871 /* This needs to run early */
873 if (!strcasecmp(action, "rss")) {
874 display_rss(bstr("room"), request_method);
875 goto SKIP_ALL_THIS_CRAP;
880 * The GroupDAV stuff relies on HTTP authentication instead of
881 * our session's authentication.
883 if (!strncasecmp(action, "groupdav", 8)) {
884 groupdav_main(HTTPHeaders,
885 ReqLine, request_method,
886 ContentType, /* do GroupDAV methods */
887 ContentLength, content, body_start);
888 if (!WCC->logged_in) {
889 WCC->killthis = 1; /* If not logged in, don't */
890 } /* keep the session active */
891 goto SKIP_ALL_THIS_CRAP;
896 * Automatically send requests with any method other than GET or
897 * POST to the GroupDAV code as well.
899 if ((strcasecmp(ChrPtr(request_method), "GET")) && (strcasecmp(ChrPtr(request_method), "POST"))) {
900 groupdav_main(HTTPHeaders, ReqLine,
901 request_method, ContentType, /** do GroupDAV methods */
902 ContentLength, content, body_start);
903 if (!WCC->logged_in) {
904 WCC->killthis = 1; /** If not logged in, don't */
905 } /** keep the session active */
906 goto SKIP_ALL_THIS_CRAP;
910 * If we're not logged in, but we have username and password cookies
911 * supplied by the browser, try using them to log in.
913 if ((!WCC->logged_in)
914 && (!IsEmptyStr(c_username))
915 && (!IsEmptyStr(c_password))) {
916 serv_printf("USER %s", c_username);
917 serv_getln(buf, sizeof buf);
919 serv_printf("PASS %s", c_password);
920 serv_getln(buf, sizeof buf);
923 become_logged_in(c_username, c_password, buf);
924 if (get_preference("language", &Lang)) {
925 set_selected_language(ChrPtr(Lang));
926 go_selected_language(); /* set locale */
928 get_preference("default_header_charset", &WCC->DefaultCharset);
933 * If we don't have a current room, but a cookie specifying the
934 * current room is supplied, make an effort to go there.
936 if ((IsEmptyStr(WCC->wc_roomname)) && (!IsEmptyStr(c_roomname))) {
937 serv_printf("GOTO %s", c_roomname);
938 serv_getln(buf, sizeof buf);
940 safestrncpy(WCC->wc_roomname, c_roomname, sizeof WCC->wc_roomname);
944 GetHash(HandlerHash, action, strlen(action) /* TODO*/, &vHandler),
945 Handler = (WebcitHandler*) vHandler;
946 if (Handler != NULL) {
947 if (!WCC->logged_in && ((Handler->Flags & ANONYMOUS) == 0)) {
951 if((Handler->Flags & NEED_URL)) {
952 if (WCC->UrlFragment1 == NULL)
953 WCC->UrlFragment1 = NewStrBuf();
954 if (WCC->UrlFragment2 == NULL)
955 WCC->UrlFragment2 = NewStrBuf();
956 if (WCC->UrlFragment3 == NULL)
957 WCC->UrlFragment3 = NewStrBuf();
958 StrBufPrintf(WCC->UrlFragment1, "%s", index[0]);
959 StrBufPrintf(WCC->UrlFragment2, "%s", index[1]);
960 StrBufPrintf(WCC->UrlFragment3, "%s", index[2]);
962 if ((Handler->Flags & AJAX) != 0)
963 begin_ajax_response();
965 if ((Handler->Flags & AJAX) != 0)
969 /* When all else fais, display the main menu. */
979 if (content != NULL) {
980 FreeStrBuf(&content);
983 DeleteHash(&WCC->urlstrings);
984 if (WCC->upload_length > 0) {
986 WCC->upload_length = 0;
988 FreeStrBuf(&WCC->trailing_javascript);
993 * Replacement for sleep() that uses select() in order to avoid SIGALRM
995 void sleeeeeeeeeep(int seconds)
1001 select(0, NULL, NULL, NULL, &tv);
1004 void diagnostics(void)
1006 output_headers(1, 1, 1, 0, 0, 0);
1007 wprintf("Session: %d<hr />\n", WC->wc_session);
1008 wprintf("Command: <br /><PRE>\n");
1009 StrEscPuts(WC->UrlFragment1);
1010 wprintf("<br />\n");
1011 StrEscPuts(WC->UrlFragment2);
1012 wprintf("<br />\n");
1013 StrEscPuts(WC->UrlFragment3);
1014 wprintf("</PRE><hr />\n");
1015 wprintf("Variables: <br /><PRE>\n");
1017 wprintf("</PRE><hr />\n");
1022 int ConditionalImportantMesage(WCTemplateToken *Tokens, void *Context, int ContextType)
1024 wcsession *WCC = WC;
1026 return (!IsEmptyStr(WCC->ImportantMessage));
1031 void tmplput_importantmessage(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1033 wcsession *WCC = WC;
1037 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType,
1038 WCC->ImportantMessage, 0);
1040 StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
1041 WCC->ImportantMessage[0] = '\0';
1045 int ConditionalBstr(WCTemplateToken *Tokens, void *Context, int ContextType)
1047 if(Tokens->nParameters == 3)
1048 return HaveBstr(Tokens->Params[2]->Start,
1049 Tokens->Params[2]->len);
1051 return strcmp(Bstr(Tokens->Params[2]->Start,
1052 Tokens->Params[2]->len),
1053 Tokens->Params[3]->Start) == 0;
1056 void tmplput_bstr(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1058 const StrBuf *Buf = SBstr(Tokens->Params[0]->Start,
1059 Tokens->Params[0]->len);
1061 StrBufAppendTemplate(Target, nArgs, Tokens,
1062 Context, ContextType,
1066 void tmplput_trailing_javascript(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
1068 wcsession *WCC = WC;
1071 StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType,
1072 WCC->trailing_javascript, 0);
1075 void tmplput_csslocal(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1077 extern StrBuf *csslocal;
1078 StrBufAppendBuf(Target,
1082 void tmplput_url_part(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1085 wcsession *WCC = WC;
1088 if (Tokens->Params[0]->lvalue == 0)
1089 UrlBuf = WCC->UrlFragment1;
1090 else if (Tokens->Params[0]->lvalue == 1)
1091 UrlBuf = WCC->UrlFragment2;
1093 UrlBuf = WCC->UrlFragment3;
1095 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType,
1107 WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS);
1108 WebcitAddUrlHandler(HKEY("do_template"), url_do_template, ANONYMOUS);
1109 WebcitAddUrlHandler(HKEY("sslg"), seconds_since_last_gexp, AJAX);
1110 WebcitAddUrlHandler(HKEY("ajax_servcmd"), ajax_servcmd, 0);
1111 WebcitAddUrlHandler(HKEY("diagnostics"), diagnostics, NEED_URL);
1113 RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
1114 RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr, CTX_NONE);
1115 RegisterNamespace("BSTR", 1, 2, tmplput_bstr, CTX_NONE);
1116 RegisterNamespace("URLPART", 1, 2, tmplput_url_part, CTX_NONE);
1117 RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
1118 RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
1119 RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, CTX_NONE);
1120 RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, CTX_NONE);