70fa41948e7e5ae5f326815c9a5887c05bc217cf
[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         return count;\r
36 end\r
37 \r
38 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
39         local node, host = jid_split(from_bare);\r
40         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
41         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
42         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
43         if stanza.attr.type == "subscribe" then\r
44                 -- 1. route stanza\r
45                 -- 2. roster push (subscription = none, ask = subscribe)\r
46                 if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
47                         rostermanager.roster_push(node, host, to_bare);\r
48                 end -- else file error\r
49                 core_route_stanza(origin, stanza);\r
50         elseif stanza.attr.type == "unsubscribe" then\r
51                 -- 1. route stanza\r
52                 -- 2. roster push (subscription = none or from)\r
53                 if rostermanager.unsubscribe(node, host, to_bare) then\r
54                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
55                 end -- else file error\r
56                 core_route_stanza(origin, stanza);\r
57         elseif stanza.attr.type == "subscribed" then\r
58                 -- 1. route stanza\r
59                 -- 2. roster_push ()\r
60                 -- 3. send_presence_of_available_resources\r
61                 if rostermanager.subscribed(node, host, to_bare) then\r
62                         rostermanager.roster_push(node, host, to_bare);\r
63                         core_route_stanza(origin, stanza);\r
64                         send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
65                 end\r
66         elseif stanza.attr.type == "unsubscribed" then\r
67                 -- 1. route stanza\r
68                 -- 2. roster push (subscription = none or to)\r
69                 if rostermanager.unsubscribed(node, host, to_bare) then\r
70                         rostermanager.roster_push(node, host, to_bare);\r
71                         core_route_stanza(origin, stanza);\r
72                 end\r
73         end\r
74         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
75 end\r
76 \r
77 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
78         local node, host = jid_split(to_bare);\r
79         local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
80         stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
81         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
82         if stanza.attr.type == "probe" then\r
83                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
84                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
85                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
86                         end\r
87                 else\r
88                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
89                 end\r
90         elseif stanza.attr.type == "subscribe" then\r
91                 if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
92                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
93                 else\r
94                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
95                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
96                                         sessionmanager.send_to_available_resources(node, host, stanza);\r
97                                 end -- TODO else return error, unable to save\r
98                         end\r
99                 end\r
100         elseif stanza.attr.type == "unsubscribe" then\r
101                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
102                         rostermanager.roster_push(node, host, from_bare);\r
103                 end\r
104         elseif stanza.attr.type == "subscribed" then\r
105                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
106                         rostermanager.roster_push(node, host, from_bare);\r
107                 end\r
108         elseif stanza.attr.type == "unsubscribed" then\r
109                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
110                         rostermanager.roster_push(node, host, from_bare);\r
111                 end\r
112         end -- discard any other type\r
113         stanza.attr.from, stanza.attr.to = st_from, st_to;\r
114 end\r
115 \r
116 return _M;\r