Annotate

mod_conversejs/mod_conversejs.lua @ 3494:4feab7e87675

mod_conversejs: Add dependency on mod_bookmarks Converse.js stores bookmarks in PEP
author Kim Alvefur <zash@zash.se>
date Sun, 24 Mar 2019 03:04:34 +0100
parent 3492:f59334da6df9
child 3598:1921ae4449b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_conversejs
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2017 Kim Alvefur
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local json_encode = require"util.json".encode;
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
3329
43d0e298ddda mod_conversejs: Explicitly depend on mod_http
Kim Alvefur <zash@zash.se>
parents: 3324
diff changeset
6 module:depends"http";
3363
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
7
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
8 local has_bosh = pcall(function ()
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
9 module:depends"bosh";
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
10 end);
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local has_ws = pcall(function ()
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 module:depends("websocket");
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end);
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
3494
4feab7e87675 mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 3492
diff changeset
16 pcall(function ()
4feab7e87675 mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 3492
diff changeset
17 module:depends("bookmarks");
4feab7e87675 mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 3492
diff changeset
18 end);
4feab7e87675 mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 3492
diff changeset
19
3331
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
20 local cdn_url = module:get_option_string("conversejs_cdn", "https://cdn.conversejs.org");
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
21
3348
f753cf4f7224 mod_conversejs: Default to latest version of Converse.js
Kim Alvefur <zash@zash.se>
parents: 3347
diff changeset
22 local version = module:get_option_string("conversejs_version", "");
3347
823156d5885b mod_conversejs: Strip extra slash if version is set to the empty string
Kim Alvefur <zash@zash.se>
parents: 3337
diff changeset
23 if version ~= "" then version = "/" .. version end
823156d5885b mod_conversejs: Strip extra slash if version is set to the empty string
Kim Alvefur <zash@zash.se>
parents: 3337
diff changeset
24 local js_url = module:get_option_string("conversejs_script", cdn_url..version.."/dist/converse.min.js");
823156d5885b mod_conversejs: Strip extra slash if version is set to the empty string
Kim Alvefur <zash@zash.se>
parents: 3337
diff changeset
25 local css_url = module:get_option_string("conversejs_css", cdn_url..version.."/css/converse.min.css");
3331
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
26
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
27 local html_template = ([[
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 <!DOCTYPE html>
3038
48cbf6a3f112 mod_conversejs: Make HTML more well-formed
Kim Alvefur <zash@zash.se>
parents: 2998
diff changeset
29 <html>
48cbf6a3f112 mod_conversejs: Make HTML more well-formed
Kim Alvefur <zash@zash.se>
parents: 2998
diff changeset
30 <head>
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 <meta charset="utf-8">
3331
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
32 <link rel="stylesheet" type="text/css" media="screen" href="$css_url"/>
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
33 <script charset="utf-8" src="$js_url"></script>
3324
3114b403362d mod_conversejs: Add a page title
Kim Alvefur <zash@zash.se>
parents: 3323
diff changeset
34 <title>Prosody IM and Converse.js</title>
3038
48cbf6a3f112 mod_conversejs: Make HTML more well-formed
Kim Alvefur <zash@zash.se>
parents: 2998
diff changeset
35 </head>
3039
df77580be2f0 mod_conversejs: Appologise for the JavaScript
Kim Alvefur <zash@zash.se>
parents: 3038
diff changeset
36 <body>
df77580be2f0 mod_conversejs: Appologise for the JavaScript
Kim Alvefur <zash@zash.se>
parents: 3038
diff changeset
37 <noscript>
df77580be2f0 mod_conversejs: Appologise for the JavaScript
Kim Alvefur <zash@zash.se>
parents: 3038
diff changeset
38 <h1>Converse.js</h1>
df77580be2f0 mod_conversejs: Appologise for the JavaScript
Kim Alvefur <zash@zash.se>
parents: 3038
diff changeset
39 <p>I&apos;m sorry, but this XMPP client application won&apos;t work without JavaScript.</p>
3040
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
40 <p>Perhaps you would like to try one of these clients:</p>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
41 <dl>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
42 <dt>Desktop</dt>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
43 <dd><ul>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
44 <li><a href="https://gajim.org/">Gajim</a></li>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
45 <li><a href="https://poez.io/">Poezio</a></li>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
46 <li><a href="https://swift.im/">Swift</a></li>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
47 </ul></dd>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
48 <dt>Mobile</dt>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
49 <dd><ul>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
50 <li><a href="https://github.com/siacs/Conversations">Conversations</a></li>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
51 <li><a href="https://yaxim.org/">Yaxim</a></li>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
52 </ul></dd>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
53 </dl>
81b75086a781 mod_conversejs: Suggest some alternative cliets if JavaScript is disabled
Kim Alvefur <zash@zash.se>
parents: 3039
diff changeset
54 <p><a href="https://xmpp.org/software/clients.html">More clients...</a></p>
3039
df77580be2f0 mod_conversejs: Appologise for the JavaScript
Kim Alvefur <zash@zash.se>
parents: 3038
diff changeset
55 </noscript>
3312
e714be00aaad mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents: 3310
diff changeset
56 <script>%s</script>
3039
df77580be2f0 mod_conversejs: Appologise for the JavaScript
Kim Alvefur <zash@zash.se>
parents: 3038
diff changeset
57 </body>
3038
48cbf6a3f112 mod_conversejs: Make HTML more well-formed
Kim Alvefur <zash@zash.se>
parents: 2998
diff changeset
58 </html>
3331
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
59 ]]):gsub("$([%w_]+)", { js_url = js_url, css_url = css_url });
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
3492
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
61 js_template = [[
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
62 if(typeof converse == 'undefined') {
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
63 var div = document.createElement("div");
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
64 var noscript = document.getElementsByTagName("noscript")[0];
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
65 div.innerHTML = noscript.innerText;
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
66 document.body.appendChild(div);
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
67 } else {
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
68 converse.initialize(%s);
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
69 }
f59334da6df9 mod_conversejs: Show fallback text if converse.js fails to load (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents: 3363
diff changeset
70 ]];
3312
e714be00aaad mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents: 3310
diff changeset
71
3332
4fdd8b77da54 mod_conversejs: Variable rename for clarity (user may override options)
Matthew Wild <mwild1@gmail.com>
parents: 3331
diff changeset
72 local user_options = module:get_option("conversejs_options");
2919
0ea93da47db9 mod_conversejs: Allow passing arbitrary options trough to Converse.js
Kim Alvefur <zash@zash.se>
parents: 2694
diff changeset
73
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
74 local function get_converse_options()
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
75 local allow_registration = module:get_option_boolean("allow_registration", false);
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
76 local converse_options = {
3363
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
77 bosh_service_url = has_bosh and module:http_url("bosh","/http-bind") or nil;
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
78 websocket_url = has_ws and module:http_url("websocket","xmpp-websocket"):gsub("^http", "ws") or nil;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
79 authentication = module:get_option_string("authentication") == "anonymous" and "anonymous" or "login";
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
80 jid = module.host;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
81 default_domain = module.host;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
82 domain_placeholder = module.host;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
83 allow_registration = allow_registration;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
84 registration_domain = allow_registration and module.host or nil;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
85 };
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
86
3332
4fdd8b77da54 mod_conversejs: Variable rename for clarity (user may override options)
Matthew Wild <mwild1@gmail.com>
parents: 3331
diff changeset
87 if type(user_options) == "table" then
4fdd8b77da54 mod_conversejs: Variable rename for clarity (user may override options)
Matthew Wild <mwild1@gmail.com>
parents: 3331
diff changeset
88 for k,v in pairs(user_options) do
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
89 converse_options[k] = v;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
90 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
91 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
92
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
93 return converse_options;
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
94 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
95
3333
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
96 local add_tags = module:get_option_set("conversejs_tags");
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
97
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
98 if add_tags then
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
99 local tags = {};
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
100 for tag in add_tags do
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
101 table.insert(tags, tag);
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
102 end
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
103 html_template = html_template:gsub("</head>", table.concat(tags, "\n"):gsub("%%", "%%").."\n</head>");
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
104 end
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
105
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 module:provides("http", {
3337
b46bb9392efe mod_conversejs: Set a friendly name for mod_http_index
Kim Alvefur <zash@zash.se>
parents: 3333
diff changeset
107 title = "Converse.js";
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 route = {
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 GET = function (event)
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
110 local converse_options = get_converse_options();
3310
908b2bc05d26 mod_conversejs: Restore accidentally removed configuration option handling
Kim Alvefur <zash@zash.se>
parents: 3309
diff changeset
111
2919
0ea93da47db9 mod_conversejs: Allow passing arbitrary options trough to Converse.js
Kim Alvefur <zash@zash.se>
parents: 2694
diff changeset
112 event.response.headers.content_type = "text/html";
3312
e714be00aaad mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents: 3310
diff changeset
113 return html_template:format(js_template:format(json_encode(converse_options)));
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end;
3314
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
115
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
116 ["GET /prosody-converse.js"] = function (event)
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
117 local converse_options = get_converse_options();
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
118
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
119 event.response.headers.content_type = "application/javascript";
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
120 return js_template:format(json_encode(converse_options));
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
121 end;
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 }
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 });
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124