Annotate

mod_jsxc/mod_jsxc.lua @ 4930:13070c6a7ce8

mod_http_muc_log: Fix exception on lack of trailing slash in room path A request to /room leads to the match call returning nil which in turn calls nodeprep(nil). In Prosody 0.11.x this does nothing and simply returns the nil, while in 0.12 it is an error. Now it redirects to the calendar view at /room/ - even for non-existant rooms. Discovered at a deployment with http_paths = { muc_log = "/" } and requests to /robots.txt and similar, which now result in a uses redirect before returning 404.
author Kim Alvefur <zash@zash.se>
date Fri, 22 Apr 2022 14:29:32 +0200
parent 4825:4bdfd83e091f
child 4976:75b6e5df65f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
22 local serve;
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
23 if not pcall(function()
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
24 -- Prosody >= trunk / 0.12
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
25 local http_files = require "net.http.files";
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
26 serve = http_files.serve;
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
27 end) then
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
28 -- Prosody <= 0.11
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
29 serve = module:depends "http_files".serve;
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
30 end
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
31 local mime_map = module:shared("/*/http_files/mime").types or { css = "text/css", js = "application/javascript" };
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
32 serve_dist = 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
33
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
34 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
35 end
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
36
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
37 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
38 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
39
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
40 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
41
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
42 do
4165
6b2a1c9ef6e2 mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents: 4153
diff changeset
43 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
44 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
45 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
46 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
47 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
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 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
50 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
51 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
52 <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
53 <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
54 { 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
55 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
56 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
57
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
58 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
59 do
4165
6b2a1c9ef6e2 mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents: 4153
diff changeset
60 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
61 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
62 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
63 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
64 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
65 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
66 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
67 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
68 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
69 { 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
70 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
71 end
3312
e714be00aaad mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents: 3310
diff changeset
72
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
73 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
74 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
75 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
76
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
77 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
78
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 module:provides("http", {
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
80 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
81 route = {
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 GET = function (event)
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
83 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
84
2919
0ea93da47db9 mod_conversejs: Allow passing arbitrary options trough to Converse.js
Kim Alvefur <zash@zash.se>
parents: 2694
diff changeset
85 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
86 return render(html_template, {
3599
42fa833169bb mod_conversejs: Make title configurable (fixes #1362)
Kim Alvefur <zash@zash.se>
parents: 3598
diff changeset
87 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
88 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
89 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
90 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
91 jsxcjs = {
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
92 options = jsxc_options;
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
93 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
94 };
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
95 });
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end;
3314
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
97
4825
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4209
diff changeset
98 ["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
99 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
100
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
101 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
102 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
103 end;
4147
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
104 ["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
105 }
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 });
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107