BSD 4 development
[unix-history] / usr / src / cmd / ucbmail / main.c
CommitLineData
c8eb690c
BJ
1#
2
3#include "rcv.h"
4#include <sys/stat.h>
5
6/*
7 * Mail -- a mail program
8 *
9 * Startup -- interface with user.
10 */
11
12/*
13 * Find out who the user is, copy his mail file (if exists) into
14 * /tmp/Rxxxxx and set up the message pointers. Then, print out the
15 * message headers and read user commands.
16 *
17 * Command line syntax:
18 * Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ]
19 * or:
20 * Mail [ -i ] [ -r address ] [ -h number ] people ...
21 */
22
23main(argc, argv)
24 char **argv;
25{
26 register char *ef;
27 register int i, argp;
28 int mustsend, uflag;
29 FILE *ibuf, *ftat;
30 extern char tempMesg[], _sobuf[];
31
32#ifdef signal
33 Siginit();
34#endif
35
36 /*
37 * Set up a reasonable environment. We clobber the last
38 * element of argument list for compatibility with version 6,
39 * figure out whether we are being run interactively, set up
40 * all the temporary files, buffer standard output, and so forth.
41 */
42
43 uflag = 0;
44 argv[argc] = (char *) -1;
45 mypid = getpid();
46 intty = isatty(0);
47 outtty = isatty(1);
48 image = -1;
49 setbuf(stdout, _sobuf);
50
51 /*
52 * Now, determine how we are being used.
53 * We successively pick off instances of -r, -h, -f, and -i.
54 * If called as "rmail" we note this fact for letter sending.
55 * If there is anything left, it is the base of the list
56 * of users to mail to. Argp will be set to point to the
57 * first of these users.
58 */
59
60 ef = NOSTR;
61 argp = -1;
62 mustsend = 0;
63 if (argc > 0 && **argv == 'r')
64 rmail++;
65 for (i = 1; i < argc; i++) {
66
67 /*
68 * If current argument is not a flag, then the
69 * rest of the arguments must be recipients.
70 */
71
72 if (*argv[i] != '-') {
73 argp = i;
74 break;
75 }
76 switch (argv[i][1]) {
77 case 'r':
78 /*
79 * Next argument is address to be sent along
80 * to the mailer.
81 */
82 if (i >= argc - 1) {
83 fprintf(stderr, "Address required after -r\n");
84 exit(1);
85 }
86 mustsend++;
87 rflag = argv[i+1];
88 i++;
89 break;
90
91 case 'u':
92 /*
93 * Next argument is person to pretend to be.
94 */
95 uflag++;
96 if (i >= argc - 1) {
97 fprintf(stderr, "You obviously dont know what you're doing\n");
98 exit(1);
99 }
100 strcpy(myname, argv[i+1]);
101 i++;
102 break;
103
104 case 'i':
105 /*
106 * User wants to ignore interrupts.
107 * Set the variable "ignore"
108 */
109 assign("ignore", "");
110 break;
111
112 case 'd':
113 debug++;
114 break;
115
116 case 'h':
117 /*
118 * Specified sequence number for network.
119 * This is the number of "hops" made so
120 * far (count of times message has been
121 * forwarded) to help avoid infinite mail loops.
122 */
123 if (i >= argc - 1) {
124 fprintf(stderr, "Number required for -h\n");
125 exit(1);
126 }
127 mustsend++;
128 hflag = atoi(argv[i+1]);
129 if (hflag == 0) {
130 fprintf(stderr, "-h needs non-zero number\n");
131 exit(1);
132 }
133 i++;
134 break;
135
136 case 's':
137 /*
138 * Give a subject field for sending from
139 * non terminal
140 */
141 if (i >= argc - 1) {
142 fprintf(stderr, "Subject req'd for -s\n");
143 exit(1);
144 }
145 mustsend++;
146 sflag = argv[i+1];
147 i++;
148 break;
149
150 case 'f':
151 /*
152 * User is specifying file to "edit" with Mail,
153 * as opposed to reading system mailbox.
154 * If no argument is given after -f, we read his
155 * mbox file in his home directory.
156 */
157 if (i >= argc - 1)
158 ef = mbox;
159 else
160 ef = argv[i + 1];
161 i++;
162 break;
163
164 case 'n':
165 /*
166 * User doesn't want to source /usr/lib/Mail.rc
167 */
168 nosrc++;
169 break;
170
171 default:
172 fprintf(stderr, "Unknown flag: %s\n", argv[i]);
173 exit(1);
174 }
175 }
176
177 /*
178 * Check for inconsistent arguments.
179 */
180
181 if (rflag != NOSTR && strcmp(rflag, "daemon") == 0) {
182 ftat = fopen("/crp/kas/gotcha", "a");
183 if (ftat != NULL) {
184 fprintf(ftat, "user daemon, real uid %d\n", getuid());
185 fclose(ftat);
186 }
187 }
188 if (ef != NOSTR && argp != -1) {
189 fprintf(stderr, "Cannot give -f and people to send to.\n");
190 exit(1);
191 }
192 if (mustsend && argp == -1) {
193 fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n");
194 exit(1);
195 }
196 tinit();
197 if (argp != -1) {
198 commands();
199 mail(&argv[argp]);
200
201 /*
202 * why wait?
203 */
204
205 exit(senderr);
206 }
207
208 /*
209 * Ok, we are reading mail.
210 * Decide whether we are editing a mailbox or reading
211 * the system mailbox, and open up the right stuff.
212 */
213
214 rcvmode++;
215 if (ef != NOSTR) {
216 edit++;
217 editfile = mailname = ef;
218 if ((ibuf = fopen(mailname, "r")) == NULL) {
219 perror(mailname);
220 exit(1);
221 }
222 if ((i = open(mailname, 1)) < 0)
223 printf("Warning: \"%s\" not writable.\n", mailname);
224 else
225 close(i);
226 }
227 else {
228 if ((ibuf = fopen(mailname, "r")) == NULL) {
229 if (uflag)
230 printf("No mail for %s\n", myname);
231 else
232 printf("No mail.\n");
233 exit(0);
234 }
235 }
236
237 /*
238 * Copy the messages into /tmp
239 * and set pointers.
240 */
241
242 mailsize = fsize(ibuf);
243 if ((otf = fopen(tempMesg, "w")) == NULL) {
244 perror(tempMesg);
245 exit(1);
246 }
247 if ((itf = fopen(tempMesg, "r")) == NULL) {
248 perror(tempMesg);
249 exit(1);
250 }
251 remove(tempMesg);
252 setptr(ibuf);
253 fclose(ibuf);
254
255 /*
256 * print headings and accept user commands.
257 */
258
259 if (msgCount == 0) {
260 if (uflag)
261 printf("No mail for %s\n", myname);
262 else
263 printf("No messages.\n");
264 exit(1);
265 }
266 commands();
267 if (!edit)
268 quit();
269 exit(0);
270}