summaryrefslogtreecommitdiff
path: root/src/txt-converter/login-converter.c
blob: 114ec77359ce2a3f9699719919b0bdeffd6a21b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// (c) eAthena Dev Team - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include "../common/cbasetypes.h"
#include "../common/mmo.h"
#include "../common/core.h"
#include "../common/db.h"
#include "../common/showmsg.h"
#include "../common/sql.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char login_account_id[256]="account_id";
char login_userid[256]="userid";
char login_user_pass[256]="user_pass";
char login_db[256]="login";
char globalreg_db[256]="global_reg_value";

static struct dbt *gm_account_db;

int db_server_port = 3306;
char db_server_ip[32] = "127.0.0.1";
char db_server_id[32] = "ragnarok";
char db_server_pw[32] = "ragnarok";
char db_server_logindb[32] = "ragnarok";

#define INTER_CONF_NAME "conf/inter_athena.conf"
#define GM_ACCOUNT_NAME "conf/GM_account.txt"
#define ACCOUNT_TXT_NAME "save/account.txt"
//--------------------------------------------------------

int isGM(int account_id)
{
	struct gm_account *p;
	p = idb_get(gm_account_db,account_id);
	if( p == NULL)
		return 0;
	return p->level;
}

int read_gm_account()
{
	char line[8192];
	struct gm_account *p;
	FILE *fp;
	int line_counter = 0, gm_counter = 0;
	
	ShowStatus("Starting reading gm_account\n");
	
	if( (fp = fopen(GM_ACCOUNT_NAME,"r")) == NULL )
		return 1;
	
	gm_account_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int)); //FIXME: never deallocated
	
	while(fgets(line,sizeof(line),fp))
	{
		line_counter++;
		if ((line[0] == '/' && line[1] == '/') || line[0] == '\0' || line[0] == '\n' || line[0] == '\r')
			continue;
		
		p = (struct gm_account*)malloc(sizeof(struct gm_account));
		if(p==NULL){
			ShowFatalError("gm_account: out of memory!\n");
			exit(0);
		}
		
		if(sscanf(line,"%d %d",&p->account_id,&p->level) != 2 || p->level <= 0) {
			ShowWarning("gm_account: unsupported data format [conf/GM_account.txt] on line %d\n", line_counter);
			continue;
		}
		else {
			if(p->level > 99)
				p->level = 99;
			idb_put(gm_account_db,p->account_id,p);
			gm_counter++;
			ShowInfo("GM ID: %d Level: %d\n",p->account_id,p->level);
		}
	}

	fclose(fp);
	ShowStatus("%d ID of gm_accounts read.\n", gm_counter);
	return 0;
}

int convert_login(void)
{
	Sql* mysql_handle;
	SqlStmt* stmt;
	int line_counter = 0;
	FILE *fp;
	int account_id, logincount, user_level, state, n, i;
	char line[2048], userid[2048], pass[2048], lastlogin[2048], sex, email[2048], error_message[2048], last_ip[2048], memo[2048];
	time_t ban_until_time;
	time_t connect_until_time;
	char dummy[2048];

	mysql_handle = Sql_Malloc();
	if ( SQL_ERROR == Sql_Connect(mysql_handle, db_server_id, db_server_pw, db_server_ip, db_server_port, db_server_logindb) )
	{
		Sql_ShowDebug(mysql_handle);
		Sql_Free(mysql_handle);
		exit(1);
	}
	ShowStatus("Connect: Success!\n");
	
	ShowStatus("Convert start...\n");
	fp = fopen(ACCOUNT_TXT_NAME,"r");
	if(fp == NULL)
		return 0;

	while(fgets(line,sizeof(line),fp) != NULL)
	{
		line_counter++;
		if(line[0]=='/' && line[1]=='/')
			continue;

		i = sscanf(line, "%d\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%d\t%[^\t]\t%[^\t]\t%ld\t%[^\t]\t%[^\t]\t%ld\t%[^\r\n]%n",
			&account_id, userid, pass, lastlogin, &sex, &logincount, &state,
			email, error_message, &connect_until_time, last_ip, memo, &ban_until_time, dummy, &n);

		if (i < 13) {
			ShowWarning("Skipping incompatible data on line %d\n", line_counter);
			continue;
 		}

		if (i > 13)
			ShowWarning("Reading login account variables is not implemented, data will be lost! (line %d)\n", line_counter);

		user_level = isGM(account_id);
		ShowInfo("Converting user (id: %d, name: %s, gm level: %d)\n", account_id, userid, user_level);
		
		stmt = SqlStmt_Malloc(mysql_handle);
		if( SQL_ERROR == SqlStmt_Prepare(stmt, 
			"REPLACE INTO `login` "
			"(`account_id`, `userid`, `user_pass`, `lastlogin`, `sex`, `logincount`, `email`, `level`, `error_message`, `connect_until`, `last_ip`, `memo`, `ban_until`, `state`) "
			"VALUES "
			"(%d, ?, ?, '%s', '%c', %d, '%s', %d, '%s', %u, '%s', '%s', %u, %d)",
			account_id, lastlogin, sex, logincount, email, user_level, error_message, (uint32)connect_until_time, last_ip, memo, (uint32)ban_until_time, state)
		||	SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_STRING, userid, strnlen(userid, 255))
		||	SQL_ERROR == SqlStmt_BindParam(stmt, 1, SQLDT_STRING, pass, strnlen(pass, 32))
		||	SQL_ERROR == SqlStmt_Execute(stmt) )
		{
			SqlStmt_ShowDebug(stmt);
		}
		SqlStmt_Free(stmt);
	
		//TODO: parse the rest of the line to read the login-stored account variables, and import them to `global_reg_value`
		//      then remove the 'dummy' buffer
	}
	fclose(fp);
	Sql_Free(mysql_handle);

	ShowStatus("Convert end...\n");

	return 0;
}

int login_config_read(const char* cfgName)
{
	int i;
	char line[1024], w1[1024], w2[1024];
	FILE *fp;

	ShowStatus("Start reading interserver configuration: %s\n", cfgName);

	fp=fopen(cfgName,"r");
	if(fp==NULL){
		ShowError("File not found: %s\n", cfgName);
		return 1;
	}

	while(fgets(line, sizeof(line), fp))
	{
		if(line[0] == '/' && line[1] == '/')
			continue;

		i=sscanf(line,"%[^:]:%s", w1, w2);
		if(i!=2)
			continue;

		//add for DB connection
		if(strcmpi(w1,"db_server_ip")==0){
			strcpy(db_server_ip, w2);
			ShowStatus("set db_server_ip : %s\n",w2);
		}
		else if(strcmpi(w1,"db_server_port")==0){
			db_server_port=atoi(w2);
			ShowStatus("set db_server_port : %s\n",w2);
		}
		else if(strcmpi(w1,"db_server_id")==0){
			strcpy(db_server_id, w2);
			ShowStatus("set db_server_id : %s\n",w2);
		}
		else if(strcmpi(w1,"db_server_pw")==0){
			strcpy(db_server_pw, w2);
			ShowStatus("set db_server_pw : %s\n",w2);
		}
		else if(strcmpi(w1,"db_server_logindb")==0){
			strcpy(db_server_logindb, w2);
			ShowStatus("set db_server_logindb : %s\n",w2);
		}
		//support the import command, just like any other config
		else if(strcmpi(w1,"import")==0){
			login_config_read(w2);
		}
	}
	fclose(fp);
	ShowStatus("End reading interserver configuration...\n");
	return 0;
}

int do_init(int argc, char** argv)
{
	int input;
	login_config_read( (argc > 1) ? argv[1] : INTER_CONF_NAME );
	read_gm_account();

	ShowInfo("\nWarning : Make sure you backup your databases before continuing!\n");
	ShowInfo("\nDo you wish to convert your Login Database to SQL? (y/n) : ");
	input = getchar();
	if(input == 'y' || input == 'Y')
		convert_login();
	return 0;
}

void do_abort(void) {}

void do_final(void) {}