author | Matthew Wild <mwild1@gmail.com> |
Wed, 01 Oct 2008 22:11:28 +0100 | |
changeset 36 | 62998e5319e3 |
parent 34 | fd693ef5d978 |
child 37 | 06eadafafefa |
permissions | -rw-r--r-- |
0 | 1 |
require "luarocks.require" |
2 |
||
12 | 3 |
server = require "net.server" |
0 | 4 |
require "socket" |
5 |
require "ssl" |
|
6 |
require "lxp" |
|
7 |
||
8 |
function log(type, area, message) |
|
9 |
print(type, area, message); |
|
10 |
end |
|
36
62998e5319e3
Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents:
34
diff
changeset
|
11 |
|
62998e5319e3
Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents:
34
diff
changeset
|
12 |
dofile "lxmppd.cfg" |
62998e5319e3
Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents:
34
diff
changeset
|
13 |
|
62998e5319e3
Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents:
34
diff
changeset
|
14 |
sessions = {}; |
18
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
15 |
|
0 | 16 |
require "core.stanza_dispatch" |
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
17 |
require "core.xmlhandlers" |
0 | 18 |
require "core.rostermanager" |
19 |
require "core.offlinemessage" |
|
30 | 20 |
require "core.modulemanager" |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
21 |
require "core.usermanager" |
30 | 22 |
require "core.sessionmanager" |
23 |
require "core.stanza_router" |
|
0 | 24 |
require "util.stanza" |
25 |
require "util.jid" |
|
18
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
26 |
|
0 | 27 |
-- Locals for faster access -- |
28 |
local t_insert = table.insert; |
|
29 |
local t_concat = table.concat; |
|
30 |
local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end |
|
31 |
local m_random = math.random; |
|
32 |
local format = string.format; |
|
33 |
local st = stanza; |
|
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
34 |
local init_xmlhandlers = xmlhandlers.init_xmlhandlers; |
0 | 35 |
------------------------------ |
36 |
||
36
62998e5319e3
Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents:
34
diff
changeset
|
37 |
|
0 | 38 |
|
39 |
local hosts, users = hosts, users; |
|
40 |
||
41 |
function connect_host(host) |
|
42 |
hosts[host] = { type = "remote", sendbuffer = {} }; |
|
43 |
end |
|
44 |
||
18
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
45 |
local function route_stanza(stanza) |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
46 |
if not stanza.attr.to then |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
47 |
-- Has no 'to' attribute, handle internally |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
48 |
end |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
49 |
local node, host, resource = jid.split(stanza.attr.to); |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
50 |
if host and hosts[host] and hosts[host].type == "local" then |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
51 |
-- Is a local host, handle internally |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
52 |
|
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
53 |
else |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
54 |
-- Is not for us or a local user, route accordingly |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
55 |
end |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
56 |
end |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
57 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
58 |
local function send_to(session, to, stanza) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
59 |
local node, host, resource = jid.split(to); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
60 |
if not hosts[host] then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
61 |
-- s2s |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
62 |
elseif hosts[host].type == "local" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
63 |
print(" ...is to a local user") |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
64 |
local destuser = hosts[host].sessions[node]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
65 |
if destuser and destuser.sessions then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
66 |
if not destuser.sessions[resource] then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
67 |
local best_session; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
68 |
for resource, session in pairs(destuser.sessions) do |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
69 |
if not best_session then best_session = session; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
70 |
elseif session.priority >= best_session.priority and session.priority >= 0 then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
71 |
best_session = session; |
0 | 72 |
end |
73 |
end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
74 |
if not best_session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
75 |
offlinemessage.new(node, host, stanza); |
0 | 76 |
else |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
77 |
print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
78 |
resource = best_session.resource; |
0 | 79 |
end |
80 |
end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
81 |
if destuser.sessions[resource] == session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
82 |
log("warn", "core", "Attempt to send stanza to self, dropping..."); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
83 |
else |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
84 |
print("...sending...", tostring(stanza)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
85 |
--destuser.sessions[resource].conn.write(tostring(data)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
86 |
print(" to conn ", destuser.sessions[resource].conn); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
87 |
destuser.sessions[resource].conn.write(tostring(stanza)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
88 |
print("...sent") |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
89 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
90 |
elseif stanza.name == "message" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
91 |
print(" ...will be stored offline"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
92 |
offlinemessage.new(node, host, stanza); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
93 |
elseif stanza.name == "iq" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
94 |
print(" ...is an iq"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
95 |
session.send(st.reply(stanza) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
96 |
:tag("error", { type = "cancel" }) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
97 |
:tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" })); |
0 | 98 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
99 |
print(" ...done routing"); |
0 | 100 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
101 |
end |
0 | 102 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
103 |
function handler(conn, data, err) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
104 |
local session = sessions[conn]; |
7 | 105 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
106 |
if not session then |
30 | 107 |
sessions[conn] = sessionmanager.new_session(conn); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
108 |
session = sessions[conn]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
109 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
110 |
-- Logging functions -- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
111 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
112 |
local mainlog, log = log; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
113 |
do |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
114 |
local conn_name = tostring(conn):match("%w+$"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
115 |
log = function (type, area, message) mainlog(type, conn_name, message); end |
7 | 116 |
--log = function () end |
0 | 117 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
118 |
local print = function (...) log("info", "core", t_concatall({...}, "\t")); end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
119 |
session.log = log; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
120 |
|
7 | 121 |
print("Client connected"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
122 |
|
30 | 123 |
session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
7 | 124 |
session.xml_handlers = init_xmlhandlers(session); |
125 |
session.parser = lxp.new(session.xml_handlers, ":"); |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
126 |
|
7 | 127 |
function session.disconnect(err) |
128 |
if session.last_presence and session.last_presence.attr.type ~= "unavailable" then |
|
129 |
local pres = st.presence{ type = "unavailable" }; |
|
130 |
if err == "closed" then err = "connection closed"; end |
|
131 |
pres:tag("status"):text("Disconnected: "..err); |
|
132 |
session.stanza_dispatch(pres); |
|
133 |
end |
|
134 |
if session.username then |
|
2 | 135 |
hosts[session.host].sessions[session.username] = nil; |
0 | 136 |
end |
7 | 137 |
session = nil; |
138 |
print("Disconnected: "..err); |
|
139 |
collectgarbage("collect"); |
|
0 | 140 |
end |
7 | 141 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
142 |
if data then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
143 |
session.parser:parse(data); |
0 | 144 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
145 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
146 |
--log("info", "core", "Client disconnected, connection closed"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
147 |
end |
0 | 148 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
149 |
function disconnect(conn, err) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
150 |
sessions[conn].disconnect(err); |
34
fd693ef5d978
Fixed: Session data was never removed from sessions list
Waqas Hussain <waqas20@gmail.com>
parents:
33
diff
changeset
|
151 |
sessions[conn] = nil; |
0 | 152 |
end |
153 |
||
33
091f91a1f67a
Let modules set/write globals
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
154 |
modulemanager.loadall(); |
091f91a1f67a
Let modules set/write globals
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
155 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
156 |
setmetatable(_G, { __index = function (t, k) print("WARNING: ATTEMPT TO READ A NIL GLOBAL!!!", k); error("Attempt to read a non-existent global. Naughty boy.", 2); end, __newindex = function (t, k, v) print("ATTEMPT TO SET A GLOBAL!!!!", tostring(k).." = "..tostring(v)); error("Attempt to set a global. Naughty boy.", 2); end }) --]][][[]][]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
157 |
|
0 | 158 |
|
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
159 |
local protected_handler = function (conn, data, err) local success, ret = pcall(handler, conn, data, err); if not success then print("ERROR on "..tostring(conn)..": "..ret); conn:close(); end end; |
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
160 |
local protected_disconnect = function (conn, err) local success, ret = pcall(disconnect, conn, err); if not success then print("ERROR on "..tostring(conn).." disconnect: "..ret); conn:close(); end end; |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
161 |
|
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
162 |
server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, nil ) -- server.add will send a status message |
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
163 |
server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5223, "*", 1, ssl_ctx ) -- server.add will send a status message |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
164 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
165 |
server.loop(); |