]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
Update copyright notice.
[empserver] / src / client / servcmd.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  *     Ron Koenderink, 2005
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #ifdef _WIN32
44 #include <io.h>
45 #else
46 #include <unistd.h>
47 #endif
48
49 #include "ioqueue.h"
50 #include "misc.h"
51 #include "proto.h"
52 #include "tags.h"
53
54 static char num_teles[64];
55 static char the_prompt[1024];
56 static int mode;
57 static int nbtu;
58 static int nmin;
59 static FILE *redir_fp;
60 static FILE *pipe_fp;
61 static int exec_fd;
62
63 static void prompt(FILE *auxfi);
64 static void doredir(char *p);
65 static void dopipe(char *p);
66 static void doexecute(char *p, FILE *auxfi);
67 static void output(int code, char *buf, FILE *auxfi, int eol);
68 static void screen(char *buf);
69
70 void
71 servercmd(struct ioqueue *ioq, FILE *auxfi)
72 {
73     char buf[1024];
74     char *p;
75     static int code = -1;
76     int eol;
77
78     while (ioq_gets(ioq, buf, sizeof(buf), &eol)) {
79         p = buf;
80         if (code == -1) {
81             if (isalpha(*buf))
82                 code = 10 + (*buf - 'a');
83             else
84                 code = *buf - '0';
85             while (*p && !isspace(*p))
86                 p++;
87             *p++ = 0;
88         }
89         /*
90          * FIXME
91          * C_REDIR, C_PIPE, and C_EXECUTE will not
92          * work with filename longer than one buffer
93          */
94         switch (code) {
95         case C_PROMPT:
96             if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
97                 fprintf(stderr, "prompt: bad server prompt %s\n", p);
98             }
99             mode = code;
100             sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
101             prompt(auxfi);
102             break;
103         case C_REDIR:
104             if (eol)
105                 p[strlen(p) - 1] = '\0';
106             doredir(p);
107             break;
108         case C_PIPE:
109             if (eol)
110                 p[strlen(p) - 1] = '\0';
111             dopipe(p);
112             break;
113         case C_FLUSH:
114             mode = code;
115             if (eol)
116                 p[strlen(p) - 1] = '\0';
117             sprintf(the_prompt, "%s", p);
118             prompt(auxfi);
119             break;
120         case C_EXECUTE:
121             if (eol)
122                 p[strlen(p) - 1] = '\0';
123             doexecute(p, auxfi);
124             break;
125         case C_INFORM:
126             if (eol)
127                 p[strlen(p) - 1] = '\0';
128             if (*p) {
129                 p[strlen(p) - 1] = '\0';
130                 sprintf(num_teles, "(%s) ", p + 1);
131                 if (!redir_fp && !pipe_fp && !exec_fd) {
132                     putchar('\07');
133                     prompt(NULL);
134                 }
135             } else
136                 *num_teles = '\0';
137             break;
138         default:
139             output(code, p, auxfi, eol);
140             break;
141         }
142         if (eol)
143             code = -1;
144     }
145 }
146
147 static void
148 prompt(FILE *auxfi)
149 {
150     if (mode == C_PROMPT) {
151         if (redir_fp) {
152             (void)fclose(redir_fp);
153             redir_fp = NULL;
154         } else if (pipe_fp) {
155             (void)pclose(pipe_fp);
156             pipe_fp = NULL;
157         } else if (exec_fd > 0) {
158             close(exec_fd);
159             close(0);
160             exec_fd = -1;
161             open("/dev/tty", O_RDONLY, 0);
162         }
163     }
164     if (mode == C_PROMPT)
165         printf("\n");
166     printf("%s%s", num_teles, the_prompt);
167     (void)fflush(stdout);
168     if (auxfi) {
169         fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
170         (void)fflush(auxfi);
171     }
172 }
173
174 /*
175  * opens redir_fp if successful
176  */
177 static void
178 doredir(char *p)
179 {
180     char *how;
181     char *name;
182     char *tag;
183     int mode;
184     int fd;
185
186     if (redir_fp) {
187         (void)fclose(redir_fp);
188         redir_fp = NULL;
189     }
190     how = p++;
191     if (*p && ((*p == '>') || (*p == '!')))
192         p++;
193     tag = gettag(p);
194     while (*p && isspace(*p))
195         p++;
196     name = p;
197     while (*p && !isspace(*p))
198         p++;
199     *p = 0;
200     if (tag == NULL) {
201         fprintf(stderr, "WARNING!  Server redirected output to file %s\n",
202                 name);
203         return;
204     }
205     mode = O_WRONLY | O_CREAT;
206     if (how[1] == '>')
207         mode |= O_APPEND;
208     else if (how[1] == '!')
209         mode |= O_TRUNC;
210     else
211         mode |= O_EXCL;
212     if (*name == 0) {
213         fprintf(stderr, "Null file name after redirect\n");
214         free(tag);
215         return;
216     }
217     if ((fd = open(name, mode, 0600)) < 0) {
218         fprintf(stderr, "Redirect open failed\n");
219         perror(name);
220     } else {
221         redir_fp = fdopen(fd, "w");
222     }
223     free(tag);
224 }
225
226 /*
227  * opens "pipe_fp" if successful
228  */
229 static void
230 dopipe(char *p)
231 {
232     char *tag;
233
234     if (*p == '|')
235         p++;
236     tag = gettag(p);
237     while (*p && isspace(*p))
238         p++;
239     if (tag == NULL) {
240         fprintf(stderr, "WARNING!  Server attempted to run: %s\n", p);
241         return;
242     }
243     if (*p == 0) {
244         fprintf(stderr, "Null program name after redirect\n");
245         free(tag);
246         return;
247     }
248     if ((pipe_fp = popen(p, "w")) == NULL) {
249         fprintf(stderr, "Pipe open failed\n");
250         perror(p);
251     }
252     free(tag);
253 }
254
255 static void
256 doexecute(char *p, FILE *auxfi)
257 {
258     int fd;
259     char *tag;
260
261     tag = gettag(p);
262     while (*p && isspace(*p))
263         p++;
264     if (tag == NULL) {
265         fprintf(stderr,
266                 "WARNING!  Server attempted unauthorized read of file %s\n",
267                 p);
268         return;
269     }
270     if (p == NULL) {
271         fprintf(stderr, "Null file to execute\n");
272         free(tag);
273         return;
274     }
275     if ((fd = open(p, O_RDONLY, 0)) < 0) {
276         fprintf(stderr, "Can't open execute file\n");
277         perror(p);
278         free(tag);
279         return;
280     }
281     /* copies 4k at a time to the socket */
282     while (termio(fd, sock, auxfi))     /*do copy */
283         ;
284     /*
285      * Some platforms don't send the eof (cntl-D) at the end of
286      * copying a file.  If emp_client hangs at the end of an
287      * execute, include the following line and notify wolfpack
288      * of the platform you are using.
289      * sendeof(sock);
290      */
291     close(fd);
292     free(tag);
293 }
294
295 static void
296 output(int code, char *buf, FILE *auxfi, int eol)
297 {
298     switch (code) {
299     case C_NOECHO:
300         /* not implemented; serve doesn't send it */
301         break;
302     case C_ABORT:
303         printf("Aborted\n");
304         if (auxfi)
305             fprintf(auxfi, "Aborted\n");
306         break;
307     case C_CMDERR:
308     case C_BADCMD:
309         printf("Error; ");
310         if (auxfi)
311             fprintf(auxfi, "Error; ");
312         break;
313     case C_EXIT:
314         printf("Exit: ");
315         if (auxfi)
316             fprintf(auxfi, "Exit: ");
317         break;
318     case C_FLASH:
319         printf("\n");
320         break;
321     default:
322         break;
323     }
324     if (auxfi) {
325         fprintf(auxfi, "%s", buf);
326     }
327
328     if (redir_fp)
329         fprintf(redir_fp, "%s", buf);
330     else if (pipe_fp)
331         fprintf(pipe_fp, "%s", buf);
332     else {
333         screen(buf);
334     }
335 }
336
337 static void
338 screen(char *buf)
339 {
340     char c;
341
342     while ((c = *buf++)) {
343         if (eight_bit_clean) {
344             if (c == 14)
345                 putso();
346             else if (c == 15)
347                 putse();
348             else
349                 putchar(c);
350         } else if (c & 0x80) {
351             putso();
352             putchar(c & 0x7f);
353             putse();
354         } else
355             putchar(c);
356     }
357 }