2016-07-24 11:22:15 -04:00
|
|
|
/*
|
|
|
|
* files.c
|
2019-11-13 06:11:05 -05:00
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
2016-07-24 11:22:15 -04:00
|
|
|
*
|
2019-01-22 05:31:45 -05:00
|
|
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
2023-01-10 04:37:25 -05:00
|
|
|
* Copyright (C) 2020 - 2023 Michael Vetter <jubalh@idoru.org>
|
2016-07-24 11:22:15 -04:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity 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.
|
|
|
|
*
|
|
|
|
* Profanity 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 Profanity. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-07-24 16:35:12 -04:00
|
|
|
#include <unistd.h>
|
2022-03-13 06:58:56 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2020-07-07 07:53:30 -04:00
|
|
|
#include <glib.h>
|
2016-07-24 11:22:15 -04:00
|
|
|
|
|
|
|
#include "common.h"
|
2020-07-07 07:53:30 -04:00
|
|
|
#include "log.h"
|
2016-07-24 11:22:15 -04:00
|
|
|
#include "config/files.h"
|
2016-07-24 16:35:12 -04:00
|
|
|
#include "config/preferences.h"
|
|
|
|
|
|
|
|
static char* _files_get_xdg_config_home(void);
|
|
|
|
static char* _files_get_xdg_data_home(void);
|
2016-07-24 11:22:15 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
files_create_directories(void)
|
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_config = _files_get_xdg_config_home();
|
|
|
|
gchar* xdg_data = _files_get_xdg_data_home();
|
2016-07-24 11:22:15 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* themes_dir = g_string_new(xdg_config);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(themes_dir, "/profanity/themes");
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* icons_dir = g_string_new(xdg_config);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(icons_dir, "/profanity/icons");
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* chatlogs_dir = g_string_new(xdg_data);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(chatlogs_dir, "/profanity/chatlogs");
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* logs_dir = g_string_new(xdg_data);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(logs_dir, "/profanity/logs");
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* plugins_dir = g_string_new(xdg_data);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(plugins_dir, "/profanity/plugins");
|
|
|
|
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(themes_dir->str)) {
|
2016-07-24 11:22:15 -04:00
|
|
|
log_error("Error while creating directory %s", themes_dir->str);
|
|
|
|
}
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(icons_dir->str)) {
|
2016-07-24 11:22:15 -04:00
|
|
|
log_error("Error while creating directory %s", icons_dir->str);
|
|
|
|
}
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(chatlogs_dir->str)) {
|
2016-07-24 11:22:15 -04:00
|
|
|
log_error("Error while creating directory %s", chatlogs_dir->str);
|
|
|
|
}
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(logs_dir->str)) {
|
2016-07-24 11:22:15 -04:00
|
|
|
log_error("Error while creating directory %s", logs_dir->str);
|
|
|
|
}
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(plugins_dir->str)) {
|
2016-07-24 11:22:15 -04:00
|
|
|
log_error("Error while creating directory %s", plugins_dir->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(themes_dir, TRUE);
|
|
|
|
g_string_free(icons_dir, TRUE);
|
|
|
|
g_string_free(chatlogs_dir, TRUE);
|
|
|
|
g_string_free(logs_dir, TRUE);
|
|
|
|
g_string_free(plugins_dir, TRUE);
|
|
|
|
|
|
|
|
g_free(xdg_config);
|
|
|
|
g_free(xdg_data);
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:12:21 -04:00
|
|
|
gchar*
|
2016-07-24 16:35:12 -04:00
|
|
|
files_get_inputrc_file(void)
|
2016-07-24 12:12:09 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_config = _files_get_xdg_config_home();
|
|
|
|
GString* inputrc_file = g_string_new(xdg_config);
|
2016-07-24 12:12:09 -04:00
|
|
|
g_free(xdg_config);
|
|
|
|
|
|
|
|
g_string_append(inputrc_file, "/profanity/inputrc");
|
|
|
|
|
|
|
|
if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) {
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* result = g_strdup(inputrc_file->str);
|
2016-07-24 12:12:09 -04:00
|
|
|
g_string_free(inputrc_file, TRUE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(inputrc_file, TRUE);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-24 16:35:12 -04:00
|
|
|
char*
|
2020-07-07 08:18:57 -04:00
|
|
|
files_get_log_file(const char* const log_file)
|
2016-07-24 16:35:12 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_data = _files_get_xdg_data_home();
|
2020-12-11 04:40:10 -05:00
|
|
|
GString* logfile;
|
2020-02-21 15:10:00 -05:00
|
|
|
|
|
|
|
if (log_file) {
|
2020-12-11 04:40:10 -05:00
|
|
|
gchar* log_path = g_path_get_dirname(log_file);
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(log_path)) {
|
2020-12-11 04:40:10 -05:00
|
|
|
log_error("Error while creating directory %s", log_path);
|
|
|
|
}
|
|
|
|
g_free(log_path);
|
|
|
|
|
|
|
|
logfile = g_string_new(log_file);
|
2020-02-21 15:10:00 -05:00
|
|
|
} else {
|
2020-12-11 04:40:10 -05:00
|
|
|
logfile = g_string_new(xdg_data);
|
2020-02-21 15:10:00 -05:00
|
|
|
g_string_append(logfile, "/profanity/logs/profanity");
|
|
|
|
|
2020-12-11 04:40:10 -05:00
|
|
|
if (!prefs_get_boolean(PREF_LOG_SHARED)) {
|
|
|
|
g_string_append_printf(logfile, "%d", getpid());
|
|
|
|
}
|
2020-02-21 15:10:00 -05:00
|
|
|
|
2020-12-11 04:40:10 -05:00
|
|
|
g_string_append(logfile, ".log");
|
|
|
|
}
|
2020-02-21 15:10:00 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* result = g_strdup(logfile->str);
|
2020-02-21 15:10:00 -05:00
|
|
|
|
2016-07-24 16:35:12 -04:00
|
|
|
free(xdg_data);
|
|
|
|
g_string_free(logfile, TRUE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:12:21 -04:00
|
|
|
gchar*
|
2020-07-07 08:18:57 -04:00
|
|
|
files_get_config_path(const char* const config_base)
|
2016-07-24 16:35:12 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_config = _files_get_xdg_config_home();
|
2022-03-13 07:18:03 -04:00
|
|
|
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_config, config_base);
|
2016-07-24 16:35:12 -04:00
|
|
|
g_free(xdg_config);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:12:21 -04:00
|
|
|
gchar*
|
2020-07-07 08:18:57 -04:00
|
|
|
files_get_data_path(const char* const data_base)
|
2016-07-24 16:35:12 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_data = _files_get_xdg_data_home();
|
2022-03-13 07:18:03 -04:00
|
|
|
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_data, data_base);
|
2016-07-24 16:35:12 -04:00
|
|
|
g_free(xdg_data);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:05:45 -04:00
|
|
|
gchar*
|
2020-07-07 08:18:57 -04:00
|
|
|
files_get_account_data_path(const char* const specific_dir, const char* const jid)
|
2020-07-01 04:05:45 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* data_dir = files_get_data_path(specific_dir);
|
|
|
|
gchar* account_dir = str_replace(jid, "@", "_at_");
|
2020-07-01 04:05:45 -04:00
|
|
|
|
2022-03-13 07:18:03 -04:00
|
|
|
gchar* result = g_strdup_printf("%s/%s", data_dir, account_dir);
|
|
|
|
|
|
|
|
g_free(account_dir);
|
|
|
|
g_free(data_dir);
|
2020-07-01 04:05:45 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-13 06:58:56 -04:00
|
|
|
gchar*
|
|
|
|
files_file_in_account_data_path(const char* const specific_dir, const char* const jid, const char* const file_name)
|
|
|
|
{
|
|
|
|
gchar* data_path = files_get_account_data_path(specific_dir, jid);
|
|
|
|
|
2022-03-13 07:02:59 -04:00
|
|
|
if (!create_dir(data_path)) {
|
2022-03-13 06:58:56 -04:00
|
|
|
g_free(data_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_name) {
|
|
|
|
return data_path;
|
|
|
|
}
|
|
|
|
gchar* filename = g_strdup_printf("%s/%s", data_path, file_name);
|
|
|
|
g_free(data_path);
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2016-07-24 16:35:12 -04:00
|
|
|
static char*
|
|
|
|
_files_get_xdg_config_home(void)
|
2016-07-24 11:22:15 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_config_home_env = getenv("XDG_CONFIG_HOME");
|
|
|
|
gchar* xdg_config_home = NULL;
|
2020-06-12 04:23:31 -04:00
|
|
|
|
|
|
|
if (xdg_config_home_env) {
|
|
|
|
xdg_config_home = strdup(xdg_config_home_env);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_strstrip(xdg_config_home);
|
2020-06-12 04:23:31 -04:00
|
|
|
}
|
2016-07-24 11:22:15 -04:00
|
|
|
|
|
|
|
if (xdg_config_home && (strcmp(xdg_config_home, "") != 0)) {
|
2020-06-12 04:23:31 -04:00
|
|
|
return xdg_config_home;
|
2016-07-24 11:22:15 -04:00
|
|
|
} else {
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* default_path = g_string_new(getenv("HOME"));
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(default_path, "/.config");
|
2020-07-07 08:18:57 -04:00
|
|
|
char* result = strdup(default_path->str);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_free(default_path, TRUE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 16:35:12 -04:00
|
|
|
static char*
|
|
|
|
_files_get_xdg_data_home(void)
|
2016-07-24 11:22:15 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* xdg_data_home_env = getenv("XDG_DATA_HOME");
|
|
|
|
gchar* xdg_data_home = NULL;
|
2020-06-12 04:23:31 -04:00
|
|
|
|
|
|
|
if (xdg_data_home_env) {
|
|
|
|
xdg_data_home = strdup(xdg_data_home_env);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_strstrip(xdg_data_home);
|
2020-06-12 04:23:31 -04:00
|
|
|
}
|
2016-07-24 11:22:15 -04:00
|
|
|
|
|
|
|
if (xdg_data_home && (strcmp(xdg_data_home, "") != 0)) {
|
2020-06-12 04:23:31 -04:00
|
|
|
return xdg_data_home;
|
2016-07-24 11:22:15 -04:00
|
|
|
} else {
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* default_path = g_string_new(getenv("HOME"));
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_append(default_path, "/.local/share");
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* result = strdup(default_path->str);
|
2016-07-24 11:22:15 -04:00
|
|
|
g_string_free(default_path, TRUE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|