list.c
7.59 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2002, 2005, 2006,
2007 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include "imap4d.h"
#include <dirent.h>
#include <pwd.h>
#define NOMATCH (0)
#define MATCH (1 << 0)
#define RECURSE_MATCH (1 << 1)
#define NOSELECT (1 << 2)
#define NOINFERIORS (1 << 3)
#define NOSELECT_RECURSE (1 << 4)
static int
imap4d_match (const char *name, void *pat, int flags)
{
return util_wcard_match (name, pat, "/");
}
struct refinfo
{
char *refptr; /* Original reference */
size_t reflen; /* Length of the original reference */
size_t pfxlen; /* Length of the current prefix */
size_t homelen; /* Length of homedir */
char *buf;
size_t bufsize;
};
static int
list_fun (mu_folder_t folder, struct mu_list_response *resp, void *data)
{
char *name;
struct refinfo *refinfo = data;
size_t size;
name = resp->name;
size = strlen (name);
if (size == refinfo->homelen + 6
&& memcmp (name, homedir, refinfo->homelen) == 0
&& memcmp (name + refinfo->homelen + 1, "INBOX", 5) == 0)
return 0;
util_send ("* %s", "LIST (");
if ((resp->type & (MU_FOLDER_ATTRIBUTE_FILE|MU_FOLDER_ATTRIBUTE_DIRECTORY))
== (MU_FOLDER_ATTRIBUTE_FILE|MU_FOLDER_ATTRIBUTE_DIRECTORY))
/* nothing */;
else if (resp->type & MU_FOLDER_ATTRIBUTE_FILE)
util_send ("\\NoInferiors");
else if (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY)
util_send ("\\NoSelect");
util_send (") \"%c\" ", resp->separator);
name = resp->name + refinfo->pfxlen;
size = strlen (name) + refinfo->reflen + 1;
if (size > refinfo->bufsize)
{
if (refinfo->buf == NULL)
{
refinfo->bufsize = size;
refinfo->buf = malloc (refinfo->bufsize);
if (!refinfo->buf)
{
mu_error ("%s", mu_strerror (errno));
return 1;
}
memcpy (refinfo->buf, refinfo->refptr, refinfo->reflen);
}
else
{
char *p = realloc (refinfo->buf, size);
if (!p)
{
mu_error ("%s", mu_strerror (errno));
return 1;
}
refinfo->buf = p;
refinfo->bufsize = size;
}
}
if ((refinfo->reflen == 0 || refinfo->refptr[refinfo->reflen - 1] == '/')
&& name[0] == '/')
name++;
strcpy (refinfo->buf + refinfo->reflen, name);
name = refinfo->buf;
if (strpbrk (name, "\"{}"))
util_send ("{%d}\r\n%s\r\n", strlen (name), name);
else if (is_atom (name))
util_send ("%s\r\n", name);
else
util_send ("\"%s\"\r\n", name);
return 0;
}
/*
1- IMAP4 insists: the reference argument present in the
interpreted form SHOULD prefix the interpreted form. It SHOULD
also be in the same form as the reference name argument. This
rule permits the client to determine if the returned mailbox name
is in the context of the reference argument, or if something about
the mailbox argument overrode the reference argument.
ex:
Reference Mailbox --> Interpretation
~smith/Mail foo.* --> ~smith/Mail/foo.*
archive % --> archive/%
#news comp.mail.* --> #news.comp.mail.*
~smith/Mail /usr/doc/foo --> /usr/doc/foo
archive ~fred/Mail --> ~fred/Mail/ *
2- The character "*" is a wildcard, and matches zero or more characters
at this position. The character "%" is similar to "*",
but it does not match the hierarchy delimiter. */
int
imap4d_list (struct imap4d_command *command, char *arg)
{
char *sp = NULL;
char *ref;
char *wcard;
const char *delim = "/";
ref = util_getword (arg, &sp);
wcard = util_getword (NULL, &sp);
if (!ref || !wcard)
return util_finish (command, RESP_BAD, "Too few arguments");
/* Remove the double quotes. */
util_unquote (&ref);
util_unquote (&wcard);
/* If wildcard is empty, it is a special case: we have to
return the hierarchy. */
if (*wcard == '\0')
{
util_out (RESP_NONE, "LIST (\\NoSelect) \"%s\" \"%s\"", delim,
(*ref) ? delim : "");
}
/* There is only one mailbox in the "INBOX" hierarchy ... INBOX. */
else if (strcasecmp (ref, "INBOX") == 0)
{
util_out (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX");
}
else
{
int status;
mu_folder_t folder;
char *cwd;
char *p, *q;
struct refinfo refinfo;
switch (*wcard)
{
/* Absolute Path in wcard, dump the old ref. */
case '/':
{
ref = calloc (2, 1);
ref[0] = *wcard;
wcard++;
}
break;
/* Absolute Path, but take care of things like ~guest/Mail,
ref becomes ref = ~guest. */
case '~':
{
char *s = strchr (wcard, '/');
if (s)
{
ref = calloc (s - wcard + 1, 1);
memcpy (ref, wcard, s - wcard);
ref [s - wcard] = '\0';
wcard = s + 1;
}
else
{
ref = strdup (wcard);
wcard += strlen (wcard);
}
}
break;
default:
ref = strdup (ref);
}
/* Move any directory not containing a wildcard into the reference
So (ref = ~guest, wcard = Mail/folder1/%.vf) -->
(ref = ~guest/Mail/folder1, wcard = %.vf). */
for (p = wcard; (q = strpbrk (p, "/%*")) && *q == '/'; p = q + 1)
;
if (p > wcard)
{
size_t seglen = p - wcard;
size_t reflen = strlen (ref);
int addslash = !!(reflen == 0 || ref[reflen-1] != '/');
size_t len = seglen + reflen + addslash + 1;
ref = realloc (ref, len);
if (addslash)
ref[reflen++] = '/';
memcpy (ref + reflen, wcard, seglen);
ref[reflen + seglen] = 0;
wcard += seglen;
}
/* Allocates. */
cwd = namespace_checkfullpath (ref, wcard, delim);
if (!cwd)
{
free (ref);
return util_finish (command, RESP_NO,
"The requested item could not be found.");
}
status = mu_folder_create (&folder, cwd);
if (status)
{
free (ref);
free (cwd);
return util_finish (command, RESP_NO,
"The requested item could not be found.");
}
mu_folder_set_match (folder, imap4d_match);
memset (&refinfo, 0, sizeof refinfo);
refinfo.refptr = ref;
refinfo.reflen = strlen (ref);
refinfo.pfxlen = strlen (cwd);
refinfo.homelen = strlen (homedir);
/* The special name INBOX is included in the output from LIST, if
INBOX is supported by this server for this user and if the
uppercase string "INBOX" matches the interpreted reference and
mailbox name arguments with wildcards as described above. The
criteria for omitting INBOX is whether SELECT INBOX will return
failure; it is not relevant whether the user's real INBOX resides
on this or some other server. */
if (!*ref && (imap4d_match ("INBOX", wcard, 0) == 0
|| imap4d_match ("inbox", wcard, 0) == 0))
util_out (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX");
mu_folder_enumerate (folder, NULL, wcard, 0, 0, NULL,
list_fun, &refinfo);
mu_folder_destroy (&folder);
free (refinfo.buf);
free (cwd);
free (ref);
}
return util_finish (command, RESP_OK, "Completed");
}