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:")) {
61 gchar *a = g_strdup(acd->content);
63 debug_print("libravatar added '%s'\n", a);
64 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
67 return FALSE; /* keep getting */
70 static gchar *federated_base_url_from_address(const gchar *address)
73 TODO: no federation supported right now
74 Details on http://wiki.libravatar.org/running_your_own/
76 return g_strdup(libravatarprefs.base_url);
79 static GtkWidget *image_widget_from_filename(const gchar *filename)
81 GtkWidget *image = NULL;
82 GdkPixbuf *picture = NULL;
86 gdk_pixbuf_get_file_info(filename, &w, &h);
88 if (w != AVATAR_SIZE || h != AVATAR_SIZE)
89 /* server can provide a different size from the requested in URL */
90 picture = gdk_pixbuf_new_from_file_at_scale(
91 filename, AVATAR_SIZE, AVATAR_SIZE, TRUE, &error);
93 picture = gdk_pixbuf_new_from_file(filename, &error);
96 g_warning("Failed to load image '%s': %s\n", filename, error->message);
100 image = gtk_image_new_from_pixbuf(picture);
101 g_object_unref(picture);
103 g_warning("Failed to load image '%s': no error returned!\n", filename);
109 static gchar *cache_name_for_md5(const gchar *md5)
111 if (libravatarprefs.default_mode >= DEF_MODE_MM
112 && libravatarprefs.default_mode <= DEF_MODE_RETRO) {
113 /* cache dir for generated avatars */
114 return g_strconcat(cache_dir, def_mode[libravatarprefs.default_mode - 10],
115 G_DIR_SEPARATOR_S, md5, NULL);
117 /* default cache dir */
118 return g_strconcat(cache_dir, md5, NULL);
121 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
123 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
124 debug_print("received %zu bytes from avatar server\n", written);
129 static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
131 GtkWidget *image = NULL;
136 curl = curl_easy_init();
138 g_warning("could not initialize curl to get image from url\n");
141 curl_easy_setopt(curl, CURLOPT_URL, url);
142 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
144 filename = cache_name_for_md5(md5);
145 file = fopen(filename, "wb");
149 if (libravatarprefs.allow_redirects) {
150 long maxredirs = (libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L;
152 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
153 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
155 curl_easy_setopt(curl, CURLOPT_FILE, file);
156 debug_print("retrieving URL to file: %s -> %s\n", url, filename);
157 curl_easy_perform(curl);
158 filesize = ftell(file);
161 if (filesize < MIN_PNG_SIZE)
162 debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
164 image = image_widget_from_filename(filename);
166 if (!libravatarprefs.cache_icons) {
167 if (g_unlink(filename) < 0)
168 g_warning("failed to delete cache file %s\n", filename);
172 missing_add_md5(libravatarmisses, md5);
174 g_warning("could not open '%s' for writting\n", filename);
176 curl_easy_cleanup(curl);
182 static gboolean is_recent_enough(const gchar *filename)
187 if (libravatarprefs.cache_icons) {
189 if (t != (time_t)-1 && !g_stat(filename, &s)) {
190 if (t - s.st_ctime <= libravatarprefs.cache_interval * 3600)
195 return FALSE; /* re-download */
198 static GtkWidget *image_widget_from_cached_md5(const gchar *md5)
200 GtkWidget *image = NULL;
203 filename = cache_name_for_md5(md5);
204 if (is_file_exist(filename) && is_recent_enough(filename)) {
205 debug_print("found cached image for %s\n", md5);
206 image = image_widget_from_filename(filename);
213 static gchar *libravatar_url_for_md5(const gchar *base, const gchar *md5)
215 if (libravatarprefs.default_mode >= DEF_MODE_404) {
216 return g_strdup_printf("%s/%s?s=%u&d=%s",
217 base, md5, AVATAR_SIZE,
218 def_mode[libravatarprefs.default_mode - 10]);
219 } else if (libravatarprefs.default_mode == DEF_MODE_URL) {
220 return g_strdup_printf("%s/%s?s=%u&d=%s",
221 base, md5, AVATAR_SIZE,
222 libravatarprefs.default_mode_url);
223 } else if (libravatarprefs.default_mode == DEF_MODE_NONE) {
224 return g_strdup_printf("%s/%s?s=%u",
225 base, md5, AVATAR_SIZE);
228 g_warning("invalid libravatar default mode: %d\n", libravatarprefs.default_mode);
232 static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
234 AvatarRender *ar = (AvatarRender *)source;
235 GtkWidget *image = NULL;
236 gchar *a = NULL, *url = NULL;
239 debug_print("libravatar avatar_image_render invoked\n");
241 a = procmsg_msginfo_get_avatar(ar->full_msginfo, AVATAR_LIBRAVATAR);
245 md5_hex_digest(md5sum, a);
246 /* try missing cache */
247 if (is_missing_md5(libravatarmisses, md5sum)) {
251 image = image_widget_from_cached_md5(md5sum);
253 if (ar->image) /* previous plugin set one */
254 gtk_widget_destroy(ar->image);
258 /* not cached copy: try network */
259 if (prefs_common.work_offline) {
260 debug_print("working off-line: libravatar network retrieval skipped\n");
263 base = federated_base_url_from_address(a);
264 url = libravatar_url_for_md5(base, md5sum);
266 image = image_widget_from_url(url, md5sum);
269 if (ar->image) /* previous plugin set one */
270 gtk_widget_destroy(ar->image);
279 return FALSE; /* keep rendering */
282 static gint cache_dir_init()
287 cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
288 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
290 if (!is_dir_exist(cache_dir)) {
291 if (make_dir(cache_dir) < 0) {
296 for (i = DEF_MODE_MM; i <= DEF_MODE_RETRO; ++i) {
297 subdir = g_strconcat(cache_dir, def_mode[i - 10], NULL);
298 if (!is_dir_exist(subdir)) {
299 if (make_dir(subdir) < 0) {
300 g_warning("cannot create directory %s\n", subdir);
311 static gint missing_cache_init()
313 gchar *cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
314 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
315 LIBRAVATAR_MISSING_FILE, NULL);
317 libravatarmisses = missing_load_from_file(cache_file);
320 if (libravatarmisses == NULL)
326 static void missing_cache_done()
330 if (libravatarmisses != NULL) {
331 cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
332 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
333 LIBRAVATAR_MISSING_FILE, NULL);
334 missing_save_to_file(libravatarmisses, cache_file);
336 g_hash_table_destroy(libravatarmisses);
343 * @param error For storing the returned error message.
345 * @return 0 if initialization succeeds, -1 on failure.
347 gint plugin_init(gchar **error)
349 if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,9,3,29),
350 VERSION_NUMERIC, _("Libravatar"), error))
352 /* get info from headers */
353 update_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
354 libravatar_header_update_hook,
356 if (update_hook_id == -1) {
357 *error = g_strdup(_("Failed to register avatar header update hook"));
360 /* get image for displaying */
361 render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
362 libravatar_image_render_hook,
364 if (render_hook_id == -1) {
365 *error = g_strdup(_("Failed to register avatar image render hook"));
369 if (cache_dir_init() == -1) {
370 *error = g_strdup(_("Failed to create avatar image cache directory"));
373 /* preferences page */
374 libravatar_prefs_init();
376 curl_global_init(CURL_GLOBAL_DEFAULT);
378 if (missing_cache_init() == -1) {
379 *error = g_strdup(_("Failed to load missing items cache"));
382 debug_print("Libravatar plugin loaded\n");
388 * Destructor for the plugin.
389 * Unregister the callback function and frees matcher.
391 * @return Always TRUE.
393 gboolean plugin_done(void)
395 if (render_hook_id != -1) {
396 hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
400 if (update_hook_id != -1) {
401 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
405 libravatar_prefs_done();
406 missing_cache_done();
407 if (cache_dir != NULL)
409 debug_print("Libravatar plugin unloaded\n");
415 * Get the name of the plugin.
417 * @return The plugin's name, maybe translated.
419 const gchar *plugin_name(void)
421 return _("Libravatar");
425 * Get the description of the plugin.
427 * @return The plugin's description, maybe translated.
429 const gchar *plugin_desc(void)
431 return _("Get and display libravatar images for mail messages.\n\n"
432 "Info about libravatar at http://www.libravatar.org/\n\n"
433 "Feedback to <ricardo@mones.org> is welcome.\n");
437 * Get the kind of plugin.
439 * @return The "GTK2" constant.
441 const gchar *plugin_type(void)
447 * Get the license acronym the plugin is released under.
449 * @return The "GPL3+" constant.
451 const gchar *plugin_licence(void)
457 * Get the version of the plugin.
459 * @return The current version string.
461 const gchar *plugin_version(void)
467 * Get the features implemented by the plugin.
469 * @return A constant PluginFeature structure with the features.
471 struct PluginFeature *plugin_provides(void)
473 static struct PluginFeature features[] =
474 { {PLUGIN_OTHER, N_("Libravatar")},
475 {PLUGIN_NOTHING, NULL}};