2 // Copyright (c) 2016-2019 by the citadel.org team
4 // This program is open source software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 3.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
13 var current_room = "_BASEROOM_";
15 var total_messages = 0;
19 var current_user = _("Not logged in.");
22 var messages_per_page = 20;
26 // Placeholder for when we add i18n later
32 // Generate a random string of the specified length
33 // Useful for generating one-time-use div names
35 function randomString(length) {
36 var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
40 length = Math.floor(Math.random() * chars.length);
42 for (var i = 0; i < length; i++) {
43 str += chars[Math.floor(Math.random() * chars.length)];
49 // string escape for html display
51 function escapeHTML(text) {
53 return text.replace(/[\"&<>]/g, function (a) {
64 // string escape for html display
66 function escapeHTMLURI(text) {
68 return text.replace(/./g, function (a) {
69 return '%' + a.charCodeAt(0).toString(16);
74 // string escape for JavaScript string
76 function escapeJS(text) {
78 return text.replace(/[\"\']/g, function (a) {
84 // This is called at the very beginning of the main page load.
86 function ctdl_startup() {
88 const csa = async () => {
89 console.log("starting");
90 const response = await fetch("/ctdl/c/info");
91 const the_text = await(response.text());
94 serv_info = JSON.parse(the_text);
95 if (serv_info.serv_rev_level < 905) {
96 alert("Citadel server is too old, some functions may not work");
101 // for now, show a room list in the main div
102 gotoroom("_BASEROOM_");
110 // Display a room list in the main div.
112 function display_room_list() {
113 document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ; // show throbber while loading
115 var request = new XMLHttpRequest();
116 request.open("GET", "/ctdl/r/", true);
117 request.onreadystatechange = function() {
118 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
119 display_room_list_renderer(JSON.parse(this.responseText));
127 // Renderer for display_room_list()
129 function display_room_list_renderer(data) {
130 data = data.sort(function(a,b) {
131 if (a.floor != b.floor) {
132 return(a.floor - b.floor);
134 if (a.rorder != b.rorder) {
135 return(a.rorder - b.rorder);
137 return(a.name < b.name);
140 new_roomlist_text = "<ul>" ;
142 for (var i in data) {
144 if (data[i].floor != data[i-1].floor) {
145 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
148 new_roomlist_text = new_roomlist_text +
150 + (data[i].hasnewmsgs ? "<b>" : "")
151 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
152 + escapeHTML(data[i].name)
153 + (data[i].hasnewmsgs ? "</b>" : "")
157 new_roomlist_text = new_roomlist_text + "</ul>";
158 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
162 // Update the "banner" div with all relevant info.
164 function update_banner() {
167 document.getElementById("ctdl_banner_title").innerHTML = current_room;
168 document.title = current_room;
171 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
173 document.getElementById("current_user").innerHTML = current_user ;
175 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
178 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
185 function gotoroom(roomname) {
186 var request = new XMLHttpRequest();
187 request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
188 request.onreadystatechange = function() {
189 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
190 gotoroom_2(JSON.parse(this.responseText));
196 function gotoroom_2(data) {
197 current_room = data.name;
198 new_messages = data.new_messages;
199 total_messages = data.total_messages;
200 current_view = data.current_view;
201 default_view = data.default_view;
202 last_seen = data.last_seen;
204 render_room_view(0, 9999999999);
208 // Goto next room with unread messages
209 // which_oper is 0=ungoto, 1=skip, 2=goto
211 function gotonext(which_oper) {
212 if (which_oper != 2) return; // FIXME implement the other two
213 if (march_list.length == 0) {
214 load_new_march_list(); // we will recurse back here
217 next_room = march_list[0].name;
218 march_list.splice(0, 1);
219 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
225 // Called by gotonext() when the march list is empty.
227 function load_new_march_list() {
228 var request = new XMLHttpRequest();
229 request.open("GET", "/ctdl/r/", true);
230 request.onreadystatechange = function() {
231 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
232 march_list = (JSON.parse(this.responseText));
233 march_list = march_list.filter(function(room) {
234 return room.hasnewmsgs;
236 march_list = march_list.sort(function(a,b) {
237 if (a.floor != b.floor) {
238 return(a.floor - b.floor);
240 if (a.rorder != b.rorder) {
241 return(a.rorder - b.rorder);
243 return(a.name < b.name);
245 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});