1 |
/* (C) Copyright 1993,1994 by Carnegie Mellon University |
2 |
* All Rights Reserved. |
3 |
* |
4 |
* Permission to use, copy, modify, distribute, and sell this software |
5 |
* and its documentation for any purpose is hereby granted without |
6 |
* fee, provided that the above copyright notice appear in all copies |
7 |
* and that both that copyright notice and this permission notice |
8 |
* appear in supporting documentation, and that the name of Carnegie |
9 |
* Mellon University not be used in advertising or publicity |
10 |
* pertaining to distribution of the software without specific, |
11 |
* written prior permission. Carnegie Mellon University makes no |
12 |
* representations about the suitability of this software for any |
13 |
* purpose. It is provided "as is" without express or implied |
14 |
* warranty. |
15 |
* |
16 |
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO |
17 |
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
18 |
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE |
19 |
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
20 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
21 |
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
22 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
23 |
* SOFTWARE. |
24 |
*/ |
25 |
#include <stdio.h> |
26 |
#include <stdlib.h> |
27 |
#include <string.h> |
28 |
#include <errno.h> |
29 |
#include <getopt.h> |
30 |
#include <sys/types.h> |
31 |
#include <sys/wait.h> |
32 |
#include <unistd.h> |
33 |
#include "common.h" |
34 |
#include "version.h" |
35 |
#include "xmalloc.h" |
36 |
|
37 |
#define MAXADDRESS 100 |
38 |
|
39 |
extern int attachment; |
40 |
|
41 |
void usage(void); |
42 |
void sendmail(FILE *infile, char **addr, int start); |
43 |
void inews(FILE *infile); |
44 |
void os_perror(char *str); |
45 |
int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile, |
46 |
char *subject, char *headers, long int maxsize, |
47 |
char *typeoverride, char *outfname); |
48 |
|
49 |
int main(int argc, char **argv) |
50 |
{ |
51 |
int opt; |
52 |
char *fname = 0; |
53 |
char *subject = 0; |
54 |
char *descfname = 0; |
55 |
long maxsize = 0; |
56 |
char *outfname = 0; |
57 |
char *newsgroups = 0; |
58 |
char *ctype = 0; |
59 |
char *headers = 0; |
60 |
int i; |
61 |
char *p; |
62 |
char sbuf[1024]; |
63 |
char fnamebuf[4096]; |
64 |
int part; |
65 |
FILE *infile; |
66 |
FILE *descfile = 0; |
67 |
|
68 |
if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') { |
69 |
maxsize = atoi(p); |
70 |
} |
71 |
|
72 |
while ((opt = getopt(argc, argv, "as:d:m:c:o:n:")) != EOF) { |
73 |
switch (opt) { |
74 |
case 's': |
75 |
subject = optarg; |
76 |
break; |
77 |
|
78 |
case 'd': |
79 |
descfname = optarg; |
80 |
break; |
81 |
|
82 |
case 'm': |
83 |
maxsize = atoi(optarg); |
84 |
break; |
85 |
|
86 |
case 'c': |
87 |
ctype = optarg; |
88 |
break; |
89 |
|
90 |
case 'o': |
91 |
outfname = optarg; |
92 |
break; |
93 |
|
94 |
case 'n': |
95 |
newsgroups = optarg; |
96 |
break; |
97 |
|
98 |
case 'a': |
99 |
attachment = 1; |
100 |
break; |
101 |
|
102 |
default: |
103 |
usage(); |
104 |
|
105 |
} |
106 |
} |
107 |
|
108 |
if (ctype) { |
109 |
if (!strncasecmp(ctype, "text/", 5)) { |
110 |
fprintf(stderr, "This program is not appropriate for encoding textual data\n"); |
111 |
exit(1); |
112 |
} |
113 |
if (strncasecmp(ctype, "application/", 12) && strncasecmp(ctype, "audio/", 6) && |
114 |
strncasecmp(ctype, "image/", 6) && strncasecmp(ctype, "video/", 6)) { |
115 |
fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n"); |
116 |
exit(1); |
117 |
} |
118 |
} |
119 |
|
120 |
if (optind == argc) { |
121 |
fprintf(stderr, "An input file must be specified\n"); |
122 |
usage(); |
123 |
} |
124 |
fname = argv[optind++]; |
125 |
|
126 |
/* Must have exactly one of -o, -n, or destination addrs */ |
127 |
if (optind == argc) { |
128 |
if (outfname && newsgroups) { |
129 |
fprintf(stderr, "The -o and -n switches are mutually exclusive.\n"); |
130 |
usage(); |
131 |
} |
132 |
if (!outfname && !newsgroups) { |
133 |
fprintf(stderr, "Either an address or one of the -o or -n switches is required\n"); |
134 |
usage(); |
135 |
} |
136 |
if (newsgroups) { |
137 |
headers = xmalloc(strlen(newsgroups) + 25); |
138 |
sprintf(headers, "Newsgroups: %s\n", newsgroups); |
139 |
} |
140 |
} |
141 |
else { |
142 |
if (outfname) { |
143 |
fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n"); |
144 |
usage(); |
145 |
} |
146 |
if (newsgroups) { |
147 |
fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n"); |
148 |
usage(); |
149 |
} |
150 |
headers = xmalloc(strlen(argv[optind]) + 25); |
151 |
sprintf(headers, "To: %s", argv[optind]); |
152 |
for (i = optind+1; i < argc; i++) { |
153 |
headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25); |
154 |
strcat(headers, ",\n\t"); |
155 |
strcat(headers, argv[i]); |
156 |
} |
157 |
strcat(headers, "\n"); |
158 |
} |
159 |
|
160 |
if (!subject) { |
161 |
fputs("Subject: ", stdout); |
162 |
fflush(stdout); |
163 |
if (!fgets(sbuf, sizeof(sbuf), stdin)) { |
164 |
fprintf(stderr, "A subject is required\n"); |
165 |
usage(); |
166 |
} |
167 |
if ((p = strchr(sbuf, '\n'))) *p = '\0'; |
168 |
subject = sbuf; |
169 |
} |
170 |
|
171 |
if (!outfname) { |
172 |
if (getenv("TMPDIR")) { |
173 |
strcpy(fnamebuf, getenv("TMPDIR")); |
174 |
} |
175 |
else { |
176 |
strcpy(fnamebuf, "/var/tmp"); |
177 |
} |
178 |
strcat(fnamebuf, "/mpackXXXXXX"); |
179 |
mktemp(fnamebuf); |
180 |
outfname = strsave(fnamebuf); |
181 |
} |
182 |
|
183 |
infile = fopen(fname, "r"); |
184 |
if (!infile) { |
185 |
os_perror(fname); |
186 |
exit(1); |
187 |
} |
188 |
|
189 |
if (descfname) { |
190 |
descfile = fopen(descfname, "r"); |
191 |
if (!descfile) { |
192 |
os_perror(descfname); |
193 |
exit(1); |
194 |
} |
195 |
} |
196 |
|
197 |
if (encode(infile, (FILE *)0, fname, descfile, subject, headers, |
198 |
maxsize, ctype, outfname)) exit(1); |
199 |
|
200 |
if (optind < argc || newsgroups) { |
201 |
for (part = 0;;part++) { |
202 |
sprintf(fnamebuf, "%s.%02d", outfname, part); |
203 |
infile = fopen(part ? fnamebuf : outfname, "r"); |
204 |
if (!infile) { |
205 |
if (part) break; |
206 |
continue; |
207 |
} |
208 |
if (newsgroups) { |
209 |
inews(infile); |
210 |
} |
211 |
else { |
212 |
sendmail(infile, argv, optind); |
213 |
} |
214 |
fclose(infile); |
215 |
remove(part ? fnamebuf : outfname); |
216 |
} |
217 |
} |
218 |
|
219 |
exit(0); |
220 |
} |
221 |
|
222 |
void usage(void) |
223 |
{ |
224 |
fprintf(stderr, "mpack version %s\n", MPACK_VERSION); |
225 |
fprintf(stderr, |
226 |
"usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n"); |
227 |
fprintf(stderr, |
228 |
" mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n"); |
229 |
fprintf(stderr, |
230 |
" mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n"); |
231 |
exit(1); |
232 |
} |
233 |
|
234 |
void sendmail(FILE *infile, char **addr, int start) |
235 |
{ |
236 |
int status; |
237 |
int pid; |
238 |
|
239 |
if (start < 2) abort(); |
240 |
|
241 |
#ifdef SCO |
242 |
addr[--start] = "execmail"; |
243 |
#else |
244 |
addr[--start] = "-oi"; |
245 |
addr[--start] = "sendmail"; |
246 |
#endif |
247 |
|
248 |
do { |
249 |
pid = fork(); |
250 |
} while (pid == -1 && errno == EAGAIN); |
251 |
|
252 |
if (pid == -1) { |
253 |
perror("fork"); |
254 |
return; |
255 |
} |
256 |
if (pid != 0) { |
257 |
while (pid != wait(&status)); |
258 |
return; |
259 |
} |
260 |
|
261 |
dup2(fileno(infile), 0); |
262 |
fclose(infile); |
263 |
#ifdef SCO |
264 |
execv("/usr/lib/mail/execmail", addr+start); |
265 |
#else |
266 |
execv("/usr/lib/sendmail", addr+start); |
267 |
execv("/usr/sbin/sendmail", addr+start); |
268 |
#endif |
269 |
perror("execv"); |
270 |
_exit(1); |
271 |
} |
272 |
|
273 |
void inews(FILE *infile) |
274 |
{ |
275 |
int status; |
276 |
int pid; |
277 |
|
278 |
do { |
279 |
pid = fork(); |
280 |
} while (pid == -1 && errno == EAGAIN); |
281 |
|
282 |
if (pid == -1) { |
283 |
perror("fork"); |
284 |
return; |
285 |
} |
286 |
if (pid != 0) { |
287 |
while (pid != wait(&status)); |
288 |
return; |
289 |
} |
290 |
|
291 |
dup2(fileno(infile), 0); |
292 |
fclose(infile); |
293 |
execlp("inews", "inews", "-h", "-S", (char *)0); |
294 |
execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0); |
295 |
execl("/usr/local/lib/news/inews", "inews", "-h", "-S", (char *)0); |
296 |
execl("/etc/inews", "inews", "-h", "-S", (char *)0); |
297 |
execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0); |
298 |
execl("/usr/news/inews", "inews", "-h", "-S", (char *)0); |
299 |
execl("/usr/news/bin/inews", "inews", "-h", "-S", (char *)0); |
300 |
perror("execl"); |
301 |
_exit(1); |
302 |
} |
303 |
|
304 |
void warn(void) |
305 |
{ |
306 |
abort(); |
307 |
} |