mod_jsxc/mod_jsxc.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 24 Sep 2022 09:26:26 +0100
changeset 5063 5f1120c284c5
parent 4980 75b6e5df65f9
permissions -rw-r--r--
mod_cloud_notify_extensions: Add note about dependency Noting here because people might not click through to see it on the mod_cloud_notify_encrypted page.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
     1
-- mod_jsxc
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
     2
-- Copyright (C) 2021 Kim Alvefur
2661
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;
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
diff changeset
     6
local render = require "util.interpolation".new("%b{}", xml_escape, { json = json_encode });
2661
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
3333
43d0e298ddda mod_conversejs: Explicitly depend on mod_http
Kim Alvefur <zash@zash.se>
parents: 3328
diff changeset
     8
module:depends"http";
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
     9
module:depends"bosh";
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    10
module:depends"http_libjs";
2661
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    12
local jquery_url = module:get_option_string("jquery_url", "/share/jquery/jquery.min.js");
2661
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    14
local cdn_url = module:get_option_string("jsxc_cdn", "");
3498
4feab7e87675 mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 3496
diff changeset
    15
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    16
local version = module:get_option_string("jsxc_version", "");
3351
823156d5885b mod_conversejs: Strip extra slash if version is set to the empty string
Kim Alvefur <zash@zash.se>
parents: 3341
diff changeset
    17
if version ~= "" then version = "/" .. version end
4151
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    18
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    19
local serve_dist = nil;
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    20
local resources = module:get_option_path("jsxc_resources");
4151
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    21
if resources then
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    22
	local serve;
4980
75b6e5df65f9 various: Improve error reporting if missing file server module on 0.12
Kim Alvefur <zash@zash.se>
parents: 4829
diff changeset
    23
	if prosody.process_type == "prosody" then
4151
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    24
		-- Prosody >= trunk / 0.12
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
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: 4051
diff changeset
    26
		serve = http_files.serve;
4980
75b6e5df65f9 various: Improve error reporting if missing file server module on 0.12
Kim Alvefur <zash@zash.se>
parents: 4829
diff changeset
    27
	else
4151
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    28
		-- Prosody <= 0.11
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
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: 4051
diff changeset
    30
	end
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
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: 4213
diff changeset
    32
	serve_dist = serve({ path = resources, mime_map = mime_map });
4151
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    33
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
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: 4051
diff changeset
    35
end
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
    36
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
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: 4213
diff changeset
    38
local css_url = module:get_option_string("jsxc_css", cdn_url..version.."/dist/styles/jsxc.bundle.css");
3335
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3333
diff changeset
    39
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    40
local html_template;
2661
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    42
do
4169
6b2a1c9ef6e2 mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents: 4157
diff changeset
    43
	local template_filename = module:get_option_string(module.name .. "_html_template", "templates/template.html");
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
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: 3498
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: 3498
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: 3498
diff changeset
    48
	end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
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: 3498
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: 3498
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: 3498
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: 3498
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: 3498
diff changeset
    55
	end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    56
end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    57
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
diff changeset
    59
do
4169
6b2a1c9ef6e2 mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents: 4157
diff changeset
    60
	local template_filename = module:get_option_string(module.name .. "_js_template", "templates/template.js");
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
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: 3498
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: 3498
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: 3498
diff changeset
    65
	end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
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: 3498
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: 3498
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: 3498
diff changeset
    70
	end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    71
end
3316
e714be00aaad mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents: 3314
diff changeset
    72
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
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: 4213
diff changeset
    74
	return { xmpp = { url = module:http_url("bosh", "/http-bind"), domain = module.host } };
3317
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3316
diff changeset
    75
end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3316
diff changeset
    76
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    77
local add_tags = module:get_option_array("jsxc_tags", {});
3337
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3336
diff changeset
    78
2661
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", {
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    80
	title = "jsxc.js";
2661
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)
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    83
			local jsxc_options = get_jsxc_options();
3314
908b2bc05d26 mod_conversejs: Restore accidentally removed configuration option handling
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
    84
2923
0ea93da47db9 mod_conversejs: Allow passing arbitrary options trough to Converse.js
Kim Alvefur <zash@zash.se>
parents: 2698
diff changeset
    85
			event.response.headers.content_type = "text/html";
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    86
			return render(html_template, {
3603
42fa833169bb mod_conversejs: Make title configurable (fixes #1362)
Kim Alvefur <zash@zash.se>
parents: 3602
diff changeset
    87
					service_name = module:get_option_string("name");
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    88
					header_scripts = { jquery_url, js_url };
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
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: 3498
diff changeset
    90
					header_tags = add_tags;
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    91
					jsxcjs = {
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    92
						options = jsxc_options;
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
    93
						startup = { script = js_template:format(json_encode(jsxc_options)); }
3602
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    94
					};
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3498
diff changeset
    95
				});
2661
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
		end;
3318
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3317
diff changeset
    97
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
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: 4213
diff changeset
    99
			local jsxc_options = get_jsxc_options();
3318
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3317
diff changeset
   100
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3317
diff changeset
   101
			event.response.headers.content_type = "application/javascript";
4829
4bdfd83e091f mod_jsxc: Demo module serving JSXC relatively easily from Prosody
Kim Alvefur <zash@zash.se>
parents: 4213
diff changeset
   102
			return js_template:format(json_encode(jsxc_options));
3318
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3317
diff changeset
   103
		end;
4151
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4051
diff changeset
   104
		["GET /dist/*"] = serve_dist;
2661
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