Fix FSF address (Tobias Mueller, #470445)
[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-2003 Ximian, Inc. (www.ximian.com)
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of version 2 of the GNU Lesser General Public
13  * License as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23  * USA
24  */
25
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <ctype.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <glib.h>
38 #include <glib/gi18n-lib.h>
39
40 #include <libedataserver/e-msgport.h>
41
42 #include "camel-exception.h"
43 #include "camel-operation.h"
44 #include "camel-private.h"
45 #include "camel-service.h"
46 #include "camel-session.h"
47
48 #define d(x)
49 #define w(x)
50
51 static CamelObjectClass *parent_class = NULL;
52
53 /* Returns the class for a CamelService */
54 #define CSERV_CLASS(so) CAMEL_SERVICE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
55
56 static void construct (CamelService *service, CamelSession *session,
57                        CamelProvider *provider, CamelURL *url,
58                        CamelException *ex);
59 static gboolean service_connect(CamelService *service, CamelException *ex);
60 static gboolean service_disconnect(CamelService *service, gboolean clean,
61                                    CamelException *ex);
62 static void cancel_connect (CamelService *service);
63 static GList *query_auth_types (CamelService *service, CamelException *ex);
64 static char *get_name (CamelService *service, gboolean brief);
65 static char *get_path (CamelService *service);
66
67 static int service_setv (CamelObject *object, CamelException *ex, CamelArgV *args);
68 static int service_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args);
69
70
71 static void
72 camel_service_class_init (CamelServiceClass *camel_service_class)
73 {
74         CamelObjectClass *object_class = CAMEL_OBJECT_CLASS (camel_service_class);
75         
76         parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
77         
78         /* virtual method overloading */
79         object_class->setv = service_setv;
80         object_class->getv = service_getv;
81         
82         /* virtual method definition */
83         camel_service_class->construct = construct;
84         camel_service_class->connect = service_connect;
85         camel_service_class->disconnect = service_disconnect;
86         camel_service_class->cancel_connect = cancel_connect;
87         camel_service_class->query_auth_types = query_auth_types;
88         camel_service_class->get_name = get_name;
89         camel_service_class->get_path = get_path;
90 }
91
92 static void
93 camel_service_init (void *o, void *k)
94 {
95         CamelService *service = o;
96         
97         service->priv = g_malloc0(sizeof(*service->priv));
98         g_static_rec_mutex_init(&service->priv->connect_lock);
99         g_static_mutex_init(&service->priv->connect_op_lock);
100 }
101
102 static void
103 camel_service_finalize (CamelObject *object)
104 {
105         CamelService *service = CAMEL_SERVICE (object);
106
107         if (service->status == CAMEL_SERVICE_CONNECTED) {
108                 CamelException ex;
109                 
110                 camel_exception_init (&ex);
111                 CSERV_CLASS (service)->disconnect (service, TRUE, &ex);
112                 if (camel_exception_is_set (&ex)) {
113                         w(g_warning ("camel_service_finalize: silent disconnect failure: %s",
114                                      camel_exception_get_description (&ex)));
115                 }
116                 camel_exception_clear (&ex);
117         }
118         
119         if (service->url)
120                 camel_url_free (service->url);
121         if (service->session)
122                 camel_object_unref (service->session);
123         
124         g_static_rec_mutex_free (&service->priv->connect_lock);
125         g_static_mutex_free (&service->priv->connect_op_lock);
126         
127         g_free (service->priv);
128 }
129
130
131
132 CamelType
133 camel_service_get_type (void)
134 {
135         static CamelType type = CAMEL_INVALID_TYPE;
136         
137         if (type == CAMEL_INVALID_TYPE) {
138                 type =
139                         camel_type_register (CAMEL_OBJECT_TYPE,
140                                              "CamelService",
141                                              sizeof (CamelService),
142                                              sizeof (CamelServiceClass),
143                                              (CamelObjectClassInitFunc) camel_service_class_init,
144                                              NULL,
145                                              (CamelObjectInitFunc) camel_service_init,
146                                              camel_service_finalize );
147         }
148         
149         return type;
150 }
151
152
153 static int
154 service_setv (CamelObject *object, CamelException *ex, CamelArgV *args)
155 {
156         CamelService *service = (CamelService *) object;
157         CamelURL *url = service->url;
158         gboolean reconnect = FALSE;
159         guint32 tag;
160         int i;
161         
162         for (i = 0; i < args->argc; i++) {
163                 tag = args->argv[i].tag;
164                 
165                 /* make sure this is an arg we're supposed to handle */
166                 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
167                     (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
168                         continue;
169                 
170                 if (tag == CAMEL_SERVICE_USERNAME) {
171                         /* set the username */
172                         if (strcmp (url->user, args->argv[i].ca_str) != 0) {
173                                 camel_url_set_user (url, args->argv[i].ca_str);
174                                 reconnect = TRUE;
175                         }
176                 } else if (tag == CAMEL_SERVICE_AUTH) {
177                         /* set the auth mechanism */
178                         if (strcmp (url->authmech, args->argv[i].ca_str) != 0) {
179                                 camel_url_set_authmech (url, args->argv[i].ca_str);
180                                 reconnect = TRUE;
181                         }
182                 } else if (tag == CAMEL_SERVICE_HOSTNAME) {
183                         /* set the hostname */
184                         if (strcmp (url->host, args->argv[i].ca_str) != 0) {
185                                 camel_url_set_host (url, args->argv[i].ca_str);
186                                 reconnect = TRUE;
187                         }
188                 } else if (tag == CAMEL_SERVICE_PORT) {
189                         /* set the port */
190                         if (url->port != args->argv[i].ca_int) {
191                                 camel_url_set_port (url, args->argv[i].ca_int);
192                                 reconnect = TRUE;
193                         }
194                 } else if (tag == CAMEL_SERVICE_PATH) {
195                         /* set the path */
196                         if (strcmp (url->path, args->argv[i].ca_str) != 0) {
197                                 camel_url_set_path (url, args->argv[i].ca_str);
198                                 reconnect = TRUE;
199                         }
200                 } else {
201                         /* error? */
202                         continue;
203                 }
204                 
205                 /* let our parent know that we've handled this arg */
206                 camel_argv_ignore (args, i);
207         }
208         
209         /* FIXME: what if we are in the process of connecting? */
210         if (reconnect && service->status == CAMEL_SERVICE_CONNECTED) {
211                 /* reconnect the service using the new URL */
212                 if (camel_service_disconnect (service, TRUE, ex))
213                         camel_service_connect (service, ex);
214         }
215         
216         return CAMEL_OBJECT_CLASS (parent_class)->setv (object, ex, args);
217 }
218
219 static int
220 service_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args)
221 {
222         CamelService *service = (CamelService *) object;
223         CamelURL *url = service->url;
224         guint32 tag;
225         int i;
226         
227         for (i = 0; i < args->argc; i++) {
228                 tag = args->argv[i].tag;
229                 
230                 /* make sure this is an arg we're supposed to handle */
231                 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
232                     (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
233                         continue;
234                 
235                 switch (tag) {
236                 case CAMEL_SERVICE_USERNAME:
237                         /* get the username */
238                         *args->argv[i].ca_str = url->user;
239                         break;
240                 case CAMEL_SERVICE_AUTH:
241                         /* get the auth mechanism */
242                         *args->argv[i].ca_str = url->authmech;
243                         break;
244                 case CAMEL_SERVICE_HOSTNAME:
245                         /* get the hostname */
246                         *args->argv[i].ca_str = url->host;
247                         break;
248                 case CAMEL_SERVICE_PORT:
249                         /* get the port */
250                         *args->argv[i].ca_int = url->port;
251                         break;
252                 case CAMEL_SERVICE_PATH:
253                         /* get the path */
254                         *args->argv[i].ca_str = url->path;
255                         break;
256                 default:
257                         /* error? */
258                         break;
259                 }
260         }
261         
262         return CAMEL_OBJECT_CLASS (parent_class)->getv (object, ex, args);
263 }
264
265 static void
266 construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
267 {
268         char *err, *url_string;
269         
270         if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_USER) &&
271             (url->user == NULL || url->user[0] == '\0')) {
272                 err = _("URL '%s' needs a username component");
273                 goto fail;
274         } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_HOST) &&
275                    (url->host == NULL || url->host[0] == '\0')) {
276                 err = _("URL '%s' needs a host component");
277                 goto fail;
278         } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_PATH) &&
279                    (url->path == NULL || url->path[0] == '\0')) {
280                 err = _("URL '%s' needs a path component");
281                 goto fail;
282         }
283         
284         service->provider = provider;
285         service->url = camel_url_copy(url);
286         service->session = session;
287         camel_object_ref (session);
288         
289         service->status = CAMEL_SERVICE_DISCONNECTED;
290
291         return;
292
293 fail:
294         url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
295         camel_exception_setv(ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID, err, url_string);
296         g_free(url_string);
297 }
298
299 /**
300  * camel_service_construct:
301  * @service: a #CamelService object
302  * @session: the #CamelSession for @service
303  * @provider: the #CamelProvider associated with @service
304  * @url: the default URL for the service (may be %NULL)
305  * @ex: a #CamelException
306  *
307  * Constructs a #CamelService initialized with the given parameters.
308  **/
309 void
310 camel_service_construct (CamelService *service, CamelSession *session,
311                          CamelProvider *provider, CamelURL *url,
312                          CamelException *ex)
313 {
314         g_return_if_fail (CAMEL_IS_SERVICE (service));
315         g_return_if_fail (CAMEL_IS_SESSION (session));
316         
317         CSERV_CLASS (service)->construct (service, session, provider, url, ex);
318 }
319
320
321 static gboolean
322 service_connect (CamelService *service, CamelException *ex)
323 {
324         /* Things like the CamelMboxStore can validly
325          * not define a connect function.
326          */
327          return TRUE;
328 }
329
330
331 /**
332  * camel_service_connect:
333  * @service: a #CamelService object
334  * @ex: a #CamelException
335  *
336  * Connect to the service using the parameters it was initialized
337  * with.
338  *
339  * Returns %TRUE if the connection is made or %FALSE otherwise
340  **/
341 gboolean
342 camel_service_connect (CamelService *service, CamelException *ex)
343 {
344         gboolean ret = FALSE;
345         gboolean unreg = FALSE;
346         
347         g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
348         g_return_val_if_fail (service->session != NULL, FALSE);
349         g_return_val_if_fail (service->url != NULL, FALSE);
350         
351         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
352         
353         if (service->status == CAMEL_SERVICE_CONNECTED) {
354                 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
355                 return TRUE;
356         }
357
358         /* Register a separate operation for connecting, so that
359          * the offline code can cancel it.
360          */
361         CAMEL_SERVICE_LOCK (service, connect_op_lock);
362         service->connect_op = camel_operation_registered ();
363         if (!service->connect_op) {
364                 service->connect_op = camel_operation_new (NULL, NULL);
365                 camel_operation_register (service->connect_op);
366                 unreg = TRUE;
367         }
368         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
369
370         service->status = CAMEL_SERVICE_CONNECTING;
371         ret = CSERV_CLASS (service)->connect (service, ex);
372         service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
373
374         CAMEL_SERVICE_LOCK (service, connect_op_lock);
375         if (service->connect_op) {
376                 if (unreg)
377                         camel_operation_unregister (service->connect_op);
378                 
379                 camel_operation_unref (service->connect_op);
380                 service->connect_op = NULL;
381         }
382         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
383
384         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
385         
386         return ret;
387 }
388
389 static gboolean
390 service_disconnect (CamelService *service, gboolean clean, CamelException *ex)
391 {
392         /*service->connect_level--;*/
393
394         /* We let people get away with not having a disconnect
395          * function -- CamelMboxStore, for example. 
396          */
397         
398         return TRUE;
399 }
400
401
402 /**
403  * camel_service_disconnect:
404  * @service: a #CamelService object
405  * @clean: whether or not to try to disconnect cleanly
406  * @ex: a #CamelException
407  *
408  * Disconnect from the service. If @clean is %FALSE, it should not
409  * try to do any synchronizing or other cleanup of the connection.
410  *
411  * Returns %TRUE if the disconnect was successful or %FALSE otherwise
412  **/
413 gboolean
414 camel_service_disconnect (CamelService *service, gboolean clean,
415                           CamelException *ex)
416 {
417         gboolean res = TRUE;
418         int unreg = FALSE;
419
420         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
421         
422         if (service->status != CAMEL_SERVICE_DISCONNECTED
423             && service->status != CAMEL_SERVICE_DISCONNECTING) {
424                 CAMEL_SERVICE_LOCK (service, connect_op_lock);
425                 service->connect_op = camel_operation_registered ();
426                 if (!service->connect_op) {
427                         service->connect_op = camel_operation_new (NULL, NULL);
428                         camel_operation_register (service->connect_op);
429                         unreg = TRUE;
430                 }
431                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
432
433                 service->status = CAMEL_SERVICE_DISCONNECTING;
434                 res = CSERV_CLASS (service)->disconnect (service, clean, ex);
435                 service->status = CAMEL_SERVICE_DISCONNECTED;
436
437                 CAMEL_SERVICE_LOCK (service, connect_op_lock);
438                 if (unreg)
439                         camel_operation_unregister (service->connect_op);
440
441                 camel_operation_unref (service->connect_op);
442                 service->connect_op = NULL;
443                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
444         }
445         
446         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
447         
448                 service->status = CAMEL_SERVICE_DISCONNECTED;
449         return res;
450 }
451
452 static void
453 cancel_connect (CamelService *service)
454 {
455         camel_operation_cancel (service->connect_op);
456 }
457
458 /**
459  * camel_service_cancel_connect:
460  * @service: a #CamelService object
461  *
462  * If @service is currently attempting to connect to or disconnect
463  * from a server, this causes it to stop and fail. Otherwise it is a
464  * no-op.
465  **/
466 void
467 camel_service_cancel_connect (CamelService *service)
468 {
469         CAMEL_SERVICE_LOCK (service, connect_op_lock);
470         if (service->connect_op)
471                 CSERV_CLASS (service)->cancel_connect (service);
472         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
473 }
474
475 /**
476  * camel_service_get_url:
477  * @service: a #CamelService object
478  *
479  * Gets the URL representing @service. The returned URL must be
480  * freed when it is no longer needed. For security reasons, this
481  * routine does not return the password.
482  *
483  * Returns the URL representing @service
484  **/
485 char *
486 camel_service_get_url (CamelService *service)
487 {
488         return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
489 }
490
491
492 static char *
493 get_name (CamelService *service, gboolean brief)
494 {
495         w(g_warning ("CamelService::get_name not implemented for `%s'",
496                      camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service))));
497         return g_strdup ("???");
498 }               
499
500
501 /**
502  * camel_service_get_name:
503  * @service: a #CamelService object
504  * @brief: whether or not to use a briefer form
505  *
506  * This gets the name of the service in a "friendly" (suitable for
507  * humans) form. If @brief is %TRUE, this should be a brief description
508  * such as for use in the folder tree. If @brief is %FALSE, it should
509  * be a more complete and mostly unambiguous description.
510  *
511  * Returns a description of the service which the caller must free
512  **/
513 char *
514 camel_service_get_name (CamelService *service, gboolean brief)
515 {
516         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
517         g_return_val_if_fail (service->url, NULL);
518         
519         return CSERV_CLASS (service)->get_name (service, brief);
520 }
521
522
523 static char *
524 get_path (CamelService *service)
525 {
526         CamelProvider *prov = service->provider;
527         CamelURL *url = service->url;
528         GString *gpath;
529         char *path;
530         
531         /* A sort of ad-hoc default implementation that works for our
532          * current set of services.
533          */
534         
535         gpath = g_string_new (service->provider->protocol);
536         if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
537                 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
538                         g_string_append_printf (gpath, "/%s@%s",
539                                                 url->user ? url->user : "",
540                                                 url->host ? url->host : "");
541                         
542                         if (url->port)
543                                 g_string_append_printf (gpath, ":%d", url->port);
544                 } else {
545                         g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
546                                                 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
547                 }
548         } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
549                 g_string_append_printf (gpath, "/%s%s",
550                                         CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
551                                         url->host ? url->host : "");
552                 
553                 if (url->port)
554                         g_string_append_printf (gpath, ":%d", url->port);
555         }
556         
557         if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
558                 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
559         
560         path = gpath->str;
561         g_string_free (gpath, FALSE);
562         
563         return path;
564 }               
565
566 /**
567  * camel_service_get_path:
568  * @service: a #CamelService object
569  *
570  * This gets a valid UNIX relative path describing @service, which
571  * is guaranteed to be different from the path returned for any
572  * different service. This path MUST start with the name of the
573  * provider, followed by a "/", but after that, it is up to the
574  * provider.
575  *
576  * Returns the path, which the caller must free
577  **/
578 char *
579 camel_service_get_path (CamelService *service)
580 {
581         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
582         g_return_val_if_fail (service->url, NULL);
583         
584         return CSERV_CLASS (service)->get_path (service);
585 }
586
587
588 /**
589  * camel_service_get_session:
590  * @service: a #CamelService object
591  *
592  * Gets the #CamelSession associated with the service.
593  *
594  * Returns the session
595  **/
596 CamelSession *
597 camel_service_get_session (CamelService *service)
598 {
599         return service->session;
600 }
601
602
603 /**
604  * camel_service_get_provider:
605  * @service: a #CamelService object
606  *
607  * Gets the #CamelProvider associated with the service.
608  *
609  * Returns the provider
610  **/
611 CamelProvider *
612 camel_service_get_provider (CamelService *service)
613 {
614         return service->provider;
615 }
616
617 static GList *
618 query_auth_types (CamelService *service, CamelException *ex)
619 {
620         return NULL;
621 }
622
623
624 /**
625  * camel_service_query_auth_types:
626  * @service: a #CamelService object
627  * @ex: a #CamelException
628  *
629  * This is used by the mail source wizard to get the list of
630  * authentication types supported by the protocol, and information
631  * about them.
632  *
633  * Returns a list of #CamelServiceAuthType records. The caller
634  * must free the list with #g_list_free when it is done with it.
635  **/
636 GList *
637 camel_service_query_auth_types (CamelService *service, CamelException *ex)
638 {
639         GList *ret;
640         
641         /* note that we get the connect lock here, which means the callee
642            must not call the connect functions itself */
643         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
644         ret = CSERV_CLASS (service)->query_auth_types (service, ex);
645         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
646         
647         return ret;
648 }