]>
diplodocus.org Git - nmh/blob - uip/viamail.c
4fde61bb04cab802bec908e01ed74fe4187a09a2
3 * viamail.c -- send multiple files in a MIME message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
21 static struct swit switches
[] = {
25 { "from mailpath", 0 },
27 { "subject subject", 0 },
29 { "parameters arguments", 0 },
31 { "description text", 0 },
33 { "comment text", 0 },
35 { "delay seconds", 0 },
56 static int via_mail (char *, char *, char *, char *, char *, int, char *);
60 main (int argc
, char **argv
)
63 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
64 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
65 static char postpath
[PATH_MAX
];
66 char *cp
, buf
[BUFSIZ
];
67 char **argp
, **arguments
;
70 setlocale(LC_ALL
, "");
72 invo_name
= r1bindex (argv
[0], '/');
74 /* foil search of user profile/context */
75 if (context_foil (NULL
) == -1)
78 arguments
= getarguments (invo_name
, argc
, argv
, 0);
81 while ((cp
= *argp
++)) {
83 switch (smatch (++cp
, switches
)) {
85 ambigsw (cp
, switches
);
88 adios (NULL
, "-%s unknown", cp
);
91 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
92 print_help (buf
, switches
, 1);
95 print_version(invo_name
);
100 adios (NULL
, "missing argument to %s", argp
[-2]);
104 adios (NULL
, "missing argument to %s", argp
[-2]);
108 adios (NULL
, "missing argument to %s", argp
[-2]);
112 adios (NULL
, "missing argument to %s", argp
[-2]);
116 adios (NULL
, "missing argument to %s", argp
[-2]);
119 if (!(cp
= *argp
++) || *cp
== '-')
120 adios (NULL
, "missing argument to %s", argp
[-2]);
123 * If there is an error, just reset the delay parameter
124 * to -1. We will set a default delay later.
126 if (sscanf (cp
, "%d", &delay
) != 1)
131 adios (NULL
, "missing argument to %s", argp
[-2]);
149 adios (NULL
, "missing -viamail \"mailpath\" switch");
151 /* viamail doesn't read the context and postproc isn't always what
152 we want, such as when running make distcheck. If we have the
153 absolute path, set postproc to point to post in the same
154 directory as this executable.
155 This could be generalized to handle relative paths (by
156 converting to absolute), to find the full path from PATH given
157 just the basename, and to squash out ../ but it's only needed
158 here. viamail is typically called from sendfiles, which
159 provides the absolute path.
161 if (argv
[0] && argv
[0][0] == '/' &&
162 strlen(argv
[0]) - 3 < sizeof postpath
) {
163 strncpy (postpath
, argv
[0], sizeof postpath
- 1);
164 postpath
[sizeof postpath
- 1] = '\0';
165 if ((cp
= strrchr (postpath
, '/'))) {
169 /* strlen ("post") <= sizeof postpath - (cp - postpath) - 2
170 but use strncat just in case the code above changes. */
171 strncat (postpath
, "post", sizeof postpath
- (cp
- postpath
) - 2);
173 if (stat (postpath
, &st
) == OK
) {
179 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
180 return 0; /* dead code to satisfy the compiler */
189 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
190 char *cmntsw
, int delay
, char *fromsw
)
192 int status
, vecp
= 1;
199 umask (~m_gmprot ());
201 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
202 if (tfile
== NULL
) adios("viamail", "unable to create temporary file");
204 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
206 if (!strchr(mailsw
, '@'))
207 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
208 fprintf (fp
, "To: %s\n", mailsw
);
211 fprintf (fp
, "Subject: %s\n", subjsw
);
214 if (!strchr(fromsw
, '@'))
215 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
216 fprintf (fp
, "From: %s\n", fromsw
);
219 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
220 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
223 fprintf (fp
, "; %s", parmsw
);
226 fprintf (fp
, "\n\t(%s)", cmntsw
);
229 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
231 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
234 adios (tmpfil
, "error writing to");
236 writeBase64aux (stdin
, fp
);
238 adios (tmpfil
, "error writing to");
240 if (fstat (fileno (fp
), &st
) == NOTOK
)
241 adios ("failed", "fstat of %s", tmpfil
);
249 vec
[0] = r1bindex (postproc
, '/');
251 vec
[vecp
++] = "-verbose";
253 switch (sendsbr (vec
, vecp
, tmpfil
, &st
, 0, (char *)0, 0)) {
263 if (unlink (tmpfil
) == -1)
264 advise (NULL
, "unable to remove temp file %s", tmpfil
);