Blame view

mh/folder.c 13.5 KB
Wojciech Polak authored
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3

Wojciech Polak authored
4
   GNU Mailutils is free software; you can redistribute it and/or modify
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.

Wojciech Polak authored
9
   GNU Mailutils is distributed in the hope that it will be useful,
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
Wojciech Polak authored
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  */
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/* MH folder command */

#include <mh.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <dirent.h>

#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#include <obstack.h>

35
const char *program_version = "folder (" PACKAGE_STRING ")";
uid65697 authored
36 37
/* TRANSLATORS: Please, preserve the vertical tabulation (^K character)
   in this message */
38 39
static char doc[] = N_("GNU MH folder\v\
Use -help to obtain the list of traditional MH options.");
Wojciech Polak authored
40
static char args_doc[] = N_("[action] [msg]");
41 42

static struct argp_option options[] = {
Sergey Poznyakoff authored
43
  {N_("Actions are:"), 0, 0, OPTION_DOC, NULL, 0 },
44 45 46 47 48
  {"print", ARG_PRINT, NULL, 0,
   N_("List the folders (default)"), 1 },
  {"list",  ARG_LIST,  NULL, 0,
   N_("List the contents of the folder stack"), 1},
  {"push",  ARG_PUSH,  N_("FOLDER"), OPTION_ARG_OPTIONAL,
Wojciech Polak authored
49 50 51 52
    N_("Push the folder on the folder stack. If FOLDER is specified, it is pushed. "
       "Otherwise, if a folder is given in the command line (via + or --folder), "
       "it is pushed on stack. Otherwise, the current folder and the top of the folder "
       "stack are exchanged"), 1},
53 54
  {"pop",   ARG_POP,    NULL, 0,
   N_("Pop the folder off the folder stack"), 1},
55
  
Sergey Poznyakoff authored
56
  {N_("Options are:"), 0, 0, OPTION_DOC, NULL, 2 },
57 58 59 60 61 62
  
  {"folder", ARG_FOLDER, N_("FOLDER"), 0,
   N_("Specify folder to operate upon"), 3},
  {"all",    ARG_ALL,    NULL, 0,
   N_("List all folders"), 3},
  {"create", ARG_CREATE, N_("BOOL"), OPTION_ARG_OPTIONAL, 
63
    N_("Create non-existing folders"), 3},
64
  {"nocreate", ARG_NOCREATE, NULL, OPTION_HIDDEN, ""},
65
  {"fast",   ARG_FAST, N_("BOOL"), OPTION_ARG_OPTIONAL, 
66
    N_("List only the folder names"), 3},
67
  {"nofast", ARG_NOFAST, NULL, OPTION_HIDDEN, ""},
68
  {"header", ARG_HEADER, N_("BOOL"), OPTION_ARG_OPTIONAL, 
69
    N_("Print the header line"), 3},
70
  {"noheader", ARG_NOHEADER, NULL, OPTION_HIDDEN, ""},
71
  {"recurse",ARG_RECURSIVE, N_("BOOL"), OPTION_ARG_OPTIONAL,
72
    N_("Scan folders recursively"), 3},
73
  {"norecurse", ARG_NORECURSIVE, NULL, OPTION_HIDDEN, ""},
74
  {"total",  ARG_TOTAL, N_("BOOL"), OPTION_ARG_OPTIONAL, 
75
    N_("Output the total statistics"), 3},
76
  {"nototal", ARG_NOTOTAL, NULL, OPTION_HIDDEN, ""},
77
  
78 79 80
  {"license", ARG_LICENSE, 0,      0,
   N_("Display software license"), -1},

81 82 83 84 85
  {NULL},
};

/* Traditional MH options */
struct mh_option mh_option[] = {
86 87 88 89 90 91 92 93 94 95
  {"print",   2, 0, NULL },
  {"list",    1, 0, NULL },
  {"push",    2, 0, NULL },
  {"pop",     2, 0, NULL },
  {"all",     1, 0, NULL },
  {"create",  1, MH_OPT_BOOL, NULL},
  {"fast",    1, MH_OPT_BOOL, NULL},
  {"header",  1, MH_OPT_BOOL, NULL},
  {"recurse", 1, MH_OPT_BOOL, NULL},
  {"total",   1, MH_OPT_BOOL, NULL},
96 97 98 99 100 101 102 103 104 105 106 107 108
  {NULL},
};

typedef int (*folder_action) ();

static int action_print ();
static int action_list ();
static int action_push ();
static int action_pop ();

static folder_action action = action_print;

int show_all = 0; /* List all folders. Raised by --all switch */
109 110 111 112
int create_flag = -1; /* Create non-existent folders (--create).
		         -1: Prompt before creating
		          0: Do not create
		          1: Always create without prompting */
113 114 115 116 117 118 119 120 121 122 123
int fast_mode = 0; /* Fast operation mode. (--fast) */
int print_header = 0; /* Display the header line (--header) */
int recurse = 0; /* Recurse sub-folders */
int print_total; /* Display total stats */

char *push_folder; /* Folder name to push on stack */

char *mh_seq_name; /* Name of the mh sequence file (defaults to
		      .mh_sequences) */

static int
124
opt_handler (int key, char *arg, void *unused, struct argp_state *state)
125 126 127
{
  switch (key)
    {
128
    case ARG_PRINT:
129 130 131
      action = action_print;
      break;
      
132
    case ARG_LIST:
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      action = action_list;
      break;
      
    case ARG_PUSH:
      action = action_push;
      if (arg)
	{
	  push_folder = mh_current_folder ();
	  current_folder = arg;
	}
      break;
      
    case ARG_POP:
      action = action_pop;
      break;
      
149
    case ARG_ALL:
150 151 152
      show_all++;
      break;

153
    case ARG_CREATE:
154 155 156
      create_flag = is_true (arg);
      break;

157 158 159
    case ARG_NOCREATE:
      create_flag = 0;
      
160
    case ARG_FAST:
161 162 163
      fast_mode = is_true (arg);
      break;

164 165 166 167
    case ARG_NOFAST:
      fast_mode = 0;
      break;
      
168
    case ARG_HEADER:
169 170 171
      print_header = is_true (arg);
      break;

172 173 174 175
    case ARG_NOHEADER:
      print_header = 0;
      break;
      
176
    case ARG_RECURSIVE:
177 178 179
      recurse = is_true (arg);
      break;

180 181 182 183
    case ARG_NORECURSIVE:
      recurse = 0;
      break;
      
184
    case ARG_TOTAL:
185 186
      print_total = is_true (arg);
      break;
187 188 189 190

    case ARG_NOTOTAL:
      print_total = 0;
      break;
191
      
192
    case ARG_FOLDER:
193 194 195 196
      push_folder = mh_current_folder ();
      current_folder = arg;
      break;
      
197 198 199 200
    case ARG_LICENSE:
      mh_license (argp_program_version);
      break;

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
    default:
      return 1;
    }
  return 0;
}

struct folder_info {
  char *name;              /* Folder name */
  size_t message_count;    /* Number of messages in this folder */
  size_t min;              /* First used sequence number (=uid) */
  size_t max;              /* Last used sequence number */
  size_t cur;              /* UID of the current message */
  size_t others;           /* Number of non-message files */ 
};

struct obstack folder_info_stack; /* Memory storage for folder infp */
struct folder_info *folder_info;  /* Finalized array of information
				     structures */
size_t folder_info_count;         /* Number of the entries in the array */

size_t message_count;             /* Total number of messages */

int name_prefix_len;              /* Length of the mu_path_folder_dir */


void
install_folder_info (const char *name, struct folder_info *info)
{
  info->name = strdup (name) + name_prefix_len;
  obstack_grow (&folder_info_stack, info, sizeof (*info));
  folder_info_count++;
  message_count += info->message_count;
}

static int
folder_info_comp (const void *a, const void *b)
{
  return strcmp (((struct folder_info *)a)->name,
		 ((struct folder_info *)b)->name);
}

static void
read_seq_file (struct folder_info *info, const char *prefix, const char *name)
{
  char *pname = NULL;
  mh_context_t *ctx;
  char *p;
  
  asprintf (&pname, "%s/%s", prefix, name);
  if (!pname)
    abort ();
  ctx = mh_context_create (pname, 1);
  mh_context_read (ctx);
  
  p = mh_context_get_value (ctx, "cur", NULL);
  if (p)
    info->cur = strtoul (p, NULL, 0);
  free (pname);
  free (ctx);
}

static void
_scan (const char *name, int depth)
{
  DIR *dir;
  struct dirent *entry;
  struct folder_info info;
  char *p;
  struct stat st;
  size_t uid;

272
  if (!recurse)
273
    {
274 275 276 277 278 279 280 281 282 283
      if (fast_mode && depth > 0)
	{
	  memset (&info, 0, sizeof (info));
	  info.name = strdup (name);
	  install_folder_info (name, &info);
	  return;
	}
      
      if (depth > 1)
	return;
284 285 286 287
    }
  
  dir = opendir (name);

288
  if (!dir && errno == ENOENT)
289
    {
290 291 292 293 294 295 296 297 298 299 300 301 302 303
      if (create_flag)
	{
	  if (mh_check_folder (name, create_flag == -1))
	    {
	      push_folder = 0;
	      return;
	    }
	  dir = opendir (name);
	}
      else
	{
	  push_folder = 0;
	  return;
	}
304 305 306 307
    }

  if (!dir)
    {
Wojciech Polak authored
308
      mh_error (_("can't scan folder %s: %s"), name, strerror (errno));
309 310 311 312 313 314 315
      return;
    }

  memset (&info, 0, sizeof (info));
  info.name = strdup (name);
  while ((entry = readdir (dir)))
    {
316
      if (entry->d_name[0] == '.')
317 318 319
	{
	  if (strcmp (entry->d_name, mh_seq_name) == 0)
	    read_seq_file (&info, name, entry->d_name);
320 321 322
	}
      else if (entry->d_name[0] != ',')
	{
323 324
	  asprintf (&p, "%s/%s", name, entry->d_name);
	  if (stat (p, &st) < 0)
Wojciech Polak authored
325
	    mh_error (_("can't stat %s: %s"), p, strerror (errno));
326 327
	  else if (S_ISDIR (st.st_mode))
	    {
328
	      info.others++;
329 330 331
	      _scan (p, depth+1);
	    }
	  else
332 333 334 335 336 337 338 339 340 341 342 343 344 345
	    {
	      char *endp;
	      uid = strtoul (entry->d_name, &endp, 10);
	      if (*endp)
		info.others++;
	      else
		{
		  info.message_count++;
		  if (info.min == 0 || uid < info.min)
		    info.min = uid;
		  if (uid > info.max)
		    info.max = uid;
		}
	    }
346
	}
347 348 349 350 351 352 353 354
    }
  
  if (info.cur)
    {
      asprintf (&p, "%s/%lu", name, (unsigned long) info.cur);
      if (stat (p, &st) < 0 || !S_ISREG (st.st_mode))
	info.cur = 0;
      free (p);
355 356
    }
  closedir (dir);
357 358
  if (depth > 0)
    install_folder_info (name, &info);
359 360 361 362 363 364 365 366 367
}
    
static void
print_all ()
{
  struct folder_info *info, *end = folder_info + folder_info_count;

  for (info = folder_info; info < end; info++)
    {
368
      int len = strlen (info->name);
369 370 371 372 373
      if (len < 22)
	printf ("%22.22s", info->name);
      else
	printf ("%s", info->name);
      
374
      if (strcmp (info->name, current_folder) == 0)
375 376 377 378
	printf ("+");
      else
	printf (" ");
      
379 380
      if (info->message_count)
	{
Sergey Poznyakoff authored
381 382 383
	  printf (ngettext(" has %4lu message  (%4lu-%4lu)",
			   " has %4lu messages (%4lu-%4lu)",
			   info->message_count),
384 385 386 387 388 389 390 391
		  (unsigned long) info->message_count,
		  (unsigned long) info->min,
		  (unsigned long) info->max);
	  if (info->cur)
	    printf ("; cur=%4lu", (unsigned long) info->cur);
	}
      else
	{
Wojciech Polak authored
392
	  printf (_(" has no messages"));
393 394 395 396 397 398 399 400
	}
      
      if (info->others)
	{
	  if (!info->cur)
	    printf (";           ");
	  else
	    printf ("; ");
Wojciech Polak authored
401
	  printf (_("(others)"));
402
	}
403
      printf (".\n");
404 405 406 407 408 409 410 411 412
    }
}

static void
print_fast ()
{
  struct folder_info *info, *end = folder_info + folder_info_count;

  for (info = folder_info; info < end; info++)
413
    printf ("%s\n", info->name);
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
}

static int
action_print ()
{
  mh_seq_name = mh_global_profile_get ("mh-sequences", MH_SEQUENCES_FILE);

  name_prefix_len = strlen (mu_path_folder_dir);
  if (mu_path_folder_dir[name_prefix_len - 1] == '/')
    name_prefix_len++;
  name_prefix_len++;  /* skip past the slash */

  obstack_init (&folder_info_stack);

  if (show_all)
    {
      _scan (mu_path_folder_dir, 0);
    }
  else
    {
434
      char *p = mh_expand_name (NULL, current_folder, 0);
435 436 437 438 439 440 441 442 443 444 445
      _scan (p, 1);
      free (p);
    }
  
  folder_info = obstack_finish (&folder_info_stack);
  qsort (folder_info, folder_info_count, sizeof (folder_info[0]),
	 folder_info_comp);

  if (fast_mode)
    print_fast ();
  else
446
    {
Sergey Poznyakoff authored
447 448
      if (print_header)                                   
	printf (_("Folder                  # of messages     (  range  )  cur msg   (other files)\n"));
449 450 451 452
		
      print_all ();

      if (print_total)
Sergey Poznyakoff authored
453 454 455 456 457 458 459 460 461 462
	{
	  printf ("\n%24.24s=", _("TOTAL"));
	  printf (ngettext ("%4lu message  ", "%4lu messages ",
			    message_count),
		  (unsigned long) message_count);
	  printf (ngettext ("in %4lu folder", "in %4lu folders",
			    folder_info_count),
		  (unsigned long) folder_info_count);
	  printf ("\n");
	}
463
    }
464 465 466 467 468 469 470 471 472 473 474 475 476
  if (push_folder)
    mh_global_save_state ();

  return 0;
}

static int
action_list ()
{
  char *stack = mh_global_context_get ("Folder-Stack", NULL);

  printf ("%s", current_folder);
  if (stack)
477 478
    printf (" %s", stack);
  printf ("\n");
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
  return 0;
}

static char *
make_stack (char *folder, char *old_stack)
{
  char *stack = NULL;
  if (old_stack)
    asprintf (&stack,  "%s %s", folder, old_stack);
  else
    stack = strdup (folder);
  return stack;
}

static int
action_push ()
{
  char *stack = mh_global_context_get ("Folder-Stack", NULL);
  char *new_stack = NULL;

  if (push_folder)
    new_stack = make_stack (push_folder, stack);
  else
    {
      char *s, *p = strtok_r (stack, " ", &s);
      new_stack = make_stack (current_folder, stack);
      current_folder = p;
    }
  
  mh_global_context_set ("Folder-Stack", new_stack);
  action_list ();
  mh_global_save_state ();
  return 0;
}

static int
action_pop ()
{
  char *stack = mh_global_context_get ("Folder-Stack", NULL);
518 519 520 521 522 523 524 525 526 527 528 529 530
  char *s, *p;

  if (stack)
    {
      p = strtok_r (stack, " ", &s);
      if (s[0] == 0)
	s = NULL;
    }
  else
    {
      p = current_folder;
      s = NULL;
    }
531 532 533 534 535 536 537 538 539 540 541
  mh_global_context_set ("Folder-Stack", s);
  current_folder = p;
  action_list ();
  mh_global_save_state ();
  return 0;
}

int
main (int argc, char **argv)
{
  int index = 0;
542
  mh_msgset_t msgset;
Wojciech Polak authored
543 544 545 546

  /* Native Language Support */
  mu_init_nls ();

547
  mu_argp_init (program_version, NULL);
548
  mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
549 550 551 552 553 554 555
		 opt_handler, NULL, &index);

  /* If  folder  is invoked by a name ending with "s" (e.g.,  folders),
     `-all'  is  assumed */
  if (program_invocation_short_name[strlen (program_invocation_short_name) - 1] == 's')
    show_all++;

556 557 558
  if (argc - index == 1)
    {
      mailbox_t mbox = mh_open_folder (current_folder, 0);
559 560
      mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
      mh_msgset_current (mbox, &msgset, 0);
561 562 563 564 565 566
      mh_global_save_state ();
      mailbox_close (mbox);
      mailbox_destroy (&mbox);
    }
  else if (argc - index > 1)
    {
Wojciech Polak authored
567
      mh_error (_("too many arguments"));
568 569 570
      exit (1);
    }
  
571 572 573 574 575
  if (show_all)
    print_header = print_total = 1;
  
  return (*action) ();
}