Annotate

plugins/mod_presence.lua @ 1568:b11bac42d56f

mod_presence: Ignore presence sent to host and invalid JIDs, fixes traceback (thanks Deepspawn)
author Matthew Wild <mwild1@gmail.com>
date Wed, 22 Jul 2009 21:35:41 +0100
parent 1523:841d61be198f
child 1606:bbadcaab0036
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
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
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;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
21
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
22 local rostermanager = require "core.rostermanager";
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
23 local sessionmanager = require "core.sessionmanager";
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
24 local offlinemanager = require "core.offlinemanager";
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
25
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
26 local _core_route_stanza = core_route_stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
27 local core_route_stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
28 function core_route_stanza(origin, stanza)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
29 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
30 local node, host = jid_split(stanza.attr.to);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
31 host = hosts[host];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
32 if host and host.type == "local" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
33 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
34 return;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
35 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
36 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
37 _core_route_stanza(origin, stanza);
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
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
40 local function select_top_resources(user)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
41 local priority = 0;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
42 local recipients = {};
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
43 for _, session in pairs(user.sessions) do -- find resource with greatest priority
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
44 if session.presence then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
45 -- TODO check active privacy list for session
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
46 local p = session.priority;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
47 if p > priority then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
48 priority = p;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
49 recipients = {session};
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
50 elseif p == priority then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
51 t_insert(recipients, session);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
52 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
53 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
54 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
55 return recipients;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
56 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
57 local function recalc_resource_map(origin)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
58 local user = hosts[origin.host].sessions[origin.username];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
59 user.top_resources = select_top_resources(user);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
60 if #user.top_resources == 0 then user.top_resources = nil; end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
61 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
62
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
63 function handle_normal_presence(origin, stanza, core_route_stanza)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
64 local roster = origin.roster;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
65 local node, host = origin.username, origin.host;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
66 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
67 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
68 stanza.attr.to = res.full_jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
69 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
70 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
71 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
72 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
73 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
74 stanza.attr.to = jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
75 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
76 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
77 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
78 if stanza.attr.type == nil and not origin.presence then -- initial presence
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
79 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
80 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
81 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
82 probe.attr.to = jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
83 core_route_stanza(origin, probe);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
84 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
85 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
86 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
87 if res ~= origin and res.presence then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
88 res.presence.attr.to = origin.full_jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
89 core_route_stanza(res, res.presence);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
90 res.presence.attr.to = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
91 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
92 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
93 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
94 for jid in pairs(roster.pending) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
95 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
96 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
97 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
98 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
99 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
100 if item.ask then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
101 request.attr.to = jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
102 core_route_stanza(origin, request);
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 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
105 local offline = offlinemanager.load(node, host);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
106 if offline then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
107 for _, msg in ipairs(offline) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
108 origin.send(msg); -- FIXME do we need to modify to/from in any way?
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
109 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
110 offlinemanager.deleteAll(node, host);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
111 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
112 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
113 if stanza.attr.type == "unavailable" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
114 origin.presence = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
115 if origin.priority then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
116 origin.priority = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
117 recalc_resource_map(origin);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
118 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
119 if origin.directed then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
120 for jid in pairs(origin.directed) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
121 stanza.attr.to = jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
122 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
123 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
124 origin.directed = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
125 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
126 else
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
127 origin.presence = stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
128 local priority = stanza:child_with_name("priority");
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
129 if priority and #priority > 0 then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
130 priority = t_concat(priority);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
131 if s_find(priority, "^[+-]?[0-9]+$") then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
132 priority = tonumber(priority);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
133 if priority < -128 then priority = -128 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
134 if priority > 127 then priority = 127 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
135 else priority = 0; end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
136 else priority = 0; end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
137 if origin.priority ~= priority then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
138 origin.priority = priority;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
139 recalc_resource_map(origin);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
140 end
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 stanza.attr.to = nil; -- reset it
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
143 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
144
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
145 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
146 local h = hosts[host];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
147 local count = 0;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
148 if h and h.type == "local" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
149 local u = h.sessions[user];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
150 if u then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
151 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
152 local pres = session.presence;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
153 if pres then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
154 pres.attr.to = jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
155 core_route_stanza(session, pres);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
156 pres.attr.to = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
157 count = count + 1;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
158 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
159 end
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 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
163 return count;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
164 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
165
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
166 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
167 local node, host = jid_split(from_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
168 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
169 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
170 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
171 if stanza.attr.type == "subscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
172 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
173 -- 2. roster push (subscription = none, ask = subscribe)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
174 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
175 rostermanager.roster_push(node, host, to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
176 end -- else file error
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
177 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
178 elseif stanza.attr.type == "unsubscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
179 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
180 -- 2. roster push (subscription = none or from)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
181 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
182 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
183 end -- else file error
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
184 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
185 elseif stanza.attr.type == "subscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
186 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
187 -- 2. roster_push ()
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
188 -- 3. send_presence_of_available_resources
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
189 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
190 rostermanager.roster_push(node, host, to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
191 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
192 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
193 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
194 elseif stanza.attr.type == "unsubscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
195 -- 1. route stanza
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
196 -- 2. roster push (subscription = none or to)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
197 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
198 rostermanager.roster_push(node, host, to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
199 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
200 core_route_stanza(origin, stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
201 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
202 stanza.attr.from, stanza.attr.to = st_from, st_to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
203 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
204
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
205 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
206 local node, host = jid_split(to_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
207 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
208 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
209 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
210
b11bac42d56f mod_presence: Ignore presence sent to host and invalid JIDs, fixes traceback (thanks Deepspawn)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
211 if not node then
b11bac42d56f mod_presence: Ignore presence sent to host and invalid JIDs, fixes traceback (thanks Deepspawn)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
212 log("debug", "dropping presence sent to host or invalid address '%s'", tostring(to_bare));
b11bac42d56f mod_presence: Ignore presence sent to host and invalid JIDs, fixes traceback (thanks Deepspawn)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
213 end
b11bac42d56f mod_presence: Ignore presence sent to host and invalid JIDs, fixes traceback (thanks Deepspawn)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
214
1508
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
215 if stanza.attr.type == "probe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
216 if rostermanager.is_contact_subscribed(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
217 if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
218 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
219 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
220 else
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
221 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
222 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
223 elseif stanza.attr.type == "subscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
224 if rostermanager.is_contact_subscribed(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
225 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
226 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
227 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
228 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
229 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
230 else
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
231 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
232 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
233 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
234 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
235 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
236 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
237 elseif stanza.attr.type == "unsubscribe" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
238 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
239 rostermanager.roster_push(node, host, from_bare);
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 elseif stanza.attr.type == "subscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
242 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
243 rostermanager.roster_push(node, host, from_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
244 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
245 elseif stanza.attr.type == "unsubscribed" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
246 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
247 rostermanager.roster_push(node, host, from_bare);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
248 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
249 end -- discard any other type
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
250 stanza.attr.from, stanza.attr.to = st_from, st_to;
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
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
253 local outbound_presence_handler = function(data)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
254 -- outbound presence recieved
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
255 local origin, stanza = data.origin, data.stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
256
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
257 local to = stanza.attr.to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
258 if to then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
259 local t = stanza.attr.type;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
260 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
261 handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
262 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
263 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
264
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
265 local to_bare = jid_bare(to);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
266 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
267 origin.directed = origin.directed or {};
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
268 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
269 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
270 else
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
271 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
272 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
273 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
274 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
275 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
276
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
277 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
278 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
279 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
280
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
281 module:hook("presence/bare", function(data)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
282 -- inbound presence to bare JID recieved
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
283 local origin, stanza = data.origin, data.stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
284
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
285 local to = stanza.attr.to;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
286 local t = stanza.attr.type;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
287 if to then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
288 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
289 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
290 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
291 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
292
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
293 local user = bare_sessions[to];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
294 if user then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
295 for _, session in pairs(user.sessions) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
296 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
297 session.send(stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
298 end
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 end -- no resources not online, discard
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
301 elseif not t or t == "unavailable" then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
302 handle_normal_presence(origin, stanza, core_route_stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
303 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
304 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
305 end);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
306 module:hook("presence/full", function(data)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
307 -- inbound presence to full JID recieved
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
308 local origin, stanza = data.origin, data.stanza;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
309
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
310 local t = stanza.attr.type;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
311 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
312 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
313 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
314 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
315
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
316 local session = full_sessions[stanza.attr.to];
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
317 if session then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
318 -- TODO fire post processing event
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
319 session.send(stanza);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
320 end -- resource not online, discard
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
321 return true;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
322 end);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
323
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
324 module:hook("resource-unbind", function(event)
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
325 local session, err = event.session, event.error;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
326 -- Send unavailable presence
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
327 if session.presence then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
328 local pres = st.presence{ type = "unavailable" };
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
329 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
330 pres:tag("status"):text("Disconnected: "..err):up();
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
331 session:dispatch_stanza(pres);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
332 elseif session.directed then
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
333 local pres = st.presence{ type = "unavailable" };
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
334 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
335 pres:tag("status"):text("Disconnected: "..err):up();
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
336 for jid in pairs(session.directed) do
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
337 pres.attr.to = jid;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
338 core_route_stanza(session, pres);
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
339 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
340 session.directed = nil;
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
341 end
762d758e116a mod_presence: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1488
diff changeset
342 end);