annotate mod_conversejs/mod_conversejs.lua @ 6299:5cf5ee23b361

mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs. diff --git a/mod_voipms/README.md b/mod_voipms/README.md new file mode 100644 --- /dev/null +++ b/mod_voipms/README.md @@ -0,0 +1,51 @@ +--- +labels: +- 'Stage-Alpha' +- 'Type-Web' +summary: Send and receive SMS/MMS via VoIP.ms APIs. +rockspec: + build: + modules: + mod_voipms: mod_voipms.lua +... + +Introduction +============ + +This is a Prosody module to map JIDs to DIDs on VoIP.ms and support sending/receiving SMS/MMS. + +Configuration +============= + +| option | type | default | +|-----------------------|--------|---------| +| voipms\_api\_username | string | nil | +| voipms\_api\_password | string | nil | +| voipms\_query\_key | string | nil | +| voipms\_jid\_map | table | nil | +``` +VirtualHost "sms.example.com" +modules_enabled = { + "voipms"; +} +voipms_api_username = john@example.com -- E-mail registered with VoIP.ms +voipms_api_password = abcd1234 -- API password configured in VoIP.ms +voipms_query_key = some_query_key -- query param 'key' part of your URL callback +voipms_jid_map = { + ["your_jid@your_domain.com"] = "+1234567890" +} +``` + +HTTP +==== + +The module is served on Prosody's default HTTP ports at the path /voipms. More details on configuring HTTP modules in Prosody can be found in the HTTP documentation. + +VoIP.ms Webhook URL +=================== + +This module receives the VoIP.ms Webhook URL (POST) at the /voipms endpoint. It uses the sendSMS/sendMMS GET methods against the VoIP.ms APIs. This is an example webhook to use in VoIP.ms: + +``` +https://sms.example.com/voipms?key=some_query_key +``` diff --git a/mod_voipms/mod_voipms.lua b/mod_voipms/mod_voipms.lua new file mode 100644 --- /dev/null +++ b/mod_voipms/mod_voipms.lua @@ -0,0 +1,166 @@ +local http = require "net.http" +local json = require "util.json" +local st = require "util.stanza" + +local api_username = module:get_option_string("voipms_api_username") +local api_password = module:get_option_string("voipms_api_password") +local query_key = module:get_option("voipms_query_key") +local jid_map = module:get_option("voipms_jid_map") or {} +local rest_endpoint = "https://voip.ms/api/v1/rest.php" + +if not api_username or not api_password or not query_key then + module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)") + return +end + +module:depends("http") + +local function normalize_number(num) + if not num then return nil end + if num:sub(1, 1) ~= "+" then + return "+1" .. num + end + return num +end + +local function extract_query_key(event) + return (event.request.url.query or ""):match("key=([^&]+)") +end + +module:provides("http", { + route = { + ["POST"] = function(event) + local req = event.request + local body = req.body or "" + + if extract_query_key(event) ~= query_key then + module:log("warn", "Unauthorized webhook: missing or invalid key") + return { status_code = 403 } + end + + local json_payload, err = json.decode(body) + if not json_payload then + module:log("warn", "Invalid JSON: %s", err or "unknown error") + return { status_code = 400 } + end + + local payload = json_payload.data and json_payload.data.payload + if not payload then + module:log("warn", "Missing payload in JSON") + return { status_code = 400 } + end + + local from = payload.from and payload.from.phone_number + local to_list = payload.to or {} + local to = #to_list > 0 and to_list[1].phone_number or nil + + if not from or not to then + module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to)) + return { status_code = 400 } + end + + local normalized_to = normalize_number(to) + local target_jid = nil + + for jid, did in pairs(jid_map) do + if normalize_number(did) == normalized_to then + target_jid = jid + break + end + end + + if not target_jid then + module:log("warn", "No JID mapping for DID %s", normalized_to) + return { status_code = 404 } + end + + local normalized_from = normalize_number(from) + local message_text = payload.text or "" + + local message = st.message({ + from = normalized_from .. "@" .. module.host, + to = target_jid, + type = "chat" + }):tag("body"):text(message_text):up() + + if payload.media and #payload.media > 0 then + for _, media_item in ipairs(payload.media) do + if media_item.url then + message_text = message_text .. "\n" .. media_item.url + message:tag("x", { xmlns = "jabber:x:oob" }) + :tag("url"):text(media_item.url):up():up() + end + end + end + + module:send(message) + module:log("info", "Delivered SMS from %s to %s", normalized_from, target_jid) + + return { status_code = 204 } + end + } +}) + +module:hook("message/bare", function(event) + local stanza = event.stanza + if stanza.attr.type ~= "chat" then return end + + local from_jid = stanza.attr.from + local to_jid = stanza.attr.to + local body = stanza:get_child_text("body") + if not body or body == "" then return end + + local from_number = jid_map[from_jid] + if not from_number then + module:log("warn", "No DID mapping for JID %s", from_jid) + return + end + + local to_number = to_jid:match("^([^@]+)") + if not to_number then + module:log("warn", "Malformed JID in message.to: %s", to_jid) + return + end + + to_number = normalize_number(to_number) + + local media_urls = {} + for line in body:gmatch("[^\r\n]+") do + if line:match("^https?://") then + table.insert(media_urls, line) + end + end + + local method = (#media_urls > 0) and "sendMMS" or "sendSMS" + local query = { + api_username = api_username, + api_password = api_password, + method = method, + did = from_number, + dst = to_number, + message = body + } + + if method == "sendMMS" then + for i, url in ipairs(media_urls) do + query["media_url[" .. (i - 1) .. "]"] = url + end + end + + local query_str = http.formencode(query) + + http.request(rest_endpoint .. "?" .. query_str, { + method = "GET"; + }, function(response_body, code) + if code == 200 then + local resp, err = json.decode(response_body) + if not resp or resp.status ~= "success" then + module:log("error", "Failed to send %s: %s", method, err or (resp and resp.status) or "unknown") + else + module:log("info", "Sent %s from %s to %s", method, from_number, to_number) + end + else + module:log("error", "HTTP error sending %s: code %s", method, tostring(code)) + end + end) +end)
author Chaz <menel@snikket.de>
date Mon, 21 Jul 2025 23:57:37 +0200
parents 30adcea825c3
children
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
4914
f2c918fe8cf0 mod_conversejs: Bump Copyright year
Kim Alvefur <zash@zash.se>
parents: 4891
diff changeset
2 -- Copyright (C) 2017-2022 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;
4176
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
6 local urlencode = require "util.http".urlencode;
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
7 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
8
3329
43d0e298ddda mod_conversejs: Explicitly depend on mod_http
Kim Alvefur <zash@zash.se>
parents: 3324
diff changeset
9 module:depends"http";
3363
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
10
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
11 local has_bosh = pcall(function ()
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
12 module:depends"bosh";
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
13 end);
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 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
16 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
17 end);
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
4891
99cdc7cde150 mod_conversejs: Revert back to depending on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 4855
diff changeset
19 pcall(function ()
99cdc7cde150 mod_conversejs: Revert back to depending on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 4855
diff changeset
20 module:depends("bookmarks");
99cdc7cde150 mod_conversejs: Revert back to depending on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 4855
diff changeset
21 end);
3494
4feab7e87675 mod_conversejs: Add dependency on mod_bookmarks
Kim Alvefur <zash@zash.se>
parents: 3492
diff changeset
22
3331
d98341bca458 mod_conversejs: Allow overriding CDN URL, or script/css URLs independently
Matthew Wild <mwild1@gmail.com>
parents: 3329
diff changeset
23 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
24
3348
f753cf4f7224 mod_conversejs: Default to latest version of Converse.js
Kim Alvefur <zash@zash.se>
parents: 3347
diff changeset
25 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
26 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
27
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
28 local serve_dist = nil;
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
29 local resources = module:get_option_path("conversejs_resources");
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
30 if resources then
6191
767b8594fb9d mod_conversejs: Remove compatibility with 0.11
Link Mauve <linkmauve@linkmauve.fr>
parents: 6171
diff changeset
31 local http_files = require "net.http.files";
4147
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
32 local mime_map = module:shared("/*/http_files/mime").types or {css = "text/css"; js = "application/javascript"};
6191
767b8594fb9d mod_conversejs: Remove compatibility with 0.11
Link Mauve <linkmauve@linkmauve.fr>
parents: 6171
diff changeset
33 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
34
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
35 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
36 end
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
37
3347
823156d5885b mod_conversejs: Strip extra slash if version is set to the empty string
Kim Alvefur <zash@zash.se>
parents: 3337
diff changeset
38 local js_url = module:get_option_string("conversejs_script", cdn_url..version.."/dist/converse.min.js");
3641
58b49d883f0c mod_conversejs: Change CSS URL
Kim Alvefur <zash@zash.se>
parents: 3599
diff changeset
39 local css_url = module:get_option_string("conversejs_css", cdn_url..version.."/dist/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
40
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
41 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
42
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
43 do
4165
6b2a1c9ef6e2 mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents: 4153
diff changeset
44 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
45 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
46 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
47 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
48 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
49 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
50 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
51 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
52 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
53 <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
54 <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
55 { 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
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 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
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
59 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
60 do
4165
6b2a1c9ef6e2 mod_conversejs: Move templates into a directory for easier install
Kim Alvefur <zash@zash.se>
parents: 4153
diff changeset
61 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
62 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
63 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
64 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
65 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
66 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
67 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
68 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
69 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
70 { 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
71 end
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
72 end
3312
e714be00aaad mod_conversejs: Factor JavaScript part out of HTML
Kim Alvefur <zash@zash.se>
parents: 3310
diff changeset
73
5211
079ca766193b mod_conversejs: This one weird trick updates options on reload
Kim Alvefur <zash@zash.se>
parents: 4976
diff changeset
74 local function get_converse_options()
079ca766193b mod_conversejs: This one weird trick updates options on reload
Kim Alvefur <zash@zash.se>
parents: 4976
diff changeset
75 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
76
6265
30adcea825c3 mod_conversejs: Fix hostname set as default username (thanks roughnecks)
Kim Alvefur <zash@zash.se>
parents: 6208
diff changeset
77 local authentication = module:get_option_string("authentication");
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
78 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
79 local converse_options = {
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
80 -- Auto-detected connection endpoints
3363
2681f74750b2 mod_conversejs: Weaken dependency on mod_bosh
Kim Alvefur <zash@zash.se>
parents: 3348
diff changeset
81 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
82 websocket_url = has_ws and module:http_url("websocket","xmpp-websocket"):gsub("^http", "ws") or nil;
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
83 -- Since we provide those, XEP-0156 based auto-discovery should not be used
4047
36b6e3e3f9e2 mod_conversejs: Disable automatic BOSH/WS endpoint discovery
Kim Alvefur <zash@zash.se>
parents: 3737
diff changeset
84 discover_connection_methods = false;
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
85 -- Authentication mode to use (normal or guest login)
6265
30adcea825c3 mod_conversejs: Fix hostname set as default username (thanks roughnecks)
Kim Alvefur <zash@zash.se>
parents: 6208
diff changeset
86 authentication = authentication == "anonymous" and "anonymous" or "login";
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
87 -- Host to connect to for anonymous access
6265
30adcea825c3 mod_conversejs: Fix hostname set as default username (thanks roughnecks)
Kim Alvefur <zash@zash.se>
parents: 6208
diff changeset
88 jid = authentication == "anonymous" and module.host or nil;
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
89 -- Let users login with only username
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
90 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
91 domain_placeholder = module.host;
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
92 -- If registration is enabled
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
93 allow_registration = allow_registration;
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
94 -- and if it is, which domain to register with
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
95 registration_domain = allow_registration and module.host or nil;
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
96 -- Path to resources like emoji, icons, sounds
4153
4ee2a90d3818 mod_conversejs: Generate 'assets_path' to fix locating certain resources
Kim Alvefur <zash@zash.se>
parents: 4147
diff changeset
97 assets_path = cdn_url..version.."/dist/";
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
98 -- Default most suited for use as a "normal" client
3737
49e65a7e9415 mod_conversejs: Use the fullscreen view mode by default
Kim Alvefur <zash@zash.se>
parents: 3641
diff changeset
99 view_mode = "fullscreen";
3313
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
100 };
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
101
4209
37aa50ed79c1 mod_conversejs: Add comments about default settings
Kim Alvefur <zash@zash.se>
parents: 4176
diff changeset
102 -- Let config override the above defaults
3332
4fdd8b77da54 mod_conversejs: Variable rename for clarity (user may override options)
Matthew Wild <mwild1@gmail.com>
parents: 3331
diff changeset
103 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
104 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
105 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
106 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
107 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
108
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
109 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
110 end
d6b922191aeb mod_conversejs: Factor out option handling into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 3312
diff changeset
111
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
112 local add_tags = module:get_option_array("conversejs_tags", {});
3333
5be90562e14b mod_conversejs: Allow custom tags to be inserted into the generated HTML
Matthew Wild <mwild1@gmail.com>
parents: 3332
diff changeset
113
6208
303fcfe3a7e8 mod_conversejs: Make PWA options match documentation
Kim Alvefur <zash@zash.se>
parents: 6191
diff changeset
114 local service_name = module:get_option_string("conversejs_name", module:get_option_string("name", "Prosody IM and Converse.js"));
303fcfe3a7e8 mod_conversejs: Make PWA options match documentation
Kim Alvefur <zash@zash.se>
parents: 6191
diff changeset
115 local service_short_name = module:get_option_string("conversejs_short_name", "Converse");
303fcfe3a7e8 mod_conversejs: Make PWA options match documentation
Kim Alvefur <zash@zash.se>
parents: 6191
diff changeset
116 local service_description = module:get_option_string("conversejs_description", "Messaging Freedom")
303fcfe3a7e8 mod_conversejs: Make PWA options match documentation
Kim Alvefur <zash@zash.se>
parents: 6191
diff changeset
117 local pwa_color = module:get_option_string("conversejs_pwa_color", "#397491")
5871
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
118
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 module:provides("http", {
3337
b46bb9392efe mod_conversejs: Set a friendly name for mod_http_index
Kim Alvefur <zash@zash.se>
parents: 3333
diff changeset
120 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
121 route = {
6171
fe8222112cf4 mod_conversejs: Serve base app at /
Matthew Wild <mwild1@gmail.com>
parents: 5871
diff changeset
122 ["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
123 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
124
2919
0ea93da47db9 mod_conversejs: Allow passing arbitrary options trough to Converse.js
Kim Alvefur <zash@zash.se>
parents: 2694
diff changeset
125 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
126 return render(html_template, {
5871
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
127 service_name = service_name;
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
128 -- note that using a relative path won’t work as this URL doesn’t end in a /
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
129 manifest_url = module:http_url().."/manifest.json",
3598
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
130 header_scripts = { js_url };
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
131 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
132 header_tags = add_tags;
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
133 conversejs = {
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
134 options = converse_options;
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
135 startup = { script = js_template:format(json_encode(converse_options)); }
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
136 };
1921ae4449b8 mod_conversejs: Separate out templates into separate configurable files (breaks 0.9 compat)
Kim Alvefur <zash@zash.se>
parents: 3494
diff changeset
137 });
2657
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 end;
3314
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
139
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
140 ["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
141 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
142
ab67f222d88b mod_conversejs: Add an endpoint returning only initialization snippet
Kim Alvefur <zash@zash.se>
parents: 3313
diff changeset
143 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
144 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
145 end;
5871
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
146 ["GET /manifest.json"] = function (event)
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
147 -- See manifest.json in the root of Converse.js’s git repository
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
148 local data = {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
149 short_name = service_short_name,
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
150 name = service_name,
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
151 description = service_description,
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
152 categories = {"social"},
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
153 icons = module:get_option_array("manifest_icons", {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
154 {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
155 src = cdn_url..version.."/dist/images/logo/conversejs-filled-512.png",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
156 sizes = "512x512",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
157 },
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
158 {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
159 src = cdn_url..version.."/dist/images/logo/conversejs-filled-192.png",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
160 sizes = "192x192",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
161 },
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
162 {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
163 src = cdn_url..version.."/dist/images/logo/conversejs-filled-192.svg",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
164 sizes = "192x192",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
165 },
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
166 {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
167 src = cdn_url..version.."/dist/images/logo/conversejs-filled-512.svg",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
168 sizes = "512x512",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
169 },
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
170 }),
6171
fe8222112cf4 mod_conversejs: Serve base app at /
Matthew Wild <mwild1@gmail.com>
parents: 5871
diff changeset
171 start_url = module:http_url().."/",
5871
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
172 background_color = pwa_color,
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
173 display = "standalone",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
174 scope = module:http_url().."/",
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
175 theme_color = pwa_color,
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
176 }
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
177 return {
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
178 headers = { content_type = "application/schema+json" },
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
179 body = json_encode(data),
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
180 }
1c8197075d04 mod_conversejs: Allow installation as PWA
BetaRays <BetaRays@proton.me>
parents: 5211
diff changeset
181 end;
4147
3a06dea21ea1 mod_conversejs: Enable serving resources from built-in http server
Kim Alvefur <zash@zash.se>
parents: 4047
diff changeset
182 ["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
183 }
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 });
6f5c99c9f6cc mod_conversejs: Simple demo module for serving converse.js from internal http server
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185
4176
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
186 module:provides("site-app", {
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
187 name = "Converse.js";
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
188 text = [[A free and open-source XMPP chat client in your browser]];
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
189 image = "assets/logos/converse-js.svg";
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
190 link = "https://conversejs.org/";
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
191 magic_link_format = "/register?t={invite.token}&c=converse-js";
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
192 login_link_format = module:http_url();
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
193 platforms = { "Web" };
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
194 download = {
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
195 buttons = {
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
196 {
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
197 text = "Open web chat";
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
198 url = module:http_url();
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
199 };
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
200 };
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
201 };
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
202
0016618e0975 mod_conversejs: Automatically register as a site app (see mod_register_apps)
Matthew Wild <mwild1@gmail.com>
parents: 4165
diff changeset
203 });