File: plugin.c

package info (click to toggle)
wget2 2.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,468 kB
  • sloc: ansic: 121,166; sh: 11,559; makefile: 878; xml: 182; sed: 16
file content (252 lines) | stat: -rw-r--r-- 8,102 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright (c) 2017-2024 Free Software Foundation, Inc.
 *
 * This file is part of libwget.
 *
 * Libwget is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Libwget is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
 *
 *
 * Plugin support
 *
 */

#include <config.h>

#include <wget.h>
#include "private.h"

/**
 * \file
 * \brief Plugin API for wget2
 * \defgroup libwget-plugin Plugin API for wget2
 * @{
 *
 * This is the plugin API for wget2.
 *
 * Each plugin must define a `wget_plugin_initializer()` function which will be called when the plugin is loaded.
 * See \ref wget_plugin_initializer_t "wget_plugin_initializer_t" for the prototype.
 * `wget_plugin_initializer()` must also be declared to be exported using `WGET_EXPORT`.
 */

/**
 * Gets the name the plugin is known as.
 * \param[in] plugin The plugin handle
 * \return the name of this plugin. The returned string is owned by wget and should not be freed or altered.
 */
const char *wget_plugin_get_name(wget_plugin *plugin)
{
	return plugin->vtable->get_name(plugin);
}

/**
 * Registers a function to be called when wget exits.
 * \param[in] plugin The plugin handle
 * \param[in] fn A function pointer to be called
 */
void wget_plugin_register_finalizer(wget_plugin *plugin, wget_plugin_finalizer_fn *fn)
{
	plugin->vtable->register_finalizer(plugin, fn);
}

/**
 * Registers a function for command line option forwarding.
 *
 * A option can be forwarded using an option following the pattern:
 *
 *     --plugin-opt=<plugin-name>.<option>[=<value>]
 *
 * \param[in] plugin The plugin handle
 * \param[in] fn The function pointer to register
 */
void wget_plugin_register_option_callback(wget_plugin *plugin, wget_plugin_option_callback *fn)
{
	plugin->vtable->register_argp(plugin, fn);
}

/**
 * Marks the intercepted URL to be rejected. The URL will not be fetched by wget2 or passed to remaining plugins.
 *
 * Mutually exclusive with `wget_intercept_action_accept()`.
 *
 * \param action Handle for any action taken by the plugin
 */
void wget_intercept_action_reject(wget_intercept_action *action)
{
	action->vtable->action_reject(action);
}

/**
 * Marks the intercepted URL to be accepted.
 * The URL will not be passed to remaining plugins. wget2 will not filter the URL by any accept or reject pattern.
 *
 * Mutually exclusive with `wget_intercept_action_reject()`.
 *
 * \param action Handle for any action taken by the plugin
 */
void wget_intercept_action_accept(wget_intercept_action *action)
{
	action->vtable->action_accept(action);
}

/**
 * Specifies an alternative URL to be fetched instead of the intercepted URL.
 *
 * \param action Handle for any action taken by the plugin
 * \param iri Alternative URL to be fetched
 */
void wget_intercept_action_set_alt_url(wget_intercept_action *action, const wget_iri *iri)
{
	action->vtable->action_set_alt_url(action, iri);
}

/**
 * Specifies that the fetched data from intercepted URL should be written to an alternative file.
 *
 * \param action Handle for any action taken by the plugin
 * \param local_filename Alternative file name to use
 */
void wget_intercept_action_set_local_filename(wget_intercept_action *action, const char *local_filename)
{
	action->vtable->action_set_local_filename(action, local_filename);
}

/**
 * Registers a plugin function for intercepting URLs
 *
 * The registered function will be passed an abstract object of type
 * \ref wget_intercept_action_t "wget_intercept_action_t" which can be used to influence how wget will process the
 * URL.
 *
 * \see wget_intercept_action_reject
 * \see wget_intercept_action_accept
 * \see wget_intercept_action_set_alt_url
 * \see wget_intercept_action_set_local_filename
 *
 * \param[in] plugin The plugin handle
 * \param[in] filter_fn The plugin function that will be passed the URL to be fetched
 */
void wget_plugin_register_url_filter_callback(wget_plugin *plugin, wget_plugin_url_filter_callback *filter_fn)
{
	plugin->vtable->register_url_filter(plugin, filter_fn);
}

/**
 * Gets the source address the file was downloaded from.
 *
 * \param[in] file Downloaded file handle
 * \return The address the file was downloaded from. The returned object is owned by wget and should not be free'd.
 */
const wget_iri *wget_downloaded_file_get_source_url(wget_downloaded_file *file)
{
	return file->vtable->file_get_source_url(file);
}

/**
 * Gets the file name the downloaded file was written to.
 *
 * \param[in] file Downloaded file handle
 * \return The file name the file was written to. The returned string is owned by wget and should not be free'd.
 */
const char *wget_downloaded_file_get_local_filename(wget_downloaded_file *file)
{
	return file->vtable->file_get_local_filename(file);
}

/**
 * Gets the size of the downloaded file.
 *
 * \param[in] file Downloaded file handle
 * \return The size of the downloaded file
 */
uint64_t wget_downloaded_file_get_size(wget_downloaded_file *file)
{
	return file->vtable->file_get_size(file);
}

/**
 * Reads the downloaded file into memory.
 *
 * Be careful, reading large files into memory can cause all sorts of problems like running out of memory.
 * Use \ref wget_downloaded_file_open_stream "wget_downloaded_file_open_stream()" whenever possible.
 *
 * \param[in] file Downloaded file handle
 * \param[out] data The contents of the downloaded file.
 *                  The memory is owned by wget and must not be free'd or modified.
 * \param[out] size Size of the downloaded file.
 */
int wget_downloaded_file_get_contents(wget_downloaded_file *file, const void **data, size_t *size)
{
	return file->vtable->file_get_contents(file, data, size);
}

/**
 * Opens the downloaded file as a new stream.
 *
 * \param[in] file Downloaded file handle
 * \return A newly opened stream for reading. The returned stream must be closed with fclose() after use.
 */
FILE *wget_downloaded_file_open_stream(wget_downloaded_file *file)
{
	return file->vtable->file_open_stream(file);
}

/**
 * Gets whether the downloaded file should be scanned for more URLs.
 *
 * \param[in] file Downloaded file handle
 * \return whether the file should be scanned for more URLs.
 */
bool wget_downloaded_file_get_recurse(wget_downloaded_file *file)
{
	return file->vtable->file_get_recurse(file);
}

/**
 * Adds a URL for recursive downloading. This function has no effect if
 * \ref wget_downloaded_file_get_recurse "wget_downloaded_file_get_recurse()" returns false.
 *
 * \param[in] file Downloaded file handle
 * \param[in] iri The URL to be fetched.
 */
void wget_downloaded_file_add_recurse_url(wget_downloaded_file *file, const wget_iri *iri)
{
	file->vtable->file_add_recurse_url(file, iri);
}

/**
 * Registers a plugin function for intercepting downloaded files.
 *
 * The registered function will be passed an abstract object of type
 * \ref wget_downloaded_file_t "wget_downloaded_file_t" which can be used to fetch the contents of the downloaded
 * files and adding parsed URLs for recursive downloading.
 *
 * \see wget_downloaded_file_get_source_url
 * \see wget_downloaded_file_get_local_filename
 * \see wget_downloaded_file_get_size
 * \see wget_downloaded_file_get_contents
 * \see wget_downloaded_file_open_stream
 * \see wget_downloaded_file_get_recurse
 * \see wget_downloaded_file_add_recurse_url
 *
 * \param[in] plugin The plugin handle
 * \param[in] fn The plugin function that will be passed a handle to downloaded files.
 *
 */
void
wget_plugin_register_post_processor(wget_plugin *plugin, wget_plugin_post_processor *fn)
{
	plugin->vtable->register_post_processor(plugin, fn);
}

/** @} */