main.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 01 May 2013 13:54:44 +0100
branchtls
changeset 5556 7407b1160b46
parent 65 9c471840acb9
child 97 c3f12fd0c823
permissions -rw-r--r--
Close 'tls' branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
12
90f22275f7ae Moved server module to net/
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
     3
server = require "net.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
36
62998e5319e3 Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
    11
62998e5319e3 Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
    12
dofile "lxmppd.cfg"
62998e5319e3 Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
    13
 
62998e5319e3 Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
    14
sessions = {};
18
ae161e907149 Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
    15
 
49
1cd2a8db392d New "import" module to help tidy up all the local declarations at the top of modules
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
    16
require "util.import"
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    17
require "core.stanza_dispatch"
20
6885fd2cf51f Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
    18
require "core.xmlhandlers"
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    19
require "core.rostermanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    20
require "core.offlinemessage"
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
    21
require "core.modulemanager"
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    22
require "core.usermanager"
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
    23
require "core.sessionmanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
    24
require "core.stanza_router"
38
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
    25
require "net.connhandlers"
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    26
require "util.stanza"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    27
require "util.jid"
49
1cd2a8db392d New "import" module to help tidy up all the local declarations at the top of modules
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
    28
18
ae161e907149 Beginning of new routing logic
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
    29
 
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    30
-- Locals for faster access --
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    31
local t_insert = table.insert;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    32
local t_concat = table.concat;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    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
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    34
local m_random = math.random;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    35
local format = string.format;
53
14ea0fe6ca86 Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents: 49
diff changeset
    36
local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    37
local st = stanza;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    38
------------------------------
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    39
36
62998e5319e3 Moved hosts to a config file, still need better config though
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
    40
0
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
local hosts, users = hosts, users;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    43
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    44
function connect_host(host)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    45
	hosts[host] = { type = "remote", sendbuffer = {} };
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    46
end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    47
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    48
function handler(conn, data, err)
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    49
	local session = sessions[conn];
7
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    50
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    51
	if not session then
53
14ea0fe6ca86 Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents: 49
diff changeset
    52
		sessions[conn] = sm_new_session(conn);
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    53
		session = sessions[conn];
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    54
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    55
		-- Logging functions --
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    56
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    57
		local mainlog, log = log;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    58
		do
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    59
			local conn_name = tostring(conn):match("%w+$");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    60
			log = function (type, area, message) mainlog(type, conn_name, message); end
7
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    61
			--log = function () end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    62
		end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    63
		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
    64
		session.log = log;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    65
7
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    66
		print("Client connected");
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    67
		
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
    68
		session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
38
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
    69
		
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
    70
		session.connhandler = connhandlers.new("xmpp-client", session);
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    71
			
7
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    72
		function session.disconnect(err)
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    73
			if session.last_presence and session.last_presence.attr.type ~= "unavailable" then
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    74
				local pres = st.presence{ type = "unavailable" };
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    75
				if err == "closed" then err = "connection closed"; end
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    76
				pres:tag("status"):text("Disconnected: "..err);
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    77
				session.stanza_dispatch(pres);
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    78
			end
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    79
			session = nil;
53
14ea0fe6ca86 Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents: 49
diff changeset
    80
			print("Disconnected: "..tostring(err));
7
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    81
			collectgarbage("collect");
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    82
		end
7
dcc5ac721c20 - Remove some debugging code
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
    83
	end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    84
	if data then
38
Matthew Wild <mwild1@gmail.com>
parents: 37
diff changeset
    85
		session.connhandler:data(data);
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    86
	end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    87
	
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    88
	--log("info", "core", "Client disconnected, connection closed");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    89
end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    90
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    91
function disconnect(conn, err)
53
14ea0fe6ca86 Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents: 49
diff changeset
    92
	sm_destroy_session(sessions[conn]);
34
fd693ef5d978 Fixed: Session data was never removed from sessions list
Waqas Hussain <waqas20@gmail.com>
parents: 33
diff changeset
    93
	sessions[conn] = nil;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    94
end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    95
33
091f91a1f67a Let modules set/write globals
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    96
modulemanager.loadall();
091f91a1f67a Let modules set/write globals
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    97
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    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 }) --]][][[]][];
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
    99
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
   100
20
6885fd2cf51f Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
   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;
6885fd2cf51f Remove some debugging messages
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
   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;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
   103
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
   104
server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, ssl_ctx ) -- server.add will send a status message
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
   105
--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
   106
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
   107
server.loop();