173 lines
3.5 KiB
C
173 lines
3.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
#include <errno.h>
|
|
#include <time.h>
|
|
#include "irc.h"
|
|
|
|
#ifdef ENABLE_SSL
|
|
#include <openssl/err.h>
|
|
#endif
|
|
|
|
#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
|
|
};
|
|
|
|
void die(const char *msg) {
|
|
fprintf(stderr, "history: %s: %s\n", msg, strerror(errno));
|
|
IRCC_close(&ircbot);
|
|
exit(1);
|
|
}
|
|
|
|
void sig_handler(int sig) {
|
|
fprintf(stderr, "history: received signal: %d\n", sig);
|
|
IRCC_close(&ircbot);
|
|
exit(0);
|
|
}
|
|
|
|
char *GetFilename(void) {
|
|
time_t t = time(NULL);
|
|
struct tm *tm = localtime(&t);
|
|
if (tm == NULL)
|
|
die("localtime()");
|
|
|
|
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);
|
|
return filename;
|
|
}
|
|
|
|
void WriteToFile(void) {
|
|
/* Secret message */
|
|
if (ircbot.irc_msg[0] == '!')
|
|
return;
|
|
|
|
FILE *fp = fopen(GetFilename(), "a");
|
|
if (fp == NULL) {
|
|
fprintf(stderr, "history: cant open file: %s\n", ircbot.irc_channel);
|
|
return;
|
|
}
|
|
|
|
fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg);
|
|
fprintf(fp, "\n");
|
|
fclose(fp);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
signal(SIGTERM, sig_handler);
|
|
signal(SIGKILL, sig_handler);
|
|
signal(SIGINT, sig_handler);
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "h:p:d:n:f:e:tD")) != -1) {
|
|
switch (opt) {
|
|
case 'h':
|
|
cfg.host = optarg;
|
|
break;
|
|
|
|
case 'p':
|
|
cfg.port = atoi(optarg);
|
|
break;
|
|
|
|
case 'd':
|
|
cfg.dir = optarg;
|
|
break;
|
|
|
|
case 'n':
|
|
cfg.nick = optarg;
|
|
break;
|
|
|
|
case 'e':
|
|
cfg.ext = optarg;
|
|
break;
|
|
|
|
case 'f':
|
|
cfg.fmt = optarg;
|
|
break;
|
|
|
|
case 't':
|
|
cfg.usessl = 1;
|
|
break;
|
|
|
|
case 'D':
|
|
cfg.debug = 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");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
if (chdir(cfg.dir))
|
|
die("chdir()");
|
|
|
|
int status = IRCC_connect(&ircbot, cfg.host, cfg.port);
|
|
if (status == IRCC_ERROR)
|
|
die("connect()");
|
|
|
|
if (cfg.usessl) {
|
|
#ifdef ENABLE_SSL
|
|
if (IRCC_initssl(&ircbot) == IRCC_ERROR)
|
|
die(ERR_error_string(ERR_get_error(), NULL));
|
|
#endif
|
|
}
|
|
|
|
/* Register and skip MOTD */
|
|
if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED)
|
|
die("irc register");
|
|
|
|
sleep(5);
|
|
|
|
/* Join a channel */
|
|
for (int i = 0; i < argc; i++) {
|
|
char *key = "";
|
|
char *channel = argv[i];
|
|
|
|
char *p = strchr(channel, ':');
|
|
if (p != NULL) {
|
|
p[0] = '\0';
|
|
key = p + 1;
|
|
}
|
|
|
|
IRCC_join(&ircbot, channel, key);
|
|
}
|
|
|
|
/* Logging */
|
|
while (1) {
|
|
int irc_status = IRCC_recv(&ircbot);
|
|
if (irc_status == IRCC_DISCONNECTED)
|
|
die("irc recv");
|
|
|
|
if (cfg.debug)
|
|
printf("\033[32m%s\033[0m", ircbot.irc_raw);
|
|
|
|
irc_status = IRCC_parse(&ircbot);
|
|
if (ircbot.irc_nick != NULL && ircbot.irc_channel != NULL && ircbot.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
|
|
WriteToFile();
|
|
}
|
|
}
|