-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.cpp
383 lines (347 loc) · 13.4 KB
/
manager.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
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
#include <string>
#include <dvutil/strings.h>
#include <dvutil/enum2str.h>
#include "command.h"
#include "manager.h"
Manager::Manager (const std::string& name, const Dv::Props& config, Dv::Debugable* debug) :
thread_ (name, *this, config ("timeout"), 0, config ("debuglevel"), debug),
done_ (false), config_ (config), _maildrops (config ("top").str ()) { }
void
Manager::kill ()
{
// Kill all the player threads.
for ( Player::Set::iterator p = players_.begin(); p != players_.end(); ++p )
( *p )->kill();
// And wait for them to finish.
for ( Player::Set::iterator p = players_.begin(); p != players_.end(); ++p )
( *p )->wait();
// Now kill the manager thread.
thread_.kill();
// And wait for it to finish.
thread_.join();
}
void
Manager::remove_player (Player* p)
{
if ( players_.count(p) )
{
log() << "remove " << p->name() << std::endl;
roots_.erase(p);
players_by_name_.erase(p->name());
players_.erase(p);
p->kill();
}
}
std::string
Manager::operator()(const Player::Message& m) throw (std::runtime_error)
{
// Status indicators
static const std::string ok("+OK");
static const std::string error("-ERR");
static const std::string anonymous("anonymous");
// logging output to server console
log() << "< "
<< (m.first->name().size() ? m.first->name() : anonymous)
<< ":" << m.second << std::endl;
// dump the message to an string stream for easy parsing
std::istringstream iss(m.second);
std::string command_word;
// read first word of command, it should correspond to one
// of the Command values.
if ( !(iss >> command_word) )
return "syntax error";
try
{
// the following will throw if there is no value in Command
// corresponding to the first word
Command c = str2command(Dv::String::trim(command_word));
if ( c != ADDPLAYER )
{
// Don't reply to players that have been killed (but apparently
// don't know it yet).
if ( players_.count(m.first) == 0 )
throw std::runtime_error("abandon request from killed player");
}
switch (c)
{
case ADDPLAYER: // ADDPLAYER -- add the sending player
{
players_.insert(m.first);
_players_states.insert(std::pair<Player*, State > (m.first, Authorization));
return ok;
}
break;
case USER: // USER name -- sending player logs in with the username
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Authorization )
{
std::string user_name;
// Did the user enter a username?
if ( iss >> user_name )
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
// The player's name is set find his or her maildrop later
it->first->set_name(user_name);
// The player enters the transaction name
if ( _maildrops.new_maildrop(m.first) )
{
it->second = Transaction;
return ok;
}
else
return error + " invalid username";
}
else
return error + " user <username>";
}
else
return error;
}
break;
case PASS: // PASS string -- sending players enters password
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Authorization )
return ok;
else
return error;
}
case QUIT: // QUIT -- sending player quits
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
/* If th player is in the transaction state we need to clean up
* his or her maildrop */
if ( it->second == Transaction )
{
_maildrops.remove_maildrop(m.first);
remove_player(m.first);
return ok;
}
else
{
remove_player(m.first);
return ok;
}
}
break;
case STAT: // STAT -- get info on sending player's maildrop
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
std::ostringstream oss;
oss << ok << " " << maildrop->nr_of_messages()
<< " " << maildrop->size();
return oss.str();
}
else
return error + " use 'user <username>' first";
}
break;
case RETR: // RETR msg -- send message with mesg number ot sending player
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
std::ostringstream oss;
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
int msg_nr;
// Did the player enter a message number?
if ( (iss >> msg_nr) )
{
Message * message(maildrop->retrieve_message(msg_nr));
if ( message )
{
oss << ok << " " << message->size()
<< " octets" << std::endl
<< message->message_string() << std::endl;
return oss.str();
}
else
return error + " invalid message number";
}
else
return error + " retr <msg number>";
}
else
return error + " use 'user <username>' first";
}
break;
case DELE: // DELE msg -- dlete message with message number from sending player's maildrop
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
int msg_nr;
if ( iss >> msg_nr )
{
bool flag(maildrop->delete_message(msg_nr));
if ( flag )
return ok + " message deleted";
else
return error + " invalid message number";
}
else
return error + " dele <msg number>";
}
else
return error + " use 'user <username>' first";
}
break;
case LIST: // LIST [msg] -- get info on all or one message from sending playe's maildrop
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
std::ostringstream oss;
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
int msg_nr;
// Message number is given
if ( iss >> msg_nr )
{
Message * message(maildrop->retrieve_message(msg_nr));
if ( message )
{
oss << ok << " " << (*message);
return oss.str();
}
else
return error + " invalid message number";
}
// Give list info for each message
else
{
std::vector<Message*> msgs(maildrop->messages());
oss << ok << " " << maildrop->nr_of_messages() << " messages "
<< "(" << maildrop->size() << " octets)" << std::endl;
for ( unsigned int i = 0; i < msgs.size(); i++ )
{
oss << (*msgs[i]) << std::endl;
}
return oss.str();
}
}
else
return error + " use 'user <username>' first";
}
break;
case NOOP: // NOOP -- just returns positive response
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
return ok;
else
return error + " use 'user <username>' first";
}
break;
case UIDL: // UIDL [msg] -- get uidl of all or one message of the sending player's maildrop
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
std::ostringstream oss;
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
int msg_nr;
// Message number is given
if ( iss >> msg_nr )
{
Message * message(maildrop->retrieve_message(msg_nr));
if ( message )
{
oss << msg_nr << " " << message->uidl() << "\n";
return ok + " " + oss.str();
}
else
return error + " invalid message number";
}
// Give uidl for each message in the maildrop
else
{
std::vector<Message*> msgs(maildrop->messages());
for ( unsigned int i = 0; i < msgs.size(); i++ )
{
oss << msgs.at(i)->number() << " " << msgs.at(i)->uidl() << std::endl;
}
return oss.str();
}
}
else
return error + " use 'user <username>' first";
}
break;
case TOP: // TOP msg n -- same as retr but only top n lines
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
std::ostringstream oss;
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
int msg_nr;
if ( iss >> msg_nr )
{
int n;
if ( iss >> n )
{
Message * message(maildrop->retrieve_message(msg_nr));
if ( message )
{
oss << ok << " " << message->size() << " " << "\n";
oss << message->message_string(n) << "\n";
return oss.str();
}
else
return error + " invalid message number";
}
else
return error + " top <msg number> <nr of lines>";
}
else
return error + " top <msg number> <nr of lines>";
}
else
return error + " use 'user <username>' first";
}
break;
case RSET:
{
std::map<Player*, State>::iterator it(_players_states.find(m.first));
if ( it->second == Transaction )
{
std::ostringstream oss;
Maildrop * maildrop(_maildrops.find_maildrop(m.first));
if ( maildrop )
{
std::vector<Message*> msgs(maildrop->messages(true));
for ( unsigned int i = 0; i < msgs.size(); i++ )
{
if ( msgs.at(i)->deleted() )
msgs.at(i)->deleted(false);
}
oss << ok << " maildrop has " << maildrop->nr_of_messages()
<< " messages (" << maildrop->size() << " octets)";
return oss.str();
}
}
else
return error + " use 'user <username>' first";
}
break;
case SHUTDOWN: // SHUTDOWN -- shutdown server, only for convenience
{
done_ = true;
return ok;
}
default:
return command2str(c) + ": not yet implemented";
}
}
catch (std::logic_error& e)
{
log() << e.what() << std::endl;
return "syntax error";
}
return "impossible reply";
}