1 /* Python scripting hooks */
2 /* $Id: hooks.c,v 1.10 2005/06/30 15:10:04 witekfl Exp $ */
8 #include "scripting/python/core.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
, unsigned char **html
,
107 PyObject
*pFunc
= PyDict_GetItemString(pDict
, "pre_format_html_hook");
109 if (pFunc
&& PyCallable_Check(pFunc
)) {
110 PyObject
*pValue
= PyObject_CallFunction(pFunc
, "ss", url
, *html
);
112 if (pValue
&& (pValue
!= Py_None
)) {
113 const unsigned char *str
= PyString_AsString(pValue
);
116 *html_len
= PyString_Size(pValue
); /* strlen(str); */
117 *html
= memacpy((unsigned char *)str
, *html_len
);
118 /* Isn't a memleak here? --witekfl */
119 if (!*html
) *html_len
= 0;
123 if (PyErr_Occurred()) {
131 static enum evhook_status
132 script_hook_pre_format_html(va_list ap
, void *data
)
134 unsigned char **html
= va_arg(ap
, unsigned char **);
135 int *html_len
= va_arg(ap
, int *);
136 struct session
*ses
= va_arg(ap
, struct session
*);
137 unsigned char *url
= va_arg(ap
, unsigned char *);
139 if (pDict
&& ses
&& url
&& *html
&& *html_len
)
140 do_script_hook_pre_format_html(url
, html
, html_len
);
142 return EVENT_HOOK_STATUS_NEXT
;
146 do_script_hook_get_proxy(unsigned char **new_proxy_url
, unsigned char *url
)
148 PyObject
*pFunc
= PyDict_GetItemString(pDict
, "proxy_for_hook");
150 if (pFunc
&& PyCallable_Check(pFunc
)) {
151 PyObject
*pValue
= PyObject_CallFunction(pFunc
, "s", url
);
153 if (pValue
&& (pValue
!= Py_None
)) {
154 const unsigned char *str
= PyString_AsString(pValue
);
157 unsigned char *new_url
= stracpy((unsigned char *)str
);
159 if (new_url
) mem_free_set(new_proxy_url
, new_url
);
163 if (PyErr_Occurred()) {
171 static enum evhook_status
172 script_hook_get_proxy(va_list ap
, void *data
)
174 unsigned char **new_proxy_url
= va_arg(ap
, unsigned char **);
175 unsigned char *url
= va_arg(ap
, unsigned char *);
177 if (pDict
&& new_proxy_url
&& url
)
178 do_script_hook_get_proxy(new_proxy_url
, url
);
180 return EVENT_HOOK_STATUS_NEXT
;
184 do_script_hook_quit(void)
186 PyObject
*pFunc
= PyDict_GetItemString(pDict
, "quit_hook");
188 if (pFunc
&& PyCallable_Check(pFunc
)) {
189 PyObject
*pValue
= PyObject_CallFunction(pFunc
, NULL
);
192 if (pValue
!= Py_None
) {
196 if (PyErr_Occurred()) {
204 static enum evhook_status
205 script_hook_quit(va_list ap
, void *data
)
207 if (pDict
) do_script_hook_quit();
208 return EVENT_HOOK_STATUS_NEXT
;
211 struct event_hook_info python_scripting_hooks
[] = {
212 { "goto-url", 0, script_hook_goto_url
, NULL
},
213 { "follow-url", 0, script_hook_follow_url
, NULL
},
214 { "pre-format-html", 0, script_hook_pre_format_html
, NULL
},
215 { "get-proxy", 0, script_hook_get_proxy
, NULL
},
216 { "quit", 0, script_hook_quit
, NULL
},
217 NULL_EVENT_HOOK_INFO
,