From 366e4c2fd1d8ee0036ee9705c49241655874ae92 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 14 Jan 2022 11:08:55 -0500 Subject: [PATCH] This is a better version of detect_logged_in() for webcit-ng that checks the server cookie instead of clumsily attempting a command. This works synchronously so it can be combined with other commands without creating a race condition. Also moved the login screen to a modal. --- webcit-ng/ctdlclient.c | 4 +-- webcit-ng/http.c | 2 +- webcit-ng/static/index.html | 6 +--- webcit-ng/static/js/login.js | 64 +++++++++++++++++------------------- webcit-ng/static/js/main.js | 28 +++++++++++----- webcit-ng/static/js/util.js | 9 +++++ webcit/listsub.c | 1 + 7 files changed, 63 insertions(+), 51 deletions(-) diff --git a/webcit-ng/ctdlclient.c b/webcit-ng/ctdlclient.c index d44270d64..3ef786707 100644 --- a/webcit-ng/ctdlclient.c +++ b/webcit-ng/ctdlclient.c @@ -57,12 +57,12 @@ int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes) { --len; } buf[len] = 0; - // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf); + syslog(LOG_DEBUG, "\033[32m[ %s\033[0m", buf); return (len); } ++len; } - // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf); + syslog(LOG_DEBUG, "\033[32m[ %s\033[0m", buf); return (len); } diff --git a/webcit-ng/http.c b/webcit-ng/http.c index f39ebe214..a16e7c5af 100644 --- a/webcit-ng/http.c +++ b/webcit-ng/http.c @@ -229,7 +229,7 @@ void perform_one_http_transaction(struct client_handle *ch) { #endif // Output the results back to the client. - syslog(LOG_DEBUG, "> %03d %s", h.response_code, h.response_string); + syslog(LOG_DEBUG, "\033[33m\033[1m> %03d %s\033[0m", h.response_code, h.response_string); client_printf(ch, "HTTP/1.1 %03d %s\r\n", h.response_code, h.response_string); client_printf(ch, "Connection: close\r\n"); client_printf(ch, "Content-Length: %ld\r\n", h.response_body_length); diff --git a/webcit-ng/static/index.html b/webcit-ng/static/index.html index af8aab676..4e1f71b3a 100644 --- a/webcit-ng/static/index.html +++ b/webcit-ng/static/index.html @@ -18,11 +18,7 @@ diff --git a/webcit-ng/static/js/login.js b/webcit-ng/static/js/login.js index 20e5de5f1..9b8e425f5 100644 --- a/webcit-ng/static/js/login.js +++ b/webcit-ng/static/js/login.js @@ -1,5 +1,4 @@ -// -// Copyright (c) 2016-2020 by the citadel.org team +// Copyright (c) 2016-2022 by the citadel.org team // // This program is open source software. It runs great on the // Linux operating system (and probably elsewhere). You can use, @@ -12,23 +11,26 @@ // GNU General Public License for more details. -function display_login_screen(any_message) -{ - document.getElementById("ctdl-main").innerHTML = - "


Put the login screen here, dummary

" + - any_message + "

" + - "
" + - _("User name:") + "
" + - _("Password:") + "

" + - "" + _("Log in") + "
" - ; +function display_login_screen(any_message) { + + document.getElementById("ctdl_big_modal").innerHTML = + "
" + + "
" + + + "Put the login screen here, dummy

" + + any_message + "

" + + "
" + + _("User name:") + "
" + + _("Password:") + "

" + + "" + _("Log in") + "" - update_banner(); + + "
" + + "
"; + document.getElementById("ctdl_big_modal").style.display = "block"; } -function logout() -{ +function logout() { var request = new XMLHttpRequest(); request.open("GET", "/ctdl/a/logout", true); request.onreadystatechange = function() { @@ -39,8 +41,7 @@ function logout() } -function login_button(username) -{ +function login_button(username) { parms = document.getElementById("username").value + "|" @@ -58,13 +59,13 @@ function login_button(username) } -function login_result(data) -{ +function login_result(data) { if (data.substring(0,1) == "2") { logged_in = 1; current_user = data.substring(4).split("|")[0]; update_banner(); document.getElementById("ctdl-main").innerHTML = "FIXME ok we are logged in as " + current_user + " ... " ; + document.getElementById("ctdl_big_modal").style.display = "none"; } else { display_login_screen(data.substring(4)); @@ -73,22 +74,17 @@ function login_result(data) // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly. -// -function detect_logged_in() -{ - var request = new XMLHttpRequest(); - request.open("GET", "/ctdl/a/whoami", true); - request.onreadystatechange = function() { - detect_logged_in_2(this.responseText); - }; - request.send(); - request = null; -} -function detect_logged_in_2(data) -{ - if (data.length > 0) { +function detect_logged_in() { + try { + wcauth_decoded = atob(getCookieValue("wcauth")); + wcauth_user = wcauth_decoded.split(":")[0]; + } + catch(err) { + wcauth_user = ""; + } + if (wcauth_user.length > 0) { logged_in = 1; - current_user = data; + current_user = wcauth_user; } else { logged_in = 0; diff --git a/webcit-ng/static/js/main.js b/webcit-ng/static/js/main.js index 615bdaaf4..77779f075 100644 --- a/webcit-ng/static/js/main.js +++ b/webcit-ng/static/js/main.js @@ -1,4 +1,3 @@ -// // Copyright (c) 2016-2020 by the citadel.org team // // This program is open source software. It runs great on the @@ -44,9 +43,15 @@ ctdl_startup = async() => { update_banner(); - // for now, show a room list in the main div - gotoroom("_BASEROOM_"); - display_room_list(); + // What do we do upon landing? + + if ( (serv_info.serv_supports_guest) || (logged_in) ) { // If the Lobby is visible, + gotoroom("_BASEROOM_"); // go there. + display_room_list(); + } + else { // Otherwise, + display_login_screen("logged in users only. sheeeeeeeeeit."); // display the login modal. + } } else { document.getElementById("ctdl-main").innerHTML = @@ -131,13 +136,12 @@ function update_banner() { // goto room -// function gotoroom(roomname) { fetch_room = async() => { response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/"); - data = await(response.json()); if (response.ok) { + data = await(response.json()); current_room = data.name; new_messages = data.new_messages; total_messages = data.total_messages; @@ -198,9 +202,15 @@ function load_new_march_list() { // Activate the "Loading..." modal function activate_loading_modal() { - document.getElementById("ctdl_big_modal_content").innerHTML = - "  " - + _("Loading messages from server, please wait"); + document.getElementById("ctdl_big_modal").innerHTML = + "
" + + "
" + + + "  " + + _("Loading messages from server, please wait") + + + "
" + + "
"; document.getElementById("ctdl_big_modal").style.display = "block"; } diff --git a/webcit-ng/static/js/util.js b/webcit-ng/static/js/util.js index 201247450..2499ca759 100644 --- a/webcit-ng/static/js/util.js +++ b/webcit-ng/static/js/util.js @@ -114,3 +114,12 @@ function convertTimestamp(timestamp) { return time; } + + +// Get the value of a cookie from the HTTP session +// Shamelessly swiped from https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript +const getCookieValue = (name) => ( + document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || '' +) + + diff --git a/webcit/listsub.c b/webcit/listsub.c index 6236f4b36..95f9ecfdb 100644 --- a/webcit/listsub.c +++ b/webcit/listsub.c @@ -98,6 +98,7 @@ int confirm_sub_or_unsub(char *cmd, StrBuf *Target, WCTemplputParams *TP) { return rc == 2; } + int Conditional_LISTSUB_EXECUTE_CONFIRMSUBSCRIBE(StrBuf *Target, WCTemplputParams *TP) { if (strcmp(bstr("cmd"), "confirm_subscribe")) { return 0; -- 2.30.2