forked from steaphangreene/acidmud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
388 lines (336 loc) · 9.74 KB
/
player.cpp
File metadata and controls
388 lines (336 loc) · 9.74 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// *************************************************************************
// This file is part of AcidMUD by Steaphan Greene
//
// Copyright 1999-2022 Steaphan Greene <steaphan@gmail.com>
//
// AcidMUD is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// AcidMUD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AcidMUD (see the file named "COPYING");
// If not, see <http://www.gnu.org/licenses/>.
//
// *************************************************************************
#include <array>
#include <set>
#include <vector>
#include <md5.hpp>
#include "color.hpp"
#include "dice.hpp"
#include "log.hpp"
#include "net.hpp"
#include "player.hpp"
#include "utils.hpp"
#include "version.hpp"
static std::map<std::u8string, Player*> player_list;
static std::set<Player*> non_init;
static const std::u8string salt_char =
u8"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
static std::u8string md5_crypt(const std::u8string_view& pass, const std::u8string_view& insalt) {
Chocobo1::MD5 md5;
std::u8string_view salt = insalt;
if (!salt.starts_with(u8"$1$") || salt.length() < 11 ||
(salt.length() > 11 && salt[11] != u8'$')) {
loger(u8"Invalid magic in salt submitted to md5_crypt!");
return u8"";
}
salt = salt.substr(0, 11);
// Initial Hash
md5.addData(pass);
md5.addData(salt.substr(3));
md5.addData(pass);
md5.finalize();
auto alternate = md5.toArray();
{ // Shake
md5.reset();
md5.addData(pass);
md5.addData(salt);
std::vector<char8_t> sub(alternate.begin(), alternate.begin() + pass.length());
md5.addData(sub);
for (auto bit = pass.length(); bit != 0; bit >>= 1) {
if (bit & 1) {
md5.addData(u8"\0", 1);
} else {
md5.addData(pass.substr(0, 1));
}
}
}
md5.finalize();
auto intermediate = md5.toArray();
// Rattle
for (int i = 0; i < 1000; ++i) {
md5.reset();
if (i % 2 == 0) {
md5.addData(intermediate);
}
if (i % 2 != 0) {
md5.addData(pass);
}
if (i % 3 != 0) {
md5.addData(salt.substr(3));
}
if (i % 7 != 0) {
md5.addData(pass);
}
if (i % 2 == 0) {
md5.addData(pass);
}
if (i % 2 != 0) {
md5.addData(intermediate);
}
md5.finalize();
intermediate = md5.toArray();
}
// Roll
std::array<char8_t, 18> final = {{
0,
0,
intermediate[11],
intermediate[4],
intermediate[10],
intermediate[5],
intermediate[3],
intermediate[9],
intermediate[15],
intermediate[2],
intermediate[8],
intermediate[14],
intermediate[1],
intermediate[7],
intermediate[13],
intermediate[0],
intermediate[6],
intermediate[12],
}};
// Final Encoding
std::u8string hash = fmt::format(u8"{}$", salt);
static const char8_t* cb64 = u8"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (int off = 15; off >= 0; off -= 3) {
hash.push_back(cb64[final[off + 2] & 0x3F]);
hash.push_back(cb64[(final[off + 1] << 2 | final[off + 2] >> 6) & 0x3F]);
if (off != 0) {
hash.push_back(cb64[(final[off] << 4 | final[off + 1] >> 4) & 0x3F]);
hash.push_back(cb64[final[off] >> 2]);
}
}
return hash;
}
Player::Player(const std::u8string_view& nm, const std::u8string_view& ps) {
room = new Object();
creator = nullptr;
world = nullptr;
SetName(nm);
if (ps[0] == '$' && ps[1] == '1' && ps[2] == '$') {
pass = ps;
} else {
pass = EncPass(ps);
}
player_list[name] = this;
non_init.insert(this);
}
Player::~Player() {
notify_player_deleted(this);
player_list.erase(name);
non_init.erase(this);
creator = nullptr;
world = nullptr;
if (room)
delete room;
}
void Player::SetPass(const std::u8string_view& nm) {
if (nm.starts_with(u8"$1$")) {
pass = nm;
}
}
bool Player::AuthPass(const std::u8string_view& ps) {
return AuthPass(pass, ps);
}
bool Player::AuthPass(const std::u8string_view& ep, const std::u8string_view& ps) {
std::u8string enpass = md5_crypt(ps, ep);
return (enpass == ep);
}
std::u8string Player::EncPass(const std::u8string_view& ps) {
std::u8string salt = u8"$1$", app = u8" ";
for (int ctr = 0; ctr < 8; ++ctr) {
app[0] = salt_char[Dice::Rand(0, 63)];
salt += app;
}
return md5_crypt(ps, salt);
}
void Player::SetName(const std::u8string_view& nm) {
player_list.erase(name);
name = nm;
player_list[name] = this;
room->SetShortDesc(fmt::format(u8"{}'s character room", nm));
std::u8string desc = u8"Available commands:\n";
desc += u8" " CMAG u8"look" CNRM u8": Show all your existing characters.\n";
desc += u8" " CMAG u8"world" CNRM u8": Show list of currently available worlds.\n";
desc +=
u8" " CMAG u8"world" CNRM u8" " CGRN u8"<world>" CNRM u8": Select a world to make a new character in.\n";
desc +=
u8" " CMAG u8"enter" CNRM u8" " CGRN u8"<character_name>" CNRM u8": Enter a finished character.\n";
desc +=
u8" " CMAG u8"select" CNRM u8" " CGRN u8"<character_name>" CNRM u8": Select an unfinished character.\n";
desc +=
u8" " CMAG u8"newcharacter" CNRM u8" " CGRN u8"<character_name>" CNRM u8": Create a new character.\n";
desc +=
u8" " CMAG u8"raise" CNRM u8" " CGRN u8"<stat>" CNRM u8": Raise a stat of the currently selected character.\n";
desc += u8" " CMAG u8"randomize" CNRM
u8": Randomly spend all remaining points of current "
u8"character.\n";
desc +=
u8" " CMAG u8"help" CNRM u8" " CGRN u8"<topic>" CNRM u8": Get more info (try 'help commands').\n";
desc += u8" Here are all of your current characters:";
room->SetDesc(desc);
}
void Player::Link(Object* obj) {}
void Player::AddChar(Object* ch) {
room->AddLink(ch);
creator = ch;
world = ch->World();
}
Object* Player::World() {
return (world && Object::Universe()->Contains(world)) ? world : nullptr;
}
int player_exists(const std::u8string_view& name) {
return player_list.count(std::u8string(name));
}
Player* get_player(const std::u8string_view& name) {
if (!player_list.contains(std::u8string(name))) {
return nullptr;
}
Player* pl = player_list[std::u8string(name)];
non_init.erase(pl);
return pl;
}
Player* player_login(const std::u8string_view& name, const std::u8string_view& pass) {
if (!player_list.contains(std::u8string(name))) {
return nullptr;
}
Player* pl = player_list[std::u8string(name)];
if (!pl->AuthPass(pass)) {
if (non_init.count(pl)) {
player_list.erase(std::u8string(name));
non_init.erase(pl);
delete pl;
}
return nullptr;
}
non_init.erase(pl);
if ((player_list.size() - non_init.size()) == 1) {
pl->Set(PLAYER_NINJA);
pl->Set(PLAYER_SUPERNINJA);
}
return pl;
}
int Player::LoadFrom(std::u8string_view& fl) {
SetName(std::u8string(getuntil(fl, '\n')));
skipspace(fl);
pass = getuntil(fl, '\n');
skipspace(fl);
// Player experience - obsolete, ignored
nextchar(fl); // Skip Colon
nextnum(fl); // Skip Number
char8_t delim = nextchar(fl);
while (delim == ';') {
nextnum(fl);
// Do nothing - ignore these
delim = nextchar(fl);
}
if (delim == ':') {
flags = nexthex(fl);
}
while (nextchar(fl) == ':') {
AddChar(getbynum(nextnum(fl)));
}
// Won't be present previous to v0x02, but that's ok, will work fine anyway
while (nextchar(fl) == ';') {
auto varname = getuntil(fl, ':');
nextchar(fl); // Skip the ':'
vars[std::u8string(varname)] = getuntil(fl, ';');
}
return 0;
}
void free_players() {
std::set<Player*> todel;
for (auto p : player_list) {
todel.insert(p.second);
}
for (auto p : todel) {
delete p;
}
}
int load_players(const std::u8string_view& fn) {
loge(u8"Loading Players.");
infile fl(fn);
if (!fl)
return -1;
unsigned int ver = nexthex(fl);
skipspace(fl);
int num = nextnum(fl);
skipspace(fl);
loge(u8"Loading Players: {},{}", ver, num);
for (int ctr = 0; ctr < num; ++ctr) {
Player* pl = new Player(u8";;TEMP;;", u8";;TEMP;;");
non_init.erase(pl);
pl->LoadFrom(fl);
// loge(u8"Loaded Player: {}", pl->Name());
}
return 0;
}
int Player::SaveTo(outfile& fl) {
fl.append(u8"{}\n{}\n", name, pass);
fl.append(u8":0"); // Player experience, obsolete, always zero
fl.append(u8":{:X}", flags);
fl.append(u8"{}\n", room->WriteContents());
for (auto var : vars) {
fl.append(u8";{}:{}", var.first, var.second);
}
fl.append(u8"\n");
return 0;
}
int save_players(const std::u8string_view& fn) {
outfile fl(fn);
if (!fl)
return -1;
fl.append(u8"{:08X}\n", CurrentVersion.savefile_version_player);
fl.append(u8"{}\n", player_list.size() - non_init.size());
for (auto pl : player_list) {
if (non_init.count(pl.second) == 0) {
if (pl.second->SaveTo(fl))
return -1;
}
}
return 0;
}
void player_rooms_erase(Object* obj) {
for (auto pl : player_list) {
if (pl.second->Creator() == obj)
pl.second->SetCreator(nullptr);
pl.second->Room()->RemoveLink(obj);
}
}
int is_pc(const Object* obj) {
for (auto pl : player_list) {
if (pl.second->Room() && pl.second->Room()->Contains(obj)) {
return 1;
}
}
return 0;
}
std::vector<Player*> get_all_players() {
std::vector<Player*> ret;
for (auto pl : player_list) {
if (pl.second->Room()) {
ret.push_back(pl.second);
}
}
return ret;
}