OSDN Git Service

First version
[st-ro/stro.git] / src / char / char_cnslif.c
1 // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
2 // For more information, see LICENCE in the main folder
3
4 #include "../common/socket.h"
5 #include "../common/showmsg.h"
6 #include "../common/timer.h"
7 #include "../common/ers.h"
8 #include "../common/cli.h"
9 #include "char.h"
10 #include "char_cnslif.h"
11
12 #include <stdlib.h>
13 #include <string.h>
14
15 /*======================================================
16  * Login-Server help option info
17  *------------------------------------------------------*/
18 void display_helpscreen(bool do_exit)
19 {
20         ShowInfo("Usage: %s [options]\n", SERVER_NAME);
21         ShowInfo("\n");
22         ShowInfo("Options:\n");
23         ShowInfo("  -?, -h [--help]\t\tDisplays this help screen.\n");
24         ShowInfo("  -v [--version]\t\tDisplays the server's version.\n");
25         ShowInfo("  --run-once\t\t\tCloses server after loading (testing).\n");
26         ShowInfo("  --char-config <file>\t\tAlternative char-server configuration.\n");
27         ShowInfo("  --lan-config <file>\t\tAlternative lag configuration.\n");
28         ShowInfo("  --inter-config <file>\t\tAlternative inter-server configuration.\n");
29         ShowInfo("  --msg-config <file>\t\tAlternative message configuration.\n");
30         if( do_exit )
31                 exit(EXIT_SUCCESS);
32 }
33
34 /**
35  * Timered function to check if the console has a new event to be read.
36  * @param tid: timer id
37  * @param tick: tick of execution
38  * @param id: user account id
39  * @param data: unused
40  * @return 0
41  */
42 int cnslif_console_timer(int tid, unsigned int tick, int id, intptr_t data) {
43         char buf[MAX_CONSOLE_IN]; //max cmd atm is 63+63+63+3+3
44
45         memset(buf,0,MAX_CONSOLE_IN); //clear out buf
46
47         if(cli_hasevent()){
48                 if(fgets(buf, MAX_CONSOLE_IN, stdin)==NULL)
49                         return -1;
50                 else if(strlen(buf)>MIN_CONSOLE_IN)
51                         cnslif_parse(buf);
52         }
53         return 0;
54 }
55
56 // Console Command Parser [Wizputer]
57 int cnslif_parse(const char* buf)
58 {
59         char type[64];
60         char command[64];
61         int n=0;
62
63         if( ( n = sscanf(buf, "%63[^:]:%63[^\n]", type, command) ) < 2 ){
64                 if((n = sscanf(buf, "%63[^\n]", type))<1) return -1; //nothing to do no arg
65         }
66         if( n != 2 ){ //end string
67                 ShowNotice("Type: '%s'\n",type);
68                 command[0] = '\0';
69         }
70         else
71                 ShowNotice("Type of command: '%s' || Command: '%s'\n",type,command);
72
73         if( n == 2 && strcmpi("server", type) == 0 ){
74                 if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 ){
75                         runflag = 0;
76                 }
77                 else if( strcmpi("alive", command) == 0 || strcmpi("status", command) == 0 )
78                         ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
79                 else if( strcmpi("reloadconf", command) == 0 ) {
80                         ShowInfo("Reloading config file \"%s\"\n", CHAR_CONF_NAME);
81                         char_config_read(CHAR_CONF_NAME, false);
82                 }
83         }
84         else if( strcmpi("ers_report", type) == 0 ){
85                 ers_report();
86         }
87         else if( strcmpi("help", type) == 0 ){
88                 ShowInfo("Available commands:\n");
89                 ShowInfo("\t server:shutdown => Stops the server.\n");
90                 ShowInfo("\t server:alive => Checks if the server is running.\n");
91                 ShowInfo("\t server:reloadconf => Reload config file: \"%s\"\n", CHAR_CONF_NAME);
92                 ShowInfo("\t ers_report => Displays database usage.\n");
93         }
94
95         return 0;
96 }
97
98 void do_init_chcnslif(void){
99         if( charserv_config.console ){ //start listening
100                 add_timer_func_list(cnslif_console_timer, "cnslif_console_timer");
101                 add_timer_interval(gettick()+1000, cnslif_console_timer, 0, 0, 1000); //start in 1s each 1sec
102         }
103 }