2023-04-20 09:54:25 +02:00
|
|
|
/*
|
|
|
|
This is part of WHY2
|
|
|
|
Copyright (C) 2022 Václav Šmejkal
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2023-04-20 19:14:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <why2/chat/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2023-04-20 21:35:18 +02:00
|
|
|
#include <stdlib.h>
|
2023-08-26 18:40:52 +02:00
|
|
|
#include <string.h>
|
2023-08-26 18:12:26 +02:00
|
|
|
#include <sys/stat.h>
|
2023-04-20 19:14:13 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-04-20 21:23:53 +02:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include <why2/flags.h>
|
2023-04-20 21:26:33 +02:00
|
|
|
#include <why2/memory.h>
|
2023-04-20 21:35:18 +02:00
|
|
|
#include <why2/misc.h>
|
2023-04-20 19:14:13 +02:00
|
|
|
|
2023-08-26 18:40:52 +02:00
|
|
|
//LOCAL
|
2024-02-21 10:42:19 +01:00
|
|
|
enum CONFIG_TYPES
|
|
|
|
{
|
|
|
|
CLIENT,
|
|
|
|
SERVER
|
|
|
|
};
|
|
|
|
|
2023-08-26 18:40:52 +02:00
|
|
|
void init_config(char *filename)
|
2023-04-20 19:14:13 +02:00
|
|
|
{
|
2023-08-26 19:14:11 +02:00
|
|
|
struct stat st;
|
2023-08-27 11:05:11 +02:00
|
|
|
char *buffer = why2_replace(WHY2_CONFIG_DIR, "{USER}", getenv("USER"));
|
2023-08-26 19:14:11 +02:00
|
|
|
|
|
|
|
//CREATE USER CONFIG FOLDER [THIS SHOULDN'T HAPPEN ON CLIENT, BUT IT'S NEEDED ON FRESH SERVERS]
|
2023-08-27 11:05:11 +02:00
|
|
|
if (stat(buffer, &st) == -1)
|
2023-08-26 19:14:11 +02:00
|
|
|
{
|
2023-08-27 11:05:11 +02:00
|
|
|
mkdir(buffer, 0700);
|
2023-08-26 19:14:11 +02:00
|
|
|
}
|
|
|
|
|
2023-08-26 18:40:52 +02:00
|
|
|
//GET THE CONFIG TYPE
|
2023-08-27 11:05:11 +02:00
|
|
|
buffer = why2_realloc(buffer, strlen(WHY2_CHAT_CONFIG_DIR) + strlen(filename) + 2);
|
2023-08-26 18:40:52 +02:00
|
|
|
sprintf(buffer, "%s/%s", WHY2_CHAT_CONFIG_DIR, filename);
|
|
|
|
|
|
|
|
char *path = why2_replace(buffer, "{USER}", getenv("USER"));
|
2023-08-26 18:12:26 +02:00
|
|
|
|
|
|
|
if (access(path, R_OK) != 0) //CONFIG DOESN'T EXIST
|
2023-04-20 19:14:13 +02:00
|
|
|
{
|
2023-08-26 18:12:26 +02:00
|
|
|
char *config_dir = why2_replace(WHY2_CHAT_CONFIG_DIR, "{USER}", getenv("USER"));
|
|
|
|
|
2023-08-26 18:40:52 +02:00
|
|
|
//CREATE CONFIG DIRECTORY
|
2023-08-26 18:12:26 +02:00
|
|
|
mkdir(config_dir, 0700);
|
|
|
|
|
2023-04-20 21:23:53 +02:00
|
|
|
CURL *curl = curl_easy_init();
|
2023-04-20 21:35:18 +02:00
|
|
|
FILE *file_buffer = why2_fopen(path, "w+");
|
2023-04-20 21:23:53 +02:00
|
|
|
|
2023-08-26 18:40:52 +02:00
|
|
|
buffer = why2_realloc(buffer, strlen(WHY2_CHAT_CONFIG_URL) + strlen(filename) + 1);
|
|
|
|
sprintf(buffer, "%s%s", WHY2_CHAT_CONFIG_URL, filename);
|
|
|
|
|
|
|
|
//DOWNLOAD CONTENT
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, buffer);
|
2023-04-20 21:35:18 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file_buffer);
|
2023-04-20 21:23:53 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, WHY2_CURL_TIMEOUT);
|
|
|
|
curl_easy_perform(curl);
|
|
|
|
|
|
|
|
//CLEANUP
|
|
|
|
curl_easy_cleanup(curl);
|
2023-08-26 18:40:52 +02:00
|
|
|
why2_deallocate(buffer);
|
2023-04-20 21:35:18 +02:00
|
|
|
why2_deallocate(path);
|
2023-08-26 18:12:26 +02:00
|
|
|
why2_deallocate(config_dir);
|
2023-04-20 21:35:18 +02:00
|
|
|
why2_deallocate(file_buffer);
|
2023-04-20 19:14:13 +02:00
|
|
|
}
|
2023-08-26 18:40:52 +02:00
|
|
|
}
|
|
|
|
|
2024-02-21 10:42:19 +01:00
|
|
|
char *config(char *key, enum CONFIG_TYPES type)
|
|
|
|
{
|
2024-02-21 10:48:48 +01:00
|
|
|
char *path = NULL;
|
2024-02-21 10:42:19 +01:00
|
|
|
|
2024-02-21 10:48:48 +01:00
|
|
|
switch (type) //GET path
|
2024-02-21 10:42:19 +01:00
|
|
|
{
|
|
|
|
case CLIENT:
|
|
|
|
path = why2_replace(WHY2_CHAT_CONFIG_DIR "/" WHY2_CHAT_CONFIG_CLIENT, "{USER}", getenv("USER"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SERVER:
|
|
|
|
path = why2_replace(WHY2_CHAT_CONFIG_DIR "/" WHY2_CHAT_CONFIG_SERVER, "{USER}", getenv("USER"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
why2_die("CONFIG_TYPE not implemented!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//VALUE
|
|
|
|
char *value = why2_toml_read(path, key);
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
why2_deallocate(path);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-08-26 18:40:52 +02:00
|
|
|
//GLOBAL
|
|
|
|
void why2_chat_init_server_config(void)
|
|
|
|
{
|
2023-08-26 18:57:53 +02:00
|
|
|
init_config(WHY2_CHAT_CONFIG_SERVER);
|
2023-08-26 18:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void why2_chat_init_client_config(void)
|
|
|
|
{
|
2023-08-26 18:57:53 +02:00
|
|
|
init_config(WHY2_CHAT_CONFIG_CLIENT);
|
2024-02-21 10:42:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char *why2_chat_server_config(char *key)
|
|
|
|
{
|
|
|
|
return config(key, SERVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *why2_chat_client_config(char *key)
|
|
|
|
{
|
|
|
|
return config(key, CLIENT);
|
2023-04-20 19:14:13 +02:00
|
|
|
}
|