2.0
This commit is contained in:
parent
570bff605f
commit
2dab4affc4
1 changed files with 69 additions and 22 deletions
87
main.c
87
main.c
|
@ -3,16 +3,19 @@
|
|||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include "irc.h"
|
||||
|
||||
#ifdef ENABLE_SSL
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/err.h>
|
||||
#endif
|
||||
|
||||
#define bool char
|
||||
#define VERSION "2.0"
|
||||
|
||||
typedef struct {
|
||||
char *dir;
|
||||
char *ext;
|
||||
|
@ -24,24 +27,30 @@ typedef struct {
|
|||
bool debug;
|
||||
} CFG;
|
||||
|
||||
char *prog_name;
|
||||
IRCC_client ircbot;
|
||||
static CFG cfg = {
|
||||
.dir = "./",
|
||||
.ext = ".ext",
|
||||
.ext = ".txt",
|
||||
.fmt = "[%s] %s",
|
||||
.host = "127.0.0.1",
|
||||
.nick = "historybot",
|
||||
.port = 6667
|
||||
};
|
||||
|
||||
void die(const char *msg) {
|
||||
fprintf(stderr, "history: %s: %s\n", msg, strerror(errno));
|
||||
void die(const char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
IRCC_close(&ircbot);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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);
|
||||
exit(0);
|
||||
}
|
||||
|
@ -50,7 +59,7 @@ char *GetFilename(void) {
|
|||
time_t t = time(NULL);
|
||||
struct tm *tm = localtime(&t);
|
||||
if (tm == NULL)
|
||||
die("localtime()");
|
||||
die("%s: localtime(): %s\n", prog_name, strerror(errno));
|
||||
|
||||
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);
|
||||
|
@ -62,24 +71,24 @@ void WriteToFile(void) {
|
|||
if (ircbot.irc_msg[0] == '!')
|
||||
return;
|
||||
|
||||
FILE *fp = fopen(GetFilename(), "a");
|
||||
char *filename = GetFilename();
|
||||
FILE *fp = fopen(filename, "a");
|
||||
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;
|
||||
}
|
||||
|
||||
fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg);
|
||||
fprintf(fp, "\n");
|
||||
fputs("\n", fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
signal(SIGTERM, sig_handler);
|
||||
signal(SIGKILL, sig_handler);
|
||||
signal(SIGINT, sig_handler);
|
||||
prog_name = argv[0];
|
||||
bool daemonize = 0;
|
||||
|
||||
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) {
|
||||
case 'h':
|
||||
cfg.host = optarg;
|
||||
|
@ -109,36 +118,74 @@ int main(int argc, char **argv) {
|
|||
cfg.usessl = 1;
|
||||
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':
|
||||
cfg.debug = 1;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
daemonize = 1;
|
||||
break;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
argc -= optind;
|
||||
|
||||
if (chdir(cfg.dir))
|
||||
die("chdir()");
|
||||
die("%s: chdir(): %s\n", prog_name, strerror(errno));
|
||||
|
||||
int status = IRCC_connect(&ircbot, cfg.host, cfg.port);
|
||||
if (status == IRCC_ERROR)
|
||||
die("connect()");
|
||||
die("%s: connect(): %s\n", prog_name, strerror(errno));
|
||||
|
||||
if (cfg.usessl) {
|
||||
#ifdef ENABLE_SSL
|
||||
#if defined(ENABLE_SSL) || defined(ENABLE_TLS)
|
||||
if (IRCC_initssl(&ircbot) == IRCC_ERROR)
|
||||
die(ERR_error_string(ERR_get_error(), NULL));
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Register and skip MOTD */
|
||||
if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED)
|
||||
die("irc register");
|
||||
die("%s: irc register: %s\n", prog_name, strerror(errno));
|
||||
|
||||
sleep(5);
|
||||
|
||||
|
@ -160,7 +207,7 @@ int main(int argc, char **argv) {
|
|||
while (1) {
|
||||
int irc_status = IRCC_recv(&ircbot);
|
||||
if (irc_status == IRCC_DISCONNECTED)
|
||||
die("irc recv");
|
||||
die("%s: irc recv: %s\n", prog_name, strerror(errno));
|
||||
|
||||
if (cfg.debug)
|
||||
printf("\033[32m%s\033[0m", ircbot.irc_raw);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue