1 /*
2 * Copyright (c) 2017-2021 Free Software Foundation, Inc.
3 *
4 * This file is part of libwget.
5 *
6 * Libwget is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libwget is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with libwget. If not, see <https://www.gnu.org/licenses/>.
18 *
19 *
20 * Plugin support
21 *
22 */
23
24 #include <config.h>
25
26 #include <wget.h>
27 #include "private.h"
28
29 /**
30 * \file
31 * \brief Plugin API for wget2
32 * \defgroup libwget-plugin Plugin API for wget2
33 * @{
34 *
35 * This is the plugin API for wget2.
36 *
37 * Each plugin must define a `wget_plugin_initializer()` function which will be called when the plugin is loaded.
38 * See \ref wget_plugin_initializer_t "wget_plugin_initializer_t" for the prototype.
39 * `wget_plugin_initializer()` must also be declared to be exported using `WGET_EXPORT`.
40 */
41
42 /**
43 * Gets the name the plugin is known as.
44 * \param[in] plugin The plugin handle
45 * \return the name of this plugin. The returned string is owned by wget and should not be freed or altered.
46 */
wget_plugin_get_name(wget_plugin * plugin)47 const char *wget_plugin_get_name(wget_plugin *plugin)
48 {
49 return plugin->vtable->get_name(plugin);
50 }
51
52 /**
53 * Registers a function to be called when wget exits.
54 * \param[in] plugin The plugin handle
55 * \param[in] fn A function pointer to be called
56 */
wget_plugin_register_finalizer(wget_plugin * plugin,wget_plugin_finalizer_fn * fn)57 void wget_plugin_register_finalizer(wget_plugin *plugin, wget_plugin_finalizer_fn *fn)
58 {
59 plugin->vtable->register_finalizer(plugin, fn);
60 }
61
62 /**
63 * Registers a function for command line option forwarding.
64 *
65 * A option can be forwarded using an option following the pattern:
66 *
67 * --plugin-opt=<plugin-name>.<option>[=<value>]
68 *
69 * \param[in] plugin The plugin handle
70 * \param[in] fn The function pointer to register
71 */
wget_plugin_register_option_callback(wget_plugin * plugin,wget_plugin_option_callback * fn)72 void wget_plugin_register_option_callback(wget_plugin *plugin, wget_plugin_option_callback *fn)
73 {
74 plugin->vtable->register_argp(plugin, fn);
75 }
76
77 /**
78 * Marks the intercepted URL to be rejected. The URL will not be fetched by wget2 or passed to remaining plugins.
79 *
80 * Mutually exclusive with `wget_intercept_action_accept()`.
81 *
82 * \param action Handle for any action taken by the plugin
83 */
wget_intercept_action_reject(wget_intercept_action * action)84 void wget_intercept_action_reject(wget_intercept_action *action)
85 {
86 action->vtable->action_reject(action);
87 }
88
89 /**
90 * Marks the intercepted URL to be accepted.
91 * The URL will not be passed to remaining plugins. wget2 will not filter the URL by any accept or reject pattern.
92 *
93 * Mutually exclusive with `wget_intercept_action_reject()`.
94 *
95 * \param action Handle for any action taken by the plugin
96 */
wget_intercept_action_accept(wget_intercept_action * action)97 void wget_intercept_action_accept(wget_intercept_action *action)
98 {
99 action->vtable->action_accept(action);
100 }
101
102 /**
103 * Specifies an alternative URL to be fetched instead of the intercepted URL.
104 *
105 * \param action Handle for any action taken by the plugin
106 * \param iri Alternative URL to be fetched
107 */
wget_intercept_action_set_alt_url(wget_intercept_action * action,const wget_iri * iri)108 void wget_intercept_action_set_alt_url(wget_intercept_action *action, const wget_iri *iri)
109 {
110 action->vtable->action_set_alt_url(action, iri);
111 }
112
113 /**
114 * Specifies that the fetched data from intercepted URL should be written to an alternative file.
115 *
116 * \param action Handle for any action taken by the plugin
117 * \param local_filename Alternative file name to use
118 */
wget_intercept_action_set_local_filename(wget_intercept_action * action,const char * local_filename)119 void wget_intercept_action_set_local_filename(wget_intercept_action *action, const char *local_filename)
120 {
121 action->vtable->action_set_local_filename(action, local_filename);
122 }
123
124 /**
125 * Registers a plugin function for intercepting URLs
126 *
127 * The registered function will be passed an abstract object of type
128 * \ref wget_intercept_action_t "wget_intercept_action_t" which can be used to influence how wget will process the
129 * URL.
130 *
131 * \see wget_intercept_action_reject
132 * \see wget_intercept_action_accept
133 * \see wget_intercept_action_set_alt_url
134 * \see wget_intercept_action_set_local_filename
135 *
136 * \param[in] plugin The plugin handle
137 * \param[in] filter_fn The plugin function that will be passed the URL to be fetched
138 */
wget_plugin_register_url_filter_callback(wget_plugin * plugin,wget_plugin_url_filter_callback * filter_fn)139 void wget_plugin_register_url_filter_callback(wget_plugin *plugin, wget_plugin_url_filter_callback *filter_fn)
140 {
141 plugin->vtable->register_url_filter(plugin, filter_fn);
142 }
143
144 /**
145 * Gets the source address the file was downloaded from.
146 *
147 * \param[in] file Downloaded file handle
148 * \return The address the file was downloaded from. The returned object is owned by wget and should not be free'd.
149 */
wget_downloaded_file_get_source_url(wget_downloaded_file * file)150 const wget_iri *wget_downloaded_file_get_source_url(wget_downloaded_file *file)
151 {
152 return file->vtable->file_get_source_url(file);
153 }
154
155 /**
156 * Gets the file name the downloaded file was written to.
157 *
158 * \param[in] file Downloaded file handle
159 * \return The file name the file was written to. The returned string is owned by wget and should not be free'd.
160 */
wget_downloaded_file_get_local_filename(wget_downloaded_file * file)161 const char *wget_downloaded_file_get_local_filename(wget_downloaded_file *file)
162 {
163 return file->vtable->file_get_local_filename(file);
164 }
165
166 /**
167 * Gets the size of the downloaded file.
168 *
169 * \param[in] file Downloaded file handle
170 * \return The size of the downloaded file
171 */
wget_downloaded_file_get_size(wget_downloaded_file * file)172 uint64_t wget_downloaded_file_get_size(wget_downloaded_file *file)
173 {
174 return file->vtable->file_get_size(file);
175 }
176
177 /**
178 * Reads the downloaded file into memory.
179 *
180 * Be careful, reading large files into memory can cause all sorts of problems like running out of memory.
181 * Use \ref wget_downloaded_file_open_stream "wget_downloaded_file_open_stream()" whenever possible.
182 *
183 * \param[in] file Downloaded file handle
184 * \param[out] data The contents of the downloaded file.
185 * The memory is owned by wget and must not be free'd or modified.
186 * \param[out] size Size of the downloaded file.
187 */
wget_downloaded_file_get_contents(wget_downloaded_file * file,const void ** data,size_t * size)188 int wget_downloaded_file_get_contents(wget_downloaded_file *file, const void **data, size_t *size)
189 {
190 return file->vtable->file_get_contents(file, data, size);
191 }
192
193 /**
194 * Opens the downloaded file as a new stream.
195 *
196 * \param[in] file Downloaded file handle
197 * \return A newly opened stream for reading. The returned stream must be closed with fclose() after use.
198 */
wget_downloaded_file_open_stream(wget_downloaded_file * file)199 FILE *wget_downloaded_file_open_stream(wget_downloaded_file *file)
200 {
201 return file->vtable->file_open_stream(file);
202 }
203
204 /**
205 * Gets whether the downloaded file should be scanned for more URLs.
206 *
207 * \param[in] file Downloaded file handle
208 * \return whether the file should be scanned for more URLs.
209 */
wget_downloaded_file_get_recurse(wget_downloaded_file * file)210 bool wget_downloaded_file_get_recurse(wget_downloaded_file *file)
211 {
212 return file->vtable->file_get_recurse(file);
213 }
214
215 /**
216 * Adds a URL for recursive downloading. This function has no effect if
217 * \ref wget_downloaded_file_get_recurse "wget_downloaded_file_get_recurse()" returns false.
218 *
219 * \param[in] file Downloaded file handle
220 * \param[in] iri The URL to be fetched.
221 */
wget_downloaded_file_add_recurse_url(wget_downloaded_file * file,const wget_iri * iri)222 void wget_downloaded_file_add_recurse_url(wget_downloaded_file *file, const wget_iri *iri)
223 {
224 file->vtable->file_add_recurse_url(file, iri);
225 }
226
227 /**
228 * Registers a plugin function for intercepting downloaded files.
229 *
230 * The registered function will be passed an abstract object of type
231 * \ref wget_downloaded_file_t "wget_downloaded_file_t" which can be used to fetch the contents of the downloaded
232 * files and adding parsed URLs for recursive downloading.
233 *
234 * \see wget_downloaded_file_get_source_url
235 * \see wget_downloaded_file_get_local_filename
236 * \see wget_downloaded_file_get_size
237 * \see wget_downloaded_file_get_contents
238 * \see wget_downloaded_file_open_stream
239 * \see wget_downloaded_file_get_recurse
240 * \see wget_downloaded_file_add_recurse_url
241 *
242 * \param[in] plugin The plugin handle
243 * \param[in] fn The plugin function that will be passed a handle to downloaded files.
244 *
245 */
246 void
wget_plugin_register_post_processor(wget_plugin * plugin,wget_plugin_post_processor * fn)247 wget_plugin_register_post_processor(wget_plugin *plugin, wget_plugin_post_processor *fn)
248 {
249 plugin->vtable->register_post_processor(plugin, fn);
250 }
251
252 /** @} */
253