1 /* $Id$ */
2 /* Main console module */
3
4 /*
5 * This is intentionally minimal, as system specific stuff may
6 * need be done in "main" (or "WinMain" for Windows systems).
7 * The real non-system-specific initialization is done in
8 * "c-init.c".
9 */
10
11 #include "angband.h"
12
13 client_opts c_cfg;
14
main(int argc,char ** argv)15 int main(int argc, char **argv)
16 {
17 int i;
18
19 /* Save the program name */
20 argv0 = argv[0];
21
22 /* Acquire the version strings */
23 version_build();
24
25 /* Process the command line arguments */
26 for (i = 1; argv && (i < argc); i++)
27 {
28 /* Require proper options */
29 if (argv[i][0] != '-')
30 {
31 strcpy(server_name, argv[i]);
32 continue;
33 }
34
35 /* Analyze option */
36 switch (argv[i][1])
37 {
38 case 'p':
39 {
40 cfg_console_port = atoi(&argv[i][2]);
41 break;
42 }
43 case 'P':
44 {
45 strcpy(pass, argv[i]+2);
46 break;
47 }
48 case 'c':
49 {
50 force_cui = TRUE;
51 break;
52 }
53 case 's':
54 {
55 server_shutdown = TRUE;
56 break;
57 }
58
59 default:
60 /* Dump usage information */
61 puts(longVersion);
62 puts("Usage : tomenet.console [options] [servername]");
63 puts("Example: tomenet.console -Pmypass -c -s my.server.net");
64 puts(" -c Always use CUI(GCU) interface");
65 puts(" -p<num> Change console port number");
66 puts(" -P<pass> Specify password");
67 puts(" -s Shutdown the server immediataly");
68 quit(NULL);
69 }
70 }
71
72 /* Call the initialization function */
73 console_init();
74
75 return 0;
76 }
77
get_player_list(void)78 static bool get_player_list(void)
79 {
80 Packet_printf(&ibuf, "%c", CONSOLE_STATUS);
81
82 return TRUE;
83 }
84
get_artifact_list(void)85 static bool get_artifact_list(void)
86 {
87 Packet_printf(&ibuf, "%c", CONSOLE_ARTIFACT_LIST);
88
89 return TRUE;
90 }
91
get_unique_list(void)92 static bool get_unique_list(void)
93 {
94 Packet_printf(&ibuf, "%c", CONSOLE_UNIQUE_LIST);
95
96 return TRUE;
97 }
98
modify_artifact(void)99 static bool modify_artifact(void)
100 {
101 int artifact;
102 char ch;
103
104 /* Clear screen */
105 Term_clear();
106
107 /* Request artifact number */
108 artifact = c_get_quantity("Artifact to modify (by number): ", MAX_A_IDX);
109
110 /* Check for bad numbers */
111 if (!artifact) return FALSE;
112
113 /* Print menu */
114 prt("Changing artifact status", 1, 1);
115
116 prt("(1) Make findable", 3, 5);
117 prt("(2) Make unfindable", 4, 5);
118 prt("(3) Abort", 5, 5);
119
120 /* Prompt */
121 prt("Enter choice: ", 7, 1);
122
123 /* Try until done */
124 while (1)
125 {
126 /* Get key */
127 ch = inkey();
128
129 /* Check for bad key */
130 if (!ch) return FALSE;
131
132 /* Check for command */
133 switch (ch)
134 {
135 case '1':
136 Packet_printf(&ibuf, "%c%d%d", CONSOLE_CHANGE_ARTIFACT, artifact, 0);
137 return TRUE;
138 case '2':
139 Packet_printf(&ibuf, "%c%d%d", CONSOLE_CHANGE_ARTIFACT, artifact, 1);
140 return TRUE;
141 case '3':
142 return FALSE;
143 }
144 }
145 }
146
modify_unique(void)147 static bool modify_unique(void)
148 {
149 int unique;
150 char killer[80], ch;
151
152 /* Clear screen */
153 Term_clear();
154
155 /* Request unique number */
156 unique = c_get_quantity("Unique to modify (by number): ", MAX_R_IDX);
157
158 /* Check for bad numbers */
159 if (!unique) return FALSE;
160
161 /* Print menu */
162 prt("Changing unique status", 1, 1);
163
164 prt("(1) Kill unique", 3, 5);
165 prt("(2) Resurrect unique", 4, 5);
166 prt("(3) Abort", 5, 5);
167
168 /* Prompt */
169 prt("Enter choice: ", 7, 1);
170
171 /* Try until done */
172 while (1)
173 {
174 /* Get key */
175 ch = inkey();
176
177 /* Check for bad key */
178 if (!ch) return FALSE;
179
180 /* Check for command */
181 switch (ch)
182 {
183 case '1':
184 /* Prompt for killer name */
185 prt("Enter the killer's name: ", 10, 1);
186
187 /* Default */
188 strcpy(killer, "nobody");
189
190 /* Get an input */
191 askfor_aux(killer, 80, 0);
192
193 /* Never send a null-length string */
194 if (!strlen(killer)) strcpy(killer, "nobody");
195
196 /* Send command */
197 Packet_printf(&ibuf, "%c%d%s", CONSOLE_CHANGE_UNIQUE, unique, killer);
198
199 return TRUE;
200 case '2':
201 Packet_printf(&ibuf, "%c%d%s", CONSOLE_CHANGE_UNIQUE, unique, "nobody");
202 return TRUE;
203 case '3':
204 return FALSE;
205 }
206 }
207 }
208
get_player_info(void)209 static bool get_player_info(void)
210 {
211 return FALSE;
212 }
213
send_message(void)214 static bool send_message(void)
215 {
216 char buf[80];
217
218 /* Clear screen */
219 Term_clear();
220
221 /* Empty message */
222 strcpy(buf, "");
223
224 /* Get message */
225 get_string("Message: ", buf, 79);
226
227 /* Send it */
228 Packet_printf(&ibuf, "%c%s", CONSOLE_MESSAGE, buf);
229
230 return TRUE;
231 }
232
kick_player(void)233 static bool kick_player(void)
234 {
235 char name[80];
236
237 /* Clear screen */
238 Term_clear();
239
240 /* Title */
241 prt("Kick player", 1, 1);
242
243 /* Prompt for name */
244 prt("Kick who: ", 3, 1);
245
246 /* Default */
247 strcpy(name, "");
248
249 /* Get name */
250 askfor_aux(name, 80, 0);
251
252 /* Never send null-length string */
253 if (!strlen(name)) return FALSE;
254
255 /* Send command */
256 Packet_printf(&ibuf, "%c%s", CONSOLE_KICK_PLAYER, name);
257
258 return TRUE;
259 }
260
reload_server_preferences()261 static bool reload_server_preferences()
262 {
263 Packet_printf(&ibuf, "%c", CONSOLE_RELOAD_SERVER_PREFERENCES);
264 return TRUE;
265 }
266
shutdown_server(void)267 static bool shutdown_server(void)
268 {
269 char ch;
270
271 /* Clear screen */
272 Term_clear();
273
274 /* Title */
275 prt("Server shutdown", 1, 1);
276
277 /* Prompt */
278 prt("Really shut the server down [y/n]: ", 3, 1);
279
280 /* Get key */
281 ch = inkey();
282
283 /* Check for anything except 'y' */
284 if (ch != 'y' && ch != 'Y') return FALSE;
285
286 /* Send the command */
287 Packet_printf(&ibuf, "%c", CONSOLE_SHUTDOWN);
288
289 return TRUE;
290 }
291
quit_console(void)292 static bool quit_console(void)
293 {
294 quit(NULL);
295
296 return TRUE;
297 }
298
process_command(void)299 bool process_command(void)
300 {
301 /* Convert to lowercase if necessary */
302 if (command_cmd >= 'A' && command_cmd <= 'Z')
303 {
304 command_cmd += 'a' - 'A';
305 }
306
307 /* Check the command */
308 switch(command_cmd)
309 {
310 case 'a': return get_player_list();
311 break;
312 case 'b': return get_artifact_list();
313 break;
314 case 'c': return get_unique_list();
315 break;
316 case 'd': return modify_artifact();
317 break;
318 case 'e': return modify_unique();
319 break;
320 case 'f': return get_player_info();
321 break;
322 case 'g': return send_message();
323 break;
324 case 'h': return kick_player();
325 break;
326 case 'i': return reload_server_preferences();
327 break;
328 case 'j': return shutdown_server();
329 break;
330 case 'k': return quit_console();
331 break;
332 default: /* Bad command */
333 return FALSE;
334 }
335
336 /* Oops.... */
337 return FALSE;
338 }
339
show_status(void)340 static void show_status(void)
341 {
342 int i, j, n, lev;
343 char buf[1024], name[1024], race[80], class[80];
344 struct worldpos wpos;
345
346 /* Clear screen */
347 Term_clear();
348
349 /* Extract number of players */
350 Packet_scanf(&ibuf, "%d", &n);
351
352 /* Show number */
353 sprintf(buf, "%d player%s in the game.", n, ((n == 1) ? " is" : "s are"));
354
355 /* Put on screen */
356 prt(buf, 1, 1);
357
358 /* Initialize line counter */
359 j = 3;
360
361 /* Print each player's info */
362 for (i = 1; i <= n; i++)
363 {
364 /* Check for end of page */
365 if (j == 21)
366 {
367 /* Prompt */
368 prt("[Hit any key to continue]", 23, 1);
369
370 /* Refresh */
371 Term_fresh();
372
373 /* Wait for key */
374 inkey();
375
376 /* Start at the top */
377 j = 3;
378
379 /* Clear screen */
380 Term_clear();
381
382 /* Show number */
383 sprintf(buf, "%d player%s in the game.", n, ((n == 1) ? " is" : "s are"));
384
385 /* Title */
386 prt(buf, 1, 1);
387 }
388
389 /* Extract info */
390 Packet_scanf(&ibuf, "%s%s%s%d%d%d%d", name, race, class, &lev, &wpos.wx, &wpos.wy, &wpos.wz);
391
392 /* Format the string */
393 sprintf(buf, "%s the %s %s (Level %d) at %d feet of (%d,%d)", name, race, class, lev, wpos.wz*50, wpos.wx, wpos.wy);
394
395 /* Print it */
396 prt(buf, j, 1);
397
398 /* Next line */
399 j++;
400 }
401
402 /* Prompt */
403 prt("[Hit any key to exit]", 23, 1);
404
405 /* Wait */
406 inkey();
407 }
408
409
show_artifact_list(void)410 static void show_artifact_list(void)
411 {
412 int i, j, n, number;
413 char name[80], buf[1024];
414
415 /* Clear screen */
416 Term_clear();
417
418 /* Extract number of artifacts */
419 Packet_scanf(&ibuf, "%d", &n);
420
421 /* Title */
422 prt("Artifact list", 1, 1);
423
424 /* Initialize line counter */
425 j = 3;
426
427 /* Print each artifact info */
428 for (i = 1; i <= n; i++)
429 {
430 /* Check for end of page */
431 if (j == 21)
432 {
433 /* Prompt */
434 prt("[Hit any key to continue]", 23, 1);
435
436 /* Refresh */
437 Term_fresh();
438
439 /* Wait for key */
440 inkey();
441
442 /* Start at the top */
443 j = 3;
444
445 /* Clear screen */
446 Term_clear();
447
448 /* Title */
449 prt("Artifact list", 1, 1);
450 }
451
452 /* Extract info */
453 Packet_scanf(&ibuf, "%d%s", &number, name);
454
455 /* Format the string */
456 sprintf(buf, "%3d) %s", number, name);
457
458 /* Print it */
459 prt(buf, j, 1);
460
461 /* Next line */
462 j++;
463 }
464
465 /* Prompt */
466 prt("[Hit any key to exit]", 23, 1);
467
468 /* Wait */
469 inkey();
470 }
471
show_unique_list(void)472 static void show_unique_list(void)
473 {
474 int i, j, n, number;
475 char name[80], buf[1024];
476
477 /* Clear screen */
478 Term_clear();
479
480 /* Extract number of artifacts */
481 Packet_scanf(&ibuf, "%d", &n);
482
483 /* Title */
484 prt("Unique list", 1, 1);
485
486 /* Initialize line counter */
487 j = 3;
488
489 /* Print each artifact info */
490 for (i = 1; i <= n; i++)
491 {
492 /* Check for end of page */
493 if (j == 21)
494 {
495 /* Prompt */
496 prt("[Hit any key to continue]", 23, 1);
497
498 /* Refresh */
499 Term_fresh();
500
501 /* Wait for key */
502 inkey();
503
504 /* Start at the top */
505 j = 3;
506
507 /* Clear */
508 Term_clear();
509
510 /* Title */
511 prt("Unique list", 1, 1);
512 }
513
514 /* Extract info */
515 Packet_scanf(&ibuf, "%d%s", &number, name);
516
517 /* Format the string */
518 sprintf(buf, "%3d) %s", number, name);
519
520 /* Print it */
521 prt(buf, j, 1);
522
523 /* Next line */
524 j++;
525 }
526
527 /* Prompt */
528 prt("[Hit any key to exit]", 23, 1);
529
530 /* Wait */
531 inkey();
532 }
533
show_generic_reply(void)534 static void show_generic_reply(void)
535 {
536 char status;
537
538 /* Extract reply */
539 Packet_scanf(&ibuf, "%c", &status);
540
541 /* Check for success */
542 if (status)
543 {
544 /* Message */
545 prt("Command succeeded!", 15, 1);
546 }
547 else
548 {
549 /* Message */
550 prt("Command failed!", 15, 1);
551 }
552
553 /* Prompt */
554 prt("[Hit any key to exit]", 23, 1);
555
556 /* Wait */
557 inkey();
558 }
559
process_reply(void)560 void process_reply(void)
561 {
562 char ch;
563
564 /* Extract reply type */
565 Packet_scanf(&ibuf, "%c", &ch);
566
567 /* Determine what to do */
568 switch (ch)
569 {
570 case CONSOLE_STATUS:
571 show_status();
572 break;
573 case CONSOLE_ARTIFACT_LIST:
574 show_artifact_list();
575 break;
576 case CONSOLE_UNIQUE_LIST:
577 show_unique_list();
578 break;
579 case CONSOLE_CHANGE_ARTIFACT:
580 show_generic_reply();
581 break;
582 case CONSOLE_CHANGE_UNIQUE:
583 show_generic_reply();
584 break;
585 case CONSOLE_KICK_PLAYER:
586 show_generic_reply();
587 break;
588 case CONSOLE_MESSAGE:
589 break;
590 case CONSOLE_SHUTDOWN:
591 quit("Server shutdown");
592 break;
593 case CONSOLE_DENIED:
594 quit("Denied access");
595 break;
596 }
597 }
598