net/connlisteners.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 27 Sep 2009 12:10:50 +0100
changeset 1835 5ae3209fefa2
parent 1523 841d61be198f
child 2036 0f9c121713e1
permissions -rw-r--r--
Merge with waqas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1099
diff changeset
     1
-- Prosody IM
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 471
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 471
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 471
diff changeset
     8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 471
diff changeset
     9
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
471
727d7bd97cd2 Fix for loading connlisteners when running without CFG_SOURCEDIR
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
    11
local listeners_dir = (CFG_SOURCEDIR or ".").."/net/";
658
1952fdcf1017 Fix specifying ports in config, and SSL support
Matthew Wild <mwild1@gmail.com>
parents: 624
diff changeset
    12
local server = require "net.server";
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local log = require "util.logger".init("connlisteners");
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local dofile, pcall, error = 
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	dofile, pcall, error
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
module "connlisteners"
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
local listeners = {};
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
function register(name, listener)
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	if listeners[name] and listeners[name] ~= listener then
1099
127e6ae089f8 net.connlisteners: Lower log level of multiple listeners warning (not interesting to end-users)
Matthew Wild <mwild1@gmail.com>
parents: 908
diff changeset
    24
		log("debug", "Listener %s is already registered, not registering any more", name);
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
		return false;
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	listeners[name] = listener;
1099
127e6ae089f8 net.connlisteners: Lower log level of multiple listeners warning (not interesting to end-users)
Matthew Wild <mwild1@gmail.com>
parents: 908
diff changeset
    28
	log("debug", "Registered connection listener %s", name);
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	return true;
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
function deregister(name)
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
	listeners[name] = nil;
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
127
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    36
function get(name)
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    37
	local h = listeners[name];
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	if not h then
624
04fe1a00aa16 Protect loading of connlisteners, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    39
		local ok, ret = pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua");
04fe1a00aa16 Protect loading of connlisteners, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    40
		if not ok then return nil, ret; end
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		h = listeners[name];
127
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    42
	end
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    43
	return h;
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    44
end
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    45
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    46
function start(name, udata)
721
51233a8ae3d4 net.connlisteners: Fix to report errors loading connlisteners
Matthew Wild <mwild1@gmail.com>
parents: 661
diff changeset
    47
	local h, err = get(name);
127
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
    48
	if not h then
721
51233a8ae3d4 net.connlisteners: Fix to report errors loading connlisteners
Matthew Wild <mwild1@gmail.com>
parents: 661
diff changeset
    49
		error("No such connection module: "..name.. (err and (" ("..err..")") or ""), 0);
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	end
658
1952fdcf1017 Fix specifying ports in config, and SSL support
Matthew Wild <mwild1@gmail.com>
parents: 624
diff changeset
    51
	
739
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    52
	if udata then
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    53
		if (udata.type == "ssl" or udata.type == "tls") and not udata.ssl then
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    54
			error("No SSL context supplied for a "..tostring(udata.type):upper().." connection!", 0);
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    55
		elseif udata.ssl and udata.type == "tcp" then
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    56
			error("SSL context supplied for a TCP connection!", 0);
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    57
		end
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    58
	end
658
1952fdcf1017 Fix specifying ports in config, and SSL support
Matthew Wild <mwild1@gmail.com>
parents: 624
diff changeset
    59
	
739
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 721
diff changeset
    60
	return server.addserver(h, 
380
2b22b8eee939 Small fix for connlisteners to accept nil for userdata
Matthew Wild <mwild1@gmail.com>
parents: 145
diff changeset
    61
			(udata and udata.port) or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0), 
908
b0ecd18e0558 net.connlisteners: Allow listeners to specify default interface
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    62
				(udata and udata.interface) or h.default_interface or "*", (udata and udata.mode) or h.default_mode or 1, (udata and udata.ssl) or nil, 99999999, udata and udata.type == "ssl");
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 380
diff changeset
    65
return _M;