38 |
38 |
39 local hosts, users = hosts, users; |
39 local hosts, users = hosts, users; |
40 |
40 |
41 function connect_host(host) |
41 function connect_host(host) |
42 hosts[host] = { type = "remote", sendbuffer = {} }; |
42 hosts[host] = { type = "remote", sendbuffer = {} }; |
43 end |
|
44 |
|
45 local function route_stanza(stanza) |
|
46 if not stanza.attr.to then |
|
47 -- Has no 'to' attribute, handle internally |
|
48 end |
|
49 local node, host, resource = jid.split(stanza.attr.to); |
|
50 if host and hosts[host] and hosts[host].type == "local" then |
|
51 -- Is a local host, handle internally |
|
52 |
|
53 else |
|
54 -- Is not for us or a local user, route accordingly |
|
55 end |
|
56 end |
|
57 |
|
58 local function send_to(session, to, stanza) |
|
59 local node, host, resource = jid.split(to); |
|
60 if not hosts[host] then |
|
61 -- s2s |
|
62 elseif hosts[host].type == "local" then |
|
63 print(" ...is to a local user") |
|
64 local destuser = hosts[host].sessions[node]; |
|
65 if destuser and destuser.sessions then |
|
66 if not destuser.sessions[resource] then |
|
67 local best_session; |
|
68 for resource, session in pairs(destuser.sessions) do |
|
69 if not best_session then best_session = session; |
|
70 elseif session.priority >= best_session.priority and session.priority >= 0 then |
|
71 best_session = session; |
|
72 end |
|
73 end |
|
74 if not best_session then |
|
75 offlinemessage.new(node, host, stanza); |
|
76 else |
|
77 print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'"); |
|
78 resource = best_session.resource; |
|
79 end |
|
80 end |
|
81 if destuser.sessions[resource] == session then |
|
82 log("warn", "core", "Attempt to send stanza to self, dropping..."); |
|
83 else |
|
84 print("...sending...", tostring(stanza)); |
|
85 --destuser.sessions[resource].conn.write(tostring(data)); |
|
86 print(" to conn ", destuser.sessions[resource].conn); |
|
87 destuser.sessions[resource].conn.write(tostring(stanza)); |
|
88 print("...sent") |
|
89 end |
|
90 elseif stanza.name == "message" then |
|
91 print(" ...will be stored offline"); |
|
92 offlinemessage.new(node, host, stanza); |
|
93 elseif stanza.name == "iq" then |
|
94 print(" ...is an iq"); |
|
95 session.send(st.reply(stanza) |
|
96 :tag("error", { type = "cancel" }) |
|
97 :tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" })); |
|
98 end |
|
99 print(" ...done routing"); |
|
100 end |
|
101 end |
43 end |
102 |
44 |
103 function handler(conn, data, err) |
45 function handler(conn, data, err) |
104 local session = sessions[conn]; |
46 local session = sessions[conn]; |
105 |
47 |