ManaPlus
loginhandler.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "net/ea/loginhandler.h"
25 
26 #include "client.h"
27 #include "configuration.h"
28 #include "logger.h"
29 #include "settings.h"
30 
31 #include "utils/cast.h"
32 #include "utils/dtor.h"
33 
34 #include "net/logindata.h"
35 
36 #include "net/ea/loginrecv.h"
37 
38 #include "debug.h"
39 
40 namespace Ea
41 {
42 
44  Net::LoginHandler()
45 {
48  LoginRecv::mUpdateHost.clear();
49  LoginRecv::mWorlds.clear();
51 }
52 
54 {
56 }
57 
59 {
61 }
62 
64 {
65  // Not supported, so move on
67 }
68 
69 void LoginHandler::loginAccount(LoginData *const loginData1) const
70 {
71  if (loginData1 != nullptr)
72  {
73  loginData1->resetCharacterSlots();
74  sendLoginRegister(loginData1->username, loginData1->password, "");
75  }
76 }
77 
78 void LoginHandler::chooseServer(const unsigned int server,
79  const bool persistentIp) const
80 {
81  if (CAST_SIZE(server) >= LoginRecv::mWorlds.size() ||
82  (LoginRecv::mWorlds[server] == nullptr))
83  {
84  return;
85  }
86 
88  if (charServer != nullptr)
89  {
90  if (config.getBoolValue("usePersistentIP") || persistentIp)
91  {
93  }
94  else
95  {
97  LoginRecv::mWorlds[server]->address);
98  }
99  charServer->port = LoginRecv::mWorlds[server]->port;
100  }
102 
104 }
105 
106 void LoginHandler::registerAccount(const LoginData *const loginData1) const
107 {
108  if (loginData1 == nullptr)
109  return;
110 
111  std::string username = loginData1->username;
112  switch (loginData1->gender)
113  {
114  case Gender::FEMALE:
115  case Gender::UNSPECIFIED:
116  username.append("_F");
117  break;
118  case Gender::MALE:
119  username.append("_M");
120  break;
121  default:
122  break;
123  }
124 
125  sendLoginRegister(username, loginData1->password, loginData1->email);
126 }
127 
129 {
130  return LoginRecv::mWorlds;
131 }
132 
134 {
136  LoginRecv::mWorlds.clear();
137 }
138 
140 {
141  if (data == nullptr)
142  return;
143 
144  logger->log("Username is %s", data->username.c_str());
145 
146  // Send login infos
147  if (data->registerLogin)
149  else
151 
152  const bool remember = data->remember;
153  if (remember)
154  {
155  serverConfig.setValue("username", data->username);
156 #ifdef SAVE_PASSWORD
157  serverConfig.setValue("password", data->password);
158 #endif
159  }
160  else
161  {
162  serverConfig.setValue("username", "");
163 #ifdef SAVE_PASSWORD
164  serverConfig.setValue("password", "");
165 #endif
166  }
167  serverConfig.setValue("remember", remember);
168 
169  // Clear the password, avoids auto login when returning to login
170  data->password.clear();
171 }
172 
174 {
175 }
176 
177 void LoginHandler::changeEmail(const std::string &email A_UNUSED) const
178 {
179 }
180 
181 void LoginHandler::unregisterAccount(const std::string &username A_UNUSED,
182  const std::string &password
183  A_UNUSED) const
184 {
185 }
186 
188 {
189  return LoginRecv::mToken;
190 }
191 
192 } // namespace Ea
#define CAST_SIZE
Definition: cast.h:34
void setState(const StateT state)
Definition: client.h:66
bool getBoolValue(const std::string &key) const
void setValue(const std::string &key, const std::string &value)
void registerAccount(const LoginData *const loginData) const
void loginOrRegister(LoginData *const data) const
void loginAccount(LoginData *const loginData) const
virtual ServerInfo * getCharServer() const =0
const Token & getToken() const A_CONST
void changeEmail(const std::string &email) const
void chooseServer(const unsigned int server, const bool persistentIp) const
void logout() const
bool isRegistrationEnabled() const
virtual void sendLoginRegister(const std::string &username, const std::string &password, const std::string &email) const =0
void unregisterAccount(const std::string &username, const std::string &password) const
const Worlds & getWorlds() const A_CONST
void clearWorlds() const
void getRegistrationDetails() const
void log(const char *const log_text,...)
Definition: logger.cpp:269
GenderT gender
Definition: logindata.h:71
std::string email
Definition: logindata.h:67
std::string username
Definition: logindata.h:59
std::string password
Definition: logindata.h:60
void resetCharacterSlots()
Definition: logindata.h:105
ServerInfo mServer
Definition: loginhandler.h:116
std::string hostname
Definition: serverinfo.h:45
std::string althostname
Definition: serverinfo.h:46
uint16_t port
Definition: serverinfo.h:58
std::string serverName
Definition: settings.h:114
Configuration config
Configuration serverConfig
void delete_all(Container &c)
Definition: dtor.h:56
Client * client
Definition: client.cpp:118
#define A_UNUSED
Definition: localconsts.h:160
Logger * logger
Definition: logger.cpp:89
uint32_t data
ServerInfo charServer
bool mVersionResponse
Definition: loginrecv.cpp:47
bool mRegistrationEnabled
Definition: loginrecv.cpp:48
std::string mUpdateHost
Definition: loginrecv.cpp:44
Worlds mWorlds
Definition: loginrecv.cpp:45
int size()
Definition: emotedb.cpp:306
@ UNSPECIFIED
Definition: gender.h:33
@ MALE
Definition: gender.h:31
@ FEMALE
Definition: gender.h:32
@ UPDATE
Definition: state.h:44
@ REGISTER
Definition: state.h:54
Settings settings
Definition: settings.cpp:32
const char * ipToString(const uint32_t address)
Definition: stringutils.cpp:86
Definition: token.h:31
void clear()
Definition: token.h:46
std::vector< WorldInfo * > Worlds
Definition: worldinfo.h:59