Automated merge with http://waqas.ath.cx:8000/
[prosody.git] / net / connlisteners.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local listeners_dir = (CFG_SOURCEDIR or ".").."/net/";
23 local server_add = require "net.server".add;
24 local log = require "util.logger".init("connlisteners");
25
26 local dofile, pcall, error = 
27         dofile, pcall, error
28
29 module "connlisteners"
30
31 local listeners = {};
32
33 function register(name, listener)
34         if listeners[name] and listeners[name] ~= listener then
35                 log("warning", "Listener %s is already registered, not registering any more", name);
36                 return false;
37         end
38         listeners[name] = listener;
39         log("info", "Registered connection listener %s", name);
40         return true;
41 end
42
43 function deregister(name)
44         listeners[name] = nil;
45 end
46
47 function get(name)
48         local h = listeners[name];
49         if not h then
50                 local ok, ret = pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua");
51                 if not ok then return nil, ret; end
52                 h = listeners[name];
53         end
54         return h;
55 end
56
57 function start(name, udata)
58         local h = get(name);
59         if not h then
60                 error("No such connection module: "..name, 0);
61         end
62         return server_add(h, 
63                         (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), 
64                                 (udata and udata.interface) or "*", (udata and udata.mode) or h.default_mode or 1, (udata and udata.ssl) or nil );
65 end
66
67 return _M;