1/* mail.c
2   Send mail to a user.
3
4   Copyright (C) 1992, 1993 Ian Lance Taylor
5
6   This file is part of the Taylor UUCP package.
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21
22   The author of the program may be contacted at ian@airs.com.
23   */
24
25#include "uucp.h"
26
27#include "uudefs.h"
28#include "sysdep.h"
29#include "system.h"
30
31#include <errno.h>
32
33#if HAVE_TIME_H
34#include <time.h>
35#endif
36
37#ifndef ctime
38extern char *ctime ();
39#endif
40
41/* Mail a message to a user.  */
42
43boolean
44fsysdep_mail (zto, zsubject, cstrs, paz)
45     const char *zto;
46     const char *zsubject;
47     int cstrs;
48     const char **paz;
49{
50  char **pazargs;
51  char *zcopy, *ztok;
52  size_t cargs, iarg;
53  FILE *e;
54  pid_t ipid;
55  time_t itime;
56  int i;
57
58  /* Parse MAIL_PROGRAM into an array of arguments.  */
59  zcopy = zbufcpy (MAIL_PROGRAM);
60
61  cargs = 0;
62  for (ztok = strtok (zcopy, " \t");
63       ztok != NULL;
64       ztok = strtok ((char *) NULL, " \t"))
65    ++cargs;
66
67  pazargs = (char **) xmalloc ((cargs + 4) * sizeof (char *));
68
69  memcpy (zcopy, MAIL_PROGRAM, sizeof MAIL_PROGRAM);
70  for (ztok = strtok (zcopy, " \t"), iarg = 0;
71       ztok != NULL;
72       ztok = strtok ((char *) NULL, " \t"), ++iarg)
73    pazargs[iarg] = ztok;
74
75#if ! MAIL_PROGRAM_SUBJECT_BODY
76  pazargs[iarg++] = (char *) "-s";
77  pazargs[iarg++] = (char *) zsubject;
78#endif
79
80#if ! MAIL_PROGRAM_TO_BODY
81  pazargs[iarg++] = (char *) zto;
82#endif
83
84  pazargs[iarg] = NULL;
85
86  e = espopen ((const char **) pazargs, FALSE, &ipid);
87
88  ubuffree (zcopy);
89  xfree ((pointer) pazargs);
90
91  if (e == NULL)
92    {
93      ulog (LOG_ERROR, "espopen (%s): %s", MAIL_PROGRAM,
94	    strerror (errno));
95      return FALSE;
96    }
97
98#if MAIL_PROGRAM_TO_BODY
99  fprintf (e, "To: %s\n", zto);
100#endif
101#if MAIL_PROGRAM_SUBJECT_BODY
102  fprintf (e, "Subject: %s\n", zsubject);
103#endif
104
105#if MAIL_PROGRAM_TO_BODY || MAIL_PROGRAM_SUBJECT_BODY
106  fprintf (e, "\n");
107#endif
108
109  (void) time (&itime);
110  /* Remember that ctime includes a \n, so this skips a line.  */
111  fprintf (e, "Message from UUCP on %s %s\n", zSlocalname,
112	   ctime (&itime));
113
114  ulog(LOG_ERROR, "mail %s about %s on %s", zto, zsubject, zSlocalname);
115  for (i = 0; i < cstrs; i++) {
116    ulog(LOG_ERROR, "- %s", paz[i]);
117    fputs (paz[i], e);
118  }
119
120  (void) fclose (e);
121
122  return ixswait ((unsigned long) ipid, MAIL_PROGRAM) == 0;
123}
124