aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/local/camel-local-provider.c
blob: a52dfbde5ffce3d7d7e04d41ebc5760b7734ccc9 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * 
 * Authors: Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 2000 Ximian (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
 */

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

#include <stdio.h>
#include <string.h>

#include "camel-provider.h"
#include "camel-session.h"
#include "camel-url.h"

#include "camel-mh-store.h"
#include "camel-mbox-store.h"
#include "camel-maildir-store.h"
#include "camel-spool-store.h"

#define d(x)

static CamelProviderConfEntry mh_conf_entries[] = {
    CAMEL_PROVIDER_CONF_DEFAULT_PATH,
    { CAMEL_PROVIDER_CONF_CHECKBOX, "dotfolders", NULL,
      N_("Use the `.folders' folder summary file (exmh)"), "0" },
    { CAMEL_PROVIDER_CONF_END }
};

static CamelProvider mh_provider = {
    "mh",
    N_("MH-format mail directories"),
    N_("For storing local mail in MH-like mail directories."),
    "mail",
    CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL,
    CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
    mh_conf_entries,
    /* ... */
};

static CamelProviderConfEntry mbox_conf_entries[] = {
    CAMEL_PROVIDER_CONF_DEFAULT_PATH,
    { CAMEL_PROVIDER_CONF_END }
};

static CamelProvider mbox_provider = {
    "mbox",
    N_("Local delivery"),
    N_("For retrieving (moving) local mail from standard mbox formated spools into folders managed by Evolution."),
    "mail",
    CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL,
    CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
    mbox_conf_entries,
    /* ... */
};

static CamelProviderConfEntry maildir_conf_entries[] = {
    CAMEL_PROVIDER_CONF_DEFAULT_PATH,
    { CAMEL_PROVIDER_CONF_CHECKBOX, "filter", NULL,
      N_("Apply filters to new messages in INBOX"), "0" },
    { CAMEL_PROVIDER_CONF_END }
};

static CamelProvider maildir_provider = {
    "maildir",
    N_("Maildir-format mail directories"),
    N_("For storing local mail in maildir directories."),
    "mail",
    CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL,
    CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
    maildir_conf_entries,
    /* ... */
};

static CamelProviderConfEntry spool_conf_entries[] = {
    CAMEL_PROVIDER_CONF_DEFAULT_PATH,
    { CAMEL_PROVIDER_CONF_CHECKBOX, "filter", NULL, N_("Apply filters to new messages in INBOX"), "0" },
    { CAMEL_PROVIDER_CONF_CHECKBOX, "xstatus", NULL, N_("Store status headers in Elm/Pine/Mutt format"), "0" },
    { CAMEL_PROVIDER_CONF_END }
};

static CamelProvider spool_provider = {
    "spool",
    N_("Standard Unix mbox spool or directory"),
    N_("For reading and storing local mail in external standard mbox spool files.\nMay also be used to read a tree of Elm, Pine, or Mutt style folders."),
    "mail",
    CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE,
    CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
    spool_conf_entries,
    /* ... */
};

/* build a canonical 'path' */
static char *
make_can_path(char *p, char *o)
{
    char c, last, *start = o;

    d(printf("canonical '%s' = ", p));

    last = 0;
    while ((c = *p++)) {
        if (c!='/'
            || (c=='/' && last != '/'))
            *o++ = c;
        last = c;
    }
    if (o>start && o[-1] == '/')
        o[-1] = 0;
    else
        *o = 0;

    d(printf("'%s'\n", start));

    return start;
}

/* 'helper' function for it */
#define get_can_path(p) ((p == NULL) ? NULL : (make_can_path ((p), g_alloca (strlen (p) + 1))))

static guint
local_url_hash (const void *v)
{
    const CamelURL *u = v;
    guint hash = 0;
    
#define ADD_HASH(s) if (s) hash ^= g_str_hash (s);
    
    ADD_HASH (u->protocol);
    ADD_HASH (u->user);
    ADD_HASH (u->authmech);
    ADD_HASH (u->host);
    if (u->path)
        hash ^= g_str_hash(get_can_path(u->path));
    ADD_HASH (u->path);
    ADD_HASH (u->query);
    hash ^= u->port;
    
    return hash;
}

static int
check_equal (char *s1, char *s2)
{
    if (s1 == NULL) {
        if (s2 == NULL)
            return TRUE;
        else
            return FALSE;
    }
    
    if (s2 == NULL)
        return FALSE;

    return strcmp (s1, s2) == 0;
}

static int
local_url_equal(const void *v, const void *v2)
{
    const CamelURL *u1 = v, *u2 = v2;
    char *p1, *p2;

    p1 = get_can_path(u1->path);
    p2 = get_can_path(u2->path);
    return check_equal(p1, p2)
        && check_equal(u1->protocol, u2->protocol)
        && check_equal(u1->user, u2->user)
        && check_equal(u1->authmech, u2->authmech)
        && check_equal(u1->host, u2->host)
        && check_equal(u1->query, u2->query)
        && u1->port == u2->port;
}

void camel_provider_module_init(CamelSession * session)
{
    char *path;
    
    mh_conf_entries[0].value = "";  /* default path */
    mh_provider.object_types[CAMEL_PROVIDER_STORE] = camel_mh_store_get_type ();
    mh_provider.url_hash = local_url_hash;
    mh_provider.url_equal = local_url_equal;
    camel_session_register_provider(session, &mh_provider);
    
    if (!(path = getenv ("MAIL")))
        path = g_strdup_printf (SYSTEM_MAIL_DIR "/%s", g_get_user_name ());
    mbox_conf_entries[0].value = path;  /* default path */
    mbox_provider.object_types[CAMEL_PROVIDER_STORE] = camel_mbox_store_get_type ();
    mbox_provider.url_hash = local_url_hash;
    mbox_provider.url_equal = local_url_equal;
    camel_session_register_provider(session, &mbox_provider);
    
    spool_conf_entries[0].value = path;  /* default path - same as mbox */
    spool_provider.object_types[CAMEL_PROVIDER_STORE] = camel_spool_store_get_type ();
    spool_provider.url_hash = local_url_hash;
    spool_provider.url_equal = local_url_equal;
    camel_session_register_provider(session, &spool_provider);
    
    path = getenv ("MAILDIR");
    maildir_conf_entries[0].value = path ? path : "";  /* default path */
    maildir_provider.object_types[CAMEL_PROVIDER_STORE] = camel_maildir_store_get_type ();
    maildir_provider.url_hash = local_url_hash;
    maildir_provider.url_equal = local_url_equal;
    camel_session_register_provider(session, &maildir_provider);
}