This commit is contained in:
Your Name 2025-06-12 22:40:49 +03:00
parent 570bff605f
commit 2dab4affc4

83
main.c
View file

@ -3,6 +3,7 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#include <limits.h> #include <limits.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
@ -13,6 +14,8 @@
#endif #endif
#define bool char #define bool char
#define VERSION "2.0"
typedef struct { typedef struct {
char *dir; char *dir;
char *ext; char *ext;
@ -24,24 +27,30 @@ typedef struct {
bool debug; bool debug;
} CFG; } CFG;
char *prog_name;
IRCC_client ircbot; IRCC_client ircbot;
static CFG cfg = { static CFG cfg = {
.dir = "./", .dir = "./",
.ext = ".ext", .ext = ".txt",
.fmt = "[%s] %s", .fmt = "[%s] %s",
.host = "127.0.0.1", .host = "127.0.0.1",
.nick = "historybot", .nick = "historybot",
.port = 6667 .port = 6667
}; };
void die(const char *msg) { void die(const char *fmt, ...) {
fprintf(stderr, "history: %s: %s\n", msg, strerror(errno)); va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
IRCC_close(&ircbot); IRCC_close(&ircbot);
exit(1); exit(1);
} }
void sig_handler(int sig) { void sig_handler(int sig) {
fprintf(stderr, "history: received signal: %d\n", sig); fprintf(stderr, "%s: received signal: %d\n", prog_name, sig);
IRCC_close(&ircbot); IRCC_close(&ircbot);
exit(0); exit(0);
} }
@ -50,7 +59,7 @@ char *GetFilename(void) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm *tm = localtime(&t); struct tm *tm = localtime(&t);
if (tm == NULL) if (tm == NULL)
die("localtime()"); die("%s: localtime(): %s\n", prog_name, strerror(errno));
static char filename[PATH_MAX + 1]; static char filename[PATH_MAX + 1];
snprintf(filename, PATH_MAX, "%s_%d-%d-%d%s", ircbot.irc_channel, tm->tm_mday, tm->tm_mon, 1900 + tm->tm_year, cfg.ext); snprintf(filename, PATH_MAX, "%s_%d-%d-%d%s", ircbot.irc_channel, tm->tm_mday, tm->tm_mon, 1900 + tm->tm_year, cfg.ext);
@ -62,24 +71,24 @@ void WriteToFile(void) {
if (ircbot.irc_msg[0] == '!') if (ircbot.irc_msg[0] == '!')
return; return;
FILE *fp = fopen(GetFilename(), "a"); char *filename = GetFilename();
FILE *fp = fopen(filename, "a");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "history: cant open file: %s\n", ircbot.irc_channel); fprintf(stderr, "%s: cant open file: %s\n", prog_name, filename);
return; return;
} }
fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg); fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg);
fprintf(fp, "\n"); fputs("\n", fp);
fclose(fp); fclose(fp);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
signal(SIGTERM, sig_handler); prog_name = argv[0];
signal(SIGKILL, sig_handler); bool daemonize = 0;
signal(SIGINT, sig_handler);
int opt; int opt;
while ((opt = getopt(argc, argv, "h:p:d:n:f:e:tD")) != -1) { while ((opt = getopt(argc, argv, "h:p:d:n:f:e:tDVa")) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
cfg.host = optarg; cfg.host = optarg;
@ -109,28 +118,66 @@ int main(int argc, char **argv) {
cfg.usessl = 1; cfg.usessl = 1;
break; break;
case 'V':
printf("%s %s (history) and using irclibs %s\nWritten under WTFPL License\n", prog_name, VERSION, IRCC_VERSION);
return 0;
case 'D': case 'D':
cfg.debug = 1; cfg.debug = 1;
break; break;
case 'a':
daemonize = 1;
break;
default: default:
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"); printf("%s -[h:p:d:n:e:f:tDVa] [\"#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-D Print messages(debug) Default: %s\n\t-V Print version and license\n\t-a Fork and daemonize\n",
prog_name,
cfg.host,
cfg.port,
cfg.dir,
cfg.nick,
cfg.ext,
cfg.fmt,
(cfg.debug) ? "true" : "false");
#if defined(ENABLE_SSL) || defined(ENABLE_TLS)
printf("\t-t Use ssl Default: %s\n", (cfg.usessl) ? "true" : "false");
#endif
return 0; return 0;
} }
} }
if (daemonize) {
pid_t pid = fork();
if (pid < 0)
die("%s: fork: %s\n", prog_name, strerror(errno));
/* Parent */
else if (pid != 0)
return 0;
if (daemon(1, 0) < 0)
die("%s: daemon: %s\n", prog_name, strerror(errno));
}
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGINT, sig_handler);
argv += optind; argv += optind;
argc -= optind; argc -= optind;
if (chdir(cfg.dir)) if (chdir(cfg.dir))
die("chdir()"); die("%s: chdir(): %s\n", prog_name, strerror(errno));
int status = IRCC_connect(&ircbot, cfg.host, cfg.port); int status = IRCC_connect(&ircbot, cfg.host, cfg.port);
if (status == IRCC_ERROR) if (status == IRCC_ERROR)
die("connect()"); die("%s: connect(): %s\n", prog_name, strerror(errno));
if (cfg.usessl) { if (cfg.usessl) {
#ifdef ENABLE_SSL #if defined(ENABLE_SSL) || defined(ENABLE_TLS)
if (IRCC_initssl(&ircbot) == IRCC_ERROR) if (IRCC_initssl(&ircbot) == IRCC_ERROR)
die(ERR_error_string(ERR_get_error(), NULL)); die(ERR_error_string(ERR_get_error(), NULL));
#endif #endif
@ -138,7 +185,7 @@ int main(int argc, char **argv) {
/* Register and skip MOTD */ /* Register and skip MOTD */
if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED) if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED)
die("irc register"); die("%s: irc register: %s\n", prog_name, strerror(errno));
sleep(5); sleep(5);
@ -160,7 +207,7 @@ int main(int argc, char **argv) {
while (1) { while (1) {
int irc_status = IRCC_recv(&ircbot); int irc_status = IRCC_recv(&ircbot);
if (irc_status == IRCC_DISCONNECTED) if (irc_status == IRCC_DISCONNECTED)
die("irc recv"); die("%s: irc recv: %s\n", prog_name, strerror(errno));
if (cfg.debug) if (cfg.debug)
printf("\033[32m%s\033[0m", ircbot.irc_raw); printf("\033[32m%s\033[0m", ircbot.irc_raw);