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 
LoginHandler()43 LoginHandler::LoginHandler() :
44     Net::LoginHandler()
45 {
46     LoginRecv::mVersionResponse = false;
47     LoginRecv::mRegistrationEnabled = true;
48     LoginRecv::mUpdateHost.clear();
49     LoginRecv::mWorlds.clear();
50     LoginRecv::mToken.clear();
51 }
52 
~LoginHandler()53 LoginHandler::~LoginHandler()
54 {
55     delete_all(LoginRecv::mWorlds);
56 }
57 
isRegistrationEnabled() const58 bool LoginHandler::isRegistrationEnabled() const
59 {
60     return LoginRecv::mRegistrationEnabled;
61 }
62 
getRegistrationDetails() const63 void LoginHandler::getRegistrationDetails() const
64 {
65     // Not supported, so move on
66     client->setState(State::REGISTER);
67 }
68 
loginAccount(LoginData * const loginData1) const69 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 
chooseServer(const unsigned int server,const bool persistentIp) const78 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 
87     ServerInfo *const charServer = getCharServer();
88     if (charServer != nullptr)
89     {
90         if (config.getBoolValue("usePersistentIP") || persistentIp)
91         {
92             charServer->hostname = settings.serverName;
93         }
94         else
95         {
96             charServer->hostname = ipToString(
97                 LoginRecv::mWorlds[server]->address);
98         }
99         charServer->port = LoginRecv::mWorlds[server]->port;
100     }
101     charServer->althostname = mServer.althostname;
102 
103     client->setState(State::UPDATE);
104 }
105 
registerAccount(const LoginData * const loginData1) const106 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 
getWorlds() const128 const Worlds &LoginHandler::getWorlds() const
129 {
130     return LoginRecv::mWorlds;
131 }
132 
clearWorlds() const133 void LoginHandler::clearWorlds() const
134 {
135     delete_all(LoginRecv::mWorlds);
136     LoginRecv::mWorlds.clear();
137 }
138 
loginOrRegister(LoginData * const data) const139 void LoginHandler::loginOrRegister(LoginData *const data) const
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)
148         registerAccount(data);
149     else
150         loginAccount(data);
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 
logout() const173 void LoginHandler::logout() const
174 {
175 }
176 
changeEmail(const std::string & email A_UNUSED) const177 void LoginHandler::changeEmail(const std::string &email A_UNUSED) const
178 {
179 }
180 
unregisterAccount(const std::string & username A_UNUSED,const std::string & password A_UNUSED) const181 void LoginHandler::unregisterAccount(const std::string &username A_UNUSED,
182                                      const std::string &password
183                                      A_UNUSED) const
184 {
185 }
186 
getToken() const187 const Token &LoginHandler::getToken() const
188 {
189     return LoginRecv::mToken;
190 }
191 
192 }  // namespace Ea
193