add a "GList *authtypes", so you can get the list of authtypes used by a
[platform/upstream/evolution-data-server.git] / camel / camel-service.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-service.c : Abstract class for an email service */
3
4 /*
5  *
6  * Author :
7  *  Bertrand Guiheneuf <bertrand@helixcode.com>
8  *
9  * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24  * USA
25  */
26 #include <config.h>
27 #include "camel-service.h"
28 #include "camel-session.h"
29 #include "camel-exception.h"
30
31 #include <ctype.h>
32 #include <stdlib.h>
33
34 #include "camel-private.h"
35
36 static CamelObjectClass *parent_class = NULL;
37
38 /* Returns the class for a CamelService */
39 #define CSERV_CLASS(so) CAMEL_SERVICE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
40
41 static void construct (CamelService *service, CamelSession *session,
42                        CamelProvider *provider, CamelURL *url,
43                        CamelException *ex);
44 static gboolean service_connect(CamelService *service, CamelException *ex);
45 static gboolean service_disconnect(CamelService *service, gboolean clean,
46                                    CamelException *ex);
47 /*static gboolean is_connected (CamelService *service);*/
48 static GList *  query_auth_types (CamelService *service, CamelException *ex);
49 static char *   get_name (CamelService *service, gboolean brief);
50 static char *   get_path (CamelService *service);
51
52
53 static void
54 camel_service_class_init (CamelServiceClass *camel_service_class)
55 {
56         parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
57
58         /* virtual method definition */
59         camel_service_class->construct = construct;
60         camel_service_class->connect = service_connect;
61         camel_service_class->disconnect = service_disconnect;
62         camel_service_class->query_auth_types = query_auth_types;
63         camel_service_class->get_name = get_name;
64         camel_service_class->get_path = get_path;
65 }
66
67 static void
68 camel_service_init (void *o, void *k)
69 {
70         CamelService *service = o;
71
72         service->priv = g_malloc0(sizeof(*service->priv));
73 #ifdef ENABLE_THREADS
74         service->priv->connect_lock = e_mutex_new(E_MUTEX_REC);
75 #endif
76 }
77
78 static void
79 camel_service_finalize (CamelObject *object)
80 {
81         CamelService *camel_service = CAMEL_SERVICE (object);
82
83         if (camel_service->connected) {
84                 CamelException ex;
85
86                 /*g_warning ("camel_service_finalize: finalizing while still connected!");*/
87                 camel_exception_init (&ex);
88                 CSERV_CLASS (camel_service)->disconnect (camel_service, FALSE, &ex);
89                 if (camel_exception_is_set (&ex)) {
90                         g_warning ("camel_service_finalize: silent disconnect failure: %s",
91                                    camel_exception_get_description(&ex));
92                 }
93                 camel_exception_clear (&ex);
94         }
95
96         if (camel_service->url)
97                 camel_url_free (camel_service->url);
98         if (camel_service->session)
99                 camel_object_unref (CAMEL_OBJECT (camel_service->session));
100
101 #ifdef ENABLE_THREADS
102         e_mutex_destroy(camel_service->priv->connect_lock);
103 #endif
104         g_free(camel_service->priv);
105 }
106
107
108
109 CamelType
110 camel_service_get_type (void)
111 {
112         static CamelType camel_service_type = CAMEL_INVALID_TYPE;
113
114         if (camel_service_type == CAMEL_INVALID_TYPE) {
115                 camel_service_type =
116                         camel_type_register (CAMEL_OBJECT_TYPE, "CamelService",
117                                              sizeof (CamelService),
118                                              sizeof (CamelServiceClass),
119                                              (CamelObjectClassInitFunc) camel_service_class_init,
120                                              NULL,
121                                              (CamelObjectInitFunc) camel_service_init,
122                                              camel_service_finalize );
123         }
124         
125         return camel_service_type;
126 }
127
128
129 static void
130 construct (CamelService *service, CamelSession *session,
131            CamelProvider *provider, CamelURL *url, CamelException *ex)
132 {
133         char *url_string;
134
135         if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_USER) &&
136             (url->user == NULL || url->user[0] == '\0')) {
137                 url_string = camel_url_to_string (url, FALSE);
138                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
139                                       _("URL '%s' needs a username component"),
140                                       url_string);
141                 g_free (url_string);
142                 return;
143         } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_HOST) &&
144                    (url->host == NULL || url->host[0] == '\0')) {
145                 url_string = camel_url_to_string (url, FALSE);
146                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
147                                       _("URL '%s' needs a host component"),
148                                       url_string);
149                 g_free (url_string);
150                 return;
151         } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_PATH) &&
152                    (url->path == NULL || url->path[0] == '\0')) {
153                 url_string = camel_url_to_string (url, FALSE);
154                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
155                                       _("URL '%s' needs a path component"),
156                                       url_string);
157                 g_free (url_string);
158                 return;
159         }
160
161         service->provider = provider;
162         service->url = url;
163         service->session = session;
164         camel_object_ref (CAMEL_OBJECT (session));
165
166         service->connected = FALSE;
167 }
168
169 /**
170  * camel_service_construct:
171  * @service: the CamelService
172  * @session: the session for the service
173  * @provider: the service's provider
174  * @url: the default URL for the service (may be NULL)
175  * @ex: a CamelException
176  *
177  * Constructs a CamelService initialized with the given parameters.
178  **/
179 void
180 camel_service_construct (CamelService *service, CamelSession *session,
181                          CamelProvider *provider, CamelURL *url,
182                          CamelException *ex)
183 {
184         g_return_if_fail (CAMEL_IS_SERVICE (service));
185         g_return_if_fail (CAMEL_IS_SESSION (session));
186
187         CSERV_CLASS (service)->construct (service, session, provider, url, ex);
188 }
189
190
191 static gboolean
192 service_connect (CamelService *service, CamelException *ex)
193 {
194         /* Things like the CamelMboxStore can validly
195          * not define a connect function.
196          */
197          return TRUE;
198 }
199
200 /**
201  * camel_service_connect:
202  * @service: CamelService object
203  * @ex: a CamelException
204  *
205  * Connect to the service using the parameters it was initialized
206  * with.
207  *
208  * Return value: whether or not the connection succeeded
209  **/
210
211 gboolean
212 camel_service_connect (CamelService *service, CamelException *ex)
213 {
214         gboolean ret = FALSE;
215
216         g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
217         g_return_val_if_fail (service->session != NULL, FALSE);
218         g_return_val_if_fail (service->url != NULL, FALSE);
219
220         CAMEL_SERVICE_LOCK(service, connect_lock);
221
222         if (service->connected) {
223                 /* But we're still connected, so no exception
224                  * and return true.
225                  */
226                 g_warning ("camel_service_connect: trying to connect to an already connected service");
227                 ret = TRUE;
228         } else if (CSERV_CLASS (service)->connect (service, ex)) {
229                 service->connected = TRUE;
230                 ret = TRUE;
231         }
232
233         CAMEL_SERVICE_UNLOCK(service, connect_lock);
234
235         return ret;
236 }
237
238 static gboolean
239 service_disconnect (CamelService *service, gboolean clean, CamelException *ex)
240 {
241         /*service->connect_level--;*/
242
243         /* We let people get away with not having a disconnect
244          * function -- CamelMboxStore, for example. 
245          */
246
247         return TRUE;
248 }
249
250 /**
251  * camel_service_disconnect:
252  * @service: CamelService object
253  * @clean: whether or not to try to disconnect cleanly.
254  * @ex: a CamelException
255  *
256  * Disconnect from the service. If @clean is %FALSE, it should not
257  * try to do any synchronizing or other cleanup of the connection.
258  *
259  * Return value: whether or not the disconnection succeeded without
260  * errors. (Consult @ex if %FALSE.)
261  **/
262 gboolean
263 camel_service_disconnect (CamelService *service, gboolean clean,
264                           CamelException *ex)
265 {
266         gboolean res = TRUE;
267
268         CAMEL_SERVICE_LOCK(service, connect_lock);
269
270         if (service->connected) {
271                 res = CSERV_CLASS (service)->disconnect (service, clean, ex);
272                 service->connected = FALSE;
273         }
274
275         CAMEL_SERVICE_UNLOCK(service, connect_lock);
276
277         return res;
278 }
279
280 /**
281  * camel_service_get_url:
282  * @service: a service
283  *
284  * Returns the URL representing a service. The returned URL must be
285  * freed when it is no longer needed. For security reasons, this
286  * routine does not return the password.
287  *
288  * Return value: the url name
289  **/
290 char *
291 camel_service_get_url (CamelService *service)
292 {
293         return camel_url_to_string(service->url, FALSE);
294 }
295
296
297 static char *
298 get_name (CamelService *service, gboolean brief)
299 {
300         g_warning ("CamelService::get_name not implemented for `%s'",
301                    camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
302         return g_strdup ("???");
303 }               
304
305 /**
306  * camel_service_get_name:
307  * @service: the service
308  * @brief: whether or not to use a briefer form
309  *
310  * This gets the name of the service in a "friendly" (suitable for
311  * humans) form. If @brief is %TRUE, this should be a brief description
312  * such as for use in the folder tree. If @brief is %FALSE, it should
313  * be a more complete and mostly unambiguous description.
314  *
315  * Return value: the description, which the caller must free.
316  **/
317 char *
318 camel_service_get_name (CamelService *service, gboolean brief)
319 {
320         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
321         g_return_val_if_fail (service->url, NULL);
322
323         return CSERV_CLASS (service)->get_name (service, brief);
324 }
325
326
327 static char *
328 get_path (CamelService *service)
329 {
330         GString *gpath;
331         char *path;
332         CamelURL *url = service->url;
333         CamelProvider *prov = service->provider;
334
335         /* A sort of ad-hoc default implementation that works for our
336          * current set of services.
337          */
338
339         gpath = g_string_new (service->provider->protocol);
340         if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
341                 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
342                         g_string_sprintfa (gpath, "/%s@%s",
343                                            url->user ? url->user : "",
344                                            url->host ? url->host : "");
345                 } else {
346                         g_string_sprintfa (gpath, "/%s%s",
347                                            url->user ? url->user : "",
348                                            CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
349                 }
350         } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
351                 g_string_sprintfa (gpath, "/%s%s",
352                                    CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
353                                    url->host ? url->host : "");
354         }
355         if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH)) {
356                 g_string_sprintfa (gpath, "%s%s",
357                                    *url->path == '/' ? "" : "/",
358                                    url->path);
359         }
360
361         path = gpath->str;
362         g_string_free (gpath, FALSE);
363         return path;
364 }               
365
366 /**
367  * camel_service_get_path:
368  * @service: the service
369  *
370  * This gets a valid UNIX relative path describing the service, which
371  * is guaranteed to be different from the path returned for any
372  * different service. This path MUST start with the name of the
373  * provider, followed by a "/", but after that, it is up to the
374  * provider.
375  *
376  * Return value: the path, which the caller must free.
377  **/
378 char *
379 camel_service_get_path (CamelService *service)
380 {
381         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
382         g_return_val_if_fail (service->url, NULL);
383
384         return CSERV_CLASS (service)->get_path (service);
385 }
386
387
388 /**
389  * camel_service_get_session:
390  * @service: a service
391  *
392  * Returns the CamelSession associated with the service.
393  *
394  * Return value: the session
395  **/
396 CamelSession *
397 camel_service_get_session (CamelService *service)
398 {
399         return service->session;
400 }
401
402 /**
403  * camel_service_get_provider:
404  * @service: a service
405  *
406  * Returns the CamelProvider associated with the service.
407  *
408  * Return value: the provider
409  **/
410 CamelProvider *
411 camel_service_get_provider (CamelService *service)
412 {
413         return service->provider;
414 }
415
416 static GList *
417 query_auth_types (CamelService *service, CamelException *ex)
418 {
419         return NULL;
420 }
421
422 /**
423  * camel_service_query_auth_types:
424  * @service: a CamelService
425  * @ex: a CamelException
426  *
427  * This is used by the mail source wizard to get the list of
428  * authentication types supported by the protocol, and information
429  * about them.
430  *
431  * Return value: a list of CamelServiceAuthType records. The caller
432  * must free the list with g_list_free() when it is done with it.
433  **/
434 GList *
435 camel_service_query_auth_types (CamelService *service, CamelException *ex)
436 {
437         GList *ret;
438
439         /* note that we get the connect lock here, which means the callee
440            must not call the connect functions itself */
441         CAMEL_SERVICE_LOCK(service, connect_lock);
442         ret = CSERV_CLASS (service)->query_auth_types (service, ex);
443         CAMEL_SERVICE_UNLOCK(service, connect_lock);
444
445         return ret;
446 }
447
448 /* URL utility routines */
449
450 /**
451  * camel_service_gethost:
452  * @service: a CamelService
453  * @ex: a CamelException
454  *
455  * This is a convenience function to do a gethostbyname on the host
456  * for the service's URL.
457  *
458  * Return value: a (statically-allocated) hostent.
459  **/
460 struct hostent *
461 camel_service_gethost (CamelService *service, CamelException *ex)
462 {
463         struct hostent *h;
464         char *hostname;
465
466 #warning "This needs to use gethostbyname_r()"
467
468         if (service->url->host)
469                 hostname = service->url->host;
470         else
471                 hostname = "localhost";
472         h = gethostbyname (hostname);
473         if (!h) {
474                 extern int h_errno;
475
476                 if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA) {
477                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
478                                               _("No such host %s."), hostname);
479                 } else {
480                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
481                                               _("Temporarily unable to look "
482                                                 "up hostname %s."), hostname);
483                 }
484                 return NULL;
485         }
486
487         return h;
488 }