]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
Fix previous rev.
[empserver] / src / client / servcmd.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  servercmd.c: Change the state depending on the command from the server.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1998
33  */
34
35 #include "misc.h"
36 #include "proto.h"
37 #include "queue.h"
38 #include "ioqueue.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <fcntl.h>
45 #if !defined(_WIN32)
46 #include <unistd.h>
47 #endif
48
49 extern s_char *gettag();
50
51 s_char num_teles[64];
52 static s_char the_prompt[1024];
53 static int mode;
54 static int nbtu;
55 static int nmin;
56 FILE *redir_fp;
57 FILE *pipe_fp;
58 int exec_fd;
59
60 void prompt();
61 void doredir();
62 void dopipe();
63 void doexecute();
64 void output();
65 void screen();
66 int sendeof();
67 int termio();
68 void _noecho();
69
70 extern s_char *SO;
71 extern s_char *SE;
72
73 void
74 servercmd(ioq, auxfi)
75 struct ioqueue *ioq;
76 FILE *auxfi;
77 {
78     s_char *ioq_gets(struct ioqueue *, s_char *, int);
79     s_char buf[1024];
80     s_char *p;
81     int code;
82
83     while (ioq_gets(ioq, buf, sizeof(buf))) {
84         p = buf;
85         while (*p && !isspace(*p))
86             p++;
87         *p++ = 0;
88         if (isalpha(*buf))
89             code = 10 + (*buf - 'a');
90         else
91             code = *buf - '0';
92         switch (code) {
93         case C_PROMPT:
94             if (sscanf(p, "%d %d", &nbtu, &nmin) != 2) {
95                 fprintf(stderr, "prompt: bad server prompt %s\n", p);
96             }
97             mode = code;
98             sprintf(the_prompt, "[%d:%d] Command : ", nbtu, nmin);
99             prompt(auxfi);
100             break;
101         case C_REDIR:
102             doredir(p);
103             break;
104         case C_PIPE:
105             dopipe(p);
106             break;
107         case C_FLUSH:
108             mode = code;
109             sprintf(the_prompt, "%s", p);
110             prompt(auxfi);
111             break;
112         case C_EXECUTE:
113             doexecute(p, auxfi);
114             break;
115         case C_INFORM:
116             if (*p) {
117                 p[strlen(p) - 1] = '\0';
118                 sprintf(num_teles, "(%s) ", p + 1);
119                 if (!redir_fp && !pipe_fp && !exec_fd) {
120                     putchar('\07');
121                     prompt(0);
122                 }
123             } else
124                 *num_teles = '\0';
125             break;
126         default:
127             output(code, p, auxfi);
128             break;
129         }
130     }
131 }
132
133 void
134 prompt(auxfi)
135 FILE *auxfi;
136 {
137     if (mode == C_PROMPT) {
138         if (redir_fp) {
139             (void)fclose(redir_fp);
140             redir_fp = 0;
141         } else if (pipe_fp) {
142 #ifndef _WIN32
143             (void)pclose(pipe_fp);
144 #endif
145             pipe_fp = 0;
146         } else if (exec_fd > 0) {
147             close(exec_fd);
148             close(0);
149             exec_fd = -1;
150             open("/dev/tty", O_RDONLY, 0);
151         }
152     }
153     if (mode == C_PROMPT)
154         printf("\n");
155     printf("%s%s", num_teles, the_prompt);
156     (void)fflush(stdout);
157     if (auxfi) {
158         fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
159         (void)fflush(auxfi);
160     }
161 }
162
163 /*
164  * opens redir_fp if successful
165  */
166 void
167 doredir(p)
168 s_char *p;
169 {
170     s_char *how;
171     s_char *name;
172     s_char *tag;
173     int mode;
174     int fd;
175
176     if (redir_fp) {
177         (void)fclose(redir_fp);
178         redir_fp = 0;
179     }
180     how = p++;
181     if (*p && ((*p == '>') || (*p == '!')))
182         p++;
183     tag = gettag(p);
184     while (*p && isspace(*p))
185         p++;
186     name = p;
187     while (*p && !isspace(*p))
188         p++;
189     *p = 0;
190     if (tag == NULL) {
191         fprintf(stderr, "WARNING!  Server redirected output to file %s\n",
192                 name);
193         return;
194     }
195     mode = O_WRONLY | O_CREAT;
196     if (how[1] == '>')
197         mode |= O_APPEND;
198     else if (how[1] == '!')
199         mode |= O_TRUNC;
200     else
201         mode |= O_EXCL;
202     if (*name == 0) {
203         fprintf(stderr, "Null file name after redirect\n");
204         free(tag);
205         return;
206     }
207     if ((fd = open(name, mode, 0600)) < 0) {
208         fprintf(stderr, "Redirect open failed\n");
209         perror(name);
210     } else {
211         redir_fp = fdopen(fd, "w");
212     }
213     free(tag);
214 }
215
216 /*
217  * opens "pipe_fp" if successful
218  */
219 void
220 dopipe(p)
221 s_char *p;
222 {
223     s_char *tag;
224
225     if (*p == '|')
226         p++;
227     tag = gettag(p);
228     while (*p && isspace(*p))
229         p++;
230     if (tag == NULL) {
231         fprintf(stderr, "WARNING!  Server attempted to run: %s\n", p);
232         return;
233     }
234     if (*p == 0) {
235         fprintf(stderr, "Null program name after redirect\n");
236         free(tag);
237         return;
238     }
239 #ifndef _WIN32
240     if ((pipe_fp = popen(p, "w")) == 0) {
241 #else
242     if (1) {
243 #endif
244         fprintf(stderr, "Pipe open failed\n");
245         perror(p);
246     }
247     free(tag);
248 }
249
250 void
251 doexecute(p, auxfi)
252 s_char *p;
253 FILE *auxfi;
254 {
255     extern int sock;
256     int fd;
257     s_char *tag;
258
259     tag = gettag(p);
260     while (*p && isspace(*p))
261         p++;
262     if (tag == NULL) {
263         fprintf(stderr,
264                 "WARNING!  Server attempted unauthorized read of file %s\n",
265                 p);
266         return;
267     }
268     if (p == 0) {
269         fprintf(stderr, "Null file to execute\n");
270         free(tag);
271         return;
272     }
273 #if !defined(_WIN32)
274     if ((fd = open(p, O_RDONLY, 0)) < 0) {
275 #else
276     if ((fd = open(p, O_RDONLY | O_BINARY, 0)) < 0) {
277 #endif
278         fprintf(stderr, "Can't open execute file\n");
279         perror(p);
280         free(tag);
281         return;
282     }
283     /* copies 4k at a time to the socket */
284     while (termio(fd, sock, auxfi))     /*do copy */
285         ;
286     /* Some platforms don't send the eof (cntl-D) at the end of
287        copying a file.  If emp_client hangs at the end of an
288        execute, include the following line and notify wolfpack
289        of the platform you are using.
290        sendeof(sock);
291      */
292     close(fd);
293     free(tag);
294 }
295
296 void
297 output(code, buf, auxfi)
298 int code;
299 s_char *buf;
300 FILE *auxfi;
301 {
302     switch (code) {
303     case C_NOECHO:
304         _noecho(0);
305         break;
306     case C_FLUSH:
307         (void)fflush(stdout);
308         if (auxfi)
309             (void)fflush(auxfi);
310         break;
311     case C_ABORT:
312         printf("Aborted\n");
313         if (auxfi)
314             fprintf(auxfi, "Aborted\n");
315         break;
316     case C_CMDERR:
317     case C_BADCMD:
318         printf("Error; ");
319         if (auxfi)
320             fprintf(auxfi, "Error; ");
321         break;
322     case C_EXIT:
323         printf("Exit: ");
324         if (auxfi)
325             fprintf(auxfi, "Exit: ");
326         break;
327     case C_FLASH:
328         printf("\n");
329         break;
330     default:
331         break;
332     }
333     if (auxfi) {
334         fprintf(auxfi, "%s", buf);
335         if (code == C_FLUSH)
336             (void)fflush(auxfi);
337         else
338             (void)putc('\n', auxfi);
339     }
340
341     if (redir_fp)
342         fprintf(redir_fp, "%s\n", buf);
343     else if (pipe_fp)
344         fprintf(pipe_fp, "%s\n", buf);
345     else {
346         if (SO && SE)
347             screen(buf);
348         else
349             fputs(buf, stdout);
350         if (code == C_FLUSH)
351             (void)fflush(stdout);
352         else
353             (void)putc('\n', stdout);
354     }
355 }
356
357 void
358 screen(buf)
359 register s_char *buf;
360 {
361     register s_char *sop;
362     register s_char c;
363
364     while ((c = *buf++)) {
365         if (c & 0x80) {
366             for (sop = SO; putc(*sop, stdout); sop++) ;
367             (void)putc(c & 0x7f, stdout);
368             for (sop = SE; putc(*sop, stdout); sop++) ;
369         } else
370             (void)putc(c, stdout);
371     }
372 }