Automated merge with http://waqas.ath.cx:8000/
[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
99                         local offline = offlinemanager.load(node, host);
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);
105                         end\r
106                 end\r
107                 origin.priority = 0;\r
108                 if stanza.attr.type == "unavailable" then\r
109                         origin.presence = nil;
110                         if origin.directed then
111                                 for _, jid in ipairs(origin.directed) do
112                                         stanza.attr.to = jid;
113                                         core_route_stanza(origin, stanza);
114                                 end
115                                 origin.directed = nil;
116                         end\r
117                 else\r
118                         origin.presence = stanza;\r
119                         local priority = stanza:child_with_name("priority");\r
120                         if priority and #priority > 0 then\r
121                                 priority = t_concat(priority);\r
122                                 if s_find(priority, "^[+-]?[0-9]+$") then\r
123                                         priority = tonumber(priority);\r
124                                         if priority < -128 then priority = -128 end\r
125                                         if priority > 127 then priority = 127 end\r
126                                         origin.priority = priority;\r
127                                 end\r
128                         end\r
129                 end\r
130                 stanza.attr.to = nil; -- reset it\r
131         else\r
132                 log("error", "presence recieved from client with no roster");\r
133         end\r
134 end\r
135 \r
136 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)\r
137         local h = hosts[host];\r
138         local count = 0;\r
139         if h and h.type == "local" then\r
140                 local u = h.sessions[user];\r
141                 if u then\r
142                         for k, session in pairs(u.sessions) do\r
143                                 local pres = session.presence;\r
144                                 if pres then\r
145                                         pres.attr.to = jid;\r
146                                         pres.attr.from = session.full_jid;\r
147                                         core_route_stanza(session, pres);\r
148                                         pres.attr.to = nil;\r
149                                         pres.attr.from = nil;\r
150                                         count = count + 1;\r
151                                 end\r
152                         end\r
153                 end\r
154         end\r
155         log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);\r
156         return count;\r
157 end\r
158 \r
159 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
160         local node, host = jid_split(from_bare);\r
161         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
162         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
163         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
164         if stanza.attr.type == "subscribe" then\r
165                 -- 1. route stanza\r
166                 -- 2. roster push (subscription = none, ask = subscribe)\r
167                 if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
168                         rostermanager.roster_push(node, host, to_bare);\r
169                 end -- else file error\r
170                 core_route_stanza(origin, stanza);\r
171         elseif stanza.attr.type == "unsubscribe" then\r
172                 -- 1. route stanza\r
173                 -- 2. roster push (subscription = none or from)\r
174                 if rostermanager.unsubscribe(node, host, to_bare) then\r
175                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
176                 end -- else file error\r
177                 core_route_stanza(origin, stanza);\r
178         elseif stanza.attr.type == "subscribed" then\r
179                 -- 1. route stanza\r
180                 -- 2. roster_push ()\r
181                 -- 3. send_presence_of_available_resources\r
182                 if rostermanager.subscribed(node, host, to_bare) then\r
183                         rostermanager.roster_push(node, host, to_bare);\r
184                 end\r
185                 core_route_stanza(origin, stanza);\r
186                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
187         elseif stanza.attr.type == "unsubscribed" then\r
188                 -- 1. route stanza\r
189                 -- 2. roster push (subscription = none or to)\r
190                 if rostermanager.unsubscribed(node, host, to_bare) then\r
191                         rostermanager.roster_push(node, host, to_bare);\r
192                 end\r
193                 core_route_stanza(origin, stanza);\r
194         end\r
195         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
196 end\r
197 \r
198 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
199         local node, host = jid_split(to_bare);\r
200         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
201         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
202         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
203         if stanza.attr.type == "probe" then\r
204                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
205                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
206                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
207                         end\r
208                 else\r
209                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
210                 end\r
211         elseif stanza.attr.type == "subscribe" then\r
212                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
213                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
214                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate\r
215                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
216                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
217                         end\r
218                 else\r
219                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
220                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
221                                         sessionmanager.send_to_available_resources(node, host, stanza);\r
222                                 end -- TODO else return error, unable to save\r
223                         end\r
224                 end\r
225         elseif stanza.attr.type == "unsubscribe" then\r
226                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
227                         rostermanager.roster_push(node, host, from_bare);\r
228                 end\r
229         elseif stanza.attr.type == "subscribed" then\r
230                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
231                         rostermanager.roster_push(node, host, from_bare);\r
232                 end\r
233         elseif stanza.attr.type == "unsubscribed" then\r
234                 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then\r
235                         rostermanager.roster_push(node, host, from_bare);\r
236                 end\r
237         end -- discard any other type\r
238         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
239 end\r
240 \r
241 return _M;\r