From d19099eb3c776a07331e86f4b3f4dd5ff74966b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gomez?= Date: Mon, 3 Jan 2022 09:25:52 +0100 Subject: [PATCH] Log unhandled IRC errors as LOG_INFO in bip.log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will allow for user feedback in main bip.log when an IRC error occurs, like: - 401 ERR_NOSUCHNICK - 404 ERR_CANNOTSENDTOCHAN - 432 ERR_ERRONEUSNICKNAME These should not be logged as LOG_ERROR as they are not bip errors but usually on the end user instead. Signed-off-by: Loïc Gomez --- src/irc.c | 16 ++++++++++++++++ src/line.c | 6 ++++++ src/line.h | 1 + 3 files changed, 23 insertions(+) diff --git a/src/irc.c b/src/irc.c index dd226df..63a602d 100644 --- a/src/irc.c +++ b/src/irc.c @@ -33,6 +33,7 @@ extern int sighup; extern bip_t *_bip; +static int irc_generic_error(struct link_server *server, struct line *line); static int irc_join(struct link_server *server, struct line *line); static int irc_part(struct link_server *server, struct line *line); static int irc_mode(struct link_server *server, struct line *line); @@ -625,6 +626,10 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, ret = irc_quit(server, line); } else if (irc_line_elem_equals(line, 0, "NICK")) { ret = irc_nick(server, line); + } else if (irc_line_is_error(line)) { + // IRC errors catchall (for unhandled ones) + // logs error to bip.log + ret = irc_generic_error(server, line); } if (ret == OK_COPY) { @@ -1448,6 +1453,17 @@ static int origin_is_me(struct line *l, struct link_server *server) return 0; } +static int irc_generic_error(struct link_server *server, struct line *line) +{ + if (irc_line_count(line) == 4) + mylog(LOG_INFO, "[%s] IRC error: %s", LINK(server)->name, + irc_line_elem(line, 3)); + else + mylog(LOG_INFO, "[%s] IRC error: %s", LINK(server)->name, + irc_line_to_string(line)); + return OK_COPY; +} + static int irc_join(struct link_server *server, struct line *line) { char *s_nick; diff --git a/src/line.c b/src/line.c index 34cc19d..49efbca 100644 --- a/src/line.c +++ b/src/line.c @@ -143,6 +143,12 @@ void irc_line_drop(struct line *line, int elem) bip_cfree(array_drop(&line->words, elem)); } +int irc_line_is_error(struct line *line) +{ + const char *irc_code = irc_line_elem(line, 0); + return (irc_code[0] == '4'); +} + int irc_line_elem_equals(struct line *line, int elem, const char *cmp) { return !strcmp(irc_line_elem(line, elem), cmp); diff --git a/src/line.h b/src/line.h index 2965766..253b241 100644 --- a/src/line.h +++ b/src/line.h @@ -98,6 +98,7 @@ int irc_line_includes(struct line *line, int elem); const char *irc_line_elem(struct line *line, int elem); int irc_line_count(struct line *line); char *irc_line_pop(struct line *l); +int irc_line_is_error(struct line *line); int irc_line_elem_equals(struct line *line, int elem, const char *cmp); int irc_line_elem_case_equals(struct line *line, int elem, const char *cmp); void irc_line_drop(struct line *line, int elem);