about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2021-10-22 12:38:39 +0200
committerGitHub <noreply@github.com>2021-10-22 12:38:39 +0200
commit59063aa3b43cd973ab328b406f1ab40dcf09b41a (patch)
tree59efb04637298ba2b34bfb7351b40823b02c062c
parent37e1699c84bfb34dafaee4cd061eedad79fc114d (diff)
parentda3a6d87437ce14215acbb5f1740cb81066df39d (diff)
downloadprofani-tty-59063aa3b43cd973ab328b406f1ab40dcf09b41a.tar.gz
Merge pull request #1601 from MarcoPolo-PasTonMolo/master
Add /correct-editor command
-rw-r--r--src/command/cmd_ac.c26
-rw-r--r--src/command/cmd_defs.c17
-rw-r--r--src/command/cmd_funcs.c126
-rw-r--r--src/command/cmd_funcs.h1
-rw-r--r--src/ui/ui.h1
-rw-r--r--src/ui/window.c29
6 files changed, 145 insertions, 55 deletions
diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c
index 9db42841..ee0f717e 100644
--- a/src/command/cmd_ac.c
+++ b/src/command/cmd_ac.c
@@ -49,6 +49,7 @@
 #include "command/cmd_funcs.h"
 #include "tools/parser.h"
 #include "plugins/plugins.h"
+#include "ui/ui.h"
 #include "ui/win_types.h"
 #include "ui/window_list.h"
 #include "xmpp/muc.h"
@@ -4031,31 +4032,8 @@ _correction_autocomplete(ProfWin* window, const char* const input, gboolean prev
 static char*
 _correct_autocomplete(ProfWin* window, const char* const input, gboolean previous)
 {
-    char* last_message = NULL;
-    switch (window->type) {
-    case WIN_CHAT:
-    {
-        ProfChatWin* chatwin = (ProfChatWin*)window;
-        assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
-        last_message = chatwin->last_message;
-        break;
-    }
-    case WIN_MUC:
-    {
-        ProfMucWin* mucwin = (ProfMucWin*)window;
-        assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
-        last_message = mucwin->last_message;
-    }
-    default:
-        break;
-    }
-
-    if (last_message == NULL) {
-        return NULL;
-    }
-
     GString* result_str = g_string_new("/correct ");
-    g_string_append(result_str, last_message);
+    g_string_append(result_str, win_get_last_sent_message(window));
     char* result = result_str->str;
     g_string_free(result_str, FALSE);
 
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c
index f435d77f..7bc4a1ff 100644
--- a/src/command/cmd_defs.c
+++ b/src/command/cmd_defs.c
@@ -2622,6 +2622,23 @@ static struct cmd_t command_defs[] = {
       CMD_NOEXAMPLES
     },
 
+    { "/correct-editor",
+      parse_args, 0, 0, NULL,
+      CMD_NOSUBFUNCS
+      CMD_MAINFUNC(cmd_correct_editor)
+      CMD_TAGS(
+              CMD_TAG_CHAT,
+              CMD_TAG_GROUPCHAT)
+      CMD_SYN(
+              "/correct-editor")
+      CMD_DESC(
+              "Spawn external editor to correct and resend the last message (XEP-0308). "
+              "For more information on how to configure corrections, see: /help correction. "
+              "Use /executable to set your favourite editor.")
+      CMD_NOARGS
+      CMD_NOEXAMPLES
+    },
+
     { "/silence",
       parse_args, 1, 1, &cons_silence_setting,
       CMD_NOSUBFUNCS
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 6e590598..3ce45a93 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -9101,52 +9101,64 @@ cmd_correction(ProfWin* window, const char* const command, gchar** args)
 }
 
 gboolean
-cmd_correct(ProfWin* window, const char* const command, gchar** args)
+_can_correct(ProfWin* window)
 {
     jabber_conn_status_t conn_status = connection_get_status();
     if (conn_status != JABBER_CONNECTED) {
         cons_show("You are currently not connected.");
-        return TRUE;
-    }
-
-    if (!prefs_get_boolean(PREF_CORRECTION_ALLOW)) {
+        return FALSE;
+    } else if (!prefs_get_boolean(PREF_CORRECTION_ALLOW)) {
         win_println(window, THEME_DEFAULT, "!", "Corrections not enabled. See /help correction.");
-        return TRUE;
-    }
-
-    if (window->type == WIN_CHAT) {
+        return FALSE;
+    } else if (window->type == WIN_CHAT) {
         ProfChatWin* chatwin = (ProfChatWin*)window;
         assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
 
         if (chatwin->last_msg_id == NULL || chatwin->last_message == NULL) {
             win_println(window, THEME_DEFAULT, "!", "No last message to correct.");
-            return TRUE;
+            return FALSE;
+        }
+    } else if (window->type == WIN_MUC) {
+        ProfMucWin* mucwin = (ProfMucWin*)window;
+        assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
+
+        if (mucwin->last_msg_id == NULL || mucwin->last_message == NULL) {
+            win_println(window, THEME_DEFAULT, "!", "No last message to correct.");
+            return FALSE;
         }
+    } else {
+        win_println(window, THEME_DEFAULT, "!", "Command /correct-editor only valid in regular chat windows.");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+gboolean
+cmd_correct(ProfWin* window, const char* const command, gchar** args)
+{
+    if (!_can_correct(window)) {
+        return TRUE;
+    }
+
+    if (window->type == WIN_CHAT) {
+        ProfChatWin* chatwin = (ProfChatWin*)window;
 
         // send message again, with replace flag
         gchar* message = g_strjoinv(" ", args);
         cl_ev_send_msg_correct(chatwin, message, FALSE, TRUE);
 
         free(message);
-        return TRUE;
     } else if (window->type == WIN_MUC) {
         ProfMucWin* mucwin = (ProfMucWin*)window;
-        assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
-
-        if (mucwin->last_msg_id == NULL || mucwin->last_message == NULL) {
-            win_println(window, THEME_DEFAULT, "!", "No last message to correct.");
-            return TRUE;
-        }
 
         // send message again, with replace flag
         gchar* message = g_strjoinv(" ", args);
         cl_ev_send_muc_msg_corrected(mucwin, message, FALSE, TRUE);
 
         free(message);
-        return TRUE;
     }
 
-    win_println(window, THEME_DEFAULT, "!", "Command /correct only valid in regular chat windows.");
     return TRUE;
 }
 
@@ -9449,16 +9461,10 @@ cmd_change_password(ProfWin* window, const char* const command, gchar** args)
     return TRUE;
 }
 
+// Returns true if an error occured
 gboolean
-cmd_editor(ProfWin* window, const char* const command, gchar** args)
+_get_message_from_editor(gchar* message, gchar** returned_message)
 {
-    jabber_conn_status_t conn_status = connection_get_status();
-
-    if (conn_status != JABBER_CONNECTED) {
-        cons_show("You are currently not connected.");
-        return TRUE;
-    }
-
     // create editor dir if not present
     char* jid = connection_get_barejid();
     gchar* path = files_get_account_data_path(DIR_EDITOR, jid);
@@ -9477,6 +9483,12 @@ cmd_editor(ProfWin* window, const char* const command, gchar** args)
     GFile* file = g_file_new_for_path(filename);
     GFileOutputStream* fos = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, &creation_error);
 
+    if (message != NULL && strlen(message) > 0) {
+        int fd_output_file = open(g_file_get_path(file), O_WRONLY);
+        write(fd_output_file, message, strlen(message));
+        close(fd_output_file);
+    }
+
     free(filename);
 
     if (creation_error) {
@@ -9508,7 +9520,7 @@ cmd_editor(ProfWin* window, const char* const command, gchar** args)
         if (size_read > 0 && size_read <= COUNT) {
             buf[size_read - 1] = '\0';
             GString* text = g_string_new(buf);
-            rl_insert_text(text->str);
+            *returned_message = g_strdup(text->str);
             g_string_free(text, TRUE);
         }
         close(fd_input_file);
@@ -9520,10 +9532,62 @@ cmd_editor(ProfWin* window, const char* const command, gchar** args)
             return TRUE;
         }
         g_object_unref(file);
-        ui_resize();
-        rl_point = rl_end;
-        rl_forced_update_display();
     }
+
+    return FALSE;
+}
+
+gboolean
+cmd_editor(ProfWin* window, const char* const command, gchar** args)
+{
+    jabber_conn_status_t conn_status = connection_get_status();
+
+    if (conn_status != JABBER_CONNECTED) {
+        cons_show("You are currently not connected.");
+        return TRUE;
+    }
+
+    gchar* message = NULL;
+
+    if (_get_message_from_editor(NULL, &message)) {
+        return TRUE;
+    }
+
+    rl_insert_text(message);
+    ui_resize();
+    rl_point = rl_end;
+    rl_forced_update_display();
+    g_free(message);
+
+    return TRUE;
+}
+
+gboolean
+cmd_correct_editor(ProfWin* window, const char* const command, gchar** args)
+{
+    if (!_can_correct(window)) {
+        return TRUE;
+    }
+
+    gchar* initial_message = win_get_last_sent_message(window);
+
+    gchar* message = NULL;
+    if (_get_message_from_editor(initial_message, &message)) {
+        return TRUE;
+    }
+
+    if (window->type == WIN_CHAT) {
+        ProfChatWin* chatwin = (ProfChatWin*)window;
+
+        cl_ev_send_msg_correct(chatwin, message, FALSE, TRUE);
+    } else if (window->type == WIN_MUC) {
+        ProfMucWin* mucwin = (ProfMucWin*)window;
+
+        cl_ev_send_muc_msg_corrected(mucwin, message, FALSE, TRUE);
+    }
+
+    g_free(message);
+
     return TRUE;
 }
 
diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h
index f092bb39..5e2a7876 100644
--- a/src/command/cmd_funcs.h
+++ b/src/command/cmd_funcs.h
@@ -246,6 +246,7 @@ gboolean cmd_executable_urlsave(ProfWin* window, const char* const command, gcha
 gboolean cmd_executable_editor(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_mam(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_editor(ProfWin* window, const char* const command, gchar** args);
+gboolean cmd_correct_editor(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_silence(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_register(ProfWin* window, const char* const command, gchar** args);
 
diff --git a/src/ui/ui.h b/src/ui/ui.h
index bb81dfc3..5257df60 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -398,6 +398,7 @@ void win_command_exec_error(ProfWin* window, const char* const command, const ch
 void win_handle_command_list(ProfWin* window, GSList* cmds);
 void win_handle_command_exec_status(ProfWin* window, const char* const type, const char* const value);
 void win_handle_command_exec_result_note(ProfWin* window, const char* const type, const char* const value);
+char* win_get_last_sent_message(ProfWin* window);
 
 // desktop notifications
 void notifier_initialise(void);
diff --git a/src/ui/window.c b/src/ui/window.c
index 3123e211..7442ce9c 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -399,6 +399,35 @@ win_get_tab_identifier(ProfWin* window)
 }
 
 char*
+win_get_last_sent_message(ProfWin* window)
+{
+    char* last_message = NULL;
+    switch (window->type) {
+    case WIN_CHAT:
+    {
+        ProfChatWin* chatwin = (ProfChatWin*)window;
+        assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
+        last_message = chatwin->last_message;
+        break;
+    }
+    case WIN_MUC:
+    {
+        ProfMucWin* mucwin = (ProfMucWin*)window;
+        assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
+        last_message = mucwin->last_message;
+    }
+    default:
+        break;
+    }
+
+    if (last_message == NULL) {
+        return NULL;
+    }
+
+    return last_message;
+}
+
+char*
 win_to_string(ProfWin* window)
 {
     assert(window != NULL);