1
/* Python scripting hooks */
7
#include "scripting/python/core.h"
11
#include "cache/cache.h"
12
#include "main/event.h"
13
#include "protocol/uri.h"
14
#include "scripting/python/hooks.h"
15
#include "session/location.h"
16
#include "session/session.h"
17
#include "util/string.h"
19
/* The events that will trigger the functions below and what they are expected
20
* to do is explained in doc/events.txt */
23
do_script_hook_goto_url(struct session *ses, unsigned char **url)
25
PyObject *pFunc = PyDict_GetItemString(pDict, "goto_url_hook");
27
if (pFunc && PyCallable_Check(pFunc)) {
31
if (!ses || !have_location(ses)) {
34
str = struri(cur_loc(ses)->vs.uri);
36
pValue = PyObject_CallFunction(pFunc, "s", str);
37
if (pValue && (pValue != Py_None)) {
38
const unsigned char *res = PyString_AsString(pValue);
41
unsigned char *new_url = stracpy((unsigned char *)res);
43
if (new_url) mem_free_set(url, new_url);
47
if (PyErr_Occurred()) {
55
static enum evhook_status
56
script_hook_goto_url(va_list ap, void *data)
58
unsigned char **url = va_arg(ap, unsigned char **);
59
struct session *ses = va_arg(ap, struct session *);
62
do_script_hook_goto_url(ses, url);
64
return EVENT_HOOK_STATUS_NEXT;
68
do_script_hook_follow_url(unsigned char **url)
70
PyObject *pFunc = PyDict_GetItemString(pDict, "follow_url_hook");
72
if (pFunc && PyCallable_Check(pFunc)) {
73
PyObject *pValue = PyObject_CallFunction(pFunc, "s", *url);
74
if (pValue && (pValue != Py_None)) {
75
const unsigned char *str = PyString_AsString(pValue);
76
unsigned char *new_url;
79
new_url = stracpy((unsigned char *)str);
80
if (new_url) mem_free_set(url, new_url);
84
if (PyErr_Occurred()) {
92
static enum evhook_status
93
script_hook_follow_url(va_list ap, void *data)
95
unsigned char **url = va_arg(ap, unsigned char **);
98
do_script_hook_follow_url(url);
100
return EVENT_HOOK_STATUS_NEXT;
104
do_script_hook_pre_format_html(unsigned char *url, struct cache_entry *cached,
105
struct fragment *fragment)
107
PyObject *pFunc = PyDict_GetItemString(pDict, "pre_format_html_hook");
109
if (pFunc && PyCallable_Check(pFunc)) {
110
PyObject *pValue = PyObject_CallFunction(pFunc, "ss#", url,
114
if (pValue && (pValue != Py_None)) {
115
const unsigned char *str = PyString_AsString(pValue);
118
int len = PyString_Size(pValue); /* strlen(str); */
120
add_fragment(cached, 0, (unsigned char *) str, len);
121
normalize_cache_entry(cached, len);
125
if (PyErr_Occurred()) {
133
static enum evhook_status
134
script_hook_pre_format_html(va_list ap, void *data)
136
struct session *ses = va_arg(ap, struct session *);
137
struct cache_entry *cached = va_arg(ap, struct cache_entry *);
138
struct fragment *fragment = get_cache_fragment(cached);
139
unsigned char *url = struri(cached->uri);
141
if (pDict && ses && url && cached->length && *fragment->data)
142
do_script_hook_pre_format_html(url, cached, fragment);
144
return EVENT_HOOK_STATUS_NEXT;
148
do_script_hook_get_proxy(unsigned char **new_proxy_url, unsigned char *url)
150
PyObject *pFunc = PyDict_GetItemString(pDict, "proxy_for_hook");
152
if (pFunc && PyCallable_Check(pFunc)) {
153
PyObject *pValue = PyObject_CallFunction(pFunc, "s", url);
155
if (pValue && (pValue != Py_None)) {
156
const unsigned char *str = PyString_AsString(pValue);
159
unsigned char *new_url = stracpy((unsigned char *)str);
161
if (new_url) mem_free_set(new_proxy_url, new_url);
165
if (PyErr_Occurred()) {
173
static enum evhook_status
174
script_hook_get_proxy(va_list ap, void *data)
176
unsigned char **new_proxy_url = va_arg(ap, unsigned char **);
177
unsigned char *url = va_arg(ap, unsigned char *);
179
if (pDict && new_proxy_url && url)
180
do_script_hook_get_proxy(new_proxy_url, url);
182
return EVENT_HOOK_STATUS_NEXT;
186
do_script_hook_quit(void)
188
PyObject *pFunc = PyDict_GetItemString(pDict, "quit_hook");
190
if (pFunc && PyCallable_Check(pFunc)) {
191
PyObject *pValue = PyObject_CallFunction(pFunc, NULL);
194
if (pValue != Py_None) {
198
if (PyErr_Occurred()) {
206
static enum evhook_status
207
script_hook_quit(va_list ap, void *data)
209
if (pDict) do_script_hook_quit();
210
return EVENT_HOOK_STATUS_NEXT;
213
struct event_hook_info python_scripting_hooks[] = {
214
{ "goto-url", 0, script_hook_goto_url, NULL },
215
{ "follow-url", 0, script_hook_follow_url, NULL },
216
{ "pre-format-html", 0, script_hook_pre_format_html, NULL },
217
{ "get-proxy", 0, script_hook_get_proxy, NULL },
218
{ "quit", 0, script_hook_quit, NULL },
219
NULL_EVENT_HOOK_INFO,