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