This commit is contained in:
Your Name 2024-02-24 10:36:22 +03:00
parent aa28da18fe
commit 6a7a61d6ae
4 changed files with 106 additions and 72 deletions

View File

@ -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

2
README.txt Normal file
View File

@ -0,0 +1,2 @@
Irclibs: https://git.macaw.me/8nl/irclibs
Move all files in irclibs/C to history project dir

27
cfg.h
View File

@ -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<br>\n"
#define FORMAT_AWK "%s %s\n"
#define FORMAT FORMAT_TXT
//File ext
#define EXT ".txt"

144
main.c
View File

@ -1,34 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <time.h>
#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();
}