Mon, 25 Aug 2008 22:19:47 +0100
Added tests/ folder
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 | |
11 | ||
12 | require "core.stanza_dispatch" | |
7 | 13 | local init_xmlhandlers = require "core.xmlhandlers" |
0 | 14 | require "core.rostermanager" |
15 | require "core.offlinemessage" | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
16 | require "core.usermanager" |
0 | 17 | require "util.stanza" |
18 | require "util.jid" | |
19 | ||
20 | -- Locals for faster access -- | |
21 | local t_insert = table.insert; | |
22 | local t_concat = table.concat; | |
23 | 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 | |
24 | local m_random = math.random; | |
25 | local format = string.format; | |
26 | local st = stanza; | |
27 | ------------------------------ | |
28 | ||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
29 | sessions = {}; |
0 | 30 | hosts = { |
31 | ["localhost"] = { | |
32 | type = "local"; | |
33 | connected = true; | |
34 | sessions = {}; | |
35 | }; | |
36 | ["getjabber.ath.cx"] = { | |
37 | type = "local"; | |
38 | connected = true; | |
39 | sessions = {}; | |
40 | }; | |
41 | } | |
42 | ||
43 | local hosts, users = hosts, users; | |
44 | ||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
45 | local ssl_ctx = { mode = "server", protocol = "sslv23", key = "/home/matthew/ssl_cert/server.key", |
0 | 46 | certificate = "/home/matthew/ssl_cert/server.crt", capath = "/etc/ssl/certs", verify = "none", } |
47 | ||
48 | ||
49 | function connect_host(host) | |
50 | hosts[host] = { type = "remote", sendbuffer = {} }; | |
51 | end | |
52 | ||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
53 | local function send_to(session, to, stanza) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
54 | local node, host, resource = jid.split(to); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
55 | if not hosts[host] then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
56 | -- s2s |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
57 | elseif hosts[host].type == "local" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
58 | print(" ...is to a local user") |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
59 | local destuser = hosts[host].sessions[node]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
60 | if destuser and destuser.sessions then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
61 | if not destuser.sessions[resource] then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
62 | local best_session; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
63 | for resource, session in pairs(destuser.sessions) do |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
64 | if not best_session then best_session = session; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
65 | 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
|
66 | best_session = session; |
0 | 67 | end |
68 | end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
69 | if not best_session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
70 | offlinemessage.new(node, host, stanza); |
0 | 71 | else |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
72 | 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
|
73 | resource = best_session.resource; |
0 | 74 | end |
75 | end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
76 | if destuser.sessions[resource] == session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
77 | 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
|
78 | else |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
79 | print("...sending...", tostring(stanza)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
80 | --destuser.sessions[resource].conn.write(tostring(data)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
81 | print(" to conn ", destuser.sessions[resource].conn); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
82 | destuser.sessions[resource].conn.write(tostring(stanza)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
83 | print("...sent") |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
84 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
85 | elseif stanza.name == "message" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
86 | print(" ...will be stored offline"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
87 | offlinemessage.new(node, host, stanza); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
88 | elseif stanza.name == "iq" then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
89 | print(" ...is an iq"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
90 | session.send(st.reply(stanza) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
91 | :tag("error", { type = "cancel" }) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
92 | :tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" })); |
0 | 93 | end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
94 | print(" ...done routing"); |
0 | 95 | end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
96 | end |
0 | 97 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
98 | function handler(conn, data, err) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
99 | local session = sessions[conn]; |
7 | 100 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
101 | if not session then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
102 | sessions[conn] = { conn = conn, notopen = true, priority = 0 }; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
103 | session = sessions[conn]; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
104 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
105 | -- Logging functions -- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
106 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
107 | local mainlog, log = log; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
108 | do |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
109 | local conn_name = tostring(conn):match("%w+$"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
110 | log = function (type, area, message) mainlog(type, conn_name, message); end |
7 | 111 | --log = function () end |
0 | 112 | end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
113 | 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
|
114 | session.log = log; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
115 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
116 | -- -- -- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
117 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
118 | -- Send buffers -- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
119 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
120 | local send = function (data) print("Sending...", tostring(data)); conn.write(tostring(data)); end; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
121 | session.send, session.send_to = send, send_to; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
122 | |
7 | 123 | print("Client connected"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
124 | |
7 | 125 | session.stanza_dispatch = init_stanza_dispatcher(session); |
126 | session.xml_handlers = init_xmlhandlers(session); | |
127 | session.parser = lxp.new(session.xml_handlers, ":"); | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
128 | |
7 | 129 | function session.disconnect(err) |
130 | if session.last_presence and session.last_presence.attr.type ~= "unavailable" then | |
131 | local pres = st.presence{ type = "unavailable" }; | |
132 | if err == "closed" then err = "connection closed"; end | |
133 | pres:tag("status"):text("Disconnected: "..err); | |
134 | session.stanza_dispatch(pres); | |
135 | end | |
136 | if session.username then | |
2 | 137 | hosts[session.host].sessions[session.username] = nil; |
0 | 138 | end |
7 | 139 | session = nil; |
140 | print("Disconnected: "..err); | |
141 | collectgarbage("collect"); | |
0 | 142 | end |
7 | 143 | end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
144 | if data then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
145 | session.parser:parse(data); |
0 | 146 | end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
147 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
148 | --log("info", "core", "Client disconnected, connection closed"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
149 | end |
0 | 150 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
151 | function disconnect(conn, err) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
152 | sessions[conn].disconnect(err); |
0 | 153 | end |
154 | ||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
155 | print("ssl_ctx:", type(ssl_ctx)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
156 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
157 | 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
|
158 | |
0 | 159 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
160 | local protected_handler = function (...) local success, ret = pcall(handler, ...); if not success then print("ERROR on "..tostring((select(1, ...)))..": "..ret); end end; |
4 | 161 | local protected_disconnect = function (...) local success, ret = pcall(disconnect, ...); if not success then print("ERROR on "..tostring((select(1, ...))).." disconnect: "..ret); end end; |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
162 | |
4 | 163 | print( server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, nil ) ) -- server.add will send a status message |
164 | print( 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
|
165 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
166 | server.loop(); |