diff options
author | Ratakor <ratakor@disroot.org> | 2023-06-16 23:22:03 +0200 |
---|---|---|
committer | Ratakor <ratakor@disroot.org> | 2023-06-16 23:22:03 +0200 |
commit | 633bf2b35079cbcde3318efd43dd9e92f1cd9a18 (patch) | |
tree | e6effebd50a4f0e3ac3c71a721641bb80538ef53 | |
parent | e36044757eadcc2c45d68500a92f92d5b39ccf2d (diff) |
Rework quotes, add i/ufmt, add copyright to filesv0.0.11
Fix __log that would not output the right errno.
Remove argc/argv and root check.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | src/cmd_correct.c | 6 | ||||
-rw-r--r-- | src/cmd_help.c | 3 | ||||
-rw-r--r-- | src/cmd_info.c | 5 | ||||
-rw-r--r-- | src/cmd_lbraid.c | 4 | ||||
-rw-r--r-- | src/cmd_leaderboard.c | 4 | ||||
-rw-r--r-- | src/cmd_source.c | 2 | ||||
-rw-r--r-- | src/cmd_uraid.c | 6 | ||||
-rw-r--r-- | src/init.c | 2 | ||||
-rw-r--r-- | src/main.c | 13 | ||||
-rw-r--r-- | src/nolan.h | 3 | ||||
-rw-r--r-- | src/ocr.c | 3 | ||||
-rw-r--r-- | src/raids.c | 2 | ||||
-rw-r--r-- | src/roles.c | 2 | ||||
-rw-r--r-- | src/run.c | 2 | ||||
-rw-r--r-- | src/stats.c | 66 | ||||
-rw-r--r-- | src/util.c | 84 | ||||
-rw-r--r-- | src/util.h | 10 |
19 files changed, 129 insertions, 96 deletions
@@ -1,3 +1,5 @@ +# Copywrong © 2023 Ratakor. See LICENSE file for license details. + NAME = nolan PREFIX ?= /usr/local CC = cc @@ -12,14 +12,14 @@ A discord bot for Orna. - ascensions (track mats) ### chore +- change players to uint32_t - optimize raids.c and cmd_..raid with slayers - add documentation +- rework roles and make it available to other servers - add logging to file - rework DIFF in ocr.c and raids.c -- rework roles and make it available to other servers ### misc -- u64snowflake is an unsigned long long -- use sql instead of csv +- use sql for raids and roles - better wrong image input detection for raids - async ? diff --git a/src/cmd_correct.c b/src/cmd_correct.c index 2c79234..3161b71 100644 --- a/src/cmd_correct.c +++ b/src/cmd_correct.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <limits.h> #include <stdarg.h> #include <stdlib.h> @@ -214,10 +216,10 @@ stats", siz); return; } s += snprintf(buf, siz, "<@%lu>\n%s: ", userid, fields[j]); - s += longfmt(buf + s, siz - s, "'", old); + s += ufmt(buf + s, siz - s, old); if (j == DISTANCE) s += strlcpy(buf + s, "m", siz - s); s += strlcpy(buf + s, " -> ", siz - s); - s += longfmt(buf + s, siz - s, "'", new); + s += ufmt(buf + s, siz - s, new); if (j == DISTANCE) s += strlcpy(buf + s, "m", siz - s); ((long *)&players[i])[j] = new; } diff --git a/src/cmd_help.c b/src/cmd_help.c index 7d419ec..e960c9a 100644 --- a/src/cmd_help.c +++ b/src/cmd_help.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <string.h> #include "nolan.h" @@ -26,6 +28,7 @@ help(char *buf, size_t siz, u64snowflake guild_id) if (s >= siz) { WARN("string truncation\n\ \033[33mhint:\033[39m this is probably because stats_ids is too big"); + return; } } snprintf(buf + s, siz - s, "<#%lu> ", stats_ids[i]); diff --git a/src/cmd_info.c b/src/cmd_info.c index 75531b3..1936b3c 100644 --- a/src/cmd_info.c +++ b/src/cmd_info.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdlib.h> #include <string.h> @@ -77,8 +79,7 @@ write_info(char *buf, size_t siz, const Player *player) free(playtime); } else if (((const long *)player)[i]) { s += snprintf(buf + s, siz - s, "%s: ", fields[i]); - s += longfmt(buf + s, siz - s, "'", - ((const long *)player)[i]); + s += ufmt(buf + s, siz - s, ((const long *)player)[i]); if (i == DISTANCE) s += strlcpy(buf + s, "m", siz - s); s += strlcpy(buf + s, "\n", siz - s); } diff --git a/src/cmd_lbraid.c b/src/cmd_lbraid.c index f899ac6..91d6470 100644 --- a/src/cmd_lbraid.c +++ b/src/cmd_lbraid.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdlib.h> #include <string.h> #include <time.h> @@ -94,7 +96,7 @@ write_lbraid(char *buf, size_t siz, Slayer slayers[], size_t nslayers) } s += snprintf(buf + s, siz - s, "%d. %s: ", i, slayers[i].name); - s += uintfmt(buf + s, siz - s, slayers[i].damage); + s += ufmt(buf + s, siz - s, slayers[i].damage); s += strlcpy(buf + s, " damage\n", siz - s); } } diff --git a/src/cmd_leaderboard.c b/src/cmd_leaderboard.c index bc065b0..2503703 100644 --- a/src/cmd_leaderboard.c +++ b/src/cmd_leaderboard.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdlib.h> #include <string.h> @@ -166,7 +168,7 @@ write_player(char *buf, size_t siz, unsigned int i, int author) strlcat(buf, plt, siz); free(plt); } else { - longfmt(stat, ssiz, "'", ((long *)&players[i])[category]); + ufmt(stat, ssiz, ((long *)&players[i])[category]); strlcat(buf, stat, siz); if (i == DISTANCE) strlcat(buf, "m", siz); } diff --git a/src/cmd_source.c b/src/cmd_source.c index 377c62d..c3800cb 100644 --- a/src/cmd_source.c +++ b/src/cmd_source.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdlib.h> #include <string.h> diff --git a/src/cmd_uraid.c b/src/cmd_uraid.c index 292c493..d1ec3c3 100644 --- a/src/cmd_uraid.c +++ b/src/cmd_uraid.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdlib.h> #include <string.h> #include <time.h> @@ -81,7 +83,7 @@ write_uraid(char *buf, size_t siz, char *username, unsigned int *dmgs) if (d < 0) d = 6; total += dmgs[i]; s += snprintf(buf + s, siz - s, "%s: ", week[d]); - s += uintfmt(buf + s, siz - s, dmgs[i]); + s += ufmt(buf + s, siz - s, dmgs[i]); s += strlcpy(buf + s, " damage\n", siz - s); if (s >= siz) { WARN("string truncation"); @@ -89,7 +91,7 @@ write_uraid(char *buf, size_t siz, char *username, unsigned int *dmgs) } } s += strlcpy(buf + s, "\nTotal: ", siz - s); - s += uintfmt(buf + s, siz - s, total); + s += ufmt(buf + s, siz - s, total); s += strlcpy(buf + s, " damage", siz - s); if (s >= siz) WARN("string truncation"); @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <string.h> #include <sys/stat.h> @@ -1,6 +1,7 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <locale.h> #include <stdlib.h> -#include <unistd.h> #include "concord/discord.h" #include "concord/discord-internal.h" @@ -36,20 +37,12 @@ const char *fields[] = { }; int -main(int argc, char *argv[]) +main(void) { char *src[] = { "src", "source" }; char *lb[] = { "lb", "leaderboard" }; struct discord *client; -#ifdef DEVEL - UNUSED(argv); -#else - if (getuid() != 0) - DIE("please run %s as root", argv[0]); -#endif /* DEVEL */ - - UNUSED(argc); setlocale(LC_NUMERIC, ""); create_folders(); create_stats_file(); diff --git a/src/nolan.h b/src/nolan.h index cd89bd6..03d4f49 100644 --- a/src/nolan.h +++ b/src/nolan.h @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #ifndef NOLAN_H #define NOLAN_H @@ -48,6 +50,7 @@ enum { UPDATE, USERID, }; + typedef struct { char *name; char *kingdom; @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <curl/curl.h> #include <gd.h> #include <leptonica/allheaders.h> @@ -28,6 +30,7 @@ curl(char *url) CURL *handle = curl_easy_init(); char *buf = emalloc(MAX_MESSAGE_LEN); + *buf = '\0'; curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(handle, CURLOPT_WRITEDATA, buf); diff --git a/src/raids.c b/src/raids.c index 7cb2e24..06e8165 100644 --- a/src/raids.c +++ b/src/raids.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdarg.h> #include <stdlib.h> #include <string.h> diff --git a/src/roles.c b/src/roles.c index e6a6cd3..b63265a 100644 --- a/src/roles.c +++ b/src/roles.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include "nolan.h" static void level(struct discord *client, u64snowflake userid, Player *player); @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <string.h> #include "nolan.h" diff --git a/src/stats.c b/src/stats.c index 3d6af42..e6d67c7 100644 --- a/src/stats.c +++ b/src/stats.c @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdlib.h> #include <string.h> #include <time.h> @@ -19,6 +21,7 @@ static long playtime_to_long(char *playtime, int lang); static long trim_stat(const char *str); static void parse_line(Player *player, char *line); static void for_line(Player *player, char *txt); +static char *get_quote(void); static size_t write_quote(char *buf, size_t siz); static void create_player(Player *player, unsigned int i); static void update_player(char *buf, size_t siz, Player *player, unsigned int i); @@ -251,25 +254,53 @@ for_line(Player *player, char *txt) } } -size_t -write_quote(char *buf, size_t siz) +char * +get_quote(void) { - char *quote = curl(URL), *start, *end; - size_t ret; + char *quote = curl(URL), *p, *sentence, *name; - start = nstrchr(quote, '"', 3) + 1; - if (!start) { + if ((sentence = nstrchr(quote, '"', 3)) == NULL) { free(quote); - return 0; + return NULL; } - end = strchr(start, '"'); - if (!end) { + sentence++; + if ((name = nstrchr(quote, '"', 8)) == NULL) { free(quote); - return 0; + return NULL; + } + *name++ = '\n'; + *name++ = '>'; + *name = ' '; + name -= 2; + + if ((p = strchr(sentence, '"')) == NULL) { + free(quote); + return NULL; + } + *p = '\0'; + if ((p = strchr(name, '"')) == NULL) { + free(quote); + return NULL; } - *end = '\0'; + *p = '\0'; - ret = snprintf(buf, siz, "%s\n\n", start); + p = quote; + while ((*p++ = *sentence++)); + p--; + while ((*p++ = *name++)); + + return quote; +} + +size_t +write_quote(char *buf, size_t siz) +{ + char *quote = get_quote(); + size_t ret; + + if (quote == NULL) + return 0; + ret = snprintf(buf, siz, "%s\n\n", quote); free(quote); return ret; } @@ -296,7 +327,6 @@ update_player(char *buf, size_t siz, Player *player, unsigned int i) /* keep this commented to not update name and keep corrected change */ /* strlcpy(players[i].name, player->name, MAX_USERNAME_LEN); */ - s += write_quote(buf + s, siz - s); s += snprintf(buf + s, siz - s, "**%s**'s profile has been updated.\n", players[i].name); @@ -338,13 +368,13 @@ update_player(char *buf, size_t siz, Player *player, unsigned int i) free(pltd); } else { s += snprintf(buf + s, siz - s, "%s: ", fields[j]); - s += longfmt(buf + s, siz - s, "'", old); + s += ufmt(buf + s, siz - s, old); if (j == DISTANCE) s += strlcpy(buf + s, "m", siz - s); s += strlcpy(buf + s, " -> ", siz - s); - s += longfmt(buf + s, siz - s, "'", new); + s += ufmt(buf + s, siz - s, new); if (j == DISTANCE) s += strlcpy(buf + s, "m", siz - s); s += strlcpy(buf + s, " (", siz - s); - s += longfmt(buf + s, siz - s, "'+", diff); + s += ifmt(buf + s, siz - s, diff); if (j == DISTANCE) s += strlcpy(buf + s, "m", siz - s); s += strlcpy(buf + s, ")\n", siz - s); } @@ -423,18 +453,18 @@ update_players(char *buf, size_t siz, Player *player) while (i < nplayers && players[i].userid != player->userid) i++; + s += write_quote(buf + s, siz - s); if (i == nplayers) { /* new player */ nplayers++; if (nplayers > MAX_PLAYERS) DIE("there is too much players (max:%d)", MAX_PLAYERS); create_player(player, i); - s += write_quote(buf + s, siz - s); s += snprintf(buf + s, siz - s, "**%s** has been registrated in the database.\n", player->name); write_info(buf + s, siz - s, &players[i]); } else { - update_player(buf, siz, player, i); + update_player(buf + s, siz - s, player, i); } update_file(&players[i]); @@ -1,4 +1,7 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #include <stdarg.h> +#include <stdint.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> @@ -22,7 +25,7 @@ const char *lvl_strings[] = { "log", "warning", "error" }; const int lvl_colors[] = { CYAN, MAGENTA, RED }; char * -nstrchr(const char *s, int c, int n) +nstrchr(const char *s, int c, size_t n) { if (n == 0) return NULL; @@ -39,17 +42,17 @@ nstrchr(const char *s, int c, int n) size_t strlcpy(char *dst, const char *src, size_t siz) { - const char *p = src; + const char *osrc = src; if (siz == 0) /* duh */ return strlen(src); - while (--siz != 0 && (*dst++ = *p++) != '\0'); + while (--siz != 0 && (*dst++ = *src++) != '\0'); if (siz == 0) { *dst = '\0'; - while (*p++); + while (*src++); } - return p - src - 1; + return src - osrc - 1; } size_t @@ -61,13 +64,6 @@ strlcat(char *dst, const char *src, size_t siz) return dsiz + strlcpy(dst + dsiz, src, siz - dsiz); } -int -file_exists(const char *filename) -{ - struct stat buf; - return (stat(filename, &buf) == 0); -} - time_t ltime(void) { @@ -103,9 +99,9 @@ __log(int lvl, char *file, int line, const char *func, char *fmt, ...) vfprintf(stderr, fmt, ap); va_end(ap); - if (fmt == NULL) { + if (!fmt[0]) { perror(NULL); - } else if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { + } else if (fmt[strlen(fmt) - 1] == ':') { fputc(' ', stderr); perror(NULL); } else { @@ -120,7 +116,7 @@ emalloc(size_t size) void *p; if ((p = malloc(size)) == NULL) - DIE(NULL); + DIE(""); return p; } @@ -130,20 +126,20 @@ efopen(const char *filename, const char *mode) FILE *fp; if ((fp = fopen(filename, mode)) == NULL) - DIE("failed to open %s (%s):", filename, mode); + DIE("%s [%s]:", filename, mode); return fp; } size_t -uintfmt(char *dst, size_t dsiz, unsigned int num) +ufmt(char *dst, size_t dsiz, uint64_t n) { - char buf[16], *p = buf + sizeof(buf) - 1; + char buf[32], *p = buf + sizeof(buf) - 1; size_t sizn = 1; *p-- = '\0'; while (sizn < sizeof(buf)) { - *p-- = (num % 10) + '0'; - if ((num /= 10) == 0) + *p-- = (n % 10) + '0'; + if ((n /= 10) == 0) break; if ((sizn % 3) == 0) *p-- = ','; @@ -154,43 +150,25 @@ uintfmt(char *dst, size_t dsiz, unsigned int num) } size_t -longfmt(char *dst, size_t dsiz, const char *opt, long num) +ifmt(char *dst, size_t dsiz, int64_t n) { - char buf[32], *p = buf + sizeof(buf) - 1; - size_t sizn = 1; - int sign = 0, fmt = 0; - - do { - switch (*opt) { - case '\'': - fmt = 1; - break; - case '+': - sign = '+'; - break; - case ' ': - sign = ' '; - break; - } - } while (*opt++); + if (dsiz == 0) + return 0; - *p-- = '\0'; - if (num < 0) { - sign = '-'; - num *= -1; + if (n < 0) { + *dst = '-'; + n = -n; + } else { + *dst = '+'; } - while (sizn < sizeof(buf)) { - *p-- = (num % 10) + '0'; - if ((num /= 10) == 0) - break; - if (fmt && (sizn % 3) == 0) - *p-- = ','; - sizn++; - } + return ufmt(dst + 1, dsiz - 1, n); - if (sign && p - buf > 0) - *p-- = sign; +} - return strlcpy(dst, ++p, dsiz); +int +file_exists(const char *filename) +{ + struct stat buf; + return (stat(filename, &buf) == 0); } @@ -1,3 +1,5 @@ +/* Copywrong © 2023 Ratakor. See LICENSE file for license details. */ + #ifndef UTIL_H #define UTIL_H @@ -15,15 +17,15 @@ enum { LOG__, WARN__, DIE__ }; #define WARN(...) (__LOG(WARN__, __VA_ARGS__)) #define DIE(...) (__LOG(DIE__, __VA_ARGS__), exit(EXIT_FAILURE)) -char *nstrchr(const char *s, int c, int n); +char *nstrchr(const char *s, int c, size_t n); size_t strlcpy(char *dst, const char *src, size_t siz); size_t strlcat(char *dst, const char *src, size_t siz); -int file_exists(const char *filename); time_t ltime(void); void __log(int lvl, char *file, int line, const char *func, char *fmt, ...); void *emalloc(size_t size); FILE *efopen(const char *filename, const char *mode); -size_t uintfmt(char *dst, size_t dsiz, unsigned int num); -size_t longfmt(char *dst, size_t dsiz, const char *opt, long num); +size_t ufmt(char *dst, size_t dsiz, uint64_t n); +size_t ifmt(char *dst, size_t dsiz, int64_t n); +int file_exists(const char *filename); #endif /* UTIL_H */ |