aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRatakor <ratakor@disroot.org>2023-06-02 03:26:24 +0200
committerRatakor <ratakor@disroot.org>2023-06-02 03:26:24 +0200
commit4da40e77cd8073617dbe1c89a3387d75010e7f2d (patch)
treefa582c253785b6af317fd382d50add084222464e
parenta410143fd63cd6b3b2a1f87403d3b42556b9b683 (diff)
Make lbraid and uraid saferv0.0.6
-rw-r--r--README.md14
-rw-r--r--src/cmd_help.c7
-rw-r--r--src/cmd_lbraid.c22
-rw-r--r--src/cmd_uraid.c51
-rw-r--r--src/nolan.h1
-rw-r--r--src/raids.c4
-rw-r--r--src/stats.c2
7 files changed, 50 insertions, 51 deletions
diff --git a/README.md b/README.md
index ecd65a5..c1818a2 100644
--- a/README.md
+++ b/README.md
@@ -6,17 +6,19 @@ A discord bot for Orna.
- [tesseract](https://github.com/tesseract-ocr/tesseract)
- [gd](https://github.com/libgd/libgd)
-## TODO:
+## TODO
-- refactor cmd_lbraid and cmd_uraid
-- refactor cropping in ocr.c
+### features
- automatic roles attribution (for Orna FR) -> with updatemsg
- add player with name in event->content for ham
-- redefine weird DISCORD_MAX macro (/4 because no wide char)
-
- add option to correct stats
- ascensions (track mats)
-- better wrong image input detection
+### chore
+- redefine weird DISCORD_MAX macro (/4 because no wide char)
- bug with updatemsg ?
+- refactor cropping in ocr.c
- refactor parseline() in stats.c
+
+### misc
+- better wrong image input detection
diff --git a/src/cmd_help.c b/src/cmd_help.c
index 1fbef64..5328692 100644
--- a/src/cmd_help.c
+++ b/src/cmd_help.c
@@ -17,7 +17,8 @@ static void
help(char *buf, size_t siz)
{
char *p;
- int i, rsiz, len = LENGTH(stats_ids);
+ size_t rsiz;
+ int i, len = LENGTH(stats_ids);
strlcpy(buf, "Post a screenshot of your stats to ", siz);
for (i = 0; i < len; i++) {
@@ -48,7 +49,7 @@ void
on_help(struct discord * client, const struct discord_message * event)
{
size_t siz = DISCORD_MAX_MESSAGE_LEN;
- char buf[DISCORD_MAX_MESSAGE_LEN];
+ char buf[siz];
#ifdef DEVEL
if (event->channel_id != DEVEL)
@@ -67,7 +68,7 @@ on_help_interaction(struct discord *client,
const struct discord_interaction *event)
{
size_t siz = DISCORD_MAX_MESSAGE_LEN;
- char buf[DISCORD_MAX_MESSAGE_LEN];
+ char buf[siz];
help(buf, siz);
struct discord_interaction_response params = {
diff --git a/src/cmd_lbraid.c b/src/cmd_lbraid.c
index 29f1399..b373b2e 100644
--- a/src/cmd_lbraid.c
+++ b/src/cmd_lbraid.c
@@ -1,23 +1,20 @@
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include "nolan.h"
-#define MAX_SLAYERS 60 /* different from raids.c in case a lot joined */
-
static void parse_file(char *fname, Slayer slayers[], int *nslayers);
static void load_files(Slayer slayers[], int *nslayers);
static int compare(const void *s1, const void *s2);
-static void write_lbraid(char *buf, size_t siz, Slayer slayers[], int nslayers);
+static void write_lbraid(char *buf, int siz, Slayer slayers[], int nslayers);
static void lbraid(char *buf, size_t siz);
-/* FIXME rsiz >= siz with strncpy + clean */
-
void
create_slash_lbraid(struct discord *client)
{
struct discord_create_global_application_command cmd = {
.name = "lbraid",
- .description = "Shows the weekly raid leaderboard",
+ .description = "Shows the raid leaderboard for the last 7 days",
};
discord_create_global_application_command(client, APP_ID, &cmd, NULL);
}
@@ -59,7 +56,7 @@ load_files(Slayer slayers[], int *nslayers)
{
int i;
long day = time(NULL) / 86400;
- char fname[64];
+ char fname[128];
for (i = 0; i < 6; i++) {
snprintf(fname, sizeof(fname), "%s%ld.csv",
@@ -79,15 +76,16 @@ compare(const void *s1, const void *s2)
return dmg2 - dmg1;
}
-/* FIXME: unsafe */
static void
-write_lbraid(char *buf, size_t siz, Slayer slayers[], int nslayers)
+write_lbraid(char *buf, int siz, Slayer slayers[], int nslayers)
{
int i, lb_max = MIN(nslayers, LB_MAX);
char *p = buf;
for (i = 0; i < lb_max; i++) {
- sprintf(p, "%d. %s: %'lu damage\n", i, slayers[i].name,
- slayers[i].damage);
+ siz -= snprintf(p, siz, "%d. %s: %'lu damage\n", i,
+ slayers[i].name, slayers[i].damage);
+ if (siz <= 0)
+ warn("nolan: truncation in write_lbraid\n");
p = strchr(buf, '\0');
}
}
@@ -135,7 +133,7 @@ on_lbraid_interaction(struct discord *client,
const struct discord_interaction *event)
{
size_t siz = DISCORD_MAX_MESSAGE_LEN;
- char buf[DISCORD_MAX_MESSAGE_LEN];
+ char buf[siz];
lbraid(buf, siz);
struct discord_interaction_response params = {
diff --git a/src/cmd_uraid.c b/src/cmd_uraid.c
index df1a7ea..431c4b7 100644
--- a/src/cmd_uraid.c
+++ b/src/cmd_uraid.c
@@ -1,14 +1,11 @@
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include "nolan.h"
-#define MAX_SLAYERS 60 /* different from raids.c in case a lot joined */
-
-/* FIXME rsiz >= siz with strncpy + clean */
-
static void write_invalid(char *buf, size_t siz);
static unsigned long parse_file(char *fname, char *username);
-static void write_uraid(char *buf, size_t siz, char *username,
+static void write_uraid(char *buf, int siz, char *username,
unsigned long *dmgs);
static unsigned long *load_files(char *username);
static void uraid(char *buf, size_t siz, char *username);
@@ -26,7 +23,7 @@ create_slash_uraid(struct discord *client)
};
struct discord_create_global_application_command cmd = {
.name = "uraid",
- .description = "Shows the past week damage for a user",
+ .description = "Shows the user raid damage for the last 7 days",
.options = &(struct discord_application_command_options)
{
.size = LENGTH(options),
@@ -40,6 +37,7 @@ static void
write_invalid(char *buf, size_t siz)
{
strlcpy(buf, "NO WRONG YOU MUST USE AN ARGUMENT.\n", siz);
+ strlcat(buf, "Valid argument is an Orna username.\n", siz);
}
static unsigned long
@@ -67,32 +65,13 @@ parse_file(char *fname, char *username)
return 0;
}
-/* FIXME: unsafe */
-static void
-write_uraid(char *buf, size_t siz, char *username, unsigned long *dmgs)
-{
- int i, n = 1;
- char *p;
- unsigned long total = 0;
-
- snprintf(buf, siz, "%s raids stats for last week\n", username);
- p = strchr(buf, '\0');
- for (i = 6; i >= 0; i--) {
- sprintf(p, "day %d: %'lu damage\n", n++, dmgs[i]);
- total += dmgs[i];
- p = strchr(buf, '\0');
- }
- sprintf(p, "\ntotal: %'lu damage\n", total);
-
-}
-
static unsigned long *
load_files(char *username)
{
int i;
unsigned long *dmgs = calloc(7, sizeof(unsigned long));
long day = time(NULL) / 86400;
- char fname[64];
+ char fname[128];
for (i = 0; i < 6; i++) {
snprintf(fname, sizeof(fname), "%s%ld.csv",
@@ -106,6 +85,26 @@ load_files(char *username)
}
static void
+write_uraid(char *buf, int siz, char *username, unsigned long *dmgs)
+{
+ int i, n = 1;
+ char *p;
+ unsigned long total = 0;
+
+ siz -= snprintf(buf, siz, "%s raids stats for last 7 days\n", username);
+ p = strchr(buf, '\0');
+ for (i = 6; i >= 0; i--) {
+ siz -= snprintf(p, siz, "day %d: %'lu damage\n", n++, dmgs[i]);
+ if (siz <= 0)
+ warn("nolan: truncation in write_uraid\n");
+ total += dmgs[i];
+ p = strchr(buf, '\0');
+ }
+ snprintf(p, siz, "\ntotal: %'lu damage\n", total);
+
+}
+
+static void
uraid(char *buf, size_t siz, char *username)
{
unsigned long *dmgs;
diff --git a/src/nolan.h b/src/nolan.h
index e468336..7716345 100644
--- a/src/nolan.h
+++ b/src/nolan.h
@@ -4,6 +4,7 @@
#include "util.h"
#define MAX_PLAYERS LENGTH(kingdoms) * 50
+#define MAX_SLAYERS 50
#define LINE_SIZE 300 + 1
#ifdef DEVEL
#define SAVE_FOLDER "./"
diff --git a/src/raids.c b/src/raids.c
index cdc614c..0013545 100644
--- a/src/raids.c
+++ b/src/raids.c
@@ -3,7 +3,6 @@
#include <time.h>
#include "nolan.h"
-#define MAX_SLAYERS 50
#define DAMAGE_CAP 300000000 / 7 /* daily */
static void emsg(struct discord *client, const struct discord_message *event);
@@ -205,8 +204,7 @@ overcap_msg(char *name, unsigned long dmg, struct discord *client,
return;
snprintf(buf, sizeof(buf), "%s has overcapped the limit by %'lu \
-damage he is now at %'lu damage.", name, dmg - DAMAGE_CAP,
- dmg);
+damage he is now at %'lu damage.", name, dmg - DAMAGE_CAP, dmg);
struct discord_create_message msg = {
.content = buf
diff --git a/src/stats.c b/src/stats.c
index 6e9bae6..acf033f 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -305,7 +305,7 @@ save_player_to_file(Player *player)
char *
update_msg(Player *player, int iplayer)
{
- size_t sz = DISCORD_MAX_MESSAGE_LEN;
+ int sz = DISCORD_MAX_MESSAGE_LEN;
char *buf = malloc(sz), *p, *plto, *pltn, *pltd;
unsigned long i;
long old, new, diff;