/
account.cpp
301 lines (253 loc) · 7.66 KB
/
account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
History
=======
2005/05/25 Shinigami: added void writeto( ConfigElem& elem )
2005/05/25 Shinigami: added getnextfreeslot()
2009/08/06 MuadDib: Removed PasswordOnlyHash support
2009/09/03 MuadDib: Relocation of account related cpp/h
Notes
=======
*/
#include "account.h"
#include "accounts.h"
#include "../../clib/cfgelem.h"
#include "../../clib/clib_MD5.h"
#include "../../clib/streamsaver.h"
#include "../globals/uvars.h"
#include "../../plib/systemstate.h"
#include "../mobile/charactr.h"
#include "../network/client.h"
#include "../cmdlevel.h"
#include "../polcfg.h"
#include <cstring>
namespace Pol {
namespace Accounts {
Account::Account( Clib::ConfigElem& elem ) :
active_character( NULL ),
characters_(),
name_( elem.remove_string( "NAME" ) ),
enabled_( true ),
banned_( false ),
default_cmdlevel_( 0 )
{
// If too low, will cause the client to freeze and the console to report
// Exception in message handler 0x91: vector
for ( int i = 0; i < Plib::systemstate.config.character_slots; i++ )
characters_.push_back( Core::CharacterRef( NULL ) );
readfrom( elem );
}
void Account::readfrom( Clib::ConfigElem& elem )
{
if ( elem.has_prop( "Password" ) )
{
std::string temppass = elem.remove_string("Password");
if ( Plib::systemstate.config.retain_cleartext_passwords )
{
password_ = temppass;
}
if ( !Clib::MD5_Encrypt( name_ + temppass, passwordhash_ ) ) //MD5
elem.throw_error( "Failed to encrypt password for " + name_ );
Plib::systemstate.accounts_txt_dirty = true;
}
else if ( elem.has_prop( "PasswordHash" ) )
{
passwordhash_ = elem.remove_string( "PasswordHash" );
}
else
elem.throw_error( "Failed password reads for account " + name_ );
enabled_ = elem.remove_bool( "ENABLED", true );
banned_ = elem.remove_bool( "BANNED", false );
uo_expansion_ = convert_uo_expansion( elem.remove_string( "UOExpansion", "T2A" ) );
default_privs_.readfrom( elem.remove_string( "DefaultPrivs", "" ) );
std::string cmdaccstr = elem.remove_string( "DefaultCmdLevel", "player" );
Core::CmdLevel* cmdlevel_search = Core::find_cmdlevel( cmdaccstr.c_str( ) );
if ( cmdlevel_search != NULL )
default_cmdlevel_ = cmdlevel_search->cmdlevel;
else
elem.throw_error( "Didn't understand cmdlevel of '" + cmdaccstr + "'" );
props_.clear();
props_.readProperties( elem );
}
void Account::writeto( Clib::StreamWriter& sw ) const
{
sw() << "Account" << pf_endl
<< "{" << pf_endl
<< "\tName\t" << name_ << pf_endl;
//dave 6/5/3 don't write cleartext unless configured to
if ( Plib::systemstate.config.retain_cleartext_passwords && !password_.empty( ) )
sw() << "\tPassword\t" << password_ << pf_endl;
sw() << "\tPasswordHash\t" << passwordhash_ << pf_endl; //MD5
sw() << "\tEnabled\t" << enabled_ << pf_endl
<< "\tBanned\t" << banned_ << pf_endl;
if ( !default_privs_.empty() )
{
sw() << "\tDefaultPrivs\t" << default_privs_.extract() << pf_endl;
}
if ( default_cmdlevel_ )
{
sw( ) << "\tDefaultCmdLevel\t" << Core::gamestate.cmdlevels[default_cmdlevel_].name.c_str( ) << pf_endl;
}
if ( uo_expansion_ )
{
sw() << "\tUOExpansion\t" << uo_expansion() << pf_endl;
}
props_.printProperties( sw );
sw() << "}" << pf_endl
<< pf_endl;
//sw.flush();
}
void Account::writeto( Clib::ConfigElem& elem ) const
{
elem.add_prop( "Name", name_.c_str() );
//dave 6/5/3 don't write cleartext unless configured to
if ( Plib::systemstate.config.retain_cleartext_passwords && !password_.empty( ) )
elem.add_prop( "Password", password_.c_str() );
elem.add_prop( "PasswordHash", passwordhash_.c_str() );
elem.add_prop( "Enabled", ( (unsigned int)( enabled_ ? 1 : 0 ) ) );
elem.add_prop( "Banned", ( (unsigned int)( banned_ ? 1 : 0 ) ) );
if ( !default_privs_.empty() )
{
elem.add_prop( "DefaultPrivs", default_privs_.extract().c_str() );
}
if ( default_cmdlevel_ )
{
elem.add_prop( "DefaultCmdLevel", Core::gamestate.cmdlevels[default_cmdlevel_].name.c_str( ) );
}
if ( uo_expansion_ )
{
elem.add_prop( "UOExpansion", uo_expansion().c_str() );
}
props_.printProperties( elem );
}
Account::~Account()
{
if ( active_character != NULL )
{
if ( ( active_character->client != NULL ) &&
( active_character->client->acct == this ) )
{
active_character->client->acct = NULL;
}
if ( active_character->acct == this )
{
active_character->acct.clear();
}
active_character = NULL;
}
for ( int i = 0; i < Plib::systemstate.config.character_slots; i++ )
{
if ( characters_[i].get() )
{
characters_[i]->acct.clear();
characters_[i].clear();
}
}
}
size_t Account::estimatedSize() const
{
size_t size = sizeof( Account )
+ name_.capacity()+ password_.capacity()+passwordhash_.capacity();
size += 3 * sizeof( Core::CharacterRef* ) + characters_.capacity( ) * sizeof( Core::CharacterRef );
size += props_.estimatedSize();
size += default_privs_.estimatedSize();
size += options_.estimatedSize();
return size;
}
Mobile::Character* Account::get_character( int index )
{
return characters_.at( index ).get();
}
void Account::set_character( int index, Mobile::Character* chr )
{
characters_.at( index ).set( chr );
}
void Account::clear_character( int index )
{
characters_.at( index ).clear();
}
const char* Account::name() const
{
return name_.c_str();
}
const std::string Account::password() const
{
return password_;
}
const std::string Account::passwordhash() const
{
return passwordhash_;
}
const std::string Account::uo_expansion() const
{
switch ( uo_expansion_ )
{
case Network::HSA: return "HSA";
case Network::SA: return "SA";
case Network::KR: return "KR";
case Network::ML: return "ML";
case Network::SE: return "SE";
case Network::AOS: return "AOS";
case Network::LBR: return "LBR";
case Network::T2A: return "T2A";
default: return "";
}
}
unsigned short Account::uo_expansion_flag() const
{
return uo_expansion_;
}
u16 Account::convert_uo_expansion( const std::string& expansion )
{
const auto not_found = std::string::npos;
if ( expansion.find( "HSA" ) != not_found )
return Network::HSA;
else if (expansion.find("SA") != not_found)
return Network::SA;
else if (expansion.find("KR") != not_found)
return Network::KR;
else if (expansion.find("ML") != not_found)
return Network::ML;
else if (expansion.find("SE") != not_found)
return Network::SE;
else if (expansion.find("AOS") != not_found)
return Network::AOS;
else if (expansion.find("LBR") != not_found)
return Network::LBR;
else if (expansion.find("T2A") != not_found)
return Network::T2A;
else
return 0;
}
bool Account::enabled() const
{
return enabled_;
}
bool Account::banned() const
{
return banned_;
}
std::string Account::default_privlist() const
{
return default_privs_.extract();
}
unsigned char Account::default_cmdlevel() const
{
return default_cmdlevel_;
}
int Account::numchars() const
{
int num = 0;
for ( int i = 0; i < Plib::systemstate.config.character_slots; i++ )
if ( characters_[i].get() )
++num;
return num;
}
int Account::getnextfreeslot() const
{
for ( int i = 0; i < Plib::systemstate.config.character_slots; i++ )
if ( !characters_[i].get() )
return ( i + 1 );
return -1;
}
}
}