history/main.c

173 lines
3.5 KiB
C
Raw Normal View History

2024-01-23 17:00:22 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2024-02-24 07:36:22 +00:00
#include <signal.h>
2024-03-04 14:48:15 +00:00
#include <string.h>
2024-08-16 18:23:54 +00:00
#include <limits.h>
#include <errno.h>
2024-01-23 17:00:22 +00:00
#include <time.h>
2023-08-01 19:42:36 +00:00
#include "irc.h"
2024-01-23 17:00:22 +00:00
2024-03-04 14:48:15 +00:00
#ifdef ENABLE_SSL
#include <openssl/err.h>
#endif
2024-08-16 18:23:54 +00:00
#define bool char
typedef struct {
char *dir;
char *ext;
char *fmt;
char *host;
char *nick;
int port;
bool usessl;
bool debug;
} CFG;
IRCC_client ircbot;
static CFG cfg = {
.dir = "./",
.ext = ".ext",
.fmt = "[%s] %s",
.host = "127.0.0.1",
.nick = "historybot",
.port = 6667
};
2024-02-24 07:36:22 +00:00
void die(const char *msg) {
2024-08-16 18:23:54 +00:00
fprintf(stderr, "history: %s: %s\n", msg, strerror(errno));
IRCC_close(&ircbot);
2023-08-01 19:42:36 +00:00
exit(1);
}
2024-02-24 07:36:22 +00:00
void sig_handler(int sig) {
2024-08-16 18:23:54 +00:00
fprintf(stderr, "history: received signal: %d\n", sig);
IRCC_close(&ircbot);
2024-02-24 07:36:22 +00:00
exit(0);
2023-08-01 19:42:36 +00:00
}
char *GetFilename(void) {
2024-02-24 07:36:22 +00:00
time_t t = time(NULL);
2024-08-16 18:23:54 +00:00
struct tm *tm = localtime(&t);
if (tm == NULL)
die("localtime()");
2023-08-01 19:42:36 +00:00
2024-08-16 18:23:54 +00:00
static char filename[PATH_MAX + 1];
snprintf(filename, PATH_MAX, "%s_%d-%d-%d%s", ircbot.irc_channel + 1, tm->tm_mday, tm->tm_mon, 1900 + tm->tm_year, cfg.ext);
2023-08-01 19:42:36 +00:00
return filename;
}
void WriteToFile(void) {
2024-08-16 18:23:54 +00:00
/* Secret message */
if (ircbot.irc_msg[0] == '!')
2023-08-01 19:42:36 +00:00
return;
2024-08-16 18:23:54 +00:00
FILE *fp = fopen(GetFilename(), "a");
2023-08-01 19:42:36 +00:00
if (fp == NULL) {
2024-08-16 18:23:54 +00:00
fprintf(stderr, "history: cant open file: %s\n", ircbot.irc_channel);
2023-08-01 19:42:36 +00:00
return;
}
2024-08-16 18:23:54 +00:00
fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg);
2024-02-24 07:36:22 +00:00
fprintf(fp, "\n");
2023-08-01 19:42:36 +00:00
fclose(fp);
}
2024-02-24 07:36:22 +00:00
int main(int argc, char **argv) {
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
2024-08-16 18:23:54 +00:00
signal(SIGINT, sig_handler);
2024-02-24 07:36:22 +00:00
int opt;
2024-08-16 18:23:54 +00:00
while ((opt = getopt(argc, argv, "h:p:d:n:f:e:tD")) != -1) {
2024-02-24 07:36:22 +00:00
switch (opt) {
case 'h':
2024-08-16 18:23:54 +00:00
cfg.host = optarg;
2024-02-24 07:36:22 +00:00
break;
case 'p':
2024-08-16 18:23:54 +00:00
cfg.port = atoi(optarg);
2024-02-24 07:36:22 +00:00
break;
case 'd':
2024-08-16 18:23:54 +00:00
cfg.dir = optarg;
2024-02-24 07:36:22 +00:00
break;
case 'n':
2024-08-16 18:23:54 +00:00
cfg.nick = optarg;
2024-02-24 07:36:22 +00:00
break;
case 'e':
2024-08-16 18:23:54 +00:00
cfg.ext = optarg;
2024-02-24 07:36:22 +00:00
break;
case 'f':
2024-08-16 18:23:54 +00:00
cfg.fmt = optarg;
2024-02-24 07:36:22 +00:00
break;
case 't':
2024-08-16 18:23:54 +00:00
cfg.usessl = 1;
break;
case 'D':
cfg.debug = 1;
2024-02-24 07:36:22 +00:00
break;
default:
2024-08-16 18:23:54 +00:00
printf("history -[h:p:d:n:e:f:tD] [\"#channel:key\"]\n\t-h STR Server's ip Default: %s\n\t-p INT Server's port Default: %d\n\t-d STR Logging dir Default: %s\n\t-n STR Bot nickname Default: %s\n\t-e STR File extantion Default: %s\n\t-f STR Log format Default: %s\n\t-t Use ssl Default: %s\n\t-D Print messages(debug) Default: %s\n", cfg.host, cfg.port, cfg.dir, cfg.nick, cfg.ext, cfg.fmt, (cfg.usessl) ? "true" : "false", (cfg.debug) ? "true" : "false");
2024-02-24 07:36:22 +00:00
return 0;
}
2023-08-07 15:08:35 +00:00
}
2024-02-24 07:36:22 +00:00
argv += optind;
argc -= optind;
2023-08-01 19:42:36 +00:00
2024-08-16 18:23:54 +00:00
if (chdir(cfg.dir))
die("chdir()");
2024-02-24 07:36:22 +00:00
2024-08-16 18:23:54 +00:00
int status = IRCC_connect(&ircbot, cfg.host, cfg.port);
2023-08-01 19:42:36 +00:00
if (status == IRCC_ERROR)
2024-08-16 18:23:54 +00:00
die("connect()");
2024-02-24 07:36:22 +00:00
2024-08-16 18:23:54 +00:00
if (cfg.usessl) {
2024-03-04 14:48:15 +00:00
#ifdef ENABLE_SSL
2024-08-16 18:23:54 +00:00
if (IRCC_initssl(&ircbot) == IRCC_ERROR)
2024-03-04 14:48:15 +00:00
die(ERR_error_string(ERR_get_error(), NULL));
#endif
2024-02-24 07:36:22 +00:00
}
2023-08-01 19:42:36 +00:00
2024-08-16 18:23:54 +00:00
/* Register and skip MOTD */
if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED)
die("irc register");
2023-08-07 15:08:35 +00:00
sleep(5);
2023-08-01 19:42:36 +00:00
2024-08-16 18:23:54 +00:00
/* Join a channel */
2024-02-24 07:36:22 +00:00
for (int i = 0; i < argc; i++) {
char *key = "";
char *channel = argv[i];
2023-10-09 14:16:02 +00:00
2024-08-16 18:23:54 +00:00
char *p = strchr(channel, ':');
2024-02-24 07:36:22 +00:00
if (p != NULL) {
p[0] = '\0';
key = p + 1;
}
2024-08-16 18:23:54 +00:00
IRCC_join(&ircbot, channel, key);
2023-10-09 14:16:02 +00:00
}
2024-08-16 18:23:54 +00:00
/* Logging */
2024-02-24 07:36:22 +00:00
while (1) {
2024-08-16 18:23:54 +00:00
int irc_status = IRCC_recv(&ircbot);
2024-02-24 07:36:22 +00:00
if (irc_status == IRCC_DISCONNECTED)
2024-08-16 18:23:54 +00:00
die("irc recv");
if (cfg.debug)
printf("\033[32m%s\033[0m", ircbot.irc_raw);
2024-02-24 07:36:22 +00:00
2024-08-16 18:23:54 +00:00
irc_status = IRCC_parse(&ircbot);
if (ircbot.irc_nick != NULL && ircbot.irc_channel != NULL && ircbot.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
2024-02-24 07:36:22 +00:00
WriteToFile();
}
2023-08-01 19:42:36 +00:00
}