~ubuntu-branches/ubuntu/feisty/elinks/feisty-updates

« back to all changes in this revision

Viewing changes to src/scripting/python/hooks.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-06-30 08:57:43 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060630085743-l81fgbw9dehvl1ds
Tags: 0.11.1-1ubuntu1
* Merge to Debian unstable.
* Removed gnutls12 porting, this is upstream now.
* Only Ubuntu changes left:
  - Killed type-handling.
  - Add X-Ubuntu-Gettext-Domain to .desktop files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Python scripting hooks */
 
2
 
 
3
#ifdef HAVE_CONFIG_H
 
4
#include "config.h"
 
5
#endif
 
6
 
 
7
#include "scripting/python/core.h"
 
8
 
 
9
#include "elinks.h"
 
10
 
 
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"
 
18
 
 
19
/* The events that will trigger the functions below and what they are expected
 
20
 * to do is explained in doc/events.txt */
 
21
 
 
22
static void
 
23
do_script_hook_goto_url(struct session *ses, unsigned char **url)
 
24
{
 
25
        PyObject *pFunc = PyDict_GetItemString(pDict, "goto_url_hook");
 
26
 
 
27
        if (pFunc && PyCallable_Check(pFunc)) {
 
28
                PyObject *pValue;
 
29
                unsigned char *str;
 
30
 
 
31
                if (!ses || !have_location(ses)) {
 
32
                        str = NULL;
 
33
                } else {
 
34
                        str = struri(cur_loc(ses)->vs.uri);
 
35
                }
 
36
                pValue = PyObject_CallFunction(pFunc, "s", str);
 
37
                if (pValue && (pValue != Py_None)) {
 
38
                        const unsigned char *res = PyString_AsString(pValue);
 
39
 
 
40
                        if (res) {
 
41
                                unsigned char *new_url = stracpy((unsigned char *)res);
 
42
 
 
43
                                if (new_url) mem_free_set(url, new_url);
 
44
                        }
 
45
                        Py_DECREF(pValue);
 
46
                } else {
 
47
                        if (PyErr_Occurred()) {
 
48
                                PyErr_Print();
 
49
                                PyErr_Clear();
 
50
                        }
 
51
                }
 
52
        }
 
53
}
 
54
 
 
55
static enum evhook_status
 
56
script_hook_goto_url(va_list ap, void *data)
 
57
{
 
58
        unsigned char **url = va_arg(ap, unsigned char **);
 
59
        struct session *ses = va_arg(ap, struct session *);
 
60
 
 
61
        if (pDict && *url)
 
62
                do_script_hook_goto_url(ses, url);
 
63
 
 
64
        return EVENT_HOOK_STATUS_NEXT;
 
65
}
 
66
 
 
67
static void
 
68
do_script_hook_follow_url(unsigned char **url)
 
69
{
 
70
        PyObject *pFunc = PyDict_GetItemString(pDict, "follow_url_hook");
 
71
 
 
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;
 
77
 
 
78
                        if (str) {
 
79
                                new_url = stracpy((unsigned char *)str);
 
80
                                if (new_url) mem_free_set(url, new_url);
 
81
                        }
 
82
                        Py_DECREF(pValue);
 
83
                } else {
 
84
                        if (PyErr_Occurred()) {
 
85
                                PyErr_Print();
 
86
                                PyErr_Clear();
 
87
                        }
 
88
                }
 
89
        }
 
90
}
 
91
 
 
92
static enum evhook_status
 
93
script_hook_follow_url(va_list ap, void *data)
 
94
{
 
95
        unsigned char **url = va_arg(ap, unsigned char **);
 
96
 
 
97
        if (pDict && *url)
 
98
                do_script_hook_follow_url(url);
 
99
 
 
100
        return EVENT_HOOK_STATUS_NEXT;
 
101
}
 
102
 
 
103
static void
 
104
do_script_hook_pre_format_html(unsigned char *url, struct cache_entry *cached,
 
105
                               struct fragment *fragment)
 
106
{
 
107
        PyObject *pFunc = PyDict_GetItemString(pDict, "pre_format_html_hook");
 
108
 
 
109
        if (pFunc && PyCallable_Check(pFunc)) {
 
110
                PyObject *pValue = PyObject_CallFunction(pFunc, "ss#", url,
 
111
                                                         fragment->data,
 
112
                                                         fragment->length);
 
113
 
 
114
                if (pValue && (pValue != Py_None)) {
 
115
                        const unsigned char *str = PyString_AsString(pValue);
 
116
 
 
117
                        if (str) {
 
118
                                int len = PyString_Size(pValue); /* strlen(str); */
 
119
 
 
120
                                add_fragment(cached, 0, (unsigned char *) str, len);
 
121
                                normalize_cache_entry(cached, len);
 
122
                        }
 
123
                        Py_DECREF(pValue);
 
124
                } else {
 
125
                        if (PyErr_Occurred()) {
 
126
                                PyErr_Print();
 
127
                                PyErr_Clear();
 
128
                        }
 
129
                }
 
130
        }
 
131
}
 
132
 
 
133
static enum evhook_status
 
134
script_hook_pre_format_html(va_list ap, void *data)
 
135
{
 
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);
 
140
 
 
141
        if (pDict && ses && url && cached->length && *fragment->data)
 
142
                do_script_hook_pre_format_html(url, cached, fragment);
 
143
 
 
144
        return EVENT_HOOK_STATUS_NEXT;
 
145
}
 
146
 
 
147
static inline void
 
148
do_script_hook_get_proxy(unsigned char **new_proxy_url, unsigned char *url)
 
149
{
 
150
        PyObject *pFunc = PyDict_GetItemString(pDict, "proxy_for_hook");
 
151
 
 
152
        if (pFunc && PyCallable_Check(pFunc)) {
 
153
                PyObject *pValue = PyObject_CallFunction(pFunc, "s", url);
 
154
 
 
155
                if (pValue && (pValue != Py_None)) {
 
156
                        const unsigned char *str = PyString_AsString(pValue);
 
157
 
 
158
                        if (str) {
 
159
                                unsigned char *new_url = stracpy((unsigned char *)str);
 
160
 
 
161
                                if (new_url) mem_free_set(new_proxy_url, new_url);
 
162
                        }
 
163
                        Py_DECREF(pValue);
 
164
                } else {
 
165
                        if (PyErr_Occurred()) {
 
166
                                PyErr_Print();
 
167
                                PyErr_Clear();
 
168
                        }
 
169
                }
 
170
        }
 
171
}
 
172
 
 
173
static enum evhook_status
 
174
script_hook_get_proxy(va_list ap, void *data)
 
175
{
 
176
        unsigned char **new_proxy_url = va_arg(ap, unsigned char **);
 
177
        unsigned char *url = va_arg(ap, unsigned char *);
 
178
 
 
179
        if (pDict && new_proxy_url && url)
 
180
                do_script_hook_get_proxy(new_proxy_url, url);
 
181
 
 
182
        return EVENT_HOOK_STATUS_NEXT;
 
183
}
 
184
 
 
185
static void
 
186
do_script_hook_quit(void)
 
187
{
 
188
        PyObject *pFunc = PyDict_GetItemString(pDict, "quit_hook");
 
189
 
 
190
        if (pFunc && PyCallable_Check(pFunc)) {
 
191
                PyObject *pValue = PyObject_CallFunction(pFunc, NULL);
 
192
 
 
193
                if (pValue) {
 
194
                        if (pValue != Py_None) {
 
195
                                Py_DECREF(pValue);
 
196
                        }
 
197
                } else {
 
198
                        if (PyErr_Occurred()) {
 
199
                                PyErr_Print();
 
200
                                PyErr_Clear();
 
201
                        }
 
202
                }
 
203
        }
 
204
}
 
205
 
 
206
static enum evhook_status
 
207
script_hook_quit(va_list ap, void *data)
 
208
{
 
209
        if (pDict) do_script_hook_quit();
 
210
        return EVENT_HOOK_STATUS_NEXT;
 
211
}
 
212
 
 
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,
 
220
};