diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2023-09-08 17:07:03 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2023-09-16 13:00:03 +0200 |
commit | fb00bc1f4b9ede63153cf55ceb38d0fdcb39786c (patch) | |
tree | e95b3f0c70d0fc3a8b00b2c97bf88dae21032c34 /src/plugins/python | |
parent | 24d2ba3338c8467a51ce28eccb05937603e3e6cc (diff) | |
download | weechat-fb00bc1f4b9ede63153cf55ceb38d0fdcb39786c.zip |
scripts: add function hook_url in scripting API
Diffstat (limited to 'src/plugins/python')
-rw-r--r-- | src/plugins/python/weechat-python-api.c | 79 | ||||
-rw-r--r-- | src/plugins/python/weechat.pyi | 26 |
2 files changed, 105 insertions, 0 deletions
diff --git a/src/plugins/python/weechat-python-api.c b/src/plugins/python/weechat-python-api.c index 9ffac5b3b..2cb87d55f 100644 --- a/src/plugins/python/weechat-python-api.c +++ b/src/plugins/python/weechat-python-api.c @@ -2580,6 +2580,84 @@ API_FUNC(hook_process_hashtable) } int +weechat_python_api_hook_url_cb (const void *pointer, void *data, + const char *url, + struct t_hashtable *options, + struct t_hashtable *output) +{ + struct t_plugin_script *script; + void *func_argv[4]; + char empty_arg[1] = { '\0' }; + const char *ptr_function, *ptr_data; + int *rc, ret; + + script = (struct t_plugin_script *)pointer; + plugin_script_get_function_and_data (data, &ptr_function, &ptr_data); + + if (ptr_function && ptr_function[0]) + { + func_argv[0] = (ptr_data) ? (char *)ptr_data : empty_arg; + func_argv[1] = (url) ? (char *)url : empty_arg; + func_argv[2] = options; + func_argv[3] = output; + + rc = (int *) weechat_python_exec (script, + WEECHAT_SCRIPT_EXEC_INT, + ptr_function, + "sshh", func_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + + return ret; + } + + return WEECHAT_RC_ERROR; +} + +API_FUNC(hook_url) +{ + char *url, *function, *data; + const char *result; + int timeout; + struct t_hashtable *options; + PyObject *dict; + + API_INIT_FUNC(1, "hook_url", API_RETURN_EMPTY); + url = NULL; + dict = NULL; + options = NULL; + timeout = 0; + function = NULL; + data = NULL; + if (!PyArg_ParseTuple (args, "sOiss", &url, &dict, &timeout, &function, + &data)) + API_WRONG_ARGS(API_RETURN_EMPTY); + + options = weechat_python_dict_to_hashtable (dict, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + result = API_PTR2STR(plugin_script_api_hook_url (weechat_python_plugin, + python_current_script, + url, + options, + timeout, + &weechat_python_api_hook_url_cb, + function, + data)); + if (options) + weechat_hashtable_free (options); + + API_RETURN_STRING(result); +} + +int weechat_python_api_hook_connect_cb (const void *pointer, void *data, int status, int gnutls_rc, int sock, const char *error, @@ -5461,6 +5539,7 @@ PyMethodDef weechat_python_funcs[] = API_DEF_FUNC(hook_fd), API_DEF_FUNC(hook_process), API_DEF_FUNC(hook_process_hashtable), + API_DEF_FUNC(hook_url), API_DEF_FUNC(hook_connect), API_DEF_FUNC(hook_line), API_DEF_FUNC(hook_print), diff --git a/src/plugins/python/weechat.pyi b/src/plugins/python/weechat.pyi index c21438aa9..db2b5ceae 100644 --- a/src/plugins/python/weechat.pyi +++ b/src/plugins/python/weechat.pyi @@ -1362,6 +1362,32 @@ def hook_process_hashtable(command: str, options: Dict[str, str], timeout: int, ... +def hook_url(url: str, options: Dict[str, str], timeout: int, callback: str, callback_data: str) -> str: + """`hook_url in WeeChat plugin API reference <https://weechat.org/doc/weechat/api/#_hook_url>`_ + :: + + # example + def my_url_cb(data: str, url: str, options: Dict[str, str], output: Dict[str, str]) -> int: + weechat.prnt("", "output: %s" % output) + return weechat.WEECHAT_RC_OK + + # example 1: output to a file + hook1 = weechat.hook_url("https://weechat.org/", + {"file_out": "/tmp/weechat.org.html"}, + 20000, "my_url_cb", "") + + # example 2: custom HTTP headers, output sent to callback + options = { + "httpheader": "\n".join([ + "Header1: value1", + "Header2: value2", + ]), + } + hook2 = weechat.hook_url("http://localhost:8080/", options, 20000, "my_url_cb", "") + """ + ... + + def hook_connect(proxy: str, address: str, port: int, ipv6: int, retry: int, local_hostname: str, callback: str, callback_data: str) -> str: """`hook_connect in WeeChat plugin API reference <https://weechat.org/doc/weechat/api/#_hook_connect>`_ |