comparison mod_conversejs/mod_conversejs.lua @ 3598:1921ae4449b8

mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
author Kim Alvefur <zash@zash.se>
date Thu, 16 May 2019 08:45:41 +0200
parents 4feab7e87675
children 42fa833169bb
comparison
equal deleted inserted replaced
3597:da7ec4ed6ddf 3598:1921ae4449b8
1 -- mod_conversejs 1 -- mod_conversejs
2 -- Copyright (C) 2017 Kim Alvefur 2 -- Copyright (C) 2017 Kim Alvefur
3 3
4 local json_encode = require"util.json".encode; 4 local json_encode = require"util.json".encode;
5 local xml_escape = require "util.stanza".xml_escape;
6 local render = require "util.interpolation".new("%b{}", xml_escape, { json = json_encode });
5 7
6 module:depends"http"; 8 module:depends"http";
7 9
8 local has_bosh = pcall(function () 10 local has_bosh = pcall(function ()
9 module:depends"bosh"; 11 module:depends"bosh";
22 local version = module:get_option_string("conversejs_version", ""); 24 local version = module:get_option_string("conversejs_version", "");
23 if version ~= "" then version = "/" .. version end 25 if version ~= "" then version = "/" .. version end
24 local js_url = module:get_option_string("conversejs_script", cdn_url..version.."/dist/converse.min.js"); 26 local js_url = module:get_option_string("conversejs_script", cdn_url..version.."/dist/converse.min.js");
25 local css_url = module:get_option_string("conversejs_css", cdn_url..version.."/css/converse.min.css"); 27 local css_url = module:get_option_string("conversejs_css", cdn_url..version.."/css/converse.min.css");
26 28
27 local html_template = ([[ 29 local html_template;
28 <!DOCTYPE html>
29 <html>
30 <head>
31 <meta charset="utf-8">
32 <link rel="stylesheet" type="text/css" media="screen" href="$css_url"/>
33 <script charset="utf-8" src="$js_url"></script>
34 <title>Prosody IM and Converse.js</title>
35 </head>
36 <body>
37 <noscript>
38 <h1>Converse.js</h1>
39 <p>I&apos;m sorry, but this XMPP client application won&apos;t work without JavaScript.</p>
40 <p>Perhaps you would like to try one of these clients:</p>
41 <dl>
42 <dt>Desktop</dt>
43 <dd><ul>
44 <li><a href="https://gajim.org/">Gajim</a></li>
45 <li><a href="https://poez.io/">Poezio</a></li>
46 <li><a href="https://swift.im/">Swift</a></li>
47 </ul></dd>
48 <dt>Mobile</dt>
49 <dd><ul>
50 <li><a href="https://github.com/siacs/Conversations">Conversations</a></li>
51 <li><a href="https://yaxim.org/">Yaxim</a></li>
52 </ul></dd>
53 </dl>
54 <p><a href="https://xmpp.org/software/clients.html">More clients...</a></p>
55 </noscript>
56 <script>%s</script>
57 </body>
58 </html>
59 ]]):gsub("$([%w_]+)", { js_url = js_url, css_url = css_url });
60 30
61 js_template = [[ 31 do
62 if(typeof converse == 'undefined') { 32 local template_filename = module:get_option_string(module.name .. "_html_template", "template.html");
63 var div = document.createElement("div"); 33 local template_file, err = module:load_resource(template_filename);
64 var noscript = document.getElementsByTagName("noscript")[0]; 34 if template_file then
65 div.innerHTML = noscript.innerText; 35 html_template, err = template_file:read("*a");
66 document.body.appendChild(div); 36 template_file:close();
67 } else { 37 end
68 converse.initialize(%s); 38 if not html_template then
69 } 39 module:log("error", "Error loading HTML template: %s", err);
70 ]]; 40 html_template = render("<h1>mod_{module} could not read the template</h1>\
41 <p>Tried to open <b>{filename}</b></p>\
42 <pre>{error}</pre>",
43 { module = module.name, filename = template_filename, error = err });
44 end
45 end
46
47 local js_template;
48 do
49 local template_filename = module:get_option_string(module.name .. "_js_template", "template.js");
50 local template_file, err = module:load_resource(template_filename);
51 if template_file then
52 js_template, err = template_file:read("*a");
53 template_file:close();
54 end
55 if not js_template then
56 module:log("error", "Error loading JS template: %s", err);
57 js_template = render("console.log(\"mod_{module} could not read the JS template: %s\", {error|json})",
58 { module = module.name, filename = template_filename, error = err });
59 end
60 end
71 61
72 local user_options = module:get_option("conversejs_options"); 62 local user_options = module:get_option("conversejs_options");
73 63
74 local function get_converse_options() 64 local function get_converse_options()
75 local allow_registration = module:get_option_boolean("allow_registration", false); 65 local allow_registration = module:get_option_boolean("allow_registration", false);
91 end 81 end
92 82
93 return converse_options; 83 return converse_options;
94 end 84 end
95 85
96 local add_tags = module:get_option_set("conversejs_tags"); 86 local add_tags = module:get_option_array("conversejs_tags", {});
97
98 if add_tags then
99 local tags = {};
100 for tag in add_tags do
101 table.insert(tags, tag);
102 end
103 html_template = html_template:gsub("</head>", table.concat(tags, "\n"):gsub("%%", "%%").."\n</head>");
104 end
105 87
106 module:provides("http", { 88 module:provides("http", {
107 title = "Converse.js"; 89 title = "Converse.js";
108 route = { 90 route = {
109 GET = function (event) 91 GET = function (event)
110 local converse_options = get_converse_options(); 92 local converse_options = get_converse_options();
111 93
112 event.response.headers.content_type = "text/html"; 94 event.response.headers.content_type = "text/html";
113 return html_template:format(js_template:format(json_encode(converse_options))); 95 return render(html_template, {
96 header_scripts = { js_url };
97 header_style = { css_url };
98 header_tags = add_tags;
99 conversejs = {
100 options = converse_options;
101 startup = { script = js_template:format(json_encode(converse_options)); }
102 };
103 });
114 end; 104 end;
115 105
116 ["GET /prosody-converse.js"] = function (event) 106 ["GET /prosody-converse.js"] = function (event)
117 local converse_options = get_converse_options(); 107 local converse_options = get_converse_options();
118 108