Software /
code /
prosody
Annotate
core/presencemanager.lua @ 758:b1885732e979
GPL->MIT!
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 30 Jan 2009 17:22:56 +0000 |
parent | 649:5f8382768530 |
child | 759:5cccfb5da6cb |
rev | line source |
---|---|
615 | 1 -- Prosody IM v0.2 |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
468
diff
changeset
|
2 -- Copyright (C) 2008 Matthew Wild |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
468
diff
changeset
|
3 -- Copyright (C) 2008 Waqas Hussain |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
468
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
468
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
468
diff
changeset
|
8 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
468
diff
changeset
|
9 |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local log = require "util.logger".init("presencemanager") |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local require = require; |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
14 local pairs, ipairs = pairs, ipairs; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
15 local t_concat = table.concat; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
16 local s_find = string.find; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
17 local tonumber = tonumber; |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 local st = require "util.stanza"; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 local jid_split = require "util.jid".split; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local hosts = hosts; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 local rostermanager = require "core.rostermanager"; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local sessionmanager = require "core.sessionmanager"; |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
25 local offlinemanager = require "core.offlinemanager"; |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 module "presencemanager" |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
29 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound) |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
30 local type = stanza.attr.type; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
31 if type and type ~= "unavailable" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
32 if inbound then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
33 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
34 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
35 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
36 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
37 elseif not inbound and not stanza.attr.to then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
38 handle_normal_presence(origin, stanza, core_route_stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
39 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
40 core_route_stanza(origin, stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
41 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
42 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
43 |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
44 function handle_normal_presence(origin, stanza, core_route_stanza) |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
45 if origin.roster then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
46 for jid in pairs(origin.roster) do -- broadcast to all interested contacts |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
47 local subscription = origin.roster[jid].subscription; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
48 if subscription == "both" or subscription == "from" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
49 stanza.attr.to = jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
50 core_route_stanza(origin, stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
51 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
52 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
53 local node, host = jid_split(stanza.attr.from); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
54 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
55 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
56 stanza.attr.to = res.full_jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
57 core_route_stanza(origin, stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
58 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
59 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
60 if stanza.attr.type == nil and not origin.presence then -- initial presence |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
61 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
62 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
63 local subscription = origin.roster[jid].subscription; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
64 if subscription == "both" or subscription == "to" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
65 probe.attr.to = jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
66 core_route_stanza(origin, probe); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
67 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
68 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
70 if res ~= origin and res.presence then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
71 res.presence.attr.to = origin.full_jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
72 core_route_stanza(res, res.presence); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
73 res.presence.attr.to = nil; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
74 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
75 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
76 if origin.roster.pending then -- resend incoming subscription requests |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
77 for jid in pairs(origin.roster.pending) do |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
78 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
79 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
80 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
81 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
82 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
83 if item.ask then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
84 request.attr.to = jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
85 core_route_stanza(origin, request); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
86 end |
649
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
87 end |
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
88 local offline = offlinemanager.load(node, host); |
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
89 if offline then |
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
90 for _, msg in ipairs(offline) do |
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
91 origin.send(msg); -- FIXME do we need to modify to/from in any way? |
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
92 end |
5f8382768530
Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents:
641
diff
changeset
|
93 offlinemanager.deleteAll(node, host); |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
94 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
95 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
96 origin.priority = 0; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
97 if stanza.attr.type == "unavailable" then |
641 | 98 origin.presence = nil; |
99 if origin.directed then | |
100 for _, jid in ipairs(origin.directed) do | |
101 stanza.attr.to = jid; | |
102 core_route_stanza(origin, stanza); | |
103 end | |
104 origin.directed = nil; | |
105 end | |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
106 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
107 origin.presence = stanza; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
108 local priority = stanza:child_with_name("priority"); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
109 if priority and #priority > 0 then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
110 priority = t_concat(priority); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
111 if s_find(priority, "^[+-]?[0-9]+$") then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
112 priority = tonumber(priority); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
113 if priority < -128 then priority = -128 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
114 if priority > 127 then priority = 127 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
115 origin.priority = priority; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
116 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
117 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
118 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
119 stanza.attr.to = nil; -- reset it |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
120 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
121 log("error", "presence recieved from client with no roster"); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
122 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
123 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
124 |
322
d370fea67264
Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents:
321
diff
changeset
|
125 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 local h = hosts[host]; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 local count = 0; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 if h and h.type == "local" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 local u = h.sessions[user]; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 if u then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 for k, session in pairs(u.sessions) do |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 local pres = session.presence; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 if pres then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 pres.attr.to = jid; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 pres.attr.from = session.full_jid; |
323
7c9d2cabd245
Second fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents:
322
diff
changeset
|
136 core_route_stanza(session, pres); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 pres.attr.to = nil; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 pres.attr.from = nil; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 count = count + 1; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 end |
325
5de1048b3fbf
Adding logging for outbound presence broadcasts (changed it a bit)
Waqas Hussain <waqas20@gmail.com>
parents:
324
diff
changeset
|
144 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 return count; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 local node, host = jid_split(from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 local st_from, st_to = stanza.attr.from, stanza.attr.to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 stanza.attr.from, stanza.attr.to = from_bare, to_bare; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 if stanza.attr.type == "subscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 -- 2. roster push (subscription = none, ask = subscribe) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 if rostermanager.set_contact_pending_out(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 rostermanager.roster_push(node, host, to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 end -- else file error |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 core_route_stanza(origin, stanza); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 elseif stanza.attr.type == "unsubscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 -- 2. roster push (subscription = none or from) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 if rostermanager.unsubscribe(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed? |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 end -- else file error |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 core_route_stanza(origin, stanza); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 elseif stanza.attr.type == "subscribed" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 -- 2. roster_push () |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 -- 3. send_presence_of_available_resources |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 if rostermanager.subscribed(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 rostermanager.roster_push(node, host, to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 end |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
174 core_route_stanza(origin, stanza); |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
175 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 elseif stanza.attr.type == "unsubscribed" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 -- 2. roster push (subscription = none or to) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 if rostermanager.unsubscribed(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 rostermanager.roster_push(node, host, to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 end |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
182 core_route_stanza(origin, stanza); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 stanza.attr.from, stanza.attr.to = st_from, st_to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 local node, host = jid_split(to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 local st_from, st_to = stanza.attr.from, stanza.attr.to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 stanza.attr.from, stanza.attr.to = from_bare, to_bare; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 if stanza.attr.type == "probe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
322
d370fea67264
Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents:
321
diff
changeset
|
194 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 else |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 elseif stanza.attr.type == "subscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
203 -- Sending presence is not clearly stated in the RFC, but it seems appropriate |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
204 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
205 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
206 end |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 else |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 if not rostermanager.is_contact_pending_in(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 if rostermanager.set_contact_pending_in(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 sessionmanager.send_to_available_resources(node, host, stanza); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 end -- TODO else return error, unable to save |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 elseif stanza.attr.type == "unsubscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 rostermanager.roster_push(node, host, from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 elseif stanza.attr.type == "subscribed" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 rostermanager.roster_push(node, host, from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 elseif stanza.attr.type == "unsubscribed" then |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
223 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 rostermanager.roster_push(node, host, from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 end -- discard any other type |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 stanza.attr.from, stanza.attr.to = st_from, st_to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 return _M; |