Fixed URL encoding to generate %0x instead of %x
[prosody.git] / prosody
1 #!/usr/bin/env lua
2 -- Prosody IM v0.2
3 -- Copyright (C) 2008 Matthew Wild
4 -- Copyright (C) 2008 Waqas Hussain
5 -- 
6 -- This program is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU General Public License
8 -- as published by the Free Software Foundation; either version 2
9 -- of the License, or (at your option) any later version.
10 -- 
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 -- GNU General Public License for more details.
15 -- 
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 --
20
21 -- Config here --
22
23 CFG_SOURCEDIR=nil;
24 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
25 CFG_PLUGINDIR=nil;
26 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
27
28 -- -- -- -- -- --
29
30 if CFG_SOURCEDIR then
31         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
32         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
33 end
34
35 if CFG_DATADIR then
36         if os.getenv("HOME") then
37                 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
38         end
39 end
40
41 -- Required to be able to find packages installed with luarocks
42 pcall(require, "luarocks.require")
43
44
45 config = require "core.configmanager"
46 log = require "util.logger".init("general");
47
48 -- Disable log output, needs to read from config
49 -- require "util.logger".setwriter(function () end);
50
51 do
52         -- TODO: Check for other formats when we add support for them
53         -- Use lfs? Make a new conf/ dir?
54         local ok, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
55         if not ok then
56                 log("error", "Couldn't load config file: %s", err);
57                 log("info", "Falling back to old config file format...")
58                 ok, err = pcall(dofile, "lxmppd.cfg");
59                 if not ok then
60                         log("error", "Old config format loading failed too: %s", err);
61                 else
62                         for _, host in ipairs(_G.config.hosts) do
63                                 config.set(host, "core", "defined", true);
64                         end
65                         
66                         config.set("*", "core", "modules_enabled", _G.config.modules);
67                         config.set("*", "core", "ssl", _G.config.ssl_ctx);
68                 end
69         end
70 end
71
72 local server = require "net.server"
73
74 require "util.dependencies"
75
76 -- Maps connections to sessions --
77 sessions = {};
78 hosts = {};
79
80 -- Load and initialise core modules --
81
82 require "util.import"
83 require "core.xmlhandlers"
84 require "core.rostermanager"
85 require "core.offlinemessage"
86 require "core.eventmanager"
87 require "core.hostmanager"
88 require "core.modulemanager"
89 require "core.usermanager"
90 require "core.sessionmanager"
91 require "core.stanza_router"
92
93 --[[
94 pcall(require, "remdebug.engine");
95 if remdebug then remdebug.engine.start() end
96 ]]
97
98 local cl = require "net.connlisteners";
99
100 require "util.stanza"
101 require "util.jid"
102
103 ------------------------------------------------------------------------
104
105
106 ------------- Begin code without a home ---------------------
107
108 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
109 require "util.datamanager".set_data_path(data_path);
110
111
112 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
113 local _mkdir = {}
114 function mkdir(path)
115         path = path:gsub("/", path_separator);
116         local x = io.popen("mkdir \""..path.."\" 2>&1"):read("*a");
117 end
118 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
119 function mkdirs(host)
120         if not _mkdir[host] then
121                 local host_dir = string.format("%s/%s", data_path, encode(host));
122                 mkdir(host_dir);
123                 mkdir(host_dir.."/accounts");
124                 mkdir(host_dir.."/vcard");
125                 mkdir(host_dir.."/roster");
126                 mkdir(host_dir.."/private");
127                 mkdir(host_dir.."/offline");
128                 _mkdir[host] = true;
129         end
130 end
131 mkdir(data_path);
132
133 eventmanager.add_event_hook("host-activated", mkdirs);
134
135 ----------- End of out-of-place code --------------
136
137 eventmanager.fire_event("server-starting");
138
139
140 -- setup error handling
141 setmetatable(_G, { __index = function (t, k) error("Attempt to read a non-existent global '"..k.."'", 2); end, __newindex = function (t, k, v) error("Attempt to set a global: "..tostring(k).." = "..tostring(v), 2); end });
142
143 local global_ssl_ctx = config.get("*", "core", "ssl");
144 if global_ssl_ctx then
145         local default_ssl_ctx = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none"; };
146         setmetatable(global_ssl_ctx, { __index = default_ssl_ctx });
147 end
148
149 -- start listening on sockets
150 local function do_ports(option, listener, default, key)
151         local ports = config.get("*", "core", option) or default;
152         --if type(ports) == "number" then ports = {ports} end;
153         if type(ports) ~= "table" then
154                 log("error", "core."..option.." is not a table");
155         else
156                 for _, port in ipairs(ports) do
157                         if type(port) ~= "number" then
158                                 log("error", "Non-numeric "..option..": "..tostring(port));
159                         else
160                                 cl.start(listener, { ssl = global_ssl_ctx, [key] = port });
161                         end
162                 end
163         end
164 end
165
166 do_ports("c2s_ports", "xmppclient", {5222}, "port");
167 do_ports("s2s_ports", "xmppserver", {5269}, "port");
168 do_ports("legacy_ssl_ports", "xmppclient", {}, "legacy_ssl_port");
169
170 if config.get("*", "core", "console_enabled") then
171         if cl.get("console") then
172                 cl.start("console")
173         else
174                 log("error", "Console is enabled, but the console module appears not to be loaded");
175         end
176 end
177
178 eventmanager.fire_event("server-started");
179
180 server.loop();