msgs.c revision 1.5 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1980 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.2 mycroft /*static char sccsid[] = "from: @(#)msgs.c 5.8 (Berkeley) 2/4/91";*/
42 1.5 mycroft static char rcsid[] = "$Id: msgs.c,v 1.5 1994/01/07 16:22:20 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * msgs - a user bulletin board program
47 1.1 cgd *
48 1.1 cgd * usage:
49 1.5 mycroft * msgs [fhlopqr] [[-]number] to read messages
50 1.1 cgd * msgs -s to place messages
51 1.1 cgd * msgs -c [-days] to clean up the bulletin board
52 1.1 cgd *
53 1.1 cgd * prompt commands are:
54 1.1 cgd * y print message
55 1.1 cgd * n flush message, go to next message
56 1.1 cgd * q flush message, quit
57 1.1 cgd * p print message, turn on 'pipe thru more' mode
58 1.1 cgd * P print message, turn off 'pipe thru more' mode
59 1.1 cgd * - reprint last message
60 1.1 cgd * s[-][<num>] [<filename>] save message
61 1.1 cgd * m[-][<num>] mail with message in temp mbox
62 1.1 cgd * x exit without flushing this message
63 1.1 cgd * <num> print message number <num>
64 1.1 cgd */
65 1.1 cgd
66 1.1 cgd #define V7 /* will look for TERM in the environment */
67 1.1 cgd #define OBJECT /* will object to messages without Subjects */
68 1.1 cgd /* #define REJECT /* will reject messages without Subjects
69 1.1 cgd (OBJECT must be defined also) */
70 1.1 cgd /* #define UNBUFFERED /* use unbuffered output */
71 1.1 cgd
72 1.1 cgd #include <sys/param.h>
73 1.1 cgd #include <sys/stat.h>
74 1.4 jtc #include <dirent.h>
75 1.1 cgd #include <ctype.h>
76 1.1 cgd #include <errno.h>
77 1.1 cgd #include <pwd.h>
78 1.1 cgd #include <setjmp.h>
79 1.1 cgd #include <sgtty.h>
80 1.1 cgd #include <signal.h>
81 1.1 cgd #include <stdio.h>
82 1.1 cgd #include <stdlib.h>
83 1.1 cgd #include <string.h>
84 1.1 cgd #include <time.h>
85 1.1 cgd #include "pathnames.h"
86 1.1 cgd
87 1.1 cgd #define CMODE 0666 /* bounds file creation mode */
88 1.1 cgd #define NO 0
89 1.1 cgd #define YES 1
90 1.1 cgd #define SUPERUSER 0 /* superuser uid */
91 1.1 cgd #define DAEMON 1 /* daemon uid */
92 1.1 cgd #define NLINES 24 /* default number of lines/crt screen */
93 1.1 cgd #define NDAYS 21 /* default keep time for messages */
94 1.1 cgd #define DAYS *24*60*60 /* seconds/day */
95 1.1 cgd #define MSGSRC ".msgsrc" /* user's rc file */
96 1.1 cgd #define BOUNDS "bounds" /* message bounds file */
97 1.1 cgd #define NEXT "Next message? [yq]"
98 1.1 cgd #define MORE "More? [ynq]"
99 1.1 cgd #define NOMORE "(No more) [q] ?"
100 1.1 cgd
101 1.1 cgd typedef char bool;
102 1.1 cgd
103 1.1 cgd FILE *msgsrc;
104 1.1 cgd FILE *newmsg;
105 1.1 cgd char *sep = "-";
106 1.1 cgd char inbuf[BUFSIZ];
107 1.1 cgd char fname[128];
108 1.1 cgd char cmdbuf[128];
109 1.1 cgd char subj[128];
110 1.1 cgd char from[128];
111 1.1 cgd char date[128];
112 1.1 cgd char *ptr;
113 1.1 cgd char *in;
114 1.1 cgd bool local;
115 1.1 cgd bool ruptible;
116 1.1 cgd bool totty;
117 1.1 cgd bool seenfrom;
118 1.1 cgd bool seensubj;
119 1.1 cgd bool blankline;
120 1.1 cgd bool printing = NO;
121 1.1 cgd bool mailing = NO;
122 1.1 cgd bool quitit = NO;
123 1.1 cgd bool sending = NO;
124 1.1 cgd bool intrpflg = NO;
125 1.1 cgd int uid;
126 1.1 cgd int msg;
127 1.1 cgd int prevmsg;
128 1.1 cgd int lct;
129 1.1 cgd int nlines;
130 1.1 cgd int Lpp = 0;
131 1.1 cgd time_t t;
132 1.1 cgd time_t keep;
133 1.1 cgd struct sgttyb otty;
134 1.1 cgd
135 1.1 cgd char *mktemp();
136 1.1 cgd char *nxtfld();
137 1.1 cgd void onintr();
138 1.1 cgd void onsusp();
139 1.1 cgd
140 1.1 cgd /* option initialization */
141 1.1 cgd bool hdrs = NO;
142 1.1 cgd bool qopt = NO;
143 1.1 cgd bool hush = NO;
144 1.1 cgd bool send_msg = NO;
145 1.1 cgd bool locomode = NO;
146 1.1 cgd bool use_pager = NO;
147 1.1 cgd bool clean = NO;
148 1.1 cgd bool lastcmd = NO;
149 1.5 mycroft bool restricted = NO;
150 1.1 cgd jmp_buf tstpbuf;
151 1.1 cgd
152 1.1 cgd main(argc, argv)
153 1.1 cgd int argc; char *argv[];
154 1.1 cgd {
155 1.1 cgd bool newrc, already;
156 1.1 cgd int rcfirst = 0; /* first message to print (from .rc) */
157 1.1 cgd int rcback = 0; /* amount to back off of rcfirst */
158 1.1 cgd int firstmsg, nextmsg, lastmsg = 0;
159 1.1 cgd int blast = 0;
160 1.1 cgd FILE *bounds;
161 1.1 cgd
162 1.1 cgd #ifdef UNBUFFERED
163 1.1 cgd setbuf(stdout, NULL);
164 1.1 cgd #endif
165 1.1 cgd
166 1.1 cgd gtty(fileno(stdout), &otty);
167 1.1 cgd time(&t);
168 1.1 cgd setuid(uid = getuid());
169 1.1 cgd ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
170 1.1 cgd if (ruptible)
171 1.1 cgd signal(SIGINT, SIG_DFL);
172 1.1 cgd
173 1.1 cgd argc--, argv++;
174 1.1 cgd while (argc > 0) {
175 1.1 cgd if (isdigit(argv[0][0])) { /* starting message # */
176 1.1 cgd rcfirst = atoi(argv[0]);
177 1.1 cgd }
178 1.1 cgd else if (isdigit(argv[0][1])) { /* backward offset */
179 1.1 cgd rcback = atoi( &( argv[0][1] ) );
180 1.1 cgd }
181 1.1 cgd else {
182 1.1 cgd ptr = *argv;
183 1.1 cgd while (*ptr) switch (*ptr++) {
184 1.1 cgd
185 1.1 cgd case '-':
186 1.1 cgd break;
187 1.1 cgd
188 1.1 cgd case 'c':
189 1.1 cgd if (uid != SUPERUSER && uid != DAEMON) {
190 1.1 cgd fprintf(stderr, "Sorry\n");
191 1.1 cgd exit(1);
192 1.1 cgd }
193 1.1 cgd clean = YES;
194 1.1 cgd break;
195 1.1 cgd
196 1.1 cgd case 'f': /* silently */
197 1.1 cgd hush = YES;
198 1.1 cgd break;
199 1.1 cgd
200 1.1 cgd case 'h': /* headers only */
201 1.1 cgd hdrs = YES;
202 1.1 cgd break;
203 1.1 cgd
204 1.1 cgd case 'l': /* local msgs only */
205 1.1 cgd locomode = YES;
206 1.1 cgd break;
207 1.1 cgd
208 1.1 cgd case 'o': /* option to save last message */
209 1.1 cgd lastcmd = YES;
210 1.1 cgd break;
211 1.1 cgd
212 1.1 cgd case 'p': /* pipe thru 'more' during long msgs */
213 1.1 cgd use_pager = YES;
214 1.1 cgd break;
215 1.1 cgd
216 1.1 cgd case 'q': /* query only */
217 1.1 cgd qopt = YES;
218 1.1 cgd break;
219 1.1 cgd
220 1.5 mycroft case 'r': /* restricted */
221 1.5 mycroft restricted = YES;
222 1.5 mycroft break;
223 1.5 mycroft
224 1.1 cgd case 's': /* sending TO msgs */
225 1.1 cgd send_msg = YES;
226 1.1 cgd break;
227 1.1 cgd
228 1.1 cgd default:
229 1.1 cgd fprintf(stderr,
230 1.5 mycroft "usage: msgs [fhlopqr] [[-]number]\n");
231 1.1 cgd exit(1);
232 1.1 cgd }
233 1.1 cgd }
234 1.1 cgd argc--, argv++;
235 1.1 cgd }
236 1.1 cgd
237 1.1 cgd /*
238 1.1 cgd * determine current message bounds
239 1.1 cgd */
240 1.1 cgd sprintf(fname, "%s/%s", _PATH_MSGS, BOUNDS);
241 1.1 cgd bounds = fopen(fname, "r");
242 1.1 cgd
243 1.1 cgd if (bounds != NULL) {
244 1.1 cgd fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
245 1.1 cgd fclose(bounds);
246 1.1 cgd blast = lastmsg; /* save upper bound */
247 1.1 cgd }
248 1.1 cgd
249 1.1 cgd if (clean)
250 1.1 cgd keep = t - (rcback? rcback : NDAYS) DAYS;
251 1.1 cgd
252 1.1 cgd if (clean || bounds == NULL) { /* relocate message bounds */
253 1.4 jtc struct dirent *dp;
254 1.1 cgd struct stat stbuf;
255 1.1 cgd bool seenany = NO;
256 1.1 cgd DIR *dirp;
257 1.1 cgd
258 1.1 cgd dirp = opendir(_PATH_MSGS);
259 1.1 cgd if (dirp == NULL) {
260 1.1 cgd perror(_PATH_MSGS);
261 1.1 cgd exit(errno);
262 1.1 cgd }
263 1.1 cgd
264 1.1 cgd firstmsg = 32767;
265 1.1 cgd lastmsg = 0;
266 1.1 cgd
267 1.1 cgd for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
268 1.1 cgd register char *cp = dp->d_name;
269 1.1 cgd register int i = 0;
270 1.1 cgd
271 1.1 cgd if (dp->d_ino == 0)
272 1.1 cgd continue;
273 1.1 cgd if (dp->d_namlen == 0)
274 1.1 cgd continue;
275 1.1 cgd
276 1.1 cgd if (clean)
277 1.1 cgd sprintf(inbuf, "%s/%s", _PATH_MSGS, cp);
278 1.1 cgd
279 1.1 cgd while (isdigit(*cp))
280 1.1 cgd i = i * 10 + *cp++ - '0';
281 1.1 cgd if (*cp)
282 1.1 cgd continue; /* not a message! */
283 1.1 cgd
284 1.1 cgd if (clean) {
285 1.1 cgd if (stat(inbuf, &stbuf) != 0)
286 1.1 cgd continue;
287 1.1 cgd if (stbuf.st_mtime < keep
288 1.1 cgd && stbuf.st_mode&S_IWRITE) {
289 1.1 cgd unlink(inbuf);
290 1.1 cgd continue;
291 1.1 cgd }
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd if (i > lastmsg)
295 1.1 cgd lastmsg = i;
296 1.1 cgd if (i < firstmsg)
297 1.1 cgd firstmsg = i;
298 1.1 cgd seenany = YES;
299 1.1 cgd }
300 1.1 cgd closedir(dirp);
301 1.1 cgd
302 1.1 cgd if (!seenany) {
303 1.1 cgd if (blast != 0) /* never lower the upper bound! */
304 1.1 cgd lastmsg = blast;
305 1.1 cgd firstmsg = lastmsg + 1;
306 1.1 cgd }
307 1.1 cgd else if (blast > lastmsg)
308 1.1 cgd lastmsg = blast;
309 1.1 cgd
310 1.1 cgd if (!send_msg) {
311 1.1 cgd bounds = fopen(fname, "w");
312 1.1 cgd if (bounds == NULL) {
313 1.1 cgd perror(fname);
314 1.1 cgd exit(errno);
315 1.1 cgd }
316 1.1 cgd chmod(fname, CMODE);
317 1.1 cgd fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
318 1.1 cgd fclose(bounds);
319 1.1 cgd }
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd if (send_msg) {
323 1.1 cgd /*
324 1.1 cgd * Send mode - place msgs in _PATH_MSGS
325 1.1 cgd */
326 1.1 cgd bounds = fopen(fname, "w");
327 1.1 cgd if (bounds == NULL) {
328 1.1 cgd perror(fname);
329 1.1 cgd exit(errno);
330 1.1 cgd }
331 1.1 cgd
332 1.1 cgd nextmsg = lastmsg + 1;
333 1.1 cgd sprintf(fname, "%s/%d", _PATH_MSGS, nextmsg);
334 1.1 cgd newmsg = fopen(fname, "w");
335 1.1 cgd if (newmsg == NULL) {
336 1.1 cgd perror(fname);
337 1.1 cgd exit(errno);
338 1.1 cgd }
339 1.1 cgd chmod(fname, 0644);
340 1.1 cgd
341 1.1 cgd fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
342 1.1 cgd fclose(bounds);
343 1.1 cgd
344 1.1 cgd sending = YES;
345 1.1 cgd if (ruptible)
346 1.1 cgd signal(SIGINT, onintr);
347 1.1 cgd
348 1.1 cgd if (isatty(fileno(stdin))) {
349 1.1 cgd ptr = getpwuid(uid)->pw_name;
350 1.1 cgd printf("Message %d:\nFrom %s %sSubject: ",
351 1.1 cgd nextmsg, ptr, ctime(&t));
352 1.1 cgd fflush(stdout);
353 1.1 cgd fgets(inbuf, sizeof inbuf, stdin);
354 1.1 cgd putchar('\n');
355 1.1 cgd fflush(stdout);
356 1.1 cgd fprintf(newmsg, "From %s %sSubject: %s\n",
357 1.1 cgd ptr, ctime(&t), inbuf);
358 1.1 cgd blankline = seensubj = YES;
359 1.1 cgd }
360 1.1 cgd else
361 1.1 cgd blankline = seensubj = NO;
362 1.1 cgd for (;;) {
363 1.1 cgd fgets(inbuf, sizeof inbuf, stdin);
364 1.1 cgd if (feof(stdin) || ferror(stdin))
365 1.1 cgd break;
366 1.1 cgd blankline = (blankline || (inbuf[0] == '\n'));
367 1.1 cgd seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
368 1.1 cgd fputs(inbuf, newmsg);
369 1.1 cgd }
370 1.1 cgd #ifdef OBJECT
371 1.1 cgd if (!seensubj) {
372 1.1 cgd printf("NOTICE: Messages should have a Subject field!\n");
373 1.1 cgd #ifdef REJECT
374 1.1 cgd unlink(fname);
375 1.1 cgd #endif
376 1.1 cgd exit(1);
377 1.1 cgd }
378 1.1 cgd #endif
379 1.1 cgd exit(ferror(stdin));
380 1.1 cgd }
381 1.1 cgd if (clean)
382 1.1 cgd exit(0);
383 1.1 cgd
384 1.1 cgd /*
385 1.1 cgd * prepare to display messages
386 1.1 cgd */
387 1.1 cgd totty = (isatty(fileno(stdout)) != 0);
388 1.1 cgd use_pager = use_pager && totty;
389 1.1 cgd
390 1.1 cgd sprintf(fname, "%s/%s", getenv("HOME"), MSGSRC);
391 1.5 mycroft msgsrc = fopen(fname, "r+");
392 1.1 cgd if (msgsrc) {
393 1.1 cgd newrc = NO;
394 1.5 mycroft if (fscanf(msgsrc, "%d\n", &nextmsg) != 1 ||
395 1.5 mycroft nextmsg > lastmsg+1) {
396 1.1 cgd printf("Warning: bounds have been reset (%d, %d)\n",
397 1.1 cgd firstmsg, lastmsg);
398 1.1 cgd truncate(fname, (off_t)0);
399 1.1 cgd newrc = YES;
400 1.1 cgd }
401 1.1 cgd else if (!rcfirst)
402 1.1 cgd rcfirst = nextmsg - rcback;
403 1.1 cgd }
404 1.5 mycroft else {
405 1.5 mycroft msgsrc = fopen(fname, "w+");
406 1.1 cgd newrc = YES;
407 1.5 mycroft }
408 1.1 cgd if (msgsrc == NULL) {
409 1.1 cgd perror(fname);
410 1.1 cgd exit(errno);
411 1.1 cgd }
412 1.1 cgd if (rcfirst) {
413 1.1 cgd if (rcfirst > lastmsg+1) {
414 1.1 cgd printf("Warning: the last message is number %d.\n",
415 1.1 cgd lastmsg);
416 1.1 cgd rcfirst = nextmsg;
417 1.1 cgd }
418 1.1 cgd if (rcfirst > firstmsg)
419 1.1 cgd firstmsg = rcfirst; /* don't set below first msg */
420 1.1 cgd }
421 1.1 cgd if (newrc) {
422 1.1 cgd nextmsg = firstmsg;
423 1.1 cgd fseek(msgsrc, 0L, 0);
424 1.1 cgd fprintf(msgsrc, "%d\n", nextmsg);
425 1.1 cgd fflush(msgsrc);
426 1.1 cgd }
427 1.1 cgd
428 1.1 cgd #ifdef V7
429 1.1 cgd if (totty) {
430 1.1 cgd struct winsize win;
431 1.1 cgd if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
432 1.1 cgd Lpp = win.ws_row;
433 1.1 cgd if (Lpp <= 0) {
434 1.1 cgd if (tgetent(inbuf, getenv("TERM")) <= 0
435 1.1 cgd || (Lpp = tgetnum("li")) <= 0) {
436 1.1 cgd Lpp = NLINES;
437 1.1 cgd }
438 1.1 cgd }
439 1.1 cgd }
440 1.1 cgd #endif
441 1.1 cgd Lpp -= 6; /* for headers, etc. */
442 1.1 cgd
443 1.1 cgd already = NO;
444 1.1 cgd prevmsg = firstmsg;
445 1.1 cgd printing = YES;
446 1.1 cgd if (ruptible)
447 1.1 cgd signal(SIGINT, onintr);
448 1.1 cgd
449 1.1 cgd /*
450 1.1 cgd * Main program loop
451 1.1 cgd */
452 1.1 cgd for (msg = firstmsg; msg <= lastmsg; msg++) {
453 1.1 cgd
454 1.1 cgd sprintf(fname, "%s/%d", _PATH_MSGS, msg);
455 1.1 cgd newmsg = fopen(fname, "r");
456 1.1 cgd if (newmsg == NULL)
457 1.1 cgd continue;
458 1.1 cgd
459 1.1 cgd gfrsub(newmsg); /* get From and Subject fields */
460 1.1 cgd if (locomode && !local) {
461 1.1 cgd fclose(newmsg);
462 1.1 cgd continue;
463 1.1 cgd }
464 1.1 cgd
465 1.1 cgd if (qopt) { /* This has to be located here */
466 1.1 cgd printf("There are new messages.\n");
467 1.1 cgd exit(0);
468 1.1 cgd }
469 1.1 cgd
470 1.1 cgd if (already && !hdrs)
471 1.1 cgd putchar('\n');
472 1.1 cgd
473 1.1 cgd /*
474 1.1 cgd * Print header
475 1.1 cgd */
476 1.1 cgd if (totty)
477 1.1 cgd signal(SIGTSTP, onsusp);
478 1.1 cgd (void) setjmp(tstpbuf);
479 1.1 cgd already = YES;
480 1.1 cgd nlines = 2;
481 1.1 cgd if (seenfrom) {
482 1.1 cgd printf("Message %d:\nFrom %s %s", msg, from, date);
483 1.1 cgd nlines++;
484 1.1 cgd }
485 1.1 cgd if (seensubj) {
486 1.1 cgd printf("Subject: %s", subj);
487 1.1 cgd nlines++;
488 1.1 cgd }
489 1.1 cgd else {
490 1.1 cgd if (seenfrom) {
491 1.1 cgd putchar('\n');
492 1.1 cgd nlines++;
493 1.1 cgd }
494 1.1 cgd while (nlines < 6
495 1.1 cgd && fgets(inbuf, sizeof inbuf, newmsg)
496 1.1 cgd && inbuf[0] != '\n') {
497 1.1 cgd fputs(inbuf, stdout);
498 1.1 cgd nlines++;
499 1.1 cgd }
500 1.1 cgd }
501 1.1 cgd
502 1.1 cgd lct = linecnt(newmsg);
503 1.1 cgd if (lct)
504 1.1 cgd printf("(%d%slines) ", lct, seensubj? " " : " more ");
505 1.1 cgd
506 1.1 cgd if (hdrs) {
507 1.1 cgd printf("\n-----\n");
508 1.1 cgd fclose(newmsg);
509 1.1 cgd continue;
510 1.1 cgd }
511 1.1 cgd
512 1.1 cgd /*
513 1.1 cgd * Ask user for command
514 1.1 cgd */
515 1.1 cgd if (totty)
516 1.1 cgd ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
517 1.1 cgd else
518 1.1 cgd inbuf[0] = 'y';
519 1.1 cgd if (totty)
520 1.1 cgd signal(SIGTSTP, SIG_DFL);
521 1.1 cgd cmnd:
522 1.1 cgd in = inbuf;
523 1.1 cgd switch (*in) {
524 1.1 cgd case 'x':
525 1.1 cgd case 'X':
526 1.1 cgd exit(0);
527 1.1 cgd
528 1.1 cgd case 'q':
529 1.1 cgd case 'Q':
530 1.1 cgd quitit = YES;
531 1.1 cgd printf("--Postponed--\n");
532 1.1 cgd exit(0);
533 1.1 cgd /* intentional fall-thru */
534 1.1 cgd case 'n':
535 1.1 cgd case 'N':
536 1.1 cgd if (msg >= nextmsg) sep = "Flushed";
537 1.1 cgd prevmsg = msg;
538 1.1 cgd break;
539 1.1 cgd
540 1.1 cgd case 'p':
541 1.1 cgd case 'P':
542 1.1 cgd use_pager = (*in++ == 'p');
543 1.1 cgd /* intentional fallthru */
544 1.1 cgd case '\n':
545 1.1 cgd case 'y':
546 1.1 cgd default:
547 1.1 cgd if (*in == '-') {
548 1.1 cgd msg = prevmsg-1;
549 1.1 cgd sep = "replay";
550 1.1 cgd break;
551 1.1 cgd }
552 1.1 cgd if (isdigit(*in)) {
553 1.1 cgd msg = next(in);
554 1.1 cgd sep = in;
555 1.1 cgd break;
556 1.1 cgd }
557 1.1 cgd
558 1.1 cgd prmesg(nlines + lct + (seensubj? 1 : 0));
559 1.1 cgd prevmsg = msg;
560 1.1 cgd
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd printf("--%s--\n", sep);
564 1.1 cgd sep = "-";
565 1.1 cgd if (msg >= nextmsg) {
566 1.1 cgd nextmsg = msg + 1;
567 1.1 cgd fseek(msgsrc, 0L, 0);
568 1.1 cgd fprintf(msgsrc, "%d\n", nextmsg);
569 1.1 cgd fflush(msgsrc);
570 1.1 cgd }
571 1.1 cgd if (newmsg)
572 1.1 cgd fclose(newmsg);
573 1.1 cgd if (quitit)
574 1.1 cgd break;
575 1.1 cgd }
576 1.1 cgd
577 1.1 cgd /*
578 1.1 cgd * Make sure .rc file gets updated
579 1.1 cgd */
580 1.1 cgd if (--msg >= nextmsg) {
581 1.1 cgd nextmsg = msg + 1;
582 1.1 cgd fseek(msgsrc, 0L, 0);
583 1.1 cgd fprintf(msgsrc, "%d\n", nextmsg);
584 1.1 cgd fflush(msgsrc);
585 1.1 cgd }
586 1.1 cgd if (already && !quitit && lastcmd && totty) {
587 1.1 cgd /*
588 1.1 cgd * save or reply to last message?
589 1.1 cgd */
590 1.1 cgd msg = prevmsg;
591 1.1 cgd ask(NOMORE);
592 1.1 cgd if (inbuf[0] == '-' || isdigit(inbuf[0]))
593 1.1 cgd goto cmnd;
594 1.1 cgd }
595 1.1 cgd if (!(already || hush || qopt))
596 1.1 cgd printf("No new messages.\n");
597 1.1 cgd exit(0);
598 1.1 cgd }
599 1.1 cgd
600 1.1 cgd prmesg(length)
601 1.1 cgd int length;
602 1.1 cgd {
603 1.1 cgd FILE *outf;
604 1.5 mycroft char *env_pager;
605 1.1 cgd
606 1.1 cgd if (use_pager && length > Lpp) {
607 1.1 cgd signal(SIGPIPE, SIG_IGN);
608 1.1 cgd signal(SIGQUIT, SIG_IGN);
609 1.5 mycroft if ((env_pager = getenv("PAGER")) == NULL) {
610 1.5 mycroft sprintf(cmdbuf, _PATH_PAGER, Lpp);
611 1.5 mycroft } else {
612 1.5 mycroft strcpy(cmdbuf, env_pager);
613 1.5 mycroft }
614 1.1 cgd outf = popen(cmdbuf, "w");
615 1.1 cgd if (!outf)
616 1.1 cgd outf = stdout;
617 1.1 cgd else
618 1.1 cgd setbuf(outf, (char *)NULL);
619 1.1 cgd }
620 1.1 cgd else
621 1.1 cgd outf = stdout;
622 1.1 cgd
623 1.1 cgd if (seensubj)
624 1.1 cgd putc('\n', outf);
625 1.1 cgd
626 1.1 cgd while (fgets(inbuf, sizeof inbuf, newmsg)) {
627 1.1 cgd fputs(inbuf, outf);
628 1.1 cgd if (ferror(outf)) {
629 1.1 cgd clearerr(outf);
630 1.1 cgd break;
631 1.1 cgd }
632 1.1 cgd }
633 1.1 cgd
634 1.1 cgd if (outf != stdout) {
635 1.1 cgd pclose(outf);
636 1.1 cgd signal(SIGPIPE, SIG_DFL);
637 1.1 cgd signal(SIGQUIT, SIG_DFL);
638 1.1 cgd }
639 1.1 cgd else {
640 1.1 cgd fflush(stdout);
641 1.1 cgd }
642 1.1 cgd
643 1.1 cgd /* trick to force wait on output */
644 1.1 cgd stty(fileno(stdout), &otty);
645 1.1 cgd }
646 1.1 cgd
647 1.1 cgd void
648 1.1 cgd onintr()
649 1.1 cgd {
650 1.1 cgd signal(SIGINT, onintr);
651 1.1 cgd if (mailing)
652 1.1 cgd unlink(fname);
653 1.1 cgd if (sending) {
654 1.1 cgd unlink(fname);
655 1.1 cgd puts("--Killed--");
656 1.1 cgd exit(1);
657 1.1 cgd }
658 1.1 cgd if (printing) {
659 1.1 cgd putchar('\n');
660 1.1 cgd if (hdrs)
661 1.1 cgd exit(0);
662 1.1 cgd sep = "Interrupt";
663 1.1 cgd if (newmsg)
664 1.1 cgd fseek(newmsg, 0L, 2);
665 1.1 cgd intrpflg = YES;
666 1.1 cgd }
667 1.1 cgd }
668 1.1 cgd
669 1.1 cgd /*
670 1.1 cgd * We have just gotten a susp. Suspend and prepare to resume.
671 1.1 cgd */
672 1.1 cgd void
673 1.1 cgd onsusp()
674 1.1 cgd {
675 1.1 cgd
676 1.1 cgd signal(SIGTSTP, SIG_DFL);
677 1.1 cgd sigsetmask(0);
678 1.1 cgd kill(0, SIGTSTP);
679 1.1 cgd signal(SIGTSTP, onsusp);
680 1.1 cgd if (!mailing)
681 1.1 cgd longjmp(tstpbuf, 0);
682 1.1 cgd }
683 1.1 cgd
684 1.1 cgd linecnt(f)
685 1.1 cgd FILE *f;
686 1.1 cgd {
687 1.1 cgd off_t oldpos = ftell(f);
688 1.1 cgd int l = 0;
689 1.1 cgd char lbuf[BUFSIZ];
690 1.1 cgd
691 1.1 cgd while (fgets(lbuf, sizeof lbuf, f))
692 1.1 cgd l++;
693 1.1 cgd clearerr(f);
694 1.1 cgd fseek(f, oldpos, 0);
695 1.1 cgd return (l);
696 1.1 cgd }
697 1.1 cgd
698 1.1 cgd next(buf)
699 1.1 cgd char *buf;
700 1.1 cgd {
701 1.1 cgd int i;
702 1.1 cgd sscanf(buf, "%d", &i);
703 1.1 cgd sprintf(buf, "Goto %d", i);
704 1.1 cgd return(--i);
705 1.1 cgd }
706 1.1 cgd
707 1.1 cgd ask(prompt)
708 1.1 cgd char *prompt;
709 1.1 cgd {
710 1.1 cgd char inch;
711 1.1 cgd int n, cmsg;
712 1.1 cgd off_t oldpos;
713 1.1 cgd FILE *cpfrom, *cpto;
714 1.1 cgd
715 1.1 cgd printf("%s ", prompt);
716 1.1 cgd fflush(stdout);
717 1.1 cgd intrpflg = NO;
718 1.1 cgd (void) fgets(inbuf, sizeof inbuf, stdin);
719 1.1 cgd if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
720 1.1 cgd inbuf[n - 1] = '\0';
721 1.1 cgd if (intrpflg)
722 1.1 cgd inbuf[0] = 'x';
723 1.1 cgd
724 1.1 cgd /*
725 1.1 cgd * Handle 'mail' and 'save' here.
726 1.1 cgd */
727 1.5 mycroft if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
728 1.1 cgd if (inbuf[1] == '-')
729 1.1 cgd cmsg = prevmsg;
730 1.1 cgd else if (isdigit(inbuf[1]))
731 1.1 cgd cmsg = atoi(&inbuf[1]);
732 1.1 cgd else
733 1.1 cgd cmsg = msg;
734 1.1 cgd sprintf(fname, "%s/%d", _PATH_MSGS, cmsg);
735 1.1 cgd
736 1.1 cgd oldpos = ftell(newmsg);
737 1.1 cgd
738 1.1 cgd cpfrom = fopen(fname, "r");
739 1.1 cgd if (!cpfrom) {
740 1.1 cgd printf("Message %d not found\n", cmsg);
741 1.1 cgd ask (prompt);
742 1.1 cgd return;
743 1.1 cgd }
744 1.1 cgd
745 1.1 cgd if (inch == 's') {
746 1.1 cgd in = nxtfld(inbuf);
747 1.1 cgd if (*in) {
748 1.1 cgd for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
749 1.1 cgd fname[n] = in[n];
750 1.1 cgd }
751 1.1 cgd fname[n] = NULL;
752 1.1 cgd }
753 1.1 cgd else
754 1.1 cgd strcpy(fname, "Messages");
755 1.1 cgd }
756 1.1 cgd else {
757 1.1 cgd strcpy(fname, _PATH_TMP);
758 1.1 cgd mktemp(fname);
759 1.1 cgd sprintf(cmdbuf, _PATH_MAIL, fname);
760 1.1 cgd mailing = YES;
761 1.1 cgd }
762 1.1 cgd cpto = fopen(fname, "a");
763 1.1 cgd if (!cpto) {
764 1.1 cgd perror(fname);
765 1.1 cgd mailing = NO;
766 1.1 cgd fseek(newmsg, oldpos, 0);
767 1.1 cgd ask(prompt);
768 1.1 cgd return;
769 1.1 cgd }
770 1.1 cgd
771 1.1 cgd while (n = fread(inbuf, 1, sizeof inbuf, cpfrom))
772 1.1 cgd fwrite(inbuf, 1, n, cpto);
773 1.1 cgd
774 1.1 cgd fclose(cpfrom);
775 1.1 cgd fclose(cpto);
776 1.1 cgd fseek(newmsg, oldpos, 0); /* reposition current message */
777 1.1 cgd if (inch == 's')
778 1.1 cgd printf("Message %d saved in \"%s\"\n", cmsg, fname);
779 1.1 cgd else {
780 1.1 cgd system(cmdbuf);
781 1.1 cgd unlink(fname);
782 1.1 cgd mailing = NO;
783 1.1 cgd }
784 1.1 cgd ask(prompt);
785 1.1 cgd }
786 1.1 cgd }
787 1.1 cgd
788 1.1 cgd gfrsub(infile)
789 1.1 cgd FILE *infile;
790 1.1 cgd {
791 1.1 cgd off_t frompos;
792 1.1 cgd
793 1.1 cgd seensubj = seenfrom = NO;
794 1.1 cgd local = YES;
795 1.1 cgd subj[0] = from[0] = date[0] = NULL;
796 1.1 cgd
797 1.1 cgd /*
798 1.1 cgd * Is this a normal message?
799 1.1 cgd */
800 1.1 cgd if (fgets(inbuf, sizeof inbuf, infile)) {
801 1.1 cgd if (strncmp(inbuf, "From", 4)==0) {
802 1.1 cgd /*
803 1.1 cgd * expected form starts with From
804 1.1 cgd */
805 1.1 cgd seenfrom = YES;
806 1.1 cgd frompos = ftell(infile);
807 1.1 cgd ptr = from;
808 1.1 cgd in = nxtfld(inbuf);
809 1.1 cgd if (*in) while (*in && *in > ' ') {
810 1.1 cgd if (*in == ':' || *in == '@' || *in == '!')
811 1.1 cgd local = NO;
812 1.1 cgd *ptr++ = *in++;
813 1.1 cgd /* what about sizeof from ? */
814 1.1 cgd }
815 1.1 cgd *ptr = NULL;
816 1.1 cgd if (*(in = nxtfld(in)))
817 1.1 cgd strncpy(date, in, sizeof date);
818 1.1 cgd else {
819 1.1 cgd date[0] = '\n';
820 1.1 cgd date[1] = NULL;
821 1.1 cgd }
822 1.1 cgd }
823 1.1 cgd else {
824 1.1 cgd /*
825 1.1 cgd * not the expected form
826 1.1 cgd */
827 1.1 cgd fseek(infile, 0L, 0);
828 1.1 cgd return;
829 1.1 cgd }
830 1.1 cgd }
831 1.1 cgd else
832 1.1 cgd /*
833 1.1 cgd * empty file ?
834 1.1 cgd */
835 1.1 cgd return;
836 1.1 cgd
837 1.1 cgd /*
838 1.1 cgd * look for Subject line until EOF or a blank line
839 1.1 cgd */
840 1.1 cgd while (fgets(inbuf, sizeof inbuf, infile)
841 1.1 cgd && !(blankline = (inbuf[0] == '\n'))) {
842 1.1 cgd /*
843 1.1 cgd * extract Subject line
844 1.1 cgd */
845 1.1 cgd if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
846 1.1 cgd seensubj = YES;
847 1.1 cgd frompos = ftell(infile);
848 1.1 cgd strncpy(subj, nxtfld(inbuf), sizeof subj);
849 1.1 cgd }
850 1.1 cgd }
851 1.1 cgd if (!blankline)
852 1.1 cgd /*
853 1.1 cgd * ran into EOF
854 1.1 cgd */
855 1.1 cgd fseek(infile, frompos, 0);
856 1.1 cgd
857 1.1 cgd if (!seensubj)
858 1.1 cgd /*
859 1.1 cgd * for possible use with Mail
860 1.1 cgd */
861 1.1 cgd strncpy(subj, "(No Subject)\n", sizeof subj);
862 1.1 cgd }
863 1.1 cgd
864 1.1 cgd char *
865 1.1 cgd nxtfld(s)
866 1.1 cgd char *s;
867 1.1 cgd {
868 1.1 cgd if (*s) while (*s && *s > ' ') s++; /* skip over this field */
869 1.1 cgd if (*s) while (*s && *s <= ' ') s++; /* find start of next field */
870 1.1 cgd return (s);
871 1.1 cgd }
872