GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/loginrecv.cpp Lines: 1 71 1.4 %
Date: 2017-11-29 Branches: 0 20 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-2017  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
45
namespace TmwAthena
46
{
47
48
extern ServerInfo charServer;
49
50
enum ServerFlags
51
{
52
    FLAG_REGISTRATION = 1
53
};
54
55
void LoginRecv::processServerVersion(Net::MessageIn &msg)
56
{
57
    packetVersion = 0;
58
    const uint8_t b1 = msg.readUInt8("b1");  // -1
59
    const uint8_t b2 = msg.readUInt8("b2");
60
    const uint8_t b3 = msg.readUInt8("b3");
61
    msg.readUInt8("b4");
62
    if (b1 == 255)
63
    {   // old TMWA
64
        const unsigned int options = msg.readInt32("options");
65
        Ea::LoginRecv::mRegistrationEnabled =
66
            ((options & FLAG_REGISTRATION) != 0u);
67
        serverVersion = 0;
68
        tmwServerVersion = 0;
69
    }
70
    else if (b1 >= 0x0d)
71
    {   // new TMWA
72
        const unsigned int options = msg.readInt32("options");
73
        Ea::LoginRecv::mRegistrationEnabled =
74
            ((options & FLAG_REGISTRATION) != 0u);
75
        serverVersion = 0;
76
        tmwServerVersion = (b1 << 16) | (b2 << 8) | b3;
77
    }
78
    else
79
    {   // eAthena
80
        const unsigned int options = msg.readInt32("options");
81
        Ea::LoginRecv::mRegistrationEnabled =
82
            ((options & FLAG_REGISTRATION) != 0u);
83
        serverVersion = 0;
84
        tmwServerVersion = 0;
85
    }
86
    if (tmwServerVersion > 0)
87
        logger->log("Tmw server version: x%06x", tmwServerVersion);
88
    else
89
        logger->log("Server without version");
90
    updateProtocol();
91
92
    if (client->getState() != State::LOGIN)
93
        client->setState(State::LOGIN);
94
95
    // Leave this last
96
    Ea::LoginRecv::mVersionResponse = true;
97
}
98
99
void LoginRecv::processCharPasswordResponse(Net::MessageIn &msg)
100
{
101
    // 0: acc not found, 1: success, 2: password mismatch, 3: pass too short
102
    const uint8_t errMsg = msg.readUInt8("result code");
103
    // Successful pass change
104
    if (errMsg == 1)
105
    {
106
        client->setState(State::CHANGEPASSWORD_SUCCESS);
107
    }
108
    // pass change failed
109
    else
110
    {
111
        switch (errMsg)
112
        {
113
            case 0:
114
                errorMessage =
115
                    // TRANSLATORS: error message
116
                    _("Account was not found. Please re-login.");
117
                break;
118
            case 2:
119
                // TRANSLATORS: error message
120
                errorMessage = _("Old password incorrect.");
121
                break;
122
            case 3:
123
                // TRANSLATORS: error message
124
                errorMessage = _("New password too short.");
125
                break;
126
            default:
127
                // TRANSLATORS: error message
128
                errorMessage = _("Unknown error.");
129
                break;
130
        }
131
        client->setState(State::ACCOUNTCHANGE_ERROR);
132
    }
133
}
134
135
void LoginRecv::processLoginData(Net::MessageIn &msg)
136
{
137
    msg.readInt16("len");
138
139
    loginHandler->clearWorlds();
140
141
    const int worldCount = (msg.getLength() - 47) / 32;
142
143
    Ea::LoginRecv::mToken.session_ID1 = msg.readInt32("session id1");
144
    Ea::LoginRecv::mToken.account_ID = msg.readBeingId("accound id");
145
    Ea::LoginRecv::mToken.session_ID2 = msg.readInt32("session id2");
146
    msg.readInt32("old ip");
147
    loginData.lastLogin = msg.readString(24, "last login");
148
    msg.readInt16("unused");
149
150
    // reserve bits for future usage
151
    Ea::LoginRecv::mToken.sex = Being::intToGender(CAST_U8(
152
        msg.readUInt8("gender") & 3U));
153
154
    for (int i = 0; i < worldCount; i++)
155
    {
156
        WorldInfo *const world = new WorldInfo;
157
158
        world->address = msg.readInt32("ip address");
159
        world->port = msg.readInt16("port");
160
        world->name = msg.readString(20, "name");
161
        world->online_users = msg.readInt16("online number");
162
        config.setValue("updatehost", Ea::LoginRecv::mUpdateHost);
163
        world->updateHost = Ea::LoginRecv::mUpdateHost;
164
        msg.readInt16("maintenance");
165
        msg.readInt16("new");
166
167
        logger->log("Network: Server: %s (%s:%d)", world->name.c_str(),
168
            ipToString(world->address), world->port);
169
170
        Ea::LoginRecv::mWorlds.push_back(world);
171
    }
172
    client->setState(State::WORLD_SELECT);
173
}
174
175
4
}  // namespace TmwAthena