Forced merge.
[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 = pairs;\r
6 \r
7 local st = require "util.stanza";\r
8 local jid_split = require "util.jid".split;\r
9 local hosts = hosts;\r
10 \r
11 local rostermanager = require "core.rostermanager";\r
12 local sessionmanager = require "core.sessionmanager";\r
13 \r
14 module "presencemanager"\r
15 \r
16 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)\r
17         local h = hosts[host];\r
18         local count = 0;\r
19         if h and h.type == "local" then\r
20                 local u = h.sessions[user];\r
21                 if u then\r
22                         for k, session in pairs(u.sessions) do\r
23                                 local pres = session.presence;\r
24                                 if pres then\r
25                                         pres.attr.to = jid;\r
26                                         pres.attr.from = session.full_jid;\r
27                                         core_route_stanza(session, pres);\r
28                                         pres.attr.to = nil;\r
29                                         pres.attr.from = nil;\r
30                                         count = count + 1;\r
31                                 end\r
32                         end\r
33                 end\r
34         end\r
35         log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);\r
36         return count;\r
37 end\r
38 \r
39 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
40         local node, host = jid_split(from_bare);\r
41         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
42         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
43         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
44         if stanza.attr.type == "subscribe" then\r
45                 -- 1. route stanza\r
46                 -- 2. roster push (subscription = none, ask = subscribe)\r
47                 if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
48                         rostermanager.roster_push(node, host, to_bare);\r
49                 end -- else file error\r
50                 core_route_stanza(origin, stanza);\r
51         elseif stanza.attr.type == "unsubscribe" then\r
52                 -- 1. route stanza\r
53                 -- 2. roster push (subscription = none or from)\r
54                 if rostermanager.unsubscribe(node, host, to_bare) then\r
55                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
56                 end -- else file error\r
57                 core_route_stanza(origin, stanza);\r
58         elseif stanza.attr.type == "subscribed" then\r
59                 -- 1. route stanza\r
60                 -- 2. roster_push ()\r
61                 -- 3. send_presence_of_available_resources\r
62                 if rostermanager.subscribed(node, host, to_bare) then\r
63                         rostermanager.roster_push(node, host, to_bare);\r
64                 end\r
65                 core_route_stanza(origin, stanza);\r
66                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
67         elseif stanza.attr.type == "unsubscribed" then\r
68                 -- 1. route stanza\r
69                 -- 2. roster push (subscription = none or to)\r
70                 if rostermanager.unsubscribed(node, host, to_bare) then\r
71                         rostermanager.roster_push(node, host, to_bare);\r
72                 end\r
73                 core_route_stanza(origin, stanza);\r
74         end\r
75         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
76 end\r
77 \r
78 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
79         local node, host = jid_split(to_bare);\r
80         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
81         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
82         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
83         if stanza.attr.type == "probe" then\r
84                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
85                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
86                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
87                         end\r
88                 else\r
89                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
90                 end\r
91         elseif stanza.attr.type == "subscribe" then\r
92                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
93                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
94                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate\r
95                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
96                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
97                         end\r
98                 else\r
99                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
100                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
101                                         sessionmanager.send_to_available_resources(node, host, stanza);\r
102                                 end -- TODO else return error, unable to save\r
103                         end\r
104                 end\r
105         elseif stanza.attr.type == "unsubscribe" then\r
106                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
107                         rostermanager.roster_push(node, host, from_bare);\r
108                 end\r
109         elseif stanza.attr.type == "subscribed" then\r
110                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
111                         rostermanager.roster_push(node, host, from_bare);\r
112                 end\r
113         elseif stanza.attr.type == "unsubscribed" then\r
114                 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then\r
115                         rostermanager.roster_push(node, host, from_bare);\r
116                 end\r
117         end -- discard any other type\r
118         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
119 end\r
120 \r
121 return _M;\r