-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.cpp
More file actions
80 lines (69 loc) · 1.65 KB
/
Message.cpp
File metadata and controls
80 lines (69 loc) · 1.65 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
/*
* =====================================================================================
*
* Filename: Message.cpp
*
* Description:
*
* Version: 1.0
* Created: 04/04/17 13:03:18
* Revision: none
* Compiler: g++
*
* Author: Feng Jie<[email protected]>
*
* =====================================================================================
*/
#include <sstream>
#include "Message.h"
#include "UserCommand.h"
#include "PassCommand.h"
#include "PrivmsgCommand.h"
Message::Message(std::string &msg, MsgCommandParser &p)
: mMessage(msg), mParser(p)
{
mCmd = mParser.parse(mMessage);
}
Message::~Message()
{
if (NULL != mCmd) {
delete mCmd;
}
}
std::string Message::response()
{
std::string rsp("");
if (mCmd->verifyParams()) {
mCmd->execute(rsp);
}
return rsp;
}
int MsgCommand::addParam(std::string &p)
{
mParamList.push_back(p);
return mParamList.size();
}
MsgCommand *MsgCommandParser::parse(std::string &msg)
{
// Get cmd phase
std::string cmd_text = msg.substr(0, msg.find(' '));
std::string param_text = msg.substr(cmd_text.length(), msg.length() - cmd_text.length() - 1);
// Create concrete cmd subclass
MsgCommand *cmd;
if ("USER" == cmd_text) {
cmd = new UserCommand;
} else if ("PASS" == cmd_text) {
cmd = new PassCommand;
} else if ("PRIVMSG" == cmd_text) {
cmd = new PrivmsgCommand;
} else {
cmd = NULL;
}
// Parse the parameter list
std::string tmp;
std::stringstream ss(param_text);
while (ss >> tmp) {
cmd->addParam(tmp);
}
return cmd;
}