Major rework on notifiers structure

This commit is contained in:
Davidson Francis 2024-07-28 18:00:36 -03:00
parent 34963499c3
commit 2fb5a9fa4e
4 changed files with 72 additions and 78 deletions

View File

@ -241,6 +241,7 @@ static int handle_regex(struct log_event *ev, int idx_env)
char time_str[32] = {0};
regmatch_t pmatch[MAX_MATCHES] = {0};
struct str_ab notif_message;
struct notifier *self;
int ret;
int notif_idx;
@ -248,6 +249,7 @@ static int handle_regex(struct log_event *ev, int idx_env)
env_ev = &env_events[idx_env];
notif_idx = env_ev->ev_notifier_idx;
self = &notifiers[notif_idx];
if (regexec(&env_ev->regex, ev->msg, MAX_MATCHES, pmatch, 0) == REG_NOMATCH)
return 0;
@ -286,7 +288,7 @@ static int handle_regex(struct log_event *ev, int idx_env)
return 0;
}
if (notifiers[notif_idx].send_notification(notif_message.buff) < 0) {
if (self->send_notification(self, notif_message.buff) < 0) {
log_msg("unable to send the notification through %s\n",
notifiers_str[notif_idx]);
}
@ -307,11 +309,14 @@ static int handle_substr(struct log_event *ev, int idx_env)
int ret;
int notif_idx;
char time_str[32] = {0};
struct notifier *self;
struct env_event *env_ev;
struct str_ab notif_message;
env_ev = &env_events[idx_env];
env_ev = &env_events[idx_env];
notif_idx = env_ev->ev_notifier_idx;
self = &notifiers[notif_idx];
if (!strstr(ev->msg, env_ev->ev_match_str))
return 0;
@ -332,7 +337,7 @@ static int handle_substr(struct log_event *ev, int idx_env)
if (ret)
return 0;
if (notifiers[notif_idx].send_notification(notif_message.buff) < 0) {
if (self->send_notification(self, notif_message.buff) < 0) {
log_msg("unable to send the notification through %s\n",
notifiers_str[notif_idx]);
}
@ -372,6 +377,7 @@ int process_environment_event(struct log_event *ev)
*/
int init_environment_events(void)
{
struct notifier *self;
char *tmp;
tmp = getenv("ENV_EVENTS");
@ -412,7 +418,8 @@ int init_environment_events(void)
);
/* Try to setup notifier if not yet. */
notifiers[env_events[i].ev_notifier_idx].setup();
self = &notifiers[env_events[i].ev_notifier_idx];
self->setup(self);
/* If regex, compile it first. */
if (env_events[i].ev_match_type == EVNT_REGEX) {

View File

@ -121,6 +121,7 @@ int process_static_event(struct log_event *ev)
*/
int init_static_events(void)
{
struct notifier *self;
char *ptr, *end;
long ev;
@ -172,7 +173,8 @@ int init_static_events(void)
);
/* Try to setup notifier if not yet. */
notifiers[static_events[i].ev_notifier_idx].setup();
self = &notifiers[static_events[i].ev_notifier_idx];
self->setup(self);
/* If regex, compile it first. */
if (static_events[i].ev_match_type == EVNT_REGEX) {
@ -254,6 +256,7 @@ static void handle_wifi_login_attempts(struct log_event *ev, int idx_env)
char mac_addr[32] = {0};
char wifi_iface[32] = {0};
struct str_ab notif_message;
struct notifier *self;
int notif_idx;
int ret;
@ -279,7 +282,9 @@ static void handle_wifi_login_attempts(struct log_event *ev, int idx_env)
log_msg("> Retrieved info, MAC: (%s), Interface: (%s)\n", mac_addr, wifi_iface);
notif_idx = static_events[idx_env].ev_notifier_idx;
if (notifiers[notif_idx].send_notification(notif_message.buff) < 0) {
self = &notifiers[notif_idx];
if (self->send_notification(self, notif_message.buff) < 0) {
log_msg("unable to send the notification!\n");
return;
}

View File

@ -17,6 +17,11 @@
* Notification handling/notifiers
*/
struct webhook_data {
char *webhook_url;
const char *env_var;
};
/* EPOCH in secs of last sent notification. */
static time_t time_last_sent_notify;
@ -220,12 +225,14 @@ static int send_generic_webhook(const char *url, const char *text)
static char *telegram_bot_token;
static char *telegram_chat_id;
void setup_telegram(void)
void setup_telegram(struct notifier *self)
{
static int setup = 0;
if (setup)
return;
((void)self);
telegram_bot_token = getenv("TELEGRAM_BOT_TOKEN");
telegram_chat_id = getenv("TELEGRAM_CHAT_ID");
if (!telegram_bot_token || !telegram_chat_id) {
@ -239,13 +246,15 @@ void setup_telegram(void)
setup = 1;
}
static int send_telegram_notification(const char *msg)
static int send_telegram_notification(const struct notifier *self, const char *msg)
{
struct str_ab full_request_url;
char *escaped_msg = NULL;
CURL *hnd = NULL;
int ret;
((void)self);
if (!(hnd = curl_easy_init())) {
log_msg("Failed to initialize libcurl!\n");
return -1;
@ -272,81 +281,42 @@ static int send_telegram_notification(const char *msg)
}
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////// SLACK //////////////////////////////////////
///////////////////////////////// GENERIC /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* Slack settings. */
static char *slack_webhook_url;
void setup_slack(void)
void setup_generic_webhook(struct notifier *self)
{
static int setup = 0;
if (setup)
struct webhook_data *data = self->data;
if (data->webhook_url)
return;
slack_webhook_url = getenv("SLACK_WEBHOOK_URL");
if (!slack_webhook_url) {
panic("Unable to find env vars for Slack, please check if you have set "
"the SLACK_WEBHOOK_URL!!\n");
data->webhook_url = getenv(data->env_var);
if (!data->webhook_url) {
panic("Unable to find env vars, please check if you have set the %s!!\n",
data->env_var);
}
setup = 1;
}
static int send_slack_notification(const char *msg) {
return send_generic_webhook(slack_webhook_url, msg);
}
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////// TEAMS //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* Teams settings. */
static char *teams_webhook_url;
void setup_teams(void)
static int send_generic_webhook_notification(
const struct notifier *self, const char *msg)
{
static int setup = 0;
if (setup)
return;
teams_webhook_url = getenv("TEAMS_WEBHOOK_URL");
if (!teams_webhook_url) {
panic("Unable to find env vars for Teams, please check if you have set "
"the TEAMS_WEBHOOK_URL!!\n");
}
setup = 1;
}
static int send_teams_notification(const char *msg) {
return send_generic_webhook(teams_webhook_url, msg);
struct webhook_data *data = self->data;
return send_generic_webhook(data->webhook_url, msg);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////// DISCORD /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* Discord settings. */
static char *discord_webhook_url;
void setup_discord(void)
{
static int setup = 0;
if (setup)
return;
discord_webhook_url = getenv("DISCORD_WEBHOOK_URL");
if (!discord_webhook_url) {
panic("Unable to find env vars for Discord, please check if you have set "
"the DISCORD_WEBHOOK_URL!!\n");
}
setup = 1;
}
/* Discord in Slack-compatible mode. */
static int send_discord_notification(const char *msg) {
static int send_discord_notification(
const struct notifier *self, const char *msg)
{
struct webhook_data *data = self->data;
struct str_ab url;
ab_init(&url);
if (ab_append_fmt("%s/slack", discord_webhook_url) < 0)
if (ab_append_fmt(&url, "%s/slack", data->webhook_url) < 0)
return 1;
return send_generic_webhook(url.buff, msg);
}
@ -362,22 +332,33 @@ const char *const notifiers_str[] = {
struct notifier notifiers[] = {
/* Telegram. */
{
.setup = setup_telegram,
.send_notification = send_telegram_notification
.setup = setup_telegram,
.send_notification = send_telegram_notification,
},
/* Slack. */
{
.setup = setup_slack,
.send_notification = send_slack_notification
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "SLACK_WEBHOOK_URL"},
},
/* Teams. */
{
.setup = setup_teams,
.send_notification = send_teams_notification
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "TEAMS_WEBHOOK_URL"},
},
/* Discord. */
/*
* Discord:
* Since Discord doesn't follow like the others, we need
* to slightly change the URL before proceeding, so this
* is why its function is not generic!.
*/
{
.setup = setup_discord,
.send_notification = send_discord_notification
.setup = setup_generic_webhook,
.send_notification = send_discord_notification,
.data = &(struct webhook_data)
{.env_var = "DISCORD_WEBHOOK_URL"},
}
};

View File

@ -17,8 +17,8 @@
#define NUM_NOTIFIERS 8
#define NOTIFY_IDX_TELE 0
#define NOTIFY_IDX_SLACK 1
#define NOTIFY_IDX_DISCORD 2
#define NOTIFY_IDX_TEAMS 3
#define NOTIFY_IDX_TEAMS 2
#define NOTIFY_IDX_DISCORD 3
#define NOTIFY_IDX_GENRC1 (NUM_NOTIFIERS-4)
#define NOTIFY_IDX_GENRC2 (NUM_NOTIFIERS-3)
#define NOTIFY_IDX_GENRC3 (NUM_NOTIFIERS-2)
@ -40,8 +40,9 @@
/* Notifier struct. */
struct notifier {
void(*setup)(void);
int(*send_notification)(const char *msg);
void *data;
void(*setup)(struct notifier *self);
int(*send_notification)(const struct notifier *self, const char *msg);
};
extern struct notifier notifiers[NUM_NOTIFIERS];