src/client/login.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  login.c: Log into an empire server
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1989
00032  *     Steve McClure, 1998
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include <ctype.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041 #if !defined(_WIN32)
00042 #include <unistd.h>
00043 #endif
00044 #include "misc.h"
00045 #include "proto.h"
00046 
00047 int
00048 login(int s, char *uname, char *cname, char *cpass,
00049       int kill_proc, int utf8)
00050 {
00051     char tmp[128];
00052     char buf[1024];
00053     char *ptr;
00054     char *p;
00055     int len, code;
00056 
00057     if (!expect(s, C_INIT, buf))
00058         return 0;
00059     sendcmd(s, "user", uname);
00060     if (!expect(s, C_CMDOK, buf)) {
00061         fprintf(stderr, "Server rejects your user name\n");
00062         return 0;
00063     }
00064     if (utf8) {
00065         sendcmd(s, "options", "utf-8");
00066         for (;;) {
00067             code = recvline(s, buf);
00068             if (code == C_CMDOK)
00069                 break;
00070             if (code != C_DATA) {
00071                 fprintf(stderr, "Server doesn't support UTF-8\n");
00072                 return 0;
00073             }
00074         }
00075     }
00076     if (cname == NULL) {
00077         (void)printf("Country name? ");
00078         fflush(stdout);
00079         cname = fgets(tmp, sizeof(tmp), stdin);
00080         if (cname == NULL || *cname == 0)
00081             return 0;
00082     }
00083     len = strlen(cname);
00084     if (cname[len-1] == '\n')
00085         cname[len-1] = 0;
00086     sendcmd(s, "coun", cname);
00087     if (!expect(s, C_CMDOK, buf)) {
00088         (void)fprintf(stderr, "No such country\n");
00089         return 0;
00090     }
00091     if (cpass == NULL) {
00092         cpass = getpass("Your name? ");
00093         if (cpass == NULL || *cpass == 0)
00094             return 0;
00095     }
00096     (void)printf("\n");
00097     sendcmd(s, "pass", cpass);
00098     memset(cpass, 0, strlen(cpass));    /* for core dumps */
00099     if (!expect(s, C_CMDOK, buf)) {
00100         (void)fprintf(stderr, "Bad password\n");
00101         return 0;
00102     }
00103     if (kill_proc) {
00104         sendcmd(s, "kill", NULL);
00105         (void)printf("\n\t-=O=-\n");
00106         (void)expect(s, C_EXIT, buf);
00107         fprintf(stderr, "%s\n", buf);
00108         return 0;
00109     }
00110     sendcmd(s, "play", NULL);
00111     (void)printf("\n\t-=O=-\n");
00112     if (!expect(s, C_INIT, buf)) {
00113         fprintf(stderr, "%s\n", buf);
00114         return 0;
00115     }
00116     for (ptr = buf; !isspace(*ptr); ptr++) ;
00117     ptr++;
00118     p = strchr(ptr, '\n');
00119     if (p != NULL)
00120         *p = 0;
00121     if (atoi(ptr) != CLIENTPROTO) {
00122         printf("Empire client out of date; get new version!\n");
00123         printf("   this version: %d, current version: %d\n",
00124                CLIENTPROTO, atoi(ptr));
00125     }
00126     fflush(stdout);
00127     return 1;
00128 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2