1 /*
2  * module-yahoo-backend.c
3  *
4  * This library is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-data-server-config.h"
19 
20 #include <glib/gi18n-lib.h>
21 
22 #include <libebackend/libebackend.h>
23 
24 /* Standard GObject macros */
25 #define E_TYPE_YAHOO_BACKEND \
26 	(e_yahoo_backend_get_type ())
27 #define E_YAHOO_BACKEND(obj) \
28 	(G_TYPE_CHECK_INSTANCE_CAST \
29 	((obj), E_TYPE_YAHOO_BACKEND, EYahooBackend))
30 
31 /* Just for readability... */
32 #define METHOD(x) (CAMEL_NETWORK_SECURITY_METHOD_##x)
33 
34 /* IMAP Configuration Details */
35 #define YAHOO_IMAP_BACKEND_NAME		"imapx"
36 #define YAHOO_IMAP_HOST			"imap.mail.yahoo.com"
37 #define YAHOO_IMAP_PORT			993
38 #define YAHOO_IMAP_SECURITY_METHOD	METHOD (SSL_ON_ALTERNATE_PORT)
39 
40 /* SMTP Configuration Details */
41 #define YAHOO_SMTP_BACKEND_NAME		"smtp"
42 #define YAHOO_SMTP_HOST			"smtp.mail.yahoo.com"
43 #define YAHOO_SMTP_PORT			465
44 #define YAHOO_SMTP_SECURITY_METHOD	METHOD (SSL_ON_ALTERNATE_PORT)
45 
46 /* WebDAV Configuration Details */
47 #define YAHOO_CALDAV_URL		"https://caldav.calendar.yahoo.com/dav/"
48 #define YAHOO_CARDDAV_URL		"https://carddav.address.yahoo.com/dav/"
49 
50 typedef struct _EYahooBackend EYahooBackend;
51 typedef struct _EYahooBackendClass EYahooBackendClass;
52 
53 typedef struct _EYahooBackendFactory EYahooBackendFactory;
54 typedef struct _EYahooBackendFactoryClass EYahooBackendFactoryClass;
55 
56 struct _EYahooBackend {
57 	EWebDAVCollectionBackend parent;
58 	GWeakRef mail_identity_source;
59 };
60 
61 struct _EYahooBackendClass {
62 	EWebDAVCollectionBackendClass parent_class;
63 };
64 
65 struct _EYahooBackendFactory {
66 	ECollectionBackendFactory parent;
67 };
68 
69 struct _EYahooBackendFactoryClass {
70 	ECollectionBackendFactoryClass parent_class;
71 };
72 
73 /* Module Entry Points */
74 void e_module_load (GTypeModule *type_module);
75 void e_module_unload (GTypeModule *type_module);
76 
77 /* Forward Declarations */
78 GType e_yahoo_backend_get_type (void);
79 GType e_yahoo_backend_factory_get_type (void);
80 
G_DEFINE_DYNAMIC_TYPE(EYahooBackend,e_yahoo_backend,E_TYPE_WEBDAV_COLLECTION_BACKEND)81 G_DEFINE_DYNAMIC_TYPE (
82 	EYahooBackend,
83 	e_yahoo_backend,
84 	E_TYPE_WEBDAV_COLLECTION_BACKEND)
85 
86 G_DEFINE_DYNAMIC_TYPE (
87 	EYahooBackendFactory,
88 	e_yahoo_backend_factory,
89 	E_TYPE_COLLECTION_BACKEND_FACTORY)
90 
91 static ESourceAuthenticationResult
92 yahoo_backend_authenticate_sync (EBackend *backend,
93 				 const ENamedParameters *credentials,
94 				 gchar **out_certificate_pem,
95 				 GTlsCertificateFlags *out_certificate_errors,
96 				 GCancellable *cancellable,
97 				 GError **error)
98 {
99 	g_return_val_if_fail (E_IS_COLLECTION_BACKEND (backend), E_SOURCE_AUTHENTICATION_ERROR);
100 
101 	return e_webdav_collection_backend_discover_sync (E_WEBDAV_COLLECTION_BACKEND (backend),
102 		YAHOO_CALDAV_URL, YAHOO_CARDDAV_URL, credentials,
103 		out_certificate_pem, out_certificate_errors, cancellable, error);
104 }
105 
106 static void
yahoo_backend_child_added(ECollectionBackend * backend,ESource * child_source)107 yahoo_backend_child_added (ECollectionBackend *backend,
108                            ESource *child_source)
109 {
110 	EYahooBackend *yahoo_backend;
111 	ESource *collection_source;
112 	const gchar *extension_name;
113 	gboolean is_mail = FALSE;
114 
115 	/* Chain up to parent's child_added() method. */
116 	E_COLLECTION_BACKEND_CLASS (e_yahoo_backend_parent_class)->
117 		child_added (backend, child_source);
118 
119 	yahoo_backend = E_YAHOO_BACKEND (backend);
120 	collection_source = e_backend_get_source (E_BACKEND (backend));
121 
122 	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
123 	is_mail |= e_source_has_extension (child_source, extension_name);
124 
125 	/* Take special note of the mail identity source.
126 	 * We need it to build the calendar CalDAV path. */
127 	extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
128 	if (e_source_has_extension (child_source, extension_name)) {
129 		GWeakRef *weak_ref;
130 
131 		weak_ref = &yahoo_backend->mail_identity_source;
132 		g_weak_ref_set (weak_ref, child_source);
133 		is_mail = TRUE;
134 	}
135 
136 	extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
137 	is_mail |= e_source_has_extension (child_source, extension_name);
138 
139 	/* Synchronize mail-related user with the collection identity. */
140 	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
141 	if (is_mail && e_source_has_extension (child_source, extension_name)) {
142 		ESourceAuthentication *auth_child_extension;
143 		ESourceCollection *collection_extension;
144 		const gchar *collection_identity;
145 		const gchar *auth_child_user;
146 
147 		extension_name = E_SOURCE_EXTENSION_COLLECTION;
148 		collection_extension = e_source_get_extension (
149 			collection_source, extension_name);
150 		collection_identity = e_source_collection_get_identity (
151 			collection_extension);
152 
153 		extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
154 		auth_child_extension = e_source_get_extension (
155 			child_source, extension_name);
156 		auth_child_user = e_source_authentication_get_user (
157 			auth_child_extension);
158 
159 		/* XXX Do not override an existing user name setting.
160 		 *     The IMAP or (especially) SMTP configuration may
161 		 *     have been modified to use a non-Yahoo! server. */
162 		if (auth_child_user == NULL)
163 			e_source_authentication_set_user (
164 				auth_child_extension,
165 				collection_identity);
166 	}
167 }
168 
169 static void
yahoo_backend_finalize(GObject * object)170 yahoo_backend_finalize (GObject *object)
171 {
172 	EYahooBackend *backend = E_YAHOO_BACKEND (object);
173 
174 	g_weak_ref_clear (&backend->mail_identity_source);
175 
176 	G_OBJECT_CLASS (e_yahoo_backend_parent_class)->finalize (object);
177 }
178 
179 static void
e_yahoo_backend_class_init(EYahooBackendClass * class)180 e_yahoo_backend_class_init (EYahooBackendClass *class)
181 {
182 	GObjectClass *object_class;
183 	EBackendClass *backend_class;
184 	ECollectionBackendClass *collection_backend_class;
185 
186 	object_class = G_OBJECT_CLASS (class);
187 	object_class->finalize = yahoo_backend_finalize;
188 
189 	backend_class = E_BACKEND_CLASS (class);
190 	backend_class->authenticate_sync = yahoo_backend_authenticate_sync;
191 
192 	collection_backend_class = E_COLLECTION_BACKEND_CLASS (class);
193 	collection_backend_class->child_added = yahoo_backend_child_added;
194 }
195 
196 static void
e_yahoo_backend_class_finalize(EYahooBackendClass * class)197 e_yahoo_backend_class_finalize (EYahooBackendClass *class)
198 {
199 }
200 
201 static void
e_yahoo_backend_init(EYahooBackend * backend)202 e_yahoo_backend_init (EYahooBackend *backend)
203 {
204 	g_weak_ref_init (&backend->mail_identity_source, NULL);
205 }
206 
207 static void
yahoo_backend_prepare_mail_account_source(ESource * source)208 yahoo_backend_prepare_mail_account_source (ESource *source)
209 {
210 	ESourceCamel *camel_extension;
211 	ESourceExtension *extension;
212 	CamelSettings *settings;
213 	const gchar *backend_name;
214 	const gchar *extension_name;
215 
216 	backend_name = YAHOO_IMAP_BACKEND_NAME;
217 
218 	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
219 	extension = e_source_get_extension (source, extension_name);
220 
221 	e_source_backend_set_backend_name (
222 		E_SOURCE_BACKEND (extension), backend_name);
223 
224 	extension_name = e_source_camel_get_extension_name (backend_name);
225 	camel_extension = e_source_get_extension (source, extension_name);
226 	settings = e_source_camel_get_settings (camel_extension);
227 
228 	/* The "auth-mechanism" should be determined elsewhere. */
229 
230 	camel_network_settings_set_host (
231 		CAMEL_NETWORK_SETTINGS (settings),
232 		YAHOO_IMAP_HOST);
233 
234 	camel_network_settings_set_port (
235 		CAMEL_NETWORK_SETTINGS (settings),
236 		YAHOO_IMAP_PORT);
237 
238 	camel_network_settings_set_security_method (
239 		CAMEL_NETWORK_SETTINGS (settings),
240 		YAHOO_IMAP_SECURITY_METHOD);
241 }
242 
243 static void
yahoo_backend_prepare_mail_transport_source(ESource * source)244 yahoo_backend_prepare_mail_transport_source (ESource *source)
245 {
246 	ESourceCamel *camel_extension;
247 	ESourceExtension *extension;
248 	CamelSettings *settings;
249 	const gchar *backend_name;
250 	const gchar *extension_name;
251 
252 	/* Configure the mail transport source. */
253 
254 	backend_name = YAHOO_SMTP_BACKEND_NAME;
255 
256 	extension_name = E_SOURCE_EXTENSION_MAIL_TRANSPORT;
257 	extension = e_source_get_extension (source, extension_name);
258 
259 	e_source_backend_set_backend_name (
260 		E_SOURCE_BACKEND (extension), backend_name);
261 
262 	extension_name = e_source_camel_get_extension_name (backend_name);
263 	camel_extension = e_source_get_extension (source, extension_name);
264 	settings = e_source_camel_get_settings (camel_extension);
265 
266 	/* The "auth-mechanism" should be determined elsewhere. */
267 
268 	camel_network_settings_set_host (
269 		CAMEL_NETWORK_SETTINGS (settings),
270 		YAHOO_SMTP_HOST);
271 
272 	camel_network_settings_set_port (
273 		CAMEL_NETWORK_SETTINGS (settings),
274 		YAHOO_SMTP_PORT);
275 
276 	camel_network_settings_set_security_method (
277 		CAMEL_NETWORK_SETTINGS (settings),
278 		YAHOO_SMTP_SECURITY_METHOD);
279 }
280 
281 static void
yahoo_backend_factory_prepare_mail(ECollectionBackendFactory * factory,ESource * mail_account_source,ESource * mail_identity_source,ESource * mail_transport_source)282 yahoo_backend_factory_prepare_mail (ECollectionBackendFactory *factory,
283                                     ESource *mail_account_source,
284                                     ESource *mail_identity_source,
285                                     ESource *mail_transport_source)
286 {
287 	ECollectionBackendFactoryClass *parent_class;
288 
289 	/* Chain up to parent's prepare_mail() method. */
290 	parent_class =
291 		E_COLLECTION_BACKEND_FACTORY_CLASS (
292 		e_yahoo_backend_factory_parent_class);
293 	parent_class->prepare_mail (
294 		factory,
295 		mail_account_source,
296 		mail_identity_source,
297 		mail_transport_source);
298 
299 	yahoo_backend_prepare_mail_account_source (mail_account_source);
300 	yahoo_backend_prepare_mail_transport_source (mail_transport_source);
301 }
302 
303 static void
e_yahoo_backend_factory_class_init(EYahooBackendFactoryClass * class)304 e_yahoo_backend_factory_class_init (EYahooBackendFactoryClass *class)
305 {
306 	ECollectionBackendFactoryClass *factory_class;
307 
308 	factory_class = E_COLLECTION_BACKEND_FACTORY_CLASS (class);
309 	factory_class->factory_name = "yahoo";
310 	factory_class->backend_type = E_TYPE_YAHOO_BACKEND;
311 	factory_class->prepare_mail = yahoo_backend_factory_prepare_mail;
312 }
313 
314 static void
e_yahoo_backend_factory_class_finalize(EYahooBackendFactoryClass * class)315 e_yahoo_backend_factory_class_finalize (EYahooBackendFactoryClass *class)
316 {
317 }
318 
319 static void
e_yahoo_backend_factory_init(EYahooBackendFactory * factory)320 e_yahoo_backend_factory_init (EYahooBackendFactory *factory)
321 {
322 }
323 
324 G_MODULE_EXPORT void
e_module_load(GTypeModule * type_module)325 e_module_load (GTypeModule *type_module)
326 {
327 	e_yahoo_backend_register_type (type_module);
328 	e_yahoo_backend_factory_register_type (type_module);
329 }
330 
331 G_MODULE_EXPORT void
e_module_unload(GTypeModule * type_module)332 e_module_unload (GTypeModule *type_module)
333 {
334 }
335 
336