Mercurial > prosody-modules
annotate mod_sms_clickatell/mod_sms_clickatell.lua @ 1188:5eaecb7f680d
mod_watchuntrusted: New module that will warn admins about s2s connections that fail due to lack of encryption or invalid certificates.
author | Thijs Alkemade <me@thijsalkema.de> |
---|---|
date | Fri, 06 Sep 2013 13:07:57 +0200 |
parents | ac9bf3fcbcfe |
children | 3fd6579d60cc |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
4 -- Clickatell gateway's HTTP API |
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 componentmanager = require "core.componentmanager"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
39 local datamanager = require "util.datamanager"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
40 local timer = require "util.timer"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
41 local config_get = require "core.configmanager".get; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
42 local http = require "net.http"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
43 local base64 = require "util.encodings".base64; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
44 local serialize = require "util.serialization".serialize; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
45 local pairs, ipairs = pairs, ipairs; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
46 local setmetatable = setmetatable; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
47 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
48 local component_host = module:get_host(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
49 local component_name = module.name; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
50 local data_cache = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
51 |
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
|
52 --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
|
53 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
|
54 --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
|
55 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
56 --local users = setmetatable({}, {__mode="k"}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
57 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
58 -- User data is held in smsuser objects |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
59 local smsuser = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
60 smsuser.__index = smsuser; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
61 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
62 -- 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
|
63 -- 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
|
64 -- 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
|
65 local users = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
66 setmetatable(users, { __index = function (table, key) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
67 return smsuser:register(key); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
68 end }); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
69 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
70 -- Create a new smsuser object |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
71 function smsuser:new() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
72 newuser = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
73 setmetatable(newuser, self); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
74 return newuser; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
75 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
76 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
77 -- Store (save) the user object |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
78 function smsuser:store() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
79 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
|
80 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
81 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
82 -- For debug |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
83 function smsuser:logjid() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
84 module:log("logjid: ", self.jid); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
85 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
86 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
87 -- 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
|
88 -- 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
|
89 function smsuser:register(bjid) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
90 reguser = smsuser:new(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
91 reguser.jid = bjid; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
92 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
|
93 return reguser; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
94 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
95 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
96 -- Add a roster entry for the user |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
97 -- 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
|
98 function smsuser:roster_add(sms_number) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
99 if self.data.roster == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
100 self.data.roster = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
101 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
102 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
103 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
|
104 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
105 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
106 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
107 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
108 -- 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
|
109 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
|
110 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
111 smsuser:roster_add(sms_number); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
112 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
113 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
|
114 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
115 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
116 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
117 -- 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
|
118 function smsuser:roster_update_subscription(sms_number, subscription) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
119 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
120 smsuser:roster_add(sms_number); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
121 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
122 self.data.roster[sms_number].subscription = subscription; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
123 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
124 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
125 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
126 -- Delete an entry from the roster |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
127 function smsuser:roster_delete(sms_number) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
128 self.data.roster[sms_number] = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
129 self:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
130 end |
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 -- |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
133 function smsuser:roster_stanza_args(sms_number) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
134 if self.data.roster[sms_number] == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
135 return nil |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
136 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
137 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
|
138 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
|
139 args.subscription = self.data.roster[sms_number].subscription |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
140 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
141 return args |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
142 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
143 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
144 --[[ 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
|
145 function send_stanza(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
146 if stanza ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
147 core_route_stanza(prosody.hosts[component_host], stanza) |
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 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
150 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
151 function dmsg(jid, msg) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
152 module:log("debug", msg or "nil"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
153 if jid ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
154 send_stanza(st.message({to=jid, from=component_host, type='chat'}):tag("body"):text(msg or "nil"):up()); |
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 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
157 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
158 function substring(string, start_string, ending_string) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
159 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
|
160 if start_string ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
161 _, s_value_start = string:find(start_string); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
162 if s_value_start == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
163 -- error |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
164 return nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
165 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
166 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
167 return nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
168 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
169 if ending_string ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
170 _, 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
|
171 if s_value_finish == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
172 -- error |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
173 return nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
174 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
175 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
176 s_value_finish = string:len()+1; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
177 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
178 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
|
179 end |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
182 local http_timeout = 30; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
183 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
|
184 data_cache['prosody_os'] = prosody.platform; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
185 data_cache['prosody_version'] = prosody.version; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
186 local http_headers = { |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
187 ["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
|
188 }; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
189 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
190 function http_action_callback(response, code, request, xcallback) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
191 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
|
192 local id = http_queue[request]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
193 http_queue[request] = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
194 if xcallback == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
195 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
|
196 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
197 xcallback(id, response, request); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
198 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
199 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
200 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
201 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
202 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
|
203 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
|
204 http_queue[request] = tid; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
205 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
|
206 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
207 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
208 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
209 -- Clickatell SMS HTTP API interaction function |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
210 function clickatell_send_sms(user, number, message) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
211 module.log("info", "Clickatell API interaction function triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
212 -- 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
|
213 if message == nil or message == "" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
214 return false; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
215 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
216 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
217 local sms_message = sms_message_prefix..message; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
218 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
|
219 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
|
220 local query_string = ""; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
221 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
222 for param, data in pairs(params) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
223 --module:log("info", "Inside query constructor: "..param..data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
224 if query_string ~= "" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
225 query_string = query_string.."&"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
226 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
227 query_string = query_string..param.."="..http.urlencode(data); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
228 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
229 local url = clickatell_base_url.."?"..query_string; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
230 module:log("info", "Clickatell SMS URL: "..url); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
231 http_add_action(message, url, "GET", params, nil); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
232 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
233 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
234 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
235 function iq_success(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
236 local reply = data_cache.success; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
237 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
238 reply = st.iq({type='result', from=stanza.attr.to or component_host}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
239 data_cache.success = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
240 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
241 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
242 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
243 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
244 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
245 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
246 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
247 -- XMPP Service Discovery (disco) info callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
248 -- 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
|
249 -- information as per XEP-0030 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
250 function iq_disco_info(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
251 module:log("info", "Disco info triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
252 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
253 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
|
254 local bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
255 local reply = data_cache.disco_info; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
256 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
257 --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
|
258 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
|
259 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
|
260 reply:tag("feature", {var="urn:xmpp:receipts"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
261 reply:tag("feature", {var="jabber:iq:register"}):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
262 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
|
263 --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
|
264 --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
|
265 --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
|
266 data_cache.disco_info = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
267 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
268 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
269 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
270 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
271 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
272 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
273 -- XMPP Service Discovery (disco) items callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
274 -- 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
|
275 -- information as per XEP-0030 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
276 -- (Nothing much happening here at the moment) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
277 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
278 function iq_disco_items(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
279 module:log("info", "Disco items triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
280 local reply = data_cache.disco_items; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
281 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
282 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
|
283 :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
|
284 data_cache.disco_items = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
285 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
286 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
287 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
288 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
289 end |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
292 -- XMPP Register callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
293 -- The client must register with the gateway. In this case, the gateway is |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
294 -- Clickatell's http api, so we |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
295 function iq_register(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
296 module:log("info", "Register event triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
297 if stanza.attr.type == "get" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
298 local reply = data_cache.registration_form; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
299 if reply == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
300 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
|
301 :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
|
302 :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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 :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
|
308 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
|
309 :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
|
310 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
|
311 :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
|
312 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
|
313 :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
|
314 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
|
315 :tag("required"):up():up(); |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
316 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
|
317 --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
|
318 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
319 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
320 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
321 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
322 elseif stanza.attr.type == "set" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
323 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
324 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
|
325 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
|
326 local username, password, api_id, source_number = "", "", "", ""; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
327 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
|
328 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
|
329 -- 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
|
330 -- 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
|
331 -- 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
|
332 -- 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
|
333 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
|
334 username = tag.tags[1][1]; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
335 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
|
336 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
|
337 password = tag.tags[1][1]; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
338 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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 source_number = tag.tags[1][1]; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
344 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
345 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
|
346 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
|
347 users[bjid] = smsuser:register(bjid); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
348 users[bjid].data.username = username; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
349 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
|
350 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
|
351 users[bjid].data.source_number = source_number; |
346
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
352 users[bjid]:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
353 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
354 iq_success(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
355 return true; |
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 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
358 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
359 -- XMPP Roster callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
360 -- 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
|
361 -- 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
|
362 function iq_roster(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
363 module:log("info", "Roster request triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
364 local from = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
365 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
|
366 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
367 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
|
368 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
369 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
370 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
371 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
372 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
|
373 if users[from_bjid].data.roster ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
374 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
|
375 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
|
376 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
377 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
378 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
379 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
380 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
381 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
382 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
383 -- Roster Exchange: iq variant |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
384 -- Sends sms targets to client's roster |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
385 function iq_roster_push(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
386 module:log("info", "Sending Roster iq"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
387 local from = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
388 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
|
389 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
390 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
|
391 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
392 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
393 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
394 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
395 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
|
396 reply:tag("query", {xmlns="jabber:iq:roster"}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
397 if users[from_bjid].data.roster ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
398 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
|
399 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
|
400 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
401 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
402 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
403 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
404 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
405 -- XMPP Presence handling |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
406 function presence_stanza_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
407 module:log("info", "Presence handler triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
408 local to = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
409 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
410 local pres = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
411 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
|
412 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
|
413 pres.type = stanza.attr.type; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
414 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
|
415 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
416 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
|
417 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
418 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
419 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
420 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
421 local to_bjid = nil |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
422 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
|
423 to_bjid = to.node.."@"..to.host |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
424 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
425 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
426 if to.node == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
427 -- Component presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
428 -- 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
|
429 if pres.type == 'subscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
430 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
|
431 --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
|
432 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
433 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
434 -- 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
|
435 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
|
436 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
437 -- 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
|
438 iq_roster_push(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
439 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
440 -- SMS user presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
441 if pres.type == 'subscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
442 users[from_bjid]:roster_add(to.node); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
443 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
|
444 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
445 if pres.type == 'unsubscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
446 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
|
447 iq_roster_push(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
448 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
|
449 users[from_bjid]:roster_delete(to.node) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
450 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
451 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
|
452 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
|
453 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
454 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
455 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
456 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
457 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
458 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
459 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
460 --[[ Not using this ATM |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
461 function confirm_message_delivery(event) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
462 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
|
463 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
464 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
465 end |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
468 -- 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
|
469 -- bjid = base JID i.e. without resource identifier |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
470 function message_stanza_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
471 module:log("info", "Message handler triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
472 local to = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
473 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
474 local msg = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
475 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
|
476 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
|
477 local bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
478 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
|
479 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
480 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
481 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
482 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
483 local to_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
484 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
|
485 to_bjid = to.node.."@"..to.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
486 elseif to.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
487 to_bjid = to.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
488 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
489 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
490 -- 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
|
491 for _, tag in ipairs(stanza.tags) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
492 msg[tag.name] = tag[1]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
493 if tag.attr.xmlns == "urn:xmpp:receipts" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
494 confirm_message_delivery({origin=origin, stanza=stanza}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
495 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
496 -- can handle more xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
497 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
498 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
499 -- Now parse the message |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
500 if stanza.attr.to == component_host then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
501 -- Messages directly to the component jget echoed |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
502 origin.send(st.message({to=stanza.attr.from, from=component_host, type='chat'}):tag("body"):text(msg.body):up()); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
503 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
|
504 -- 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
|
505 if msg.body ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
506 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
|
507 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
508 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
509 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
510 end |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
513 -- Component event handler |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
514 function sms_event_handler(origin, stanza) |
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); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
517 |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
549 |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
554 |
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) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
574 -- IQ to a local host recieved |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
586 -- manager |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
587 module.unload = function() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
588 componentmanager.deregister_component(component_host); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
589 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
590 component = componentmanager.register_component(component_host, sms_event_handler); |