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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2002 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 2, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
struct inode_list
{
struct inode_list *next;
ino_t inode;
dev_t dev;
};
/*
1- IMAP4 insists: the reference argument that is include 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 charcater "%" is similar to "*",
but it does not match ahierarchy delimiter. */
static int match __P ((const char *, const char *, const char *));
static void list_file __P ((const char *, const char *, const char *, const char *, struct inode_list *));
static void print_file __P ((const char *, const char *, const char *));
static void print_dir __P ((const char *, const char *, const char *));
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
{
char *cwd;
char *dir;
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 (; (dir = strpbrk (wcard, "/%*")); wcard = dir)
{
if (*dir == '/')
{
*dir = '\0';
ref = realloc (ref, strlen (ref) + 1 + (dir - wcard) + 1);
if (*ref && ref[strlen (ref) - 1] != '/')
strcat (ref, "/");
strcat (ref, wcard);
dir++;
}
else
dir = wcard;
break;
}
/* Allocates. */
cwd = namespace_checkfullpath (ref, wcard, delim);
if (!cwd)
{
free (ref);
return util_finish (command, RESP_NO,
"The requested item could not be found.");
}
/* 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 && (match ("INBOX", wcard, delim)
|| match ("inbox", wcard, delim)))
util_out (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX");
if (chdir (cwd) == 0)
{
struct stat st;
struct inode_list inode_rec;
stat (cwd, &st);
inode_rec.next = NULL;
inode_rec.inode = st.st_ino;
inode_rec.dev = st.st_dev;
list_file (cwd, ref, (dir) ? dir : wcard, delim, &inode_rec);
chdir (homedir);
}
free (cwd);
free (ref);
}
return util_finish (command, RESP_OK, "Completed");
}
static int
inode_list_lookup (struct inode_list *list, struct stat *st)
{
for (; list; list = list->next)
if (list->inode == st->st_ino && list->dev == st->st_dev)
return 1;
return 0;
}
/* Recusively calling the files. */
static void
list_file (const char *cwd, const char *ref, const char *pattern,
const char *delim, struct inode_list *inode_list)
{
DIR *dirp;
struct dirent *dp;
char *next;
/* Shortcut no wildcards. */
if (*pattern == '\0' || !strpbrk (pattern, "%*"))
{
/* Equivalent to stat(). */
int status;
if (*pattern == '\0')
status = match (cwd, cwd, delim);
else
status = match (pattern, pattern, delim);
if (status & NOSELECT)
print_dir (ref, pattern, delim);
else if (status & NOINFERIORS)
print_file (ref, pattern, delim);
return ;
}
dirp = opendir (".");
if (dirp == NULL)
return;
next = strchr (pattern, delim[0]);
if (next)
*next++ = '\0';
while ((dp = readdir (dirp)) != NULL)
{
/* Skip "", ".", and "..". "" is returned by at least one buggy
implementation: Solaris 2.4 readdir on NFS filesystems. */
char const *entry = dp->d_name;
if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0' &&
!(!strcmp (entry, "INBOX") && !strcmp(cwd, homedir)))
{
int status = match (entry, pattern, delim);
if (status)
{
if (status & NOSELECT)
{
struct stat st;
if (stat (entry, &st))
{
mu_error (_("Cannot stat %s: %s"),
entry, strerror (errno));
continue;
}
if (next || status & RECURSE_MATCH)
{
if (!next)
print_dir (ref, entry, delim);
if (S_ISDIR (st.st_mode)
&& inode_list_lookup (inode_list, &st) == 0)
{
if (chdir (entry) == 0)
{
char *rf;
char *cd;
struct inode_list inode_rec;
inode_rec.inode = st.st_ino;
inode_rec.dev = st.st_dev;
inode_rec.next = inode_list;
rf = calloc (strlen (ref) + strlen (delim) +
strlen (entry) + 1, 1);
sprintf (rf, "%s%s%s", ref, delim, entry);
cd = calloc (strlen (cwd) + strlen (delim) +
strlen (entry) + 1, 1);
sprintf (cd, "%s%s%s", cwd, delim, entry);
list_file (cd, rf, (next) ? next : pattern,
delim, &inode_rec);
free (rf);
free (cd);
chdir (cwd);
}
}
}
else
print_dir (ref, entry, delim);
}
else if (status & NOINFERIORS)
{
print_file (ref, entry, delim);
}
}
}
}
closedir (dirp);
}
/* Make sure that the file name does not contain any undesirable
chars like "{}. If yes send it as a literal string. */
static void
print_file (const char *ref, const char *file, const char *delim)
{
if (strpbrk (file, "\"{}"))
{
util_out (RESP_NONE, "LIST (\\NoInferiors) \"%s\" {%d}", delim,
strlen (ref) + strlen ((*ref) ? delim : "") + strlen (file));
util_send ("%s%s%s\r\n", ref, (*ref) ? delim : "", file);
}
else
util_out (RESP_NONE, "LIST (\\NoInferiors) \"%s\" %s%s%s", delim,
ref, (*ref) ? delim : "", file);
}
/* Make sure that the file name does not contain any undesirable
chars like "{}. If yes send it as a literal string. */
static void
print_dir (const char *ref, const char *file, const char *delim)
{
if (strpbrk (file, "\"{}"))
{
util_out (RESP_NONE, "LIST (\\NoSelect) \"%s\" {%d}", delim,
strlen (ref) + strlen ((*ref) ? delim : "") + strlen (file));
util_send ("%s%s%s\r\n", ref, (*ref) ? delim : "", file);
}
else if (*file)
util_out (RESP_NONE, "LIST (\\NoSelect) \"%s\" %s%s%s", delim,
ref, (*ref) ? delim : "", file);
else
util_out (RESP_NONE, "LIST (\\NoSelect) \"%s\" \"%s%s\"", delim,
ref, (*ref) ? delim : "");
}
/* Calls the imap_matcher if a match found out the attribute. */
static int
match (const char *entry, const char *pattern, const char *delim)
{
struct stat stats;
int status = util_wcard_match (entry, pattern, delim);
switch (status)
{
case WCARD_RECURSE_MATCH:
status = RECURSE_MATCH;
break;
case WCARD_MATCH:
status = MATCH;
break;
case WCARD_NOMATCH:
status = NOMATCH;
}
if (status)
{
if (stat (entry, &stats) == 0)
status |= (S_ISREG (stats.st_mode)) ? NOINFERIORS : NOSELECT;
}
return status;
}
|