Mercurial > prosody-modules
annotate mod_voipms/mod_voipms.lua @ 6556:7477e97a9045
mod_firewall: Apply pre-reload state before re-reading config
This change makes load/reload a bit more robust. module.load() runs before
module.restore() and it reads from the config and updates the state (if
needed).
However, after this, module.restore() could run and apply the old state again.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 24 May 2026 20:03:20 +0100 |
| parents | fa976338928c |
| children |
| rev | line source |
|---|---|
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
1 local http = require "net.http" |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
2 local json = require "util.json" |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
3 local st = require "util.stanza" |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
4 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
5 local api_username = module:get_option_string("voipms_api_username") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
6 local api_password = module:get_option_string("voipms_api_password") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
7 local query_key = module:get_option("voipms_query_key") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
8 local jid_map = module:get_option("voipms_jid_map") or {} |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
9 local rest_endpoint = "https://voip.ms/api/v1/rest.php" |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
10 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
11 if not api_username or not api_password or not query_key then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
12 module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
13 return |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
14 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
15 |
| 6300 | 16 for jid, dids in pairs(jid_map) do |
| 17 if type(dids) ~= "table" then | |
| 18 module:log("error", "Invalid voipms_jid_map entry for %s: must be a list of DIDs", jid) | |
| 19 return | |
| 20 end | |
| 21 end | |
| 22 | |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
23 module:depends("http") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
24 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
25 local function normalize_number(num) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
26 if not num then return nil end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
27 if num:sub(1, 1) ~= "+" then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
28 return "+1" .. num |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
29 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
30 return num |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
31 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
32 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
33 local function extract_query_key(event) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
34 return (event.request.url.query or ""):match("key=([^&]+)") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
35 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
36 |
|
6341
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
37 local function send_message(query, stanza) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
38 local query_str = http.formencode(query) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
39 |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
40 http.request(rest_endpoint .. "?" .. query_str, { |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
41 method = "GET"; |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
42 }, function(response_body, code) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
43 if code == 200 then |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
44 local resp, err = json.decode(response_body) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
45 if not resp or resp.status ~= "success" then |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
46 local err_msg = string.format("Failed to send %s: %s", query.method, err or (resp and resp.status) or "unknown") |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
47 module:log("error", "%s", err_msg) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
48 local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
49 module:send(err_reply) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
50 else |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
51 module:log("debug", "Sent %s from %s to %s", query.method, query.did, query.dst) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
52 end |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
53 else |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
54 local err_msg = string.format("HTTP error sending %s: code %s", method, tostring(code)) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
55 module:log("error", "%s", err_msg) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
56 local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
57 module:send(err_reply) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
58 end |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
59 end) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
60 end |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
61 |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
62 module:provides("http", { |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
63 route = { |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
64 ["POST"] = function(event) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
65 local req = event.request |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
66 local body = req.body or "" |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
67 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
68 if extract_query_key(event) ~= query_key then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
69 module:log("warn", "Unauthorized webhook: missing or invalid key") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
70 return { status_code = 403 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
71 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
72 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
73 local json_payload, err = json.decode(body) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
74 if not json_payload then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
75 module:log("warn", "Invalid JSON: %s", err or "unknown error") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
76 return { status_code = 400 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
77 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
78 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
79 local payload = json_payload.data and json_payload.data.payload |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
80 if not payload then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
81 module:log("warn", "Missing payload in JSON") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
82 return { status_code = 400 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
83 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
84 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
85 local from = payload.from and payload.from.phone_number |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
86 local to_list = payload.to or {} |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
87 local to = #to_list > 0 and to_list[1].phone_number or nil |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
88 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
89 if not from or not to then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
90 module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to)) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
91 return { status_code = 400 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
92 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
93 |
| 6300 | 94 local normalized_from = normalize_number(from) |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
95 local normalized_to = normalize_number(to) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
96 local target_jid = nil |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
97 |
| 6300 | 98 for jid, dids in pairs(jid_map) do |
| 99 for _, did in ipairs(dids) do | |
| 100 if normalize_number(did) == normalized_to then | |
| 101 target_jid = jid | |
| 102 break | |
| 103 end | |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
104 end |
| 6300 | 105 if target_jid then break end |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
106 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
107 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
108 if not target_jid then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
109 module:log("warn", "No JID mapping for DID %s", normalized_to) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
110 return { status_code = 404 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
111 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
112 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
113 local message_text = payload.text or "" |
| 6300 | 114 local from_jid = normalized_from .. "%" .. normalized_to .. "@" .. module.host |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
115 |
| 6300 | 116 if message_text ~= "" then |
| 117 local text_message = st.message({ | |
| 118 from = from_jid, | |
| 119 to = target_jid, | |
| 120 type = "chat" | |
| 121 }):tag("body"):text(message_text):up() | |
| 122 | |
| 123 module:send(text_message) | |
|
6341
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
124 module:log("debug", "Delivered text SMS from %s to %s", normalized_from, target_jid) |
| 6300 | 125 end |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
126 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
127 if payload.media and #payload.media > 0 then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
128 for _, media_item in ipairs(payload.media) do |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
129 if media_item.url then |
| 6300 | 130 local media_message = st.message({ |
| 131 from = from_jid, | |
| 132 to = target_jid, | |
| 133 type = "chat" | |
| 134 }) | |
| 135 :tag("body"):text(media_item.url):up() | |
| 136 :tag("x", { xmlns = "jabber:x:oob" }) | |
| 137 :tag("url"):text(media_item.url):up() | |
| 138 :up() | |
| 139 | |
| 140 module:send(media_message) | |
|
6341
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
141 module:log("debug", "Delivered media URL from %s to %s: %s", normalized_from, target_jid, media_item.url) |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
142 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
143 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
144 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
145 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
146 return { status_code = 204 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
147 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
148 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
149 }) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
150 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
151 module:hook("message/bare", function(event) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
152 local stanza = event.stanza |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
153 if stanza.attr.type ~= "chat" then return end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
154 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
155 local from_jid = stanza.attr.from |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
156 local to_jid = stanza.attr.to |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
157 local body = stanza:get_child_text("body") |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
158 if not body or body == "" then return end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
159 |
| 6300 | 160 local to_node = to_jid:match("^(.-)@") |
| 161 if not to_node then | |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
162 module:log("warn", "Malformed JID in message.to: %s", to_jid) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
163 return |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
164 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
165 |
| 6300 | 166 local dst_number, from_number = to_node:match("([^%%]+)%%(.+)") |
| 167 if not dst_number or not from_number then | |
| 168 module:log("warn", "Malformed to JID node: %s", to_node) | |
| 169 return | |
| 170 end | |
| 171 | |
| 172 dst_number = normalize_number(dst_number) | |
| 173 from_number = normalize_number(from_number) | |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
174 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
175 local media_urls = {} |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
176 for line in body:gmatch("[^\r\n]+") do |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
177 if line:match("^https?://") then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
178 table.insert(media_urls, line) |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
179 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
180 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
181 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
182 local method = (#media_urls > 0) and "sendMMS" or "sendSMS" |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
183 local query = { |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
184 api_username = api_username, |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
185 api_password = api_password, |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
186 method = method, |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
187 did = from_number, |
| 6300 | 188 dst = dst_number, |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
189 message = body |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
190 } |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
191 |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
192 if method == "sendMMS" then |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
193 for i, url in ipairs(media_urls) do |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
194 query["media_url[" .. (i - 1) .. "]"] = url |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
195 end |
|
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
196 |
|
6341
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
197 send_message(query, stanza) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
198 else |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
199 for i = 1, #body, 160 do |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
200 local chunked_query = { |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
201 api_username = query.api_username, |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
202 api_password = query.api_password, |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
203 method = method, |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
204 did = from_number, |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
205 dst = dst_number, |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
206 message = body:sub(i, i + 159) |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
207 } |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
208 |
|
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
209 send_message(chunked_query, stanza) |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
210 end |
|
6341
fa976338928c
mod_voipms: Chunk messages larger than 160 characters as per the API
Chaz Kettleson <chaz@pyr3x.com>
parents:
6316
diff
changeset
|
211 end |
|
6307
c194bec4b56d
mod_voipms: Fix bug from not returning in message/bare hook
Chaz Kettleson <chaz@pyr3x.com>
parents:
6300
diff
changeset
|
212 |
|
c194bec4b56d
mod_voipms: Fix bug from not returning in message/bare hook
Chaz Kettleson <chaz@pyr3x.com>
parents:
6300
diff
changeset
|
213 return true |
|
6299
5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
Chaz <menel@snikket.de>
parents:
diff
changeset
|
214 end) |
