summaryrefslogtreecommitdiff
path: root/src/plugins/script
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-09-08 11:35:12 +0200
committerSébastien Helleu <flashcode@flashtux.org>2023-09-16 12:58:57 +0200
commit24d2ba3338c8467a51ce28eccb05937603e3e6cc (patch)
tree0fa269a0b8e166ebb658358b13461fc1d80dd22c /src/plugins/script
parent63922ca03840caaa6deaf010d7627ad9542e19c5 (diff)
downloadweechat-24d2ba3338c8467a51ce28eccb05937603e3e6cc.zip
script: replace hook_process_hashtable by hook_url to download scripts and list of scripts (closes #1723)
Diffstat (limited to 'src/plugins/script')
-rw-r--r--src/plugins/script/script-action.c540
-rw-r--r--src/plugins/script/script-repo.c79
-rw-r--r--src/plugins/script/script.c26
-rw-r--r--src/plugins/script/script.h1
4 files changed, 308 insertions, 338 deletions
diff --git a/src/plugins/script/script-action.c b/src/plugins/script/script-action.c
index 7de928e3a..fca3f596c 100644
--- a/src/plugins/script/script-action.c
+++ b/src/plugins/script/script-action.c
@@ -561,84 +561,84 @@ script_action_installnext_timer_cb (const void *pointer, void *data,
*/
int
-script_action_install_process_cb (const void *pointer, void *data,
- const char *command,
- int return_code, const char *out,
- const char *err)
+script_action_install_url_cb (const void *pointer, void *data,
+ const char *url,
+ struct t_hashtable *options,
+ struct t_hashtable *output)
{
- char *pos, *filename, *filename2, str_signal[256];
+ const char *pos_name, *ptr_error;
+ char *filename, *filename2, str_signal[256];
int quiet, auto_load, length;
struct t_script_repo *ptr_script;
/* make C compiler happy */
(void) data;
- (void) out;
+ (void) options;
quiet = (pointer) ? 1 : 0;
- if (return_code >= 0)
+ pos_name = strrchr (url, '/');
+ if (pos_name)
+ pos_name++;
+
+ ptr_error = weechat_hashtable_get (output, "error");
+ if (ptr_error && ptr_error[0])
{
- pos = strrchr (command, '/');
+ weechat_printf (NULL,
+ _("%s%s: error downloading script \"%s\": %s"),
+ weechat_prefix ("error"),
+ SCRIPT_PLUGIN_NAME,
+ (pos_name) ? pos_name : "?",
+ ptr_error);
+ return WEECHAT_RC_OK;
+ }
- if (err && err[0])
- {
- weechat_printf (NULL,
- _("%s%s: error downloading script \"%s\": %s"),
- weechat_prefix ("error"),
- SCRIPT_PLUGIN_NAME,
- (pos) ? pos + 1 : "?",
- err);
- return WEECHAT_RC_OK;
- }
+ if (!pos_name)
+ {
+ /* silently ignore malformed URL (it should never happen) */
+ return WEECHAT_RC_OK;
+ }
- if (pos)
- {
- ptr_script = script_repo_search_by_name_ext (pos + 1);
- if (ptr_script)
- {
- filename = script_config_get_script_download_filename (ptr_script,
- NULL);
- if (filename)
- {
- length = 16 + strlen (filename) + 1;
- filename2 = malloc (length);
- if (filename2)
- {
- if (ptr_script->status & SCRIPT_STATUS_INSTALLED)
- {
- auto_load = (ptr_script->status & SCRIPT_STATUS_AUTOLOADED) ?
- 1 : 0;
- }
- else
- {
- auto_load = weechat_config_boolean (
- script_config_scripts_autoload);
- }
- snprintf (filename2, length,
- "%s%s%s",
- (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "",
- (auto_load) ? "-a " : "",
- filename);
- snprintf (str_signal, sizeof (str_signal),
- "%s_script_install",
- script_language[ptr_script->language]);
- (void) weechat_hook_signal_send (str_signal,
- WEECHAT_HOOK_SIGNAL_STRING,
- filename2);
- free (filename2);
- }
- free (filename);
- }
+ ptr_script = script_repo_search_by_name_ext (pos_name);
+ if (!ptr_script)
+ return WEECHAT_RC_OK;
- /* schedule install of next script */
- weechat_hook_timer (10, 0, 1,
- &script_action_installnext_timer_cb,
- (quiet) ? (void *)1 : (void *)0,
- NULL);
- }
- }
+ filename = script_config_get_script_download_filename (ptr_script, NULL);
+ if (!filename)
+ return WEECHAT_RC_OK;
+
+ length = 16 + strlen (filename) + 1;
+ filename2 = malloc (length);
+ if (!filename2)
+ {
+ free (filename);
+ return WEECHAT_RC_OK;
}
+ if (ptr_script->status & SCRIPT_STATUS_INSTALLED)
+ auto_load = (ptr_script->status & SCRIPT_STATUS_AUTOLOADED) ? 1 : 0;
+ else
+ auto_load = weechat_config_boolean (script_config_scripts_autoload);
+ snprintf (filename2, length,
+ "%s%s%s",
+ (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "",
+ (auto_load) ? "-a " : "",
+ filename);
+ snprintf (str_signal, sizeof (str_signal),
+ "%s_script_install",
+ script_language[ptr_script->language]);
+ (void) weechat_hook_signal_send (str_signal,
+ WEECHAT_HOOK_SIGNAL_STRING,
+ filename2);
+ free (filename);
+ free (filename2);
+
+ /* schedule install of next script */
+ weechat_hook_timer (10, 0, 1,
+ &script_action_installnext_timer_cb,
+ (quiet) ? (void *)1 : (void *)0,
+ NULL);
+
return WEECHAT_RC_OK;
}
@@ -675,7 +675,7 @@ void
script_action_run_install (int quiet)
{
struct t_script_repo *ptr_script_to_install;
- char *filename, *url;
+ char *filename;
struct t_hashtable *options;
if (!script_download_enabled (1))
@@ -705,40 +705,42 @@ script_action_run_install (int quiet)
script_language[ptr_script_to_install->language]);
}
+ if (!ptr_script_to_install->url || !ptr_script_to_install->url[0])
+ return;
+
filename = script_config_get_script_download_filename (ptr_script_to_install,
NULL);
- if (filename)
+ if (!filename)
+ return;
+
+ options = weechat_hashtable_new (32,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING,
+ NULL, NULL);
+ if (!options)
{
- options = weechat_hashtable_new (32,
- WEECHAT_HASHTABLE_STRING,
- WEECHAT_HASHTABLE_STRING,
- NULL, NULL);
- if (options)
- {
- url = script_build_download_url (ptr_script_to_install->url);
- if (url)
- {
- if (!weechat_config_boolean (script_config_look_quiet_actions))
- {
- weechat_printf (NULL,
- _("%s: downloading script \"%s\"..."),
- SCRIPT_PLUGIN_NAME,
- ptr_script_to_install->name_with_extension);
- }
- weechat_hashtable_set (options, "file_out", filename);
- weechat_hook_process_hashtable (
- url,
- options,
- weechat_config_integer (script_config_scripts_download_timeout) * 1000,
- &script_action_install_process_cb,
- (quiet) ? (void *)1 : (void *)0,
- NULL);
- free (url);
- }
- weechat_hashtable_free (options);
- }
free (filename);
+ return;
}
+
+ if (!weechat_config_boolean (script_config_look_quiet_actions))
+ {
+ weechat_printf (NULL,
+ _("%s: downloading script \"%s\"..."),
+ SCRIPT_PLUGIN_NAME,
+ ptr_script_to_install->name_with_extension);
+ }
+
+ weechat_hashtable_set (options, "file_out", filename);
+ weechat_hook_url (
+ ptr_script_to_install->url,
+ options,
+ weechat_config_integer (script_config_scripts_download_timeout) * 1000,
+ &script_action_install_url_cb,
+ (quiet) ? (void *)1 : (void *)0,
+ NULL);
+ weechat_hashtable_free (options);
+ free (filename);
}
/*
@@ -978,14 +980,14 @@ script_action_show_diff_process_cb (const void *pointer, void *data,
*/
int
-script_action_show_source_process_cb (const void *pointer, void *data,
- const char *command,
- int return_code, const char *out,
- const char *err)
+script_action_show_source_url_cb (const void *pointer, void *data,
+ const char *url,
+ struct t_hashtable *options,
+ struct t_hashtable *output)
{
- char *pos, *filename, *filename_loaded, line[4096], *ptr_line;
+ const char *pos_name, *ptr_error, *ptr_diff_command;
+ char *filename, *filename_loaded, line[4096], *ptr_line;
char *diff_command;
- const char *ptr_diff_command;
struct t_script_repo *ptr_script;
FILE *file;
int length, diff_made;
@@ -993,129 +995,132 @@ script_action_show_source_process_cb (const void *pointer, void *data,
/* make C compiler happy */
(void) pointer;
(void) data;
- (void) out;
+ (void) options;
+
+ pos_name = strrchr (url, '/');
+ if (pos_name)
+ pos_name++;
- if (return_code >= 0)
+ ptr_error = weechat_hashtable_get (output, "error");
+ if (ptr_error && ptr_error[0])
{
- pos = strrchr (command, '/');
+ weechat_printf (NULL,
+ _("%s%s: error downloading script \"%s\": %s"),
+ weechat_prefix ("error"),
+ SCRIPT_PLUGIN_NAME,
+ (pos_name) ? pos_name : "?",
+ ptr_error);
+ return WEECHAT_RC_OK;
+ }
- if (err && err[0])
- {
- weechat_printf (NULL,
- _("%s%s: error downloading script \"%s\": %s"),
- weechat_prefix ("error"),
- SCRIPT_PLUGIN_NAME,
- (pos) ? pos + 1 : "?",
- err);
- return WEECHAT_RC_OK;
- }
+ if (!pos_name)
+ {
+ /* silently ignore malformed URL (it should never happen) */
+ return WEECHAT_RC_OK;
+ }
- if (pos)
+ ptr_script = script_repo_search_by_name_ext (pos_name);
+ if (!ptr_script)
+ return WEECHAT_RC_OK;
+
+ filename = script_config_get_script_download_filename (ptr_script,
+ ".repository");
+ if (!filename)
+ return WEECHAT_RC_OK;
+
+ /*
+ * read file and display content on script buffer
+ * (only if script buffer is still displaying detail of
+ * this script)
+ */
+ if (script_buffer && script_buffer_detail_script
+ && (script_buffer_detail_script == ptr_script))
+ {
+ file = fopen (filename, "r");
+ if (file)
{
- ptr_script = script_repo_search_by_name_ext (pos + 1);
- if (ptr_script)
+ while (!feof (file))
{
- filename = script_config_get_script_download_filename (ptr_script,
- ".repository");
- if (filename)
+ ptr_line = fgets (line, sizeof (line) - 1, file);
+ if (ptr_line)
{
- /*
- * read file and display content on script buffer
- * (only if script buffer is still displaying detail of
- * this script)
- */
- if (script_buffer && script_buffer_detail_script
- && (script_buffer_detail_script == ptr_script))
- {
- file = fopen (filename, "r");
- if (file)
- {
- while (!feof (file))
- {
- ptr_line = fgets (line, sizeof (line) - 1, file);
- if (ptr_line)
- {
- /* remove trailing '\r' and '\n' */
- length = strlen (line) - 1;
- while ((length >= 0)
- && ((line[length] == '\n')
- || (line[length] == '\r')))
- {
- line[length] = '\0';
- length--;
- }
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- "%s", ptr_line);
- }
- }
- fclose (file);
- }
- else
- {
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- _("Error: file not found"));
- }
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- "%s----------------------------------------"
- "----------------------------------------",
- weechat_color ("lightcyan"));
- }
- diff_made = 0;
- ptr_diff_command = script_config_get_diff_command ();
- if (ptr_diff_command && ptr_diff_command[0]
- && (ptr_script->status & SCRIPT_STATUS_NEW_VERSION))
- {
- /*
- * diff command set => get the diff with a new process,
- * file will be deleted later (in callback of this new
- * process)
- */
- filename_loaded = script_repo_get_filename_loaded (ptr_script);
- if (filename_loaded)
- {
- length = strlen (ptr_diff_command) + 1
- + strlen (filename_loaded) + 1
- + strlen (filename) + 1;
- diff_command = malloc (length);
- if (diff_command)
- {
- snprintf (diff_command, length,
- "%s %s %s",
- ptr_diff_command,
- filename_loaded,
- filename);
- script_buffer_detail_script_last_line++;
- script_buffer_detail_script_line_diff = script_buffer_detail_script_last_line;
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- "%s", diff_command);
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- "%s----------------------------------------"
- "----------------------------------------",
- weechat_color ("magenta"));
- weechat_hook_process (diff_command, 10000,
- &script_action_show_diff_process_cb,
- filename, NULL);
- diff_made = 1;
- free (diff_command);
- }
- free (filename_loaded);
- }
- }
- if (!diff_made)
+ /* remove trailing '\r' and '\n' */
+ length = strlen (line) - 1;
+ while ((length >= 0)
+ && ((line[length] == '\n')
+ || (line[length] == '\r')))
{
- /* no diff made: delete temporary file now */
- unlink (filename);
- free (filename);
+ line[length] = '\0';
+ length--;
}
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ "%s", ptr_line);
}
}
+ fclose (file);
+ }
+ else
+ {
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ _("Error: file not found"));
+ }
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ "%s----------------------------------------"
+ "----------------------------------------",
+ weechat_color ("lightcyan"));
+ }
+ diff_made = 0;
+ ptr_diff_command = script_config_get_diff_command ();
+ if (ptr_diff_command && ptr_diff_command[0]
+ && (ptr_script->status & SCRIPT_STATUS_NEW_VERSION))
+ {
+ /*
+ * diff command set => get the diff with a new process,
+ * file will be deleted later (in callback of this new
+ * process)
+ */
+ filename_loaded = script_repo_get_filename_loaded (ptr_script);
+ if (filename_loaded)
+ {
+ length = strlen (ptr_diff_command) + 1
+ + strlen (filename_loaded) + 1
+ + strlen (filename) + 1;
+ diff_command = malloc (length);
+ if (diff_command)
+ {
+ snprintf (diff_command, length,
+ "%s %s %s",
+ ptr_diff_command,
+ filename_loaded,
+ filename);
+ script_buffer_detail_script_last_line++;
+ script_buffer_detail_script_line_diff = script_buffer_detail_script_last_line;
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ "%s", diff_command);
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ "%s----------------------------------------"
+ "----------------------------------------",
+ weechat_color ("magenta"));
+ weechat_hook_process (diff_command, 10000,
+ &script_action_show_diff_process_cb,
+ filename, NULL);
+ diff_made = 1;
+ free (diff_command);
+ }
+ free (filename_loaded);
}
}
+ if (!diff_made)
+ {
+ /* no diff made: delete temporary file now */
+ unlink (filename);
+ free (filename);
+ }
return WEECHAT_RC_OK;
}
@@ -1128,76 +1133,75 @@ void
script_action_run_show (const char *name, int quiet)
{
struct t_script_repo *ptr_script;
- char *filename, *url;
+ char *filename;
struct t_hashtable *options;
if (!script_download_enabled (1))
return;
- if (name)
+ if (!name)
{
- ptr_script = script_repo_search_by_name_ext (name);
- if (ptr_script)
+ script_buffer_show_detail_script (NULL);
+ return;
+ }
+
+ ptr_script = script_repo_search_by_name_ext (name);
+ if (!ptr_script)
+ {
+ if (!quiet)
{
- script_buffer_show_detail_script (ptr_script);
- if (weechat_config_boolean (script_config_look_display_source))
- {
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- _("Source code:"));
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line++,
- "%s----------------------------------------"
- "----------------------------------------",
- weechat_color ("lightcyan"));
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line,
- _("Downloading script..."));
- weechat_printf_y (script_buffer,
- script_buffer_detail_script_last_line + 1,
- "%s----------------------------------------"
- "----------------------------------------",
- weechat_color ("lightcyan"));
- filename = script_config_get_script_download_filename (ptr_script,
- ".repository");
- if (filename)
- {
- options = weechat_hashtable_new (32,
- WEECHAT_HASHTABLE_STRING,
- WEECHAT_HASHTABLE_STRING,
- NULL, NULL);
- if (options)
- {
- url = script_build_download_url (ptr_script->url);
- if (url)
- {
- weechat_hashtable_set (options, "file_out", filename);
- weechat_hook_process_hashtable (
- url,
- options,
- weechat_config_integer (script_config_scripts_download_timeout) * 1000,
- &script_action_show_source_process_cb,
- NULL, NULL);
- free (url);
- }
- weechat_hashtable_free (options);
- }
- free (filename);
- }
- }
+ weechat_printf (NULL,
+ _("%s: script \"%s\" not found"),
+ SCRIPT_PLUGIN_NAME, name);
}
- else
+ return;
+ }
+
+ script_buffer_show_detail_script (ptr_script);
+
+ if (!weechat_config_boolean (script_config_look_display_source))
+ return;
+
+ if (!ptr_script->url)
+ return;
+
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ _("Source code:"));
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line++,
+ "%s----------------------------------------"
+ "----------------------------------------",
+ weechat_color ("lightcyan"));
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line,
+ _("Downloading script..."));
+ weechat_printf_y (script_buffer,
+ script_buffer_detail_script_last_line + 1,
+ "%s----------------------------------------"
+ "----------------------------------------",
+ weechat_color ("lightcyan"));
+ filename = script_config_get_script_download_filename (ptr_script,
+ ".repository");
+ if (filename)
+ {
+ options = weechat_hashtable_new (32,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING,
+ NULL, NULL);
+ if (options)
{
- if (!quiet)
- {
- weechat_printf (NULL,
- _("%s: script \"%s\" not found"),
- SCRIPT_PLUGIN_NAME, name);
- }
+ weechat_hashtable_set (options, "file_out", filename);
+ weechat_hook_url (
+ ptr_script->url,
+ options,
+ weechat_config_integer (script_config_scripts_download_timeout) * 1000,
+ &script_action_show_source_url_cb,
+ NULL, NULL);
+ weechat_hashtable_free (options);
}
+ free (filename);
}
- else
- script_buffer_show_detail_script (NULL);
}
/*
diff --git a/src/plugins/script/script-repo.c b/src/plugins/script/script-repo.c
index 91c461096..f702a04cc 100644
--- a/src/plugins/script/script-repo.c
+++ b/src/plugins/script/script-repo.c
@@ -1431,42 +1431,41 @@ script_repo_file_read (int quiet)
*/
int
-script_repo_file_update_process_cb (const void *pointer, void *data,
- const char *command,
- int return_code, const char *out,
- const char *err)
+script_repo_file_update_url_cb (const void *pointer, void *data,
+ const char *url,
+ struct t_hashtable *options,
+ struct t_hashtable *output)
{
+ const char *ptr_error;
int quiet;
/* make C compiler happy */
(void) data;
- (void) command;
- (void) out;
+ (void) url;
+ (void) options;
quiet = (pointer) ? 1 : 0;
- if (return_code >= 0)
+ ptr_error = weechat_hashtable_get (output, "error");
+ if (ptr_error && ptr_error[0])
{
- if (err && err[0])
- {
- weechat_printf (NULL,
- _("%s%s: error downloading list of scripts: %s"),
- weechat_prefix ("error"),
- SCRIPT_PLUGIN_NAME,
- err);
- return WEECHAT_RC_OK;
- }
+ weechat_printf (NULL,
+ _("%s%s: error downloading list of scripts: %s"),
+ weechat_prefix ("error"),
+ SCRIPT_PLUGIN_NAME,
+ ptr_error);
+ return WEECHAT_RC_OK;
+ }
- if (script_repo_file_read (quiet) && scripts_repo)
- {
- if (script_buffer)
- script_buffer_refresh (1);
- if (!script_action_run_all ())
- script_buffer_refresh (1);
- }
- else
+ if (script_repo_file_read (quiet) && scripts_repo)
+ {
+ if (script_buffer)
+ script_buffer_refresh (1);
+ if (!script_action_run_all ())
script_buffer_refresh (1);
}
+ else
+ script_buffer_refresh (1);
return WEECHAT_RC_OK;
}
@@ -1482,7 +1481,7 @@ script_repo_file_update_process_cb (const void *pointer, void *data,
int
script_repo_file_update (int quiet)
{
- char *filename, *url;
+ char *filename;
struct t_hashtable *options;
if (!script_download_enabled (1))
@@ -1500,26 +1499,20 @@ script_repo_file_update (int quiet)
NULL, NULL);
if (options)
{
- url = script_build_download_url (
- weechat_config_string (script_config_scripts_url));
- if (url)
+ if (!quiet)
{
- if (!quiet)
- {
- weechat_printf (NULL,
- _("%s: downloading list of scripts..."),
- SCRIPT_PLUGIN_NAME);
- }
- weechat_hashtable_set (options, "file_out", filename);
- weechat_hook_process_hashtable (
- url,
- options,
- weechat_config_integer (script_config_scripts_download_timeout) * 1000,
- &script_repo_file_update_process_cb,
- (quiet) ? (void *)1 : (void *)0,
- NULL);
- free (url);
+ weechat_printf (NULL,
+ _("%s: downloading list of scripts..."),
+ SCRIPT_PLUGIN_NAME);
}
+ weechat_hashtable_set (options, "file_out", filename);
+ weechat_hook_url (
+ weechat_config_string (script_config_scripts_url),
+ options,
+ weechat_config_integer (script_config_scripts_download_timeout) * 1000,
+ &script_repo_file_update_url_cb,
+ (quiet) ? (void *)1 : (void *)0,
+ NULL);
weechat_hashtable_free (options);
}
diff --git a/src/plugins/script/script.c b/src/plugins/script/script.c
index 67636e0f4..f04ccb248 100644
--- a/src/plugins/script/script.c
+++ b/src/plugins/script/script.c
@@ -131,32 +131,6 @@ script_download_enabled (int display_error)
}
/*
- * Builds download URL (to use with hook_process or hook_process_hashtable).
- *
- * Note: result must be freed after use.
- */
-
-char *
-script_build_download_url (const char *url)
-{
- char *result;
- int length;
-
- if (!url || !url[0])
- return NULL;
-
- /* length of url + "url:" */
- length = 4 + strlen (url) + 1;
- result = malloc (length);
- if (!result)
- return NULL;
-
- snprintf (result, length, "url:%s", url);
-
- return result;
-}
-
-/*
* Gets loaded plugins (in array of integers).
*/
diff --git a/src/plugins/script/script.h b/src/plugins/script/script.h
index 60b74b3e7..b87666002 100644
--- a/src/plugins/script/script.h
+++ b/src/plugins/script/script.h
@@ -36,7 +36,6 @@ extern struct t_hashtable *script_loaded;
extern int script_language_search (const char *language);
extern int script_language_search_by_extension (const char *extension);
extern int script_download_enabled (int display_error);
-extern char *script_build_download_url (const char *url);
extern void script_get_loaded_plugins ();
extern void script_get_scripts ();