var.c 14.9 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 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 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 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 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 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2001, 2002, 2005 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., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

/* Functions for handling escape variables */

#include "mail.h"
#include <sys/stat.h>

static void
dump_headers (FILE *fp, compose_env_t *env)
{
  char buffer[512];
  stream_t stream = NULL;
  size_t off = 0, n;
  
  header_get_stream (env->header, &stream);
  while (stream_read (stream, buffer, sizeof buffer - 1, off, &n) == 0
	 && n != 0)
    {
      buffer[n] = 0;
      fprintf (fp, "%s", buffer);
      off += n;
    }
}

#define STATE_INIT 0
#define STATE_READ 1
#define STATE_BODY 2

static int
parse_headers (FILE *fp, compose_env_t *env)
{
  int status;
  header_t header;
  char *name = NULL;
  char *value = NULL;
  char *buf = NULL;
  int state = STATE_INIT;
  size_t n = 0;
  int errcnt = 0, line = 0;
  
  if ((status = header_create (&header, NULL, 0, NULL)) != 0)
    {
      util_error (_("Cannot create header: %s"), mu_strerror (status));
      return 1;
    }

  while (state != STATE_BODY
	 && errcnt == 0 && getline (&buf, &n, fp) > 0 && n > 0)
    {
      int len = strlen (buf);
      if (len > 0 && buf[len-1] == '\n')
	buf[len-1] = 0;

      line++;
      switch (state)
	{
	case STATE_INIT:
	  if (!buf[0] || isspace (buf[0]))
	    continue;
	  else
	    state = STATE_READ;
	  /*FALLTHRU*/
	  
	case STATE_READ:
	  if (buf[0] == 0)
	    state = STATE_BODY;
	  else if (isspace (buf[0]))
	    {
	      /* A continuation line */
	      if (name)
		{
		  char *p = NULL;
		  asprintf (&p, "%s\n%s", value, buf);
		  free (value);
		  value = p;
		}
	      else
		{
		  util_error (_("%d: not a header line"), line);
		  errcnt++;
		}
	    }
	  else
	    {
	      char *p;
	      
	      if (name)
		{
		  header_set_value (header, name, value[0] ? value : NULL, 0);
		  free (name);
		  free (value);
		  name = value = NULL;
		}
	      p = strchr (buf, ':');
	      if (p)
		{
		  *p++ = 0;
		  while (*p && isspace(*p))
		    p++;
		  value = strdup (p);
		  name = strdup (buf);
		}
	      else
		{
		  util_error (_("%d: not a header line"), line);
		  errcnt++;
		}
	    }
	  break;
	}
    }
  
  free (buf);
  if (name)
    {
      header_set_value (header, name, value, 0);
      free (name);
      free (value);
    }     

  if (errcnt)
    {
      char *p;
      
      header_destroy (&header, NULL);
      p = ml_readline (_("Edit again?"));
      if (mu_true_answer_p (p) == 1)
	return -1;
      else
	return 1;
    }

  header_destroy (&env->header, NULL);
  env->header = header;
  return 0;
}

static void
var_continue (void)
{
  fprintf (stdout, _("(continue)\n"));
}

static int 
var_check_args (int argc, char **argv)
{
  if (argc == 1)
    {
      char *escape = "~";
      util_getenv (&escape, "escape", Mail_env_string, 0);
      util_error (_("%c%s requires an argument"), escape[0], argv[0]);
      return 1;
    }
  return 0;
}

/* ~![shell-command] */
int
var_shell (int argc, char **argv, compose_env_t *env)
{
  int status;
  ofile = env->ofile;
  ++*argv;
  status = mail_execute (1, argc, argv);
  ofile = env->file;
  return status;
}

/* ~:[mail-command] */
/* ~-[mail-command] */
int
var_command (int argc, char **argv, compose_env_t *env)
{
  struct mail_command_entry *entry;
  int status;

  if (var_check_args (argc, argv))
    return 1;
  if (argv[1][0] == '#')
    return 0;
  entry = mail_find_command (argv[1]);
  if (!entry)
    {
      util_error (_("Unknown command: %s"), argv[1]);
      return 1;
    }
  if (entry->flags & (EF_FLOW | EF_SEND))
    {
      util_error (_("Command not allowed in an escape sequence\n"));
      return 1;
    }

  ofile = env->ofile;
  status = (*entry->func) (argc - 1, argv + 1);
  ofile = env->file;
  return status;
}

/* ~? */
int
var_help (int argc, char **argv, compose_env_t *env ARG_UNUSED)
{
  if (argc < 2)
    return mail_escape_help (NULL);
  else
    {
      int status = 0;

      while (--argc)
	status |= mail_escape_help (*++argv);

      return status;
    }
  return 1;
}

/* ~A */
/* ~a */
int
var_sign (int argc ARG_UNUSED, char **argv, compose_env_t *env ARG_UNUSED)
{
  char *p;

  if (util_getenv (&p, isupper (argv[0][0]) ? "Sign" : "sign",
		   Mail_env_string, 1) == 0)
    {
      if (isupper (argv[0][0]))
	{
	  char *name = util_fullpath (p);
	  FILE *fp = fopen (name, "r");
	  char *buf = NULL;
	  size_t n = 0;

	  if (!fp)
	    {
	      util_error (_("Cannot open %s: %s"), name, strerror (errno));
	      free (name);
	    }

	  fprintf (stdout, _("Reading %s\n"), name);
	  while (getline (&buf, &n, fp) > 0)
	    fprintf (ofile, "%s", buf);

	  fclose (fp);
	  free (buf);
	  free (name);
	}
      else
	fprintf (ofile, "%s", p);
      var_continue ();
    }
  return 0;
}

/* ~b[bcc-list] */
int
var_bcc (int argc, char **argv, compose_env_t *env)
{
  while (--argc)
    compose_header_set (env, MU_HEADER_BCC, *++argv, COMPOSE_SINGLE_LINE);
  return 0;
}

/* ~c[cc-list] */
int
var_cc (int argc, char **argv, compose_env_t *env)
{
  while (--argc)
    compose_header_set (env, MU_HEADER_CC, *++argv, COMPOSE_SINGLE_LINE);
  return 0;
}

/* ~d */
int
var_deadletter (int argc ARG_UNUSED, char **argv ARG_UNUSED,
                compose_env_t *env ARG_UNUSED)
{
  FILE *dead = fopen (getenv ("DEAD"), "r");
  int c;

  while ((c = fgetc (dead)) != EOF)
    fputc (c, ofile);
  fclose (dead);
  return 0;
}

static int
run_editor (char *ed, char *arg)
{
  char *argv[3];

  argv[0] = ed;
  argv[1] = arg;
  argv[2] = NULL;
  return mail_execute (1, 2, argv);
}

static int
var_run_editor (char *ed, int argc, char **argv, compose_env_t *env)
{
  if (!util_getenv (NULL, "editheaders", Mail_env_boolean, 0))
    {
      char *filename;
      int fd = mu_tempfile (NULL, &filename);
      FILE *fp = fdopen (fd, "w+");
      char buffer[512];
      int rc;
      
      dump_headers (fp, env);

      rewind (env->file);
      while (fgets (buffer, sizeof (buffer), env->file))
	fputs (buffer, fp);

      fclose (env->file);
      
      do
	{
	  fclose (fp);
	  run_editor (ed, filename);
	  fp = fopen (filename, "r");
	}
      while ((rc = parse_headers (fp, env)) < 0);

      if (rc == 0)
	{
	  env->file = fopen (env->filename, "w");
	  while (fgets (buffer, sizeof (buffer), fp))
	    fputs (buffer, env->file);

	  fclose (env->file);
	}
      fclose (fp);
      unlink (filename);
      free (filename);
    }
  else
    {
      fclose (env->file);
      ofile = env->ofile;
      run_editor (ed, env->filename);
    }

  env->file = fopen (env->filename, "a+");
  ofile = env->file;
      
  var_continue ();
  return 0;
}

/* ~e */
int
var_editor (int argc, char **argv, compose_env_t *env)
{
  return var_run_editor (getenv ("EDITOR"), argc, argv, env);
}

/* ~v */
int
var_visual (int argc, char **argv, compose_env_t *env)
{
  return var_run_editor (getenv ("VISUAL"), argc, argv, env);
}

/* ~f[mesg-list] */
/* ~F[mesg-list] */
int
var_print (int argc, char **argv, compose_env_t *env ARG_UNUSED)
{
  return mail_print (argc, argv);
}

void
reread_header (compose_env_t *env, char *hdr, char *prompt)
{
  char *p;
  p = strdup (compose_header_get (env, hdr, ""));
  ml_reread (prompt, &p);
  compose_header_set (env, hdr, p, COMPOSE_REPLACE);
  free (p);
}

/* ~h */
int
var_headers (int argc, char **argv, compose_env_t *env)
{
  reread_header (env, MU_HEADER_TO, "To: ");
  reread_header (env, MU_HEADER_CC, "Cc: ");
  reread_header (env, MU_HEADER_BCC, "Bcc: ");
  reread_header (env, MU_HEADER_SUBJECT, "Subject: ");
  var_continue ();
  return 0;
}

/* ~i[var-name] */
int
var_insert (int argc, char **argv, compose_env_t *send_env ARG_UNUSED)
{
  struct mail_env_entry *env;

  if (var_check_args (argc, argv))
    return 1;
  env = util_find_env (argv[1], 0);
  if (env)
    switch (env->type)
      {
      case Mail_env_string:
	fprintf (ofile, "%s", env->value.string);
	break;

      case Mail_env_number:
	fprintf (ofile, "%d", env->value.number);
	break;

      case Mail_env_boolean:
	fprintf (ofile, "%s", env->set ? "yes" : "no");
	break;

      default:
	break;
      }
  return 0;
}

/* ~m[mesg-list] */
/* ~M[mesg-list] */

int
quote0 (msgset_t *mspec, message_t mesg, void *data)
{
  header_t hdr;
  body_t body;
  stream_t stream;
  char buffer[512];
  off_t off = 0;
  size_t n = 0;
  char *prefix = "\t";
  
  fprintf (stdout, _("Interpolating: %d\n"), mspec->msg_part[0]);

  util_getenv (&prefix, "indentprefix", Mail_env_string, 0);

  if (*(int*)data)
    {
      size_t i, num = 0;
      char buf[512];

      message_get_header (mesg, &hdr);
      header_get_field_count (hdr, &num);

      for (i = 1; i <= num; i++)
	{
	  header_get_field_name (hdr, i, buf, sizeof buf, NULL);
	  if (mail_header_is_visible (buf))
	    {
	      char *value;
	      
	      fprintf (ofile, "%s%s: ", prefix, buf);
	      if (header_aget_value (hdr, buf, &value) == 0)
		{
		  int i;
		  char *p, *s;

		  for (i = 0, p = strtok_r (value, "\n", &s); p;
		       p = strtok_r (NULL, "\n", &s), i++)
		    {
		      if (i)
			fprintf (ofile, "%s", prefix);
		      fprintf (ofile, "%s\n", p);
		    }
		  free (value);
		}
	    }
	}
      fprintf (ofile, "%s\n", prefix);
      message_get_body (mesg, &body);
      body_get_stream (body, &stream);
    }
  else
    message_get_stream (mesg, &stream);

  while (stream_readline (stream, buffer, sizeof buffer - 1, off, &n) == 0
	 && n != 0)
    {
      buffer[n] = '\0';
      fprintf (ofile, "%s%s", prefix, buffer);
      off += n;
    }
  return 0;
}

int
var_quote (int argc, char **argv, compose_env_t *env)
{
  int lower = islower (argv[0][0]);
  util_foreach_msg (argc, argv, MSG_NODELETED|MSG_SILENT, quote0, &lower);
  var_continue ();
  return 0;
}

/* ~p */
int
var_type_input (int argc, char **argv, compose_env_t *env)
{
  char buffer[512];

  fprintf (env->ofile, _("Message contains:\n"));

  dump_headers (env->ofile, env);

  rewind (env->file);
  while (fgets (buffer, sizeof (buffer), env->file))
    fputs (buffer, env->ofile);

  var_continue ();

  return 0;
}

/* ~r[filename] */
int
var_read (int argc, char **argv, compose_env_t *env ARG_UNUSED)
{
  char *filename;
  FILE *inf;
  size_t size, lines;
  char buf[512];

  if (var_check_args (argc, argv))
    return 1;
  filename = util_fullpath (argv[1]);
  inf = fopen (filename, "r");
  if (!inf)
    {
      util_error (_("Cannot open %s: %s"), filename, strerror (errno));
      free (filename);
      return 1;
    }

  size = lines = 0;
  while (fgets (buf, sizeof (buf), inf))
    {
      lines++;
      size += strlen (buf);
      fputs (buf, ofile);
    }
  fclose (inf);
  fprintf (stdout, "\"%s\" %lu/%lu\n", filename,
	   (unsigned long) lines, (unsigned long) size);
  free (filename);
  return 0;
}

/* ~s[string] */
int
var_subj (int argc, char **argv, compose_env_t *env)
{
  if (var_check_args (argc, argv))
    return 1;
  compose_header_set (env, MU_HEADER_SUBJECT, argv[1], COMPOSE_REPLACE);
  return 0;
}

/* ~t[name-list] */
int
var_to (int argc, char **argv, compose_env_t *env)
{
  while (--argc)
    compose_header_set (env, MU_HEADER_TO, *++argv, COMPOSE_SINGLE_LINE);
  return 0;
}

/* ~w[filename] */
int
var_write (int argc, char **argv, compose_env_t *env)
{
  char *filename;
  FILE *fp;
  size_t size, lines;
  char buf[512];

  if (var_check_args (argc, argv))
    return 1;

  filename = util_fullpath (argv[1]);
  fp = fopen (filename, "w");	/*FIXME: check for the existence first */

  if (!fp)
    {
      util_error (_("Cannot open %s: %s"), filename, strerror (errno));
      free (filename);
      return 1;
    }

  rewind (env->file);
  size = lines = 0;
  while (fgets (buf, sizeof (buf), env->file))
    {
      lines++;
      size += strlen (buf);
      fputs (buf, fp);
    }
  fclose (fp);
  fprintf (stdout, "\"%s\" %lu/%lu\n", filename,
	   (unsigned long) lines, (unsigned long) size);
  free (filename);
  return 0;
}

/* ~|[shell-command] */
int
var_pipe (int argc, char **argv, compose_env_t *env)
{
  int p[2];
  pid_t pid;
  int fd;

  if (argc == 1)
    {
      /* TRANSLATORS: 'pipe' is a command name. Do not translate it! */
      util_error (_("pipe: no command specified"));
      return 1;
    }

  if (pipe (p))
    {
      util_error ("pipe: %s", strerror (errno));
      return 1;
    }

  fd = mu_tempfile (NULL, NULL);
  if (fd == -1)
    return 1;

  if ((pid = fork ()) < 0)
    {
      close (p[0]);
      close (p[1]);
      close (fd);
      util_error ("fork: %s", strerror (errno));
      return 1;
    }
  else if (pid == 0)
    {
      /* Child */
      int i;
      char **xargv;

      /* Attache the pipes */
      close (0);
      dup (p[0]);
      close (p[0]);
      close (p[1]);

      close (1);
      dup (fd);
      close (fd);

      /* Execute the process */
      xargv = xcalloc (argc, sizeof (xargv[0]));
      for (i = 0; i < argc - 1; i++)
	xargv[i] = argv[i + 1];
      xargv[i] = NULL;
      execvp (xargv[0], xargv);
      util_error (_("Cannot exec process `%s': %s"), xargv[0], strerror (errno));
      exit (1);
    }
  else
    {
      FILE *fp;
      char *buf = NULL;
      size_t n;
      size_t lines, size;
      int rc = 1;
      int status;

      close (p[0]);

      /* Parent */
      fp = fdopen (p[1], "w");

      fclose (env->file);
      env->file = fopen (env->filename, "r");

      lines = size = 0;
      while (getline (&buf, &n, env->file) > 0)
	{
	  lines++;
	  size += n;
	  fputs (buf, fp);
	}
      fclose (env->file);
      fclose (fp);		/* Closes p[1] */

      waitpid (pid, &status, 0);
      if (!WIFEXITED (status))
	{
	  util_error (_("Child terminated abnormally: %d"), WEXITSTATUS (status));
	}
      else
	{
	  struct stat st;
	  if (fstat (fd, &st))
	    {
	      util_error (_("Cannot stat output file: %s"), strerror (errno));
	    }
	  else if (st.st_size > 0)
	    rc = 0;
	}

      fprintf (stdout, "\"|%s\" in: %lu/%lu ", argv[1],
	       (unsigned long) lines, (unsigned long) size);
      if (rc)
	{
	  fprintf (stdout, _("no lines out\n"));
	}
      else
	{
	  /* Ok, replace the old tempfile */
	  fp = fdopen (fd, "r");
	  rewind (fp);

	  env->file = fopen (env->filename, "w+");

	  lines = size = 0;
	  while (getline (&buf, &n, fp) > 0)
	    {
	      lines++;
	      size += n;
	      fputs (buf, env->file);
	    }
	  fclose (env->file);

	  fprintf (stdout, "out: %lu/%lu\n",
		   (unsigned long) lines, (unsigned long) size);
	}

      /* Clean up the things */
      if (buf)
	free (buf);

      env->file = fopen (env->filename, "a+");
      ofile = env->file;
    }

  close (fd);

  return 0;
}