Directed presence
[prosody.git] / core / presencemanager.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21 \r
22 local log = require "util.logger".init("presencemanager")\r
23 \r
24 local require = require;\r
25 local pairs, ipairs = pairs, ipairs;\r
26 local t_concat = table.concat;\r
27 local s_find = string.find;\r
28 local tonumber = tonumber;\r
29 \r
30 local st = require "util.stanza";\r
31 local jid_split = require "util.jid".split;\r
32 local hosts = hosts;\r
33 \r
34 local rostermanager = require "core.rostermanager";\r
35 local sessionmanager = require "core.sessionmanager";\r
36 local offlinemanager = require "core.offlinemanager";\r
37 \r
38 module "presencemanager"\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" 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.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence\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                         for _, msg in ipairs(offlinemanager.load(node, host) or {}) do\r
100                                 origin.send(msg); -- FIXME do we need to modify to/from in any way?\r
101                         end\r
102                         offlinemanager.deleteAll(node, host);\r
103                 end\r
104                 origin.priority = 0;\r
105                 if stanza.attr.type == "unavailable" then\r
106                         origin.presence = nil;
107                         if origin.directed then
108                                 for _, jid in ipairs(origin.directed) do
109                                         stanza.attr.to = jid;
110                                         core_route_stanza(origin, stanza);
111                                 end
112                                 origin.directed = nil;
113                         end\r
114                 else\r
115                         origin.presence = stanza;\r
116                         local priority = stanza:child_with_name("priority");\r
117                         if priority and #priority > 0 then\r
118                                 priority = t_concat(priority);\r
119                                 if s_find(priority, "^[+-]?[0-9]+$") then\r
120                                         priority = tonumber(priority);\r
121                                         if priority < -128 then priority = -128 end\r
122                                         if priority > 127 then priority = 127 end\r
123                                         origin.priority = priority;\r
124                                 end\r
125                         end\r
126                 end\r
127                 stanza.attr.to = nil; -- reset it\r
128         else\r
129                 log("error", "presence recieved from client with no roster");\r
130         end\r
131 end\r
132 \r
133 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)\r
134         local h = hosts[host];\r
135         local count = 0;\r
136         if h and h.type == "local" then\r
137                 local u = h.sessions[user];\r
138                 if u then\r
139                         for k, session in pairs(u.sessions) do\r
140                                 local pres = session.presence;\r
141                                 if pres then\r
142                                         pres.attr.to = jid;\r
143                                         pres.attr.from = session.full_jid;\r
144                                         core_route_stanza(session, pres);\r
145                                         pres.attr.to = nil;\r
146                                         pres.attr.from = nil;\r
147                                         count = count + 1;\r
148                                 end\r
149                         end\r
150                 end\r
151         end\r
152         log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);\r
153         return count;\r
154 end\r
155 \r
156 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
157         local node, host = jid_split(from_bare);\r
158         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
159         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
160         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
161         if stanza.attr.type == "subscribe" then\r
162                 -- 1. route stanza\r
163                 -- 2. roster push (subscription = none, ask = subscribe)\r
164                 if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
165                         rostermanager.roster_push(node, host, to_bare);\r
166                 end -- else file error\r
167                 core_route_stanza(origin, stanza);\r
168         elseif stanza.attr.type == "unsubscribe" then\r
169                 -- 1. route stanza\r
170                 -- 2. roster push (subscription = none or from)\r
171                 if rostermanager.unsubscribe(node, host, to_bare) then\r
172                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
173                 end -- else file error\r
174                 core_route_stanza(origin, stanza);\r
175         elseif stanza.attr.type == "subscribed" then\r
176                 -- 1. route stanza\r
177                 -- 2. roster_push ()\r
178                 -- 3. send_presence_of_available_resources\r
179                 if rostermanager.subscribed(node, host, to_bare) then\r
180                         rostermanager.roster_push(node, host, to_bare);\r
181                 end\r
182                 core_route_stanza(origin, stanza);\r
183                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
184         elseif stanza.attr.type == "unsubscribed" then\r
185                 -- 1. route stanza\r
186                 -- 2. roster push (subscription = none or to)\r
187                 if rostermanager.unsubscribed(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         end\r
192         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
193 end\r
194 \r
195 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
196         local node, host = jid_split(to_bare);\r
197         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
198         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
199         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
200         if stanza.attr.type == "probe" then\r
201                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
202                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
203                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
204                         end\r
205                 else\r
206                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
207                 end\r
208         elseif stanza.attr.type == "subscribe" then\r
209                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
210                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
211                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate\r
212                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
213                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
214                         end\r
215                 else\r
216                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
217                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
218                                         sessionmanager.send_to_available_resources(node, host, stanza);\r
219                                 end -- TODO else return error, unable to save\r
220                         end\r
221                 end\r
222         elseif stanza.attr.type == "unsubscribe" then\r
223                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
224                         rostermanager.roster_push(node, host, from_bare);\r
225                 end\r
226         elseif stanza.attr.type == "subscribed" then\r
227                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
228                         rostermanager.roster_push(node, host, from_bare);\r
229                 end\r
230         elseif stanza.attr.type == "unsubscribed" then\r
231                 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then\r
232                         rostermanager.roster_push(node, host, from_bare);\r
233                 end\r
234         end -- discard any other type\r
235         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
236 end\r
237 \r
238 return _M;\r