Mercurial > prosody-modules
annotate mod_sms_clickatell/mod_sms_clickatell.lua @ 346:2e6a74842c00
mod_sms_clickatell: initial import
author | Phil Stewart <phil.stewart@lichp.co.uk> |
---|---|
date | Thu, 24 Mar 2011 19:49:42 +0000 |
parents | |
children | cd838419a85d |
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 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
52 local clickatell_api_id = module:get_option_string("clickatell_api_id"); |
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 ""; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
54 local sms_source_number = module:get_option_string("sms_source_number") or ""; |
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) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
203 local request = http.request(url, { headers = http_headers or {}, body = "", method = method or "GET" }, function(response, code, request) http_action_callback(response, code, request, fcallback) end); |
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"; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
219 local params = {user=user.data.username, password=user.data.password, api_id=clickatell_api_id, from=sms_source_number, to=number, text=sms_message}; |
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}) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
301 :tag("query", { xmlns="jabber:iq:register" }) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
302 :tag("instructions"):text("Enter the Clickatell username and password to use with API ID "..clickatell_api_id):up() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
303 :tag("username"):up() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
304 :tag("password"):up(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
305 data_cache.registration_form = reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
306 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
307 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
308 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
309 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
310 elseif stanza.attr.type == "set" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
311 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
312 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
|
313 local bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
314 local username, password = "", ""; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
315 local reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
316 for _, tag in ipairs(stanza.tags[1].tags) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
317 if tag.name == "remove" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
318 iq_success(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
319 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
320 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
321 if tag.name == "username" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
322 username = tag[1]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
323 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
324 if tag.name == "password" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
325 password = tag[1]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
326 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
327 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
328 if username ~= nil and password ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
329 users[bjid] = smsuser:register(bjid); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
330 users[bjid].data.username = username; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
331 users[bjid].data.password = password; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
332 users[bjid]:store(); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
333 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
334 iq_success(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
335 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
336 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
337 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
338 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
339 -- XMPP Roster callback |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
340 -- 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
|
341 -- 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
|
342 function iq_roster(stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
343 module:log("info", "Roster request triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
344 local from = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
345 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
|
346 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
347 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
|
348 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
349 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
350 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
351 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
352 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
|
353 if users[from_bjid].data.roster ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
354 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
|
355 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
|
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 reply.attr.id = stanza.attr.id; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
359 reply.attr.to = stanza.attr.from; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
360 return reply; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
361 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
362 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
363 -- Roster Exchange: iq variant |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
364 -- Sends sms targets to client's roster |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
365 function iq_roster_push(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
366 module:log("info", "Sending Roster iq"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
367 local from = {} |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
368 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
|
369 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
370 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
|
371 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
372 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
373 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
374 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
375 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
|
376 reply:tag("query", {xmlns="jabber:iq:roster"}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
377 if users[from_bjid].data.roster ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
378 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
|
379 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
|
380 end |
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 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
383 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
384 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
385 -- XMPP Presence handling |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
386 function presence_stanza_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
387 module:log("info", "Presence handler triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
388 local to = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
389 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
390 local pres = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
391 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
|
392 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
|
393 pres.type = stanza.attr.type; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
394 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
|
395 local from_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
396 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
|
397 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
398 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
399 from_bjid = from.host; |
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 local to_bjid = nil |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
402 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
|
403 to_bjid = to.node.."@"..to.host |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
404 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
405 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
406 if to.node == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
407 -- Component presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
408 -- 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
|
409 if pres.type == 'subscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
410 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
|
411 --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
|
412 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
413 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
414 -- 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
|
415 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
|
416 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
417 -- 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
|
418 iq_roster_push(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
419 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
420 -- SMS user presence |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
421 if pres.type == 'subscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
422 users[from_bjid]:roster_add(to.node); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
423 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
|
424 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
425 if pres.type == 'unsubscribe' then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
426 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
|
427 iq_roster_push(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
428 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
|
429 users[from_bjid]:roster_delete(to.node) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
430 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
431 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
|
432 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
|
433 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
434 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
435 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
436 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
437 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
438 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
439 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
440 --[[ Not using this ATM |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
441 function confirm_message_delivery(event) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
442 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
|
443 origin.send(reply); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
444 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
445 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
446 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
447 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
448 -- 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
|
449 -- bjid = base JID i.e. without resource identifier |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
450 function message_stanza_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
451 module:log("info", "Message handler triggered"); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
452 local to = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
453 local from = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
454 local msg = {}; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
455 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
|
456 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
|
457 local bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
458 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
|
459 from_bjid = from.node.."@"..from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
460 elseif from.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
461 from_bjid = from.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
462 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
463 local to_bjid = nil; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
464 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
|
465 to_bjid = to.node.."@"..to.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
466 elseif to.host ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
467 to_bjid = to.host; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
468 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
469 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
470 -- 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
|
471 for _, tag in ipairs(stanza.tags) do |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
472 msg[tag.name] = tag[1]; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
473 if tag.attr.xmlns == "urn:xmpp:receipts" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
474 confirm_message_delivery({origin=origin, stanza=stanza}); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
475 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
476 -- can handle more xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
477 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
478 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
479 -- Now parse the message |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
480 if stanza.attr.to == component_host then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
481 -- Messages directly to the component jget echoed |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
482 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
|
483 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
|
484 -- 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
|
485 if msg.body ~= nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
486 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
|
487 end |
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 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
490 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
491 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
492 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
493 -- Component event handler |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
494 function sms_event_handler(origin, stanza) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
495 module:log("debug", "Received stanza: "..stanza:pretty_print()); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
496 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
|
497 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
498 -- 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
|
499 if to_node == nil then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
500 local type = stanza.attr.type; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
501 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
|
502 if stanza.name == "presence" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
503 presence_stanza_handler(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
504 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
505 if stanza.name == "iq" and type == "get" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
506 local xmlns = stanza.tags[1].attr.xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
507 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
|
508 origin.send(iq_disco_info(stanza)); |
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 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
511 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
|
512 origin.send(iq_disco_items(stanza)); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
513 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
514 --]] |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
515 elseif xmlns == "jabber:iq:register" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
516 iq_register(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
517 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
518 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
519 elseif stanza.name == "iq" and type == "set" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
520 local xmlns = stanza.tags[1].attr.xmlns |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
521 if xmlns == "jabber:iq:roster" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
522 origin.send(iq_roster(stanza)); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
523 elseif xmlns == "jabber:iq:register" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
524 iq_register(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
525 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
526 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
527 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
528 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
529 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
530 -- Handle presence (both component and SMS users) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
531 if stanza.name == "presence" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
532 presence_stanza_handler(origin, stanza); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
533 end |
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 -- Handle messages (both component and SMS users) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
536 if stanza.name == "message" then |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
537 message_stanza_handler(origin, stanza); |
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 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
540 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
541 -- 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
|
542 --module:hook("presence/host", presence_stanza_handler); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
543 --module:hook("message/host", message_stanza_handler); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
544 |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
545 --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
|
546 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
|
547 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
|
548 --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
|
549 --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
|
550 --module:hook("account-disco-info", iq_disco_info); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
551 --module:hook("account-disco-items", iq_disco_items); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
552 --[[ |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
553 module:hook("iq/host", function(data) |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
554 -- IQ to a local host recieved |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
555 local origin, stanza = data.origin, data.stanza; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
556 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
|
557 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
|
558 else |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
559 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
|
560 return true; |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
561 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
562 end); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
563 --]] |
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 -- 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
|
566 -- manager |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
567 module.unload = function() |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
568 componentmanager.deregister_component(component_host); |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
569 end |
2e6a74842c00
mod_sms_clickatell: initial import
Phil Stewart <phil.stewart@lichp.co.uk>
parents:
diff
changeset
|
570 component = componentmanager.register_component(component_host, sms_event_handler); |