send.c 9.94 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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2001 Free Software Foundation, Inc.

   This program 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.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */


/* FIXME: Those headers should be in "mail.h".  */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#include "mail.h"

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

/*
 * m[ail] address...
 if address is starting with
 '/' it is consider a file and the message is save to a file.
 '.' by extension dot is consider relative path.
 '|' a pipe the message is writent to the pipe.
 example:
 mail joe '| cat >/tmp/save'
 mail will be send to joe and the message save in /tmp/save.
 */

int
mail_send (int argc, char **argv)
{
  struct send_environ env;
  int status;

  env.to = env.cc = env.bcc = env.subj = NULL;
  env.outfiles = NULL; env.nfiles = 0;

  if (argc < 2)
    env.to = readline ("To: ");
  else
    {
      while (--argc)
	{
	  char *p = alias_expand (*++argv);
	  if (isfilename(p))
	    {
	      env.outfiles = realloc (env.outfiles,
				      (env.nfiles+1)*sizeof (*(env.outfiles)));
	      if (env.outfiles)
		{
		  env.outfiles[env.nfiles] = p;
		  env.nfiles++;
		}
	    }
	  else
	    {
	      if (env.to)
		util_strcat(&env.to, ",");
	      util_strcat(&env.to, p);
	      free (p);
	    }
	}
    }

  if ((util_find_env ("askcc"))->set)
    env.cc = readline ("Cc: ");
  if ((util_find_env ("askbcc"))->set)
    env.bcc = readline ("Bcc: ");

  if ((util_find_env ("asksub"))->set)
    env.subj = readline ("Subject: ");
  else
    env.subj = (util_find_env ("subject"))->value;

  status = mail_send0 (&env, isupper(argv[0][0]));
  free_env_headers (&env);
  return status;
}


void
free_env_headers (struct send_environ *env)
{
  if (env->to)
    free (env->to);
  if (env->cc)
    free (env->cc);
  if (env->bcc)
    free (env->bcc);
  if (env->subj)
    free (env->subj);
  if (env->outfiles)
    {
      int i;
      for (i = 0; i < env->nfiles; i++)
	free (env->outfiles[i]);
      free (env->outfiles);
    }
}

/* 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,
   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
   addresses on the command line and message contents on standard input.
*/

int
mail_send0 (struct send_environ *env, int save_to)
{
  int done = 0;
  int fd;
  char *filename;
  FILE *file;
  char *savefile = NULL;
  int int_cnt;

  fd = util_tempfile (&filename);

  if (fd == -1)
    {
      util_error ("Can not open temporary file");
      return 1;
    }

  /* Prepare environment */
  env = env;
  env->filename = filename;
  env->file = fdopen (fd, "w+");
  env->ofile = ofile;

  ml_clear_interrupt ();
  int_cnt = 0;
  while (!done)
    {
      char *buf;
      buf = readline (NULL);

      if (ml_got_interrupt ())
	{
	  if (util_find_env ("ignore")->set)
	    {
	      fprintf (stdout, "@\n");
	    }
	  else
	    {
	      if (buf)
		free (buf);
	      if (++int_cnt == 2)
		break;
	      util_error ("(Interrupt -- one more to kill letter)");
	    }
	  continue;
	}

      if (!buf)
	{
	  if (util_find_env ("ignoreeof")->set)
	    {
	      util_error (util_find_env ("dot")->set ?
			  "Use \".\" to terminate letter." :
			  "Use \"~.\" to terminate letter.");
	      continue;
	    }
	  else
	    break;
	}

      int_cnt = 0;

      if (buf[0] == '.' && util_find_env ("dot")->set)
	done = 1;
      else if (buf[0] == (util_find_env ("escape"))->value[0])
	{
	  if (buf[1] == buf[0])
	    fprintf (env->file, "%s\n", buf+1);
	  else if (buf[1] == '.')
	    done = 1;
	  else
	    {
	      int argc;
	      char **argv;
	      int status;

	      ofile = env->file;

	      if (argcv_get (buf+1, "", &argc, &argv) == 0)
		{
		  struct mail_command_entry entry;
		  entry = util_find_entry (mail_escape_table, argv[0]);

		  if (entry.func)
		    status = (*entry.func)(argc, argv, env);
		  else
		    util_error ("Unknown escape %s", argv[0]);
		}
	      else
		{
		  util_error ("can't parse escape sequence");
		}
	      argcv_free (argc, argv);

	      ofile =  env->ofile;
	    }
	}
      else
	fprintf (env->file, "%s\n", buf);
      fflush (env->file);
      free (buf);
    }

  /* If interrupted dumpt the file to dead.letter.  */
  if (int_cnt)
    {
      if (util_find_env ("save")->set)
	{
	  FILE *fp = fopen (getenv ("DEAD"),
			    util_find_env ("appenddeadletter")->set ?
			    "a" : "w");

	  if (!fp)
	    {
	      util_error ("can't open file %s: %s", getenv ("DEAD"),
			 strerror (errno));
	    }
	  else
	    {
	      char *buf = NULL;
	      int n;
	      rewind (env->file);
	      while (getline (&buf, &n, env->file) > 0)
		fputs (buf, fp);
	      fclose (fp);
	      free (buf);
	    }
	}

      fclose (env->file);
      remove (filename);
      free (filename);
      return 1;
    }

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

  file = fopen (filename, "r");
  if (file != NULL)
    {
      mailer_t mailer;
      message_t msg = NULL;
      int status;
      message_create (&msg, NULL);

      /* Fill the header.  */
      {
	header_t header = NULL;
	message_get_header (msg, &header);
	if (env->to && *env->to != '\0')
	  header_set_value (header, MU_HEADER_TO, strdup (env->to), 0);
	if (env->cc && *env->cc != '\0')
	  header_set_value (header, MU_HEADER_CC, strdup (env->cc), 0);
	if (env->bcc && *env->bcc != '\0')
	  header_set_value (header, MU_HEADER_BCC , strdup (env->bcc), 0);
	if (env->subj && *env->subj != '\0')
	  header_set_value (header, MU_HEADER_SUBJECT, strdup (env->subj), 1);
	header_set_value (header, "X-Mailer", strdup(argp_program_version), 1);
      }

      /* Fill the body.  */
      {
	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)
	  util_error("Null message body; hope that's ok\n");
	if (buf)
	    free (buf);
      }

      fclose (file);

      /* Save outgoing message */
      if (save_to)
	{
	  savefile = strdup (env->to);
	  if (savefile)
	    {
	      char *p = strchr (savefile, '@');
	      if (p)
		*p = 0;
	    }
	}
      util_save_outgoing (msg, savefile);
      if (savefile)
	free (savefile);

      /* Check if we need to save the message to files or pipes.  */
      if (env->outfiles)
	{
	  int i;
	  for (i = 0; i < env->nfiles; i++)
	    {
	      /* Pipe to a cmd.  */
	      if (env->outfiles[i][0] == '|')
		msg_to_pipe (&(env->outfiles[i][1]), msg);
	      /* Save to a file.  */
	      else
		{
		  int status;
		  mailbox_t mbx = NULL;
		  status = mailbox_create_default (&mbx, env->outfiles[i]);
		  if (status == 0)
		    {
		      status = mailbox_open (mbx, MU_STREAM_WRITE
					     | MU_STREAM_CREAT);
		      if (status == 0)
			{
			  mailbox_append_message (mbx, msg);
			  mailbox_close (mbx);
			}
		      mailbox_destroy (&mbx);
		    }
		  if (status)
		    util_error("can't create mailbox %s", env->outfiles[i]);
		}
	    }
	}

      /* Do we need to Send the message on the wire?  */
      if ((env->to && *env->to != '\0')
	  || (env->cc && *env->cc != '\0')
	  || (env->bcc && *env->bcc != '\0'))
	{
	  if (util_find_env ("sendmail")->set)
	    {
	      char *sendmail = util_find_env ("sendmail")->value;
	      status = mailer_create (&mailer, sendmail);
	      if (status == 0)
		{
		  if (util_find_env ("verbose")->set)
		    {
		      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);
		      mailer_close (mailer);
		    }
		  mailer_destroy (&mailer);
		}
	      if (status != 0)
		msg_to_pipe (sendmail, msg);
	    }
	  else
	    util_error ("variable sendmail not set: no mailer");
	}
      message_destroy (&msg, NULL);
      remove (filename);
      free (filename);
      return 0;
    }

  remove (filename);
  free (filename);
  return 1;
}

/* 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;
}

/* FIXME: Should probably be in util.c.  */
/* Call pope(cmd) and write the message to it.  */
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;
      int n = 0;
      message_get_stream (msg, &stream);
      while (stream_read (stream, buffer, sizeof (buffer) - 1, off, &n) == 0
	     && n != 0)
	{
	  buffer[n] = '\0';
	  fprintf (fp, "%s", buffer);
	  off += n;
	}
      fclose (fp);
    }
  else
    util_error ("Piping %s failed", cmd);
}