Blame view

mail/send.c 14 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3

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.

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
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

23 24
#include "mail.h"

25
static int isfilename __P ((const char *));
26 27
static void msg_to_pipe __P ((const char *cmd, message_t msg));

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

/* Additional message headers */
struct add_header
{
  int mode;
  char *name;
  char *value;
};

static list_t add_header_list;

static int
seed_headers (void *item, void *data)
{
  struct add_header *hp = item;
  compose_env_t *env = data;

  compose_header_set (env, hp->name, hp->value, hp->mode);
  return 0;
}

static int
list_headers (void *item, void *data)
{
  struct add_header *hp = item;
  char *name = data;

  if (!name || strcmp (name, hp->name) == 0)
    {
      printf ("%s: %s\n", hp->name, hp->value);
    }
  return 0;
}
    
static void
add_header (char *name, char *value, int mode)
{
  struct add_header *hp;
  
  if (!add_header_list)
    {
      int rc = list_create (&add_header_list);
      if (rc)
	{
	  util_error (_("Cannot create header list: %s"), mu_strerror (rc));
	  exit (1);
	}
    }

  hp = xmalloc (sizeof (*hp));
  hp->mode = mode;
  hp->name = name;
  hp->value = value;
  list_append (add_header_list, hp);
}

void
send_append_header (char *text)
{
  char *p;
  size_t len;
  char *name;

  p = strchr (text, ':');
  if (!p)
    {
      util_error (_("Invalid header: %s"), text);
      return;
    }
  len = p - text;
  name = xmalloc (len + 1);
  memcpy (name, text, len);
  name[len] = 0;
  for (p++; *p && isspace (*p); p++)
    ;

  add_header (name, strdup (p), COMPOSE_APPEND);
}

void
send_append_header2 (char *name, char *value, int mode)
{
  add_header (strdup (name), strdup (value), mode);
}

int
mail_sendheader (int argc, char **argv)
{
  if (argc == 1)
    list_do (add_header_list, list_headers, NULL);
  else if (argc == 2)
    {
      if (strchr (argv[1], ':'))
	send_append_header (argv[1]);
      else
	list_do (add_header_list, list_headers, argv[1]);
    }
  else
    {
      size_t len = strlen (argv[1]);
      if (len > 0 && argv[1][len - 1] == ':') 
	argv[1][len - 1] = 0;
      add_header (strdup (argv[1]), strdup (argv[2]), COMPOSE_APPEND);
    }
  return 0;
}


/* Send-related commands */

138 139 140 141 142
static void
read_cc_bcc (compose_env_t *env)
{
  if (util_getenv (NULL, "askcc", Mail_env_boolean, 0) == 0)
    compose_header_set (env, MU_HEADER_CC,
143
			ml_readline_with_intr ("Cc: "), COMPOSE_REPLACE);
144 145
  if (util_getenv (NULL, "askbcc", Mail_env_boolean, 0) == 0)
    compose_header_set (env, MU_HEADER_BCC,
146
			ml_readline_with_intr ("Bcc: "), COMPOSE_REPLACE);
147 148
}

149 150
/*
 * m[ail] address...
151
 if address is starting with
152 153 154 155 156 157
 
    '/'        it is considered a file and the message is saveed to a file;
    '.'        it is considered to be a relative path;
    '|'        it is considered to be a pipe, and the message is written to
               there;
	       
158
 example:
159 160 161 162
 
   mail joe '| cat >/tmp/save'

 mail will be sent to joe and the message saved in /tmp/save. */
163 164 165 166

int
mail_send (int argc, char **argv)
{
167
  compose_env_t env;
168
  int status;
169
  int save_to = isupper (argv[0][0]);
170
  compose_init (&env);
171

172
  if (argc < 2)
173
    compose_header_set (&env, MU_HEADER_TO, ml_readline_with_intr ("To: "),
174
			COMPOSE_REPLACE);
175 176 177 178
  else
    {
      while (--argc)
	{
179 180
	  char *p = *++argv;
	  if (isfilename (p))
181 182
	    {
	      env.outfiles = realloc (env.outfiles,
183 184
				      (env.nfiles + 1) *
				      sizeof (*(env.outfiles)));
185 186 187 188 189 190 191
	      if (env.outfiles)
		{
		  env.outfiles[env.nfiles] = p;
		  env.nfiles++;
		}
	    }
	  else
192
	    compose_header_set (&env, MU_HEADER_TO, p, COMPOSE_SINGLE_LINE);
193 194 195
	}
    }

196 197
  if (util_getenv (NULL, "mailx", Mail_env_boolean, 0))
    read_cc_bcc (&env);
198

Sergey Poznyakoff authored
199
  if (util_getenv (NULL, "asksub", Mail_env_boolean, 0) == 0)
200
    compose_header_set (&env, MU_HEADER_SUBJECT,
201
			ml_readline_with_intr ("Subject: "), COMPOSE_REPLACE);
202

203
  status = mail_send0 (&env, save_to);
204
  compose_destroy (&env);
205 206 207
  return status;
}

208
void
209
compose_init (compose_env_t * env)
210 211
{
  memset (env, 0, sizeof (*env));
212
  list_do (add_header_list, seed_headers, env);
213 214 215
}

int
216
compose_header_set (compose_env_t * env, char *name, char *value, int mode)
217 218 219
{
  int status;
  char *old_value;
220

221 222 223
  if (!env->header
      && (status = header_create (&env->header, NULL, 0, NULL)) != 0)
    {
224
      util_error (_("Cannot create header: %s"), mu_strerror (status));
225 226 227 228 229
      return status;
    }

  switch (mode)
    {
230
    case COMPOSE_REPLACE:
231 232 233
    case COMPOSE_APPEND:
      status = header_set_value (env->header, name, value, mode);
      break;
234

235
    case COMPOSE_SINGLE_LINE:
236 237
      if (!value || value[0] == 0)
	return EINVAL;
238

239 240 241
      if (header_aget_value (env->header, name, &old_value) == 0
	  && old_value[0])
	{
242 243 244 245
	  status = util_merge_addresses (&old_value, value);
	  if (status == 0)
	    status = header_set_value (env->header, name, old_value, 1);
	  free (old_value);
246 247 248 249 250 251 252 253 254
	}
      else
	status = header_set_value (env->header, name, value, 1);
    }

  return status;
}

char *
255
compose_header_get (compose_env_t * env, char *name, char *defval)
256 257
{
  char *p;
258

259 260 261
  if (header_aget_value (env->header, name, &p))
    p = defval;
  return p;
262
}
263

264
void
265
compose_destroy (compose_env_t * env)
266
{
267
  header_destroy (&env->header, NULL);
268 269 270 271 272 273 274
  if (env->outfiles)
    {
      int i;
      for (i = 0; i < env->nfiles; i++)
	free (env->outfiles[i]);
      free (env->outfiles);
    }
275 276
}

277 278 279 280 281
/* mail_send0(): shared between mail_send() and mail_reply();

   If the variable "record" is set, the outgoing message is
   saved after being sent. If "save_to" argument is non-zero,
   the name of the save file is derived from "to" argument. Otherwise,
282 283 284 285 286 287 288 289
   it is taken from the value of "record" variable.

   sendmail

   contains a command, possibly with options, that mailx invokes to send
   mail. You must manually set the default for this environment variable
   by editing ROOTDIR/etc/mailx.rc to specify the mail agent of your
   choice. The default is sendmail, but it can be any command that takes
290
   addresses on the command line and message contents on standard input. */
291

292
int
293
mail_send0 (compose_env_t * env, int save_to)
294
{
295
  int done = 0;
296 297 298
  int fd;
  char *filename;
  FILE *file;
299
  char *savefile = NULL;
300
  int int_cnt;
Sergey Poznyakoff authored
301
  char *escape;
302

Sergey Poznyakoff authored
303
  fd = mu_tempfile (NULL, &filename);
304 305 306

  if (fd == -1)
    {
307
      util_error (_("Cannot open temporary file"));
308 309 310
      return 1;
    }

311 312 313 314 315
  /* Prepare environment */
  env = env;
  env->filename = filename;
  env->file = fdopen (fd, "w+");
  env->ofile = ofile;
316 317

  ml_clear_interrupt ();
318 319
  int_cnt = 0;
  while (!done)
320
    {
321
      char *buf;
322
      buf = ml_readline (" \b");
323 324

      if (ml_got_interrupt ())
325
	{
Sergey Poznyakoff authored
326
	  if (util_getenv (NULL, "ignore", Mail_env_boolean, 0) == 0)
327 328 329 330 331
	    {
	      fprintf (stdout, "@\n");
	    }
	  else
	    {
332 333
	      if (buf)
		free (buf);
334 335
	      if (++int_cnt == 2)
		break;
336
	      util_error (_("\n(Interrupt -- one more to kill letter)"));
337
	    }
338 339 340 341
	  continue;
	}

      if (!buf)
342
	{
343 344
	  if (interactive 
	      && util_getenv (NULL, "ignoreeof", Mail_env_boolean, 0) == 0)
345
	    {
Sergey Poznyakoff authored
346
	      util_error (util_getenv (NULL, "dot", Mail_env_boolean, 0) == 0 ?
347 348
			  _("Use \".\" to terminate letter.") :
			  _("Use \"~.\" to terminate letter."));
349 350 351 352 353
	      continue;
	    }
	  else
	    break;
	}
354

355
      int_cnt = 0;
356

357 358
      if (strcmp (buf, ".") == 0
	  && util_getenv (NULL, "dot", Mail_env_boolean, 0) == 0)
359
	done = 1;
Sergey Poznyakoff authored
360
      else if (util_getenv (&escape, "escape", Mail_env_string, 0) == 0
361
	       && buf[0] == escape[0])
362
	{
363
	  if (buf[1] == buf[0])
364
	    fprintf (env->file, "%s\n", buf + 1);
365 366
	  else if (buf[1] == '.')
	    done = 1;
367 368 369 370 371
	  else if (buf[1] == 'x')
	    {
	      int_cnt = 2;
	      done = 1;
	    }
372
	  else
373
	    {
374 375 376
	      int argc;
	      char **argv;
	      int status;
377

378
	      ofile = env->file;
379

380
	      if (argcv_get (buf + 1, "", NULL, &argc, &argv) == 0)
381 382
		{
		  struct mail_command_entry entry;
383

384 385 386 387 388 389 390 391 392
		  if (argc > 0)
		    {
		      entry = util_find_entry (mail_escape_table, argv[0]);

		      if (entry.escfunc)
			status = (*entry.escfunc) (argc, argv, env);
		      else
			util_error (_("Unknown escape %s"), argv[0]);
		    }
393
		  else
394
		    util_error (_("Unfinished escape"));
395
		}
396
	      else
397
		{
398
		  util_error (_("Cannot parse escape sequence"));
399 400
		}
	      argcv_free (argc, argv);
401

402
	      ofile = env->ofile;
403 404 405
	    }
	}
      else
406
	fprintf (env->file, "%s\n", buf);
407
      fflush (env->file);
408
      free (buf);
409 410
    }

411
  /* If interrupted dump the file to dead.letter.  */
412 413
  if (int_cnt)
    {
Sergey Poznyakoff authored
414
      if (util_getenv (NULL, "save", Mail_env_boolean, 0) == 0)
415
	{
416
	  FILE *fp = fopen (getenv ("DEAD"),
Sergey Poznyakoff authored
417
			    util_getenv (NULL, "appenddeadletter",
418
					 Mail_env_boolean, 0) == 0 ?
419
			    "a" : "w");
420 421 422

	  if (!fp)
	    {
423
	      util_error (_("Cannot open file %s: %s"), getenv ("DEAD"),
424
			  strerror (errno));
425 426 427 428
	    }
	  else
	    {
	      char *buf = NULL;
429
	      size_t n;
430 431
	      rewind (env->file);
	      while (getline (&buf, &n, env->file) > 0)
432 433 434
		fputs (buf, fp);
	      fclose (fp);
	      free (buf);
435 436
	    }
	}
437

438
      fclose (env->file);
439 440 441
      remove (filename);
      free (filename);
      return 1;
442 443
    }

444
  fclose (env->file);		/* FIXME: freopen would be better */
445

446 447 448 449 450
  /* In mailx compatibility mode, ask for Cc and Bcc after editing
     the body of the message */
  if (util_getenv (NULL, "mailx", Mail_env_boolean, 0) == 0)
    read_cc_bcc (env);

451
  /* Prepare the header */
452 453
  if (util_getenv (NULL, "xmailer", Mail_env_boolean, 0) == 0)
    header_set_value (env->header, "X-Mailer", argp_program_version, 1);
454 455

  if (util_header_expand (&env->header) == 0)
456
    {
457 458 459 460 461 462 463 464 465 466 467
      file = fopen (filename, "r");
      if (file != NULL)
	{
	  mailer_t mailer;
	  message_t msg = NULL;

	  message_create (&msg, NULL);

	  message_set_header (msg, env->header, NULL);

	  /* Fill the body.  */
468
	  {
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	    body_t body = NULL;
	    stream_t stream = NULL;
	    off_t offset = 0;
	    char *buf = NULL;
	    size_t n = 0;
	    message_get_body (msg, &body);
	    body_get_stream (body, &stream);
	    while (getline (&buf, &n, file) >= 0)
	      {
		size_t len = strlen (buf);
		stream_write (stream, buf, len, offset, &n);
		offset += len;
	      }
	    
	    if (offset == 0)
484
	      util_error (_("Null message body; hope that's ok\n"));
485 486
	    if (buf)
	      free (buf);
487
	  }
488

489
	  fclose (file);
490

491 492
	  /* Save outgoing message */
	  if (save_to)
493
	    {
494 495 496 497 498 499 500 501 502 503 504 505
	      char *tmp = compose_header_get (env, MU_HEADER_TO, NULL);
	      address_t addr = NULL;
	      
	      address_create (&addr, tmp);
	      address_aget_email (addr, 1, &savefile);
	      address_destroy (&addr);
	      if (savefile)
		{
		  char *p = strchr (savefile, '@');
		  if (p)
		    *p = 0;
		}
506
	    }
507 508 509
	  util_save_outgoing (msg, savefile);
	  if (savefile)
	    free (savefile);
510

511 512
	  /* Check if we need to save the message to files or pipes.  */
	  if (env->outfiles)
513
	    {
514 515
	      int i;
	      for (i = 0; i < env->nfiles; i++)
516
		{
517 518 519 520 521
		  /* Pipe to a cmd.  */
		  if (env->outfiles[i][0] == '|')
		    msg_to_pipe (&(env->outfiles[i][1]), msg);
		  /* Save to a file.  */
		  else
522
		    {
523 524 525
		      int status;
		      mailbox_t mbx = NULL;
		      status = mailbox_create_default (&mbx, env->outfiles[i]);
526 527
		      if (status == 0)
			{
528 529 530 531
			  status = mailbox_open (mbx, MU_STREAM_WRITE
						 | MU_STREAM_CREAT);
			  if (status == 0)
			    {
532 533
			      status = mailbox_append_message (mbx, msg);
			      if (status)
534
				util_error (_("Cannot append message: %s"),
535
					    mu_strerror (status));
536 537 538
			      mailbox_close (mbx);
			    }
			  mailbox_destroy (&mbx);
539
			}
540
		      if (status)
541
			util_error (_("Cannot create mailbox %s"), env->outfiles[i]);
542 543 544
		    }
		}
	    }
545

546 547 548 549
	  /* Do we need to Send the message on the wire?  */
	  if (compose_header_get (env, MU_HEADER_TO, NULL)
	      || compose_header_get (env, MU_HEADER_CC, NULL)
	      || compose_header_get (env, MU_HEADER_BCC, NULL))
550
	    {
551 552
	      char *sendmail;
	      if (util_getenv (&sendmail, "sendmail", Mail_env_string, 0) == 0)
553
		{
554
		  int status = mailer_create (&mailer, sendmail);
555 556
		  if (status == 0)
		    {
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
		      if (util_getenv (NULL, "verbose", Mail_env_boolean, 0)
			  == 0)
			{
			  mu_debug_t debug = NULL;
			  mailer_get_debug (mailer, &debug);
			  mu_debug_set_level (debug,
					      MU_DEBUG_TRACE | MU_DEBUG_PROT);
			}
		      status = mailer_open (mailer, MU_STREAM_RDWR);
		      if (status == 0)
			{
			  mailer_send_message (mailer, msg, NULL, NULL);
			  mailer_close (mailer);
			}
		      mailer_destroy (&mailer);
572
		    }
573 574
		  if (status != 0)
		    msg_to_pipe (sendmail, msg);
575
		}
576
	      else
577
		util_error (_("Variable sendmail not set: no mailer"));
578
	    }
579 580 581 582
	  message_destroy (&msg, NULL);
	  remove (filename);
	  free (filename);
	  return 0;
583
	}
584
    }
585 586

  remove (filename);
587
  free (filename);
588 589
  return 1;
}
590 591 592 593 594 595 596 597 598 599 600

/* Starting with '|' '/' or not consider addresses and we cheat
   by adding '.' in the mix for none absolute path.  */
static int
isfilename (const char *p)
{
  if (p)
    if (*p == '/' || *p == '.' || *p == '|')
      return 1;
  return 0;
}
601 602

/* FIXME: Should probably be in util.c.  */
603
/* Call popen(cmd) and write the message to it.  */
604 605 606 607 608 609 610 611 612
static void
msg_to_pipe (const char *cmd, message_t msg)
{
  FILE *fp = popen (cmd, "w");
  if (fp)
    {
      stream_t stream = NULL;
      char buffer[512];
      off_t off = 0;
613
      size_t n = 0;
614
      message_get_stream (msg, &stream);
615
      while (stream_read (stream, buffer, sizeof buffer - 1, off, &n) == 0
616 617 618 619 620 621 622 623 624
	     && n != 0)
	{
	  buffer[n] = '\0';
	  fprintf (fp, "%s", buffer);
	  off += n;
	}
      fclose (fp);
    }
  else
625
    util_error (_("Piping %s failed"), cmd);
626
}