Mercurial > prosody-modules
annotate mod_invites_register/mod_invites_register.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 | 4aecf78cc39d |
| children |
| rev | line source |
|---|---|
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local st = require "util.stanza"; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local jid_split = require "util.jid".split; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local jid_bare = require "util.jid".bare; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local rostermanager = require "core.rostermanager"; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local require_encryption = module:get_option_boolean("c2s_require_encryption", |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 module:get_option_boolean("require_encryption", false)); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local invite_only = module:get_option_boolean("registration_invite_only", true); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local invites; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 if prosody.shutdown then -- COMPAT hack to detect prosodyctl |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 invites = module:depends("invites"); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
|
4673
eee7ef8eb0a2
mod_invites_register: advertise new stream feature from XEP-0445
Matthew Wild <mwild1@gmail.com>
parents:
4599
diff
changeset
|
15 local legacy_invite_stream_feature = st.stanza("register", { xmlns = "urn:xmpp:invite" }):up(); |
|
eee7ef8eb0a2
mod_invites_register: advertise new stream feature from XEP-0445
Matthew Wild <mwild1@gmail.com>
parents:
4599
diff
changeset
|
16 local invite_stream_feature = st.stanza("register", { xmlns = "urn:xmpp:ibr-token:0" }):up(); |
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 module:hook("stream-features", function(event) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local session, features = event.origin, event.features; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 -- Advertise to unauthorized clients only. |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 if session.type ~= "c2s_unauthed" or (require_encryption and not session.secure) then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
|
4673
eee7ef8eb0a2
mod_invites_register: advertise new stream feature from XEP-0445
Matthew Wild <mwild1@gmail.com>
parents:
4599
diff
changeset
|
25 features:add_child(legacy_invite_stream_feature); |
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 features:add_child(invite_stream_feature); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 -- XEP-0379: Pre-Authenticated Roster Subscription |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 module:hook("presence/bare", function (event) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local stanza = event.stanza; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if stanza.attr.type ~= "subscribe" then return end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local preauth = stanza:get_child("preauth", "urn:xmpp:pars:0"); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 if not preauth then return end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local token = preauth.attr.token; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 if not token then return end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local username, host = jid_split(stanza.attr.to); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 local invite, err = invites.get(token, username); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if not invite then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 module:log("debug", "Got invalid token, error: %s", err); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local contact = jid_bare(stanza.attr.from); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 module:log("debug", "Approving inbound subscription to %s from %s", username, contact); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if rostermanager.set_contact_pending_in(username, host, contact, stanza) then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if rostermanager.subscribed(username, host, contact) then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 invite:use(); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 rostermanager.roster_push(username, host, contact); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 -- Send back a subscription request (goal is mutual subscription) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 if not rostermanager.is_user_subscribed(username, host, contact) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 and not rostermanager.is_contact_pending_out(username, host, contact) then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 module:log("debug", "Sending automatic subscription request to %s from %s", contact, username); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 if rostermanager.set_contact_pending_out(username, host, contact) then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 rostermanager.roster_push(username, host, contact); |
|
4599
06c9c9ef0a51
mod_invites_register: Fix missing 'from' attribute (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
4401
diff
changeset
|
62 module:send(st.presence({type = "subscribe", from = username.."@"..host, to = contact })); |
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 else |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 module:log("warn", "Failed to set contact pending out for %s", username); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end, 1); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 -- Client is submitting a preauth token to allow registration |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 module:hook("stanza/iq/urn:xmpp:pars:0:preauth", function(event) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 local preauth = event.stanza.tags[1]; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local token = preauth.attr.token; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local validated_invite = invites.get(token); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 if not validated_invite then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 local reply = st.error_reply(event.stanza, "cancel", "forbidden", "The invite token is invalid or expired"); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 event.origin.send(reply); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 return true; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 event.origin.validated_invite = validated_invite; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local reply = st.reply(event.stanza); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 event.origin.send(reply); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 return true; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 -- Registration attempt - ensure a valid preauth token has been supplied |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 module:hook("user-registering", function (event) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local validated_invite = event.validated_invite or (event.session and event.session.validated_invite); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 if invite_only and not validated_invite then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 event.allowed = false; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 event.reason = "Registration on this server is through invitation only"; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 return; |
|
4121
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
94 elseif not validated_invite then |
|
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
95 -- This registration is not using an invite, but |
|
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
96 -- the server is not in invite-only mode, so nothing |
|
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
97 -- for this module to do... |
|
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
98 return; |
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
|
4120
d3c7be9e36d9
mod_invites_register: Fix traceback on registration via other module (thanks franck)
Kim Alvefur <zash@zash.se>
parents:
4106
diff
changeset
|
100 if validated_invite and validated_invite.additional_data and validated_invite.additional_data.allow_reset then |
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 event.allow_reset = validated_invite.additional_data.allow_reset; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 end); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 -- Make a *one-way* subscription. User will see when contact is online, |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 -- contact will not see when user is online. |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 function subscribe(host, user_username, contact_username) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 local user_jid = user_username.."@"..host; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 local contact_jid = contact_username.."@"..host; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 -- Update user's roster to say subscription request is pending... |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 rostermanager.set_contact_pending_out(user_username, host, contact_jid); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 -- Update contact's roster to say subscription request is pending... |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 rostermanager.set_contact_pending_in(contact_username, host, user_jid); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 -- Update contact's roster to say subscription request approved... |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 rostermanager.subscribed(contact_username, host, user_jid); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 -- Update user's roster to say subscription request approved... |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 rostermanager.process_inbound_subscription_approval(user_username, host, contact_jid); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 -- Make a mutual subscription between jid1 and jid2. Each JID will see |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 -- when the other one is online. |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 function subscribe_both(host, user1, user2) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 subscribe(host, user1, user2); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 subscribe(host, user2, user1); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 -- Registration successful, if there was a preauth token, mark it as used |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 module:hook("user-registered", function (event) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 local validated_invite = event.validated_invite or (event.session and event.session.validated_invite); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 if not validated_invite then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 return; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 local inviter_username = validated_invite.inviter; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 local contact_username = event.username; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 validated_invite:use(); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 if inviter_username then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 module:log("debug", "Creating mutual subscription between %s and %s", inviter_username, contact_username); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 subscribe_both(module.host, inviter_username, contact_username); |
|
4907
4aecf78cc39d
mod_invites_register: Push invitee contact entry to inviter (Thanks gerald)
Kim Alvefur <zash@zash.se>
parents:
4673
diff
changeset
|
140 rostermanager.roster_push(inviter_username, module.host, contact_username.."@"..module.host); |
|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 if validated_invite.additional_data then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 module:log("debug", "Importing roles from invite"); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 local roles = validated_invite.additional_data.roles; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 if roles then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 module:open_store("roles"):set(contact_username, roles); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 -- Equivalent of user-registered but for when the account already existed |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 -- (i.e. password reset) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 module:hook("user-password-reset", function (event) |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 local validated_invite = event.validated_invite or (event.session and event.session.validated_invite); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 if not validated_invite then |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 return; |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 end |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 validated_invite:use(); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 end); |
|
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 |
