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
111 fetch_room_list = async() => {
112 response = await fetch("/ctdl/r/");
113 room_list = await(response.json());
115 display_room_list_renderer(room_list);
122 // Renderer for display_room_list()
124 function display_room_list_renderer(data) {
125 data = data.sort(function(a,b) {
126 if (a.floor != b.floor) {
127 return(a.floor - b.floor);
129 if (a.rorder != b.rorder) {
130 return(a.rorder - b.rorder);
132 return(a.name < b.name);
135 new_roomlist_text = "<ul>" ;
137 for (var i in data) {
139 if (data[i].floor != data[i-1].floor) {
140 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
143 new_roomlist_text = new_roomlist_text +
145 + (data[i].hasnewmsgs ? "<b>" : "")
146 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
147 + escapeHTML(data[i].name)
148 + (data[i].hasnewmsgs ? "</b>" : "")
152 new_roomlist_text = new_roomlist_text + "</ul>";
153 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
157 // Update the "banner" div with all relevant info.
159 function update_banner() {
162 document.getElementById("ctdl_banner_title").innerHTML = current_room;
163 document.title = current_room;
166 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
168 document.getElementById("current_user").innerHTML = current_user ;
170 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
173 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
180 function gotoroom(roomname) {
182 fetch_room = async() => {
183 response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
184 data = await(response.json());
186 current_room = data.name;
187 new_messages = data.new_messages;
188 total_messages = data.total_messages;
189 current_view = data.current_view;
190 default_view = data.default_view;
191 last_seen = data.last_seen;
193 render_room_view(0, 9999999999);
200 // Goto next room with unread messages
201 // which_oper is 0=ungoto, 1=skip, 2=goto
203 function gotonext(which_oper) {
204 if (which_oper != 2) return; // FIXME implement the other two
205 if (march_list.length == 0) {
206 load_new_march_list(); // we will recurse back here
209 next_room = march_list[0].name;
210 march_list.splice(0, 1);
211 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
217 // Called by gotonext() when the march list is empty.
219 function load_new_march_list() {
220 fetchm = async() => {
221 response = await fetch("/ctdl/r/");
222 march_list = await(response.json());
224 march_list = march_list.filter(function(room) {
225 return room.hasnewmsgs;
227 march_list = march_list.sort(function(a,b) {
228 if (a.floor != b.floor) {
229 return(a.floor - b.floor);
231 if (a.rorder != b.rorder) {
232 return(a.rorder - b.rorder);
234 return(a.name < b.name);
236 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});