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 */
53 static GHashTable *misses;
55 static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
57 AvatarCaptureData *acd = (AvatarCaptureData *)source;
59 debug_print("libravatar avatar_header_update invoked\n");
61 if (!strcmp(acd->header, "From:")) {
62 gchar *a = g_strdup(acd->content);
64 debug_print("libravatar added '%s'\n", a);
65 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
68 return FALSE; /* keep getting */
71 static gchar *federated_base_url_from_address(const gchar *address)
74 TODO: no federation supported right now
75 Details on http://wiki.libravatar.org/running_your_own/
77 return g_strdup(libravatarprefs.base_url);
80 static GtkWidget *image_widget_from_filename(const gchar *filename)
82 GtkWidget *image = NULL;
83 GdkPixbuf *picture = NULL;
87 gdk_pixbuf_get_file_info(filename, &w, &h);
89 if (w != AVATAR_SIZE || h != AVATAR_SIZE)
90 /* server can provide a different size from the requested in URL */
91 picture = gdk_pixbuf_new_from_file_at_scale(
92 filename, AVATAR_SIZE, AVATAR_SIZE, TRUE, &error);
94 picture = gdk_pixbuf_new_from_file(filename, &error);
97 g_warning("Failed to load image '%s': %s\n", filename, error->message);
101 image = gtk_image_new_from_pixbuf(picture);
102 g_object_unref(picture);
104 g_warning("Failed to load image '%s': no error returned!\n", filename);
110 static gchar *cache_name_for_md5(const gchar *md5)
112 if (libravatarprefs.default_mode >= DEF_MODE_MM
113 && libravatarprefs.default_mode <= DEF_MODE_RETRO) {
114 /* cache dir for generated avatars */
115 return g_strconcat(cache_dir, def_mode[libravatarprefs.default_mode - 10],
116 G_DIR_SEPARATOR_S, md5, NULL);
118 /* default cache dir */
119 return g_strconcat(cache_dir, md5, NULL);
122 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
124 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
125 debug_print("received %zu bytes from avatar server\n", written);
130 static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
132 GtkWidget *image = NULL;
137 curl = curl_easy_init();
139 g_warning("could not initialize curl to get image from url\n");
142 curl_easy_setopt(curl, CURLOPT_URL, url);
143 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
145 filename = cache_name_for_md5(md5);
146 file = fopen(filename, "wb");
150 if (libravatarprefs.allow_redirects) {
151 long maxredirs = (libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L;
153 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
154 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
156 curl_easy_setopt(curl, CURLOPT_FILE, file);
157 debug_print("retrieving URL to file: %s -> %s\n", url, filename);
158 curl_easy_perform(curl);
159 filesize = ftell(file);
162 if (filesize < MIN_PNG_SIZE)
163 debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
165 image = image_widget_from_filename(filename);
167 if (!libravatarprefs.cache_icons) {
168 if (g_unlink(filename) < 0)
169 g_warning("failed to delete cache file %s\n", filename);
173 missing_add_md5(misses, md5);
175 g_warning("could not open '%s' for writting\n", filename);
177 curl_easy_cleanup(curl);
183 static gboolean is_recent_enough(const gchar *filename)
188 if (libravatarprefs.cache_icons) {
190 if (t != (time_t)-1 && !g_stat(filename, &s)) {
191 if (t - s.st_ctime <= libravatarprefs.cache_interval * 3600)
196 return FALSE; /* re-download */
199 static GtkWidget *image_widget_from_cached_md5(const gchar *md5)
201 GtkWidget *image = NULL;
204 filename = cache_name_for_md5(md5);
205 if (is_file_exist(filename) && is_recent_enough(filename)) {
206 debug_print("found cached image for %s\n", md5);
207 image = image_widget_from_filename(filename);
214 static gchar *libravatar_url_for_md5(const gchar *base, const gchar *md5)
216 if (libravatarprefs.default_mode >= DEF_MODE_404) {
217 return g_strdup_printf("%s/%s?s=%u&d=%s",
218 base, md5, AVATAR_SIZE,
219 def_mode[libravatarprefs.default_mode - 10]);
220 } else if (libravatarprefs.default_mode == DEF_MODE_URL) {
221 return g_strdup_printf("%s/%s?s=%u&d=%s",
222 base, md5, AVATAR_SIZE,
223 libravatarprefs.default_mode_url);
224 } else if (libravatarprefs.default_mode == DEF_MODE_NONE) {
225 return g_strdup_printf("%s/%s?s=%u",
226 base, md5, AVATAR_SIZE);
229 g_warning("invalid libravatar default mode: %d\n", libravatarprefs.default_mode);
233 static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
235 AvatarRender *ar = (AvatarRender *)source;
236 GtkWidget *image = NULL;
237 gchar *a = NULL, *url = NULL;
240 debug_print("libravatar avatar_image_render invoked\n");
242 a = procmsg_msginfo_get_avatar(ar->full_msginfo, AVATAR_LIBRAVATAR);
246 md5_hex_digest(md5sum, a);
247 /* try missing cache */
248 if (is_missing_md5(misses, md5sum)) {
252 image = image_widget_from_cached_md5(md5sum);
254 if (ar->image) /* previous plugin set one */
255 gtk_widget_destroy(ar->image);
259 /* not cached copy: try network */
260 if (prefs_common.work_offline) {
261 debug_print("working off-line: libravatar network retrieval skipped\n");
264 base = federated_base_url_from_address(a);
265 url = libravatar_url_for_md5(base, md5sum);
267 image = image_widget_from_url(url, md5sum);
270 if (ar->image) /* previous plugin set one */
271 gtk_widget_destroy(ar->image);
280 return FALSE; /* keep rendering */
283 static gint cache_dir_init()
288 cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
289 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
291 if (!is_dir_exist(cache_dir)) {
292 if (make_dir(cache_dir) < 0) {
297 for (i = DEF_MODE_MM; i <= DEF_MODE_RETRO; ++i) {
298 subdir = g_strconcat(cache_dir, def_mode[i - 10], NULL);
299 if (!is_dir_exist(subdir)) {
300 if (make_dir(subdir) < 0) {
301 g_warning("cannot create directory %s\n", subdir);
312 static gint missing_cache_init()
314 gchar *cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
315 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
316 LIBRAVATAR_MISSING_FILE, NULL);
318 misses = missing_load_from_file(cache_file);
327 static void missing_cache_done()
331 if (misses != NULL) {
332 cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
333 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
334 LIBRAVATAR_MISSING_FILE, NULL);
335 missing_save_to_file(misses, cache_file);
337 g_hash_table_destroy(misses);
344 * @param error For storing the returned error message.
346 * @return 0 if initialization succeeds, -1 on failure.
348 gint plugin_init(gchar **error)
350 if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,9,3,29),
351 VERSION_NUMERIC, _("Libravatar"), error))
353 /* get info from headers */
354 update_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
355 libravatar_header_update_hook,
357 if (update_hook_id == -1) {
358 *error = g_strdup(_("Failed to register avatar header update hook"));
361 /* get image for displaying */
362 render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
363 libravatar_image_render_hook,
365 if (render_hook_id == -1) {
366 *error = g_strdup(_("Failed to register avatar image render hook"));
370 if (cache_dir_init() == -1) {
371 *error = g_strdup(_("Failed to create avatar image cache directory"));
374 /* preferences page */
375 libravatar_prefs_init();
377 curl_global_init(CURL_GLOBAL_DEFAULT);
379 if (missing_cache_init() == -1) {
380 *error = g_strdup(_("Failed to load missing items cache"));
383 debug_print("Libravatar plugin loaded\n");
389 * Destructor for the plugin.
390 * Unregister the callback function and frees matcher.
392 * @return Always TRUE.
394 gboolean plugin_done(void)
396 if (render_hook_id != -1) {
397 hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
401 if (update_hook_id != -1) {
402 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
406 libravatar_prefs_done();
407 missing_cache_done();
408 if (cache_dir != NULL)
410 debug_print("Libravatar plugin unloaded\n");
416 * Get the name of the plugin.
418 * @return The plugin's name, maybe translated.
420 const gchar *plugin_name(void)
422 return _("Libravatar");
426 * Get the description of the plugin.
428 * @return The plugin's description, maybe translated.
430 const gchar *plugin_desc(void)
432 return _("Get and display libravatar images for mail messages.\n\n"
433 "Info about libravatar at http://www.libravatar.org/\n\n"
434 "Feedback to <ricardo@mones.org> is welcome.\n");
438 * Get the kind of plugin.
440 * @return The "GTK2" constant.
442 const gchar *plugin_type(void)
448 * Get the license acronym the plugin is released under.
450 * @return The "GPL3+" constant.
452 const gchar *plugin_licence(void)
458 * Get the version of the plugin.
460 * @return The current version string.
462 const gchar *plugin_version(void)
468 * Get the features implemented by the plugin.
470 * @return A constant PluginFeature structure with the features.
472 struct PluginFeature *plugin_provides(void)
474 static struct PluginFeature features[] =
475 { {PLUGIN_OTHER, N_("Libravatar")},
476 {PLUGIN_NOTHING, NULL}};