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