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('');
39 length = Math.floor(Math.random() * chars.length);
43 for (var i = 0; i < length; i++) {
44 str += chars[Math.floor(Math.random() * chars.length)];
50 // string escape for html display
52 function escapeHTML(text) {
54 return text.replace(/[\"&<>]/g, function (a) {
65 // string escape for html display
67 function escapeHTMLURI(text) {
69 return text.replace(/./g, function (a) {
70 return '%' + a.charCodeAt(0).toString(16);
75 // string escape for JavaScript string
77 function escapeJS(text) {
79 return text.replace(/[\"\']/g, function (a) {
85 // This is called at the very beginning of the main page load.
87 function ctdl_startup() {
88 var request = new XMLHttpRequest();
89 request.open("GET", "/ctdl/c/info", true);
90 request.onreadystatechange = function() {
91 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
92 ctdl_startup_2(JSON.parse(this.responseText));
99 // Continuation of ctdl_startup() after serv_info is retrieved
101 function ctdl_startup_2(data) {
104 if (data.serv_rev_level < 905) {
105 alert("Citadel server is too old, some functions may not work");
110 // for now, show a room list in the main div
111 gotoroom("_BASEROOM_");
115 // Display a room list in the main div.
117 function display_room_list() {
118 document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ; // show throbber while loading
120 var request = new XMLHttpRequest();
121 request.open("GET", "/ctdl/r/", true);
122 request.onreadystatechange = function() {
123 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
124 display_room_list_renderer(JSON.parse(this.responseText));
131 // Renderer for display_room_list()
133 function display_room_list_renderer(data) {
134 data = data.sort(function(a,b) {
135 if (a.floor != b.floor) {
136 return(a.floor - b.floor);
138 if (a.rorder != b.rorder) {
139 return(a.rorder - b.rorder);
141 return(a.name < b.name);
144 new_roomlist_text = "<ul>" ;
146 for (var i in data) {
148 if (data[i].floor != data[i-1].floor) {
149 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
152 new_roomlist_text = new_roomlist_text +
154 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
155 + escapeHTML(data[i].name)
159 new_roomlist_text = new_roomlist_text + "</ul>";
160 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
163 // Update the "banner" div with all relevant info.
165 function update_banner() {
168 document.getElementById("ctdl_banner_title").innerHTML = current_room;
169 document.title = current_room;
172 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
174 document.getElementById("current_user").innerHTML = current_user ;
176 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
179 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
186 function gotoroom(roomname) {
187 var request = new XMLHttpRequest();
188 request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
189 request.onreadystatechange = function() {
190 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
191 gotoroom_2(JSON.parse(this.responseText));
197 function gotoroom_2(data) {
198 current_room = data.name;
199 new_messages = data.new_messages;
200 total_messages = data.total_messages;
201 current_view = data.current_view;
202 default_view = data.default_view;
203 last_seen = data.last_seen;
205 render_room_view(0, 9999999999);
209 // Goto next room with unread messages
211 function gotonext() {
212 if (march_list.length == 0) {
213 load_new_march_list(); // we will recurse back here
216 next_room = march_list[0].name;
217 march_list.splice(0, 1);
218 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
224 // Called by gotonext() when the march list is empty.
226 function load_new_march_list() {
227 var request = new XMLHttpRequest();
228 request.open("GET", "/ctdl/r/", true);
229 request.onreadystatechange = function() {
230 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
231 march_list = (JSON.parse(this.responseText));
232 march_list = march_list.filter(function(room) {
233 return room.hasnewmsgs;
235 march_list = march_list.sort(function(a,b) {
236 if (a.floor != b.floor) {
237 return(a.floor - b.floor);
239 if (a.rorder != b.rorder) {
240 return(a.rorder - b.rorder);
242 return(a.name < b.name);
244 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});