]>
diplodocus.org Git - nmh/blob - uip/viamail.c
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 #define VIAMAIL_SWITCHES \
22 X("to mailpath", 0, TOSW) \
23 X("from mailpath", 0, FROMSW) \
24 X("subject subject", 0, SUBJECTSW) \
25 X("parameters arguments", 0, PARAMSW) \
26 X("description text", 0, DESCRIPTSW) \
27 X("comment text", 0, COMMENTSW) \
28 X("delay seconds", 0, DELAYSW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
33 X("debug", -5, DEBUGSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(VIAMAIL
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
50 static int via_mail (char *, char *, char *, char *, char *, int, char *);
54 main (int argc
, char **argv
)
57 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
58 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
59 static char postpath
[PATH_MAX
];
60 char *cp
, buf
[BUFSIZ
];
61 char **argp
, **arguments
;
64 setlocale(LC_ALL
, "");
66 invo_name
= r1bindex (argv
[0], '/');
68 /* foil search of user profile/context */
69 if (context_foil (NULL
) == -1)
72 arguments
= getarguments (invo_name
, argc
, argv
, 0);
75 while ((cp
= *argp
++)) {
77 switch (smatch (++cp
, switches
)) {
79 ambigsw (cp
, switches
);
82 adios (NULL
, "-%s unknown", cp
);
85 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
86 print_help (buf
, switches
, 1);
89 print_version(invo_name
);
94 adios (NULL
, "missing argument to %s", argp
[-2]);
98 adios (NULL
, "missing argument to %s", argp
[-2]);
102 adios (NULL
, "missing argument to %s", argp
[-2]);
106 adios (NULL
, "missing argument to %s", argp
[-2]);
110 adios (NULL
, "missing argument to %s", argp
[-2]);
113 if (!(cp
= *argp
++) || *cp
== '-')
114 adios (NULL
, "missing argument to %s", argp
[-2]);
117 * If there is an error, just reset the delay parameter
118 * to -1. We will set a default delay later.
120 if (sscanf (cp
, "%d", &delay
) != 1)
125 adios (NULL
, "missing argument to %s", argp
[-2]);
143 adios (NULL
, "missing -viamail \"mailpath\" switch");
145 /* viamail doesn't read the context and postproc isn't always what
146 we want, such as when running make distcheck. If we have the
147 absolute path, set postproc to point to post in the same
148 directory as this executable.
149 This could be generalized to handle relative paths (by
150 converting to absolute), to find the full path from PATH given
151 just the basename, and to squash out ../ but it's only needed
152 here. viamail is typically called from sendfiles, which
153 provides the absolute path.
155 if (argv
[0] && argv
[0][0] == '/' &&
156 strlen(argv
[0]) - 3 < sizeof postpath
) {
157 strncpy (postpath
, argv
[0], sizeof postpath
- 1);
158 postpath
[sizeof postpath
- 1] = '\0';
159 if ((cp
= strrchr (postpath
, '/'))) {
163 /* strlen ("post") <= sizeof postpath - (cp - postpath) - 2
164 but use strncat just in case the code above changes. */
165 strncat (postpath
, "post", sizeof postpath
- (cp
- postpath
) - 2);
167 if (stat (postpath
, &st
) == OK
) {
173 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
174 return 0; /* dead code to satisfy the compiler */
183 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
184 char *cmntsw
, int delay
, char *fromsw
)
187 char tmpfil
[BUFSIZ
], *program
;
193 umask (~m_gmprot ());
195 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
196 if (tfile
== NULL
) adios("viamail", "unable to create temporary file");
198 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
200 if (!strchr(mailsw
, '@'))
201 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
202 fprintf (fp
, "To: %s\n", mailsw
);
205 fprintf (fp
, "Subject: %s\n", subjsw
);
208 if (!strchr(fromsw
, '@'))
209 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
210 fprintf (fp
, "From: %s\n", fromsw
);
213 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
214 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
217 fprintf (fp
, "; %s", parmsw
);
220 fprintf (fp
, "\n\t(%s)", cmntsw
);
223 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
225 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
228 adios (tmpfil
, "error writing to");
230 writeBase64aux (stdin
, fp
);
232 adios (tmpfil
, "error writing to");
234 if (fstat (fileno (fp
), &st
) == NOTOK
)
235 adios ("failed", "fstat of %s", tmpfil
);
244 vec
= argsplit(postproc
, &program
, &vecp
);
246 vec
[vecp
++] = "-verbose";
248 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, (char *)0, 0)) {
258 if (unlink (tmpfil
) == -1)
259 advise (NULL
, "unable to remove temp file %s", tmpfil
);