Debian Codegraph @

src/client/bnchat.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected])
3 * Copyright (C) 1999 Oleg Drokin ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #include <cstring>
21 #include <cstdio>
22 #include <cctype>
23 #include <cstdarg>
24 #include <cstdlib>
25
26 #ifdef WIN32
27 # include <conio.h>
28 #endif
29 #ifdef HAVE_TERMIOS_H
30 # include <termios.h>
31 #endif
32 #ifdef HAVE_SIGACTION
33 # include <signal.h>
34 #endif
35
36 #include "compat/psock.h"
37 #include "compat/termios.h"
38 #include "compat/strcasecmp.h"
39 #include "common/field_sizes.h"
40 #include "common/bnet_protocol.h"
41 #include "common/packet.h"
42 #include "common/tag.h"
43 #include "common/file_protocol.h"
44 #include "common/bn_type.h"
45 #include "common/util.h"
46 #include "common/init_protocol.h"
47 #include "common/bnethash.h"
48 #include "common/bnethashconv.h"
49 #include "common/xalloc.h"
50 #include "common/network.h"
51 #include "common/hexdump.h"
52 #include "common/version.h"
53 #include "common/xstring.h"
54 #ifdef CLIENTDEBUG
55 # include "common/eventlog.h"
56 #endif
57 #include "ansi_term.h"
58 #include "client_connect.h"
59 #include "client.h"
60 #include "common/setup_after.h"
61
62
63 #ifdef CLIENTDEBUG
64 # define dprintf printf
65 #else
66 # define dprintf if (0) printf
67 #endif
68
69 #define CHANNEL_BNCHATBOT "Chat"
70 #define CHANNEL_STARCRAFT "Starcraft"
71 #define CHANNEL_BROODWARS "Brood War"
72 #define CHANNEL_SHAREWARE "Starcraft Shareware"
73 #define CHANNEL_DIABLORTL "Diablo Retail"
74 #define CHANNEL_DIABLOSHR "Diablo Shareware" /* FIXME: is this one right? */
75 #define CHANNEL_WARCIIBNE "War2BNE"
76 #define CHANNEL_DIABLO2DV "Diablo II"
77 #define CHANNEL_DIABLO2XP "Diablo II"
78 #define CHANNEL_WARCRAFT3 "W3"
79 #define CHANNEL_WAR3XP "W3"
80
81 using namespace pvpgn;
82 using namespace pvpgn::client;
83
84 namespace
85 {
86
87 volatile int handle_winch = 0;
88
89 typedef enum {
90 mode_chat,
91 mode_command,
92 mode_waitstat,
93 mode_waitgames,
94 mode_waitladder,
95 mode_gamename,
96 mode_gamepass,
97 mode_gamewait,
98 mode_gamestop,
99 mode_claninvite,
100 mode_clancreateinvite
101 } t_mode;
102
103 typedef struct _client_state
104 {
105 int useansi;
106 int sd;
107 struct sockaddr_in saddr;
108 unsigned int sessionkey;
109 unsigned int sessionnum;
110 unsigned int currsize;
111 unsigned int commpos;
112 struct termios in_attr_old;
113 struct termios in_attr_new;
114 int changed_in;
115 int fd_stdin;
116 unsigned int screen_width, screen_height;
117 int munged;
118 t_mode mode;
119 char text[MAX_MESSAGE_LEN];
120 } t_client_state;
121
122 typedef struct _user_info
123 {
124 char const * clienttag;
125 char const * archtag;
126 char const * gamelang;
127 char player[MAX_MESSAGE_LEN];
128 char const * cdowner;
129 char const * cdkey;
130 char const * channel;
131 char curr_gamename[MAX_GAMENAME_LEN];
132 char curr_gamepass[MAX_GAMEPASS_LEN];
133 int count, clantag;
134 char const * inviter;
135 int ignoreversion;
136
137 } t_user_info;
138
139
140 char const * mflags_get_str(unsigned int flags);
141 char const * cflags_get_str(unsigned int flags);
142 char const * mode_get_prompt(t_mode mode);
143 int print_file(struct sockaddr_in * saddr, char const * filename, char const * progname, char const * clienttag);
144 void usage(char const * progname);
145 #ifdef HAVE_SIGACTION
146 void winch_sig_handle(int unused);
147 #endif
148
149
150 #ifdef HAVE_SIGACTION
151 void winch_sig_handle(int unused)
152 {
153 handle_winch = 1;
154 }
155 #endif
156
157
158 char const * mflags_get_str(unsigned int flags)
159 {
160 static char buffer[32];
161
162 buffer[0] = buffer[1] = '\0';
163 if (flags&MF_BLIZZARD)
164 std::strcat(buffer, ",Blizzard");
165 if (flags&MF_GAVEL)
166 std::strcat(buffer, ",Gavel");
167 if (flags&MF_VOICE)
168 std::strcat(buffer, ",Megaphone");
169 if (flags&MF_BNET)
170 std::strcat(buffer, ",BNET");
171 if (flags&MF_PLUG)
172 std::strcat(buffer, ",Plug");
173 if (flags&MF_X)
174 std::strcat(buffer, ",X");
175 if (flags&MF_SHADES)
176 std::strcat(buffer, ",Shades");
177 if (flags&MF_PGLPLAY)
178 std::strcat(buffer, ",PGL_Player");
179 if (flags&MF_PGLOFFL)
180 std::strcat(buffer, ",PGL_Official");
181 buffer[0] = '[';
182 std::strcat(buffer, "]");
183
184 return buffer;
185 }
186
187
188 char const * cflags_get_str(unsigned int flags)
189 {
190 static char buffer[32];
191
192 buffer[0] = buffer[1] = '\0';
193 if (flags&CF_PUBLIC)
194 std::strcat(buffer, ",Public");
195 if (flags&CF_MODERATED)
196 std::strcat(buffer, ",Moderated");
197 if (flags&CF_RESTRICTED)
198 std::strcat(buffer, ",Restricted");
199 if (flags&CF_THEVOID)
200 std::strcat(buffer, ",The Void");
201 if (flags&CF_SYSTEM)
202 std::strcat(buffer, ",System");
203 if (flags&CF_OFFICIAL)
204 std::strcat(buffer, ",Official");
205 buffer[0] = '[';
206 std::strcat(buffer, "]");
207
208 return buffer;
209 }
210
211
212 char const * mode_get_prompt(t_mode mode)
213 {
214 switch (mode)
215 {
216 case mode_chat:
217 return "] ";
218 case mode_command:
219 return "command> ";
220 case mode_waitstat:
221 case mode_waitgames:
222 case mode_waitladder:
223 case mode_gamewait:
224 return "*please wait* ";
225 case mode_gamename:
226 return "Name: ";
227 case mode_gamepass:
228 return "Password: ";
229 case mode_gamestop:
230 return "[Return to kill game] ";
231 default:
232 return "? ";
233 }
234 }
235
236
237 int print_file(struct sockaddr_in * saddr, char const * filename, char const * progname, char const * clienttag)
238 {
239 int sd;
240 t_packet * ipacket;
241 t_packet * packet;
242 t_packet * rpacket;
243 t_packet * fpacket;
244 unsigned int currsize;
245 unsigned int filelen;
246
247 if (!saddr || !filename || !progname)
248 return -1;
249
250 if ((sd = psock_socket(PSOCK_PF_INET, PSOCK_SOCK_STREAM, PSOCK_IPPROTO_TCP)) < 0)
251 {
252 std::fprintf(stderr, "%s: could not create socket (psock_socket: %s)\n", progname, std::strerror(psock_errno()));
253 return -1;
254 }
255
256 if (psock_connect(sd, (struct sockaddr *)saddr, sizeof(*saddr)) < 0)
257 {
258 std::fprintf(stderr, "%s: could not connect to server (psock_connect: %s)\n", progname, std::strerror(psock_errno()));
259 return -1;
260 }
261
262 if (!(ipacket = packet_create(packet_class_init)))
263 {
264 std::fprintf(stderr, "%s: could not create packet\n", progname);
265 return -1;
266 }
267 bn_byte_set(&ipacket->u.client_initconn.cclass, CLIENT_INITCONN_CLASS_FILE);
268 client_blocksend_packet(sd, ipacket);
269 packet_del_ref(ipacket);
270
271 if (!(rpacket = packet_create(packet_class_file)))
272 {
273 std::fprintf(stderr, "%s: could not create packet\n", progname);
274 return -1;
275 }
276
277 if (!(fpacket = packet_create(packet_class_raw)))
278 {
279 std::fprintf(stderr, "%s: could not create packet\n", progname);
280 return -1;
281 }
282
283 if (!(packet = packet_create(packet_class_file)))
284 {
285 std::fprintf(stderr, "%s: could not create packet\n", progname);
286 return -1;
287 }
288 packet_set_size(packet, sizeof(t_client_file_req));
289 packet_set_type(packet, CLIENT_FILE_REQ);
290 bn_int_tag_set(&packet->u.client_file_req.archtag, ARCHTAG_WINX86);
291 bn_int_tag_set(&packet->u.client_file_req.clienttag, clienttag);
292 bn_int_set(&packet->u.client_file_req.adid, 0);
293 bn_int_set(&packet->u.client_file_req.extensiontag, 0);
294 bn_int_set(&packet->u.client_file_req.startoffset, 0);
295 bn_long_set_a_b(&packet->u.client_file_req.timestamp, 0x00000000, 0x00000000);
296 packet_append_string(packet, filename);
297 client_blocksend_packet(sd, packet);
298 packet_del_ref(packet);
299
300 do
301 if (client_blockrecv_packet(sd, rpacket) < 0)
302 {
303 std::fprintf(stderr, "%s: server closed file connection\n", progname);
304 packet_del_ref(fpacket);
305 packet_del_ref(rpacket);
306 return -1;
307 }
308 while (packet_get_type(rpacket) != SERVER_FILE_REPLY);
309
310 filelen = bn_int_get(rpacket->u.server_file_reply.filelen);
311 packet_del_ref(rpacket);
312
313 for (currsize = 0; currsize + MAX_PACKET_SIZE <= filelen; currsize += MAX_PACKET_SIZE)
314 {
315 if (client_blockrecv_raw_packet(sd, fpacket, MAX_PACKET_SIZE) < 0)
316 {
317 std::fflush(stdout);
318 std::fprintf(stderr, "%s: server closed file connection\n", progname);
319 packet_del_ref(fpacket);
320 return -1;
321 }
322 str_print_term(stdout, (const char*)packet_get_raw_data_const(fpacket, 0), MAX_PACKET_SIZE, 1);
323 }
324 filelen -= currsize;
325 if (filelen)
326 {
327 if (client_blockrecv_raw_packet(sd, fpacket, filelen) < 0)
328 {
329 std::fflush(stdout);
330 std::fprintf(stderr, "%s: server closed file connection\n", progname);
331 packet_del_ref(fpacket);
332 return -1;
333 }
334 str_print_term(stdout, (const char*)packet_get_raw_data_const(fpacket, 0), filelen, 1);
335 }
336 std::fflush(stdout);
337
338 psock_close(sd);
339
340 packet_del_ref(fpacket);
341
342 return 0;
343 }
344
345
346 void usage(char const * progname)
347 {
348 std::fprintf(stderr, "usage: %s [<options>] [<servername> [<TCP portnumber>]]\n", progname);
349 std::fprintf(stderr,
350 " -a, --ansi-color use ANSI colors\n"
351 " -n, --new-account create a new account\n"
352 " -c, --change-password change account password\n"
353 " --client=CHAT report client as a chat bot\n"
354 " -b, --client=SEXP report client as Brood Wars\n"
355 " -d, --client=DRTL report client as Diablo Retail\n"
356 " --client=DSHR report client as Diablo Shareware\n");
357 std::fprintf(stderr,
358 " -s, --client=STAR report client as Starcraft (default)\n"
359 " --client=SSHR report client as Starcraft Shareware\n"
360 " -w, --client=W2BN report client as Warcraft II BNE\n"
361 " --client=D2DV report client as Diablo II\n"
362 " --client=D2XP report client as Diablo II: LoD\n"
363 " --client=WAR3 report client as Warcraft III\n"
364 " --client=W3XP report client as Warcraft III Frozen Throne\n"
365 " --arch=IX86 report architecture as Intel x86 (default)\n"
366 " --arch=PMAC report architecture as PowerPC MacOS\n"
367 " --arch=XMAC report architecture as PowerPC MacOSX\n");
368 std::fprintf(stderr,
369 " -o NAME, --owner=NAME report CD owner as NAME\n"
370 " -k KEY, --cdkey=KEY report CD key as KEY\n"
371 " -l LANG --lang=LANG report language as LANG (default \"enUS\")\n"
372 " -i, --ignore-version ignore version request (do not send game version, CD owner/key)\n"
373 " -h, --help, --usage show this information and exit\n"
374 " -v, --version print version number and exit\n");
375 std::exit(EXIT_FAILURE);
376 }
377
378 int read_commandline(int argc, char * * argv,
379 char const * * servname, unsigned short * servport,
380 char const * * clienttag,
381 char const * * archtag,
382 int * changepass, int * newacct,
383 char const * * channel,
384 char const * * cdowner,
385 char const * * cdkey,
386 char const * * gamelang,
387 int * ignoreversion,
388 int * useansi)
389 {
390 int a;
391
392 if (argc < 1 || !argv || !argv[0])
393 {
394 std::fprintf(stderr, "bad arguments\n");
395 return EXIT_FAILURE;
396 }
397
398 for (a = 1; a < argc; a++)
399 if (*servname && std::isdigit((int)argv[a][0]) && a + 1 >= argc)
400 {
401 if (str_to_ushort(argv[a], servport) < 0)
402 {
403 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]);
404 usage(argv[0]);
405 }
406 }
407 else if (!(*servname) && argv[a][0] != '-' && a + 2 >= argc)
408 *servname = argv[a];
409 else if (std::strcmp(argv[a], "-a") == 0 || std::strcmp(argv[a], "--use-ansi") == 0)
410 *useansi = 1;
411 else if (std::strcmp(argv[a], "-n") == 0 || std::strcmp(argv[a], "--new-account") == 0)
412 {
413 if (*changepass)
414 {
415 std::fprintf(stderr, "%s: can not create new account when changing passwords\n", argv[0]);
416 usage(argv[0]);
417 }
418 *newacct = 1;
419 }
420 else if (std::strcmp(argv[a], "-c") == 0 || std::strcmp(argv[a], "--change-password") == 0)
421 {
422 if (*newacct)
423 {
424 std::fprintf(stderr, "%s: can not change passwords when creating a new account\n", argv[0]);
425 usage(argv[0]);
426 }
427 *changepass = 1;
428 }
429 else if (std::strcmp(argv[a], "--client=CHAT") == 0)
430 {
431 if (*clienttag)
432 {
433 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
434 usage(argv[0]);
435 }
436 *clienttag = CLIENTTAG_BNCHATBOT;
437 *channel = CHANNEL_BNCHATBOT;
438 }
439 else if (std::strcmp(argv[a], "-b") == 0 || std::strcmp(argv[a], "--client=SEXP") == 0)
440 {
441 if (*clienttag)
442 {
443 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
444 usage(argv[0]);
445 }
446 *clienttag = CLIENTTAG_BROODWARS;
447 *channel = CHANNEL_BROODWARS;
448 }
449 else if (std::strcmp(argv[a], "-d") == 0 || std::strcmp(argv[a], "--client=DRTL") == 0)
450 {
451 if (*clienttag)
452 {
453 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
454 usage(argv[0]);
455 }
456 *clienttag = CLIENTTAG_DIABLORTL;
457 *channel = CHANNEL_DIABLORTL;
458 }
459 else if (std::strcmp(argv[a], "--client=DSHR") == 0)
460 {
461 if (*clienttag)
462 {
463 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
464 usage(argv[0]);
465 }
466 *clienttag = CLIENTTAG_DIABLOSHR;
467 *channel = CHANNEL_DIABLOSHR;
468 }
469 else if (std::strcmp(argv[a], "-s") == 0 || std::strcmp(argv[a], "--client=STAR") == 0)
470 {
471 if (*clienttag)
472 {
473 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
474 usage(argv[0]);
475 }
476 *clienttag = CLIENTTAG_STARCRAFT;
477 *channel = CHANNEL_STARCRAFT;
478 }
479 else if (std::strcmp(argv[a], "--client=SSHR") == 0)
480 {
481 if (*clienttag)
482 {
483 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
484 usage(argv[0]);
485 }
486 *clienttag = CLIENTTAG_SHAREWARE;
487 *channel = CHANNEL_SHAREWARE;
488 }
489 else if (std::strcmp(argv[a], "-w") == 0 || std::strcmp(argv[a], "--client=W2BN") == 0)
490 {
491 if (*clienttag)
492 {
493 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
494 usage(argv[0]);
495 }
496 *clienttag = CLIENTTAG_WARCIIBNE;
497 *channel = CHANNEL_WARCIIBNE;
498 }
499 else if (std::strcmp(argv[a], "--client=D2DV") == 0)
500 {
501 if (*clienttag)
502 {
503 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
504 usage(argv[0]);
505 }
506 *clienttag = CLIENTTAG_DIABLO2DV;
507 *channel = CHANNEL_DIABLO2DV;
508 }
509 else if (std::strcmp(argv[a], "--client=D2XP") == 0)
510 {
511 if (*clienttag)
512 {
513 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
514 usage(argv[0]);
515 }
516 *clienttag = CLIENTTAG_DIABLO2XP;
517 *channel = CHANNEL_DIABLO2XP;
518 }
519 else if (std::strcmp(argv[a], "--client=WAR3") == 0)
520 {
521 if (*clienttag)
522 {
523 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
524 usage(argv[0]);
525 }
526 *clienttag = CLIENTTAG_WARCRAFT3;
527 *channel = CHANNEL_WARCRAFT3;
528 }
529 else if (std::strcmp(argv[a], "--client=W3XP") == 0)
530 {
531 if (*clienttag)
532 {
533 std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], *clienttag);
534 usage(argv[0]);
535 }
536 *clienttag = CLIENTTAG_WAR3XP;
537 *channel = CHANNEL_WAR3XP;
538 }
539 else if (std::strncmp(argv[a], "--client=", 9) == 0)
540 {
541 std::fprintf(stderr, "%s: unknown client tag \"%s\"\n", argv[0], &argv[a][9]);
542 usage(argv[0]);
543 }
544 else if (std::strcmp(argv[a], "--arch=IX86") == 0)
545 {
546 *archtag = ARCHTAG_WINX86;
547 }
548 else if (std::strcmp(argv[a], "--arch=PMAC") == 0)
549 {
550 *archtag = ARCHTAG_MACPPC;
551 }
552 else if (std::strcmp(argv[a], "--arch=XMAC") == 0)
553 {
554 *archtag = ARCHTAG_OSXPPC;
555 }
556 else if (std::strncmp(argv[a], "--arch=", 7) == 0)
557 {
558 std::fprintf(stderr, "%s: unknown architecture tag \"%s\"\n", argv[0], &argv[a][7]);
559 usage(argv[0]);
560 }
561 else if (std::strcmp(argv[a], "-o") == 0)
562 {
563 if (a + 1 >= argc)
564 {
565 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
566 usage(argv[0]);
567 }
568 if (*cdowner)
569 {
570 std::fprintf(stderr, "%s: CD owner was already specified as \"%s\"\n", argv[0], *cdowner);
571 usage(argv[0]);
572 }
573 *cdowner = argv[++a];
574 }
575 else if (std::strncmp(argv[a], "--owner=", 8) == 0)
576 {
577 if (*cdowner)
578 {
579 std::fprintf(stderr, "%s: CD owner was already specified as \"%s\"\n", argv[0], *cdowner);
580 usage(argv[0]);
581 }
582 *cdowner = &argv[a][8];
583 }
584 else if (std::strcmp(argv[a], "-k") == 0)
585 {
586 if (a + 1 >= argc)
587 {
588 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
589 usage(argv[0]);
590 }
591 if (*cdkey)
592 {
593 std::fprintf(stderr, "%s: CD key was already specified as \"%s\"\n", argv[0], *cdkey);
594 usage(argv[0]);
595 }
596 *cdkey = argv[++a];
597 }
598 else if (std::strncmp(argv[a], "--cdkey=", 8) == 0)
599 {
600 if (*cdkey)
601 {
602 std::fprintf(stderr, "%s: CD key was already specified as \"%s\"\n", argv[0], *cdkey);
603 usage(argv[0]);
604 }
605 *cdkey = &argv[a][8];
606 }
607 else if (std::strcmp(argv[a], "-l") == 0)
608 {
609 if (a + 1 >= argc)
610 {
611 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
612 usage(argv[0]);
613 }
614 if (std::strlen(argv[a + 1]) != 4)
615 {
616 std::fprintf(stderr, "%s: language has to be 4 characters long\n", argv[0]);
617 usage(argv[0]);
618 }
619 *gamelang = argv[++a];
620 }
621 else if (std::strncmp(argv[a], "--lang=", 7) == 0)
622 {
623 if (std::strlen(argv[a] + 7) != 4)
624 {
625 std::fprintf(stderr, "%s: language has to be 4 characters long\n", argv[0]);
626 usage(argv[0]);
627 }
628 *gamelang = &argv[a][7];
629 }
630 else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0)
631 {
632 std::printf("version " PVPGN_VERSION "\n");
633 return EXIT_SUCCESS;
634 }
635 else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage") == 0)
636 usage(argv[0]);
637 else if (std::strcmp(argv[a], "--client") == 0 || std::strcmp(argv[a], "--owner") == 0 || std::strcmp(argv[a], "--cdkey") == 0)
638 {
639 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
640 usage(argv[0]);
641 }
642 else if (std::strcmp(argv[a], "-i") == 0 || std::strcmp(argv[a], "--ignore-version") == 0)
643 {
644 *ignoreversion = 1;
645 }
646 else
647 {
648 std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
649 usage(argv[0]);
650 }
651
652 if (*servport == 0)
653 *servport = BNETD_SERV_PORT;
654 if (!(*cdowner))
655 *cdowner = BNETD_DEFAULT_OWNER;
656 if (!(*cdkey))
657 *cdkey = BNETD_DEFAULT_KEY;
658 if (!(*clienttag))
659 {
660 *clienttag = CLIENTTAG_STARCRAFT;
661 *channel = CHANNEL_STARCRAFT;
662 }
663 if (!(*servname))
664 *servname = BNETD_DEFAULT_HOST;
665
666 return 0;
667 }
668
669 void munge(t_client_state * client)
670 {
671 if (!client->munged)
672 {
673 std::printf("\r");
674 size_t client_modelen = std::strlen(mode_get_prompt(client->mode));
675 for (unsigned i = 0; i < client_modelen; i++)
676 std::printf(" ");
677
678 size_t client_textlen = std::strlen(client->text);
679 for (unsigned i = 0; i < client_textlen && i < client->screen_width - client_modelen; i++)
680 std::printf(" ");
681 std::printf("\r");
682 client->munged = 1;
683 }
684 }
685
686 void ansi_printf(t_client_state * client, int color, char const * fmt, ...)
687 {
688 std::va_list args;
689 char buffer[2048];
690
691 if (!(client))
692 return;
693
694 if (!(fmt))
695 return;
696
697 if (client->useansi)
698 ansi_text_color_fore(color);
699
700 va_start(args, fmt);
701 std::vsnprintf(buffer, 2048, fmt, args);
702 va_end(args);
703
704 str_print_term(stdout, buffer, 0, 1);
705
706 if (client->useansi)
707 ansi_text_reset();
708
709 std::fflush(stdout);
710
711 }
712
713 }
714
715 extern int main(int argc, char * argv[])
716 {
717 int newacct = 0;
718 int changepass = 0;
719 t_client_state client;
720 t_user_info user;
721 t_packet * packet;
722 t_packet * rpacket;
723 char const * servname = NULL;
724 unsigned short servport = 0;
725 char const * * channellist;
726 unsigned int statsmatch = 24; /* any random number that is rare in uninitialized fields */
727
728 std::memset(&user, 0, sizeof(t_user_info));
729 std::memset(&client, 0, sizeof(t_client_state));
730
731 /* default values */
732 user.archtag = ARCHTAG_WINX86;
733 user.gamelang = CLIENT_AUTH_INFO_GAMELANG;
734 user.ignoreversion = 0;
735
736 read_commandline(argc, argv, &servname, &servport, &user.clienttag, &user.archtag, &changepass,
737 &newacct, &user.channel, &user.cdowner, &user.cdkey, &user.gamelang, &user.ignoreversion, &client.useansi);
738
739 client.fd_stdin = fileno(stdin);
740 if (tcgetattr(client.fd_stdin, &client.in_attr_old) >= 0)
741 {
742 client.in_attr_new = client.in_attr_old;
743 client.in_attr_new.c_lflag &= ~(ECHO | ICANON); /* turn off ECHO and ICANON */
744 client.in_attr_new.c_cc[VMIN] = 0; /* don't require reads to return data */
745 client.in_attr_new.c_cc[VTIME] = 1; /* timeout = .1 seconds */
746 tcsetattr(client.fd_stdin, TCSANOW, &client.in_attr_new);
747 client.changed_in = 1;
748 }
749 else
750 {
751 std::fprintf(stderr, "%s: could not set terminal attributes for stdin\n", argv[0]);
752 client.changed_in = 0;
753 }
754
755 #ifdef HAVE_SIGACTION
756 {
757 struct sigaction winch_action;
758
759 winch_action.sa_handler = winch_sig_handle;
760 sigemptyset(&winch_action.sa_mask);
761 winch_action.sa_flags = SA_RESTART;
762
763 sigaction(SIGWINCH, &winch_action, NULL);
764 }
765 #endif
766
767
768 if (client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height) < 0)
769 {
770 std::fprintf(stderr, "%s: could not determine screen size\n", argv[0]);
771 if (client.changed_in)
772 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
773 return EXIT_FAILURE;
774 }
775
776 if (client.useansi)
777 {
778 ansi_text_reset();
779 ansi_screen_clear();
780 ansi_cursor_move_home();
781 std::fflush(stdout);
782 }
783
784 if ((client.sd = client_connect(argv[0],
785 servname, servport, user.cdowner, user.cdkey, user.clienttag, user.ignoreversion,
786 &client.saddr, &client.sessionkey, &client.sessionnum, user.archtag, user.gamelang)) < 0)
787 {
788 std::fprintf(stderr, "%s: fatal error during handshake\n", argv[0]);
789 if (client.changed_in)
790 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
791 return EXIT_FAILURE;
792 }
793
794 /* reuse this same packet over and over */
795 if (!(rpacket = packet_create(packet_class_bnet)))
796 {
797 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
798 psock_close(client.sd);
799 if (client.changed_in)
800 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
801 return EXIT_FAILURE;
802 }
803
804 if (!(packet = packet_create(packet_class_bnet)))
805 {
806 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
807 psock_close(client.sd);
808 if (client.changed_in)
809 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
810 return EXIT_FAILURE;
811 }
812 packet_set_size(packet, sizeof(t_client_fileinforeq));
813 packet_set_type(packet, CLIENT_FILEINFOREQ);
814 bn_int_set(&packet->u.client_fileinforeq.type, CLIENT_FILEINFOREQ_TYPE_TOS);
815 bn_int_set(&packet->u.client_fileinforeq.unknown2, CLIENT_FILEINFOREQ_UNKNOWN2);
816
817 if (strcasecmp(user.clienttag, CLIENTTAG_DIABLO2DV) == 0 || strcasecmp(user.clienttag, CLIENTTAG_DIABLO2XP) == 0)
818 packet_append_string(packet, CLIENT_FILEINFOREQ_FILE_TOSUNICODEUSA);
819 else
820 packet_append_string(packet, CLIENT_FILEINFOREQ_FILE_TOSUSA);
821
822 client_blocksend_packet(client.sd, packet);
823 packet_del_ref(packet);
824 do
825 if (client_blockrecv_packet(client.sd, rpacket) < 0)
826 {
827 std::fprintf(stderr, "%s: server closed connection\n", argv[0]);
828 psock_close(client.sd);
829 if (client.changed_in)
830 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
831 return EXIT_FAILURE;
832 }
833 while (packet_get_type(rpacket) != SERVER_FILEINFOREPLY);
834
835 /* real client would also send statsreq on past logins here */
836
837 std::printf("----\n");
838 if (newacct)
839 {
840 if (client.useansi)
841 ansi_text_color_fore(ansi_text_color_red);
842 std::printf("###### Terms Of Service ######\n");
843 print_file(&client.saddr, packet_get_str_const(rpacket, sizeof(t_server_fileinforeply), 1024), argv[0], user.clienttag);
844 std::printf("##############################\n\n");
845 if (client.useansi)
846 ansi_text_reset();
847 }
848
849 for (;;)
850 {
851 char password[MAX_MESSAGE_LEN];
852 int status;
853
854 if (newacct)
855 {
856 char passwordvrfy[MAX_MESSAGE_LEN];
857 t_hash passhash1;
858
859 std::printf("Enter information for your new account\n");
860 client.munged = 1;
861 client.commpos = 0;
862 user.player[0] = '\0';
863 while ((status = client_get_comm("Username: ", user.player, MAX_MESSAGE_LEN, &client.commpos,
864 1, client.munged, client.screen_width)) == 0)
865 if (handle_winch)
866 {
867 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
868 std::printf(" \r");
869 client.munged = 1;
870 handle_winch = 0;
871 }
872 else
873 client.munged = 0;
874 std::printf("\n");
875 if (status < 0)
876 continue;
877 if (std::strchr(user.player, ' ') || std::strchr(user.player, '\t') ||
878 std::strchr(user.player, '\r') || std::strchr(user.player, '\n'))
879 {
880 std::printf("Spaces are not allowed in usernames. Try again.\n");
881 continue;
882 }
883 /* we could use std::strcspn() but it doesn't exist everywhere */
884 if (std::strchr(user.player, '#') ||
885 std::strchr(user.player, '%') ||
886 std::strchr(user.player, '&') ||
887 std::strchr(user.player, '*') ||
888 std::strchr(user.player, '\\') ||
889 std::strchr(user.player, '"') ||
890 std::strchr(user.player, ',') ||
891 std::strchr(user.player, '<') ||
892 std::strchr(user.player, '/') ||
893 std::strchr(user.player, '>') ||
894 std::strchr(user.player, '?'))
895 {
896 std::printf("The special characters #%%&*\\\",</>? are not allowed in usernames. Try again.\n");
897 }
898 if (std::strlen(user.player) >= MAX_USERNAME_LEN)
899 {
900 std::printf("Usernames must not be more than %u characters long. Try again.\n", MAX_USERNAME_LEN - 1);
901 continue;
902 }
903
904 client.munged = 1;
905 client.commpos = 0;
906 password[0] = '\0';
907 while ((status = client_get_comm("Password: ", password, MAX_MESSAGE_LEN, &client.commpos, 0,
908 client.munged, client.screen_width)) == 0)
909 if (handle_winch)
910 {
911 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
912 std::printf(" \r");
913 client.munged = 1;
914 handle_winch = 0;
915 }
916 else
917 client.munged = 0;
918 std::printf("\n");
919 if (status<0)
920 continue;
921 if (std::strlen(password)>MAX_USERPASS_LEN)
922 {
923 std::printf("password must not be more than %u characters long. Try again.\n", MAX_USERPASS_LEN);
924 continue;
925 }
926 strtolower(password);
927
928 client.munged = 1;
929 client.commpos = 0;
930 passwordvrfy[0] = '\0';
931 while ((status = client_get_comm("Retype password: ", passwordvrfy, MAX_MESSAGE_LEN, &client.commpos, 0,
932 client.munged, client.screen_width)) == 0)
933 if (handle_winch)
934 {
935 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
936 std::printf(" \r");
937 client.munged = 1;
938 handle_winch = 0;
939 }
940 else
941 client.munged = 0;
942 std::printf("\n");
943 if (status < 0)
944 continue;
945 strtolower(passwordvrfy);
946
947 if (std::strcmp(password, passwordvrfy) != 0)
948 {
949 std::printf("Passwords do not match. Try again.\n");
950 continue;
951 }
952
953 bnet_hash(&passhash1, std::strlen(password), password); /* do the single hash */
954
955 if (!(packet = packet_create(packet_class_bnet)))
956 {
957 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
958 psock_close(client.sd);
959 if (client.changed_in)
960 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
961 return EXIT_FAILURE;
962 }
963 packet_set_size(packet, sizeof(t_client_createacctreq1));
964 packet_set_type(packet, CLIENT_CREATEACCTREQ1);
965 hash_to_bnhash((t_hash const *)&passhash1, packet->u.client_createacctreq1.password_hash1); /* avoid warning */
966 packet_append_string(packet, user.player);
967 client_blocksend_packet(client.sd, packet);
968 packet_del_ref(packet);
969
970 do
971 if (client_blockrecv_packet(client.sd, rpacket) < 0)
972 {
973 std::fprintf(stderr, "%s: server closed connection\n", argv[0]);
974 psock_close(client.sd);
975 if (client.changed_in)
976 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
977 return EXIT_FAILURE;
978 }
979 while (packet_get_type(rpacket) != SERVER_CREATEACCTREPLY1);
980 dprintf("Got CREATEACCTREPLY1\n");
981 if (bn_int_get(rpacket->u.server_createacctreply1.result) == SERVER_CREATEACCTREPLY1_RESULT_NO)
982 {
983 std::printf("Could not create an account under that name. Try another one.\n");
984 continue;
985 }
986 std::printf("Account created.\n");
987 }
988 else if (changepass)
989 {
990 char passwordprev[MAX_MESSAGE_LEN];
991 char passwordvrfy[MAX_MESSAGE_LEN];
992 struct
993 {
994 bn_int ticks;
995 bn_int sessionkey;
996 bn_int passhash1[5];
997 } temp;
998 t_hash oldpasshash1;
999 t_hash oldpasshash2;
1000 t_hash newpasshash1;
1001 unsigned int ticks;
1002
1003 std::printf("Enter your old and new login information\n");
1004
1005 client.munged = 1;
1006 client.commpos = 0;
1007 user.player[0] = '\0';
1008 while ((status = client_get_comm("Username: ", user.player, MAX_MESSAGE_LEN, &client.commpos, 1,
1009 client.munged, client.screen_width)) == 0)
1010 if (handle_winch)
1011 {
1012 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1013 std::printf(" \r");
1014 client.munged = 1;
1015 handle_winch = 0;
1016 }
1017 else
1018 client.munged = 0;
1019 std::printf("\n");
1020 if (status < 0)
1021 continue;
1022 if (std::strchr(user.player, ' '))
1023 {
1024 std::printf("Spaces not allowed in username. Try again.\n");
1025 continue;
1026 }
1027 if (std::strlen(user.player) >= MAX_USERNAME_LEN)
1028 {
1029 std::printf("Usernames must not be more than %u characters long. Try again.\n", MAX_USERNAME_LEN - 1);
1030 continue;
1031 }
1032
1033 client.munged = 1;
1034 client.commpos = 0;
1035 passwordprev[0] = '\0';
1036 while ((status = client_get_comm("Old password: ", passwordprev, MAX_MESSAGE_LEN, &client.commpos, 0,
1037 client.munged, client.screen_width)) == 0)
1038 if (handle_winch)
1039 {
1040 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1041 std::printf(" \r");
1042 client.munged = 1;
1043 handle_winch = 0;
1044 }
1045 else
1046 client.munged = 0;
1047 std::printf("\n");
1048 if (status < 0)
1049 continue;
1050 strtolower(passwordprev);
1051
1052 client.munged = 1;
1053 client.commpos = 0;
1054 password[0] = '\0';
1055 while ((status = client_get_comm("New password: ", password, MAX_MESSAGE_LEN, &client.commpos, 0,
1056 client.munged, client.screen_width)) == 0)
1057 if (handle_winch)
1058 {
1059 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1060 std::printf(" \r");
1061 client.munged = 1;
1062 handle_winch = 0;
1063 }
1064 else
1065 client.munged = 0;
1066 std::printf("\n");
1067 if (status < 0)
1068 continue;
1069 strtolower(password);
1070
1071 client.munged = 1;
1072 client.commpos = 0;
1073 passwordvrfy[0] = '\0';
1074 while ((status = client_get_comm("Retype new password: ", passwordvrfy, MAX_MESSAGE_LEN, &client.commpos, 0,
1075 client.munged, client.screen_width)) == 0)
1076 if (handle_winch)
1077 {
1078 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1079 std::printf(" \r");
1080 client.munged = 1;
1081 handle_winch = 0;
1082 }
1083 else
1084 client.munged = 0;
1085 std::printf("\n");
1086 if (status < 0)
1087 continue;
1088 strtolower(passwordvrfy);
1089
1090 if (std::strcmp(password, passwordvrfy) != 0)
1091 {
1092 std::printf("New passwords do not match. Try again.\n");
1093 continue;
1094 }
1095
1096 ticks = 0; /* FIXME: what to use here? */
1097 bn_int_set(&temp.ticks, ticks);
1098 bn_int_set(&temp.sessionkey, client.sessionkey);
1099 bnet_hash(&oldpasshash1, std::strlen(passwordprev), passwordprev); /* do the single hash for old */
1100 hash_to_bnhash((t_hash const *)&oldpasshash1, temp.passhash1); /* avoid warning */
1101 bnet_hash(&oldpasshash2, sizeof(temp), &temp); /* do the double hash for old */
1102 bnet_hash(&newpasshash1, std::strlen(password), password); /* do the single hash for new */
1103
1104 if (!(packet = packet_create(packet_class_bnet)))
1105 {
1106 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1107 psock_close(client.sd);
1108 if (client.changed_in)
1109 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1110 return EXIT_FAILURE;
1111 }
1112 packet_set_size(packet, sizeof(t_client_changepassreq));
1113 packet_set_type(packet, CLIENT_CHANGEPASSREQ);
1114 bn_int_set(&packet->u.client_changepassreq.ticks, ticks);
1115 bn_int_set(&packet->u.client_changepassreq.sessionkey, client.sessionkey);
1116 hash_to_bnhash((t_hash const *)&oldpasshash2, packet->u.client_changepassreq.oldpassword_hash2); /* avoid warning */
1117 hash_to_bnhash((t_hash const *)&newpasshash1, packet->u.client_changepassreq.newpassword_hash1); /* avoid warning */
1118 packet_append_string(packet, user.player);
1119 client_blocksend_packet(client.sd, packet);
1120 packet_del_ref(packet);
1121
1122 do
1123 if (client_blockrecv_packet(client.sd, rpacket) < 0)
1124 {
1125 std::fprintf(stderr, "%s: server closed connection\n", argv[0]);
1126 psock_close(client.sd);
1127 if (client.changed_in)
1128 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1129 return EXIT_FAILURE;
1130 }
1131 while (packet_get_type(rpacket) != SERVER_CHANGEPASSACK);
1132 dprintf("Got CHANGEPASSACK\n");
1133 if (bn_int_get(rpacket->u.server_changepassack.message) == SERVER_CHANGEPASSACK_MESSAGE_FAIL)
1134 {
1135 std::printf("Could not change password. Try again.\n");
1136 continue;
1137 }
1138 std::printf("Password changed.\n");
1139 }
1140 else
1141 {
1142 std::printf("Enter your login information\n");
1143
1144 client.munged = 1;
1145 client.commpos = 0;
1146 user.player[0] = '\0';
1147 while ((status = client_get_comm("Username: ", user.player, MAX_MESSAGE_LEN, &client.commpos, 1,
1148 client.munged, client.screen_width)) == 0)
1149 if (handle_winch)
1150 {
1151 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1152 std::printf(" \r");
1153 client.munged = 1;
1154 handle_winch = 0;
1155 }
1156 else
1157 client.munged = 0;
1158 std::printf("\n");
1159 if (status < 0)
1160 continue;
1161 if (std::strchr(user.player, ' '))
1162 {
1163 std::printf("Spaces not allowed in username. Try again.\n");
1164 continue;
1165 }
1166 if (std::strlen(user.player) >= MAX_USERNAME_LEN)
1167 {
1168 std::printf("Usernames must not be more than %u characters long. Try again.\n", MAX_USERNAME_LEN - 1);
1169 continue;
1170 }
1171
1172 client.munged = 1;
1173 client.commpos = 0;
1174 password[0] = '\0';
1175 while ((status = client_get_comm("Password: ", password, MAX_MESSAGE_LEN, &client.commpos, 0,
1176 client.munged, client.screen_width)) == 0)
1177 if (handle_winch)
1178 {
1179 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1180 std::printf(" \r");
1181 client.munged = 1;
1182 handle_winch = 0;
1183 }
1184 else
1185 client.munged = 0;
1186 std::printf("\n");
1187 if (status < 0)
1188 continue;
1189 strtolower(password);
1190 }
1191
1192 /* now login */
1193 {
1194 struct
1195 {
1196 bn_int ticks;
1197 bn_int sessionkey;
1198 bn_int passhash1[5];
1199 } temp;
1200 t_hash passhash1;
1201 t_hash passhash2;
1202 unsigned int ticks;
1203
1204 ticks = 0; /* FIXME: what to use here? */
1205 bn_int_set(&temp.ticks, ticks);
1206 bn_int_set(&temp.sessionkey, client.sessionkey);
1207 bnet_hash(&passhash1, std::strlen(password), password); /* do the single hash */
1208 hash_to_bnhash((t_hash const *)&passhash1, temp.passhash1); /* avoid warning */
1209 bnet_hash(&passhash2, sizeof(temp), &temp); /* do the double hash */
1210
1211 if (!(packet = packet_create(packet_class_bnet)))
1212 {
1213 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1214 psock_close(client.sd);
1215 if (client.changed_in)
1216 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1217 return EXIT_FAILURE;
1218 }
1219 packet_set_size(packet, sizeof(t_client_loginreq1));
1220 packet_set_type(packet, CLIENT_LOGINREQ1);
1221 bn_int_set(&packet->u.client_loginreq1.ticks, ticks);
1222 bn_int_set(&packet->u.client_loginreq1.sessionkey, client.sessionkey);
1223 hash_to_bnhash((t_hash const *)&passhash2, packet->u.client_loginreq1.password_hash2); /* avoid warning */
1224 packet_append_string(packet, user.player);
1225 client_blocksend_packet(client.sd, packet);
1226 packet_del_ref(packet);
1227 }
1228
1229 do
1230 if (client_blockrecv_packet(client.sd, rpacket) < 0)
1231 {
1232 std::fprintf(stderr, "%s: server closed connection\n", argv[0]);
1233 psock_close(client.sd);
1234 if (client.changed_in)
1235 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1236 return EXIT_FAILURE;
1237 }
1238 while (packet_get_type(rpacket) != SERVER_LOGINREPLY1);
1239 if (bn_int_get(rpacket->u.server_loginreply1.message) == SERVER_LOGINREPLY1_MESSAGE_SUCCESS)
1240 break;
1241 std::fprintf(stderr, "Login incorrect.\n");
1242 }
1243
1244 std::fprintf(stderr, "Logged in.\n");
1245 std::printf("----\n");
1246
1247 if (newacct && (std::strcmp(user.clienttag, CLIENTTAG_DIABLORTL) == 0 ||
1248 std::strcmp(user.clienttag, CLIENTTAG_DIABLOSHR) == 0))
1249 {
1250 if (!(packet = packet_create(packet_class_bnet)))
1251 {
1252 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1253 if (client.changed_in)
1254 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1255 }
1256 else
1257 {
1258 packet_set_size(packet, sizeof(t_client_playerinforeq));
1259 packet_set_type(packet, CLIENT_PLAYERINFOREQ);
1260 packet_append_string(packet, user.player);
1261 packet_append_string(packet, "LTRD 1 2 0 20 25 15 20 100 0"); /* FIXME: don't hardcode */
1262 client_blocksend_packet(client.sd, packet);
1263 packet_del_ref(packet);
1264 }
1265 }
1266
1267 if (!(packet = packet_create(packet_class_bnet)))
1268 {
1269 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1270 psock_close(client.sd);
1271 if (client.changed_in)
1272 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1273 return EXIT_FAILURE;
1274 }
1275 packet_set_size(packet, sizeof(t_client_progident2));
1276 packet_set_type(packet, CLIENT_PROGIDENT2);
1277 bn_int_tag_set(&packet->u.client_progident2.clienttag, user.clienttag);
1278 client_blocksend_packet(client.sd, packet);
1279 packet_del_ref(packet);
1280
1281 do
1282 if (client_blockrecv_packet(client.sd, rpacket) < 0)
1283 {
1284 std::fprintf(stderr, "%s: server closed connection\n", argv[0]);
1285 psock_close(client.sd);
1286 if (client.changed_in)
1287 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1288 return EXIT_FAILURE;
1289 }
1290 while (packet_get_type(rpacket) != SERVER_CHANNELLIST);
1291
1292 {
1293 unsigned int i;
1294 unsigned int chann_off;
1295 char const * chann;
1296
1297 channellist = (const char**)xmalloc(sizeof(char*)* 1);
1298 for (i = 0, chann_off = sizeof(t_server_channellist);
1299 (chann = packet_get_str_const(rpacket, chann_off, 128));
1300 i++, chann_off += std::strlen(chann) + 1)
1301 {
1302 if (chann[0] == '\0') break; /* channel list ends with a "" */
1303
1304 channellist = (const char**)xrealloc(channellist, sizeof(char*)*(i + 2));
1305 channellist[i] = xstrdup(chann);
1306 }
1307 channellist[i] = NULL;
1308 }
1309
1310 if (!(packet = packet_create(packet_class_bnet)))
1311 {
1312 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1313 psock_close(client.sd);
1314 if (client.changed_in)
1315 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1316 return EXIT_FAILURE;
1317 }
1318 packet_set_size(packet, sizeof(t_client_joinchannel));
1319 packet_set_type(packet, CLIENT_JOINCHANNEL);
1320 bn_int_set(&packet->u.client_joinchannel.channelflag, CLIENT_JOINCHANNEL_GENERIC);
1321 packet_append_string(packet, user.channel);
1322 client_blocksend_packet(client.sd, packet);
1323 packet_del_ref(packet);
1324
1325 if (psock_ctl(client.sd, PSOCK_NONBLOCK) < 0)
1326 std::fprintf(stderr, "%s: could not set TCP socket to non-blocking mode (psock_ctl: %s)\n", argv[0], std::strerror(psock_errno()));
1327
1328 client.mode = mode_chat;
1329
1330 {
1331 unsigned int i;
1332 int highest_fd;
1333 t_psock_fd_set rfds;
1334 #ifdef WIN32
1335 static struct timeval tv;
1336
1337 tv.tv_sec = 50 / 1000;
1338 tv.tv_usec = 50 % 1000;
1339 #endif
1340
1341 PSOCK_FD_ZERO(&rfds);
1342
1343 #ifndef WIN32
1344 highest_fd = client.fd_stdin;
1345 if (client.sd > highest_fd)
1346 #endif
1347 highest_fd = client.sd;
1348
1349 client.currsize = 0;
1350
1351 client.munged = 1; /* == need to draw prompt */
1352 client.commpos = 0;
1353 client.text[0] = '\0';
1354
1355 for (;;)
1356 {
1357
1358
1359 if (handle_winch)
1360 {
1361 client_get_termsize(client.fd_stdin, &client.screen_width, &client.screen_height);
1362 handle_winch = 0;
1363 std::printf(" \r");
1364 client.munged = 1;
1365 }
1366
1367 if (client.munged)
1368 {
1369 std::printf("%s%s", mode_get_prompt(client.mode), client.text + ((client.screen_width <= std::strlen(mode_get_prompt(client.mode)) + client.commpos) ? std::strlen(mode_get_prompt(client.mode)) + client.commpos + 1 - client.screen_width : 0));
1370 std::fflush(stdout);
1371 client.munged = 0;
1372 }
1373 PSOCK_FD_ZERO(&rfds);
1374 #ifndef WIN32
1375 PSOCK_FD_SET(client.fd_stdin, &rfds);
1376 #endif
1377 PSOCK_FD_SET(client.sd, &rfds);
1378 errno = 0;
1379
1380 #ifndef WIN32
1381 if (psock_select(highest_fd + 1, &rfds, NULL, NULL, NULL) < 0)
1382 #else
1383 if (psock_select(highest_fd + 1, &rfds, NULL, NULL, &tv) < 0)
1384 #endif
1385 {
1386 if (psock_errno() != PSOCK_EINTR)
1387 {
1388 munge(&client);
1389 std::printf("Select failed (select: %s)\n", std::strerror(psock_errno()));
1390 }
1391 continue;
1392 }
1393 #ifndef WIN32
1394 if (PSOCK_FD_ISSET(client.fd_stdin, &rfds)) /* got keyboard data */
1395 #else
1396 if (kbhit())
1397 #endif
1398 {
1399 client.munged = 0;
1400
1401 switch (client_get_comm(mode_get_prompt(client.mode), client.text, MAX_MESSAGE_LEN, &client.commpos, 1,
1402 0, client.screen_width))
1403 {
1404 case -1: /* cancel */
1405 munge(&client);
1406 if (client.mode == mode_command)
1407 client.mode = mode_chat;
1408 else
1409 client.mode = mode_command;
1410 client.commpos = 0;
1411 client.text[0] = '\0';
1412 break;
1413
1414 case 0: /* timeout */
1415 break;
1416
1417 case 1:
1418 switch (client.mode)
1419 {
1420 case mode_claninvite:
1421
1422 std::printf("\n");
1423 client.munged = 1;
1424
1425 if ((client.text[0] != '\0') && strcasecmp(client.text, "yes") && strcasecmp(client.text, "no"))
1426 {
1427 std::printf("Do you want to accept invitation (yes/no) ? [yes] ");
1428 break;
1429 }
1430
1431 if (!(packet = packet_create(packet_class_bnet)))
1432 {
1433 std::printf("Packet creation failed.\n");
1434 }
1435 else
1436 {
1437 char result;
1438
1439 packet_set_size(packet, sizeof(t_client_clan_invitereply));
1440 packet_set_type(packet, CLIENT_CLAN_INVITEREPLY);
1441 bn_int_set(&packet->u.client_clan_invitereply.count, user.count);
1442 bn_int_set(&packet->u.client_clan_invitereply.clantag, user.clantag);
1443 packet_append_string(packet, user.inviter);
1444
1445 if (!strcasecmp(client.text, "no"))
1446 result = CLAN_RESPONSE_DECLINED;
1447 else
1448 result = CLAN_RESPONSE_ACCEPT;
1449
1450 packet_append_data(packet, &result, 1);
1451
1452 client_blocksend_packet(client.sd, packet);
1453 packet_del_ref(packet);
1454
1455 if (user.inviter)
1456 xfree((void *)user.inviter);
1457 user.inviter = NULL;
1458
1459 }
1460
1461 case mode_clancreateinvite:
1462
1463 std::printf("\n");
1464 client.munged = 1;
1465
1466 if ((client.text[0] != '\0') && strcasecmp(client.text, "yes") && strcasecmp(client.text, "no"))
1467 {
1468 std::printf("Do you want to accept invitation (yes/no) ? [yes] ");
1469 break;
1470 }
1471
1472 if (!(packet = packet_create(packet_class_bnet)))
1473 {
1474 std::printf("Packet creation failed.\n");
1475 }
1476 else
1477 {
1478 char result;
1479
1480 packet_set_size(packet, sizeof(t_client_clan_createinvitereply));
1481 packet_set_type(packet, CLIENT_CLAN_CREATEINVITEREPLY);
1482 bn_int_set(&packet->u.client_clan_createinvitereply.count, user.count);
1483 bn_int_set(&packet->u.client_clan_createinvitereply.clantag, user.clantag);
1484 packet_append_string(packet, user.inviter);
1485
1486 if (!strcasecmp(client.text, "no"))
1487 result = CLAN_RESPONSE_DECLINED;
1488 else
1489 result = CLAN_RESPONSE_ACCEPT;
1490
1491 packet_append_data(packet, &result, 1);
1492
1493 client_blocksend_packet(client.sd, packet);
1494 packet_del_ref(packet);
1495
1496 if (user.inviter)
1497 xfree((void *)user.inviter);
1498 user.inviter = NULL;
1499
1500 }
1501
1502
1503 client.mode = mode_chat;
1504 break;
1505
1506 case mode_gamename:
1507 if (client.text[0] == '\0')
1508 {
1509 munge(&client);
1510 std::printf("Games must have a name.\n");
1511 break;
1512 }
1513 std::printf("\n");
1514 client.munged = 1;
1515 std::strncpy(user.curr_gamename, client.text, sizeof(user.curr_gamename));
1516 user.curr_gamename[sizeof(user.curr_gamename) - 1] = '\0';
1517 client.mode = mode_gamepass;
1518 break;
1519 case mode_gamepass:
1520 std::printf("\n");
1521 client.munged = 1;
1522 std::strncpy(user.curr_gamepass, client.text, sizeof(user.curr_gamepass));
1523 user.curr_gamepass[sizeof(user.curr_gamepass) - 1] = '\0';
1524
1525 if (!(packet = packet_create(packet_class_bnet)))
1526 {
1527 std::printf("Packet creation failed.\n");
1528 client.mode = mode_command;
1529 }
1530 else
1531 {
1532 packet_set_size(packet, sizeof(t_client_startgame4));
1533 packet_set_type(packet, CLIENT_STARTGAME4);
1534 bn_short_set(&packet->u.client_startgame4.status, CLIENT_STARTGAME4_STATUS_INIT);
1535 bn_short_set(&packet->u.client_startgame4.flag, 0x0000);
1536 bn_int_set(&packet->u.client_startgame4.unknown2, CLIENT_STARTGAME4_UNKNOWN2);
1537 bn_short_set(&packet->u.client_startgame4.gametype, CLIENT_GAMELISTREQ_MELEE);
1538 bn_short_set(&packet->u.client_startgame4.option, CLIENT_STARTGAME4_OPTION_MELEE_NORMAL);
1539 bn_int_set(&packet->u.client_startgame4.unknown4, CLIENT_STARTGAME4_UNKNOWN4);
1540 bn_int_set(&packet->u.client_startgame4.unknown5, CLIENT_STARTGAME4_UNKNOWN5);
1541 packet_append_string(packet, user.curr_gamename);
1542 packet_append_string(packet, user.curr_gamepass);
1543 packet_append_string(packet, ",,,,1,3,1,3e37a84c,7,Player\rAshrigo\r");
1544 client_blocksend_packet(client.sd, packet);
1545 packet_del_ref(packet);
1546 client.mode = mode_gamewait;
1547 }
1548 break;
1549 case mode_gamestop:
1550 std::printf("\n");
1551 client.munged = 1;
1552
1553 if (!(packet = packet_create(packet_class_bnet)))
1554 std::printf("Packet creation failed.\n");
1555 else
1556 {
1557 packet_set_size(packet, sizeof(t_client_closegame));
1558 packet_set_type(packet, CLIENT_CLOSEGAME);
1559 client_blocksend_packet(client.sd, packet);
1560 packet_del_ref(packet);
1561 std::printf("Game closed.\n");
1562 client.mode = mode_command;
1563 }
1564 break;
1565 case mode_command:
1566 if (client.text[0] == '\0')
1567 break;
1568 std::printf("\n");
1569 client.munged = 1;
1570 if (strstart(client.text, "channel") == 0)
1571 {
1572 std::printf("Available channels:\n");
1573 if (client.useansi)
1574 ansi_text_color_fore(ansi_text_color_yellow);
1575 for (i = 0; channellist[i]; i++)
1576 std::printf(" %s\n", channellist[i]);
1577 if (client.useansi)
1578 ansi_text_reset();
1579 }
1580 else if (strstart(client.text, "create") == 0)
1581 {
1582 std::printf("Enter new game information\n");
1583 client.mode = mode_gamename;
1584 }
1585 else if (strstart(client.text, "join") == 0)
1586 {
1587 std::printf("Not implemented yet.\n");
1588 }
1589 else if (strstart(client.text, "ladder") == 0)
1590 {
1591 std::printf("Not implemented yet.\n");
1592 }
1593 else if (strstart(client.text, "stats") == 0)
1594 {
1595 std::printf("Not implemented yet.\n");
1596 }
1597 else if (strstart(client.text, "help") == 0 || std::strcmp(client.text, "?") == 0)
1598 {
1599 std::printf("Available commands:\n"
1600 " channel - join or create a channel\n"
1601 " create - create a new game\n"
1602 " join - list current games\n"
1603 " ladder - list ladder rankings\n"
1604 " help - show this text\n"
1605 " info <PLAYER> - print a player's profile\n"
1606 " chinfo - modify your profile\n"
1607 " quit - exit bnchat\n"
1608 " stats <PLAYER> - print a player's game record\n"
1609 " invite <PLAYER> - invite a player to your clan\n"
1610 "Use the escape key to toggle between chat and command modes.\n");
1611 }
1612 else if (strstart(client.text, "invite") == 0)
1613 {
1614 for (i = 6; client.text[i] == ' ' || client.text[i] == '\t'; i++);
1615 if (client.text[i] == '\0')
1616 {
1617 ansi_printf(&client, ansi_text_color_red, "You must specify the player.\n");
1618 }
1619 else
1620 {
1621 if (!(packet = packet_create(packet_class_bnet)))
1622 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1623 else
1624 {
1625 std::printf("Inviting: %s\n", &client.text[i]);
1626 packet_set_size(packet, sizeof(t_client_clan_invitereq));
1627 packet_set_type(packet, CLIENT_CLAN_INVITEREQ);
1628 bn_int_set(&packet->u.client_clan_invitereq.count, 1);
1629 packet_append_string(packet, &client.text[i]);
1630 client_blocksend_packet(client.sd, packet);
1631 packet_del_ref(packet);
1632 }
1633 }
1634 }
1635 else if (strstart(client.text, "info") == 0)
1636 {
1637 for (i = 4; client.text[i] == ' ' || client.text[i] == '\t'; i++);
1638 if (client.text[i] == '\0')
1639 {
1640 ansi_printf(&client, ansi_text_color_red, "You must specify the player.\n");
1641 }
1642 else
1643 {
1644 if (!(packet = packet_create(packet_class_bnet)))
1645 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1646 else
1647 {
1648 std::printf("Profile info for %s:\n", &client.text[i]);
1649 packet_set_size(packet, sizeof(t_client_statsreq));
1650 packet_set_type(packet, CLIENT_STATSREQ);
1651 bn_int_set(&packet->u.client_statsreq.name_count, 1);
1652 bn_int_set(&packet->u.client_statsreq.key_count, 4);
1653 statsmatch = (unsigned int)std::time(NULL);
1654 bn_int_set(&packet->u.client_statsreq.requestid, statsmatch);
1655 packet_append_string(packet, &client.text[i]);
1656 #if 0
1657 packet_append_string(packet, "BNET\\acct\\username");
1658 packet_append_string(packet, "BNET\\acct\\userid");
1659 #endif
1660 packet_append_string(packet, "profile\\sex");
1661 packet_append_string(packet, "profile\\age");
1662 packet_append_string(packet, "profile\\location");
1663 packet_append_string(packet, "profile\\description");
1664 client_blocksend_packet(client.sd, packet);
1665 packet_del_ref(packet);
1666
1667 client.mode = mode_waitstat;
1668 }
1669 }
1670 }
1671 else if (strstart(client.text, "chinfo") == 0)
1672 {
1673 std::printf("Not implemented yet.\n");
1674 }
1675 else if (strstart(client.text, "quit") == 0)
1676 {
1677 psock_close(client.sd);
1678 if (client.changed_in)
1679 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1680 return EXIT_SUCCESS;
1681 }
1682 else
1683 {
1684 ansi_printf(&client, ansi_text_color_red, "Unknown local command \"%s\".\n", client.text);
1685 }
1686 break;
1687
1688 case mode_chat:
1689 if (client.text[0] == '\0')
1690 break;
1691 if (client.text[0] == '/')
1692 {
1693 munge(&client);
1694 }
1695 else
1696 {
1697 ansi_printf(&client, ansi_text_color_blue, "\r<%s>", user.player);
1698 std::printf(" ");
1699 str_print_term(stdout, client.text, 0, 0);
1700 std::printf("\n");
1701 client.munged = 1;
1702 }
1703
1704 if (!(packet = packet_create(packet_class_bnet)))
1705 {
1706 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1707 psock_close(client.sd);
1708 if (client.changed_in)
1709 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1710 return EXIT_FAILURE;
1711 }
1712 packet_set_size(packet, sizeof(t_client_message));
1713 packet_set_type(packet, CLIENT_MESSAGE);
1714 packet_append_string(packet, client.text);
1715 client_blocksend_packet(client.sd, packet);
1716 packet_del_ref(packet);
1717 break;
1718
1719 default:
1720 /* one of the wait states; erase what they typed */
1721 munge(&client);
1722 break;
1723 }
1724
1725 client.commpos = 0;
1726 client.text[0] = '\0';
1727 break;
1728 }
1729 }
1730
1731 if (PSOCK_FD_ISSET(client.sd, &rfds)) /* got network data */
1732 {
1733 /* rpacket is from server, packet is from client */
1734 switch (net_recv_packet(client.sd, rpacket, &client.currsize))
1735 {
1736 case 0: /* nothing */
1737 break;
1738
1739 case 1:
1740 switch (packet_get_type(rpacket))
1741 {
1742 case SERVER_ECHOREQ: /* might as well support it */
1743 if (packet_get_size(rpacket) < sizeof(t_server_echoreq))
1744 {
1745 munge(&client);
1746 std::printf("Got bad SERVER_ECHOREQ packet (expected %lu bytes, got %u)\n", sizeof(t_server_echoreq), packet_get_size(rpacket));
1747 break;
1748 }
1749
1750 if (!(packet = packet_create(packet_class_bnet)))
1751 {
1752 munge(&client);
1753 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
1754 psock_close(client.sd);
1755 if (client.changed_in)
1756 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
1757 return EXIT_FAILURE;
1758 }
1759 packet_set_size(packet, sizeof(t_client_echoreply));
1760 packet_set_type(packet, CLIENT_ECHOREPLY);
1761 bn_int_set(&packet->u.client_echoreply.ticks, bn_int_get(rpacket->u.server_echoreq.ticks));
1762 client_blocksend_packet(client.sd, packet);
1763 packet_del_ref(packet);
1764
1765 break;
1766
1767 case SERVER_STARTGAME4_ACK:
1768 if (client.mode == mode_gamewait)
1769 {
1770 munge(&client);
1771
1772 if (bn_int_get(rpacket->u.server_startgame4_ack.reply) == SERVER_STARTGAME4_ACK_OK)
1773 {
1774 std::printf("Game created.\n");
1775 client.mode = mode_gamestop;
1776 }
1777 else
1778 {
1779 std::printf("Game could not be created, try another name.\n");
1780 client.mode = mode_gamename;
1781 }
1782 }
1783 break;
1784
1785 case SERVER_CLAN_INVITEREQ:
1786 if (packet_get_size(rpacket) < sizeof(t_server_clan_invitereq))
1787 {
1788 munge(&client);
1789 std::printf("Got bad SERVER_CLAN_INVITEREQ packet (expected %lu bytes, got %u)\n", sizeof(t_server_clan_invitereq), packet_get_size(rpacket));
1790 break;
1791 }
1792
1793 {
1794 char const * clan;
1795 char const * inviter;
1796 int offset;
1797
1798 offset = sizeof(t_server_clan_invitereq);
1799 if (!(clan = packet_get_str_const(rpacket, offset, CLAN_NAME_MAX)))
1800 {
1801 munge(&client);
1802 std::printf("Got SERVER_CLAN_INVITEREQ with bad or missing clanname\n");
1803 break;
1804 }
1805 offset += std::strlen(clan) + 1;
1806 if (!(inviter = packet_get_str_const(rpacket, offset, MAX_USERNAME_LEN)))
1807 {
1808 munge(&client);
1809 std::printf("Got SERVER_CLAN_INVITEREQ with bad or missing inviter\n");
1810 break;
1811 }
1812 user.count = bn_int_get(rpacket->u.server_clan_invitereq.count);
1813 user.clantag = bn_int_get(rpacket->u.server_clan_invitereq.clantag);
1814 if (user.inviter)
1815 xfree((void *)user.inviter);
1816 user.inviter = xstrdup(inviter);
1817
1818 munge(&client);
1819 std::printf("%s invited you to Clan %s\n", inviter, clan);
1820 std::printf("Do you want to accept invitation (yes/no) ? [yes] ");
1821
1822 client.mode = mode_claninvite;
1823 break;
1824 }
1825
1826 case SERVER_CLAN_INVITEREPLY:
1827 if (packet_get_size(rpacket) < sizeof(t_server_clan_invitereply))
1828 {
1829 munge(&client);
1830 std::printf("Got bad SERVER_CLAN_INVITEREPLY packet (expected %lu bytes, got %u)\n", sizeof(t_server_clan_invitereply), packet_get_size(rpacket));
1831 break;
1832 }
1833
1834 {
1835 char result = bn_byte_get(rpacket->u.server_clan_invitereply.result);
1836 std::printf("Recieved result: %i\n", result);
1837 break;
1838 }
1839
1840 case SERVER_CLAN_CREATEINVITEREQ:
1841 if (packet_get_size(rpacket) < sizeof(t_server_clan_createinvitereq))
1842 {
1843 munge(&client);
1844 std::printf("Got bad SERVER_CLAN_CREATEINVITEREQ packet (expected %lu bytes, got %u)\n", sizeof(t_server_clan_createinvitereq), packet_get_size(rpacket));
1845 break;
1846 }
1847
1848 {
1849 char const * clan;
1850 char const * inviter;
1851 char invited_count;
1852 std::size_t offset = sizeof(t_server_clan_createinvitereq);
1853
1854 if (!(clan = packet_get_str_const(rpacket, offset, CLAN_NAME_MAX)))
1855 {
1856 munge(&client);
1857 std::printf("Got SERVER_CLAN_CREATEINVITEREQ with bad or missing clanname\n");
1858 break;
1859 }
1860 offset += std::strlen(clan) + 1;
1861 if (!(inviter = packet_get_str_const(rpacket, offset, MAX_USERNAME_LEN)))
1862 {
1863 munge(&client);
1864 std::printf("Got SERVER_CLAN_CREATEINVITEREQ with bad or missing inviter\n");
1865 break;
1866 }
1867 offset += std::strlen(inviter) + 1;
1868 if (packet_get_size(rpacket) < offset + 4)
1869 {
1870 munge(&client);
1871 std::printf("Got SERVER_CLAN_CREATEINVITEREQ with missing invited count\n");
1872 break;
1873 }
1874 invited_count = *((char *)packet_get_data_const(rpacket, offset, 1));
1875 offset++;
1876
1877 user.count = bn_int_get(rpacket->u.server_clan_invitereq.count);
1878 user.clantag = bn_int_get(rpacket->u.server_clan_invitereq.clantag);
1879 if (user.inviter)
1880 xfree((void *)user.inviter);
1881 user.inviter = xstrdup(inviter);
1882
1883 munge(&client);
1884 std::printf("%s invited you to Clan %s along with %i other players\n", inviter, clan, invited_count);
1885 std::printf("Do you want to accept invitation (yes/no) ? [yes] ");
1886
1887 client.mode = mode_clancreateinvite;
1888 break;
1889 }
1890
1891
1892 case SERVER_CLANMEMBERUPDATE:
1893 if (packet_get_size(rpacket) < sizeof(t_server_clanmemberupdate))
1894 {
1895 munge(&client);
1896 std::printf("Got bad SERVER_CLANMEMBERUPDATE packet (expected %lu bytes, got %u)\n", sizeof(t_server_clanmemberupdate), packet_get_size(rpacket));
1897 break;
1898 }
1899
1900 if (client.mode == mode_claninvite)
1901 break;
1902
1903 {
1904 char const * member;
1905 char rank, online;
1906 char const * rank_p;
1907 char const * online_p;
1908 char const * append_str;
1909 int offset;
1910 char const * rank_str;
1911 char const * online_str;
1912
1913 offset = sizeof(t_server_clanmemberupdate);
1914 if (!(member = packet_get_str_const(rpacket, offset, MAX_USERNAME_LEN)))
1915 {
1916 munge(&client);
1917 std::printf("Got SERVER_CLANMEMBERUPDATE with bad or missing member\n");
1918 break;
1919 }
1920 offset += std::strlen(member) + 1;
1921 if (!(rank_p = (char *)packet_get_data_const(rpacket, offset, 1)))
1922 {
1923 munge(&client);
1924 std::printf("Got SERVER_CLANMEMBERUPDATE with bad or missing rank\n");
1925 break;
1926 }
1927 rank = *rank_p;
1928 offset += 1;
1929 if (!(online_p = (char *)packet_get_data_const(rpacket, offset, 1)))
1930 {
1931 munge(&client);
1932 std::printf("Got SERVER_CLAN_MEMBERUPDATE with bad or missing online status\n");
1933 break;
1934 }
1935 online = *online_p;
1936 offset += 1;
1937 if (!(append_str = packet_get_str_const(rpacket, offset, MAX_USERNAME_LEN)))
1938 {
1939 munge(&client);
1940 std::printf("Got SERVER_CLANMEMBERUPDATE with bad or missing append_str\n");
1941 break;
1942 }
1943
1944 switch (rank)
1945 {
1946 case SERVER_CLAN_MEMBER_NEW:
1947 rank_str = "New clan member";
1948 break;
1949 case SERVER_CLAN_MEMBER_PEON:
1950 rank_str = "Peon";
1951 break;
1952 case SERVER_CLAN_MEMBER_GRUNT:
1953 rank_str = "Grunt";
1954 break;
1955 case SERVER_CLAN_MEMBER_SHAMAN:
1956 rank_str = "Shaman";
1957 break;
1958 case SERVER_CLAN_MEMBER_CHIEFTAIN:
1959 rank_str = "Chieftain";
1960 break;
1961 default:
1962 rank_str = "";
1963
1964 }
1965
1966 switch (online)
1967 {
1968 case SERVER_CLAN_MEMBER_OFFLINE:
1969 online_str = "offline";
1970 break;
1971 case SERVER_CLAN_MEMBER_ONLINE:
1972 online_str = "online";
1973 break;
1974 case SERVER_CLAN_MEMBER_CHANNEL:
1975 online_str = "in channel";
1976 break;
1977 case SERVER_CLAN_MEMBER_GAME:
1978 online_str = "in game";
1979 break;
1980 case SERVER_CLAN_MEMBER_PRIVATE_GAME:
1981 online_str = "in private game";
1982 break;
1983 default:
1984 online_str = "";
1985 }
1986
1987 munge(&client);
1988 std::printf("%s %s is now %s %s\n", rank_str, member, online_str, append_str);
1989 }
1990
1991
1992 case SERVER_STATSREPLY:
1993 if (client.mode == mode_waitstat)
1994 {
1995 unsigned int names, keys;
1996 unsigned int match;
1997 unsigned int strpos;
1998 char const * temp;
1999
2000 munge(&client);
2001
2002 names = bn_int_get(rpacket->u.server_statsreply.name_count);
2003 keys = bn_int_get(rpacket->u.server_statsreply.key_count);
2004 match = bn_int_get(rpacket->u.server_statsreply.requestid);
2005
2006 if (names != 1 || keys != 4 || match != statsmatch)
2007 std::printf("mangled reply (name_count=%u key_count=%u unknown1=%u)\n",
2008 names, keys, match);
2009
2010 strpos = sizeof(t_server_statsreply);
2011 if ((temp = packet_get_str_const(rpacket, strpos, 256)))
2012 {
2013 std::printf(" Sex: ");
2014 ansi_printf(&client, ansi_text_color_yellow, "%s\n", temp);
2015 strpos += std::strlen(temp) + 1;
2016 }
2017 if ((temp = packet_get_str_const(rpacket, strpos, 256)))
2018 {
2019 std::printf(" Age: ");
2020 ansi_printf(&client, ansi_text_color_yellow, "%s\n", temp);
2021 strpos += std::strlen(temp) + 1;
2022 }
2023 if ((temp = packet_get_str_const(rpacket, strpos, 256)))
2024 {
2025 std::printf(" Location: ");
2026 ansi_printf(&client, ansi_text_color_yellow, "%s\n", temp);
2027 strpos += std::strlen(temp) + 1;
2028 }
2029 if ((temp = packet_get_str_const(rpacket, strpos, 256)))
2030 {
2031 char msgtemp[1024];
2032 char * tok;
2033
2034 std::printf(" Description: \n");
2035 if (client.useansi)
2036 ansi_text_color_fore(ansi_text_color_yellow);
2037 std::strncpy(msgtemp, temp, sizeof(msgtemp));
2038 msgtemp[sizeof(msgtemp)-1] = '\0';
2039 for (tok = std::strtok(msgtemp, "\r\n"); tok; tok = std::strtok(NULL, "\r\n"))
2040 std::printf(" %s\n", tok);
2041 if (client.useansi)
2042 ansi_text_reset();
2043 strpos += std::strlen(temp) + 1;
2044 }
2045
2046 if (match == statsmatch)
2047 client.mode = mode_command;
2048 }
2049 break;
2050
2051 case SERVER_PLAYERINFOREPLY: /* hmm. we didn't ask for one... */
2052 break;
2053
2054 case SERVER_MESSAGE:
2055 if (packet_get_size(rpacket) < sizeof(t_server_message))
2056 {
2057 munge(&client);
2058 std::printf("Got bad SERVER_MESSAGE packet (expected %lu bytes, got %u)", sizeof(t_server_message), packet_get_size(rpacket));
2059 break;
2060 }
2061
2062 {
2063 char const * speaker;
2064 char const * message;
2065
2066 if (!(speaker = packet_get_str_const(rpacket, sizeof(t_server_message), 32)))
2067 {
2068 munge(&client);
2069 std::printf("Got SERVER_MESSAGE packet with bad or missing speaker\n");
2070 break;
2071 }
2072 if (!(message = packet_get_str_const(rpacket, sizeof(t_server_message)+std::strlen(speaker) + 1, 1024)))
2073 {
2074 munge(&client);
2075 std::printf("Got SERVER_MESSAGE packet with bad or missing message (speaker=\"%s\" start=%lu len=%u)\n", speaker, sizeof(t_server_message)+std::strlen(speaker) + 1, packet_get_size(rpacket));
2076 break;
2077 }
2078
2079 switch (bn_int_get(rpacket->u.server_message.type))
2080 {
2081 case SERVER_MESSAGE_TYPE_JOIN:
2082 munge(&client);
2083 ansi_printf(&client, ansi_text_color_green, "\"%s\" %s enters\n", speaker,
2084 mflags_get_str(bn_int_get(rpacket->u.server_message.flags)));
2085 break;
2086 case SERVER_MESSAGE_TYPE_CHANNEL:
2087 munge(&client);
2088 ansi_printf(&client, ansi_text_color_green, "Joining channel :\"%s\" %s\n", message,
2089 cflags_get_str(bn_int_get(rpacket->u.server_message.flags)));
2090 break;
2091 case SERVER_MESSAGE_TYPE_ADDUSER:
2092 munge(&client);
2093 ansi_printf(&client, ansi_text_color_green, "\"%s\" %s is here\n", speaker,
2094 mflags_get_str(bn_int_get(rpacket->u.server_message.flags)));
2095 break;
2096 case SERVER_MESSAGE_TYPE_USERFLAGS:
2097 break;
2098 case SERVER_MESSAGE_TYPE_PART:
2099 munge(&client);
2100 ansi_printf(&client, ansi_text_color_green, "\"%s\" %s leaves\n", speaker,
2101 mflags_get_str(bn_int_get(rpacket->u.server_message.flags)));
2102 break;
2103 case SERVER_MESSAGE_TYPE_WHISPER:
2104 munge(&client);
2105 ansi_printf(&client, ansi_text_color_blue, "<From: %s>", speaker);
2106 std::printf(" ");
2107 str_print_term(stdout, message, 0, 0);
2108 std::printf("\n");
2109 break;
2110 case SERVER_MESSAGE_TYPE_WHISPERACK:
2111 munge(&client);
2112 ansi_printf(&client, ansi_text_color_blue, "<To: %s>", speaker);
2113 std::printf(" ");
2114 str_print_term(stdout, message, 0, 0);
2115 std::printf("\n");
2116 break;
2117 case SERVER_MESSAGE_TYPE_BROADCAST:
2118 munge(&client);
2119 ansi_printf(&client, ansi_text_color_yellow, "<Broadcast %s> %s\n", speaker, message);
2120 break;
2121 case SERVER_MESSAGE_TYPE_ERROR:
2122 munge(&client);
2123 ansi_printf(&client, ansi_text_color_red, "<Error> %s\n", message);
2124 break;
2125 case SERVER_MESSAGE_TYPE_INFO:
2126 munge(&client);
2127 ansi_printf(&client, ansi_text_color_yellow, "<Info> %s\n", message);
2128 break;
2129 case SERVER_MESSAGE_TYPE_EMOTE:
2130 munge(&client);
2131 ansi_printf(&client, ansi_text_color_yellow, "<%s %s>\n", speaker, message);
2132 break;
2133 default:
2134 case SERVER_MESSAGE_TYPE_TALK:
2135 munge(&client);
2136 ansi_printf(&client, ansi_text_color_yellow, "<%s>", speaker);
2137 std::printf(" ");
2138 str_print_term(stdout, message, 0, 0);
2139 std::printf("\n");
2140 }
2141 }
2142 break;
2143
2144 default:
2145 munge(&client);
2146 std::printf("Unsupported server packet type 0x%04x\n", packet_get_type(rpacket));
2147 hexdump(stdout, packet_get_data_const(rpacket, 0, packet_get_size(rpacket)), packet_get_size(rpacket));
2148 std::printf("\n");
2149 }
2150
2151 client.currsize = 0;
2152 break;
2153
2154 case -1: /* error (probably connection closed) */
2155 default:
2156 munge(&client);
2157 std::printf("----\nConnection closed by server.\n");
2158 psock_close(client.sd);
2159 if (client.changed_in)
2160 tcsetattr(client.fd_stdin, TCSAFLUSH, &client.in_attr_old);
2161 return EXIT_SUCCESS;
2162 }
2163 }
2164 }
2165 }
2166
2167 /* not reached */
2168 }