Upload files to "/"

This commit is contained in:
8nl 2024-01-23 17:00:22 +00:00
parent d716e164e4
commit aa28da18fe
2 changed files with 17 additions and 11 deletions

4
cfg.h
View File

@ -1,6 +1,6 @@
//Settings //Settings
#define NICK "historybotd" #define NICK "histbot"
#define HOST "irc.rizon.net" //localhost" #define HOST "192.168.0.184" //localhost"
#define PORT 6667 #define PORT 6667
#define DIR "./" #define DIR "./"

24
main.c
View File

@ -1,6 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <time.h>
#include "irc.h" #include "irc.h"
#include "cfg.h" #include "cfg.h"
#include <time.h>
IRCC_client client; IRCC_client client;
void die(char *msg) { void die(char *msg) {
@ -18,17 +23,17 @@ char *GetFilename(void) {
//Get filename + date //Get filename + date
struct tm tm = GetTime(); struct tm tm = GetTime();
size_t filename_size = strlen(client.channel + 1) + (sizeof(tm.tm_year) * 3) + strlen(EXT) + 3; size_t filename_size = strlen(client.irc_channel + 1) + (sizeof(tm.tm_year) * 3) + strlen(EXT) + 3;
char *filename = malloc(filename_size + 1); char *filename = malloc(filename_size + 1);
if (filename == NULL) if (filename == NULL)
die("malloc returned 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, 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; return filename;
} }
void WriteToFile(void) { void WriteToFile(void) {
if (client.msg[1] == '.' || client.channel[0] != '#') if (client.irc_msg[1] == '.' || client.irc_channel[0] != '#')
return; return;
char *filename = GetFilename(); char *filename = GetFilename();
@ -36,21 +41,22 @@ void WriteToFile(void) {
free(filename); free(filename);
if (fp == NULL) { if (fp == NULL) {
printf("Cant open file %s\n", client.channel); printf("Cant open file %s\n", client.irc_channel);
return; return;
} }
fprintf(fp, FORMAT, client.nick, client.msg); fprintf(fp, FORMAT, client.irc_nick, client.irc_msg);
fclose(fp); fclose(fp);
} }
void recvinfo(void) { void recvinfo(void) {
while (1) { while (1) {
unsigned int irc_status = IRCC_recv(&client); int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED) if (irc_status == IRCC_DISCONNECTED)
die("Disconnected"); die("Disconnected");
else if (client.nick != NULL && client.channel != NULL && client.msg != NULL && irc_status == IRCC_PRIVMSG) irc_status = IRCC_parse(&client);
if (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
WriteToFile(); WriteToFile();
} }
} }
@ -77,7 +83,7 @@ int main(void) {
if (send_after_join[i] == NULL) if (send_after_join[i] == NULL)
break; break;
send(client.socket, send_after_join[i], strlen(send_after_join[i]), 0); send(client.irc_socket, send_after_join[i], strlen(send_after_join[i]), 0);
} }
recvinfo(); recvinfo();