2023-08-01 19:42:36 +00:00
|
|
|
#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();
|
|
|
|
|
2023-08-06 17:16:49 +00:00
|
|
|
size_t filename_size = strlen(client.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");
|
|
|
|
|
2023-08-06 17:16:49 +00:00
|
|
|
snprintf(filename, filename_size, "%s-%d-%d-%d%s", client.channel + 1, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday, EXT);
|
2023-08-01 19:42:36 +00:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteToFile(void) {
|
2023-08-07 15:18:00 +00:00
|
|
|
if (client.msg[1] == '.' || client.channel[0] != '#')
|
2023-08-01 19:42:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-08-17 04:46:35 +00:00
|
|
|
void recvinfo(void) {
|
2023-08-07 15:08:35 +00:00
|
|
|
while (1) {
|
|
|
|
unsigned 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
|
|
|
|
|
|
|
else if (client.nick != NULL && client.channel != NULL && client.msg != NULL && irc_status == IRCC_PRIVMSG)
|
|
|
|
WriteToFile();
|
|
|
|
|
|
|
|
else if (strstr(client.raw, "No Ident") || irc_status == IRCC_CONNECTED)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-17 04:46:35 +00:00
|
|
|
IRCC_init(&client, IRCC_MSG_MAX);
|
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
|
|
|
//Socket timeout
|
2023-08-17 04:46:35 +00:00
|
|
|
struct timeval tv = {IRCC_PING_TIMEOUT, IRCC_PING_TIMEOUT};
|
2023-08-09 18:54:13 +00:00
|
|
|
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
|
|
|
die("setsockopt");
|
|
|
|
|
|
|
|
//Register
|
2023-08-17 04:46:35 +00:00
|
|
|
recvinfo();
|
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
|
2023-08-17 04:46:35 +00:00
|
|
|
recvinfo();
|
2023-08-07 15:08:35 +00:00
|
|
|
sleep(5);
|
2023-08-01 19:42:36 +00:00
|
|
|
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
|
2023-08-09 18:54:13 +00:00
|
|
|
if (channels[i] == NULL)
|
|
|
|
break;
|
|
|
|
|
2023-08-01 19:42:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|