1 | // (c) eAthena Dev Team - Licensed under GNU GPL |
---|
2 | // For more information, see LICENCE in the main folder |
---|
3 | |
---|
4 | #include "../common/cbasetypes.h" |
---|
5 | #include "../common/mmo.h" |
---|
6 | #include "../common/core.h" |
---|
7 | #include "../common/db.h" |
---|
8 | #include "../common/showmsg.h" |
---|
9 | #include "../common/sql.h" |
---|
10 | #include "../common/malloc.h" |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | |
---|
16 | char login_account_id[256]="account_id"; |
---|
17 | char login_userid[256]="userid"; |
---|
18 | char login_user_pass[256]="user_pass"; |
---|
19 | char login_db[256]="login"; |
---|
20 | char globalreg_db[256]="global_reg_value"; |
---|
21 | |
---|
22 | static DBMap* gm_account_db=NULL; // int account_id -> struct gm_account* |
---|
23 | |
---|
24 | int db_server_port = 3306; |
---|
25 | char db_server_ip[32] = "127.0.0.1"; |
---|
26 | char db_server_id[32] = "ragnarok"; |
---|
27 | char db_server_pw[32] = "ragnarok"; |
---|
28 | char db_server_logindb[32] = "ragnarok"; |
---|
29 | |
---|
30 | #define INTER_CONF_NAME "conf/inter_athena.conf" |
---|
31 | #define GM_ACCOUNT_NAME "conf/GM_account.txt" |
---|
32 | #define ACCOUNT_TXT_NAME "save/account.txt" |
---|
33 | //-------------------------------------------------------- |
---|
34 | |
---|
35 | int isGM(int account_id) |
---|
36 | { |
---|
37 | struct gm_account* p = (struct gm_account*)idb_get(gm_account_db,account_id); |
---|
38 | return( p != NULL ) ? p->level : 0; |
---|
39 | } |
---|
40 | |
---|
41 | int read_gm_account() |
---|
42 | { |
---|
43 | char line[8192]; |
---|
44 | struct gm_account *p; |
---|
45 | FILE *fp; |
---|
46 | int line_counter = 0, gm_counter = 0; |
---|
47 | |
---|
48 | ShowStatus("Starting reading gm_account\n"); |
---|
49 | |
---|
50 | if( (fp = fopen(GM_ACCOUNT_NAME,"r")) == NULL ) |
---|
51 | return 1; |
---|
52 | |
---|
53 | gm_account_db = idb_alloc(DB_OPT_RELEASE_DATA); |
---|
54 | |
---|
55 | while(fgets(line,sizeof(line),fp)) |
---|
56 | { |
---|
57 | line_counter++; |
---|
58 | if ((line[0] == '/' && line[1] == '/') || line[0] == '\0' || line[0] == '\n' || line[0] == '\r') |
---|
59 | continue; |
---|
60 | |
---|
61 | p = (struct gm_account*)aMalloc(sizeof(struct gm_account)); |
---|
62 | if(p==NULL){ |
---|
63 | ShowFatalError("gm_account: out of memory!\n"); |
---|
64 | exit(EXIT_FAILURE); |
---|
65 | } |
---|
66 | |
---|
67 | if(sscanf(line,"%d %d",&p->account_id,&p->level) != 2 || p->level <= 0) { |
---|
68 | ShowWarning("gm_account: unsupported data format [conf/GM_account.txt] on line %d\n", line_counter); |
---|
69 | continue; |
---|
70 | } |
---|
71 | else { |
---|
72 | if(p->level > 99) |
---|
73 | p->level = 99; |
---|
74 | p = (struct gm_account*)idb_put(gm_account_db,p->account_id,p); |
---|
75 | if( p ) |
---|
76 | aFree(p);// old entry replaced |
---|
77 | gm_counter++; |
---|
78 | ShowInfo("GM ID: %d Level: %d\n",p->account_id,p->level); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | fclose(fp); |
---|
83 | ShowStatus("%d ID of gm_accounts read.\n", gm_counter); |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | int convert_login(void) |
---|
88 | { |
---|
89 | Sql* mysql_handle; |
---|
90 | SqlStmt* stmt; |
---|
91 | int line_counter = 0; |
---|
92 | FILE *fp; |
---|
93 | int account_id, logincount, user_level, state, n, i; |
---|
94 | char line[2048], userid[2048], pass[2048], lastlogin[2048], sex, email[2048], error_message[2048], last_ip[2048], memo[2048]; |
---|
95 | int unban_time, expiration_time; |
---|
96 | char dummy[2048]; |
---|
97 | |
---|
98 | mysql_handle = Sql_Malloc(); |
---|
99 | if ( SQL_ERROR == Sql_Connect(mysql_handle, db_server_id, db_server_pw, db_server_ip, db_server_port, db_server_logindb) ) |
---|
100 | { |
---|
101 | Sql_ShowDebug(mysql_handle); |
---|
102 | Sql_Free(mysql_handle); |
---|
103 | exit(EXIT_FAILURE); |
---|
104 | } |
---|
105 | ShowStatus("Connect: Success!\n"); |
---|
106 | |
---|
107 | ShowStatus("Convert start...\n"); |
---|
108 | fp = fopen(ACCOUNT_TXT_NAME,"r"); |
---|
109 | if(fp == NULL) |
---|
110 | return 0; |
---|
111 | |
---|
112 | while(fgets(line,sizeof(line),fp) != NULL) |
---|
113 | { |
---|
114 | line_counter++; |
---|
115 | if(line[0]=='/' && line[1]=='/') |
---|
116 | continue; |
---|
117 | |
---|
118 | i = sscanf(line, "%d\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%d\t%[^\t]\t%[^\t]\t%d\t%[^\t]\t%[^\t]\t%d\t%[^\r\n]%n", |
---|
119 | &account_id, userid, pass, lastlogin, &sex, &logincount, &state, |
---|
120 | email, error_message, &expiration_time, last_ip, memo, &unban_time, dummy, &n); |
---|
121 | |
---|
122 | if (i < 13) { |
---|
123 | ShowWarning("Skipping incompatible data on line %d\n", line_counter); |
---|
124 | continue; |
---|
125 | } |
---|
126 | |
---|
127 | if (i > 13) |
---|
128 | ShowWarning("Reading login account variables is not implemented, data will be lost! (line %d)\n", line_counter); |
---|
129 | |
---|
130 | user_level = isGM(account_id); |
---|
131 | ShowInfo("Converting user (id: %d, name: %s, gm level: %d)\n", account_id, userid, user_level); |
---|
132 | |
---|
133 | stmt = SqlStmt_Malloc(mysql_handle); |
---|
134 | if( SQL_ERROR == SqlStmt_Prepare(stmt, |
---|
135 | "REPLACE INTO `login` " |
---|
136 | "(`account_id`, `userid`, `user_pass`, `lastlogin`, `sex`, `logincount`, `email`, `level`, `error_message`, `expiration_time`, `last_ip`, `memo`, `unban_time`, `state`) " |
---|
137 | "VALUES " |
---|
138 | "(%d, ?, ?, '%s', '%c', %d, '%s', %d, '%s', %d, '%s', '%s', %d, %d)", |
---|
139 | account_id, lastlogin, sex, logincount, email, user_level, error_message, expiration_time, last_ip, memo, unban_time, state) |
---|
140 | || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_STRING, userid, strnlen(userid, 255)) |
---|
141 | || SQL_ERROR == SqlStmt_BindParam(stmt, 1, SQLDT_STRING, pass, strnlen(pass, 32)) |
---|
142 | || SQL_ERROR == SqlStmt_Execute(stmt) ) |
---|
143 | { |
---|
144 | SqlStmt_ShowDebug(stmt); |
---|
145 | } |
---|
146 | SqlStmt_Free(stmt); |
---|
147 | |
---|
148 | //TODO: parse the rest of the line to read the login-stored account variables, and import them to `global_reg_value` |
---|
149 | // then remove the 'dummy' buffer |
---|
150 | } |
---|
151 | fclose(fp); |
---|
152 | Sql_Free(mysql_handle); |
---|
153 | |
---|
154 | ShowStatus("Convert end...\n"); |
---|
155 | |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | int login_config_read(const char* cfgName) |
---|
160 | { |
---|
161 | int i; |
---|
162 | char line[1024], w1[1024], w2[1024]; |
---|
163 | FILE *fp; |
---|
164 | |
---|
165 | ShowStatus("Start reading interserver configuration: %s\n", cfgName); |
---|
166 | |
---|
167 | fp=fopen(cfgName,"r"); |
---|
168 | if(fp==NULL){ |
---|
169 | ShowError("File not found: %s\n", cfgName); |
---|
170 | return 1; |
---|
171 | } |
---|
172 | |
---|
173 | while(fgets(line, sizeof(line), fp)) |
---|
174 | { |
---|
175 | if(line[0] == '/' && line[1] == '/') |
---|
176 | continue; |
---|
177 | |
---|
178 | i=sscanf(line,"%[^:]:%s", w1, w2); |
---|
179 | if(i!=2) |
---|
180 | continue; |
---|
181 | |
---|
182 | //add for DB connection |
---|
183 | if(strcmpi(w1,"db_server_ip")==0){ |
---|
184 | strcpy(db_server_ip, w2); |
---|
185 | ShowStatus("set db_server_ip : %s\n",w2); |
---|
186 | } |
---|
187 | else if(strcmpi(w1,"db_server_port")==0){ |
---|
188 | db_server_port=atoi(w2); |
---|
189 | ShowStatus("set db_server_port : %s\n",w2); |
---|
190 | } |
---|
191 | else if(strcmpi(w1,"db_server_id")==0){ |
---|
192 | strcpy(db_server_id, w2); |
---|
193 | ShowStatus("set db_server_id : %s\n",w2); |
---|
194 | } |
---|
195 | else if(strcmpi(w1,"db_server_pw")==0){ |
---|
196 | strcpy(db_server_pw, w2); |
---|
197 | ShowStatus("set db_server_pw : %s\n",w2); |
---|
198 | } |
---|
199 | else if(strcmpi(w1,"db_server_logindb")==0){ |
---|
200 | strcpy(db_server_logindb, w2); |
---|
201 | ShowStatus("set db_server_logindb : %s\n",w2); |
---|
202 | } |
---|
203 | //support the import command, just like any other config |
---|
204 | else if(strcmpi(w1,"import")==0){ |
---|
205 | login_config_read(w2); |
---|
206 | } |
---|
207 | } |
---|
208 | fclose(fp); |
---|
209 | ShowStatus("End reading interserver configuration...\n"); |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | int do_init(int argc, char** argv) |
---|
214 | { |
---|
215 | int input; |
---|
216 | login_config_read( (argc > 1) ? argv[1] : INTER_CONF_NAME ); |
---|
217 | read_gm_account(); |
---|
218 | |
---|
219 | ShowInfo("\nWarning : Make sure you backup your databases before continuing!\n"); |
---|
220 | ShowInfo("\nDo you wish to convert your Login Database to SQL? (y/n) : "); |
---|
221 | input = getchar(); |
---|
222 | if(input == 'y' || input == 'Y') |
---|
223 | convert_login(); |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | void do_final(void) |
---|
228 | { |
---|
229 | if( gm_account_db ) |
---|
230 | { |
---|
231 | db_destroy(gm_account_db); |
---|
232 | gm_account_db = NULL; |
---|
233 | } |
---|
234 | } |
---|