Compare commits

14 Commits

Author SHA1 Message Date
e0352ad73b More code movement
Client can also now talk to the daemon, though still very rough
2025-09-25 21:10:17 +02:00
cb642e905f fix compiler errors and warnings 2025-09-19 13:01:03 +02:00
2564df5cec move config stuff to hdbd.c 2025-09-19 11:06:32 +02:00
34495bccbb move download() to hdbd.c and refactor it
it also wont compile because it is missing the config stuff
2025-09-18 23:36:59 +02:00
e52388311e remove h_has_internet() 2025-09-18 23:07:21 +02:00
d19b454a70 update Makefile and remove strops 2025-09-18 23:05:04 +02:00
c3d7bdfdf4 basic message parsing 2025-09-18 00:29:17 +02:00
2a728034c2 more logging and fixed bug
used the wrong buffer when trying to read clients message
2025-09-17 13:41:41 +02:00
356d2fdaa3 switch to syslog 2025-09-16 23:42:41 +02:00
4d8fd7b98e still not making any progress 2025-07-21 22:39:42 +02:00
c776501adc new unix socket stuff
I can't get logfiles to work though
2025-07-19 23:47:34 +02:00
e0274fb6d5 add some TODOs 2025-06-30 22:30:06 +02:00
9d6fa83a43 sort generated file 2025-06-05 22:27:11 +02:00
31dc467165 setup daemon 2025-06-01 22:40:56 +02:00
5 changed files with 544 additions and 449 deletions

View File

@@ -1,16 +1,17 @@
.POSIX:
hdb-debug:
gcc -ansi -ggdb -o build/hdb-debug src/main.c src/db.c -lsqlite3
hdb:
gcc -ansi -O2 -pipe -o build/hdb src/main.c src/db.c -lsqlite3
hdb: src/hdb.c
gcc -ansi -O2 -pipe -o build/hdb src/hdb.c
strip build/hdb
hdbd: src/hdbd.c
gcc -ansi -O2 -pipe -o build/hdbd src/hdbd.c -lsqlite3
strip build/hdbd
.PHONY: install uninstall
install: hdb
sudo mv build/hdb /usr/local/bin
install: hdb hdbd
sudo mv build/hdb build/hdbd /usr/local/bin
uninstall:
sudo rm /usr/local/bin/hdb
sudo rm /usr/local/bin/hdb /usr/local/bin/hdbd

140
src/db.c
View File

@@ -1,43 +1,3 @@
#include "db.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
sqlite3 *db;
int h_db_open(const char *db_name) {
int ret;
char *err_msg;
if (db != NULL) {
fprintf(stderr, "There is already a sqlite db open\n");
return 1;
}
ret = sqlite3_open(db_name, &db);
if (ret != SQLITE_OK) {
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
h_db_close();
return 1;
}
char *sql = "CREATE TABLE IF NOT EXISTS hentai (url TEXT PRIMARY KEY NOT NULL, artist_name TEXT NOT NULL, website_name TEXT NOT NULL) STRICT;";
ret = sqlite3_exec(db, sql, NULL, 0, &err_msg);
if (ret != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
h_db_close();
return 1;
}
return 0;
}
void h_db_close() {
sqlite3_close(db);
}
static int fprintf_callback(void *fptr, int argc, char **argv, char **col_name) {
if (fptr == NULL) {
fprintf(stderr, "fptr is NULL\n");
@@ -60,104 +20,6 @@ static int db_row_callback(void *db_row, int argc, char **argv, char **col_name)
return 0;
}
char* get_website_from_url(const char *url) {
if (strstr(url, "rule34.xxx") != NULL) {
return "Rule34";
} else if (strstr(url, "iwara.tv") != NULL) {
return "Iwara";
} else {
return NULL;
}
}
char* get_artist_from_url(const char *url, const char *website_name) {
size_t offset = 0;
if (strcmp(website_name, "Rule34") == 0) {
offset = 51;
} else if (strcmp(website_name, "Gelbooru") == 0) {
offset = 53;
} else if (strcmp(website_name, "ATF") == 0) {
offset = 42;
} else {
return NULL;
}
size_t name_length = strlen(url) - offset;
size_t char_count = name_length;
char *tmp_name = malloc(name_length + 1);
memcpy(tmp_name, url + offset, name_length);
tmp_name[name_length] = '\0';
size_t i;
size_t j = 0;
for (i = 0; i < strlen(tmp_name); i++) {
if (tmp_name[i] == '%') {
char url_encoded[4];
url_encoded[0] = tmp_name[i];
url_encoded[1] = tmp_name[i + 1];
url_encoded[2] = tmp_name[i + 2];
url_encoded[3] = '\0';
if (strstr("%20%21%22%23%24%26%27%28%29%2A%2C%2E%2F%3B%3C%3E%3F%5B%5C%5D%5E%60%7B%7C%7D%7E", url_encoded) != NULL) {
tmp_name[i] = ' ';
char_count -= 1;
} else if (strcmp(url_encoded, "%25") == 0) {
tmp_name[i] = '%';
} else if (strcmp(url_encoded, "%2B") == 0) {
tmp_name[i] = '+';
} else if (strcmp(url_encoded, "%2D") == 0) {
tmp_name[i] = '-';
} else if (strcmp(url_encoded, "%2E") == 0) {
tmp_name[i] = '.';
} else if (strcmp(url_encoded, "%2F") == 0) {
tmp_name[i] = '/';
} else if (strcmp(url_encoded, "%3A") == 0) {
tmp_name[i] = ':';
} else if (strcmp(url_encoded, "%3D") == 0) {
tmp_name[i] = '=';
} else if (strcmp(url_encoded, "%40") == 0) {
tmp_name[i] = '@';
} else if (strcmp(url_encoded, "%5F") == 0) {
tmp_name[i] = '_';
} else {
return NULL;
}
tmp_name[i += 1] = ' ';
tmp_name[i += 1] = ' ';
char_count -= 2;
}
}
char *artist_name = malloc(char_count + 1);
memset(artist_name, 0, char_count + 1);
for (i = 0; i < name_length; i++) {
if (tmp_name[i] == ' ') {
continue;
}
artist_name[j] = tmp_name[i];
j++;
}
artist_name[char_count] = '\0';
j = strlen(artist_name) - strlen("artist");
if (strcmp(artist_name + j, "artist") == 0) {
for (; j < strlen(artist_name); j++) {
artist_name[j] = '\0';
}
}
j = strlen(artist_name) - 1;
while (artist_name[j] == '_') {
artist_name[j] = '\0';
j--;
}
if (strlen(artist_name) < 2) {
return NULL;
}
return artist_name;
}
int h_db_add(char *db_row, const char *url) {
char *sql = malloc(10000);
int result;
@@ -213,7 +75,7 @@ int h_db_generate_file(FILE *fptr, const char *website_name) {
int result;
char *error_msg;
sprintf(sql, "SELECT * FROM hentai WHERE website_name = '%s';", website_name);
sprintf(sql, "SELECT * FROM hentai WHERE website_name = '%s' ORDER BY url ASC;", website_name);
result = sqlite3_exec(db, sql, fprintf_callback, fptr, &error_msg);
free(sql);
if (result != SQLITE_OK) {

223
src/hdb.c Normal file
View File

@@ -0,0 +1,223 @@
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
int main(int argc, char **argv) {
int ret;
int client_sock;
struct sockaddr_un remote;
int sock_len;
int data_length;
char send_buf[1000] = {0};
char receive_buf[1000] = {0};
if (argc < 2) {
fprintf(stderr, "Incorrect amount of arguments\n");
return 1;
}
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (client_sock == -1) {
perror("Failed to init socket");
return 1;
}
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, "/tmp/test_sock");
sock_len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(client_sock, (struct sockaddr*)&remote, sock_len) == -1) {
perror("Failed to connect to socket");
return 1;
}
printf("Connected to daemon.\n");
/* hdb add https://example.com optional_custom_name */
if (strcmp(argv[1], "add") == 0) {
char website_name[1000] = {0};
char artist_name[1000] = {0};
if (argc < 3) {
fprintf(stderr, "Incorrect amount of arguments\n");
close(client_sock);
return 1;
}
/* Get website name from url */
if (strstr(argv[2], "rule34.xxx") != NULL) {
strcpy(website_name, "Rule34");
} else if (strstr(argv[2], "iwara.tv") != NULL) {
strcpy(website_name, "Iwara");
} else {
fprintf(stderr, "url not supported.\n");
close(client_sock);
return 1;
}
if (argc == 3) { /* Get artist_name from url */
/* TODO: Refactor */
size_t offset = 0;
if (strcmp(website_name, "Rule34") == 0) {
offset = 51;
} else if (strcmp(website_name, "Gelbooru") == 0) {
offset = 53;
} else if (strcmp(website_name, "ATF") == 0) {
offset = 42;
} else {
fprintf(stderr, "Cannot get artist name from url.\n");
close(client_sock);
return 1;
}
size_t name_length = strlen(argv[2]) - offset;
size_t char_count = name_length;
char *tmp_name = malloc(name_length + 1);
memcpy(tmp_name, argv[2] + offset, name_length);
tmp_name[name_length] = '\0';
size_t i;
size_t j = 0;
for (i = 0; i < strlen(tmp_name); i++) {
if (tmp_name[i] == '%') {
char url_encoded[4];
url_encoded[0] = tmp_name[i];
url_encoded[1] = tmp_name[i + 1];
url_encoded[2] = tmp_name[i + 2];
url_encoded[3] = '\0';
if (strstr("%20%21%22%23%24%26%27%28%29%2A%2C%2E%2F%3B%3C%3E%3F%5B%5C%5D%5E%60%7B%7C%7D%7E", url_encoded) != NULL) {
tmp_name[i] = ' ';
char_count -= 1;
} else if (strcmp(url_encoded, "%25") == 0) {
tmp_name[i] = '%';
} else if (strcmp(url_encoded, "%2B") == 0) {
tmp_name[i] = '+';
} else if (strcmp(url_encoded, "%2D") == 0) {
tmp_name[i] = '-';
} else if (strcmp(url_encoded, "%2E") == 0) {
tmp_name[i] = '.';
} else if (strcmp(url_encoded, "%2F") == 0) {
tmp_name[i] = '/';
} else if (strcmp(url_encoded, "%3A") == 0) {
tmp_name[i] = ':';
} else if (strcmp(url_encoded, "%3D") == 0) {
tmp_name[i] = '=';
} else if (strcmp(url_encoded, "%40") == 0) {
tmp_name[i] = '@';
} else if (strcmp(url_encoded, "%5F") == 0) {
tmp_name[i] = '_';
} else {
printf("I don't know why this exists.");
close(client_sock);
return 1;
}
tmp_name[i += 1] = ' ';
tmp_name[i += 1] = ' ';
char_count -= 2;
}
}
char *artist_name = malloc(char_count + 1);
memset(artist_name, 0, char_count + 1);
for (i = 0; i < name_length; i++) {
if (tmp_name[i] == ' ') {
continue;
}
artist_name[j] = tmp_name[i];
j++;
}
artist_name[char_count] = '\0';
j = strlen(artist_name) - strlen("artist");
if (strcmp(artist_name + j, "artist") == 0) {
for (; j < strlen(artist_name); j++) {
artist_name[j] = '\0';
}
}
j = strlen(artist_name) - 1;
while (artist_name[j] == '_') {
artist_name[j] = '\0';
j--;
}
if (strlen(artist_name) < 2) {
fprintf(stderr, "Artist name unusable after cleanup. Result: '%s'\n", artist_name);
close(client_sock);
return 1;
}
sprintf(send_buf, "add %s %s %s", argv[2], website_name, artist_name);
} else if (argc > 3) { /* artist_name provided */
sprintf(send_buf, "add %s %s %s", argv[2], website_name, argv[3]);
}
if (write(client_sock, send_buf, strlen(send_buf)) == -1) {
perror("Failed to write to socket");
close(client_sock);
return 1;
}
data_length = read(client_sock, receive_buf, sizeof receive_buf);
if (data_length <= 0) {
fprintf(stderr, "Received no response from daemon.\n");
close(client_sock);
return 1;
}
printf("Response from daemon: %s\n", data_length, receive_buf);
} /*else if (strcmp(argv[1], "download_website") == 0) {
if (argc != 3) {
fprintf(stderr, "Incorrect amount of arguments\n");
h_db_close();
return 1;
}
char *file_name = malloc(10000);
sprintf(file_name, "%s/%s_generated.txt", config.tmp_dir, argv[2]);
FILE *fptr = fopen(file_name, "w+");
if (fptr == NULL) {
fprintf(stderr, "Couldn't open file '%s'\n", file_name);
h_db_close();
return 1;
}
ret = h_db_generate_file(fptr, argv[2]);
fclose(fptr);
if (ret != 0) {
h_db_close();
return 1;
}
fptr = fopen(file_name, "r");
if (fptr == NULL) {
fprintf(stderr, "Couldn't open file '%s'\n", file_name);
h_db_close();
return 1;
}
char *line = malloc(10000);
printf("Ready to download\n");
while(fgets(line, 10000, fptr) != NULL) {
ret = h_download(line, config);
if (ret != 0) {
fclose(fptr);
h_db_close();
return 1;
}
}
free(line);
fclose(fptr);
free(file_name);
}*/ else {
fprintf(stderr, "Invalid argument\n");
close(client_sock);
return 1;
}
close(client_sock);
return 0;
}

311
src/hdbd.c Normal file
View File

@@ -0,0 +1,311 @@
#define _POSIX_C_SOURCE 200809L /* needed for strndup */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <syslog.h>
#include <sqlite3.h>
pid_t pid, sid;
int server_sock;
struct sockaddr_un server;
int sockaddr_len;
struct {
char *tmp_dir;
char *data_dir;
char *gallery_dl_conf;
char *yt_dlp_conf;
} config;
sqlite3 *db;
char *sql_err;
int config_parser() {
char config_location[1000] = {0};
char *user_name = getenv("USER");
if (user_name == NULL) {
syslog(LOG_ERR, "config_parser(): Couldn't read env variable USER");
return 1;
}
sprintf(config_location, "/home/%s/.config/HDB.conf", user_name);
FILE *fptr = fopen(config_location, "r");
if (fptr == NULL) {
syslog(LOG_ERR, "config_parser(): Failed to open the config %s", config_location);
return 1;
}
char line[1000] = {0};
char left[1000] = {0};
char right[1000] = {0};
while(fgets(line, 1000, fptr) != NULL) {
line[strcspn(line, "\n#")] = '\0';
if (strcmp(line, "") == 0) {
continue;
}
if (
strchr(line, '=') == NULL
|| line[strcspn(line, "=") + 1] == ' '
|| line[strcspn(line, "=") - 1] == ' '
) {
syslog(LOG_WARNING, "config_parser(): Config line doesn't contain a valid assignment. line = '%s'", line);
continue;
}
/* TODO: support env variables */
if (strchr(line, '$') != NULL) {
syslog(LOG_WARNING, "config_parser(): Config doesn't support env variables. line = '%s'", line);
continue;
}
strcpy(left, strtok(line, "="));
strcpy(right, strtok(NULL, "\0"));
if (strcmp(left, "data_dir") == 0) {
config.data_dir = malloc(1000);
strcpy(config.data_dir, right);
} else if (strcmp(left, "gallery-dl_conf") == 0) {
config.gallery_dl_conf = malloc(1000);
strcpy(config.gallery_dl_conf, right);
} else if (strcmp(left, "yt-dlp_conf") == 0) {
config.yt_dlp_conf = malloc(1000);
strcpy(config.yt_dlp_conf, right);
} else {
syslog(LOG_WARNING, "config_parser(): Unknown config option. key = '%s'", left);
}
}
fclose(fptr);
return 0;
}
/*
Message format: (Basically like a command line)
operation operand1 operand2 operand3
Supports up to 3 operands
*/
void parse_message(char *buffer, char *operation, char *operand1, char *operand2, char *operand3) {
int len_to_ws = 0;
int offset = 0;
len_to_ws = strcspn(buffer, " ");
strncpy(operation, buffer, len_to_ws);
offset = len_to_ws + 1;
syslog(LOG_DEBUG, "parse_message(): operation = '%s'", operation);
if (strlen(buffer) == offset - 1) { return; }
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand1, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
syslog(LOG_DEBUG, "parse_message(): operand1 = '%s'", operand1);
if (strlen(buffer) == offset -1) { return; }
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand2, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
syslog(LOG_DEBUG, "parse_message(): operand2 = '%s'", operand2);
if (strlen(buffer) == offset -1) { return; }
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand3, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
syslog(LOG_DEBUG, "parse_message(): operand3 = '%s'", operand3);
if (strlen(buffer) == offset -1) { return; }
}
int download(char *url, char *artist_name, char *website_name) {
char *args[7];
char download_path[10000] = {0};
if (strstr(website_name, "Iwara") != NULL) {
sprintf(download_path, "%s/%s/%s/%(id)s.%(ext)s", config.data_dir, artist_name, website_name);
args[0] = "yt-dlp_linux";
args[1] = "--config-locations";
args[2] = config.yt_dlp_conf;
args[3] = "-o";
} else {
sprintf(download_path, "%s/%s/%s", config.data_dir, artist_name, website_name);
args[0] = "gallery-dl";
args[1] = "--config";
args[2] = config.gallery_dl_conf;
args[3] = "--directory";
}
args[4] = download_path;
args[5] = url;
args[6] = NULL;
syslog(LOG_DEBUG, "download(): Starting download using %s", args[0]);
int ret;
pid_t pid = fork();
if (pid == -1) {
syslog(LOG_ERR, "download(): Couldn't fork");
return 1;
} else if (pid > 0) {
waitpid(pid, &ret, 0);
} else {
execvp(args[0], args);
}
if (ret != 0) {
syslog(LOG_ERR, "download(): Downloader exited with non-zero code. code = %d", ret);
}
return 0;
}
int main() {
/*
Setup daemon
Used this for basic idea => https://en.wikipedia.org/wiki/Daemon_(computing)
*/
pid = fork();
if (pid == -1) {
fprintf(stderr, "Couldn't fork\n");
return 1;
} else if (pid != 0) {
printf("Pid of child is %d\n", pid);
return 0;
}
sid = setsid();
if (sid == -1) {
return 1;
}
if (chdir("/") == -1) {
return 1;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
openlog("hdbd", LOG_CONS|LOG_PID, LOG_USER);
syslog(LOG_NOTICE, "Daemon setup done.");
/* Setup Unix Socket */
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_sock == -1) {
return 1;
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, "/tmp/test_sock");
unlink(server.sun_path);
sockaddr_len = strlen(server.sun_path) + sizeof(server.sun_family);
if (bind(server_sock, (struct sockaddr*)&server, sockaddr_len) == -1) {
return 1;
}
if (listen(server_sock, 3) == -1) {
return 1;
}
syslog(LOG_NOTICE, "Unix Socket up and running.");
/* Setup DB */
if (sqlite3_open("/tmp/hdbd.db", &db) != SQLITE_OK) {
syslog(LOG_ERR, "Failed to open the db. Error: %s\nProceeding to shutdown", sqlite3_errmsg(db));
sqlite3_close(db);
close(server_sock);
return 1;
}
char *sql = "CREATE TABLE IF NOT EXISTS hentai (url TEXT PRIMARY KEY NOT NULL, artist_name TEXT NOT NULL, website_name TEXT NOT NULL) STRICT;";
if (sqlite3_exec(db, sql, NULL, 0, &sql_err) != SQLITE_OK) {
syslog(LOG_ERR, "CREATE TABLE failed. Error: %s\nProceeding to shutdown", sql_err);
sqlite3_free(sql_err);
sqlite3_close(db);
close(server_sock);
return 1;
}
/* Actual program */
int client_sock;
int data_length = 0;
char receive_buf[1000] = {0};
char send_buf[1000] = {0};
char operation[1000] = {0};
char operand1[1000] = {0};
char operand2[1000] = {0};
char operand3[1000] = {0};
config.tmp_dir = "/tmp/hdbd";
if (config_parser() != 0) {
syslog(LOG_ERR, "Failed reading the users config. Shutting down...");
close(server_sock);
return 1;
}
while(1) {
client_sock = accept(server_sock, NULL, NULL);
if (client_sock == -1) {
syslog(LOG_WARNING, "Failed connecting to client. Error: '%s'", strerror(client_sock));
continue;
}
syslog(LOG_DEBUG, "Accepted a connection.");
data_length = read(client_sock, receive_buf, sizeof receive_buf);
if (data_length <= 0) {
syslog(LOG_WARNING, "Received no data from client.");
close(client_sock);
continue;
}
syslog(LOG_DEBUG, "Received from Client: '%s'", receive_buf);
parse_message(receive_buf, operation, operand1, operand2, operand3);
if (strcmp(operation, "shutdown") == 0) {
syslog(LOG_NOTICE, "Starting shutdown.");
strcpy(send_buf, "Shutting down daemon.");
send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock);
break;
}
if (operand1[0] == '\0') {
strcpy(send_buf, "operand1 is empty!");
write(client_sock, send_buf, strlen(send_buf));
syslog(LOG_DEBUG, "Closing connection.");
close(client_sock);
}
if (strcmp(operation, "dl") == 0) {
}
if (strcmp(operation, "add") == 0) {
}
if (strcmp(operation, "add_custom") == 0) {
}
if (strcmp(operation, "delete") == 0) {
}
if (strcmp(operation, "update") == 0) {
}
syslog(LOG_DEBUG, "Closing connection.");
close(client_sock);
}
close(server_sock);
syslog(LOG_NOTICE, "Bye Bye.");
return 0;
}

View File

@@ -1,302 +0,0 @@
#define _POSIX_C_SOURCE 200112L /* needed for struct addrinfo */
#include "db.h"
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <errno.h>
typedef struct {
char *tmp_dir;
char *data_dir;
char *gallery_dl_conf;
char *yt_dlp_conf;
} Config;
int h_config_parser(Config *config) {
char *config_location = malloc(10000);
char *user_name = getenv("USER");
if (user_name == NULL) {
fprintf(stderr, "Couldn't read env variable USER\n");
return 1;
}
sprintf(config_location, "/home/%s/.config/HDB.conf", user_name);
FILE *fptr = fopen(config_location, "r");
if (fptr == NULL) {
fprintf(stderr, "Failed to open the config %s\n", config_location);
return 1;
}
char *line = malloc(10000);
char *left = malloc(10000);
char *right = malloc(10000);
while(fgets(line, 10000, fptr) != NULL) {
line[strcspn(line, "\n#")] = '\0';
if (strcmp(line, "") == 0) {
continue;
}
if (
strchr(line, '=') == NULL
|| line[strcspn(line, "=") + 1] == ' '
|| line[strcspn(line, "=") - 1] == ' '
) {
fprintf(stderr, "Config line doesn't contain a valid assignment. line = '%s'\n", line);
continue;
}
/* TODO: support env variables */
if (strchr(line, '$') != NULL) {
fprintf(stderr, "Config doesn't support env variables. line = '%s'\n", line);
continue;
}
left = strtok(line, "=");
right = strtok(NULL, "\0");
if (strcmp(left, "data_dir") == 0) {
config->data_dir = malloc(10000);
memcpy(config->data_dir, right, strlen(right));
} else if (strcmp(left, "gallery-dl_conf") == 0) {
config->gallery_dl_conf = malloc(10000);
memcpy(config->gallery_dl_conf, right, strlen(right));
} else if (strcmp(left, "yt-dlp_conf") == 0) {
config->yt_dlp_conf = malloc(10000);
memcpy(config->yt_dlp_conf, right, strlen(right));
} else {
fprintf(stderr, "Unknown config option. left = '%s'\n", left);
}
}
fclose(fptr);
free(line);
free(config_location);
return 0;
}
int h_has_internet() {
int ret;
/* https://www.man7.org/linux/man-pages/man3/getaddrinfo.3.html */
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
struct addrinfo *res, *rp;
ret = getaddrinfo("hopeless-cloud.xyz", "80", 0, &res);
if (ret != 0) {
return 1;
}
int sockfd;
for (rp = res; rp != NULL; rp = rp->ai_next) {
/* https://www.man7.org/linux/man-pages/man2/socket.2.html */
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sockfd == -1) { continue; }
/* https://www.man7.org/linux/man-pages/man2/connect.2.html */
if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0) { break; }
close(sockfd);
}
freeaddrinfo(res);
if (rp == NULL) {
return 1;
}
/* https://www.man7.org/linux/man-pages/man2/write.2.html */
char *req_msg = "GET / HTTP/1.1\r\n\r\n";
if (write(sockfd, req_msg, strlen(req_msg)) != strlen(req_msg)) {
return 1;
}
/* https://www.man7.org/linux/man-pages/man2/recv.2.html */
size_t count = 1000000;
char buf[count];
if (recv(sockfd, buf, count-1, MSG_WAITALL) == -1 ) {
return 1;
}
close(sockfd);
return 0;
}
int h_download(char *line, Config config) {
int ret;
char *url = malloc(strlen(line));
char *artist_name = malloc(strlen(line));
char *website_name = malloc(strlen(line));
sscanf(line, "%s %s %s", url, artist_name, website_name);
char *args[7];
char *tmp = malloc(10000);
if (strstr(website_name, "Iwara") != NULL) {
sprintf(tmp, "%s/%s/%s/%(id)s.%(ext)s", config.data_dir, artist_name, website_name);
args[0] = "yt-dlp_linux";
args[1] = "--config-locations";
args[2] = config.yt_dlp_conf;
args[3] = "-o";
args[4] = tmp;
args[5] = url;
} else {
sprintf(tmp, "%s/%s/%s", config.data_dir, artist_name, website_name);
args[0] = "gallery-dl";
args[1] = "--config";
args[2] = config.gallery_dl_conf;
args[3] = "--directory";
args[4] = tmp;
args[5] = url;
}
args[6] = NULL;
printf("Starting download using %s\n", args[0]);
size_t i;
for (i = 5; i > 0; i--) {
/* Keep for later debugging or logging
size_t j;
for (j = 0; args[j] != NULL; j++) {
printf("args[%d] = %s\n", j, args[j]);
}
*/
pid_t pid = fork();
if (pid == -1) {
printf("Couldn't fork'\n");
return 1;
} else if (pid > 0) {
waitpid(pid, &ret, 0);
} else {
execvp(args[0], args);
}
if (ret == 0) {
break;
}
fprintf(stderr, "Command exited with non-zero code. code = %d\n", ret);
/* TODO: move contents of h_has_internet here */
ret = h_has_internet();
if (ret != 0) {
fprintf(stderr, "No internet\n");
return 1;
}
}
free(url);
free(artist_name);
free(website_name);
free(tmp);
return 0;
}
int main(int argc, char **argv) {
int ret;
Config config = { 0 };
config.tmp_dir = "/tmp/HDB";
ret = h_config_parser(&config);
if (ret != 0) {
fprintf(stderr, "Failed to parse config\n");
return 1;
}
if(mkdir(config.tmp_dir, 0777) && errno != EEXIST) {
fprintf(stderr, "'%s' %s\n", config.tmp_dir, strerror(errno));
return 1;
}
if (argc < 2) {
fprintf(stderr, "Incorrect amount of arguments\n");
return 1;
}
ret = h_db_open("HDB.db");
if (ret != 0) {
return 1;
}
if (strcmp(argv[1], "add") == 0) {
char *db_row = malloc(strlen(argv[2]) * 2);
memset(db_row, 0, strlen(argv[2]));
if (argc == 3) {
ret = h_db_add(db_row, argv[2]);
if (ret != 0) {
h_db_close();
return 1;
}
} else if (argc == 4) {
ret = h_db_add_custom_name(db_row, argv[2], argv[3]);
if (ret != 0) {
h_db_close();
return 1;
}
} else {
h_db_close();
fprintf(stderr, "Incorrect amount of arguments\n");
return 1;
}
printf("Successfully added to DB\n");
ret = h_download(db_row, config);
free(db_row);
if (ret != 0) {
printf("Failed download\n");
h_db_close();
return 1;
}
printf("Succesfully downladed\n");
} else if (strcmp(argv[1], "download_website") == 0) {
if (argc != 3) {
fprintf(stderr, "Incorrect amount of arguments\n");
h_db_close();
return 1;
}
char *file_name = malloc(10000);
sprintf(file_name, "%s/%s_generated.txt", config.tmp_dir, argv[2]);
FILE *fptr = fopen(file_name, "w+");
if (fptr == NULL) {
fprintf(stderr, "Couldn't open file '%s'\n", file_name);
h_db_close();
return 1;
}
ret = h_db_generate_file(fptr, argv[2]);
fclose(fptr);
if (ret != 0) {
h_db_close();
return 1;
}
fptr = fopen(file_name, "r");
if (fptr == NULL) {
fprintf(stderr, "Couldn't open file '%s'\n", file_name);
h_db_close();
return 1;
}
char *line = malloc(10000);
printf("Ready to download\n");
while(fgets(line, 10000, fptr) != NULL) {
ret = h_download(line, config);
if (ret != 0) {
fclose(fptr);
h_db_close();
return 1;
}
}
free(line);
fclose(fptr);
free(file_name);
} else {
fprintf(stderr, "Invalid argument\n");
h_db_close();
return 1;
}
h_db_close();
return 0;
}