]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
mh-sequence.man: document new '=+' and '=-' for selecting relative msgs
[nmh] / uip / rcvtty.c
1
2 /*
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
4 *
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.
8 */
9
10 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
11 * a number of other things also exist. Please check.
12 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
13 */
14
15 #include <h/mh.h>
16 #include <h/signals.h>
17 #include <setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
20 #include <h/tws.h>
21 #include <h/mts.h>
22 #include <fcntl.h>
23
24 #ifdef HAVE_GETUTXENT
25 #include <utmpx.h>
26 #endif /* HAVE_GETUTXENT */
27
28 #define SCANFMT \
29 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
30 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
31 %{subject}%<{body}<<%{body}>>%>"
32
33 #define RCVTTY_SWITCHES \
34 X("biff", 0, BIFFSW) \
35 X("form formatfile", 0, FORMSW) \
36 X("format string", 5, FMTSW) \
37 X("width columns", 0, WIDTHSW) \
38 X("newline", 0, NLSW) \
39 X("nonewline", 0, NNLSW) \
40 X("bell", 0, BELSW) \
41 X("nobell", 0, NBELSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(RCVTTY);
47 #undef X
48
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(RCVTTY, switches);
51 #undef X
52
53 static jmp_buf myctx;
54 static int bell = 1;
55 static int newline = 1;
56 static int biff = 0;
57 static int width = 0;
58 static char *form = NULL;
59 static char *format = NULL;
60
61 /*
62 * external prototypes
63 */
64 char *getusername(void);
65
66 /*
67 * static prototypes
68 */
69 static void alrmser (int);
70 static int message_fd (char **);
71 static int header_fd (void);
72 static void alert (char *, int);
73
74
75 int
76 main (int argc, char **argv)
77 {
78 int md, vecp = 0;
79 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
80 char **argp, **arguments, *vec[MAXARGS];
81 struct utmpx *utp;
82 #ifdef LOCALE
83 setlocale(LC_ALL, "");
84 #endif
85 invo_name = r1bindex (argv[0], '/');
86
87 /* read user profile/context */
88 context_read();
89
90 mts_init (invo_name);
91 arguments = getarguments (invo_name, argc, argv, 1);
92 argp = arguments;
93
94 while ((cp = *argp++)) {
95 if (*cp == '-') {
96 switch (smatch (++cp, switches)) {
97 case AMBIGSW:
98 ambigsw (cp, switches);
99 done (1);
100 case UNKWNSW:
101 vec[vecp++] = --cp;
102 continue;
103
104 case HELPSW:
105 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
106 print_help (buf, switches, 1);
107 done (0);
108 case VERSIONSW:
109 print_version(invo_name);
110 done (0);
111
112 case BIFFSW:
113 biff = 1;
114 continue;
115
116 case FORMSW:
117 if (!(form = *argp++) || *form == '-')
118 adios (NULL, "missing argument to %s", argp[-2]);
119 format = NULL;
120 continue;
121 case FMTSW:
122 if (!(format = *argp++) || *format == '-')
123 adios (NULL, "missing argument to %s", argp[-2]);
124 form = NULL;
125 continue;
126
127 case WIDTHSW:
128 if (!(cp = *argp++) || *cp == '-')
129 adios(NULL, "missing argument to %s", argp[-2]);
130 width = atoi(cp);
131 continue;
132 case NLSW:
133 newline = 1;
134 continue;
135 case NNLSW:
136 newline = 0;
137 continue;
138 case BELSW:
139 bell = 1;
140 continue;
141 case NBELSW:
142 bell = 0;
143 continue;
144
145 }
146 }
147 vec[vecp++] = cp;
148 }
149 vec[vecp] = 0;
150
151 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
152 exit (RCV_MBX);
153
154 user = getusername();
155
156 #if HAVE_GETUTXENT
157 setutxent();
158 while ((utp = getutxent()) != NULL) {
159 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
160 && utp->ut_line[0] != 0
161 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
162 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
163 alert (tty, md);
164 }
165 }
166 endutxent();
167 #endif /* HAVE_GETUTXENT */
168
169 exit (RCV_MOK);
170 return 0; /* dead code to satisfy the compiler */
171 }
172
173
174 static void
175 alrmser (int i)
176 {
177 NMH_UNUSED (i);
178
179 longjmp (myctx, 1);
180 }
181
182
183 static int
184 message_fd (char **vec)
185 {
186 pid_t child_id;
187 int bytes, fd, seconds;
188 char tmpfil[BUFSIZ];
189 struct stat st;
190
191 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
192 unlink (tmpfil);
193
194 if ((child_id = fork()) == NOTOK) {
195 /* fork error */
196 close (fd);
197 return header_fd ();
198 } else if (child_id) {
199 /* parent process */
200 if (!setjmp (myctx)) {
201 SIGNAL (SIGALRM, alrmser);
202 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
203
204 /* amount of time to wait depends on message size */
205 if (bytes <= 100) {
206 /* give at least 5 minutes */
207 seconds = 300;
208 } else if (bytes >= 90000) {
209 /* but 30 minutes should be long enough */
210 seconds = 1800;
211 } else {
212 seconds = (bytes / 60) + 300;
213 }
214 alarm ((unsigned int) seconds);
215 pidwait(child_id, OK);
216 alarm (0);
217
218 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
219 return fd;
220 } else {
221 /*
222 * Ruthlessly kill the child and anything
223 * else in its process group.
224 */
225 killpg(child_id, SIGKILL);
226 }
227 close (fd);
228 return header_fd ();
229 }
230
231 /* child process */
232 rewind (stdin);
233 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
234 _exit (-1);
235 closefds (3);
236 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
237 execvp (vec[0], vec);
238 _exit (-1);
239 return 1; /* dead code to satisfy compiler */
240 }
241
242
243 static int
244 header_fd (void)
245 {
246 int fd;
247 char *nfs;
248 char *tfile = NULL;
249
250 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
251 if (tfile == NULL) return NOTOK;
252 unlink (tfile);
253
254 rewind (stdin);
255
256 /* get new format string */
257 nfs = new_fs (form, format, SCANFMT);
258 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
259 scan_finished ();
260 if (newline)
261 write (fd, "\n\r", 2);
262 write (fd, scanl, strlen (scanl));
263 if (bell)
264 write (fd, "\007", 1);
265
266 return fd;
267 }
268
269
270 static void
271 alert (char *tty, int md)
272 {
273 int i, td, mask;
274 char buffer[BUFSIZ], ttyspec[BUFSIZ];
275 struct stat st;
276
277 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
278
279 /*
280 * The mask depends on whether we are checking for
281 * write permission based on `biff' or `mesg'.
282 */
283 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
284 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
285 return;
286
287 if (!setjmp (myctx)) {
288 SIGNAL (SIGALRM, alrmser);
289 alarm (2);
290 td = open (ttyspec, O_WRONLY);
291 alarm (0);
292 if (td == NOTOK)
293 return;
294 } else {
295 alarm (0);
296 return;
297 }
298
299 lseek (md, (off_t) 0, SEEK_SET);
300
301 while ((i = read (md, buffer, sizeof(buffer))) > 0)
302 if (write (td, buffer, i) != i)
303 break;
304
305 close (td);
306 }
307