Software / code / prosody-modules
Annotate
mod_jsxc/mod_jsxc.lua @ 6319:63ef69b2f046
mod_http_oauth2: Assume Prosody 13.0+ roles are available
Per the README, 0.12 is not supported, so we should not need to worry
about this. Plus it is assumed to be present elsewhere and that would
throw errors.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 02 Jul 2025 16:15:32 +0200 |
| parent | 6228:b74ec363de08 |
| rev | line source |
|---|---|
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
1 -- mod_jsxc |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
2 -- Copyright (C) 2021 Kim Alvefur |
|
2657
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; |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
5 local xml_escape = require "util.stanza".xml_escape; |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
6 local render = require "util.interpolation".new("%b{}", xml_escape, { json = json_encode }); |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
|
3329
43d0e298ddda
mod_conversejs: Explicitly depend on mod_http
Kim Alvefur <zash@zash.se>
parents:
3324
diff
changeset
|
8 module:depends"http"; |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
9 module:depends"bosh"; |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
10 module:depends"http_libjs"; |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
12 local jquery_url = module:get_option_string("jquery_url", "/share/jquery/jquery.min.js"); |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
14 local cdn_url = module:get_option_string("jsxc_cdn", ""); |
|
3494
4feab7e87675
mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents:
3492
diff
changeset
|
15 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
16 local version = module:get_option_string("jsxc_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
|
17 if version ~= "" then version = "/" .. version end |
|
4147
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
18 |
|
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
19 local serve_dist = nil; |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
20 local resources = module:get_option_path("jsxc_resources"); |
|
4147
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
21 if resources then |
|
6228
b74ec363de08
mod_jsxc: Remove compatibility with 0.11
Link Mauve <linkmauve@linkmauve.fr>
parents:
4976
diff
changeset
|
22 local http_files = require "net.http.files"; |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
23 local mime_map = module:shared("/*/http_files/mime").types or { css = "text/css", js = "application/javascript" }; |
|
6228
b74ec363de08
mod_jsxc: Remove compatibility with 0.11
Link Mauve <linkmauve@linkmauve.fr>
parents:
4976
diff
changeset
|
24 serve_dist = http_files.serve({ path = resources, mime_map = mime_map }); |
|
4147
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
25 |
|
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
26 cdn_url = module:http_url(); |
|
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
27 end |
|
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
28 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
29 local js_url = module:get_option_string("jsxc_script", cdn_url..version.."/dist/jsxc.bundle.js"); |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
30 local css_url = module:get_option_string("jsxc_css", cdn_url..version.."/dist/styles/jsxc.bundle.css"); |
|
3331
d98341bca458
mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents:
3329
diff
changeset
|
31 |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
32 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
|
33 |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
34 do |
|
4165
6b2a1c9ef6e2
mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents:
4153
diff
changeset
|
35 local template_filename = module:get_option_string(module.name .. "_html_template", "templates/template.html"); |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
36 local template_file, err = module:load_resource(template_filename); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
37 if template_file then |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
38 html_template, err = template_file:read("*a"); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
39 template_file:close(); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
40 end |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
41 if not html_template then |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
42 module:log("error", "Error loading HTML template: %s", err); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
43 html_template = render("<h1>mod_{module} could not read the template</h1>\ |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
44 <p>Tried to open <b>{filename}</b></p>\ |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
45 <pre>{error}</pre>", |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
46 { module = module.name, filename = template_filename, error = err }); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
47 end |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
48 end |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
49 |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
50 local js_template; |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
51 do |
|
4165
6b2a1c9ef6e2
mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents:
4153
diff
changeset
|
52 local template_filename = module:get_option_string(module.name .. "_js_template", "templates/template.js"); |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
53 local template_file, err = module:load_resource(template_filename); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
54 if template_file then |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
55 js_template, err = template_file:read("*a"); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
56 template_file:close(); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
57 end |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
58 if not js_template then |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
59 module:log("error", "Error loading JS template: %s", err); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
60 js_template = render("console.log(\"mod_{module} could not read the JS template: %s\", {error|json})", |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
61 { module = module.name, filename = template_filename, error = err }); |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
62 end |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
63 end |
|
3312
e714be00aaad
mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents:
3310
diff
changeset
|
64 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
65 local function get_jsxc_options() |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
66 return { xmpp = { url = module:http_url("bosh", "/http-bind"), domain = module.host } }; |
|
3313
d6b922191aeb
mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents:
3312
diff
changeset
|
67 end |
|
d6b922191aeb
mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents:
3312
diff
changeset
|
68 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
69 local add_tags = module:get_option_array("jsxc_tags", {}); |
|
3333
5be90562e14b
mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents:
3332
diff
changeset
|
70 |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 module:provides("http", { |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
72 title = "jsxc.js"; |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 route = { |
|
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 GET = function (event) |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
75 local jsxc_options = get_jsxc_options(); |
|
3310
908b2bc05d26
mod_conversejs: Restore accidentally removed configuration option handling
Kim Alvefur <zash@zash.se>
parents:
3309
diff
changeset
|
76 |
|
2919
0ea93da47db9
mod_conversejs: Allow passing arbitrary options trough to Converse.js
Kim Alvefur <zash@zash.se>
parents:
2694
diff
changeset
|
77 event.response.headers.content_type = "text/html"; |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
78 return render(html_template, { |
|
3599
42fa833169bb
mod_conversejs: Make title configurable (fixes #1362)
Kim Alvefur <zash@zash.se>
parents:
3598
diff
changeset
|
79 service_name = module:get_option_string("name"); |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
80 header_scripts = { jquery_url, js_url }; |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
81 header_style = { css_url }; |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
82 header_tags = add_tags; |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
83 jsxcjs = { |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
84 options = jsxc_options; |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
85 startup = { script = js_template:format(json_encode(jsxc_options)); } |
|
3598
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
86 }; |
|
1921ae4449b8
mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents:
3494
diff
changeset
|
87 }); |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 end; |
|
3314
ab67f222d88b
mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents:
3313
diff
changeset
|
89 |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
90 ["GET /prosody-jsxc.js"] = function (event) |
|
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
91 local jsxc_options = get_jsxc_options(); |
|
3314
ab67f222d88b
mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents:
3313
diff
changeset
|
92 |
|
ab67f222d88b
mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents:
3313
diff
changeset
|
93 event.response.headers.content_type = "application/javascript"; |
|
4825
4bdfd83e091f
mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents:
4209
diff
changeset
|
94 return js_template:format(json_encode(jsxc_options)); |
|
3314
ab67f222d88b
mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents:
3313
diff
changeset
|
95 end; |
|
4147
3a06dea21ea1
mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents:
4047
diff
changeset
|
96 ["GET /dist/*"] = serve_dist; |
|
2657
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 } |
|
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 }); |
|
6f5c99c9f6cc
mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 |