Upload files to "/"

This commit is contained in:
8nl 2024-08-16 18:23:54 +00:00
parent 3b8975a21c
commit d2781eb63c
2 changed files with 86 additions and 54 deletions

15
README Normal file
View File

@ -0,0 +1,15 @@
> irclibs
git.macaw.me/8nl/irclibs
md5 irc.c c43a8faf4ad199a30a1148b847867022
md5 irc.h bd6a06cb02d75da9896233db6ea20cd8
> сборка
git clone https://git.macaw.me/8nl/irclibs
cc -I irclibs/C irclibs/C/irc.c main.c -lssl -lcrypto -DENABLE_SSL
> gemini://any-key.press/tt/welcome.gmi
Сделано в соответствии с духом Тривиальных Технологий
> Лицензия: WTFPL

125
main.c
View File

@ -3,63 +3,72 @@
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#include <sys/socket.h>
#include "irc.h"
#ifdef ENABLE_SSL
#include <openssl/err.h>
#endif
IRCC_client client;
char *dir = "./";
char *ext = ".txt";
char *fmt = "[%s] %s";
char *host = "127.0.0.1";
char *nick = "historybot";
int port = 6667;
int usessl = 0;
#define bool char
typedef struct {
char *dir;
char *ext;
char *fmt;
char *host;
char *nick;
int port;
bool usessl;
bool debug;
} CFG;
IRCC_client ircbot;
static CFG cfg = {
.dir = "./",
.ext = ".ext",
.fmt = "[%s] %s",
.host = "127.0.0.1",
.nick = "historybot",
.port = 6667
};
void die(const char *msg) {
fprintf(stderr, "history: %s\n", msg);
IRCC_close(&client);
fprintf(stderr, "history: %s: %s\n", msg, strerror(errno));
IRCC_close(&ircbot);
exit(1);
}
void sig_handler(int sig) {
fprintf(stderr, "history: recived signal: %d\n", sig);
IRCC_close(&client);
fprintf(stderr, "history: received signal: %d\n", sig);
IRCC_close(&ircbot);
exit(0);
}
char *GetFilename(void) {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
struct tm *tm = localtime(&t);
if (tm == NULL)
die("localtime()");
size_t filename_size = strlen(client.irc_channel) + (sizeof(tm.tm_year) * 3) + strlen(ext) + 3;
char *filename = malloc(filename_size + 1);
if (filename == NULL)
die("malloc failed");
snprintf(filename, filename_size, "%s_%d-%d-%d%s", client.irc_channel + 1, tm.tm_mday, tm.tm_mon, tm.tm_year, ext);
static char filename[PATH_MAX + 1];
snprintf(filename, PATH_MAX, "%s_%d-%d-%d%s", ircbot.irc_channel + 1, tm->tm_mday, tm->tm_mon, 1900 + tm->tm_year, cfg.ext);
return filename;
}
void WriteToFile(void) {
if (client.irc_msg[1] == '.' || client.irc_channel[0] != '#')
/* Secret message */
if (ircbot.irc_msg[0] == '!')
return;
char *filename = GetFilename();
FILE *fp = fopen(filename, "a");
free(filename);
FILE *fp = fopen(GetFilename(), "a");
if (fp == NULL) {
fprintf(stderr, "history: cant open file: %s\n", client.irc_channel);
fprintf(stderr, "history: cant open file: %s\n", ircbot.irc_channel);
return;
}
fprintf(fp, fmt, client.irc_nick, client.irc_msg);
fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg);
fprintf(fp, "\n");
fclose(fp);
}
@ -67,40 +76,45 @@ void WriteToFile(void) {
int main(int argc, char **argv) {
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGINT, sig_handler);
int opt;
while ((opt = getopt(argc, argv, "h:p:d:n:f:e:t")) != -1) {
while ((opt = getopt(argc, argv, "h:p:d:n:f:e:tD")) != -1) {
switch (opt) {
case 'h':
host = optarg;
cfg.host = optarg;
break;
case 'p':
port = atoi(optarg);
cfg.port = atoi(optarg);
break;
case 'd':
dir = optarg;
cfg.dir = optarg;
break;
case 'n':
nick = optarg;
cfg.nick = optarg;
break;
case 'e':
ext = optarg;
cfg.ext = optarg;
break;
case 'f':
fmt = optarg;
cfg.fmt = optarg;
break;
case 't':
usessl = 1;
cfg.usessl = 1;
break;
case 'D':
cfg.debug = 1;
break;
default:
printf("history [hpdneft] [\"#channel,key\"]\n\t-t Use ssl\n\t-h HOST Default: %s\n\t-p PORT Default: %d\n\t-d DIR Log dir Default: %s\n\t-n NICK Default: %s\n\t-e EXT File extantion Default: %s\n\t-f FMT Text format Default: %s\n", host, port, dir, nick, ext, fmt);
printf("history -[h:p:d:n:e:f:tD] [\"#channel:key\"]\n\t-h STR Server's ip Default: %s\n\t-p INT Server's port Default: %d\n\t-d STR Logging dir Default: %s\n\t-n STR Bot nickname Default: %s\n\t-e STR File extantion Default: %s\n\t-f STR Log format Default: %s\n\t-t Use ssl Default: %s\n\t-D Print messages(debug) Default: %s\n", cfg.host, cfg.port, cfg.dir, cfg.nick, cfg.ext, cfg.fmt, (cfg.usessl) ? "true" : "false", (cfg.debug) ? "true" : "false");
return 0;
}
}
@ -108,48 +122,51 @@ int main(int argc, char **argv) {
argv += optind;
argc -= optind;
if (chdir(dir))
die("Cant to change directory");
if (chdir(cfg.dir))
die("chdir()");
int status = IRCC_connect(&client, host, port);
int status = IRCC_connect(&ircbot, cfg.host, cfg.port);
if (status == IRCC_ERROR)
die("connection refused");
die("connect()");
if (usessl) {
if (cfg.usessl) {
#ifdef ENABLE_SSL
if (IRCC_initssl(&client) == IRCC_ERROR)
if (IRCC_initssl(&ircbot) == IRCC_ERROR)
die(ERR_error_string(ERR_get_error(), NULL));
#endif
}
//Register and skip motd
if (IRCC_register(&client, nick) == IRCC_DISCONNECTED)
die("disconnected");
/* Register and skip MOTD */
if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED)
die("irc register");
sleep(5);
//Join in channel
/* Join a channel */
for (int i = 0; i < argc; i++) {
char *key = "";
char *channel = argv[i];
char *p = strchr(channel, ',');
char *p = strchr(channel, ':');
if (p != NULL) {
p[0] = '\0';
key = p + 1;
}
IRCC_join(&client, channel, key);
IRCC_join(&ircbot, channel, key);
}
//Recv
/* Logging */
while (1) {
int irc_status = IRCC_recv(&client);
int irc_status = IRCC_recv(&ircbot);
if (irc_status == IRCC_DISCONNECTED)
die("disconnected");
die("irc recv");
irc_status = IRCC_parse(&client);
if (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
if (cfg.debug)
printf("\033[32m%s\033[0m", ircbot.irc_raw);
irc_status = IRCC_parse(&ircbot);
if (ircbot.irc_nick != NULL && ircbot.irc_channel != NULL && ircbot.irc_msg != NULL && irc_status == IRCC_PRIVMSG)
WriteToFile();
}
}