Загрузить файлы в «»

This commit is contained in:
8nlight 2023-08-01 22:42:36 +03:00
parent 7e55d1f422
commit 7a81db868b
3 changed files with 98 additions and 0 deletions

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
CFLAGS?= -s -Os -flto -pedantic
all:
cc *.c $(CFLAGS) -obot
clean:
rm irc.* bot

13
cfg.h Normal file
View File

@ -0,0 +1,13 @@
#define NICK "history"
#define HOST "irc.server.net"
#define PORT 6667
#define DIR "tmp/"
char *channels[] = {"#channel"};
//Output
#define FORMAT_TXT "<%s> %s\n"
#define FORMAT_HTML "[%s] %s<br>\n"
#define FORMAT FORMAT_TXT
#define OUTPUT ".txt"

79
main.c Normal file
View File

@ -0,0 +1,79 @@
#include "irc.h"
#include "cfg.h"
#include <time.h>
IRCC_client client;
void die(char *msg) {
puts(msg);
IRCC_close(&client);
exit(1);
}
struct tm GetTime(void) {
time_t t = time(NULL);
return *localtime(&t);
}
char *GetFilename(void) {
//Get filename + date
struct tm tm = GetTime();
size_t filename_size = strlen(client.channel + 1) + (sizeof(tm.tm_year) * 3) + strlen(OUTPUT) + 3;
char *filename = malloc(filename_size + 1);
if (filename == NULL)
die("malloc returned NULL");
snprintf(filename, filename_size, "%s-%d-%d-%d%s", client.channel + 1, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday, OUTPUT);
return filename;
}
void WriteToFile(void) {
if (client.msg[1] == '.')
return;
char *filename = GetFilename();
FILE *fp = fopen(filename, "a");
free(filename);
if (fp == NULL) {
printf("Cant open file %s\n", client.channel);
return;
}
fprintf(fp, FORMAT, client.nick, client.msg + 1);
fclose(fp);
}
int main(void) {
if (chdir(DIR))
die("Cant chdir");
//512 - size of raw buffer (max)
IRCC_init(&client, 512);
int status = IRCC_connect(&client, HOST, PORT);
if (status == IRCC_ERROR)
die("Conn refused");
//Register and join in channel
IRCC_register(&client, NICK);
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
sleep(1);
size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1;
char *tmp = malloc(join_size);
if (tmp == NULL)
die("malloc retured NULL");
snprintf(tmp, join_size, "JOIN %s\r\n", channels[i]);
send(client.socket, tmp, join_size, 0);
free(tmp);
}
while (1) {
unsigned int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED)
die("Disconnected");
else if (client.nick != NULL && client.channel != NULL && client.msg != NULL && irc_status == IRCC_PRIVMSG)
WriteToFile();
}
}