annotate prosody @ 14180:573ac422ca1d 13.0

util.pposix: Fix incorrect syslog facility mapping The array of Lua constants accidentally omitted "news", leading to later constants ("syslog", "user" and "uucp") being assigned to the wrong POSIX constants.
author Matthew Wild <mwild1@gmail.com>
date Mon, 25 May 2026 15:23:48 +0100
parents 2462247ec377
children af28d6debaca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
1 #!/usr/bin/env lua
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1493
diff changeset
2 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
3 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
4 -- Copyright (C) 2008-2010 Waqas Hussain
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
5 --
761
67ec69001fd7 Update main prosody file, since it doesn't match *.lua pattern, and sed -i treats symlinks badly
Matthew Wild <mwild1@gmail.com>
parents: 755
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
67ec69001fd7 Update main prosody file, since it doesn't match *.lua pattern, and sed -i treats symlinks badly
Matthew Wild <mwild1@gmail.com>
parents: 755
diff changeset
7 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
8 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
9
3740
69f95537e9e4 prosody: Added a comment, to match prosodyctl.
Waqas Hussain <waqas20@gmail.com>
parents: 3713
diff changeset
10 -- prosody - main executable for Prosody XMPP server
69f95537e9e4 prosody: Added a comment, to match prosodyctl.
Waqas Hussain <waqas20@gmail.com>
parents: 3713
diff changeset
11
843
1d2dab41b0db prosody: Protect main loop. Dare I say crashing finally becomes impossible.
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
12 -- Will be modified by configure script if run --
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
13
7297
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
14 CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
15 CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
16 CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
17 CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
18
2147
119323e35c32 Mainfile: Fixed some comments.
Waqas Hussain <waqas20@gmail.com>
parents: 2087
diff changeset
19 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
20
14055
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
21 -- Check before first require, to preempt the probable failure
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
22 if _VERSION < "Lua 5.2" then
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
23 io.stderr:write("Prosody is no longer compatible with Lua 5.1\n")
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
24 io.stderr:write("See https://prosody.im/doc/depends#lua for more information\n")
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
25 return os.exit(1);
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
26 end
f47b4baa7934 prosody: Abort earlier on incompatible Lua version
Kim Alvefur <zash@zash.se>
parents: 12971
diff changeset
27
2337
9eb20b3f3bbb prosody: Clarify and add some comments to describe what we're doing when and why
Matthew Wild <mwild1@gmail.com>
parents: 2330
diff changeset
28 -- Tell Lua where to find our libraries
14068
2462247ec377 prosody, prosodyctl: Always search installation directory first
Kim Alvefur <zash@zash.se>
parents: 14066
diff changeset
29 -- and ensure that resources in the current directory are not picked up by accident
2462247ec377 prosody, prosodyctl: Always search installation directory first
Kim Alvefur <zash@zash.se>
parents: 14066
diff changeset
30 do
14057
312542dd184d prosody: Fix loading local code when also installed system-wide
Kim Alvefur <zash@zash.se>
parents: 14056
diff changeset
31 -- Installed instance
14056
5b4efb5b798a prosody: Move function only used once into block where it is used
Kim Alvefur <zash@zash.se>
parents: 14055
diff changeset
32 local function is_relative(path)
5b4efb5b798a prosody: Move function only used once into block where it is used
Kim Alvefur <zash@zash.se>
parents: 14055
diff changeset
33 local path_sep = package.config:sub(1,1);
5b4efb5b798a prosody: Move function only used once into block where it is used
Kim Alvefur <zash@zash.se>
parents: 14055
diff changeset
34 return ((path_sep == "/" and path:sub(1,1) ~= "/")
5b4efb5b798a prosody: Move function only used once into block where it is used
Kim Alvefur <zash@zash.se>
parents: 14055
diff changeset
35 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
5b4efb5b798a prosody: Move function only used once into block where it is used
Kim Alvefur <zash@zash.se>
parents: 14055
diff changeset
36 end
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
37 local function filter_relative_paths(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
38 if is_relative(path) then return ""; end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
39 end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
40 local function sanitise_paths(paths)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
41 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
42 end
14068
2462247ec377 prosody, prosodyctl: Always search installation directory first
Kim Alvefur <zash@zash.se>
parents: 14066
diff changeset
43 package.path = (CFG_SOURCEDIR or ".") .. "/?.lua;" .. sanitise_paths(package.path);
2462247ec377 prosody, prosodyctl: Always search installation directory first
Kim Alvefur <zash@zash.se>
parents: 14066
diff changeset
44 package.cpath = (CFG_SOURCEDIR or ".") .. "/?.so;" .. sanitise_paths(package.cpath);
14057
312542dd184d prosody: Fix loading local code when also installed system-wide
Kim Alvefur <zash@zash.se>
parents: 14056
diff changeset
45
14068
2462247ec377 prosody, prosodyctl: Always search installation directory first
Kim Alvefur <zash@zash.se>
parents: 14066
diff changeset
46 if pcall(dofile, (CFG_SOURCEDIR or ".") .. "/loader.lua") then
14066
b085d32af140 prosody, prosodyctl: Load loader directly from source directory
Kim Alvefur <zash@zash.se>
parents: 14057
diff changeset
47 package.loaded["prosody.loader"] = true;
b085d32af140 prosody, prosodyctl: Load loader directly from source directory
Kim Alvefur <zash@zash.se>
parents: 14057
diff changeset
48 else
b085d32af140 prosody, prosodyctl: Load loader directly from source directory
Kim Alvefur <zash@zash.se>
parents: 14057
diff changeset
49 require "prosody.loader";
b085d32af140 prosody, prosodyctl: Load loader directly from source directory
Kim Alvefur <zash@zash.se>
parents: 14057
diff changeset
50 end
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
51 end
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
52
2337
9eb20b3f3bbb prosody: Clarify and add some comments to describe what we're doing when and why
Matthew Wild <mwild1@gmail.com>
parents: 2330
diff changeset
53 -- Substitute ~ with path to home directory in data path
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
54 if CFG_DATADIR then
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: 455
diff changeset
55 if os.getenv("HOME") then
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
56 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
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: 455
diff changeset
57 end
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
58 end
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: 455
diff changeset
59
10597
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 8726
diff changeset
60
12971
7214baed9e9d executables: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12954
diff changeset
61 local startup = require "prosody.util.startup";
7214baed9e9d executables: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12954
diff changeset
62 local async = require "prosody.util.async";
2587
c37f971f0fe6 prosody, prosodyctl: Re-jiggle load order again, fixes logging config not being obeyed (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 2567
diff changeset
63
8682
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
64 -- Note: it's important that this thread is not GC'd, as some C libraries
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
65 -- that are initialized here store a pointer to it ( :/ ).
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
66 local thread = async.runner();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
67
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
68 thread:run(startup.prosody);
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
69
12296
49ebac8a5260 prosody: Expose main thread on the 'prosody' global
Kim Alvefur <zash@zash.se>
parents: 12295
diff changeset
70 prosody.main_thread = thread;
49ebac8a5260 prosody: Expose main thread on the 'prosody' global
Kim Alvefur <zash@zash.se>
parents: 12295
diff changeset
71
8635
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8285
diff changeset
72 local function loop()
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
73 -- Error handler for errors that make it this far
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
74 local function catch_uncaught_error(err)
2769
826f6fb7036d prosody: Less strict matching for the magic 'interrupted' error
Matthew Wild <mwild1@gmail.com>
parents: 2321
diff changeset
75 if type(err) == "string" and err:match("interrupted!$") then
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
76 return "quitting";
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
77 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
78
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
79 prosody.log("error", "Top-level error, please report:\n%s", tostring(err));
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
80 local traceback = debug.traceback("", 2);
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
81 if traceback then
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
82 prosody.log("error", "%s", traceback);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
83 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
84
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
85 prosody.events.fire_event("very-bad-error", {error = err, traceback = traceback});
992
3153eff6dae2 prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
Matthew Wild <mwild1@gmail.com>
parents: 978
diff changeset
86 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
87
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
88 local sleep = require"socket".sleep;
12971
7214baed9e9d executables: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12954
diff changeset
89 local server = require "prosody.net.server";
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
90
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
91 while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
92 sleep(0.2);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
93 end
992
3153eff6dae2 prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
Matthew Wild <mwild1@gmail.com>
parents: 978
diff changeset
94 end
1026
e640df2e4e9b prosody: Fire events during server shutdown process
Matthew Wild <mwild1@gmail.com>
parents: 1017
diff changeset
95
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
96 loop();
853
c0a40522041e prosody: Log top-level errors
Matthew Wild <mwild1@gmail.com>
parents: 843
diff changeset
97
12553
cc0ec0277813 util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents: 12296
diff changeset
98 startup.exit();