-
Notifications
You must be signed in to change notification settings - Fork 3
/
notifiers.c
389 lines (341 loc) · 10.4 KB
/
notifiers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Alertik: a tiny 'syslog' server & notification tool for Mikrotik routers.
* This is free and unencumbered software released into the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <curl/curl.h>
#include "log.h"
#include "notifiers.h"
#include "str.h"
/*
* 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;
/* Just to omit the print to stdout. */
size_t libcurl_noop_cb(void *ptr, size_t size, size_t nmemb, void *data) {
((void)ptr);
((void)data);
return size * nmemb;
}
/**
* @brief Just updates the time (Epoch) of the last sent
* notify.
*/
void update_notify_last_sent(void) {
time_last_sent_notify = time(NULL);
}
/**
* @brief Checks if the current time is within or not
* the minimal threshold to send a nofication.
*
* @return Returns 1 if within the range (can send nofications),
* 0 otherwise.
*/
int is_within_notify_threshold(void) {
return (time(NULL) - time_last_sent_notify) > LAST_SENT_THRESHOLD_SECS;
}
/**
* @brief Initializes and configures the CURL handle for sending a request.
*
* @param hnd CURL handle.
* @param url Request URL.
* @return Returns 0.
*/
static int setopts_get_curl(CURL *hnd, const char *url)
{
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, CURL_USER_AGENT);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, libcurl_noop_cb);
#ifdef CURL_VERBOSE
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
#endif
#ifndef VALIDATE_CERTS
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
return 0;
}
/**
* @brief Cleanup all resources used by libcurl, including the handler,
* curl_slist and escape'd chars.
*
* @param hnd curl handler
* @param escape Escape string if any (leave NULL if there's none).
* @param slist String list if any (leave NULL if there's none).
*/
static void do_curl_cleanup(CURL *hnd, char *escape, struct curl_slist *slist)
{
curl_free(escape);
curl_slist_free_all(slist);
if (hnd)
curl_easy_cleanup(hnd);
}
/**
* @brief Finally sends a curl request, check its return code
* and then cleanup the resources allocated.
*
* @param hnd curl handler
* @param escape Escape string if any (leave NULL if there's none).
* @param slist String list if any (leave NULL if there's none).
*
* @return Returns CURLE_OK if success, !CURLE_OK if error.
*/
static CURLcode do_curl(CURL *hnd, char *escape, struct curl_slist *slist)
{
long response_code = 0;
CURLcode ret_curl = !CURLE_OK;
#ifndef DISABLE_NOTIFICATIONS
ret_curl = curl_easy_perform(hnd);
if (ret_curl != CURLE_OK) {
log_msg("> Unable to send request!\n");
goto error;
}
else {
curl_easy_getinfo(hnd, CURLINFO_RESPONSE_CODE, &response_code);
log_msg("> Done!\n", response_code);
if (response_code != 200) {
log_msg("(Info: Response code != 200 (%ld), your message might "
"not be correctly sent!)\n", response_code);
}
}
#endif
ret_curl = CURLE_OK;
error:
do_curl_cleanup(hnd, escape, slist);
return ret_curl;
}
/**
* @brief Initializes and configures the CURL handle for sending a POST
* request with a JSON payload.
*
* @param hnd CURL handle.
* @param url Request URL.
* @param json_payload Payload data in JSON format.
*
* @return Returns 0 if successful, 1 otherwise.
*/
static int setopts_post_json_curl(CURL *hnd, const char *url,
const char *json_payload, struct curl_slist **slist)
{
struct curl_slist *s = *slist;
s = NULL;
s = curl_slist_append(s, "Content-Type: application/json");
s = curl_slist_append(s, "Accept: application/json");
if (!s) {
*slist = s;
return 1;
}
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, s);
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, CURL_USER_AGENT);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, libcurl_noop_cb);
#ifdef CURL_VERBOSE
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
#endif
#ifndef VALIDATE_CERTS
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, json_payload);
*slist = s;
return 0;
}
/**
* @brief Sends a generic webhook POST request with JSON payload in the
* format {"text": "text here"}.
*
* @param url Target webhook URL.
* @param text Text to be sent in the json payload.
*
* @return Returns CURLE_OK if success, 1 if error.
*/
static int send_generic_webhook(const char *url, const char *text)
{
CURL *hnd = NULL;
struct curl_slist *s = NULL;
struct str_ab payload_data;
const char *t;
if (!(hnd = curl_easy_init())) {
log_msg("Failed to initialize libcurl!\n");
return 1;
}
ab_init(&payload_data);
ab_append_str(&payload_data, "{\"text\":\"", 9);
/* Append the payload data text while escaping double
* quotes.
*/
for (t = text; *t != '\0'; t++) {
if (*t != '"') {
if (ab_append_chr(&payload_data, *t) < 0)
return 1;
}
else {
if (ab_append_str(&payload_data, "\\\"", 2) < 0)
return 1;
}
}
/* End the string. */
if (ab_append_str(&payload_data, "\"}", 2) < 0)
return 1;
if (setopts_post_json_curl(hnd, url, payload_data.buff, &s))
return 1;
log_msg("> Sending notification!\n");
return do_curl(hnd, NULL, s);
}
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// TELEGRAM /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* Telegram & request settings. */
static char *telegram_bot_token;
static char *telegram_chat_id;
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) {
panic(
"Unable to find env vars, please check if you have all of the "
"following set:\n"
"- TELEGRAM_BOT_TOKEN\n"
"- TELEGRAM_CHAT_ID\n"
);
}
setup = 1;
}
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;
}
escaped_msg = curl_easy_escape(hnd, msg, 0);
if (!escaped_msg) {
log_msg("> Unable to escape notification message...\n");
do_curl_cleanup(hnd, escaped_msg, NULL);
}
ab_init(&full_request_url);
ret = ab_append_fmt(&full_request_url,
"https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
telegram_bot_token, telegram_chat_id, escaped_msg);
if (ret)
return -1;
setopts_get_curl(hnd, full_request_url.buff);
log_msg("> Sending notification!\n");
return do_curl(hnd, escaped_msg, NULL);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////// GENERIC /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void setup_generic_webhook(struct notifier *self)
{
struct webhook_data *data = self->data;
if (data->webhook_url)
return;
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);
}
}
static int send_generic_webhook_notification(
const struct notifier *self, const char *msg)
{
struct webhook_data *data = self->data;
return send_generic_webhook(data->webhook_url, msg);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////// DISCORD /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* Discord in Slack-compatible mode. */
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(&url, "%s/slack", data->webhook_url) < 0)
return 1;
return send_generic_webhook(url.buff, msg);
}
////////////////////////////////// END ////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
const char *const notifiers_str[] = {
"Telegram", "Slack", "Teams", "Discord",
"Generic1", "Generic2", "Generic3", "Generic4"
};
struct notifier notifiers[] = {
/* Telegram. */
{
.setup = setup_telegram,
.send_notification = send_telegram_notification,
},
/* Slack. */
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "SLACK_WEBHOOK_URL"},
},
/* Teams. */
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "TEAMS_WEBHOOK_URL"},
},
/*
* 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_generic_webhook,
.send_notification = send_discord_notification,
.data = &(struct webhook_data)
{.env_var = "DISCORD_WEBHOOK_URL"},
},
/* Generic webhook events: 1--4 */
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC1_WEBHOOK_URL"},
},
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC2_WEBHOOK_URL"},
},
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC3_WEBHOOK_URL"},
},
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC4_WEBHOOK_URL"},
},
};