92 lines
2.0 KiB
C
92 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <time.h>
|
|
#include "irc.h"
|
|
#include "cfg.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.irc_channel + 1) + (sizeof(tm.tm_year) * 3) + strlen(EXT) + 3;
|
|
char *filename = malloc(filename_size + 1);
|
|
if (filename == NULL)
|
|
die("malloc returned NULL");
|
|
|
|
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;
|
|
}
|
|
|
|
void WriteToFile(void) {
|
|
if (client.irc_msg[1] == '.' || client.irc_channel[0] != '#')
|
|
return;
|
|
|
|
char *filename = GetFilename();
|
|
FILE *fp = fopen(filename, "a");
|
|
free(filename);
|
|
|
|
if (fp == NULL) {
|
|
printf("Cant open file %s\n", client.irc_channel);
|
|
return;
|
|
}
|
|
|
|
fprintf(fp, FORMAT, client.irc_nick, client.irc_msg);
|
|
fclose(fp);
|
|
}
|
|
|
|
void recvinfo(void) {
|
|
while (1) {
|
|
int irc_status = IRCC_recv(&client);
|
|
if (irc_status == IRCC_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)
|
|
WriteToFile();
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
|