mod_welcome: Use module:hook instead of module:add_event_hook
[prosody.git] / plugins / mod_presence.lua
1 -- Prosody IM v0.4\r
2 -- Copyright (C) 2008-2009 Matthew Wild\r
3 -- Copyright (C) 2008-2009 Waqas Hussain\r
4 -- \r
5 -- This project is MIT/X11 licensed. Please see the\r
6 -- COPYING file in the source package for more information.\r
7 --\r
8 \r
9 local log = module._log;\r
10 \r
11 local require = require;\r
12 local pairs, ipairs = pairs, ipairs;\r
13 local t_concat = table.concat;\r
14 local s_find = string.find;\r
15 local tonumber = tonumber;\r
16 \r
17 local st = require "util.stanza";\r
18 local jid_split = require "util.jid".split;\r
19 local jid_bare = require "util.jid".bare;\r
20 local hosts = hosts;\r
21 \r
22 local rostermanager = require "core.rostermanager";\r
23 local sessionmanager = require "core.sessionmanager";\r
24 local offlinemanager = require "core.offlinemanager";\r
25 \r
26 local _core_route_stanza = core_route_stanza;\r
27 local core_route_stanza;\r
28 function core_route_stanza(origin, stanza)\r
29         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then\r
30                 local node, host = jid_split(stanza.attr.to);\r
31                 host = hosts[host];\r
32                 if host and host.type == "local" then\r
33                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);\r
34                         return;\r
35                 end\r
36         end\r
37         _core_route_stanza(origin, stanza);\r
38 end\r
39 \r
40 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)\r
41         local type = stanza.attr.type;\r
42         if type and type ~= "unavailable" and type ~= "error" then\r
43                 if inbound then\r
44                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
45                 else\r
46                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
47                 end\r
48         elseif not inbound and not stanza.attr.to then\r
49                 handle_normal_presence(origin, stanza, core_route_stanza);\r
50         else\r
51                 core_route_stanza(origin, stanza);\r
52         end\r
53 end\r
54 \r
55 function handle_normal_presence(origin, stanza, core_route_stanza)\r
56         if origin.roster then\r
57                 for jid in pairs(origin.roster) do -- broadcast to all interested contacts\r
58                         local subscription = origin.roster[jid].subscription;\r
59                         if subscription == "both" or subscription == "from" then\r
60                                 stanza.attr.to = jid;\r
61                                 core_route_stanza(origin, stanza);\r
62                         end\r
63                 end\r
64                 local node, host = jid_split(stanza.attr.from);\r
65                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources\r
66                         if res ~= origin and res.presence then -- to resource\r
67                                 stanza.attr.to = res.full_jid;\r
68                                 core_route_stanza(origin, stanza);\r
69                         end\r
70                 end\r
71                 if stanza.attr.type == nil and not origin.presence then -- initial presence\r
72                         local probe = st.presence({from = origin.full_jid, type = "probe"});\r
73                         for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to\r
74                                 local subscription = origin.roster[jid].subscription;\r
75                                 if subscription == "both" or subscription == "to" then\r
76                                         probe.attr.to = jid;\r
77                                         core_route_stanza(origin, probe);\r
78                                 end\r
79                         end\r
80                         for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources\r
81                                 if res ~= origin and res.presence then\r
82                                         res.presence.attr.to = origin.full_jid;\r
83                                         core_route_stanza(res, res.presence);\r
84                                         res.presence.attr.to = nil;\r
85                                 end\r
86                         end\r
87                         if origin.roster.pending then -- resend incoming subscription requests\r
88                                 for jid in pairs(origin.roster.pending) do\r
89                                         origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?\r
90                                 end\r
91                         end\r
92                         local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});\r
93                         for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests\r
94                                 if item.ask then\r
95                                         request.attr.to = jid;\r
96                                         core_route_stanza(origin, request);\r
97                                 end\r
98                         end\r
99                         local offline = offlinemanager.load(node, host);\r
100                         if offline then\r
101                                 for _, msg in ipairs(offline) do\r
102                                         origin.send(msg); -- FIXME do we need to modify to/from in any way?\r
103                                 end\r
104                                 offlinemanager.deleteAll(node, host);\r
105                         end\r
106                 end\r
107                 origin.priority = 0;\r
108                 if stanza.attr.type == "unavailable" then\r
109                         origin.presence = nil;\r
110                         if origin.directed then\r
111                                 local old_from = stanza.attr.from;\r
112                                 stanza.attr.from = origin.full_jid;\r
113                                 for jid in pairs(origin.directed) do\r
114                                         stanza.attr.to = jid;\r
115                                         core_route_stanza(origin, stanza);\r
116                                 end\r
117                                 stanza.attr.from = old_from;\r
118                                 origin.directed = nil;\r
119                         end\r
120                 else\r
121                         origin.presence = stanza;\r
122                         local priority = stanza:child_with_name("priority");\r
123                         if priority and #priority > 0 then\r
124                                 priority = t_concat(priority);\r
125                                 if s_find(priority, "^[+-]?[0-9]+$") then\r
126                                         priority = tonumber(priority);\r
127                                         if priority < -128 then priority = -128 end\r
128                                         if priority > 127 then priority = 127 end\r
129                                         origin.priority = priority;\r
130                                 end\r
131                         end\r
132                 end\r
133                 stanza.attr.to = nil; -- reset it\r
134         else\r
135                 log("warn", "presence recieved from client with no roster");\r
136         end\r
137 end\r
138 \r
139 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)\r
140         local h = hosts[host];\r
141         local count = 0;\r
142         if h and h.type == "local" then\r
143                 local u = h.sessions[user];\r
144                 if u then\r
145                         for k, session in pairs(u.sessions) do\r
146                                 local pres = session.presence;\r
147                                 if pres then\r
148                                         pres.attr.to = jid;\r
149                                         core_route_stanza(session, pres);\r
150                                         pres.attr.to = nil;\r
151                                         count = count + 1;\r
152                                 end\r
153                         end\r
154                 end\r
155         end\r
156         log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);\r
157         return count;\r
158 end\r
159 \r
160 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
161         local node, host = jid_split(from_bare);\r
162         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
163         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
164         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
165         if stanza.attr.type == "subscribe" then\r
166                 -- 1. route stanza\r
167                 -- 2. roster push (subscription = none, ask = subscribe)\r
168                 if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
169                         rostermanager.roster_push(node, host, to_bare);\r
170                 end -- else file error\r
171                 core_route_stanza(origin, stanza);\r
172         elseif stanza.attr.type == "unsubscribe" then\r
173                 -- 1. route stanza\r
174                 -- 2. roster push (subscription = none or from)\r
175                 if rostermanager.unsubscribe(node, host, to_bare) then\r
176                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
177                 end -- else file error\r
178                 core_route_stanza(origin, stanza);\r
179         elseif stanza.attr.type == "subscribed" then\r
180                 -- 1. route stanza\r
181                 -- 2. roster_push ()\r
182                 -- 3. send_presence_of_available_resources\r
183                 if rostermanager.subscribed(node, host, to_bare) then\r
184                         rostermanager.roster_push(node, host, to_bare);\r
185                 end\r
186                 core_route_stanza(origin, stanza);\r
187                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
188         elseif stanza.attr.type == "unsubscribed" then\r
189                 -- 1. route stanza\r
190                 -- 2. roster push (subscription = none or to)\r
191                 if rostermanager.unsubscribed(node, host, to_bare) then\r
192                         rostermanager.roster_push(node, host, to_bare);\r
193                 end\r
194                 core_route_stanza(origin, stanza);\r
195         end\r
196         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
197 end\r
198 \r
199 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
200         local node, host = jid_split(to_bare);\r
201         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
202         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
203         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
204         if stanza.attr.type == "probe" then\r
205                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
206                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
207                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
208                         end\r
209                 else\r
210                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
211                 end\r
212         elseif stanza.attr.type == "subscribe" then\r
213                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
214                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
215                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate\r
216                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
217                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
218                         end\r
219                 else\r
220                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
221                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
222                                         sessionmanager.send_to_available_resources(node, host, stanza);\r
223                                 end -- TODO else return error, unable to save\r
224                         end\r
225                 end\r
226         elseif stanza.attr.type == "unsubscribe" then\r
227                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
228                         rostermanager.roster_push(node, host, from_bare);\r
229                 end\r
230         elseif stanza.attr.type == "subscribed" then\r
231                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
232                         rostermanager.roster_push(node, host, from_bare);\r
233                 end\r
234         elseif stanza.attr.type == "unsubscribed" then\r
235                 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then\r
236                         rostermanager.roster_push(node, host, from_bare);\r
237                 end\r
238         end -- discard any other type\r
239         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
240 end\r
241 \r
242 local function presence_handler(data)\r
243         local origin, stanza = data.origin, data.stanza;\r
244         local to = stanza.attr.to;\r
245         local node, host = jid_split(to);\r
246         local to_bare = jid_bare(to);\r
247         local from_bare = jid_bare(stanza.attr.from);\r
248         if origin.type == "c2s" then\r
249                 if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence\r
250                         origin.directed = origin.directed or {};\r
251                         origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?\r
252                 end\r
253                 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then\r
254                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
255                 elseif not to then\r
256                         handle_normal_presence(origin, stanza, core_route_stanza);\r
257                 else\r
258                         core_route_stanza(origin, stanza);\r
259                 end\r
260         elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then\r
261                 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then\r
262                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
263                 else\r
264                         core_route_stanza(origin, stanza);\r
265                 end\r
266         end\r
267         return true;\r
268 end\r
269 \r
270 prosody.events.add_handler(module:get_host().."/presence", presence_handler);\r
271 module.unload = function()\r
272         prosody.events.remove_handler(module:get_host().."/presence", presence_handler);\r
273 end\r