Initial commit of the HEAD branch of the ELinks CVS repository, as of
[elinks/images.git] / src / scripting / python / hooks.c
blobf16fe98f94f1e314b08dfead0e6cc562c810ff4f
1 /* Python scripting hooks */
2 /* $Id: hooks.c,v 1.10 2005/06/30 15:10:04 witekfl Exp $ */
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
8 #include "scripting/python/core.h"
10 #include "elinks.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 */
22 static void
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)) {
28 PyObject *pValue;
29 unsigned char *str;
31 if (!ses || !have_location(ses)) {
32 str = NULL;
33 } else {
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);
40 if (res) {
41 unsigned char *new_url = stracpy((unsigned char *)res);
43 if (new_url) mem_free_set(url, new_url);
45 Py_DECREF(pValue);
46 } else {
47 if (PyErr_Occurred()) {
48 PyErr_Print();
49 PyErr_Clear();
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 *);
61 if (pDict && *url)
62 do_script_hook_goto_url(ses, url);
64 return EVENT_HOOK_STATUS_NEXT;
67 static void
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;
78 if (str) {
79 new_url = stracpy((unsigned char *)str);
80 if (new_url) mem_free_set(url, new_url);
82 Py_DECREF(pValue);
83 } else {
84 if (PyErr_Occurred()) {
85 PyErr_Print();
86 PyErr_Clear();
92 static enum evhook_status
93 script_hook_follow_url(va_list ap, void *data)
95 unsigned char **url = va_arg(ap, unsigned char **);
97 if (pDict && *url)
98 do_script_hook_follow_url(url);
100 return EVENT_HOOK_STATUS_NEXT;
103 static void
104 do_script_hook_pre_format_html(unsigned char *url, unsigned char **html,
105 int *html_len)
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);
115 if (str) {
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;
121 Py_DECREF(pValue);
122 } else {
123 if (PyErr_Occurred()) {
124 PyErr_Print();
125 PyErr_Clear();
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;
145 static inline void
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);
156 if (str) {
157 unsigned char *new_url = stracpy((unsigned char *)str);
159 if (new_url) mem_free_set(new_proxy_url, new_url);
161 Py_DECREF(pValue);
162 } else {
163 if (PyErr_Occurred()) {
164 PyErr_Print();
165 PyErr_Clear();
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;
183 static void
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);
191 if (pValue) {
192 if (pValue != Py_None) {
193 Py_DECREF(pValue);
195 } else {
196 if (PyErr_Occurred()) {
197 PyErr_Print();
198 PyErr_Clear();
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,