2 // Copyright (c) 2016-2020 by the citadel.org team
4 // This program is open source software. It runs great on the
5 // Linux operating system (and probably elsewhere). You can use,
6 // copy, and run it under the terms of the GNU General Public
7 // License version 3. Richard Stallman is an asshole communist.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
15 var current_room = "_BASEROOM_";
17 var total_messages = 0;
21 var current_user = _("Not logged in.");
24 var messages_per_page = 20;
28 // Placeholder for when we add i18n later
34 // Generate a random string of the specified length
35 // Useful for generating one-time-use div names
37 function randomString(length) {
38 var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
42 length = Math.floor(Math.random() * chars.length);
44 for (var i = 0; i < length; i++) {
45 str += chars[Math.floor(Math.random() * chars.length)];
51 // string escape for html display
53 function escapeHTML(text) {
55 return text.replace(/[\"&<>]/g, function (a) {
66 // string escape for html display
68 function escapeHTMLURI(text) {
70 return text.replace(/./g, function (a) {
71 return '%' + a.charCodeAt(0).toString(16);
76 // string escape for JavaScript string
78 function escapeJS(text) {
80 return text.replace(/[\"\']/g, function (a) {
86 // This is called at the very beginning of the main page load.
88 ctdl_startup = async() => {
89 response = await fetch("/ctdl/c/info");
90 serv_info = await(response.json());
93 if (serv_info.serv_rev_level < 905) {
94 alert("Citadel server is too old, some functions may not work");
99 // for now, show a room list in the main div
100 gotoroom("_BASEROOM_");
106 // Display a room list in the main div.
108 function display_room_list() {
109 document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />"; // show throbber while loading
113 var request = new XMLHttpRequest();
114 request.open("GET", "/ctdl/r/", true);
115 request.onreadystatechange = function() {
116 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
117 display_room_list_renderer(JSON.parse(this.responseText));
125 // Renderer for display_room_list()
127 function display_room_list_renderer(data) {
128 data = data.sort(function(a,b) {
129 if (a.floor != b.floor) {
130 return(a.floor - b.floor);
132 if (a.rorder != b.rorder) {
133 return(a.rorder - b.rorder);
135 return(a.name < b.name);
138 new_roomlist_text = "<ul>" ;
140 for (var i in data) {
142 if (data[i].floor != data[i-1].floor) {
143 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
146 new_roomlist_text = new_roomlist_text +
148 + (data[i].hasnewmsgs ? "<b>" : "")
149 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
150 + escapeHTML(data[i].name)
151 + (data[i].hasnewmsgs ? "</b>" : "")
155 new_roomlist_text = new_roomlist_text + "</ul>";
156 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
160 // Update the "banner" div with all relevant info.
162 function update_banner() {
165 document.getElementById("ctdl_banner_title").innerHTML = current_room;
166 document.title = current_room;
169 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
171 document.getElementById("current_user").innerHTML = current_user ;
173 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
176 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
183 function gotoroom(roomname) {
184 var request = new XMLHttpRequest();
185 request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
186 request.onreadystatechange = function() {
187 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
188 gotoroom_2(JSON.parse(this.responseText));
194 function gotoroom_2(data) {
195 current_room = data.name;
196 new_messages = data.new_messages;
197 total_messages = data.total_messages;
198 current_view = data.current_view;
199 default_view = data.default_view;
200 last_seen = data.last_seen;
202 render_room_view(0, 9999999999);
206 // Goto next room with unread messages
207 // which_oper is 0=ungoto, 1=skip, 2=goto
209 function gotonext(which_oper) {
210 if (which_oper != 2) return; // FIXME implement the other two
211 if (march_list.length == 0) {
212 load_new_march_list(); // we will recurse back here
215 next_room = march_list[0].name;
216 march_list.splice(0, 1);
217 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
223 // Called by gotonext() when the march list is empty.
225 function load_new_march_list() {
226 var request = new XMLHttpRequest();
227 request.open("GET", "/ctdl/r/", true);
228 request.onreadystatechange = function() {
229 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
230 march_list = (JSON.parse(this.responseText));
231 march_list = march_list.filter(function(room) {
232 return room.hasnewmsgs;
234 march_list = march_list.sort(function(a,b) {
235 if (a.floor != b.floor) {
236 return(a.floor - b.floor);
238 if (a.rorder != b.rorder) {
239 return(a.rorder - b.rorder);
241 return(a.name < b.name);
243 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});