Software /
code /
prosody-modules
Annotate
mod_sms_clickatell/mod_sms_clickatell.lua @ 5173:460f78654864
mod_muc_rtbl: also filter messages
This was a bit tricky because we don't want to run the JIDs
through SHA256 on each message. Took a while to come up with this
simple plan of just caching the SHA256 of the JIDs on the
occupants.
This will leave some dirt in the occupants after unloading the
module, but that should be ok; once they cycle the room, the
hashes will be gone.
This is direly needed, otherwise, there is a tight race between
the moderation activities and the actors joining the room.
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Tue, 21 Feb 2023 21:37:27 +0100 |
parent | 3353:e8d6f602f382 |
rev | line source |
---|---|
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
1 -- mod_sms_clickatell |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
2 -- |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
3 -- A Prosody module for sending SMS text messages from XMPP using the |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
4 -- Clickatell gateway's HTTP API |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
5 -- |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
6 -- Hacked from mod_twitter by Phil Stewart, March 2011. Anything from |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
7 -- mod_twitter copyright The Guy Who Wrote mod_twitter. Everything else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
8 -- copyright 2011 Phil Stewart. Licensed under the same terms as Prosody |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
9 -- (MIT license, as per below) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
10 -- |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
11 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
12 Permission is hereby granted, free of charge, to any person obtaining a copy |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
13 of this software and associated documentation files (the "Software"), to deal |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
14 in the Software without restriction, including without limitation the rights |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
16 copies of the Software, and to permit persons to whom the Software is |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
17 furnished to do so, subject to the following conditions: |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
18 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
19 The above copyright notice and this permission notice shall be included in |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
20 all copies or substantial portions of the Software. |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
21 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
28 THE SOFTWARE. |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
29 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
30 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
31 -- Raise an error if the modules hasn't been loaded as a component in prosody's config |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
32 if module:get_host_type() ~= "component" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
33 error(module.name.." should be loaded as a component, check out http://prosody.im/doc/components", 0); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
34 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
35 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
36 local jid_split = require "util.jid".split; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
37 local st = require "util.stanza"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
38 local datamanager = require "util.datamanager"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
39 local timer = require "util.timer"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
40 local config_get = require "core.configmanager".get; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
41 local http = require "net.http"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
42 local base64 = require "util.encodings".base64; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
43 local serialize = require "util.serialization".serialize; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
44 local pairs, ipairs = pairs, ipairs; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
45 local setmetatable = setmetatable; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
46 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
47 local component_host = module:get_host(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
48 local component_name = module.name; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
49 local data_cache = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
50 |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
51 --local clickatell_api_id = module:get_option_string("clickatell_api_id"); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
52 local sms_message_prefix = module:get_option_string("sms_message_prefix") or ""; |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
53 --local sms_source_number = module:get_option_string("sms_source_number") or ""; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
54 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
55 --local users = setmetatable({}, {__mode="k"}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
56 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
57 -- User data is held in smsuser objects |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
58 local smsuser = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
59 smsuser.__index = smsuser; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
60 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
61 -- Users table is used to store user data in the form of smsuser objects. |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
62 -- It is indexed by the base jid of the user, so when a non-extant entry in the |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
63 -- table is referenced, we pass the jid to smsuser:register to load the user |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
64 local users = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
65 setmetatable(users, { __index = function (table, key) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
66 return smsuser:register(key); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
67 end }); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
68 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
69 -- Create a new smsuser object |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
70 function smsuser:new() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
71 newuser = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
72 setmetatable(newuser, self); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
73 return newuser; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
74 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
75 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
76 -- Store (save) the user object |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
77 function smsuser:store() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
78 datamanager.store(self.jid, component_host, "data", self.data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
79 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
80 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
81 -- For debug |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
82 function smsuser:logjid() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
83 module:log("logjid: ", self.jid); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
84 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
85 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
86 -- Register a user against the base jid of the client. If a user entry for the |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
87 -- bjid is already stored in the Prosody data manager, retrieve its data |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
88 function smsuser:register(bjid) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
89 reguser = smsuser:new(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
90 reguser.jid = bjid; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
91 reguser.data = datamanager.load(bjid, component_host, "data") or {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
92 return reguser; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
93 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
94 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
95 -- Add a roster entry for the user |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
96 -- SMS users must me of the form number@component_host |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
97 function smsuser:roster_add(sms_number) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
98 if self.data.roster == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
99 self.data.roster = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
100 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
101 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
102 self.data.roster[sms_number] = {screen_name=sms_number, subscription=nil}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
103 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
104 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
105 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
106 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
107 -- Update the roster entry of sms_number with new screen name |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
108 function smsuser:roster_update_screen_name(sms_number, screen_name) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
109 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
110 smsuser:roster_add(sms_number); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
111 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
112 self.data.roster[sms_number].screen_name = screen_name; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
113 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
114 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
115 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
116 -- Update the roster entry of sms_number with new subscription detail |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
117 function smsuser:roster_update_subscription(sms_number, subscription) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
118 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
119 smsuser:roster_add(sms_number); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
120 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
121 self.data.roster[sms_number].subscription = subscription; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
122 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
123 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
124 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
125 -- Delete an entry from the roster |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
126 function smsuser:roster_delete(sms_number) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
127 self.data.roster[sms_number] = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
128 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
129 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
130 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
131 -- |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
132 function smsuser:roster_stanza_args(sms_number) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
133 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
134 return nil |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
135 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
136 local args = {jid=sms_number.."@"..component_host, name=self.data.roster[sms_number].screen_name} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
137 if self.data.roster[sms_number].subscription ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
138 args.subscription = self.data.roster[sms_number].subscription |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
139 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
140 return args |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
141 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
142 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
143 --[[ From mod_twitter, keeping 'cos I might use it later :-) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
144 function send_stanza(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
145 if stanza ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
146 core_route_stanza(prosody.hosts[component_host], stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
147 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
148 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
149 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
150 function dmsg(jid, msg) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
151 module:log("debug", msg or "nil"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
152 if jid ~= nil then |
2887
65082d91950e
Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2874
diff
changeset
|
153 send_stanza(st.message({to=jid, from=component_host, type='chat'}, msg or "nil")); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
154 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
155 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
156 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
157 function substring(string, start_string, ending_string) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
158 local s_value_start, s_value_finish = nil, nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
159 if start_string ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
160 _, s_value_start = string:find(start_string); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
161 if s_value_start == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
162 -- error |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
163 return nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
164 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
165 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
166 return nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
167 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
168 if ending_string ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
169 _, s_value_finish = string:find(ending_string, s_value_start+1); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
170 if s_value_finish == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
171 -- error |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
172 return nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
173 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
174 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
175 s_value_finish = string:len()+1; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
176 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
177 return string:sub(s_value_start+1, s_value_finish-1); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
178 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
179 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
180 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
181 local http_timeout = 30; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
182 local http_queue = setmetatable({}, { __mode = "k" }); -- auto-cleaning nil elements |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
183 data_cache['prosody_os'] = prosody.platform; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
184 data_cache['prosody_version'] = prosody.version; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
185 local http_headers = { |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
186 ["user-Agent"] = "Prosody ("..data_cache['prosody_version'].."; "..data_cache['prosody_os']..")" --"ELinks (0.4pre5; Linux 2.4.27 i686; 80x25)", |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
187 }; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
188 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
189 function http_action_callback(response, code, request, xcallback) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
190 if http_queue == nil or http_queue[request] == nil then return; end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
191 local id = http_queue[request]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
192 http_queue[request] = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
193 if xcallback == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
194 dmsg(nil, "http_action_callback reports that xcallback is nil"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
195 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
196 xcallback(id, response, request); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
197 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
198 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
199 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
200 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
201 function http_add_action(tid, url, method, post, fcallback) |
983
ac9bf3fcbcfe
mod_pubsub_feeds, mod_sms_clickatell, mod_twitter: Update for net.http API change in prosody:e3b9dc9dd940
Matthew Wild <mwild1@gmail.com>
parents:
347
diff
changeset
|
202 local request = http.request(url, { headers = http_headers or {}, body = "", method = method or "GET" }, function(response_body, code, response, request) http_action_callback(response_body, code, request, fcallback) end); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
203 http_queue[request] = tid; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
204 timer.add_task(http_timeout, function() http.destroy_request(request); end); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
205 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
206 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
207 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
208 -- Clickatell SMS HTTP API interaction function |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
209 function clickatell_send_sms(user, number, message) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
210 module.log("info", "Clickatell API interaction function triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
211 -- Don't attempt to send an SMS with a null or empty message |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
212 if message == nil or message == "" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
213 return false; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
214 end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
215 |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
216 local sms_message = sms_message_prefix..message; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
217 local clickatell_base_url = "https://api.clickatell.com/http/sendmsg"; |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
218 local params = {user=user.data.username, password=user.data.password, api_id=user.data.api_id, from=user.data.source_number, to=number, text=sms_message}; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
219 local query_string = ""; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
220 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
221 for param, data in pairs(params) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
222 --module:log("info", "Inside query constructor: "..param..data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
223 if query_string ~= "" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
224 query_string = query_string.."&"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
225 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
226 query_string = query_string..param.."="..http.urlencode(data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
227 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
228 local url = clickatell_base_url.."?"..query_string; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
229 module:log("info", "Clickatell SMS URL: "..url); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
230 http_add_action(message, url, "GET", params, nil); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
231 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
232 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
233 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
234 function iq_success(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
235 local reply = data_cache.success; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
236 if reply == nil then |
3353
e8d6f602f382
mod_password_reset: Improve error reporting when setting a password fails
Matthew Wild <mwild1@gmail.com>
parents:
2887
diff
changeset
|
237 reply = st.reply(stanza); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
238 data_cache.success = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
239 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
240 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
241 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
242 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
243 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
244 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
245 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
246 -- XMPP Service Discovery (disco) info callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
247 -- When a disco info query comes in, returns the identity and feature |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
248 -- information as per XEP-0030 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
249 function iq_disco_info(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
250 module:log("info", "Disco info triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
251 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
252 from.node, from.host, from.resource = jid_split(stanza.attr.from); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
253 local bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
254 local reply = data_cache.disco_info; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
255 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
256 --reply = st.iq({type='result', from=stanza.attr.to or component_host}):query("http://jabber.org/protocol/disco#info"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
257 reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
258 reply:tag("identity", {category='gateway', type='sms', name=component_name}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
259 reply:tag("feature", {var="urn:xmpp:receipts"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
260 reply:tag("feature", {var="jabber:iq:register"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
261 reply:tag("feature", {var="http://jabber.org/protocol/rosterx"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
262 --reply = reply:tag("feature", {var="http://jabber.org/protocol/commands"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
263 --reply = reply:tag("feature", {var="jabber:iq:time"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
264 --reply = reply:tag("feature", {var="jabber:iq:version"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
265 data_cache.disco_info = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
266 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
267 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
268 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
269 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
270 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
271 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
272 -- XMPP Service Discovery (disco) items callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
273 -- When a disco info query comes in, returns the items |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
274 -- information as per XEP-0030 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
275 -- (Nothing much happening here at the moment) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
276 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
277 function iq_disco_items(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
278 module:log("info", "Disco items triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
279 local reply = data_cache.disco_items; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
280 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
281 reply = st.iq({type='result', from=stanza.attr.to or component_host}):query("http://jabber.org/protocol/disco#items") |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
282 :tag("item", {jid='testuser'..'@'..component_host, name='SMS Test Target'}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
283 data_cache.disco_items = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
284 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
285 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
286 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
287 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
288 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
289 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
290 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
291 -- XMPP Register callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
292 -- The client must register with the gateway. In this case, the gateway is |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
293 -- Clickatell's http api, so we |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
294 function iq_register(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
295 module:log("info", "Register event triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
296 if stanza.attr.type == "get" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
297 local reply = data_cache.registration_form; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
298 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
299 reply = st.iq({type='result', from=stanza.attr.to or component_host}) |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
300 :tag("query", {xmlns="jabber:iq:register"}) |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
301 :tag("instructions"):text("Use the enclosed form to register."):up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
302 reply:tag("x", {xmlns="jabber:x:data", type='form'}); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
303 reply:tag("title"):text('SMS Gateway Registration: Clickatell'):up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
304 reply:tag("instructions"):text("Enter your Clickatell username, password, API ID, and a source number for your text messages in international format (phone field)"):up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
305 reply:tag("field", {type='hidden', var='FORM_TYPE'}) |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
306 :tag("value"):text('jabber:iq:register'):up():up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
307 reply:tag("field", {type='text-single', label='Username', var='username'}) |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
308 :tag("required"):up():up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
309 reply:tag("field", {type='text-private', label='Password', var='password'}) |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
310 :tag("required"):up():up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
311 reply:tag("field", {type='text-single', label='API ID', var='api_id'}) |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
312 :tag("required"):up():up(); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
313 reply:tag("field", {type='text-single', label='Source Number', var='source_number'}) |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
314 :tag("required"):up():up(); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
315 data_cache.registration_form = reply; |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
316 --module:log("info", "Register stanza to go: "..reply:pretty_print()); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
317 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
318 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
319 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
320 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
321 elseif stanza.attr.type == "set" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
322 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
323 from.node, from.host, from.resource = jid_split(stanza.attr.from); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
324 local bjid = from.node.."@"..from.host; |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
325 local username, password, api_id, source_number = "", "", "", ""; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
326 local reply; |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
327 for tag in stanza.tags[1].tags[1]:childtags() do |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
328 -- if tag.name == "remove" then |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
329 -- iq_success(origin, stanza); |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
330 -- return true; |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
331 -- end |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
332 if tag.attr.var == "username" then |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
333 username = tag.tags[1][1]; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
334 end |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
335 if tag.attr.var == "password" then |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
336 password = tag.tags[1][1]; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
337 end |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
338 if tag.attr.var == "api_id" then |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
339 api_id = tag.tags[1][1]; |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
340 end |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
341 if tag.attr.var == "source_number" then |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
342 source_number = tag.tags[1][1]; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
343 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
344 end |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
345 if username ~= nil and password ~= nil and api_id ~= nil then |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
346 users[bjid] = smsuser:register(bjid); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
347 users[bjid].data.username = username; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
348 users[bjid].data.password = password; |
347
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
349 users[bjid].data.api_id = api_id; |
cd838419a85d
mod_sms_clickatell: moved clickatell API ID and source number settings to component registration
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
346
diff
changeset
|
350 users[bjid].data.source_number = source_number; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
351 users[bjid]:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
352 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
353 iq_success(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
354 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
355 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
356 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
357 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
358 -- XMPP Roster callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
359 -- When the client requests the roster associated with the gateway, returns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
360 -- the users accessible via text to the client's roster |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
361 function iq_roster(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
362 module:log("info", "Roster request triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
363 local from = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
364 from.node, from.host, from.resource = jid_split(stanza.attr.from); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
365 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
366 if from.node ~= nil and from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
367 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
368 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
369 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
370 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
371 local reply = st.iq({type='result', from=stanza.attr.to or component_host}):query("") |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
372 if users[from_bjid].data.roster ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
373 for sms_number, sms_data in pairs(users[from_bjid].data.roster) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
374 reply:tag("item", users[from_bjid]:roster_stanza_args(sms_number)):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
375 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
376 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
377 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
378 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
379 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
380 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
381 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
382 -- Roster Exchange: iq variant |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
383 -- Sends sms targets to client's roster |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
384 function iq_roster_push(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
385 module:log("info", "Sending Roster iq"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
386 local from = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
387 from.node, from.host, from.resource = jid_split(stanza.attr.from); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
388 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
389 if from.node ~= nil and from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
390 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
391 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
392 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
393 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
394 reply = st.iq({to=stanza.attr.from, type='set'}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
395 reply:tag("query", {xmlns="jabber:iq:roster"}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
396 if users[from_bjid].data.roster ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
397 for sms_number, sms_data in pairs(users[from_bjid].data.roster) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
398 reply:tag("item", users[from_bjid]:roster_stanza_args(sms_number)):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
399 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
400 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
401 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
402 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
403 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
404 -- XMPP Presence handling |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
405 function presence_stanza_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
406 module:log("info", "Presence handler triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
407 local to = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
408 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
409 local pres = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
410 to.node, to.host, to.resource = jid_split(stanza.attr.to); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
411 from.node, from.host, from.resource = jid_split(stanza.attr.from); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
412 pres.type = stanza.attr.type; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
413 for _, tag in ipairs(stanza.tags) do pres[tag.name] = tag[1]; end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
414 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
415 if from.node ~= nil and from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
416 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
417 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
418 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
419 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
420 local to_bjid = nil |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
421 if to.node ~= nil and to.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
422 to_bjid = to.node.."@"..to.host |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
423 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
424 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
425 if to.node == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
426 -- Component presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
427 -- If the client is subscribing, send a 'subscribed' presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
428 if pres.type == 'subscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
429 origin.send(st.presence({to=from_bjid, from=component_host, type='subscribed'})); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
430 --origin.send(st.presence{to=from_bjid, type='subscribed'}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
431 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
432 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
433 -- The component itself is online, so send component's presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
434 origin.send(st.presence({to=from_bjid, from=component_host})); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
435 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
436 -- Do roster item exchange: send roster items to client |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
437 iq_roster_push(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
438 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
439 -- SMS user presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
440 if pres.type == 'subscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
441 users[from_bjid]:roster_add(to.node); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
442 origin.send(st.presence({to=from_bjid, from=to_bjid, type='subscribed'})); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
443 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
444 if pres.type == 'unsubscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
445 users[from_bjid]:roster_update_subscription(to.node, 'none'); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
446 iq_roster_push(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
447 origin.send(st.presence({to=from_bjid, from=to_bjid, type='unsubscribed'})); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
448 users[from_bjid]:roster_delete(to.node) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
449 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
450 if users[from_bjid].data.roster[to.node] ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
451 origin.send(st.presence({to=from_bjid, from=to_bjid})); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
452 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
453 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
454 |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
455 |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
456 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
457 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
458 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
459 --[[ Not using this ATM |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
460 function confirm_message_delivery(event) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
461 local reply = st.message({id=event.stanza.attr.id, to=event.stanza.attr.from, from=event.stanza.attr.to or component_host}):tag("received", {xmlns = "urn:xmpp:receipts"}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
462 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
463 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
464 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
465 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
466 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
467 -- XMPP Message handler - this is the bit that Actually Does Things (TM) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
468 -- bjid = base JID i.e. without resource identifier |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
469 function message_stanza_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
470 module:log("info", "Message handler triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
471 local to = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
472 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
473 local msg = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
474 to.node, to.host, to.resource = jid_split(stanza.attr.to); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
475 from.node, from.host, from.resource = jid_split(stanza.attr.from); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
476 local bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
477 if from.node ~= nil and from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
478 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
479 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
480 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
481 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
482 local to_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
483 if to.node ~= nil and to.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
484 to_bjid = to.node.."@"..to.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
485 elseif to.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
486 to_bjid = to.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
487 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
488 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
489 -- This bit looks like it confirms message receipts to the client |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
490 for _, tag in ipairs(stanza.tags) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
491 msg[tag.name] = tag[1]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
492 if tag.attr.xmlns == "urn:xmpp:receipts" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
493 confirm_message_delivery({origin=origin, stanza=stanza}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
494 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
495 -- can handle more xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
496 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
497 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
498 -- Now parse the message |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
499 if stanza.attr.to == component_host then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
500 -- Messages directly to the component jget echoed |
2887
65082d91950e
Many modules: Simplify st.message(…):tag("body"):text(…):up() into st.message(…, …)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2874
diff
changeset
|
501 origin.send(st.message({to=stanza.attr.from, from=component_host, type='chat'}, msg.body)); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
502 elseif users[from_bjid].data.roster[to.node] ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
503 -- If message contains a body, send message to SMS Test User |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
504 if msg.body ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
505 clickatell_send_sms(users[from_bjid], to.node, msg.body); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
506 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
507 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
508 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
509 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
510 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
511 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
512 -- Component event handler |
2448
1f7f66272f73
mod_sms_clickatell: Fix event handler signature (fixes #814)
Kim Alvefur <zash@zash.se>
parents:
1343
diff
changeset
|
513 function sms_event_handler(event) |
1f7f66272f73
mod_sms_clickatell: Fix event handler signature (fixes #814)
Kim Alvefur <zash@zash.se>
parents:
1343
diff
changeset
|
514 local origin, stanza = event.origin, event.stanza; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
515 module:log("debug", "Received stanza: "..stanza:pretty_print()); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
516 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
517 |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
518 -- Handle component internals (stanzas directed to component host, mainly iq stanzas) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
519 if to_node == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
520 local type = stanza.attr.type; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
521 if type == "error" or type == "result" then return; end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
522 if stanza.name == "presence" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
523 presence_stanza_handler(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
524 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
525 if stanza.name == "iq" and type == "get" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
526 local xmlns = stanza.tags[1].attr.xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
527 if xmlns == "http://jabber.org/protocol/disco#info" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
528 origin.send(iq_disco_info(stanza)); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
529 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
530 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
531 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
532 origin.send(iq_disco_items(stanza)); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
533 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
534 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
535 elseif xmlns == "jabber:iq:register" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
536 iq_register(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
537 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
538 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
539 elseif stanza.name == "iq" and type == "set" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
540 local xmlns = stanza.tags[1].attr.xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
541 if xmlns == "jabber:iq:roster" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
542 origin.send(iq_roster(stanza)); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
543 elseif xmlns == "jabber:iq:register" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
544 iq_register(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
545 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
546 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
547 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
548 end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
549 |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
550 -- Handle presence (both component and SMS users) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
551 if stanza.name == "presence" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
552 presence_stanza_handler(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
553 end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
554 |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
555 -- Handle messages (both component and SMS users) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
556 if stanza.name == "message" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
557 message_stanza_handler(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
558 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
559 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
560 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
561 -- Prosody hooks: links our handler functions with the relevant events |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
562 --module:hook("presence/host", presence_stanza_handler); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
563 --module:hook("message/host", message_stanza_handler); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
564 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
565 --module:hook("iq/host/jabber:iq:register:query", iq_register); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
566 module:add_feature("http://jabber.org/protocol/disco#info"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
567 module:add_feature("http://jabber.org/protocol/disco#items"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
568 --module:hook("iq/self/http://jabber.org/protocol/disco#info:query", iq_disco_info); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
569 --module:hook("iq/host/http://jabber.org/protocol/disco#items:query", iq_disco_items); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
570 --module:hook("account-disco-info", iq_disco_info); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
571 --module:hook("account-disco-items", iq_disco_items); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
572 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
573 module:hook("iq/host", function(data) |
2874
178b8abf8283
mod_sms_clickatell: Fix typo in comment [codespell]
Kim Alvefur <zash@zash.se>
parents:
2448
diff
changeset
|
574 -- IQ to a local host received |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
575 local origin, stanza = data.origin, data.stanza; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
576 if stanza.attr.type == "get" or stanza.attr.type == "set" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
577 return module:fire_event("iq/host/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
578 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
579 module:fire_event("iq/host/"..stanza.attr.id, data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
580 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
581 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
582 end); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
583 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
584 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
585 -- Component registration hooks: these hook in with the Prosody component |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1321
diff
changeset
|
586 -- manager |
1321
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
587 module:hook("iq/bare", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
588 module:hook("message/bare", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
589 module:hook("presence/bare", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
590 module:hook("iq/full", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
591 module:hook("message/full", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
592 module:hook("presence/full", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
593 module:hook("iq/host", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
594 module:hook("message/host", sms_event_handler); |
3fd6579d60cc
mod_sms_clickatell: Get rid of legacy componentmanager usage. Now uses stanza events.
Waqas Hussain <waqas20@gmail.com>
parents:
983
diff
changeset
|
595 module:hook("presence/host", sms_event_handler); |