Don't overwrite config on make install if it already exists
[prosody.git] / core / presencemanager.lua
1 \r
2 local log = require "util.logger".init("presencemanager")\r
3 \r
4 local require = require;\r
5 local pairs, ipairs = pairs, ipairs;\r
6 local t_concat = table.concat;\r
7 local s_find = string.find;\r
8 local tonumber = tonumber;\r
9 \r
10 local st = require "util.stanza";\r
11 local jid_split = require "util.jid".split;\r
12 local hosts = hosts;\r
13 \r
14 local rostermanager = require "core.rostermanager";\r
15 local sessionmanager = require "core.sessionmanager";\r
16 local offlinemanager = require "core.offlinemanager";\r
17 \r
18 module "presencemanager"\r
19 \r
20 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)\r
21         local type = stanza.attr.type;\r
22         if type and type ~= "unavailable" then\r
23                 if inbound then\r
24                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
25                 else\r
26                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
27                 end\r
28         elseif not inbound and not stanza.attr.to then\r
29                 handle_normal_presence(origin, stanza, core_route_stanza);\r
30         else\r
31                 core_route_stanza(origin, stanza);\r
32         end\r
33 end\r
34 \r
35 function handle_normal_presence(origin, stanza, core_route_stanza)\r
36         if origin.roster then\r
37                 for jid in pairs(origin.roster) do -- broadcast to all interested contacts\r
38                         local subscription = origin.roster[jid].subscription;\r
39                         if subscription == "both" or subscription == "from" then\r
40                                 stanza.attr.to = jid;\r
41                                 core_route_stanza(origin, stanza);\r
42                         end\r
43                 end\r
44                 local node, host = jid_split(stanza.attr.from);\r
45                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources\r
46                         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
47                                 stanza.attr.to = res.full_jid;\r
48                                 core_route_stanza(origin, stanza);\r
49                         end\r
50                 end\r
51                 if stanza.attr.type == nil and not origin.presence then -- initial presence\r
52                         local probe = st.presence({from = origin.full_jid, type = "probe"});\r
53                         for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to\r
54                                 local subscription = origin.roster[jid].subscription;\r
55                                 if subscription == "both" or subscription == "to" then\r
56                                         probe.attr.to = jid;\r
57                                         core_route_stanza(origin, probe);\r
58                                 end\r
59                         end\r
60                         for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources\r
61                                 if res ~= origin and res.presence then\r
62                                         res.presence.attr.to = origin.full_jid;\r
63                                         core_route_stanza(res, res.presence);\r
64                                         res.presence.attr.to = nil;\r
65                                 end\r
66                         end\r
67                         if origin.roster.pending then -- resend incoming subscription requests\r
68                                 for jid in pairs(origin.roster.pending) do\r
69                                         origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?\r
70                                 end\r
71                         end\r
72                         local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});\r
73                         for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests\r
74                                 if item.ask then\r
75                                         request.attr.to = jid;\r
76                                         core_route_stanza(origin, request);\r
77                                 end\r
78                         end\r
79                         for _, msg in ipairs(offlinemanager.load(node, host) or {}) do\r
80                                 origin.send(msg); -- FIXME do we need to modify to/from in any way?\r
81                         end\r
82                         offlinemanager.deleteAll(node, host);\r
83                 end\r
84                 origin.priority = 0;\r
85                 if stanza.attr.type == "unavailable" then\r
86                         origin.presence = nil;\r
87                 else\r
88                         origin.presence = stanza;\r
89                         local priority = stanza:child_with_name("priority");\r
90                         if priority and #priority > 0 then\r
91                                 priority = t_concat(priority);\r
92                                 if s_find(priority, "^[+-]?[0-9]+$") then\r
93                                         priority = tonumber(priority);\r
94                                         if priority < -128 then priority = -128 end\r
95                                         if priority > 127 then priority = 127 end\r
96                                         origin.priority = priority;\r
97                                 end\r
98                         end\r
99                 end\r
100                 stanza.attr.to = nil; -- reset it\r
101         else\r
102                 log("error", "presence recieved from client with no roster");\r
103         end\r
104 end\r
105 \r
106 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)\r
107         local h = hosts[host];\r
108         local count = 0;\r
109         if h and h.type == "local" then\r
110                 local u = h.sessions[user];\r
111                 if u then\r
112                         for k, session in pairs(u.sessions) do\r
113                                 local pres = session.presence;\r
114                                 if pres then\r
115                                         pres.attr.to = jid;\r
116                                         pres.attr.from = session.full_jid;\r
117                                         core_route_stanza(session, pres);\r
118                                         pres.attr.to = nil;\r
119                                         pres.attr.from = nil;\r
120                                         count = count + 1;\r
121                                 end\r
122                         end\r
123                 end\r
124         end\r
125         log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);\r
126         return count;\r
127 end\r
128 \r
129 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
130         local node, host = jid_split(from_bare);\r
131         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
132         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
133         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
134         if stanza.attr.type == "subscribe" then\r
135                 -- 1. route stanza\r
136                 -- 2. roster push (subscription = none, ask = subscribe)\r
137                 if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
138                         rostermanager.roster_push(node, host, to_bare);\r
139                 end -- else file error\r
140                 core_route_stanza(origin, stanza);\r
141         elseif stanza.attr.type == "unsubscribe" then\r
142                 -- 1. route stanza\r
143                 -- 2. roster push (subscription = none or from)\r
144                 if rostermanager.unsubscribe(node, host, to_bare) then\r
145                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
146                 end -- else file error\r
147                 core_route_stanza(origin, stanza);\r
148         elseif stanza.attr.type == "subscribed" then\r
149                 -- 1. route stanza\r
150                 -- 2. roster_push ()\r
151                 -- 3. send_presence_of_available_resources\r
152                 if rostermanager.subscribed(node, host, to_bare) then\r
153                         rostermanager.roster_push(node, host, to_bare);\r
154                 end\r
155                 core_route_stanza(origin, stanza);\r
156                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
157         elseif stanza.attr.type == "unsubscribed" then\r
158                 -- 1. route stanza\r
159                 -- 2. roster push (subscription = none or to)\r
160                 if rostermanager.unsubscribed(node, host, to_bare) then\r
161                         rostermanager.roster_push(node, host, to_bare);\r
162                 end\r
163                 core_route_stanza(origin, stanza);\r
164         end\r
165         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
166 end\r
167 \r
168 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
169         local node, host = jid_split(to_bare);\r
170         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
171         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
172         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
173         if stanza.attr.type == "probe" then\r
174                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
175                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
176                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
177                         end\r
178                 else\r
179                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
180                 end\r
181         elseif stanza.attr.type == "subscribe" then\r
182                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
183                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
184                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate\r
185                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
186                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
187                         end\r
188                 else\r
189                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
190                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
191                                         sessionmanager.send_to_available_resources(node, host, stanza);\r
192                                 end -- TODO else return error, unable to save\r
193                         end\r
194                 end\r
195         elseif stanza.attr.type == "unsubscribe" then\r
196                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
197                         rostermanager.roster_push(node, host, from_bare);\r
198                 end\r
199         elseif stanza.attr.type == "subscribed" then\r
200                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
201                         rostermanager.roster_push(node, host, from_bare);\r
202                 end\r
203         elseif stanza.attr.type == "unsubscribed" then\r
204                 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then\r
205                         rostermanager.roster_push(node, host, from_bare);\r
206                 end\r
207         end -- discard any other type\r
208         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
209 end\r
210 \r
211 return _M;\r