Annotate

plugins/mod_presence.lua @ 1286:a9b1675ad16e

mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
author Waqas Hussain <waqas20@gmail.com>
date Tue, 02 Jun 2009 20:10:25 +0500
parent 1285:0a6e2d6ae459
child 1287:ac82c7b9c76b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.4
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
1210
342f401f354c mod_presence: Use logger supplied by modulemanager
Matthew Wild <mwild1@gmail.com>
parents: 1209
diff changeset
9 local log = module._log;
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local require = require;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local pairs, ipairs = pairs, ipairs;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local t_concat = table.concat;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local s_find = string.find;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local tonumber = tonumber;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local st = require "util.stanza";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local jid_split = require "util.jid".split;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local jid_bare = require "util.jid".bare;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local hosts = hosts;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local rostermanager = require "core.rostermanager";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local sessionmanager = require "core.sessionmanager";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local offlinemanager = require "core.offlinemanager";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25
1044
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
26 local _core_route_stanza = core_route_stanza;
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
27 local core_route_stanza;
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
28 function core_route_stanza(origin, stanza)
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
29 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
30 local node, host = jid_split(stanza.attr.to);
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
31 host = hosts[host];
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
32 if host and host.type == "local" then
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
33 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
34 return;
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
35 end
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
36 end
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
37 _core_route_stanza(origin, stanza);
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
38 end
41a0c76127f4 mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1023
diff changeset
39
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 local type = stanza.attr.type;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 if type and type ~= "unavailable" and type ~= "error" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 if inbound then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 elseif not inbound and not stanza.attr.to then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 handle_normal_presence(origin, stanza, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 function handle_normal_presence(origin, stanza, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 if origin.roster then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 local subscription = origin.roster[jid].subscription;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 if subscription == "both" or subscription == "from" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 stanza.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 local node, host = jid_split(stanza.attr.from);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
1057
2677f5a4e66b mod_presence: Broadcast a user's presence to only the user's 'available' resources
Waqas Hussain <waqas20@gmail.com>
parents: 1044
diff changeset
66 if res ~= origin and res.presence then -- to resource
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 stanza.attr.to = res.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 if stanza.attr.type == nil and not origin.presence then -- initial presence
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 local probe = st.presence({from = origin.full_jid, type = "probe"});
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 local subscription = origin.roster[jid].subscription;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 if subscription == "both" or subscription == "to" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 probe.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 core_route_stanza(origin, probe);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 if res ~= origin and res.presence then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 res.presence.attr.to = origin.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 core_route_stanza(res, res.presence);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 res.presence.attr.to = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 if origin.roster.pending then -- resend incoming subscription requests
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 for jid in pairs(origin.roster.pending) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 if item.ask then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 request.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 core_route_stanza(origin, request);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 local offline = offlinemanager.load(node, host);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 if offline then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 for _, msg in ipairs(offline) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 origin.send(msg); -- FIXME do we need to modify to/from in any way?
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 offlinemanager.deleteAll(node, host);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 origin.priority = 0;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 if stanza.attr.type == "unavailable" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 origin.presence = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 if origin.directed then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 local old_from = stanza.attr.from;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 stanza.attr.from = origin.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 for jid in pairs(origin.directed) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 stanza.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 stanza.attr.from = old_from;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 origin.directed = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 origin.presence = stanza;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 local priority = stanza:child_with_name("priority");
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 if priority and #priority > 0 then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 priority = t_concat(priority);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 if s_find(priority, "^[+-]?[0-9]+$") then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 priority = tonumber(priority);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 if priority < -128 then priority = -128 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 if priority > 127 then priority = 127 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 origin.priority = priority;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 stanza.attr.to = nil; -- reset it
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 else
1211
d60e68855176 mod_presence: Lower some log levels to their correct values
Matthew Wild <mwild1@gmail.com>
parents: 1210
diff changeset
135 log("warn", "presence recieved from client with no roster");
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 local h = hosts[host];
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 local count = 0;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 if h and h.type == "local" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 local u = h.sessions[user];
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 if u then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 for k, session in pairs(u.sessions) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 local pres = session.presence;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 if pres then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 pres.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 core_route_stanza(session, pres);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 pres.attr.to = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 count = count + 1;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 end
1211
d60e68855176 mod_presence: Lower some log levels to their correct values
Matthew Wild <mwild1@gmail.com>
parents: 1210
diff changeset
156 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 return count;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 local node, host = jid_split(from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 local st_from, st_to = stanza.attr.from, stanza.attr.to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 if stanza.attr.type == "subscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 -- 2. roster push (subscription = none, ask = subscribe)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 if rostermanager.set_contact_pending_out(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 rostermanager.roster_push(node, host, to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 end -- else file error
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 elseif stanza.attr.type == "unsubscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 -- 2. roster push (subscription = none or from)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 if rostermanager.unsubscribe(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end -- else file error
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 elseif stanza.attr.type == "subscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 -- 2. roster_push ()
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 -- 3. send_presence_of_available_resources
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 if rostermanager.subscribed(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 rostermanager.roster_push(node, host, to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 elseif stanza.attr.type == "unsubscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 -- 2. roster push (subscription = none or to)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 if rostermanager.unsubscribed(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 rostermanager.roster_push(node, host, to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196 stanza.attr.from, stanza.attr.to = st_from, st_to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 local node, host = jid_split(to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 local st_from, st_to = stanza.attr.from, stanza.attr.to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 if stanza.attr.type == "probe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 if rostermanager.is_contact_subscribed(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 elseif stanza.attr.type == "subscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 if rostermanager.is_contact_subscribed(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 if rostermanager.set_contact_pending_in(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 sessionmanager.send_to_available_resources(node, host, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 end -- TODO else return error, unable to save
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 elseif stanza.attr.type == "unsubscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 rostermanager.roster_push(node, host, from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 elseif stanza.attr.type == "subscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 rostermanager.roster_push(node, host, from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 elseif stanza.attr.type == "unsubscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 rostermanager.roster_push(node, host, from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 end -- discard any other type
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 stanza.attr.from, stanza.attr.to = st_from, st_to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 local function presence_handler(data)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 local origin, stanza = data.origin, data.stanza;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 local to = stanza.attr.to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 local node, host = jid_split(to);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 local to_bare = jid_bare(to);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 local from_bare = jid_bare(stanza.attr.from);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 if origin.type == "c2s" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 origin.directed = origin.directed or {};
1150
d71a8f28f18b mod_presence: Added a FIXME comment about directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 1147
diff changeset
251 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 end
1019
8d750336e517 mod_presence: Fix incorrect internal routing for probes and subscriptions
Waqas Hussain <waqas20@gmail.com>
parents: 1011
diff changeset
253 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 elseif not to then
1011
beb039827c9f Stopped using presencemanager in stanza_router
Waqas Hussain <waqas20@gmail.com>
parents: 1009
diff changeset
256 handle_normal_presence(origin, stanza, core_route_stanza);
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then
1019
8d750336e517 mod_presence: Fix incorrect internal routing for probes and subscriptions
Waqas Hussain <waqas20@gmail.com>
parents: 1011
diff changeset
261 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
265 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
266 end
1147
513c1d071045 mod_presence: return true from the presence handler
Waqas Hussain <waqas20@gmail.com>
parents: 1057
diff changeset
267 return true;
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
269
1245
be5fe60bd866 mod_presence: Changed to use the prosody.events object directly, rather than through eventmanager2
Waqas Hussain <waqas20@gmail.com>
parents: 1211
diff changeset
270 prosody.events.add_handler(module:get_host().."/presence", presence_handler);
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 module.unload = function()
1245
be5fe60bd866 mod_presence: Changed to use the prosody.events object directly, rather than through eventmanager2
Waqas Hussain <waqas20@gmail.com>
parents: 1211
diff changeset
272 prosody.events.remove_handler(module:get_host().."/presence", presence_handler);
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
273 end
1276
d0e80c1578e1 mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents: 1245
diff changeset
274
1279
fa00d56a9fd3 mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1278
diff changeset
275 local outbound_presence_handler = function(data)
1282
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
276 -- outbound presence recieved
1276
d0e80c1578e1 mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents: 1245
diff changeset
277 local origin, stanza = data.origin, data.stanza;
d0e80c1578e1 mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents: 1245
diff changeset
278
1286
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
279 local to = stanza.attr.to;
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
280 if to then
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
281 local t = stanza.attr.type;
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
282 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
283 handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
284 return true;
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
285 end
1278
2abf85791f29 mod_presence: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents: 1277
diff changeset
286
1286
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
287 local to_bare = jid_bare(to);
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
288 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
289 origin.directed = origin.directed or {};
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
290 if t then -- removing from directed presence list on sending an error or unavailable
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
291 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
292 else
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
293 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
294 end
1277
f2b50efe8d44 mod_presence: Remove JIDs from directed presence list on sending error or unavailable presence
Waqas Hussain <waqas20@gmail.com>
parents: 1276
diff changeset
295 end
1286
a9b1675ad16e mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1285
diff changeset
296 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
1279
fa00d56a9fd3 mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1278
diff changeset
297 end
fa00d56a9fd3 mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1278
diff changeset
298
fa00d56a9fd3 mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1278
diff changeset
299 module:hook("pre-presence/full", outbound_presence_handler);
fa00d56a9fd3 mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1278
diff changeset
300 module:hook("pre-presence/bare", outbound_presence_handler);
fa00d56a9fd3 mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1278
diff changeset
301 module:hook("pre-presence/host", outbound_presence_handler);
1281
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
302
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
303 module:hook("presence/bare", function(data)
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
304 -- inbound presence to bare JID recieved
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
305 local origin, stanza = data.origin, data.stanza;
1282
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
306
1283
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
307 local to = stanza.attr.to;
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
308 if to then
1285
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
309 local t = stanza.attr.type;
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
310 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
311 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
312 return true;
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
313 end
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
314
1283
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
315 local user = bare_sessions[to];
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
316 if user then
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
317 for _, session in pairs(user.sessions) do
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
318 if session.presence then -- only send to available resources
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
319 session.send(stanza);
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
320 end
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
321 end
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
322 end -- no resources not online, discard
1285
0a6e2d6ae459 mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 1284
diff changeset
323 elseif not t or t == "unavailable" then
1283
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
324 handle_normal_presence(origin, stanza, core_route_stanza);
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
325 end
1284
c0fb8379696e mod_presence: return true from incoming presence handlers to prevent further processing
Waqas Hussain <waqas20@gmail.com>
parents: 1283
diff changeset
326 return true;
1281
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
327 end);
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
328 module:hook("presence/full", function(data)
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
329 -- inbound presence to full JID recieved
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
330 local origin, stanza = data.origin, data.stanza;
1282
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
331
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
332 local t = stanza.attr.type;
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
333 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
334 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
335 return true;
ff58ef687a3f mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents: 1281
diff changeset
336 end
1283
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
337
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
338 local session = full_sessions[stanza.attr.to];
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
339 if session then
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
340 -- TODO fire post processing event
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
341 session.send(stanza);
2e57f2176612 mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents: 1282
diff changeset
342 end -- resource not online, discard
1284
c0fb8379696e mod_presence: return true from incoming presence handlers to prevent further processing
Waqas Hussain <waqas20@gmail.com>
parents: 1283
diff changeset
343 return true;
1281
bc65d57c76ef mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents: 1280
diff changeset
344 end);