author | Waqas Hussain <waqas20@gmail.com> |
Wed, 01 Oct 2008 18:47:07 +0500 | |
changeset 34 | fd693ef5d978 |
parent 33 | 091f91a1f67a |
child 36 | 62998e5319e3 |
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 |
|
18
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
11 |
|
0 | 12 |
require "core.stanza_dispatch" |
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
13 |
require "core.xmlhandlers" |
0 | 14 |
require "core.rostermanager" |
15 |
require "core.offlinemessage" |
|
30 | 16 |
require "core.modulemanager" |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
17 |
require "core.usermanager" |
30 | 18 |
require "core.sessionmanager" |
19 |
require "core.stanza_router" |
|
0 | 20 |
require "util.stanza" |
21 |
require "util.jid" |
|
18
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
22 |
|
0 | 23 |
-- Locals for faster access -- |
24 |
local t_insert = table.insert; |
|
25 |
local t_concat = table.concat; |
|
26 |
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 |
|
27 |
local m_random = math.random; |
|
28 |
local format = string.format; |
|
29 |
local st = stanza; |
|
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
30 |
local init_xmlhandlers = xmlhandlers.init_xmlhandlers; |
0 | 31 |
------------------------------ |
32 |
||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
33 |
sessions = {}; |
0 | 34 |
hosts = { |
35 |
["localhost"] = { |
|
36 |
type = "local"; |
|
37 |
connected = true; |
|
38 |
sessions = {}; |
|
39 |
}; |
|
40 |
["getjabber.ath.cx"] = { |
|
41 |
type = "local"; |
|
42 |
connected = true; |
|
43 |
sessions = {}; |
|
44 |
}; |
|
45 |
} |
|
46 |
||
47 |
local hosts, users = hosts, users; |
|
48 |
||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
49 |
local ssl_ctx = { mode = "server", protocol = "sslv23", key = "/home/matthew/ssl_cert/server.key", |
0 | 50 |
certificate = "/home/matthew/ssl_cert/server.crt", capath = "/etc/ssl/certs", verify = "none", } |
51 |
||
52 |
||
53 |
function connect_host(host) |
|
54 |
hosts[host] = { type = "remote", sendbuffer = {} }; |
|
55 |
end |
|
56 |
||
18
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
57 |
local function route_stanza(stanza) |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
58 |
if not stanza.attr.to then |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
59 |
-- Has no 'to' attribute, handle internally |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
60 |
end |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
61 |
local node, host, resource = jid.split(stanza.attr.to); |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
62 |
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
|
63 |
-- Is a local host, handle internally |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
64 |
|
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
65 |
else |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
66 |
-- 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
|
67 |
end |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
68 |
end |
ae161e907149
Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents:
12
diff
changeset
|
69 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
70 |
local function send_to(session, to, stanza) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
71 |
local node, host, resource = jid.split(to); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
72 |
if not hosts[host] then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
73 |
-- s2s |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
74 |
elseif hosts[host].type == "local" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
75 |
print(" ...is to a local user") |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
76 |
local destuser = hosts[host].sessions[node]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
77 |
if destuser and destuser.sessions then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
78 |
if not destuser.sessions[resource] then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
79 |
local best_session; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
80 |
for resource, session in pairs(destuser.sessions) do |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
81 |
if not best_session then best_session = session; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
82 |
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
|
83 |
best_session = session; |
0 | 84 |
end |
85 |
end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
86 |
if not best_session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
87 |
offlinemessage.new(node, host, stanza); |
0 | 88 |
else |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
89 |
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
|
90 |
resource = best_session.resource; |
0 | 91 |
end |
92 |
end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
93 |
if destuser.sessions[resource] == session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
94 |
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
|
95 |
else |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
96 |
print("...sending...", tostring(stanza)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
97 |
--destuser.sessions[resource].conn.write(tostring(data)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
98 |
print(" to conn ", destuser.sessions[resource].conn); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
99 |
destuser.sessions[resource].conn.write(tostring(stanza)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
100 |
print("...sent") |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
101 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
102 |
elseif stanza.name == "message" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
103 |
print(" ...will be stored offline"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
104 |
offlinemessage.new(node, host, stanza); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
105 |
elseif stanza.name == "iq" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
106 |
print(" ...is an iq"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
107 |
session.send(st.reply(stanza) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
108 |
:tag("error", { type = "cancel" }) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
109 |
:tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" })); |
0 | 110 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
111 |
print(" ...done routing"); |
0 | 112 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
113 |
end |
0 | 114 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
115 |
function handler(conn, data, err) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
116 |
local session = sessions[conn]; |
7 | 117 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
118 |
if not session then |
30 | 119 |
sessions[conn] = sessionmanager.new_session(conn); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
120 |
session = sessions[conn]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
121 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
122 |
-- Logging functions -- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
123 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
124 |
local mainlog, log = log; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
125 |
do |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
126 |
local conn_name = tostring(conn):match("%w+$"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
127 |
log = function (type, area, message) mainlog(type, conn_name, message); end |
7 | 128 |
--log = function () end |
0 | 129 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
130 |
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
|
131 |
session.log = log; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
132 |
|
7 | 133 |
print("Client connected"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
134 |
|
30 | 135 |
session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
7 | 136 |
session.xml_handlers = init_xmlhandlers(session); |
137 |
session.parser = lxp.new(session.xml_handlers, ":"); |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
138 |
|
7 | 139 |
function session.disconnect(err) |
140 |
if session.last_presence and session.last_presence.attr.type ~= "unavailable" then |
|
141 |
local pres = st.presence{ type = "unavailable" }; |
|
142 |
if err == "closed" then err = "connection closed"; end |
|
143 |
pres:tag("status"):text("Disconnected: "..err); |
|
144 |
session.stanza_dispatch(pres); |
|
145 |
end |
|
146 |
if session.username then |
|
2 | 147 |
hosts[session.host].sessions[session.username] = nil; |
0 | 148 |
end |
7 | 149 |
session = nil; |
150 |
print("Disconnected: "..err); |
|
151 |
collectgarbage("collect"); |
|
0 | 152 |
end |
7 | 153 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
154 |
if data then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
155 |
session.parser:parse(data); |
0 | 156 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
157 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
158 |
--log("info", "core", "Client disconnected, connection closed"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
159 |
end |
0 | 160 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
161 |
function disconnect(conn, err) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
162 |
sessions[conn].disconnect(err); |
34
fd693ef5d978
Fixed: Session data was never removed from sessions list
Waqas Hussain <waqas20@gmail.com>
parents:
33
diff
changeset
|
163 |
sessions[conn] = nil; |
0 | 164 |
end |
165 |
||
33
091f91a1f67a
Let modules set/write globals
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
166 |
modulemanager.loadall(); |
091f91a1f67a
Let modules set/write globals
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
167 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
168 |
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
|
169 |
|
0 | 170 |
|
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
171 |
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
|
172 |
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
|
173 |
|
20
6885fd2cf51f
Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
174 |
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
|
175 |
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
|
176 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
177 |
server.loop(); |