RSSyl: Allow use of .netrc by libcurl. Bug/enhancement #3309, by Vincent Pelletier
[claws.git] / src / plugins / libravatar / libravatar_federation.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21
22 #include "libravatar_federation.h"
23 #include "utils.h"
24 #include "gtkutils.h"
25
26 #define MISSING "x"
27
28 static GHashTable *federated = NULL;
29
30 /**
31  * Get the associated avatar URL for a domain.
32  *
33  * @param domain Domain to get the URL for.
34  *
35  * @return The avatar URL for the domain or NULL if not found.
36  */
37 static gchar *get_federated_url_for_domain(const gchar *domain)
38 {
39         gchar *found;
40
41         if (federated == NULL) {
42                 return NULL;
43         }
44
45         found = (gchar *) g_hash_table_lookup(federated, domain);
46
47         if (found != NULL)
48                 debug_print("cached avatar url for domain %s found: %s\n", domain, found);
49         else    
50                 debug_print("cached avatar url for domain %s not found\n", domain);
51
52         return found;
53 }
54
55 /**
56  * Adds a URL for a domain.
57  *
58  * @param url  The computed avatar URL.
59  * @param domain  Associated domain.
60  */
61 static void add_federated_url_for_domain(const gchar *url, const gchar *domain)
62 {
63         if (url == NULL)
64                 return;
65
66         if (federated == NULL)
67                 federated = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
68
69         debug_print("new cached avatar url for domain %s: %s\n", domain, url);
70         g_hash_table_insert(federated, g_strdup(domain), g_strdup(url)); 
71 }
72
73 /**
74  * Retrieves the federated URL for a given email address.
75  *
76  * @param address  The email address.
77  *
78  * @return The avatar URL for the domain of the address. 
79  */
80 gchar *federated_url_for_address(const gchar *address)
81 {
82         gchar *domain = NULL, *last = NULL, *addr = NULL, *url = NULL;
83         gchar *host = NULL;
84         guint16 port = 0;
85
86         if (address == NULL || *address == '\0')
87                 goto invalid_addr;
88
89         addr = g_strdup(address);
90         domain = strchr(addr, '@');
91         if (domain == NULL)
92                 goto invalid_addr;
93  
94         ++domain;
95         if (strlen(domain) < 5)
96                 goto invalid_addr;
97
98         last = domain;
99         while (*last != '\0' && *last != ' ' && *last != '\t' && *last != '>')
100                 ++last;
101         *last = '\0';
102
103         /* try cached domains */
104         url = get_federated_url_for_domain(domain);
105         if (url != NULL) {
106                 g_free(addr);
107                 if (!strcmp(url, MISSING)) {
108                         return NULL;
109                 }
110                 return g_strdup(url);
111         }
112
113         /* not cached, try secure service first */
114         if (auto_configure_service_sync("avatars-sec", domain, &host, &port)) {
115                 if (port != 443) {
116                         url = g_strdup_printf("https://%s:%d/avatar", host, port);
117                 } else {
118                         url = g_strdup_printf("https://%s/avatar", host);
119                 }
120         } else { /* try standard one if no secure service available */
121                 if (auto_configure_service_sync("avatars", domain, &host, &port)) {
122                         if (port != 80) {
123                                 url = g_strdup_printf("http://%s:%d/avatar", host, port);
124                         } else {
125                                 url = g_strdup_printf("http://%s/avatar", host);
126                         }
127                 } else {
128                         debug_print("libravatar federated domain for %s not found\n", domain);
129                 }
130         }
131         if (url != NULL) {
132                 add_federated_url_for_domain(url, domain);
133         } else {
134                 add_federated_url_for_domain(MISSING, domain);
135         }
136
137         g_free(addr);
138         return url;
139
140 invalid_addr:
141         if (addr != NULL)
142                 g_free(addr);
143
144         debug_print("invalid address for libravatar federated domain\n");
145         return NULL;
146 }
147