00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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));
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 }