2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail Team
4 * Copyright (C) 2014 Ricardo Mones
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU 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.
11 * This program 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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
28 #include <curl/curl.h>
31 #include "libravatar.h"
32 #include "libravatar_prefs.h"
33 #include "libravatar_missing.h"
34 #include "prefs_common.h"
35 #include "procheader.h"
40 /* indexes of keys are default_mode - 10 if applicable */
41 static const char *def_mode[] = {
42 "404", /* not used, only useful in web pages */
50 static guint update_hook_id;
51 static guint render_hook_id;
52 static gchar *cache_dir = NULL; /* dir-separator terminated */
54 static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
56 AvatarCaptureData *acd = (AvatarCaptureData *)source;
58 debug_print("libravatar avatar_header_update invoked\n");
60 if (!strcmp(acd->header, "From:")) {
63 a = g_strdup(acd->content);
67 for (lower = a; *lower; lower++)
68 *lower = g_ascii_tolower(*lower);
70 debug_print("libravatar added '%s'\n", a);
71 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
74 return FALSE; /* keep getting */
77 static gchar *federated_base_url_from_address(const gchar *address)
80 TODO: no federation supported right now
81 Details on http://wiki.libravatar.org/running_your_own/
83 return g_strdup(libravatarprefs.base_url);
86 static GtkWidget *image_widget_from_filename(const gchar *filename)
88 GtkWidget *image = NULL;
89 GdkPixbuf *picture = NULL;
93 gdk_pixbuf_get_file_info(filename, &w, &h);
95 if (w != AVATAR_SIZE || h != AVATAR_SIZE)
96 /* server can provide a different size from the requested in URL */
97 picture = gdk_pixbuf_new_from_file_at_scale(
98 filename, AVATAR_SIZE, AVATAR_SIZE, TRUE, &error);
100 picture = gdk_pixbuf_new_from_file(filename, &error);
103 g_warning("Failed to load image '%s': %s\n", filename, error->message);
107 image = gtk_image_new_from_pixbuf(picture);
108 g_object_unref(picture);
110 g_warning("Failed to load image '%s': no error returned!\n", filename);
116 static gchar *cache_name_for_md5(const gchar *md5)
118 if (libravatarprefs.default_mode >= DEF_MODE_MM
119 && libravatarprefs.default_mode <= DEF_MODE_RETRO) {
120 /* cache dir for generated avatars */
121 return g_strconcat(cache_dir, def_mode[libravatarprefs.default_mode - 10],
122 G_DIR_SEPARATOR_S, md5, NULL);
124 /* default cache dir */
125 return g_strconcat(cache_dir, md5, NULL);
128 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
130 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
131 debug_print("received %zu bytes from avatar server\n", written);
136 static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
138 GtkWidget *image = NULL;
143 curl = curl_easy_init();
145 g_warning("could not initialize curl to get image from url\n");
148 curl_easy_setopt(curl, CURLOPT_URL, url);
149 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
151 filename = cache_name_for_md5(md5);
152 file = fopen(filename, "wb");
156 if (libravatarprefs.allow_redirects) {
157 long maxredirs = (libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L;
159 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
160 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
162 curl_easy_setopt(curl, CURLOPT_FILE, file);
163 debug_print("retrieving URL to file: %s -> %s\n", url, filename);
164 curl_easy_perform(curl);
165 filesize = ftell(file);
168 if (filesize < MIN_PNG_SIZE)
169 debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
171 image = image_widget_from_filename(filename);
173 if (!libravatarprefs.cache_icons) {
174 if (g_unlink(filename) < 0)
175 g_warning("failed to delete cache file %s\n", filename);
179 missing_add_md5(libravatarmisses, md5);
181 g_warning("could not open '%s' for writting\n", filename);
183 curl_easy_cleanup(curl);
189 static gboolean is_recent_enough(const gchar *filename)
194 if (libravatarprefs.cache_icons) {
196 if (t != (time_t)-1 && !g_stat(filename, &s)) {
197 if (t - s.st_ctime <= libravatarprefs.cache_interval * 3600)
202 return FALSE; /* re-download */
205 static GtkWidget *image_widget_from_cached_md5(const gchar *md5)
207 GtkWidget *image = NULL;
210 filename = cache_name_for_md5(md5);
211 if (is_file_exist(filename) && is_recent_enough(filename)) {
212 debug_print("found cached image for %s\n", md5);
213 image = image_widget_from_filename(filename);
220 static gchar *libravatar_url_for_md5(const gchar *base, const gchar *md5)
222 if (libravatarprefs.default_mode >= DEF_MODE_404) {
223 return g_strdup_printf("%s/%s?s=%u&d=%s",
224 base, md5, AVATAR_SIZE,
225 def_mode[libravatarprefs.default_mode - 10]);
226 } else if (libravatarprefs.default_mode == DEF_MODE_URL) {
227 return g_strdup_printf("%s/%s?s=%u&d=%s",
228 base, md5, AVATAR_SIZE,
229 libravatarprefs.default_mode_url);
230 } else if (libravatarprefs.default_mode == DEF_MODE_NONE) {
231 return g_strdup_printf("%s/%s?s=%u",
232 base, md5, AVATAR_SIZE);
235 g_warning("invalid libravatar default mode: %d\n", libravatarprefs.default_mode);
239 static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
241 AvatarRender *ar = (AvatarRender *)source;
242 GtkWidget *image = NULL;
243 gchar *a = NULL, *url = NULL;
246 debug_print("libravatar avatar_image_render invoked\n");
248 a = procmsg_msginfo_get_avatar(ar->full_msginfo, AVATAR_LIBRAVATAR);
252 md5_hex_digest(md5sum, a);
253 /* try missing cache */
254 if (is_missing_md5(libravatarmisses, md5sum)) {
258 image = image_widget_from_cached_md5(md5sum);
260 if (ar->image) /* previous plugin set one */
261 gtk_widget_destroy(ar->image);
265 /* not cached copy: try network */
266 if (prefs_common.work_offline) {
267 debug_print("working off-line: libravatar network retrieval skipped\n");
270 base = federated_base_url_from_address(a);
271 url = libravatar_url_for_md5(base, md5sum);
273 image = image_widget_from_url(url, md5sum);
276 if (ar->image) /* previous plugin set one */
277 gtk_widget_destroy(ar->image);
286 return FALSE; /* keep rendering */
289 static gint cache_dir_init()
294 cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
295 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
297 if (!is_dir_exist(cache_dir)) {
298 if (make_dir(cache_dir) < 0) {
303 for (i = DEF_MODE_MM; i <= DEF_MODE_RETRO; ++i) {
304 subdir = g_strconcat(cache_dir, def_mode[i - 10], NULL);
305 if (!is_dir_exist(subdir)) {
306 if (make_dir(subdir) < 0) {
307 g_warning("cannot create directory %s\n", subdir);
318 static gint missing_cache_init()
320 gchar *cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
321 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
322 LIBRAVATAR_MISSING_FILE, NULL);
324 libravatarmisses = missing_load_from_file(cache_file);
327 if (libravatarmisses == NULL)
333 static void missing_cache_done()
337 if (libravatarmisses != NULL) {
338 cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
339 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
340 LIBRAVATAR_MISSING_FILE, NULL);
341 missing_save_to_file(libravatarmisses, cache_file);
343 g_hash_table_destroy(libravatarmisses);
350 * @param error For storing the returned error message.
352 * @return 0 if initialization succeeds, -1 on failure.
354 gint plugin_init(gchar **error)
356 if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,9,3,29),
357 VERSION_NUMERIC, _("Libravatar"), error))
359 /* get info from headers */
360 update_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
361 libravatar_header_update_hook,
363 if (update_hook_id == -1) {
364 *error = g_strdup(_("Failed to register avatar header update hook"));
367 /* get image for displaying */
368 render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
369 libravatar_image_render_hook,
371 if (render_hook_id == -1) {
372 *error = g_strdup(_("Failed to register avatar image render hook"));
376 if (cache_dir_init() == -1) {
377 *error = g_strdup(_("Failed to create avatar image cache directory"));
380 /* preferences page */
381 libravatar_prefs_init();
383 curl_global_init(CURL_GLOBAL_DEFAULT);
385 if (missing_cache_init() == -1) {
386 *error = g_strdup(_("Failed to load missing items cache"));
389 debug_print("Libravatar plugin loaded\n");
395 * Destructor for the plugin.
396 * Unregister the callback function and frees matcher.
398 * @return Always TRUE.
400 gboolean plugin_done(void)
402 if (render_hook_id != -1) {
403 hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
407 if (update_hook_id != -1) {
408 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
412 libravatar_prefs_done();
413 missing_cache_done();
414 if (cache_dir != NULL)
416 debug_print("Libravatar plugin unloaded\n");
422 * Get the name of the plugin.
424 * @return The plugin's name, maybe translated.
426 const gchar *plugin_name(void)
428 return _("Libravatar");
432 * Get the description of the plugin.
434 * @return The plugin's description, maybe translated.
436 const gchar *plugin_desc(void)
438 return _("Display libravatar profiles' images for mail messages. More\n"
439 "info about libravatar at http://www.libravatar.org/. If you have\n"
440 "a gravatar.com profile but not a libravatar one, those will also\n"
441 "be retrieved (when redirections are allowed in plugin config).\n"
442 "Plugin config page it's available from main window at:\n"
443 "/Configuration/Preferences/Plugins/Libravatar.\n\n"
444 "This plugin uses libcurl to retrieve images, so if you're behind a\n"
445 "proxy please refer to curl(1) manpage for details on 'http_proxy'\n"
446 "configuration. More details about this and others on README file.\n\n"
447 "Feedback to <ricardo@mones.org> is welcome.\n");
451 * Get the kind of plugin.
453 * @return The "GTK2" constant.
455 const gchar *plugin_type(void)
461 * Get the license acronym the plugin is released under.
463 * @return The "GPL3+" constant.
465 const gchar *plugin_licence(void)
471 * Get the version of the plugin.
473 * @return The current version string.
475 const gchar *plugin_version(void)
481 * Get the features implemented by the plugin.
483 * @return A constant PluginFeature structure with the features.
485 struct PluginFeature *plugin_provides(void)
487 static struct PluginFeature features[] =
488 { {PLUGIN_OTHER, N_("Libravatar")},
489 {PLUGIN_NOTHING, NULL}};