]>
diplodocus.org Git - nmh/blob - uip/viamail.c
c6dfb2dc275cf80cc71ea0e11edebea139865087
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>
18 #include <h/mhparse.h>
20 #define VIAMAIL_SWITCHES \
21 X("to mailpath", 0, TOSW) \
22 X("from mailpath", 0, FROMSW) \
23 X("subject subject", 0, SUBJECTSW) \
24 X("parameters arguments", 0, PARAMSW) \
25 X("description text", 0, DESCRIPTSW) \
26 X("comment text", 0, COMMENTSW) \
27 X("delay seconds", 0, DELAYSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
32 X("debug", -5, DEBUGSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(VIAMAIL
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
49 static int via_mail (char *, char *, char *, char *, char *, int, char *);
53 main (int argc
, char **argv
)
56 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
57 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
58 static char postpath
[PATH_MAX
];
59 char *cp
, buf
[BUFSIZ
];
60 char **argp
, **arguments
;
63 setlocale(LC_ALL
, "");
65 invo_name
= r1bindex (argv
[0], '/');
67 /* foil search of user profile/context */
68 if (context_foil (NULL
) == -1)
71 arguments
= getarguments (invo_name
, argc
, argv
, 0);
74 while ((cp
= *argp
++)) {
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
81 adios (NULL
, "-%s unknown", cp
);
84 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
85 print_help (buf
, switches
, 1);
88 print_version(invo_name
);
93 adios (NULL
, "missing argument to %s", argp
[-2]);
97 adios (NULL
, "missing argument to %s", argp
[-2]);
101 adios (NULL
, "missing argument to %s", argp
[-2]);
105 adios (NULL
, "missing argument to %s", argp
[-2]);
109 adios (NULL
, "missing argument to %s", argp
[-2]);
112 if (!(cp
= *argp
++) || *cp
== '-')
113 adios (NULL
, "missing argument to %s", argp
[-2]);
116 * If there is an error, just reset the delay parameter
117 * to -1. We will set a default delay later.
119 if (sscanf (cp
, "%d", &delay
) != 1)
124 adios (NULL
, "missing argument to %s", argp
[-2]);
142 adios (NULL
, "missing -viamail \"mailpath\" switch");
144 /* viamail doesn't read the context and postproc isn't always what
145 we want, such as when running make distcheck. If we have the
146 absolute path, set postproc to point to post in the same
147 directory as this executable.
148 This could be generalized to handle relative paths (by
149 converting to absolute), to find the full path from PATH given
150 just the basename, and to squash out ../ but it's only needed
151 here. viamail is typically called from sendfiles, which
152 provides the absolute path.
154 if (argv
[0] && argv
[0][0] == '/' &&
155 strlen(argv
[0]) - 3 < sizeof postpath
) {
156 strncpy (postpath
, argv
[0], sizeof postpath
- 1);
157 postpath
[sizeof postpath
- 1] = '\0';
158 if ((cp
= strrchr (postpath
, '/'))) {
162 /* strlen ("post") <= sizeof postpath - (cp - postpath) - 2
163 but use strncat just in case the code above changes. */
164 strncat (postpath
, "post", sizeof postpath
- (cp
- postpath
) - 2);
166 if (stat (postpath
, &st
) == OK
) {
172 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
173 return 0; /* dead code to satisfy the compiler */
182 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
183 char *cmntsw
, int delay
, char *fromsw
)
186 char tmpfil
[BUFSIZ
], *program
;
192 umask (~m_gmprot ());
194 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
195 if (tfile
== NULL
) adios("viamail", "unable to create temporary file");
197 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
199 if (!strchr(mailsw
, '@'))
200 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
201 fprintf (fp
, "To: %s\n", mailsw
);
204 fprintf (fp
, "Subject: %s\n", subjsw
);
207 if (!strchr(fromsw
, '@'))
208 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
209 fprintf (fp
, "From: %s\n", fromsw
);
212 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
213 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
216 fprintf (fp
, "; %s", parmsw
);
219 fprintf (fp
, "\n\t(%s)", cmntsw
);
222 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
224 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
227 adios (tmpfil
, "error writing to");
229 writeBase64aux (stdin
, fp
);
231 adios (tmpfil
, "error writing to");
233 if (fstat (fileno (fp
), &st
) == NOTOK
)
234 adios ("failed", "fstat of %s", tmpfil
);
243 vec
= argsplit(postproc
, &program
, &vecp
);
245 vec
[vecp
++] = "-verbose";
247 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, (char *)0, 0)) {
257 if (unlink (tmpfil
) == -1)
258 advise (NULL
, "unable to remove temp file %s", tmpfil
);