Blame view

mh/mh_whatnow.c 15.2 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 2003, 2005, 2006, 2007, 2009, 2010 Free Software
   Foundation, Inc.
4 5 6

   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
7
   the Free Software Foundation; either version 3, or (at your option)
8 9 10 11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18 19

#include <mh.h>

20 21 22 23 24 25 26 27
#if defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
# define getmaxfd() sysconf (_SC_OPEN_MAX)
#elif defined (HAVE_GETDTABLESIZE)
# define getmaxfd() getdtablesize ()
#else
# define getmaxfd() 64
#endif

28 29 30
typedef int (*handler_fp) (struct mh_whatnow_env *wh,
			   int argc, char **argv,
			   int *status);
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
/* ********************* Auxiliary functions *********************** */
/* Auxiliary function for option comparisons */
static char
strnulcmp (const char *str, const char *pattern)
{
  return strncmp (str, pattern, strlen (str));
}

/* Dispatcher functions */

struct action_tab {
  char *name;
  handler_fp fp;
};

static handler_fp
func (struct action_tab *p, const char *name)
{
  int len;
  
  if (!name)
    return func (p, "help");

  len = strlen (name);
  for (; p->name; p++)
    {
      int min = strlen (p->name);
      if (min > len)
	min = len;
      if (strncmp (p->name, name, min) == 0)
	return p->fp;
    }

65
  mu_error (_("%s is unknown. Hit <CR> for help"), name);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  return NULL;
}

struct helpdata {
  char *name;
  char *descr;
};

/* Functions for printing help information */

#define OPT_DOC_COL  29		/* column in which option text starts */
#define RMARGIN      79		/* right margin used for wrapping */

static int
print_short (const char *str)
{
  int n;
83
  const char *s;
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
  
  for (n = 0; *str; str++, n++)
    {
      switch (*str)
	{
	case '+':
	  putchar ('+');
	  s = _("FOLDER");
	  n += printf ("%s", s);
	  break;

	case '<':
	  switch (str[1]) 
	    {
	    case '>':
	      s = _("SWITCHES");
	      n += printf ("%s", s) - 1;
	      str++;
	      break;
	      
	    case 'e':
	      s = _("EDITOR");
	      n += printf ("%s", s) - 1;
	      str++;
	      break;

	    default:
	      putchar (*str);
	    }
	  break;

	default:
	  putchar (*str);
	}
    }
  return n;
}

static void
123
print_descr (int n, const char *s)
124 125 126
{
  do
    {
127 128
      const char *p;
      const char *space = NULL;
129 130 131 132 133
      
      for (; n < OPT_DOC_COL; n++)
	putchar (' ');

      for (p = s; *p && p < s + (RMARGIN - OPT_DOC_COL); p++)
134
	if (mu_isspace (*p))
135 136 137 138 139 140 141 142 143 144 145
	  space = p;
      
      if (!space || p < s + (RMARGIN - OPT_DOC_COL))
	{
	  printf ("%s", s);
	  s += strlen (s);
	}
      else
	{
	  for (; s < space; s++)
	    putchar (*s);
146
	  for (; *s && mu_isspace (*s); s++)
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
	    ;
	}
      putchar ('\n');
      n = 1;
    }
  while (*s);
}

static int
_help (struct helpdata *helpdata, char *argname)
{
  struct helpdata *p;

  printf ("%s\n", _("Options are:"));
  if (argname == NULL || argname[0] == '?')
    {
      /* Short version */
      for (p = helpdata; p->name; p++)
	{
	  printf ("  ");
	  print_short (p->name);
	  putchar ('\n');
	}
    }
  else
    {
      for (p = helpdata; p->name; p++)
	{
	  int n;
	  
	  n = printf ("  ");
	  n += print_short (p->name);

	  print_descr (n+1, _(p->descr));
	}
    }
  return 0;
}

/* Display the contents of the given file on the terminal */
187 188 189
static void
display_file (const char *name)
{
190
  const char *pager = mh_global_profile_get ("moreproc", getenv ("PAGER"));
191 192 193 194 195

  if (pager)
    mh_spawnp (pager, name);
  else
    {
196
      mu_stream_t stream;
197 198 199 200
      int rc;
      size_t n;
      char buffer[512];
      
201
      rc = mu_file_stream_create (&stream, name, MU_STREAM_READ);
202 203
      if (rc)
	{
204
	  mu_error ("mu_file_stream_create: %s", mu_strerror (rc));
205 206
	  return;
	}
207 208 209

      mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
      while (mu_stream_read (stream, buffer, sizeof buffer - 1, &n) == 0
210 211 212 213 214
	     && n != 0)
	{
	  buffer[n] = '\0';
	  printf ("%s", buffer);
	}
215
      mu_stream_destroy (&stream);
216 217 218
    }
}      

219
static int
220
check_exit_status (const char *progname, int status)
221 222 223 224 225
{
  if (WIFEXITED (status))
    {
      if (WEXITSTATUS (status))
	{
226
	  mu_error (_("command `%s' exited with status %d"),
227 228 229 230 231 232
		    progname, WEXITSTATUS(status));
	  return 1;
	}
      return 0;
    }
  else if (WIFSIGNALED (status))
233
    mu_error (_("command `%s' terminated on signal %d"),
234 235
	      progname, WTERMSIG (status));
  else
236
    mu_error (_("command `%s' terminated abnormally"), progname);
237 238 239 240
  return 1;
}

static int
241 242
invoke (const char *compname, const char *defval, int argc, char **argv,
	const char *extra0, const char *extra1)
243 244
{
  int i, rc;
245
  char **xargv;
246
  const char *progname;
247 248
  int status;
  
249 250 251 252 253 254 255 256 257 258 259
  progname = mh_global_profile_get (compname, defval);
  if (!progname)
    return -1;
  
  xargv = calloc (argc+3, sizeof (*xargv));
  if (!xargv)
    {
      mh_err_memory (0);
      return -1;
    }

Sergey Poznyakoff authored
260
  xargv[0] = (char*) progname;
261 262 263
  for (i = 1; i < argc; i++)
    xargv[i] = argv[i];
  if (extra0)
Sergey Poznyakoff authored
264
    xargv[i++] = (char*) extra0;
265
  if (extra1)
Sergey Poznyakoff authored
266
    xargv[i++] = (char*) extra1;
267
  xargv[i++] = NULL;
268
  rc = mu_spawnvp (xargv[0], xargv, &status);
269
  free (xargv);
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
  return rc ? rc : check_exit_status (progname, status);
}

struct anno_data
{
  char *field;
  char *value;
  int date;
};

static int
anno (void *item, void *data)
{
  struct anno_data *d = item; 
  mh_annotate (item, d->field, d->value, d->date);
  return 0;
}

static void
annotate (struct mh_whatnow_env *wh)
{
  mu_message_t msg;
  mu_address_t addr = NULL;
  size_t i, count;
  
  if (!wh->anno_field || !wh->anno_list)
    return;
  
  msg = mh_file_to_message (NULL, wh->file);
  if (!msg)
    return;
  
  mh_expand_aliases (msg, &addr, NULL, NULL);
  mu_address_get_count (addr, &count);
  for (i = 1; i <= count; i++)
    {
      mu_address_t subaddr;

      if (mu_address_get_nth (addr, i, &subaddr) == 0)
	{
	  size_t size;
	  struct anno_data d;

	  mu_address_to_string (subaddr, NULL, 0, &size);
	  d.value = xmalloc (size + 1);
	  d.field = wh->anno_field;
	  d.date = i == 1;
	  mu_address_to_string (subaddr, d.value, size + 1, NULL);
	  mu_list_do (wh->anno_list, anno, &d);
	  free (d.value);
	  mu_address_destroy (&subaddr);
	}
    }
  mu_address_destroy (&addr);
  mu_message_destroy (&msg, NULL);
325 326
}

327 328

/* ************************ Shell Function ************************* */
329 330

static int
331 332 333
_whatnow (struct mh_whatnow_env *wh, struct action_tab *tab)
{
  int rc, status = 0;
334 335 336 337 338
  char *line = NULL;
  size_t size = 0;
  struct mu_wordsplit ws;
  int wsflags = MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT;
  
339 340
  do
    {
341
      size_t n;
342 343
      handler_fp fun;
      
344 345 346
      mu_printf ("%s ", wh->prompt);
      mu_stream_flush (mu_strout);
      rc = mu_stream_getline (mu_strin, &line, &size, &n);
347 348 349
      if (rc)
	{
	  mu_error (_("cannot read input stream: %s"), mu_strerror (rc));
350
	  status = 1;
351 352
	  break;
	}
353 354 355
      if (n == 0)
	break;
      
356
      ws.ws_comment = "#";
357
      rc = mu_wordsplit (line, &ws, wsflags);
358 359
      if (rc)
	{
360 361
	  mu_error (_("cannot split line `%s': %s"), line,
		    mu_wordsplit_strerror (&ws));
362
	  status = 1;
363 364
	  break;
	}
365
      wsflags |= MU_WRDSF_REUSE;
366
      fun = func (tab, ws.ws_wordv[0]);
367
      if (fun)
368
	rc = fun (wh, ws.ws_wordc, ws.ws_wordv, &status);
369 370 371 372
      else
	rc = 0;
    }
  while (rc == 0);
373 374 375
  if (wsflags & MU_WRDSF_REUSE)
    mu_wordsplit_free (&ws);
  free (line);
376 377 378 379 380 381 382 383
  return status;
}


/* ************************** Actions ****************************** */

/* Display action */
static int
384 385 386
display (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  if (!wh->msg)
387
    mu_error (_("no alternate message to display"));
388 389 390 391 392
  else
    display_file (wh->msg);
  return 0;
}

393
/* Edit action */
394 395 396
static int
edit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
397
  char *name;
398
  
399
  mu_asprintf (&name, "%s-next", wh->editor);
400
  invoke (name, wh->editor, argc, argv, wh->file, NULL);
401 402
  free (name);
  
403 404 405
  return 0;
}

406
/* List action */
407 408 409 410
static int
list (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  if (!wh->file)
411
    mu_error (_("no draft file to display"));
412 413 414 415 416
  else
    display_file (wh->file);
  return 0;
}

417
/* Push action */
418 419 420
static int
push (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
421 422
  if (invoke ("sendproc", MHBINDIR "/send", argc, argv, "-push", wh->file) == 0)
    annotate (wh);
423 424 425
  return 0;
}

426
/* Quit action */
427 428 429 430
static int
quit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  *status = 0;
431 432 433 434 435 436 437

  if (wh->draftfile)
    {
      if (argc == 2 && strnulcmp (argv[1], "-delete") == 0)
	unlink (wh->draftfile);
      else
	{
438
	  mu_printf (_("draft left on \"%s\".\n"), wh->draftfile);
439 440
	  if (strcmp (wh->file, wh->draftfile))
	    rename (wh->file, wh->draftfile);
441 442 443
	}
    }

444 445 446
  return 1;
}

447
/* Refile action */
448 449 450
static int
refile (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
451
  invoke ("fileproc", MHBINDIR "/refile", argc, argv, "-file", wh->file);
452 453 454
  return 0;
}

455
/* Send action */
456
static int
457
call_send (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
458
{
459
  if (invoke ("sendproc", MHBINDIR "/send", argc, argv, wh->file, NULL) == 0)
460 461 462 463
    {
      annotate (wh);
      return 1;
    }
464 465 466
  return 0;
}

467
/* Whom action */
468 469 470 471
static int
whom (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  if (!wh->file)
472
    mu_error (_("no draft file to display"));
473
  else
474 475 476
    mh_whom (wh->file, (argc == 2
			&& (strcmp (argv[1], "-check") == 0
			    || strcmp (argv[1], "--check") == 0)));
477 478 479
  return 0;
}

480 481
/* Help table for whatnow */
static struct helpdata whatnow_helptab[] = {
482 483 484 485
  { "display [<>]",
    N_("List the message being distributed/replied-to on the terminal.") },
  { "edit [<e <>]",
    N_("Edit the message. If EDITOR is omitted use the one that was used on"
Jordi Mallach authored
486
       " the preceding round unless the profile entry \"LASTEDITOR-next\""
487 488 489 490 491 492
       " names an alternate editor.") },
  { "list [<>]",
    N_("List the draft on the terminal.") },
  { "push [<>]",
    N_("Send the message in the background.") },
  { "quit [-delete]",
493
    N_("Terminate the session. Preserve the draft, unless -delete flag is given.") },
494 495 496 497 498 499 500 501 502 503 504
  { "refile [<>] +",
    N_("Refile the draft into the given FOLDER.") },
  { "send [-watch] [<>]",
    N_("Send the message. The -watch flag causes the delivery process to be "
       "monitored. SWITCHES are passed to send program verbatim.") },
  { "whom [-check] [<>]",
    N_("List the addresses and verify that they are acceptable to the "
       "transport service.") },
  { NULL },
};

505
/* Help action for whatnow shell */
506
static int
507
whatnow_help (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
508
{
509 510
  return _help (whatnow_helptab, argv[0]);
}
511

512
/* Actions specific for the ``disposition'' shell */
513

514 515 516 517 518 519 520 521 522
/* Help table for ``disposition'' shell */
static struct helpdata disposition_helptab[] = {
  { "quit",           N_("Terminate the session. Preserve the draft.") },
  { "replace",        N_("Replace the draft with the newly created one") },
  { "use",            N_("Use this draft") },
  { "list",           N_("List the draft on the terminal.") },
  { "refile [<>] +",  N_("Refile the draft into the given FOLDER.") },
  { NULL },
};
523

524 525 526
/* Help action */
static int
disp_help (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
527
{
528
  return _help (disposition_helptab, argv[0]);
529 530
}

531
/* Use action */
532
static int
533
use (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
534
{
535 536
  *status = DISP_USE;
  return 1;
537 538
}

539 540 541
/* Replace action */
static int
replace (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
542
{
543 544
  *status = DISP_REPLACE;
  return 1;
545 546
}

547 548 549 550 551 552 553 554 555 556 557 558 559

/* *********************** Interfaces ****************************** */

/* Whatnow shell */
static struct action_tab whatnow_tab[] = {
  { "help", whatnow_help },
  { "?", whatnow_help },
  { "display", display },
  { "edit", edit },
  { "list", list },
  { "push", push },
  { "quit", quit },
  { "refile", refile },
560
  { "send", call_send },
561 562 563 564
  { "whom", whom },
  { NULL }
};

565 566
static void
set_default_editor (struct mh_whatnow_env *wh)
567 568
{
  if (!wh->editor)
569 570 571 572 573 574 575
    {
      char *p;
      wh->editor = mh_global_profile_get ("Editor",
					  (p = getenv ("VISUAL")) ?
					    p : (p = (getenv ("EDITOR"))) ?
					          p : "prompter");	  
    }
576 577 578 579 580 581
}

int
mh_whatnow (struct mh_whatnow_env *wh, int initial_edit)
{
  set_default_editor (wh);
582 583 584 585 586
  
  if (initial_edit)
    mh_spawnp (wh->editor, wh->file);

  if (!wh->prompt)
587
    wh->prompt = (char*) _("What now?");
588
  
589 590
  return _whatnow (wh, whatnow_tab);
}
591

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
int
mh_whatnowproc (struct mh_whatnow_env *wh, int initial_edit, const char *prog)
{
  int rc;
  pid_t pid;
  
  if (!prog)
    return mh_whatnow (wh, initial_edit);

  pid = fork ();
  if (pid == -1)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "fork", NULL, errno);
      return 1;
    }
  
  if (pid == 0)
    {
      struct mu_wordsplit ws;
      int i;
      
      if (mu_wordsplit (prog, &ws,
			MU_WRDSF_DEFFLAGS & ~MU_WRDSF_CESCAPES))
	{
	  mu_error (_("cannot parse command line (%s): %s"), prog,
		    mu_wordsplit_strerror (&ws));
	  _exit (127);
	}
      
      set_default_editor (wh);
      mh_whatnow_env_to_environ (wh);
      for (i = getmaxfd (); i > 2; i--)
	close (i);
      execvp (ws.ws_wordv[0], ws.ws_wordv);
      mu_diag_funcall (MU_DIAG_ERROR, "execvp", prog, errno);
      _exit (127);
    }

  /* Master */
  rc = 0;
  while (1)
    {
      int status;
      
      if (waitpid (pid, &status, 0) == (pid_t)-1)
	{
	  if (errno == EINTR)
	    continue;
	  mu_diag_funcall (MU_DIAG_ERROR, "waitpid", prog, errno);
	  rc = 1;
	}
      break;
    }
  return rc;
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/* Disposition shell */
static struct action_tab disp_tab[] = {
  { "help", disp_help },
  { "?", disp_help },
  { "quit", quit },
  { "replace", replace },
  { "use", use },
  { "list", list },
  { "refile",  refile },
  { NULL }
};
    
int
mh_disposition (const char *filename)
{
  struct mh_whatnow_env wh;
664
  int rc;
665
  memset (&wh, 0, sizeof (wh));
666
  wh.file = xstrdup (filename);
667
  wh.prompt = (char*) _("Disposition?");
668 669 670
  rc = _whatnow (&wh, disp_tab);
  free (wh.file);
  return rc;
671
}
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

/* Use draft shell */

/* Help table for ``use draft'' shell */
static struct helpdata usedraft_helptab[] = {
  { "no",             N_("Don't use the draft.") },
  { "yes",            N_("Use the draft.") },
  { "list",           N_("List the draft on the terminal.") },
  { NULL },
};

static int
usedraft_help (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  return _help (usedraft_helptab, argv[0]);
}

static int
yes (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  *status = 1;
  return 1;
}

static int
no (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
  *status = 0;
  return 1;
}

static struct action_tab usedraft_tab[] = {
  { "help", usedraft_help },
  { "?", usedraft_help },
  { "yes", yes },
  { "no", no },
  { "list", list },
  { NULL }
};

int
mh_usedraft (const char *filename)
{
  struct mh_whatnow_env wh;
  int rc;
  
  memset (&wh, 0, sizeof (wh));
719
  wh.file = xstrdup (filename);
720
  mu_asprintf (&wh.prompt, _("Use \"%s\"?"), filename);
721 722
  rc = _whatnow (&wh, usedraft_tab);
  free (wh.prompt);
723
  free (wh.file);
724 725 726
  return rc;
}