diff --git a/Makefile b/Makefile
index 06c2793..1edf27d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,8 @@
-CFLAGS?= -s -Os -flto -pedantic -Wall
+CFLAGS?=-s -Os -pedantic -Wall -Wextra -Werror -DENABLE_SSL
+LDFLAGS=-lssl -lcrypto
CC?=cc
all:
- $(CC) *.c $(CFLAGS) -obot
+ $(CC) *.c $(CFLAGS) $(LDFLAGS) -obot
clean:
rm irc.* bot
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..8f43e67
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,2 @@
+Irclibs: https://git.macaw.me/8nl/irclibs
+Move all files in irclibs/C to history project dir
diff --git a/cfg.h b/cfg.h
deleted file mode 100644
index ad274eb..0000000
--- a/cfg.h
+++ /dev/null
@@ -1,27 +0,0 @@
-//Settings
-#define NICK "histbot"
-#define HOST "192.168.0.184" //localhost"
-#define PORT 6667
-#define DIR "./"
-
-struct CHANNEL {
- char *channel;
- char *key;
-};
-
-struct CHANNEL channels[] = {
- {"#hp", NULL}
-};
-
-//Put \r\n after string end
-//Exp: {..., "PRIVMSG NickServ IDENTIFY mypassword\r\n", ..., NULL};
-char *send_after_join[] = {NULL};
-
-//Output
-#define FORMAT_TXT "<%s> %s\n"
-#define FORMAT_HTML "[%s] %s
\n"
-#define FORMAT_AWK "%s %s\n"
-#define FORMAT FORMAT_TXT
-
-//File ext
-#define EXT ".txt"
diff --git a/main.c b/main.c
index b2e394d..9d2836b 100644
--- a/main.c
+++ b/main.c
@@ -1,34 +1,42 @@
#include
#include
#include
+#include
#include
#include
#include "irc.h"
-#include "cfg.h"
IRCC_client client;
+char *dir = "./";
+char *ext = ".txt";
+char *fmt = "[%s] %s";
+char *host = "127.0.0.1";
+char *nick = "historybot";
+int port = 6667;
+int usessl = 0;
-void die(char *msg) {
- puts(msg);
+void die(const char *msg) {
+ fprintf(stderr, "history: %s\n", msg);
IRCC_close(&client);
exit(1);
}
-struct tm GetTime(void) {
- time_t t = time(NULL);
- return *localtime(&t);
+void sig_handler(int sig) {
+ fprintf(stderr, "history: recived signal: %d\n", sig);
+ IRCC_close(&client);
+ exit(0);
}
char *GetFilename(void) {
- //Get filename + date
- struct tm tm = GetTime();
+ time_t t = time(NULL);
+ struct tm tm = *localtime(&t);
- size_t filename_size = strlen(client.irc_channel + 1) + (sizeof(tm.tm_year) * 3) + strlen(EXT) + 3;
+ size_t filename_size = strlen(client.irc_channel) + (sizeof(tm.tm_year) * 3) + strlen(ext) + 3;
char *filename = malloc(filename_size + 1);
if (filename == NULL)
- die("malloc returned NULL");
+ die("malloc failed");
- snprintf(filename, filename_size, "%s_%d-%d-%d%s", client.irc_channel + 1, tm.tm_mday, tm.tm_mon, tm.tm_year, EXT);
+ snprintf(filename, filename_size, "%s_%d-%d-%d%s", client.irc_channel + 1, tm.tm_mday, tm.tm_mon, tm.tm_year, ext);
return filename;
}
@@ -41,19 +49,97 @@ void WriteToFile(void) {
free(filename);
if (fp == NULL) {
- printf("Cant open file %s\n", client.irc_channel);
+ fprintf(stderr, "history: cant open file: %s\n", client.irc_channel);
return;
}
- fprintf(fp, FORMAT, client.irc_nick, client.irc_msg);
+ fprintf(fp, fmt, client.irc_nick, client.irc_msg);
+ fprintf(fp, "\n");
fclose(fp);
}
-void recvinfo(void) {
+int main(int argc, char **argv) {
+ signal(SIGTERM, sig_handler);
+ signal(SIGKILL, sig_handler);
+
+ int opt;
+ while ((opt = getopt(argc, argv, "h:p:d:n:f:e:t")) != -1) {
+ switch (opt) {
+ case 'h':
+ host = optarg;
+ break;
+
+ case 'p':
+ port = atoi(optarg);
+ break;
+
+ case 'd':
+ dir = optarg;
+ break;
+
+ case 'n':
+ nick = optarg;
+ break;
+
+ case 'e':
+ ext = optarg;
+ break;
+
+ case 'f':
+ fmt = optarg;
+ break;
+
+ case 't':
+ usessl = 1;
+ break;
+
+ default:
+ printf("history [hpdneft] [\"#channel,key\"]\n\t-t Use ssl\n\t-h HOST Default: %s\n\t-p PORT Default: %d\n\t-d DIR Log dir Default: %s\n\t-n NICK Default: %s\n\t-e EXT File extantion Default: %s\n\t-f FMT Text format Default: %s\n", host, port, dir, nick, ext, fmt);
+ return 0;
+ }
+ }
+
+ argv += optind;
+ argc -= optind;
+
+ if (chdir(dir))
+ die("Cant to change directory");
+
+ int status = IRCC_connect(&client, host, port);
+ if (status == IRCC_ERROR)
+ die("connection refused");
+
+ if (usessl) {
+ int irc_errno = 0;
+ if (IRCC_initssl(&client, &irc_errno) == IRCC_ERROR)
+ die(IRCC_errno[irc_errno]);
+ }
+
+ //Register and skip motd
+ if (IRCC_register(&client, nick) == IRCC_DISCONNECTED)
+ die("disconnected");
+
+ sleep(5);
+
+ //Join in 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(&client, channel, key);
+ }
+
+ //Recv
while (1) {
int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED)
- die("Disconnected");
+ die("disconnected");
irc_status = IRCC_parse(&client);
if (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
@@ -61,31 +147,3 @@ void recvinfo(void) {
}
}
-int main(void) {
- if (chdir(DIR))
- die("Cant chdir");
-
- //512 - size of raw buffer (max irc)
- IRCC_init(&client);
- int status = IRCC_connect(&client, HOST, PORT);
- if (status == IRCC_ERROR)
- die("Conn refused");
-
- //Register
- IRCC_register(&client, NICK);
-
- //Recv motd and join in channel
- sleep(5);
- for (size_t i = 0; i < sizeof(channels) / sizeof(struct CHANNEL); i++)
- IRCC_join(&client, channels[i].channel, channels[i].key);
-
- for (size_t i = 0; i < sizeof(send_after_join) / sizeof(char *); i++) {
- if (send_after_join[i] == NULL)
- break;
-
- send(client.irc_socket, send_after_join[i], strlen(send_after_join[i]), 0);
- }
-
- recvinfo();
-}
-