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