2024-01-23 17:00:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <time.h>
|
2023-08-01 19:42:36 +00:00
|
|
|
#include "irc.h"
|
|
|
|
#include "cfg.h"
|
2024-01-23 17:00:22 +00:00
|
|
|
|
2023-08-01 19:42:36 +00:00
|
|
|
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();
|
|
|
|
|
2024-01-23 17:00:22 +00:00
|
|
|
size_t filename_size = strlen(client.irc_channel + 1) + (sizeof(tm.tm_year) * 3) + strlen(EXT) + 3;
|
2023-08-01 19:42:36 +00:00
|
|
|
char *filename = malloc(filename_size + 1);
|
|
|
|
if (filename == NULL)
|
|
|
|
die("malloc returned NULL");
|
|
|
|
|
2024-01-23 17:00:22 +00:00
|
|
|
snprintf(filename, filename_size, "%s_%d-%d-%d%s", client.irc_channel + 1, tm.tm_mday, tm.tm_mon, tm.tm_year, EXT);
|
2023-08-01 19:42:36 +00:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteToFile(void) {
|
2024-01-23 17:00:22 +00:00
|
|
|
if (client.irc_msg[1] == '.' || client.irc_channel[0] != '#')
|
2023-08-01 19:42:36 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
char *filename = GetFilename();
|
|
|
|
FILE *fp = fopen(filename, "a");
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
2024-01-23 17:00:22 +00:00
|
|
|
printf("Cant open file %s\n", client.irc_channel);
|
2023-08-01 19:42:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-23 17:00:22 +00:00
|
|
|
fprintf(fp, FORMAT, client.irc_nick, client.irc_msg);
|
2023-08-01 19:42:36 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2023-08-17 04:46:35 +00:00
|
|
|
void recvinfo(void) {
|
2023-08-07 15:08:35 +00:00
|
|
|
while (1) {
|
2024-01-23 17:00:22 +00:00
|
|
|
int irc_status = IRCC_recv(&client);
|
2023-08-17 04:46:35 +00:00
|
|
|
if (irc_status == IRCC_DISCONNECTED)
|
|
|
|
die("Disconnected");
|
2023-08-07 15:08:35 +00:00
|
|
|
|
2024-01-23 17:00:22 +00:00
|
|
|
irc_status = IRCC_parse(&client);
|
|
|
|
if (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
|
2023-08-07 15:08:35 +00:00
|
|
|
WriteToFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 19:42:36 +00:00
|
|
|
int main(void) {
|
|
|
|
if (chdir(DIR))
|
|
|
|
die("Cant chdir");
|
|
|
|
|
2023-08-06 17:16:49 +00:00
|
|
|
//512 - size of raw buffer (max irc)
|
2023-10-06 13:04:45 +00:00
|
|
|
IRCC_init(&client);
|
2023-08-01 19:42:36 +00:00
|
|
|
int status = IRCC_connect(&client, HOST, PORT);
|
|
|
|
if (status == IRCC_ERROR)
|
|
|
|
die("Conn refused");
|
|
|
|
|
2023-08-09 18:54:13 +00:00
|
|
|
//Register
|
2023-08-01 19:42:36 +00:00
|
|
|
IRCC_register(&client, NICK);
|
2023-08-07 15:08:35 +00:00
|
|
|
|
|
|
|
//Recv motd and join in channel
|
|
|
|
sleep(5);
|
2023-12-13 13:41:27 +00:00
|
|
|
for (size_t i = 0; i < sizeof(channels) / sizeof(struct CHANNEL); i++)
|
|
|
|
IRCC_join(&client, channels[i].channel, channels[i].key);
|
2023-08-01 19:42:36 +00:00
|
|
|
|
2023-10-09 14:16:02 +00:00
|
|
|
for (size_t i = 0; i < sizeof(send_after_join) / sizeof(char *); i++) {
|
|
|
|
if (send_after_join[i] == NULL)
|
|
|
|
break;
|
|
|
|
|
2024-01-23 17:00:22 +00:00
|
|
|
send(client.irc_socket, send_after_join[i], strlen(send_after_join[i]), 0);
|
2023-10-09 14:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 04:46:35 +00:00
|
|
|
recvinfo();
|
2023-08-01 19:42:36 +00:00
|
|
|
}
|
2023-08-09 18:54:13 +00:00
|
|
|
|