Comparison

core/stanza_router.lua @ 199:eccf66b42bd7

Added resource priority handling, etc
author Waqas Hussain <waqas20@gmail.com>
date Sun, 02 Nov 2008 06:36:42 +0500
parent 192:71c389c6fc2e
child 200:5e8b3cce798f
comparison
equal deleted inserted replaced
193:13ac34255c37 199:eccf66b42bd7
19 19
20 local modules_handle_stanza = require "core.modulemanager".handle_stanza; 20 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
21 21
22 local format = string.format; 22 local format = string.format;
23 local tostring = tostring; 23 local tostring = tostring;
24 local t_concat = table.concat;
25 local tonumber = tonumber;
26 local s_find = string.find;
24 27
25 local jid_split = require "util.jid".split; 28 local jid_split = require "util.jid".split;
26 local print = print; 29 local print = print;
27 30
28 function core_process_stanza(origin, stanza) 31 function core_process_stanza(origin, stanza)
89 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence 92 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
90 stanza.attr.to = res.full_jid; 93 stanza.attr.to = res.full_jid;
91 core_route_stanza(origin, stanza); 94 core_route_stanza(origin, stanza);
92 end 95 end
93 end 96 end
94 if not origin.presence then -- presence probes on initial presence -- FIXME does unavailable qualify as initial presence? 97 if stanza.attr.type == nil and not origin.presence then -- initial presence
95 local probe = st.presence({from = origin.full_jid, type = "probe"}); 98 local probe = st.presence({from = origin.full_jid, type = "probe"});
96 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to 99 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
97 local subscription = origin.roster[jid].subscription; 100 local subscription = origin.roster[jid].subscription;
98 if subscription == "both" or subscription == "to" then 101 if subscription == "both" or subscription == "to" then
99 probe.attr.to = jid; 102 probe.attr.to = jid;
100 core_route_stanza(origin, probe); 103 core_route_stanza(origin, probe);
101 end 104 end
102 end 105 end
103 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all resources 106 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
104 if res ~= origin and stanza.attr.type ~= "unavailable" and res.presence then -- FIXME does unavailable qualify as initial presence? 107 if res ~= origin and res.presence then
105 res.presence.attr.to = origin.full_jid; 108 res.presence.attr.to = origin.full_jid;
106 core_route_stanza(res, res.presence); 109 core_route_stanza(res, res.presence);
107 res.presence.attr.to = nil; 110 res.presence.attr.to = nil;
108 end 111 end
109 end 112 end
110 -- TODO resend subscription requests 113 if origin.roster.pending then -- resend incoming subscription requests
111 end 114 for jid in pairs(origin.roster.pending) do
112 origin.presence = stanza; 115 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
116 end
117 end
118 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
119 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
120 if item.ask then
121 request.attr.to = jid;
122 core_route_stanza(origin, request);
123 end
124 end
125 end
126 origin.priority = 0;
127 if stanza.attr.type == "unavailable" then
128 origin.presence = nil;
129 else
130 origin.presence = stanza;
131 local priority = stanza:child_with_name("priority");
132 if priority and #priority > 0 then
133 priority = t_concat(priority);
134 if s_find(priority, "^[+-]?[0-9]+$") then
135 priority = tonumber(priority);
136 if priority < -128 then priority = -128 end
137 if priority > 127 then priority = 127 end
138 origin.priority = priority;
139 end
140 end
141 end
113 stanza.attr.to = nil; -- reset it 142 stanza.attr.to = nil; -- reset it
114 else 143 else
115 -- TODO error, bad type 144 -- TODO error, bad type
116 end 145 end
117 end 146 end