Annotate

main.lua @ 6:7ad47ce20394

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