Comparison

main.lua @ 30:bcf539295f2d

Huge commit to: * Break stanza routing (to be restored in a future commit) * Remove the old stanza_dispatcher code, which was never going to be maintainable nor extendable :) * Bring us plugins, starting with mod_legacyauth and mod_roster * Sessions are now created/destroyed using a standard sessionmanager interface
author Matthew Wild <mwild1@gmail.com>
date Tue, 30 Sep 2008 19:52:00 +0100
parent 20:6885fd2cf51f
child 33:091f91a1f67a
comparison
equal deleted inserted replaced
29:b847875801e5 30:bcf539295f2d
11 11
12 require "core.stanza_dispatch" 12 require "core.stanza_dispatch"
13 require "core.xmlhandlers" 13 require "core.xmlhandlers"
14 require "core.rostermanager" 14 require "core.rostermanager"
15 require "core.offlinemessage" 15 require "core.offlinemessage"
16 require "core.modulemanager"
16 require "core.usermanager" 17 require "core.usermanager"
18 require "core.sessionmanager"
19 require "core.stanza_router"
17 require "util.stanza" 20 require "util.stanza"
18 require "util.jid" 21 require "util.jid"
19 22
20 -- Locals for faster access -- 23 -- Locals for faster access --
21 local t_insert = table.insert; 24 local t_insert = table.insert;
111 114
112 function handler(conn, data, err) 115 function handler(conn, data, err)
113 local session = sessions[conn]; 116 local session = sessions[conn];
114 117
115 if not session then 118 if not session then
116 sessions[conn] = { conn = conn, notopen = true, priority = 0 }; 119 sessions[conn] = sessionmanager.new_session(conn);
117 session = sessions[conn]; 120 session = sessions[conn];
118 121
119 -- Logging functions -- 122 -- Logging functions --
120 123
121 local mainlog, log = log; 124 local mainlog, log = log;
125 --log = function () end 128 --log = function () end
126 end 129 end
127 local print = function (...) log("info", "core", t_concatall({...}, "\t")); end 130 local print = function (...) log("info", "core", t_concatall({...}, "\t")); end
128 session.log = log; 131 session.log = log;
129 132
130 -- -- --
131
132 -- Send buffers --
133
134 local send = function (data) print("Sending...", tostring(data)); conn.write(tostring(data)); end;
135 session.send, session.send_to = send, send_to;
136
137 print("Client connected"); 133 print("Client connected");
138 134
139 session.stanza_dispatch = init_stanza_dispatcher(session); 135 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
140 session.xml_handlers = init_xmlhandlers(session); 136 session.xml_handlers = init_xmlhandlers(session);
141 session.parser = lxp.new(session.xml_handlers, ":"); 137 session.parser = lxp.new(session.xml_handlers, ":");
142 138
143 function session.disconnect(err) 139 function session.disconnect(err)
144 if session.last_presence and session.last_presence.attr.type ~= "unavailable" then 140 if session.last_presence and session.last_presence.attr.type ~= "unavailable" then
166 sessions[conn].disconnect(err); 162 sessions[conn].disconnect(err);
167 end 163 end
168 164
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 }) --]][][[]][]; 165 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 }) --]][][[]][];
170 166
167 modulemanager.loadall();
171 168
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; 169 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;
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; 170 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;
174 171
175 server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, nil ) -- server.add will send a status message 172 server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, nil ) -- server.add will send a status message