Mana
Loading...
Searching...
No Matches
loginhandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "client.h"
25#include "log.h"
26
27#include "net/logindata.h"
28
33
34#include "utils/gettext.h"
35#include "utils/sha256.h"
36
38
39namespace ManaServ {
40
41extern Connection *accountServerConnection;
42extern std::string netToken;
43
61
63{
64 switch (msg.getId())
65 {
68 break;
69
72 break;
73
76 break;
77
79 {
80 int errMsg = msg.readInt8();
81 // Successful login
82 if (errMsg == ERRMSG_OK)
83 {
85 }
86 // Login failed
87 else
88 {
89 switch (errMsg)
90 {
92 errorMessage = _("Wrong magic_token.");
93 break;
94 case ERRMSG_FAILURE:
95 errorMessage = _("Already logged in.");
96 break;
97 case LOGIN_BANNED:
98 errorMessage = _("Account banned.");
99 break;
100 default:
101 errorMessage = _("Unknown error.");
102 break;
103 }
105 }
106 }
107 break;
108
110 {
111 int errMsg = msg.readInt8();
112 // Successful pass change
113 if (errMsg == ERRMSG_OK)
114 {
116 }
117 // pass change failed
118 else
119 {
120 switch (errMsg)
121 {
123 errorMessage = _("New password incorrect.");
124 break;
125 case ERRMSG_FAILURE:
126 errorMessage = _("Old password incorrect.");
127 break;
128 case ERRMSG_NO_LOGIN:
129 errorMessage = _("Account not connected. Please login first.");
130 break;
131 default:
132 errorMessage = _("Unknown error.");
133 break;
134 }
136 }
137 }
138 break;
139
141 {
142 int errMsg = msg.readInt8();
143 // Successful pass change
144 if (errMsg == ERRMSG_OK)
145 {
147 }
148 // pass change failed
149 else
150 {
151 switch (errMsg)
152 {
154 errorMessage = _("New email address incorrect.");
155 break;
156 case ERRMSG_FAILURE:
157 errorMessage = _("Old email address incorrect.");
158 break;
159 case ERRMSG_NO_LOGIN:
160 errorMessage = _("Account not connected. Please login first.");
161 break;
163 errorMessage = _("The new email address already exists.");
164 break;
165 default:
166 errorMessage = _("Unknown error.");
167 break;
168 }
170 }
171 }
172 break;
174 {
175 int errMsg = msg.readInt8();
176
177 // Successful logout
178 if (errMsg == ERRMSG_OK)
179 {
180 // TODO: handle logout
181 }
182 // Logout failed
183 else
184 {
185 switch (errMsg)
186 {
187 case ERRMSG_NO_LOGIN:
188 errorMessage = "Accountserver: Not logged in";
189 break;
190 default:
191 errorMessage = "Accountserver: Unknown error";
192 break;
193 }
195 }
196 }
197 break;
199 {
200 int errMsg = msg.readInt8();
201 // Successful unregistration
202 if (errMsg == ERRMSG_OK)
203 {
205 }
206 // Unregistration failed
207 else
208 {
209 switch (errMsg)
210 {
213 "Accountserver: Wrong username or password";
214 break;
215 default:
216 errorMessage = "Accountserver: Unknown error";
217 break;
218 }
220 }
221 }
222 break;
223
225 {
226 int allowed = msg.readInt8();
227
228 if (allowed)
229 {
232 std::string captchaURL = msg.readString();
233 std::string captchaInstructions = msg.readString();
234
235 printf("%s: %s\n", captchaURL.c_str(), captchaInstructions.c_str());
236
238 }
239 else
240 {
241 errorMessage = msg.readString();
242
243 if (errorMessage.empty())
244 errorMessage = _("Client registration is not allowed. "
245 "Please contact server administration.");
247 }
248 }
249 break;
250 }
251}
252
258
260{
261 const int errMsg = msg.readInt8();
262
263 if (errMsg == ERRMSG_OK)
264 {
265 readServerInfo(msg);
266 // No worlds atm, but future use :-D
268 }
269 else
270 {
271 switch (errMsg)
272 {
274 errorMessage = _("Client version is too old.");
275 break;
277 errorMessage = _("Wrong username or password.");
278 break;
279 case ERRMSG_FAILURE:
280 errorMessage = _("Already logged in.");
281 break;
282 case LOGIN_BANNED:
283 errorMessage = _("Account banned");
284 break;
286 errorMessage = _("Login attempt too soon after previous "
287 "attempt.");
288 break;
289 default:
290 errorMessage = _("Unknown error.");
291 break;
292 }
294 }
295}
296
298{
299 const int errMsg = msg.readInt8();
300
301 if (errMsg == ERRMSG_OK)
302 {
303 readServerInfo(msg);
305 }
306 else
307 {
308 switch (errMsg)
309 {
311 errorMessage = _("Client version is too old.");
312 break;
314 errorMessage = _("Wrong username, password or email address.");
315 break;
317 errorMessage = _("Username already exists.");
318 break;
320 errorMessage = _("Email address already exists.");
321 break;
323 errorMessage = _("You took too long with the captcha or your "
324 "response was incorrect.");
325 break;
326 default:
327 errorMessage = _("Unknown error.");
328 break;
329 }
331 }
332}
333
335{
336 // Safety check for outdated manaserv versions (remove me later)
337 if (msg.getUnreadLength() == 0)
338 return;
339
340 // Set the update host when included in the message
341 const std::string updateHost = msg.readString();
342 if (!updateHost.empty())
343 mLoginData->updateHost = updateHost;
344 else
345 Log::warn("Server does not have an update host set!");
346
347 // Read the client data folder for dynamic data loading.
348 // This is only used by the Qt client.
349 msg.readString();
350
351 // Read the number of character slots
353}
354
359
364
374
376{
377 return true;
378}
379
385
387{
388 return mMinUserNameLength;
389}
390
392{
393 return mMaxUserNameLength;
394}
395
404
417
423
424void LoginHandler::changeEmail(const std::string &email)
425{
427
428 // Email is sent clearly so the server can validate the data.
429 // Encryption is assumed server-side.
430 msg.writeString(email);
431
433}
434
435void LoginHandler::changePassword(const std::string &username,
436 const std::string &oldPassword,
437 const std::string &newPassword)
438{
440
441 // Change password using SHA2 encryption
442 msg.writeString(sha256(username + oldPassword));
443 msg.writeString(sha256(username + newPassword));
444
446}
447
448void LoginHandler::chooseServer(unsigned int server)
449{
450 // TODO
451}
452
454{
456
458
459 msg.writeInt32(PROTOCOL_VERSION); // client version
461 // Use a hashed password for privacy reasons
465
467}
468
469void LoginHandler::unregisterAccount(const std::string &username,
470 const std::string &password)
471{
473
474 msg.writeString(username);
475 msg.writeString(sha256(username + password));
476
478}
479
481{
482 return Worlds();
483}
484
491
492} // namespace ManaServ
static void setState(State state)
Definition client.h:169
static State getState()
Definition client.h:172
std::string email
Definition logindata.h:39
std::string updateHost
Definition logindata.h:37
std::string randomSeed
Definition logindata.h:35
unsigned short characterSlots
The number of character slots.
Definition logindata.h:47
std::string username
Definition logindata.h:33
std::string password
Definition logindata.h:34
std::string captchaResponse
Definition logindata.h:40
bool isConnected()
Returns whether the server is connected.
void send(const ManaServ::MessageOut &msg)
Sends a message.
bool connect(const std::string &address, enet_uint16 port)
Connects to the given server with the specified address and port.
void disconnect()
Disconnects from the given server.
void handleMessage(MessageIn &msg) override
void unregisterAccount(const std::string &username, const std::string &password) override
void disconnect() override
unsigned int mMinUserNameLength
void loginAccount(LoginData *loginData) override
unsigned int getMaxUserNameLength() const override
unsigned int getMinUserNameLength() const override
void handleLoginRandomResponse(MessageIn &msg)
void getRegistrationDetails() override
void changePassword(const std::string &username, const std::string &oldPassword, const std::string &newPassword) override
bool isRegistrationEnabled() override
void chooseServer(unsigned int server) override
void connect() override
void handleRegisterResponse(MessageIn &msg)
void handleLoginResponse(MessageIn &msg)
void registerAccount(LoginData *loginData) override
unsigned int mMaxUserNameLength
void readServerInfo(MessageIn &msg)
bool isConnected() override
void changeEmail(const std::string &email) override
Worlds getWorlds() const override
Used for parsing an incoming message from manaserv.
Definition messagein.h:37
unsigned int getUnreadLength() const
Returns the length of unread data.
Definition messagein.h:54
std::string readString(int length=-1)
Reads a string.
Definition messagein.cpp:92
uint16_t getId() const
Returns the message ID.
Definition messagein.h:44
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:43
Used for building an outgoing message to manaserv.
Definition messageout.h:37
void writeInt32(uint32_t value)
Writes an unsigned 32-bit integer to the message.
void writeString(const std::string &string, int length=-1)
Writes a string.
ServerInfo mServer
const uint16_t * handledMessages
std::string hostname
Definition serverinfo.h:42
uint16_t port
Definition serverinfo.h:43
std::string errorMessage
Definition client.cpp:94
LoginData loginData
Definition client.cpp:95
@ STATE_LOGIN_ERROR
Definition client.h:75
@ STATE_CHANGEPASSWORD_SUCCESS
Definition client.h:82
@ STATE_CONNECT_GAME
Definition client.h:72
@ STATE_CHANGEEMAIL_SUCCESS
Definition client.h:85
@ STATE_REGISTER
Definition client.h:78
@ STATE_ACCOUNTCHANGE_ERROR
Definition client.h:76
@ STATE_UNREGISTER_SUCCESS
Definition client.h:88
@ STATE_CHAR_SELECT
Definition client.h:71
@ STATE_WORLD_SELECT
Definition client.h:66
@ STATE_ERROR
Definition client.h:60
@ STATE_GAME
Definition client.h:73
#define _(s)
Definition gettext.h:38
ManaServ::LoginHandler * loginHandler
Definition net.cpp:51
Net::LoginHandler * loginHandler
Definition net.cpp:51
void warn(const char *log_text,...) LOG_PRINTF_ATTR
std::string netToken
Connection * accountServerConnection
@ ERRMSG_EMAIL_ALREADY_EXISTS
@ APMSG_REGISTER_INFO_RESPONSE
@ APMSG_PASSWORD_CHANGE_RESPONSE
@ APMSG_UNREGISTER_RESPONSE
@ APMSG_EMAIL_CHANGE_RESPONSE
@ PAMSG_REQUEST_REGISTER_INFO
@ APMSG_LOGIN_RNDTRGR_RESPONSE
std::string sha256(const std::string &string)
Returns the SHA-256 hash for the given string.
Definition sha256.cpp:276
std::vector< WorldInfo * > Worlds
Definition worldinfo.h:35