From: Egor Date: Thu, 19 Oct 2023 13:31:44 +0000 (+0300) Subject: move data files to data directory X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=25d8d0d9da239119e9e35b44a0383a2fb839e8a4;p=patchyircd.git move data files to data directory mv cert.pem chanlog.txt help.txt init.txt key.pem motd.txt \ newusers.txt ops.txt users.txt data --- diff --git a/.gitignore b/.gitignore index 3c6d701..71a2926 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,4 @@ ircd ircd.exe -config.mk setpass -ops.txt -init.txt -chanlog.txt -cert.pem -key.pem -users.txt -newusers.txt -help.txt -motd.txt +config.mk diff --git a/README.md b/README.md index e8f7769..8b13c38 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ make Some things are configurable via compile-time flags. In particular, `-DHAS_TLS` and some constants defined in `src/config.h`. +## Config files +These have to go in IRCDDATADIR (`data` by default). + For TLS you gonna need to place your certificate and private key in `cert.pem` and `key.pem` respectively. diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..d21035f --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,9 @@ +ops.txt +init.txt +chanlog.txt +cert.pem +key.pem +users.txt +newusers.txt +help.txt +motd.txt diff --git a/help.example.txt b/data/help.example.txt similarity index 100% rename from help.example.txt rename to data/help.example.txt diff --git a/src/config.h b/src/config.h index b59079f..f34bca1 100644 --- a/src/config.h +++ b/src/config.h @@ -11,3 +11,6 @@ #ifndef IRCDHOSTNAME #define IRCDHOSTNAME "example.com" #endif +#ifndef IRCDDATADIR +#define IRCDDATADIR "data" +#endif diff --git a/src/irc.c b/src/irc.c index db166fd..274c726 100644 --- a/src/irc.c +++ b/src/irc.c @@ -320,7 +320,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (argc < 2) return err_needmoreparams(client, command), sendbuf(client), 0; stringlower(args[0]); - int res = password_check(args[0], args[1], "ops.txt"); + int res = password_check(args[0], args[1], getdatafile("ops.txt")); memset(args[1], 0, strlen(args[1])); if (!(client->umode & UMODE_SECURE) || res == -1) { err_passwdmismatch(client), sendbuf(client); @@ -340,7 +340,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (!hash) return 0; // TODO: error message memset(args[1], 0, strlen(args[1])); - FILE *f = fopen("newusers.txt", "ab"); + FILE *f = fopen(getdatafile("newusers.txt"), "ab"); if (!f) return 0; // TODO: error message fprintf(f, "%s:%s\n", args[0], hash); @@ -623,7 +623,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (!is_registered(client)) return err_notregistered(client), sendbuf(client), 0; char line[512]; - FILE *f = fopen("help.txt", "r"); + FILE *f = fopen(getdatafile("help.txt"), "r"); if (f) { rpl_helpstart(client), sendbuf(client); while (fgets(line, 512, f)) { @@ -1097,7 +1097,7 @@ void newUser(struct client_data *client) { if (client->regstate == REG_CAPLS || !client->nick_reg || !client->username) return; if (client->username[0] == '~' && client->pass_reg) { - int res = password_check(client->username + 1, client->pass_reg, "users.txt"); + int res = password_check(client->username + 1, client->pass_reg, getdatafile("users.txt")); memset(client->pass_reg, 0, strlen(client->pass_reg)); free(client->pass_reg); client->pass_reg = NULL; @@ -1170,7 +1170,7 @@ void sendLUSERS(struct client_data *client) void sendMOTD(struct client_data *client) { char line[512]; - FILE *f = fopen("motd.txt", "r"); + FILE *f = fopen(getdatafile("motd.txt"), "r"); if (f) { rpl_motdstart(client), sendbuf(client); while (fgets(line, 512, f)) { diff --git a/src/main.c b/src/main.c index c0793ed..fc9d75e 100644 --- a/src/main.c +++ b/src/main.c @@ -206,9 +206,9 @@ int main(int argc, char *argv[]) return fprintf(stderr, "tls_init error\n"), 1; if (!(tls_cfg = tls_config_new())) return fprintf(stderr, "tls_config_new error\n"), 1; - if (tls_config_set_cert_file(tls_cfg, "cert.pem") == -1) + if (tls_config_set_cert_file(tls_cfg, getdatafile("cert.pem")) == -1) return fprintf(stderr, "tls_config_set_cert_file: %s\n", tls_config_error(tls_cfg)), 1; - if (tls_config_set_key_file(tls_cfg, "key.pem") == -1) + if (tls_config_set_key_file(tls_cfg, getdatafile("key.pem")) == -1) return fprintf(stderr, "tls_config_set_key_file: %s\n", tls_config_error(tls_cfg)), 1; tls_config_insecure_noverifycert(tls_cfg); tls_config_verify_client_optional(tls_cfg); @@ -257,7 +257,7 @@ int main(int argc, char *argv[]) for (int i = 0; i < listener_count; i++) net_nonblock(listeners[i].sock); - FILE *f = fopen("init.txt", "rb"); + FILE *f = fopen(getdatafile("init.txt"), "rb"); if (f) { struct client_data *fake = allocClient(); fake->umode |= UMODE_OPER | UMODE_SECURE; @@ -305,7 +305,7 @@ int main(int argc, char *argv[]) set_time(0); fclose(f); } - fchanlog = fopen("chanlog.txt", "ab"); + fchanlog = fopen(getdatafile("chanlog.txt"), "ab"); if (fchanlog) { fprintf(fchanlog, "$0 AAA :server starting %lu\r\n", (unsigned long)get_time()); fflush(fchanlog); diff --git a/src/util.c b/src/util.c index 35b4bee..bd14969 100644 --- a/src/util.c +++ b/src/util.c @@ -1,4 +1,5 @@ #include "util.h" +#include "config.h" #include #include #include @@ -136,3 +137,19 @@ int irc_glob(const char *key, const char *mask) } return 1; } + +static 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)) + 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; +} diff --git a/src/util.h b/src/util.h index e9ebc11..e42520d 100644 --- a/src/util.h +++ b/src/util.h @@ -27,4 +27,5 @@ char *mystrdup(const char *s); time_t get_time(); void set_time(time_t t); int irc_glob(const char *key, const char *mask); +const char *getdatafile(const char *fn); #endif