Blame view

imap4d/list.c 9.71 KB
1 2
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
Jakob Kaivo authored
3

4
   GNU Mailutils is free software; you can redistribute it and/or modify
Jakob Kaivo authored
5 6 7 8
   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.

9
   GNU Mailutils is distributed in the hope that it will be useful,
Jakob Kaivo authored
10 11 12 13 14
   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
15
   along with GNU Mailutils; if not, write to the Free Software
Wojciech Polak authored
16
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */
Jakob Kaivo authored
17 18

#include "imap4d.h"
19
#include <dirent.h>
20
#include <pwd.h>
21

22
#define NOMATCH          (0)
23 24
#define MATCH            (1 << 0)
#define RECURSE_MATCH    (1 << 1)
25 26 27
#define NOSELECT         (1 << 2)
#define NOINFERIORS      (1 << 3)
#define NOSELECT_RECURSE (1 << 4)
Jakob Kaivo authored
28

29 30 31 32
struct inode_list
{
  struct inode_list *next;
  ino_t inode;
33
  dev_t dev;
34 35
};

Jakob Kaivo authored
36
/*
37 38 39 40 41 42 43 44 45 46 47 48 49
  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/ *
Jakob Kaivo authored
50

51 52 53 54 55
  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 *));
56
static void list_file __P ((const char *, const char *, const char *, const char *, struct inode_list *));
57 58
static void print_file __P ((const char *, const char *, const char *));
static void print_dir __P ((const char *, const char *, const char *));
59

Jakob Kaivo authored
60
int
61
imap4d_list (struct imap4d_command *command, char *arg)
Jakob Kaivo authored
62
{
63
  char *sp = NULL;
64 65 66
  char *ref;
  char *wcard;
  const char *delim = "/";
67

68 69 70
  ref = util_getword (arg, &sp);
  wcard = util_getword (NULL, &sp);
  if (!ref || !wcard)
71 72
    return util_finish (command, RESP_BAD, "Too few arguments");

73 74 75 76 77 78 79
  /* 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')
80
    {
81 82 83 84 85 86 87
      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");
88 89 90
    }
  else
    {
91 92 93 94 95 96 97 98 99 100 101 102
      char *cwd;
      char *dir;
      switch (*wcard)
	{
	  /* Absolute Path in wcard, dump the old ref.  */
	case '/':
	  {
	    ref = calloc (2, 1);
	    ref[0] = *wcard;
	    wcard++;
	  }
	  break;
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	  /* 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;
123

124 125
	default:
	  ref = strdup (ref);
126 127
	}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      /* 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
143
	    dir = wcard;	  
144 145
	    break;
	}
146

147
      /* Allocates.  */
Sergey Poznyakoff authored
148 149 150 151 152 153 154
      cwd = namespace_checkfullpath (ref, wcard, delim);
      if (!cwd)
	{
	  free (ref);
	  return util_finish (command, RESP_NO,
			      "The requested item could not be found.");
	}
155

156 157 158 159 160 161 162 163
      /* 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. */

164 165 166
      if (!*ref && (match ("INBOX", wcard, delim)
		    || match ("inbox", wcard, delim)))
	util_out (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX");
Sergey Poznyakoff authored
167
      
168 169
      if (chdir (cwd) == 0)
	{
170 171 172 173 174 175
	  struct stat st;
	  struct inode_list inode_rec;
	  
	  stat (cwd, &st);
	  inode_rec.next = NULL;
	  inode_rec.inode = st.st_ino;
176
	  inode_rec.dev   = st.st_dev;
177
	  list_file (cwd, ref, (dir) ? dir : wcard, delim, &inode_rec);
178 179 180 181
	  chdir (homedir);
	}
      free (cwd);
      free (ref);
182
    }
Sergey Poznyakoff authored
183

184
  return util_finish (command, RESP_OK, "Completed");
Jakob Kaivo authored
185
}
186

187
static int
188
inode_list_lookup (struct inode_list *list, struct stat *st)
189 190
{
  for (; list; list = list->next)
191
    if (list->inode == st->st_ino && list->dev == st->st_dev)
192 193 194 195 196
      return 1;
  return 0;
}


197 198
/* Recusively calling the files.  */
static void
199
list_file (const char *cwd, const char *ref, const char *pattern,
200
	   const char *delim, struct inode_list *inode_list)
201 202 203
{
  DIR *dirp;
  struct dirent *dp;
204 205 206
  char *next;

  /* Shortcut no wildcards.  */
207
  if (*pattern == '\0' || !strpbrk (pattern, "%*"))
208 209
    {
      /* Equivalent to stat().  */
210 211 212 213 214
      int status;
      if (*pattern == '\0')
	status = match (cwd, cwd, delim);
      else
	status = match (pattern, pattern, delim);
215 216 217 218 219 220
      if (status & NOSELECT)
	print_dir (ref, pattern, delim);
      else if (status & NOINFERIORS)
	print_file (ref, pattern, delim);
      return ;
    }
221 222 223 224 225

  dirp = opendir (".");
  if (dirp == NULL)
    return;

226 227 228
  next = strchr (pattern, delim[0]);
  if (next)
    *next++ = '\0';
229 230
  while ((dp = readdir (dirp)) != NULL)
    {
231 232 233
      /* Skip "", ".", and "..".  "" is returned by at least one buggy
	 implementation: Solaris 2.4 readdir on NFS filesystems.  */
      char const *entry = dp->d_name;
234
      if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0' &&
235
	  !(!strcmp (entry, "INBOX") && !strcmp(cwd, homedir)))
236
	{
237 238
	  int status = match (entry, pattern, delim);
	  if (status)
239
	    {
240
	      if (status & NOSELECT)
241
		{
242 243 244 245
		  struct stat st;

		  if (stat (entry, &st))
		    {
246
		      mu_error (_("can't stat %s: %s"),
247 248 249 250
				entry, strerror (errno));
		      continue;
		    }

251
		  if (next || status & RECURSE_MATCH)
252
		    {
253 254
		      if (!next)
			print_dir (ref, entry, delim);
255 256

		      if (S_ISDIR (st.st_mode)
257
			  && inode_list_lookup (inode_list, &st) == 0)
258
			{
259 260 261 262 263 264 265
			  if (chdir (entry) == 0)
			    {
			      char *rf;
			      char *cd;
			      struct inode_list inode_rec;

			      inode_rec.inode = st.st_ino;
266
			      inode_rec.dev   = st.st_dev;
267 268 269 270 271 272 273 274 275 276 277 278 279
			      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);
			    }
280
			}
281
		    }
282 283 284 285 286 287
		  else
		    print_dir (ref, entry, delim);
		}
	      else if (status & NOINFERIORS)
		{
		  print_file (ref, entry, delim);
288 289 290 291
		}
	    }
	}
    }
292
  closedir (dirp);
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
/* 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);
    }
322
  else if (*file)
323 324
    util_out (RESP_NONE, "LIST (\\NoSelect) \"%s\" %s%s%s", delim,
	      ref, (*ref) ? delim : "", file);
325 326 327
  else
    util_out (RESP_NONE, "LIST (\\NoSelect) \"%s\" \"%s%s\"", delim,
	      ref, (*ref) ? delim : "");
328 329 330
}

/* Calls the imap_matcher if a match found out the attribute. */
331
static int
332
match (const char *entry, const char *pattern, const char *delim)
333 334
{
  struct stat stats;
335 336 337 338 339 340 341 342 343 344 345 346 347 348
  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;
    }
  
349
  if (status)
350 351 352 353 354 355 356
    {
      if (stat (entry, &stats) == 0)
	status |=  (S_ISREG (stats.st_mode)) ? NOINFERIORS : NOSELECT;
    }
  return status;
}