Mercurial > prosody-modules
comparison mod_sms_free/mod_sms_free.lua @ 3695:5a70dd2349a7
mod_sms_free: New module!
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 06 Oct 2019 17:29:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3694:e45a3d04367b | 3695:5a70dd2349a7 |
---|---|
1 local http = require "net.http"; | |
2 local jid_split = require "util.jid".split; | |
3 local dataforms_new = require "util.dataforms".new; | |
4 local adhoc_new = module:require "adhoc".new; | |
5 local adhoc_simple_form = require "util.adhoc".new_simple_form; | |
6 local t_concat = table.concat; | |
7 | |
8 local store = module:open_store(); | |
9 | |
10 local function api_handler(body, code) | |
11 if code == 200 then | |
12 module:log("debug", "SMS correctly sent."); | |
13 elseif code == 0 then | |
14 module:log("error", "error querying Free API: %s", body); | |
15 elseif code >= 400 then | |
16 module:log("warn", "received error code %d: %s", code, body); | |
17 end | |
18 end | |
19 | |
20 local function message_handler(event) | |
21 local message = event.stanza; | |
22 local username, host = jid_split(message.attr.to); | |
23 | |
24 -- Only proceed if the user has set Free credentials. | |
25 local data = store:get(username); | |
26 if not data then | |
27 return; | |
28 end | |
29 | |
30 -- Only proceed if the message is of type chat or normal. | |
31 local message_type = message.attr.type; | |
32 if message_type == "error" or message_type == "headline" or message_type == "groupchat" then | |
33 -- TODO: Maybe handle groupchat or headline in the future. | |
34 return; | |
35 end | |
36 | |
37 -- Only proceed if the message contains a body. | |
38 local body = message:get_child_text("body"); | |
39 if not body then | |
40 return; | |
41 end | |
42 | |
43 -- Only proceed if all sessions are "xa", or if there are no sessions. | |
44 local sessions = prosody.hosts[host].sessions[username]; | |
45 if sessions then | |
46 local do_send = true; | |
47 for _, session in pairs(sessions.sessions) do | |
48 local show = session.presence:get_child_text("show"); | |
49 if show ~= "xa" then | |
50 do_send = false; | |
51 end | |
52 end | |
53 if not do_send then | |
54 return; | |
55 end | |
56 end | |
57 | |
58 -- Then do the actual request to send the SMS. | |
59 local headers = { | |
60 user = data.user, | |
61 pass = data.pass, | |
62 msg = http.urlencode(body), | |
63 }; | |
64 http.request("https://smsapi.free-mobile.fr/sendmsg", { headers = headers }, api_handler); | |
65 end | |
66 | |
67 local set_form = dataforms_new { | |
68 title = "Set mobile.free.fr SMS credentials"; | |
69 instructions = "Enable the “Notifications by SMS” service at https://mobile.free.fr/moncompte/ and paste the credentials in this form."; | |
70 { | |
71 type = "hidden"; | |
72 name = "FORM_TYPE"; | |
73 value = "http://prosody.im/protocol/sms_free#set"; | |
74 }; | |
75 { | |
76 type = "text-single"; | |
77 name = "user"; | |
78 label = "Your login on Free’s website"; | |
79 }; | |
80 { | |
81 type = "text-single"; | |
82 name = "pass"; | |
83 label = "Your authentication key"; | |
84 }; | |
85 }; | |
86 | |
87 local set_adhoc = adhoc_simple_form(set_form, function (data, errors, state) | |
88 if errors then | |
89 local errmsg = {}; | |
90 for name, text in pairs(errors) do | |
91 errmsg[#errmsg + 1] = name .. ": " .. text; | |
92 end | |
93 return { status = "completed", error = { message = t_concat(errmsg, "\n") } }; | |
94 end | |
95 | |
96 local username, host = jid_split(state.from); | |
97 module:log("debug", "Setting mobile.free.fr credentials for %s@%s: user=%s, pass=%s", username, host, data.user, data.pass); | |
98 local ok, err = store:set(username, { user = data.user, pass = data.pass }); | |
99 if ok then | |
100 return { status = "completed", info = "SMS notifications to your phone enabled." }; | |
101 else | |
102 return { status = "completed", error = { message = err } }; | |
103 end | |
104 end); | |
105 | |
106 module:provides("adhoc", adhoc_new("Set mobile.free.fr SMS notification credentials", "http://prosody.im/protocol/sms_free#set", set_adhoc)); | |
107 | |
108 module:provides("adhoc", adhoc_new("Unset mobile.free.fr SMS notifications", "http://prosody.im/protocol/sms_free#unset", function (_, data) | |
109 if data.action ~= "execute" then | |
110 return { status = "canceled" }; | |
111 end | |
112 | |
113 module:log("debug", "Unsetting mobile.free.fr credentials."); | |
114 local username, host = jid_split(data.from); | |
115 local ok, err = store:set(username, nil); | |
116 if ok then | |
117 return { status = "completed", info = "SMS notifications to your phone disabled." }; | |
118 else | |
119 return { status = "completed", error = { message = err } }; | |
120 end | |
121 end)); | |
122 | |
123 -- Stanzas sent to local clients. | |
124 module:hook("message/bare", message_handler); | |
125 module:hook("message/full", message_handler); |