Annotate

plugins/mod_presence.lua @ 4036:32b55f06a3bd

core.offlinemanager, mod_message, mod_presence: Removed core.offlinemanager in favor of mod_offline.
author Robert Hoelz <rob@hoelz.ro>
date Thu, 09 Dec 2010 12:23:37 -0600
parent 3540:bc139431830b
child 3969:32d4a73901e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1508
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2643
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2643
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
4 --
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
6 -- COPYING file in the source package for more information.
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
7 --
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
8
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
9 local log = module._log;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
10
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
11 local require = require;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
12 local pairs, ipairs = pairs, ipairs;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
13 local t_concat, t_insert = table.concat, table.insert;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
14 local s_find = string.find;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
15 local tonumber = tonumber;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
16
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
17 local st = require "util.stanza";
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
18 local jid_split = require "util.jid".split;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
19 local jid_bare = require "util.jid".bare;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
20 local hosts = hosts;
2592
3af266f7037f mod_presence: Don't depend on sessions array existing for a user when handling outgoing presence broadcast.
Waqas Hussain <waqas20@gmail.com>
parents: 2532
diff changeset
21 local NULL = {};
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
22
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
23 local rostermanager = require "core.rostermanager";
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
24 local sessionmanager = require "core.sessionmanager";
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
25
3082
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
26 local function select_top_resources(user)
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
27 local priority = 0;
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
28 local recipients = {};
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
29 for _, session in pairs(user.sessions) do -- find resource with greatest priority
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
30 if session.presence then
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
31 -- TODO check active privacy list for session
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
32 local p = session.priority;
3082
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
33 if p > priority then
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
34 priority = p;
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
35 recipients = {session};
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
36 elseif p == priority then
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
37 t_insert(recipients, session);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
38 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
39 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
40 end
3082
920cd9667c7b Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
Matthew Wild <mwild1@gmail.com>
parents: 2644
diff changeset
41 return recipients;
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
42 end
2593
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
43 local function recalc_resource_map(user)
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
44 if user then
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
45 user.top_resources = select_top_resources(user);
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
46 if #user.top_resources == 0 then user.top_resources = nil; end
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
47 end
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
48 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
49
3084
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
50 local ignore_presence_priority = module:get_option("ignore_presence_priority");
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
51
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
52 function handle_normal_presence(origin, stanza)
3084
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
53 if ignore_presence_priority then
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
54 local priority = stanza:child_with_name("priority");
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
55 if priority and priority[1] ~= "0" then
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
56 for i=#priority.tags,1,-1 do priority.tags[i] = nil; end
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
57 for i=#priority,1,-1 do priority[i] = nil; end
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
58 priority[1] = "0";
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
59 end
9b17449fb5f4 mod_presence: Added option 'ignore_presence_priority'.
Waqas Hussain <waqas20@gmail.com>
parents: 3083
diff changeset
60 end
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
61 local priority = stanza:child_with_name("priority");
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
62 if priority and #priority > 0 then
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
63 priority = t_concat(priority);
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
64 if s_find(priority, "^[+-]?[0-9]+$") then
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
65 priority = tonumber(priority);
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
66 if priority < -128 then priority = -128 end
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
67 if priority > 127 then priority = 127 end
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
68 else priority = 0; end
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
69 else priority = 0; end
2643
21eb99b06f4d mod_presence: Reflect the user's own presence back to them.
Waqas Hussain <waqas20@gmail.com>
parents: 2593
diff changeset
70 if full_sessions[origin.full_jid] then -- if user is still connected
21eb99b06f4d mod_presence: Reflect the user's own presence back to them.
Waqas Hussain <waqas20@gmail.com>
parents: 2593
diff changeset
71 origin.send(stanza); -- reflect their presence back to them
21eb99b06f4d mod_presence: Reflect the user's own presence back to them.
Waqas Hussain <waqas20@gmail.com>
parents: 2593
diff changeset
72 end
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
73 local roster = origin.roster;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
74 local node, host = origin.username, origin.host;
2592
3af266f7037f mod_presence: Don't depend on sessions array existing for a user when handling outgoing presence broadcast.
Waqas Hussain <waqas20@gmail.com>
parents: 2532
diff changeset
75 local user = bare_sessions[node.."@"..host];
3af266f7037f mod_presence: Don't depend on sessions array existing for a user when handling outgoing presence broadcast.
Waqas Hussain <waqas20@gmail.com>
parents: 2532
diff changeset
76 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
77 if res ~= origin and res.presence then -- to resource
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
78 stanza.attr.to = res.full_jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
79 core_post_stanza(origin, stanza, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
80 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
81 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
82 for jid, item in pairs(roster) do -- broadcast to all interested contacts
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
83 if item.subscription == "both" or item.subscription == "from" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
84 stanza.attr.to = jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
85 core_post_stanza(origin, stanza, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
86 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
87 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
88 if stanza.attr.type == nil and not origin.presence then -- initial presence
2532
b1b1b4a7db26 mod_presence: Quick fix to make probes from local users to local hosts work.
Waqas Hussain <waqas20@gmail.com>
parents: 2531
diff changeset
89 origin.presence = stanza; -- FIXME repeated later
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
90 local probe = st.presence({from = origin.full_jid, type = "probe"});
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
91 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
92 if item.subscription == "both" or item.subscription == "to" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
93 probe.attr.to = jid;
3183
28a5c0bda928 mod_presence: Enable firing of pre-events for probes.
Waqas Hussain <waqas20@gmail.com>
parents: 3182
diff changeset
94 core_post_stanza(origin, probe, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
95 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
96 end
2592
3af266f7037f mod_presence: Don't depend on sessions array existing for a user when handling outgoing presence broadcast.
Waqas Hussain <waqas20@gmail.com>
parents: 2532
diff changeset
97 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
98 if res ~= origin and res.presence then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
99 res.presence.attr.to = origin.full_jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
100 core_post_stanza(res, res.presence, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
101 res.presence.attr.to = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
102 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
103 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
104 if roster.pending then -- resend incoming subscription requests
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
105 for jid in pairs(roster.pending) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
106 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
107 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
108 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
109 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
110 for jid, item in pairs(roster) do -- resend outgoing subscription requests
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
111 if item.ask then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
112 request.attr.to = jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
113 core_post_stanza(origin, request, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
114 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
115 end
3518
d3399d1b484a Don't send offline messages to resource with negative priorities
Robert Hoelz <rob@hoelz.ro>
parents: 3235
diff changeset
116
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
117 if priority >= 0 then
4036
32b55f06a3bd core.offlinemanager, mod_message, mod_presence: Removed core.offlinemanager in favor of mod_offline.
Robert Hoelz <rob@hoelz.ro>
parents: 3540
diff changeset
118 local event = { origin = origin }
32b55f06a3bd core.offlinemanager, mod_message, mod_presence: Removed core.offlinemanager in favor of mod_offline.
Robert Hoelz <rob@hoelz.ro>
parents: 3540
diff changeset
119 module:fire_event('message/offline/broadcast', event);
32b55f06a3bd core.offlinemanager, mod_message, mod_presence: Removed core.offlinemanager in favor of mod_offline.
Robert Hoelz <rob@hoelz.ro>
parents: 3540
diff changeset
120 module:fire_event('message/offline/delete', event);
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3518
diff changeset
121 end
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
122 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
123 if stanza.attr.type == "unavailable" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
124 origin.presence = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
125 if origin.priority then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
126 origin.priority = nil;
2593
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
127 recalc_resource_map(user);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
128 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
129 if origin.directed then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
130 for jid in pairs(origin.directed) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
131 stanza.attr.to = jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
132 core_post_stanza(origin, stanza, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
133 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
134 origin.directed = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
135 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
136 else
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
137 origin.presence = stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
138 if origin.priority ~= priority then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
139 origin.priority = priority;
2593
06995c8bfe80 mod_presence: Don't depend on user being online when calculating top resources.
Waqas Hussain <waqas20@gmail.com>
parents: 2592
diff changeset
140 recalc_resource_map(user);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
141 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
142 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
143 stanza.attr.to = nil; -- reset it
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
144 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
145
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
146 function send_presence_of_available_resources(user, host, jid, recipient_session, stanza)
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
147 local h = hosts[host];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
148 local count = 0;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
149 if h and h.type == "local" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
150 local u = h.sessions[user];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
151 if u then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
152 for k, session in pairs(u.sessions) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
153 local pres = session.presence;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
154 if pres then
2038
4ab83f96585a mod_presence: Send unavailable presence when sending unsubscribed presence stanzas (some legacy clients continue to display unsubscribed contacts as available).
Waqas Hussain <waqas20@gmail.com>
parents: 1731
diff changeset
155 if stanza then pres = stanza; pres.attr.from = session.full_jid; end
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
156 pres.attr.to = jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
157 core_post_stanza(session, pres, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
158 pres.attr.to = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
159 count = count + 1;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
160 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
161 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
162 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
163 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
164 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
165 return count;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
166 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
167
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
168 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
169 local node, host = jid_split(from_bare);
3235
651139e831b1 mod_presence: Fixed a traceback on outgoing subscriptions from offline users.
Waqas Hussain <waqas20@gmail.com>
parents: 3215
diff changeset
170 if to_bare == from_bare then return; end -- No self contacts
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
171 local st_from, st_to = stanza.attr.from, stanza.attr.to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
172 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
173 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
3182
a19dbb6446a0 mod_presence: Don't stop pre-event for outgoing probes.
Waqas Hussain <waqas20@gmail.com>
parents: 3181
diff changeset
174 if stanza.attr.type == "probe" then
a19dbb6446a0 mod_presence: Don't stop pre-event for outgoing probes.
Waqas Hussain <waqas20@gmail.com>
parents: 3181
diff changeset
175 stanza.attr.from, stanza.attr.to = st_from, st_to;
a19dbb6446a0 mod_presence: Don't stop pre-event for outgoing probes.
Waqas Hussain <waqas20@gmail.com>
parents: 3181
diff changeset
176 return;
a19dbb6446a0 mod_presence: Don't stop pre-event for outgoing probes.
Waqas Hussain <waqas20@gmail.com>
parents: 3181
diff changeset
177 elseif stanza.attr.type == "subscribe" then
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
178 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
179 -- 2. roster push (subscription = none, ask = subscribe)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
180 if rostermanager.set_contact_pending_out(node, host, to_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
181 rostermanager.roster_push(node, host, to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
182 end -- else file error
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
183 core_post_stanza(origin, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
184 elseif stanza.attr.type == "unsubscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
185 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
186 -- 2. roster push (subscription = none or from)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
187 if rostermanager.unsubscribe(node, host, to_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
188 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
189 end -- else file error
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
190 core_post_stanza(origin, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
191 elseif stanza.attr.type == "subscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
192 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
193 -- 2. roster_push ()
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
194 -- 3. send_presence_of_available_resources
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
195 if rostermanager.subscribed(node, host, to_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
196 rostermanager.roster_push(node, host, to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
197 end
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
198 core_post_stanza(origin, stanza);
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
199 send_presence_of_available_resources(node, host, to_bare, origin);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
200 elseif stanza.attr.type == "unsubscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
201 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
202 -- 2. roster push (subscription = none or to)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
203 if rostermanager.unsubscribed(node, host, to_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
204 rostermanager.roster_push(node, host, to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
205 end
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
206 core_post_stanza(origin, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
207 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
208 stanza.attr.from, stanza.attr.to = st_from, st_to;
3181
1f73b3a960cf mod_presence: Small refactoring.
Waqas Hussain <waqas20@gmail.com>
parents: 3156
diff changeset
209 return true;
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
210 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
211
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
212 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
213 local node, host = jid_split(to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
214 local st_from, st_to = stanza.attr.from, stanza.attr.to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
215 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
216 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
1568
b11bac42d56f mod_presence: Ignore presence sent to host and invalid JIDs, fixes traceback (thanks Deepspawn)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
217
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
218 if stanza.attr.type == "probe" then
3112
c311fdffccce mod_presence: Don't send 'unsubscribed' in response to probes when roster loading fails.
Waqas Hussain <waqas20@gmail.com>
parents: 3109
diff changeset
219 local result, err = rostermanager.is_contact_subscribed(node, host, from_bare);
c311fdffccce mod_presence: Don't send 'unsubscribed' in response to probes when roster loading fails.
Waqas Hussain <waqas20@gmail.com>
parents: 3109
diff changeset
220 if result then
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
221 if 0 == send_presence_of_available_resources(node, host, st_from, origin) then
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
222 core_post_stanza(hosts[host], st.presence({from=to_bare, to=st_from, type="unavailable"}), true); -- TODO send last activity
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
223 end
3112
c311fdffccce mod_presence: Don't send 'unsubscribed' in response to probes when roster loading fails.
Waqas Hussain <waqas20@gmail.com>
parents: 3109
diff changeset
224 elseif not err then
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
225 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}), true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
226 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
227 elseif stanza.attr.type == "subscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
228 if rostermanager.is_contact_subscribed(node, host, from_bare) then
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
229 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"}), true); -- already subscribed
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
230 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
231 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
232 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- TODO send last activity
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
233 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
234 else
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
235 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- acknowledging receipt
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
236 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
237 if rostermanager.set_contact_pending_in(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
238 sessionmanager.send_to_available_resources(node, host, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
239 end -- TODO else return error, unable to save
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
240 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
241 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
242 elseif stanza.attr.type == "unsubscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
243 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
2143
13ac68e8760b mod_presence: Route incoming presence subscription stanzas (types unsubscribe, subscribed and unsubscribed) to the user before roster pushes.
Waqas Hussain <waqas20@gmail.com>
parents: 2142
diff changeset
244 sessionmanager.send_to_interested_resources(node, host, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
245 rostermanager.roster_push(node, host, from_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
246 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
247 elseif stanza.attr.type == "subscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
248 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
2143
13ac68e8760b mod_presence: Route incoming presence subscription stanzas (types unsubscribe, subscribed and unsubscribed) to the user before roster pushes.
Waqas Hussain <waqas20@gmail.com>
parents: 2142
diff changeset
249 sessionmanager.send_to_interested_resources(node, host, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
250 rostermanager.roster_push(node, host, from_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
251 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
252 elseif stanza.attr.type == "unsubscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
253 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
2143
13ac68e8760b mod_presence: Route incoming presence subscription stanzas (types unsubscribe, subscribed and unsubscribed) to the user before roster pushes.
Waqas Hussain <waqas20@gmail.com>
parents: 2142
diff changeset
254 sessionmanager.send_to_interested_resources(node, host, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
255 rostermanager.roster_push(node, host, from_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
256 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
257 end -- discard any other type
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
258 stanza.attr.from, stanza.attr.to = st_from, st_to;
3181
1f73b3a960cf mod_presence: Small refactoring.
Waqas Hussain <waqas20@gmail.com>
parents: 3156
diff changeset
259 return true;
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
260 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
261
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
262 local outbound_presence_handler = function(data)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
263 -- outbound presence recieved
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
264 local origin, stanza = data.origin, data.stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
265
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
266 local to = stanza.attr.to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
267 if to then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
268 local t = stanza.attr.type;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
269 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
3181
1f73b3a960cf mod_presence: Small refactoring.
Waqas Hussain <waqas20@gmail.com>
parents: 3156
diff changeset
270 return handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
271 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
272
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
273 local to_bare = jid_bare(to);
3215
e55782f20679 mod_presence: Fixed a traceback.
Waqas Hussain <waqas20@gmail.com>
parents: 3198
diff changeset
274 local roster = origin.roster;
e55782f20679 mod_presence: Fixed a traceback.
Waqas Hussain <waqas20@gmail.com>
parents: 3198
diff changeset
275 if roster and not(roster[to_bare] and (roster[to_bare].subscription == "both" or roster[to_bare].subscription == "from")) then -- directed presence
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
276 origin.directed = origin.directed or {};
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
277 if t then -- removing from directed presence list on sending an error or unavailable
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
278 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
279 else
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
280 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
281 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
282 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
283 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
284 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
285
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
286 module:hook("pre-presence/full", outbound_presence_handler);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
287 module:hook("pre-presence/bare", outbound_presence_handler);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
288 module:hook("pre-presence/host", outbound_presence_handler);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
289
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
290 module:hook("presence/bare", function(data)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
291 -- inbound presence to bare JID recieved
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
292 local origin, stanza = data.origin, data.stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
293
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
294 local to = stanza.attr.to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
295 local t = stanza.attr.type;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
296 if to then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
297 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
3181
1f73b3a960cf mod_presence: Small refactoring.
Waqas Hussain <waqas20@gmail.com>
parents: 3156
diff changeset
298 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
299 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
300
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
301 local user = bare_sessions[to];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
302 if user then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
303 for _, session in pairs(user.sessions) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
304 if session.presence then -- only send to available resources
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
305 session.send(stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
306 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
307 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
308 end -- no resources not online, discard
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
309 elseif not t or t == "unavailable" then
3153
fd435cab928f mod_presence: Removed unnecessary parameter 'core_route_stanza' from various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3112
diff changeset
310 handle_normal_presence(origin, stanza);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
311 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
312 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
313 end);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
314 module:hook("presence/full", function(data)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
315 -- inbound presence to full JID recieved
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
316 local origin, stanza = data.origin, data.stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
317
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
318 local t = stanza.attr.type;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
319 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
3181
1f73b3a960cf mod_presence: Small refactoring.
Waqas Hussain <waqas20@gmail.com>
parents: 3156
diff changeset
320 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
321 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
322
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
323 local session = full_sessions[stanza.attr.to];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
324 if session then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
325 -- TODO fire post processing event
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
326 session.send(stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
327 end -- resource not online, discard
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
328 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
329 end);
2531
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
330 module:hook("presence/host", function(data)
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
331 -- inbound presence to the host
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
332 local origin, stanza = data.origin, data.stanza;
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
333
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
334 local from_bare = jid_bare(stanza.attr.from);
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
335 local t = stanza.attr.type;
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
336 if t == "probe" then
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
337 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
2531
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
338 elseif t == "subscribe" then
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
339 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
340 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
2531
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
341 end
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
342 return true;
697ff4f98033 mod_presence: Added handler for presence subscriptions and probes to local hosts.
Waqas Hussain <waqas20@gmail.com>
parents: 2525
diff changeset
343 end);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
344
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
345 module:hook("resource-unbind", function(event)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
346 local session, err = event.session, event.error;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
347 -- Send unavailable presence
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
348 if session.presence then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
349 local pres = st.presence{ type = "unavailable" };
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
350 if not(err) or err == "closed" then err = "connection closed"; end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
351 pres:tag("status"):text("Disconnected: "..err):up();
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
352 session:dispatch_stanza(pres);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
353 elseif session.directed then
1606
bbadcaab0036 mod_presence: Tag outgoing unavailables generated by a disconnect with a from attribute
Matthew Wild <mwild1@gmail.com>
parents: 1568
diff changeset
354 local pres = st.presence{ type = "unavailable", from = session.full_jid };
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
355 if not(err) or err == "closed" then err = "connection closed"; end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
356 pres:tag("status"):text("Disconnected: "..err):up();
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
357 for jid in pairs(session.directed) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
358 pres.attr.to = jid;
3198
5a4e766a3577 mod_presence: Enable firing of pre-events for all outbound stanzas, and switching completely to core_post_stanza.
Waqas Hussain <waqas20@gmail.com>
parents: 3196
diff changeset
359 core_post_stanza(session, pres, true);
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
360 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
361 session.directed = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
362 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
363 end);