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 <unistd.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include <errno.h>
#include <time.h> #include <time.h>
#include <sys/socket.h>
#include "irc.h" #include "irc.h"
#ifdef ENABLE_SSL #ifdef ENABLE_SSL
#include <openssl/err.h> #include <openssl/err.h>
#endif #endif
IRCC_client client; #define bool char
char *dir = "./"; typedef struct {
char *ext = ".txt"; char *dir;
char *fmt = "[%s] %s"; char *ext;
char *host = "127.0.0.1"; char *fmt;
char *nick = "historybot"; char *host;
int port = 6667; char *nick;
int usessl = 0; 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) { void die(const char *msg) {
fprintf(stderr, "history: %s\n", msg); fprintf(stderr, "history: %s: %s\n", msg, strerror(errno));
IRCC_close(&client); IRCC_close(&ircbot);
exit(1); exit(1);
} }
void sig_handler(int sig) { void sig_handler(int sig) {
fprintf(stderr, "history: recived signal: %d\n", sig); fprintf(stderr, "history: received signal: %d\n", sig);
IRCC_close(&ircbot);
IRCC_close(&client);
exit(0); exit(0);
} }
char *GetFilename(void) { char *GetFilename(void) {
time_t t = time(NULL); 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; static char filename[PATH_MAX + 1];
char *filename = malloc(filename_size + 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);
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);
return filename; return filename;
} }
void WriteToFile(void) { void WriteToFile(void) {
if (client.irc_msg[1] == '.' || client.irc_channel[0] != '#') /* Secret message */
if (ircbot.irc_msg[0] == '!')
return; return;
char *filename = GetFilename(); FILE *fp = fopen(GetFilename(), "a");
FILE *fp = fopen(filename, "a");
free(filename);
if (fp == NULL) { 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; return;
} }
fprintf(fp, fmt, client.irc_nick, client.irc_msg); fprintf(fp, cfg.fmt, ircbot.irc_nick, ircbot.irc_msg);
fprintf(fp, "\n"); fprintf(fp, "\n");
fclose(fp); fclose(fp);
} }
@ -67,40 +76,45 @@ void WriteToFile(void) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
signal(SIGTERM, sig_handler); signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler); signal(SIGKILL, sig_handler);
signal(SIGINT, sig_handler);
int opt; 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) { switch (opt) {
case 'h': case 'h':
host = optarg; cfg.host = optarg;
break; break;
case 'p': case 'p':
port = atoi(optarg); cfg.port = atoi(optarg);
break; break;
case 'd': case 'd':
dir = optarg; cfg.dir = optarg;
break; break;
case 'n': case 'n':
nick = optarg; cfg.nick = optarg;
break; break;
case 'e': case 'e':
ext = optarg; cfg.ext = optarg;
break; break;
case 'f': case 'f':
fmt = optarg; cfg.fmt = optarg;
break; break;
case 't': case 't':
usessl = 1; cfg.usessl = 1;
break;
case 'D':
cfg.debug = 1;
break; break;
default: 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; return 0;
} }
} }
@ -108,48 +122,51 @@ int main(int argc, char **argv) {
argv += optind; argv += optind;
argc -= optind; argc -= optind;
if (chdir(dir)) if (chdir(cfg.dir))
die("Cant to change directory"); die("chdir()");
int status = IRCC_connect(&client, host, port); int status = IRCC_connect(&ircbot, cfg.host, cfg.port);
if (status == IRCC_ERROR) if (status == IRCC_ERROR)
die("connection refused"); die("connect()");
if (usessl) { if (cfg.usessl) {
#ifdef ENABLE_SSL #ifdef ENABLE_SSL
if (IRCC_initssl(&client) == IRCC_ERROR) if (IRCC_initssl(&ircbot) == IRCC_ERROR)
die(ERR_error_string(ERR_get_error(), NULL)); die(ERR_error_string(ERR_get_error(), NULL));
#endif #endif
} }
//Register and skip motd /* Register and skip MOTD */
if (IRCC_register(&client, nick) == IRCC_DISCONNECTED) if (IRCC_register(&ircbot, cfg.nick) == IRCC_DISCONNECTED)
die("disconnected"); die("irc register");
sleep(5); sleep(5);
//Join in channel /* Join a channel */
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
char *key = ""; char *key = "";
char *channel = argv[i]; char *channel = argv[i];
char *p = strchr(channel, ','); char *p = strchr(channel, ':');
if (p != NULL) { if (p != NULL) {
p[0] = '\0'; p[0] = '\0';
key = p + 1; key = p + 1;
} }
IRCC_join(&client, channel, key); IRCC_join(&ircbot, channel, key);
} }
//Recv /* Logging */
while (1) { while (1) {
int irc_status = IRCC_recv(&client); int irc_status = IRCC_recv(&ircbot);
if (irc_status == IRCC_DISCONNECTED) if (irc_status == IRCC_DISCONNECTED)
die("disconnected"); die("irc recv");
irc_status = IRCC_parse(&client); if (cfg.debug)
if (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL && irc_status == IRCC_PRIVMSG) 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(); WriteToFile();
} }
} }