1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Merge pull request #1648 from profanity-im/refactor-editor

Refactor editor & some other parts
This commit is contained in:
Michael Vetter 2022-03-14 16:02:09 +01:00 committed by GitHub
commit 4e95641014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 144 additions and 268 deletions

View File

@ -3004,7 +3004,7 @@ command_mangen(void)
cmds = g_list_insert_sorted(cmds, pcmd, (GCompareFunc)_cmp_command);
}
mkdir_recursive("docs");
create_dir("docs");
GDateTime* now = g_date_time_new_now_local();
gchar* date = g_date_time_format(now, "%F");

View File

@ -71,43 +71,15 @@ struct curl_data_t
static size_t _data_callback(void* ptr, size_t size, size_t nmemb, void* data);
gboolean
create_dir(char* name)
create_dir(const char* name)
{
struct stat sb;
if (stat(name, &sb) != 0) {
if (errno != ENOENT || mkdir(name, S_IRWXU) != 0) {
if (g_mkdir_with_parents(name, S_IRWXU) != 0) {
log_error("Failed to create directory at '%s' with error '%s'", name, strerror(errno));
return FALSE;
}
} else {
if ((sb.st_mode & S_IFDIR) != S_IFDIR) {
log_debug("create_dir: %s exists and is not a directory!", name);
return FALSE;
}
}
return TRUE;
}
gboolean
mkdir_recursive(const char* dir)
{
gboolean result = TRUE;
for (int i = 1; i <= strlen(dir); i++) {
if (dir[i] == '/' || dir[i] == '\0') {
gchar* next_dir = g_strndup(dir, i);
result = create_dir(next_dir);
g_free(next_dir);
if (!result) {
break;
}
}
}
return result;
}
gboolean
copy_file(const char* const sourcepath, const char* const targetpath, const gboolean overwrite_existing)
{

View File

@ -80,8 +80,7 @@ typedef enum {
RESOURCE_XA
} resource_presence_t;
gboolean create_dir(char* name);
gboolean mkdir_recursive(const char* dir);
gboolean create_dir(const char* name);
gboolean copy_file(const char* const src, const char* const target, const gboolean overwrite_existing);
char* str_replace(const char* string, const char* substr, const char* replacement);
gboolean strtoi_range(char* str, int* saveptr, int min, int max, char** err_msg);

View File

@ -38,6 +38,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h>
#include "common.h"
@ -65,19 +67,19 @@ files_create_directories(void)
GString* plugins_dir = g_string_new(xdg_data);
g_string_append(plugins_dir, "/profanity/plugins");
if (!mkdir_recursive(themes_dir->str)) {
if (!create_dir(themes_dir->str)) {
log_error("Error while creating directory %s", themes_dir->str);
}
if (!mkdir_recursive(icons_dir->str)) {
if (!create_dir(icons_dir->str)) {
log_error("Error while creating directory %s", icons_dir->str);
}
if (!mkdir_recursive(chatlogs_dir->str)) {
if (!create_dir(chatlogs_dir->str)) {
log_error("Error while creating directory %s", chatlogs_dir->str);
}
if (!mkdir_recursive(logs_dir->str)) {
if (!create_dir(logs_dir->str)) {
log_error("Error while creating directory %s", logs_dir->str);
}
if (!mkdir_recursive(plugins_dir->str)) {
if (!create_dir(plugins_dir->str)) {
log_error("Error while creating directory %s", plugins_dir->str);
}
@ -120,7 +122,7 @@ files_get_log_file(const char* const log_file)
if (log_file) {
gchar* log_path = g_path_get_dirname(log_file);
if (!mkdir_recursive(log_path)) {
if (!create_dir(log_path)) {
log_error("Error while creating directory %s", log_path);
}
g_free(log_path);
@ -149,13 +151,8 @@ gchar*
files_get_config_path(const char* const config_base)
{
gchar* xdg_config = _files_get_xdg_config_home();
GString* file_str = g_string_new(xdg_config);
g_string_append(file_str, "/profanity/");
g_string_append(file_str, config_base);
gchar* result = g_strdup(file_str->str);
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_config, config_base);
g_free(xdg_config);
g_string_free(file_str, TRUE);
return result;
}
@ -163,13 +160,8 @@ gchar*
files_get_data_path(const char* const data_base)
{
gchar* xdg_data = _files_get_xdg_data_home();
GString* file_str = g_string_new(xdg_data);
g_string_append(file_str, "/profanity/");
g_string_append(file_str, data_base);
gchar* result = g_strdup(file_str->str);
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_data, data_base);
g_free(xdg_data);
g_string_free(file_str, TRUE);
return result;
}
@ -177,20 +169,34 @@ gchar*
files_get_account_data_path(const char* const specific_dir, const char* const jid)
{
gchar* data_dir = files_get_data_path(specific_dir);
GString* result_dir = g_string_new(data_dir);
gchar* account_dir = str_replace(jid, "@", "_at_");
gchar* result = g_strdup_printf("%s/%s", data_dir, account_dir);
g_free(account_dir);
g_free(data_dir);
gchar* account_dir = str_replace(jid, "@", "_at_");
g_string_append(result_dir, "/");
g_string_append(result_dir, account_dir);
g_free(account_dir);
gchar* result = g_strdup(result_dir->str);
g_string_free(result_dir, TRUE);
return result;
}
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);
if (!create_dir(data_path)) {
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;
}
static char*
_files_get_xdg_config_home(void)
{

View File

@ -69,4 +69,7 @@ gchar* files_get_account_data_path(const char* const specific_dir, const char* c
gchar* files_get_log_file(const char* const log_file);
gchar* files_get_inputrc_file(void);
gchar*
files_file_in_account_data_path(const char* const specific_dir, const char* const jid, const char* const file_name);
#endif

View File

@ -56,27 +56,7 @@ static prof_msg_type_t _get_message_type_type(const char* const type);
static char*
_get_db_filename(ProfAccount* account)
{
gchar* database_dir = files_get_account_data_path(DIR_DATABASE, account->jid);
int res = g_mkdir_with_parents(database_dir, S_IRWXU);
if (res == -1) {
const char* errmsg = strerror(errno);
if (errmsg) {
log_error("DATABASE: error creating directory: %s, %s", database_dir, errmsg);
} else {
log_error("DATABASE: creating directory: %s", database_dir);
}
g_free(database_dir);
return NULL;
}
GString* chatlog_filename = g_string_new(database_dir);
g_string_append(chatlog_filename, "/chatlog.db");
gchar* result = g_strdup(chatlog_filename->str);
g_string_free(chatlog_filename, TRUE);
g_free(database_dir);
return result;
return files_file_in_account_data_path(DIR_DATABASE, account->jid, "chatlog.db");
}
gboolean

View File

@ -90,9 +90,7 @@ static struct dated_chat_log* _create_log(const char* const other, const char* c
static struct dated_chat_log* _create_groupchat_log(const char* const room, const char* const login);
static void _free_chat_log(struct dated_chat_log* dated_log);
static gboolean _key_equals(void* key1, void* key2);
static char* _get_log_filename(const char* const other, const char* const login, GDateTime* dt, gboolean create);
static char* _get_groupchat_log_filename(const char* const room, const char* const login, GDateTime* dt,
gboolean create);
static char* _get_log_filename(const char* const other, const char* const login, GDateTime* dt, gboolean is_room);
static void _rotate_log_file(void);
static char* _log_string_from_level(log_level_t level);
static void _chat_log_chat(const char* const login, const char* const other, const gchar* const msg,
@ -612,7 +610,7 @@ static struct dated_chat_log*
_create_log(const char* const other, const char* const login)
{
GDateTime* now = g_date_time_new_now_local();
char* filename = _get_log_filename(other, login, now, TRUE);
char* filename = _get_log_filename(other, login, now, FALSE);
struct dated_chat_log* new_log = malloc(sizeof(struct dated_chat_log));
new_log->filename = strdup(filename);
@ -627,7 +625,7 @@ static struct dated_chat_log*
_create_groupchat_log(const char* const room, const char* const login)
{
GDateTime* now = g_date_time_new_now_local();
char* filename = _get_groupchat_log_filename(room, login, now, TRUE);
char* filename = _get_log_filename(room, login, now, TRUE);
struct dated_chat_log* new_log = malloc(sizeof(struct dated_chat_log));
new_log->filename = strdup(filename);
@ -677,70 +675,16 @@ _key_equals(void* key1, void* key2)
}
static char*
_get_log_filename(const char* const other, const char* const login, GDateTime* dt, gboolean create)
_get_log_filename(const char* const other, const char* const login, GDateTime* dt, gboolean is_room)
{
char* chatlogs_dir = files_get_data_path(DIR_CHATLOGS);
GString* log_file = g_string_new(chatlogs_dir);
free(chatlogs_dir);
gchar* chatlogs_dir = files_file_in_account_data_path(DIR_CHATLOGS, login, is_room ? "rooms" : NULL);
gchar* logfile_name = g_date_time_format(dt, "%Y_%m_%d.log");
gchar* logfile_path = files_file_in_account_data_path(chatlogs_dir, other, logfile_name);
gchar* login_dir = str_replace(login, "@", "_at_");
g_string_append_printf(log_file, "/%s", login_dir);
if (create) {
create_dir(log_file->str);
}
free(login_dir);
g_free(logfile_name);
g_free(chatlogs_dir);
gchar* other_file = str_replace(other, "@", "_at_");
g_string_append_printf(log_file, "/%s", other_file);
if (create) {
create_dir(log_file->str);
}
free(other_file);
gchar* date = g_date_time_format(dt, "/%Y_%m_%d.log");
g_string_append(log_file, date);
g_free(date);
char* result = strdup(log_file->str);
g_string_free(log_file, TRUE);
return result;
}
static char*
_get_groupchat_log_filename(const char* const room, const char* const login, GDateTime* dt, gboolean create)
{
char* chatlogs_dir = files_get_data_path(DIR_CHATLOGS);
GString* log_file = g_string_new(chatlogs_dir);
free(chatlogs_dir);
gchar* login_dir = str_replace(login, "@", "_at_");
g_string_append_printf(log_file, "/%s", login_dir);
if (create) {
create_dir(log_file->str);
}
free(login_dir);
g_string_append(log_file, "/rooms");
if (create) {
create_dir(log_file->str);
}
gchar* room_file = str_replace(room, "@", "_at_");
g_string_append_printf(log_file, "/%s", room_file);
if (create) {
create_dir(log_file->str);
}
free(room_file);
gchar* date = g_date_time_format(dt, "/%Y_%m_%d.log");
g_string_append(log_file, date);
g_free(date);
char* result = strdup(log_file->str);
g_string_free(log_file, TRUE);
return result;
return logfile_path;
}
static char*

View File

@ -231,7 +231,11 @@ omemo_on_connect(ProfAccount* account)
omemo_ctx.device_list_handler = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
omemo_ctx.known_devices = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_g_hash_table_free);
gchar* omemo_dir = files_get_account_data_path(DIR_OMEMO, account->jid);
gchar* omemo_dir = files_file_in_account_data_path(DIR_OMEMO, account->jid, NULL);
if (!omemo_dir) {
log_error("[OMEMO] failed creating directory");
return;
}
omemo_ctx.identity_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.identity_filename, "/identity.txt");
@ -242,17 +246,6 @@ omemo_on_connect(ProfAccount* account)
omemo_ctx.known_devices_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.known_devices_filename, "/known_devices.txt");
errno = 0;
int res = g_mkdir_with_parents(omemo_dir, S_IRWXU);
if (res == -1) {
const char* errmsg = strerror(errno);
if (errmsg) {
log_error("[OMEMO] error creating directory: %s, %s", omemo_dir, errmsg);
} else {
log_error("[OMEMO] creating directory: %s", omemo_dir);
}
}
g_free(omemo_dir);
omemo_devicelist_subscribe();

View File

@ -129,19 +129,20 @@ static void
cb_write_fingerprints(void* opdata)
{
gcry_error_t err = 0;
gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid);
GString* fpsfilename = g_string_new(otr_dir);
g_string_append(fpsfilename, "/fingerprints.txt");
err = otrl_privkey_write_fingerprints(user_state, fpsfilename->str);
if (err != GPG_ERR_NO_ERROR) {
log_error("Failed to write fingerprints file");
gchar* fpsfilename = files_file_in_account_data_path(DIR_OTR, jid, "fingerprints.txt");
if (!fpsfilename) {
log_error("Failed to create fingerprints file");
cons_show_error("Failed to create fingerprints file");
return;
}
g_free(otr_dir);
g_string_free(fpsfilename, TRUE);
err = otrl_privkey_write_fingerprints(user_state, fpsfilename);
if (err != GPG_ERR_NO_ERROR) {
log_error("Failed to write fingerprints file");
cons_show_error("Failed to write fingerprints file");
}
g_free(fpsfilename);
}
static void
@ -212,12 +213,10 @@ otr_on_connect(ProfAccount* account)
jid = strdup(account->jid);
log_info("Loading OTR key for %s", jid);
gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid);
if (!mkdir_recursive(otr_dir)) {
log_error("Could not create %s for account %s.", otr_dir, jid);
cons_show_error("Could not create %s for account %s.", otr_dir, jid);
g_free(otr_dir);
gchar* otr_dir = files_file_in_account_data_path(DIR_OTR, jid, NULL);
if (!otr_dir) {
log_error("Could not create directory for account %s.", jid);
cons_show_error("Could not create directory for account %s.", jid);
return;
}
@ -381,12 +380,11 @@ otr_keygen(ProfAccount* account)
jid = strdup(account->jid);
log_info("Generating OTR key for %s", jid);
gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid);
gchar* otr_dir = files_file_in_account_data_path(DIR_OTR, jid, NULL);
if (!mkdir_recursive(otr_dir)) {
log_error("Could not create %s for account %s.", otr_dir, jid);
cons_show_error("Could not create %s for account %s.", otr_dir, jid);
g_free(otr_dir);
if (!otr_dir) {
log_error("Could not create directory for account %s.", jid);
cons_show_error("Could not create directory for account %s.", jid);
return;
}

View File

@ -161,26 +161,12 @@ p_gpg_close(void)
void
p_gpg_on_connect(const char* const barejid)
{
gchar* pubsfile = files_get_account_data_path(DIR_PGP, barejid);
// mkdir if doesn't exist for account
errno = 0;
int res = g_mkdir_with_parents(pubsfile, S_IRWXU);
if (res == -1) {
const char* errmsg = strerror(errno);
if (errmsg) {
log_error("Error creating directory: %s, %s", pubsfile, errmsg);
} else {
log_error("Error creating directory: %s", pubsfile);
pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
if (!pubsloc) {
log_error("Could not create directory for account %s.", barejid);
cons_show_error("Could not create directory for account %s.", barejid);
return;
}
}
// create or read publickeys
GString* pubtmp = g_string_new(pubsfile);
g_string_append(pubtmp, "/pubkeys");
pubsloc = pubtmp->str;
g_string_free(pubtmp, FALSE);
g_free(pubsfile);
if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
g_chmod(pubsloc, S_IRUSR | S_IWUSR);

View File

@ -4,6 +4,7 @@
*
* Copyright (C) 2022 Michael Vetter <jubalh@iodoru.org>
* Copyright (C) 2022 MarcoPolo PasTonMolo <marcopolopastonmolo@protonmail.com>
* Copyright (C) 2022 Steffen Jaeckel <jaeckel-floss@eyet-services.de>
*
* This file is part of Profanity.
*
@ -35,97 +36,87 @@
*/
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <glib.h>
#include <gio/gio.h>
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
#include "config/files.h"
#include "config/preferences.h"
#include "ui/ui.h"
#include "log.h"
// Returns true if an error occurred
gboolean
get_message_from_editor(gchar* message, gchar** returned_message)
{
// create editor dir if not present
/* Make sure that there's no junk in the return-pointer in error cases */
*returned_message = NULL;
gchar* filename = NULL;
GError* glib_error = NULL;
char* jid = connection_get_barejid();
gchar* path = files_get_account_data_path(DIR_EDITOR, jid);
free(jid);
if (g_mkdir_with_parents(path, S_IRWXU) != 0) {
cons_show_error("Failed to create directory at '%s' with error '%s'", path, strerror(errno));
g_free(path);
if (jid) {
filename = files_file_in_account_data_path(DIR_EDITOR, jid, "compose.md");
} else {
log_debug("[Editor] could not get JID");
gchar* data_dir = files_get_data_path(DIR_EDITOR);
if (!create_dir(data_dir)) {
return TRUE;
}
filename = g_strdup_printf("%s/compose.md", data_dir);
g_free(data_dir);
}
if (!filename) {
log_error("[Editor] something went wrong while creating compose file");
return TRUE;
}
// build temp file name. Example: /home/user/.local/share/profanity/editor/jid/compose.md
char* filename = g_strdup_printf("%s/compose.md", path);
g_free(path);
GError* creation_error = NULL;
GFile* file = g_file_new_for_path(filename);
GFileOutputStream* fos = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, &creation_error);
free(filename);
if (message != NULL && strlen(message) > 0) {
int fd_output_file = open(g_file_get_path(file), O_WRONLY);
if (fd_output_file < 0) {
cons_show_error("Editor: Could not open file '%s': %s", file, strerror(errno));
gsize messagelen = strlen(message);
if (!g_file_set_contents(filename, message, messagelen, &glib_error)) {
log_error("[Editor] could not write to %s: %s", filename, glib_error ? glib_error->message : "No GLib error given");
if (glib_error) {
g_error_free(glib_error);
}
g_free(filename);
return TRUE;
}
if (-1 == write(fd_output_file, message, strlen(message))) {
cons_show_error("Editor: failed to write '%s' to file: %s", message, strerror(errno));
return TRUE;
}
close(fd_output_file);
}
if (creation_error) {
cons_show_error("Editor: could not create temp file");
return TRUE;
}
g_object_unref(fos);
char* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
// Fork / exec
pid_t pid = fork();
if (pid == 0) {
int x = execlp(editor, editor, g_file_get_path(file), (char*)NULL);
int x = execlp(editor, editor, filename, (char*)NULL);
if (x == -1) {
cons_show_error("Editor:Failed to exec %s", editor);
log_error("[Editor] Failed to exec %s", editor);
}
_exit(EXIT_FAILURE);
} else {
if (pid == -1) {
return TRUE;
}
int status = 0;
waitpid(pid, &status, 0);
int fd_input_file = open(g_file_get_path(file), O_RDONLY);
const size_t COUNT = 8192;
char buf[COUNT];
ssize_t size_read = read(fd_input_file, buf, COUNT);
if (size_read > 0 && size_read <= COUNT) {
buf[size_read - 1] = '\0';
GString* text = g_string_new(buf);
*returned_message = g_strdup(text->str);
g_string_free(text, TRUE);
} else {
*returned_message = g_strdup("");
}
close(fd_input_file);
waitpid(pid, NULL, 0);
GError* deletion_error = NULL;
g_file_delete(file, NULL, &deletion_error);
if (deletion_error) {
cons_show("Editor: error during file deletion");
g_free(*returned_message);
gchar* contents;
gsize length;
if (!g_file_get_contents(filename, &contents, &length, &glib_error)) {
log_error("[Editor] could not read from %s: %s", filename, glib_error ? glib_error->message : "No GLib error given");
if (glib_error) {
g_error_free(glib_error);
}
g_free(filename);
g_free(editor);
return TRUE;
}
g_object_unref(file);
/* Remove all trailing new-line characters */
g_strchomp(contents);
*returned_message = contents;
if (remove(filename) != 0) {
log_error("[Editor] error during file deletion of %s", filename);
} else {
log_debug("[Editor] deleted file: %s", filename);
}
g_free(filename);
}
g_free(editor);

View File

@ -487,7 +487,7 @@ _inp_rl_startup_hook(void)
rl_bind_keyseq("\\ea", _inp_rl_win_next_unread_handler);
rl_bind_keyseq("\\ev", _inp_rl_win_attention_handler);
rl_bind_keyseq("\\em", _inp_rl_win_attention_next_handler);
rl_bind_keyseq("\\ee", _inp_rl_send_to_editor);
rl_bind_keyseq("\\ec", _inp_rl_send_to_editor);
rl_bind_keyseq("\\e\\e[5~", _inp_rl_subwin_pageup_handler);
rl_bind_keyseq("\\e[5;3~", _inp_rl_subwin_pageup_handler);

View File

@ -747,6 +747,8 @@ connection_get_ctx(void)
const char*
connection_get_fulljid(void)
{
if (!conn.xmpp_conn)
return NULL;
const char* jid = xmpp_conn_get_bound_jid(conn.xmpp_conn);
if (jid) {
return jid;
@ -759,10 +761,11 @@ char*
connection_get_barejid(void)
{
const char* jid = connection_get_fulljid();
char* result;
if (!jid)
return NULL;
Jid* jidp = jid_create(jid);
result = strdup(jidp->barejid);
char* result = strdup(jidp->barejid);
jid_destroy(jidp);
return result;
@ -772,8 +775,9 @@ char*
connection_get_user(void)
{
const char* jid = connection_get_fulljid();
char* result;
result = strdup(jid);
if (!jid)
return NULL;
char* result = strdup(jid);
char* split = strchr(result, '@');
*split = '\0';

View File

@ -16,7 +16,7 @@ void
create_config_dir(void** state)
{
setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1);
if (!mkdir_recursive("./tests/files/xdg_config_home/profanity")) {
if (!create_dir("./tests/files/xdg_config_home/profanity")) {
assert_true(FALSE);
}
}
@ -32,7 +32,7 @@ void
create_data_dir(void** state)
{
setenv("XDG_DATA_HOME", "./tests/files/xdg_data_home", 1);
if (!mkdir_recursive("./tests/files/xdg_data_home/profanity")) {
if (!create_dir("./tests/files/xdg_data_home/profanity")) {
assert_true(FALSE);
}
}