GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/loginrecv.cpp Lines: 1 68 1.5 %
Date: 2018-12-09 Branches: 0 18 0.0 %

Line Branch Exec Source
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-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/tmwa/loginrecv.h"
24
25
#include "client.h"
26
#include "configuration.h"
27
#include "logger.h"
28
29
#include "being/being.h"
30
31
#include "net/logindata.h"
32
#include "net/loginhandler.h"
33
#include "net/messagein.h"
34
35
#include "net/ea/loginrecv.h"
36
37
#include "net/tmwa/updateprotocol.h"
38
39
#include "utils/gettext.h"
40
41
#include "debug.h"
42
43
extern int packetVersion;
44
extern int itemIdLen;
45
46
namespace TmwAthena
47
{
48
49
extern ServerInfo charServer;
50
51
enum ServerFlags
52
{
53
    FLAG_REGISTRATION = 1
54
};
55
56
void LoginRecv::processServerVersion(Net::MessageIn &msg)
57
{
58
    packetVersion = 0;
59
    itemIdLen = 2;
60
    const uint8_t b1 = msg.readUInt8("b1");  // -1
61
    const uint8_t b2 = msg.readUInt8("b2");
62
    const uint8_t b3 = msg.readUInt8("b3");
63
    msg.readUInt8("b4");
64
    if (b1 == 255)
65
    {   // old TMWA
66
        const unsigned int options = msg.readInt32("options");
67
        Ea::LoginRecv::mRegistrationEnabled =
68
            ((options & FLAG_REGISTRATION) != 0U);
69
        serverVersion = 0;
70
        tmwServerVersion = 0;
71
    }
72
    else if (b1 >= 0x0d)
73
    {   // new TMWA
74
        const unsigned int options = msg.readInt32("options");
75
        Ea::LoginRecv::mRegistrationEnabled =
76
            ((options & FLAG_REGISTRATION) != 0U);
77
        serverVersion = 0;
78
        tmwServerVersion = (b1 << 16) | (b2 << 8) | b3;
79
    }
80
    else
81
    {   // eAthena
82
        const unsigned int options = msg.readInt32("options");
83
        Ea::LoginRecv::mRegistrationEnabled =
84
            ((options & FLAG_REGISTRATION) != 0U);
85
        serverVersion = 0;
86
        tmwServerVersion = 0;
87
    }
88
    if (tmwServerVersion > 0)
89
        logger->log("Tmw server version: x%06x", tmwServerVersion);
90
    else
91
        logger->log("Server without version");
92
    updateProtocol();
93
94
    if (client->getState() != State::LOGIN)
95
        client->setState(State::LOGIN);
96
97
    // Leave this last
98
    Ea::LoginRecv::mVersionResponse = true;
99
}
100
101
void LoginRecv::processCharPasswordResponse(Net::MessageIn &msg)
102
{
103
    // 0: acc not found, 1: success, 2: password mismatch, 3: pass too short
104
    const uint8_t errMsg = msg.readUInt8("result code");
105
    // Successful pass change
106
    if (errMsg == 1)
107
    {
108
        client->setState(State::CHANGEPASSWORD_SUCCESS);
109
    }
110
    // pass change failed
111
    else
112
    {
113
        switch (errMsg)
114
        {
115
            case 0:
116
                errorMessage =
117
                    // TRANSLATORS: error message
118
                    _("Account was not found. Please re-login.");
119
                break;
120
            case 2:
121
                // TRANSLATORS: error message
122
                errorMessage = _("Old password incorrect.");
123
                break;
124
            case 3:
125
                // TRANSLATORS: error message
126
                errorMessage = _("New password too short.");
127
                break;
128
            default:
129
                // TRANSLATORS: error message
130
                errorMessage = _("Unknown error.");
131
                break;
132
        }
133
        client->setState(State::ACCOUNTCHANGE_ERROR);
134
    }
135
}
136
137
void LoginRecv::processLoginData(Net::MessageIn &msg)
138
{
139
    msg.readInt16("len");
140
141
    loginHandler->clearWorlds();
142
143
    const int worldCount = (msg.getLength() - 47) / 32;
144
145
    Ea::LoginRecv::mToken.session_ID1 = msg.readInt32("session id1");
146
    Ea::LoginRecv::mToken.account_ID = msg.readBeingId("accound id");
147
    Ea::LoginRecv::mToken.session_ID2 = msg.readInt32("session id2");
148
    msg.readInt32("old ip");
149
    loginData.lastLogin = msg.readString(24, "last login");
150
    msg.readInt16("unused");
151
152
    // reserve bits for future usage
153
    Ea::LoginRecv::mToken.sex = Being::intToGender(CAST_U8(
154
        msg.readUInt8("gender") & 3U));
155
156
    for (int i = 0; i < worldCount; i++)
157
    {
158
        WorldInfo *const world = new WorldInfo;
159
160
        world->address = msg.readInt32("ip address");
161
        world->port = msg.readInt16("port");
162
        world->name = msg.readString(20, "name");
163
        world->online_users = msg.readInt16("online number");
164
        config.setValue("updatehost", Ea::LoginRecv::mUpdateHost);
165
        world->updateHost = Ea::LoginRecv::mUpdateHost;
166
        msg.readInt16("maintenance");
167
        msg.readInt16("new");
168
169
        logger->log("Network: Server: %s (%s:%d)", world->name.c_str(),
170
            ipToString(world->address), world->port);
171
172
        Ea::LoginRecv::mWorlds.push_back(world);
173
    }
174
    client->setState(State::WORLD_SELECT);
175
}
176
177
2
}  // namespace TmwAthena