Software /
code /
prosody
Comparison
net/connlisteners.lua @ 4797:e239668aa6d2
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 29 Apr 2012 02:10:55 +0100 |
parent | 4322:aff627b1ce95 |
parent | 4792:bf107d9d4962 |
child | 6780:647adfd8f738 |
comparison
equal
deleted
inserted
replaced
4796:04a34287dc12 | 4797:e239668aa6d2 |
---|---|
1 -- Prosody IM | 1 -- COMPAT w/pre-0.9 |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 local log = require "util.logger".init("net.connlisteners"); |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 local traceback = debug.traceback; |
4 -- | |
5 -- This project is MIT/X11 licensed. Please see the | |
6 -- COPYING file in the source package for more information. | |
7 -- | |
8 | 4 |
5 module "httpserver" | |
9 | 6 |
10 | 7 function fail() |
11 local listeners_dir = (CFG_SOURCEDIR or ".").."/net/"; | 8 log("error", "Attempt to use legacy connlisteners API. For more info see http://prosody.im/doc/developers/network"); |
12 local server = require "net.server"; | 9 log("error", "Legacy connlisteners API usage, %s", traceback("", 2)); |
13 local log = require "util.logger".init("connlisteners"); | |
14 local tostring = tostring; | |
15 local type = type | |
16 local ipairs = ipairs | |
17 | |
18 local dofile, xpcall, error = | |
19 dofile, xpcall, error | |
20 | |
21 local debug_traceback = debug.traceback; | |
22 | |
23 module "connlisteners" | |
24 | |
25 local listeners = {}; | |
26 | |
27 function register(name, listener) | |
28 if listeners[name] and listeners[name] ~= listener then | |
29 log("debug", "Listener %s is already registered, not registering any more", name); | |
30 return false; | |
31 end | |
32 listeners[name] = listener; | |
33 log("debug", "Registered connection listener %s", name); | |
34 return true; | |
35 end | 10 end |
36 | 11 |
37 function deregister(name) | 12 register, deregister = fail, fail; |
38 listeners[name] = nil; | 13 get, start = fail, fail, epic_fail; |
39 end | |
40 | |
41 function get(name) | |
42 local h = listeners[name]; | |
43 if not h then | |
44 local ok, ret = xpcall(function() dofile(listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua") end, debug_traceback); | |
45 if not ok then | |
46 log("error", "Error while loading listener '%s': %s", tostring(name), tostring(ret)); | |
47 return nil, ret; | |
48 end | |
49 h = listeners[name]; | |
50 end | |
51 return h; | |
52 end | |
53 | |
54 function start(name, udata) | |
55 local h, err = get(name); | |
56 if not h then | |
57 error("No such connection module: "..name.. (err and (" ("..err..")") or ""), 0); | |
58 end | |
59 | |
60 local interfaces = (udata and udata.interface) or h.default_interface or "*"; | |
61 if type(interfaces) == "string" then interfaces = {interfaces}; end | |
62 local port = (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); | |
63 local mode = (udata and udata.mode) or h.default_mode or 1; | |
64 local ssl = (udata and udata.ssl) or nil; | |
65 local autossl = udata and udata.type == "ssl"; | |
66 | |
67 if autossl and not ssl then | |
68 return nil, "no ssl context"; | |
69 end | |
70 | |
71 ok, err = true, {}; | |
72 for _, interface in ipairs(interfaces) do | |
73 local handler | |
74 handler, err[interface] = server.addserver(interface, port, h, mode, autossl and ssl or nil); | |
75 ok = ok and handler; | |
76 end | |
77 | |
78 return ok, err; | |
79 end | |
80 | 14 |
81 return _M; | 15 return _M; |