]> PatchyGit - patchyircd.git/commitdiff
lua: don't hardcode the lua path
authorEgor <egor@patchysicp.xyz>
Thu, 27 Mar 2025 07:22:46 +0000 (09:22 +0200)
committerEgor <egor@patchysicp.xyz>
Thu, 27 Mar 2025 07:22:46 +0000 (09:22 +0200)
README.md
lua/init.lua
src/admin.cpp
src/main.cpp
src/util.cpp
src/util.h

index 89c90e1..3149caa 100644 (file)
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ by prefixing the line with `$123` (`$0` sets the time back to real time). Faking
 nick and time is useful for JOIN and TOPIC commands. `chanlog.txt` records all
 channels changes in this format.
 (NOTE: this feature is deprecated. We only use it for bootstrapping lua:
-`LUA eval :dofile"lua/init.lua"`. Then in `lua/local.lua` we set up the
+`LUA eval :dofile(getluafile"init.lua")`. Then in `lua/local.lua` we set up the
 channels. Also, be sure to PART any `--autojoin` channels if you want to give
 those a specific creation timestamp.)
 
@@ -66,6 +66,12 @@ With `--autojoin \#ircmoment` clients will automatically join the `#ircmoment`
 channel. This option can be specified multiple times.
 
 Data directory can be configured with `--datadir` argument. The default can be
-configured at compile time via IRCDDATADIR define. Otherwise it deafults to
-LOCALSTATEDIR/lib/patchyircd if LOCALSTATEDIR is defined. Otherwise it's "data"
-in the working directory.
+configured at compile time via `IRCDDATADIR` define. Otherwise it deafults to
+`LOCALSTATEDIR/lib/patchyircd` if `LOCALSTATEDIR` is defined. Otherwise it's
+`data` in the working directory.
+
+Similarly, the location of lua files is configured by `--luadir`, `IRCDLUADIR`,
+defaulting to `DATADIR/patchyircd/lua`, or `lua`.
+
+If you're installing system-wide, the following is sufficient:
+`-DLOCALSTATEDIR=/var -DDATADIR=/usr/share`
index 74c993d..7a139a3 100644 (file)
@@ -1,6 +1,6 @@
 function commands.reload(client, source, command, args)
        client_notice(client, "reloading...")
-       dofile "lua/init.lua"
+       dofile(getluafile "init.lua")
        client_notice(client, "done")
 end
 setmetatable(_G, getmetatable(ircd))
@@ -92,7 +92,7 @@ function commands.summon(client, source, command, args)
        channel_join(client_map[args[1]], args[2], true)
 end
 
-dofile "lua/irccloud.lua"
-dofile "lua/ctcp.lua"
-dofile "lua/spam.lua"
-pcall(dofile, "lua/local.lua")
+dofile(getluafile "irccloud.lua")
+dofile(getluafile "ctcp.lua")
+dofile(getluafile "spam.lua")
+pcall(dofile, getluafile "local.lua")
index 6ff8bbb..b3c85c9 100644 (file)
@@ -324,6 +324,18 @@ static int fn_db_read_rhs(lua_State *L) {
        lua_pushstring(L, res);
        return 1;
 }
+static int fn_getdatafile(lua_State *L) {
+       const char *fn = luaL_checkstring(L, 1);
+       const char *res = getdatafile(fn);
+       lua_pushstring(L, res);
+       return 1;
+}
+static int fn_getluafile(lua_State *L) {
+       const char *fn = luaL_checkstring(L, 1);
+       const char *res = getluafile(fn);
+       lua_pushstring(L, res);
+       return 1;
+}
 
 void admin_init(void)
 {
@@ -387,6 +399,8 @@ void admin_init(void)
                        {"client_chghost", fn_client_chghost},
                        {"rpl_whoisspecial", fn_rpl_whoisspecial},
                        {"db_read_rhs", fn_db_read_rhs},
+                       {"getdatafile", fn_getdatafile},
+                       {"getluafile", fn_getluafile},
                        {0, 0},
                };
                lua_pushglobaltable(L);
index e0b3ccf..e235383 100644 (file)
@@ -236,6 +236,11 @@ int main(int argc, char *argv[])
                                return fprintf(stderr, "not enough parameters\n"), 1;
                        datadir = argv[i+1];
                        i += 1;
+               } else if(!strcmp(argv[i], "--luadir")) {
+                       if (i + 1 >= argc)
+                               return fprintf(stderr, "not enough parameters\n"), 1;
+                       luadir = argv[i+1];
+                       i += 1;
                } else {
                        return fprintf(stderr, "unknown flag '%s'\n", argv[i]), 1;
                }
index 7658e02..e0fa342 100644 (file)
@@ -138,6 +138,20 @@ int irc_glob(const char *key, const char *mask)
        return 1;
 }
 
+static const char *getdirfile_r(char *buf, size_t size, const char *dir, const char *fn)
+{
+       size_t len1 = strlen(dir);
+       size_t len2 = strlen(fn);
+       if (len1+1+len2+1 > size)
+               return 0;
+       memcpy(buf, dir, len1);
+       buf[len1] = '/';
+       int needslash = len1 && dir[len1-1] != '/';
+       memcpy(buf+len1+needslash, fn, len2);
+       buf[len1+needslash+len2] = '\0';
+       return buf;
+}
+
 #ifndef IRCDDATADIR
 # ifdef LOCALSTATEDIR
 #  define IRCDDATADIR LOCALSTATEDIR "/lib/patchyircd"
@@ -149,14 +163,23 @@ const char *datadir = IRCDDATADIR;
 const char *getdatafile(const char *fn)
 {
        static char buf[256];
-       size_t len1 = strlen(datadir);
-       size_t len2 = strlen(fn);
-       if (len1+1+len2+1 > sizeof(buf))
+       if (!getdirfile_r(buf, sizeof(buf), datadir, fn))
+               abort();
+       return buf;
+}
+
+#ifndef IRCDLUADIR
+# ifdef DATADIR
+#  define IRCDLUADIR DATADIR "/patchyircd/lua"
+# else
+#  define IRCDLUADIR "lua"
+# endif
+#endif
+const char *luadir = IRCDLUADIR;
+const char *getluafile(const char *fn)
+{
+       static char buf[256];
+       if (!getdirfile_r(buf, sizeof(buf), luadir, fn))
                abort();
-       memcpy(buf, datadir, len1);
-       buf[len1] = '/';
-       int needslash = len1 && datadir[len1-1] != '/';
-       memcpy(buf+len1+needslash, fn, len2);
-       buf[len1+needslash+len2] = '\0';
        return buf;
 }
index d06776f..5749f89 100644 (file)
@@ -29,4 +29,6 @@ void set_time(time_t t);
 int irc_glob(const char *key, const char *mask);
 extern const char *datadir;
 const char *getdatafile(const char *fn);
+extern const char *luadir;
+const char *getluafile(const char *fn);
 #endif