aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-provider.c
blob: 1b8ef2cab5f8358b0ef6b0e0409b9c50a2a1290e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-provider.c: provider framework */

/*
 *
 * Authors:
 *  Bertrand Guiheneuf <bertrand@helixcode.com>
 *  Dan Winship <danw@ximian.com>
 *  Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


/* FIXME: Shouldn't we add a version number to providers ? */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include <gmodule.h>

#include "camel-provider.h"
#include "camel-exception.h"
#include "camel-string-utils.h"


/**
 * camel_provider_init:
 *
 * Initialize the Camel provider system by reading in the .urls
 * files in the provider directory and creating a hash table mapping
 * URLs to module names.
 *
 * A .urls file has the same initial prefix as the shared library it
 * correspond to, and consists of a series of lines containing the URL
 * protocols that that library handles.
 *
 * Return value: a hash table mapping URLs to module names
 **/
GHashTable *
camel_provider_init (void)
{
    GHashTable *providers;
    DIR *dir;
    struct dirent *d;
    char *p, *name, buf[80];
    CamelProviderModule *m;

    providers = g_hash_table_new (camel_strcase_hash, camel_strcase_equal);
    
    dir = opendir (CAMEL_PROVIDERDIR);
    if (!dir) {
        g_error ("Could not open camel provider directory (%s): %s",
             CAMEL_PROVIDERDIR, g_strerror (errno));
        return NULL;
    }
    
    while ((d = readdir (dir))) {
        FILE *fp;
        
        p = strrchr (d->d_name, '.');
        if (!p || strcmp (p, ".urls") != 0)
            continue;
        
        name = g_strdup_printf ("%s/%s", CAMEL_PROVIDERDIR, d->d_name);
        fp = fopen (name, "r");
        if (!fp) {
            g_warning ("Could not read provider info file %s: %s",
                   name, g_strerror (errno));
            g_free (name);
            continue;
        }
        
        p = strrchr (name, '.');
        strcpy (p, ".so");

        m = g_malloc0(sizeof(*m));
        m->path = name;

        while ((fgets (buf, sizeof (buf), fp))) {
            buf[sizeof (buf) - 1] = '\0';
            p = strchr (buf, '\n');
            if (p)
                *p = '\0';
            
            if (*buf) {
                char *protocol = g_strdup(buf);

                m->types = g_slist_prepend(m->types, protocol);
                g_hash_table_insert(providers, protocol, m);
            }
        }

        fclose (fp);
    }

    closedir (dir);
    return providers;
}

/**
 * camel_provider_load:
 * @session: the current session
 * @path: the path to a shared library
 * @ex: a CamelException
 *
 * Loads the provider at @path, and calls its initialization function,
 * passing @session as an argument. The provider should then register
 * itself with @session.
 **/ 
void
camel_provider_load (CamelSession *session, const char *path, CamelException *ex)
{
    GModule *module;
    CamelProvider *(*camel_provider_module_init) ();

    if (!g_module_supported ()) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Could not load %s: Module loading "
                      "not supported on this system."),
                      path);
        return;
    }

    module = g_module_open (path, 0);
    if (!module) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Could not load %s: %s"),
                      path, g_module_error ());
        return;
    }

    if (!g_module_symbol (module, "camel_provider_module_init",
                  (gpointer *)&camel_provider_module_init)) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Could not load %s: No initialization "
                    "code in module."), path);
        g_module_close (module);
        return;
    }

    camel_provider_module_init (session);
}


/**
 * camel_provider_auto_detect:
 * @provider: camel provider
 * @settings: currently set settings
 * @auto_detected: output hash table of auto-detected values
 * @ex: exception
 *
 * After filling in the standard Username/Hostname/Port/Path settings
 * (which must be set in @settings), if the provider supports it, you
 * may wish to have the provider auto-detect further settings based on
 * the aformentioned settings.
 *
 * If the provider does not support auto-detection, @auto_detected
 * will be set to %NULL. Otherwise the provider will attempt to
 * auto-detect whatever it can and file them into @auto_detected. If
 * for some reason it cannot auto-detect anything (not enough
 * information provided in @settings?) then @auto_deetected will be
 * set to %NULL and an exception may be set to explain why it failed.
 *
 * Returns 0 on success or -1 on fail.
 **/
int
camel_provider_auto_detect (CamelProvider *provider, CamelURL *url,
                GHashTable **auto_detected, CamelException *ex)
{
    g_return_val_if_fail (provider != NULL, -1);
    
    if (provider->auto_detect) {
        return provider->auto_detect (url, auto_detected, ex);
    } else {
        *auto_detected = NULL;
        return 0;
    }
}