]> git.pond.sub.org Git - empserver/blob - src/client/login.c
Update copyright notice
[empserver] / src / client / login.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  login.c: Log into an empire server
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2004-2009
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #ifdef _WIN32
44 #include <windows.h>
45 #endif
46 #include "misc.h"
47 #include "proto.h"
48
49 #ifndef HAVE_GETPASS
50 #define getpass ersatz_getpass
51 static char *
52 ersatz_getpass(char *prompt)
53 {
54     static char buf[128];
55     char *p;
56     size_t len;
57 #ifdef _WIN32
58     DWORD mode;
59     HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
60
61     if (GetConsoleMode(input_handle, &mode))
62         SetConsoleMode(input_handle, mode & ~ENABLE_ECHO_INPUT);
63     else
64 #endif
65         printf("Note: your input is echoed to the screen\n");
66     printf("%s", prompt);
67     fflush(stdout);
68     p = fgets(buf, sizeof(buf), stdin);
69 #ifdef _WIN32
70     if (GetConsoleMode(input_handle, &mode))
71         SetConsoleMode(input_handle, mode | ENABLE_ECHO_INPUT);
72 #endif
73     if (!p)
74         return NULL;
75     len = strlen(p);
76     if (p[len - 1] == '\n')
77         p[len - 1] = 0;
78     return p;
79 }
80 #endif
81
82 int
83 login(int s, char *uname, char *cname, char *cpass,
84       int kill_proc, int utf8)
85 {
86     char tmp[128];
87     char buf[1024];
88     char *ptr;
89     char *p;
90     int len, code;
91
92     if (!expect(s, C_INIT, buf))
93         return 0;
94     sendcmd(s, "user", uname);
95     if (!expect(s, C_CMDOK, buf)) {
96         fprintf(stderr, "Server rejects your user name\n");
97         return 0;
98     }
99     if (utf8) {
100         sendcmd(s, "options", "utf-8");
101         for (;;) {
102             code = recvline(s, buf);
103             if (code == C_CMDOK)
104                 break;
105             if (code != C_DATA) {
106                 fprintf(stderr, "Server doesn't support UTF-8\n");
107                 return 0;
108             }
109         }
110     }
111     if (cname == NULL) {
112         (void)printf("Country name? ");
113         fflush(stdout);
114         cname = fgets(tmp, sizeof(tmp), stdin);
115         if (cname == NULL || *cname == 0)
116             return 0;
117     }
118     len = strlen(cname);
119     if (cname[len-1] == '\n')
120         cname[len-1] = 0;
121     sendcmd(s, "coun", cname);
122     if (!expect(s, C_CMDOK, buf)) {
123         (void)fprintf(stderr, "No such country\n");
124         return 0;
125     }
126     if (cpass == NULL) {
127         cpass = getpass("Your name? ");
128         if (cpass == NULL || *cpass == 0)
129             return 0;
130     }
131     (void)printf("\n");
132     sendcmd(s, "pass", cpass);
133     memset(cpass, 0, strlen(cpass));    /* for core dumps */
134     if (!expect(s, C_CMDOK, buf)) {
135         (void)fprintf(stderr, "Bad password\n");
136         return 0;
137     }
138     if (kill_proc) {
139         sendcmd(s, "kill", NULL);
140         (void)printf("\n\t-=O=-\n");
141         (void)expect(s, C_EXIT, buf);
142         fprintf(stderr, "%s\n", buf);
143         return 0;
144     }
145     sendcmd(s, "play", NULL);
146     (void)printf("\n\t-=O=-\n");
147     if (!expect(s, C_INIT, buf)) {
148         fprintf(stderr, "%s\n", buf);
149         return 0;
150     }
151     for (ptr = buf; !isspace(*ptr); ptr++) ;
152     ptr++;
153     p = strchr(ptr, '\n');
154     if (p != NULL)
155         *p = 0;
156     if (atoi(ptr) != CLIENTPROTO) {
157         printf("Empire client out of date; get new version!\n");
158         printf("   this version: %d, current version: %d\n",
159                CLIENTPROTO, atoi(ptr));
160     }
161     fflush(stdout);
162     return 1;
163 }