Software /
code /
prosody
Comparison
main.lua @ 99:ba08b8a4eeef
Abstract connections with "connection listeners"
- Added connlistener for xmppclient
- SASL/TLS now use a new session:reset_stream() method
- main.lua on its way to being a bit neater
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 22 Oct 2008 17:36:21 +0100 |
parent | 97:c3f12fd0c823 |
child | 133:b92493ea6fd7 |
child | 145:fbb3a4ff9cf1 |
comparison
equal
deleted
inserted
replaced
98:3a2d327c4856 | 99:ba08b8a4eeef |
---|---|
1 require "luarocks.require" | 1 require "luarocks.require" |
2 | 2 |
3 local server = require "net.server" | 3 local server = require "net.server" |
4 require "lxp" | |
4 require "socket" | 5 require "socket" |
5 require "ssl" | 6 require "ssl" |
6 require "lxp" | |
7 | 7 |
8 function log(type, area, message) | 8 function log(type, area, message) |
9 print(type, area, message); | 9 print(type, area, message); |
10 end | 10 end |
11 | 11 |
12 dofile "lxmppd.cfg" | 12 dofile "lxmppd.cfg" |
13 | 13 |
14 -- Maps connections to sessions -- | |
14 sessions = {}; | 15 sessions = {}; |
16 | |
17 -- Load and initialise core modules -- | |
15 | 18 |
16 require "util.import" | 19 require "util.import" |
17 require "core.stanza_dispatch" | 20 require "core.stanza_dispatch" |
18 require "core.xmlhandlers" | 21 require "core.xmlhandlers" |
19 require "core.rostermanager" | 22 require "core.rostermanager" |
20 require "core.offlinemessage" | 23 require "core.offlinemessage" |
21 require "core.modulemanager" | 24 require "core.modulemanager" |
22 require "core.usermanager" | 25 require "core.usermanager" |
23 require "core.sessionmanager" | 26 require "core.sessionmanager" |
24 require "core.stanza_router" | 27 require "core.stanza_router" |
25 require "net.connhandlers" | 28 |
29 local start = require "net.connlisteners".start; | |
26 require "util.stanza" | 30 require "util.stanza" |
27 require "util.jid" | 31 require "util.jid" |
28 | 32 |
33 ------------------------------------------------------------------------ | |
29 | 34 |
30 -- Locals for faster access -- | 35 -- Locals for faster access -- |
31 local t_insert = table.insert; | 36 local t_insert = table.insert; |
32 local t_concat = table.concat; | 37 local t_concat = table.concat; |
33 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 | 38 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 |
35 local format = string.format; | 40 local format = string.format; |
36 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); | 41 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); |
37 local st = stanza; | 42 local st = stanza; |
38 ------------------------------ | 43 ------------------------------ |
39 | 44 |
40 | |
41 | |
42 local hosts, sessions = hosts, sessions; | 45 local hosts, sessions = hosts, sessions; |
43 | 46 |
44 function connect_host(host) | 47 -- Initialise modules |
45 hosts[host] = { type = "remote", sendbuffer = {} }; | |
46 end | |
47 | |
48 function handler(conn, data, err) | |
49 local session = sessions[conn]; | |
50 | |
51 if not session then | |
52 sessions[conn] = sm_new_session(conn); | |
53 session = sessions[conn]; | |
54 | |
55 -- Logging functions -- | |
56 | |
57 local mainlog, log = log; | |
58 do | |
59 local conn_name = tostring(conn):match("%w+$"); | |
60 log = function (type, area, message) mainlog(type, conn_name, message); end | |
61 --log = function () end | |
62 end | |
63 local print = function (...) log("info", "core", t_concatall({...}, "\t")); end | |
64 session.log = log; | |
65 | |
66 print("Client connected"); | |
67 | |
68 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end | |
69 | |
70 session.connhandler = connhandlers.new("xmpp-client", session); | |
71 | |
72 function session.disconnect(err) | |
73 if session.last_presence and session.last_presence.attr.type ~= "unavailable" then | |
74 local pres = st.presence{ type = "unavailable" }; | |
75 if err == "closed" then err = "connection closed"; end | |
76 pres:tag("status"):text("Disconnected: "..err); | |
77 session.stanza_dispatch(pres); | |
78 end | |
79 session = nil; | |
80 print("Disconnected: "..tostring(err)); | |
81 collectgarbage("collect"); | |
82 end | |
83 end | |
84 if data then | |
85 session.connhandler:data(data); | |
86 end | |
87 | |
88 --log("info", "core", "Client disconnected, connection closed"); | |
89 end | |
90 | |
91 function disconnect(conn, err) | |
92 sm_destroy_session(sessions[conn]); | |
93 sessions[conn] = nil; | |
94 end | |
95 | |
96 modulemanager.loadall(); | 48 modulemanager.loadall(); |
97 | 49 |
98 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 }) --]][][[]][]; | 50 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 }) --]][][[]][]; |
99 | 51 |
100 | 52 |
101 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; | 53 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; |
102 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; | 54 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; |
103 | 55 |
104 server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, ssl_ctx ) -- server.add will send a status message | 56 start("xmppclient", { ssl = ssl_ctx }) |
105 --server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5223, "*", 1, ssl_ctx ) -- server.add will send a status message | |
106 | 57 |
107 server.loop(); | 58 server.loop(); |