-
Notifications
You must be signed in to change notification settings - Fork 1
/
ircmsg.cpp
173 lines (143 loc) · 3.33 KB
/
ircmsg.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
/*
* Copyright (C) 2001-2009 Jan Vidar Krey, janvidar@extatic.org
* See the file "COPYING" for licensing details.
*/
#include "ircmsg.h"
#include <iostream>
#include <sstream>
// #define IRC_MSG_DEBUG
// #define IRC_MSG_DEBUG_SPLIT
IRC::Message::Message()
{
}
IRC::Message::Message(const IRC::Message& msg)
{
message = msg.message;
parse();
}
IRC::Message::Message(const std::string& command)
{
message = command;
parse();
}
IRC::Message::~Message()
{
}
void IRC::Message::addParameter(const std::string& param)
{
params.push_back(param);
message += " ";
message += param;
}
void IRC::Message::write(Samurai::IO::Buffer* buffer)
{
buffer->append(message);
buffer->append("\r\n");
#ifdef IRC_MSG_DEBUG
std::cout << "SEND: " << message << std::endl;
#endif
}
bool IRC::Message::read(Samurai::IO::Buffer* buffer)
{
int offset = buffer->find('\n');
if (offset == -1)
return false;
message = buffer->pop(offset);
buffer->remove(offset+1);
// strip 'cr'
if (message[message.size()-1] == '\r')
message = message.substr(0, message.size()-1);
#ifdef IRC_MSG_DEBUG
std::cout << "READ: " << message << std::endl;
#endif
return true;
}
std::string IRC::Message::getCommand()
{
return command;
}
std::string IRC::Message::getPrefix()
{
return prefix;
}
std::string IRC::Message::getArgument(size_t offset, bool stripColon)
{
if (offset >= params.size())
throw "Out of bounds!";
std::string tmp = params[offset];
if (stripColon && tmp.length() >= 1 && tmp[0] == ':')
tmp = tmp.substr(1);
return tmp;
}
std::string IRC::Message::getArguments(size_t offset, bool stripColon)
{
if (params.size() == 0 || offset >= params.size())
return "";
std::string tmp = params[offset];
if (stripColon && tmp.length() >= 1 && tmp[0] == ':')
tmp = tmp.substr(1);
for (size_t n = offset + 1; n < params.size(); n++)
{
tmp += " ";
tmp += params[n];
}
return tmp;
}
void IRC::Message::parse()
{
size_t offset = 0;
size_t length = 0;
prefix = "";
command = "";
if (message[0] == ':')
{
offset = message.find(' ');
if (offset == std::string::npos)
{
throw "Wtf?";
}
else
{
prefix = message.substr(1, offset-1);
offset++;
}
}
length = message.find(' ', offset);
if (length == std::string::npos)
{
command = message.substr(offset);
}
else
{
length -= offset;
command = message.substr(offset, length);
// extract parameters and push them into the params vector
offset += length;
offset++;
length = message.find(' ', offset);
while (length != std::string::npos)
{
length -= offset;
std::string arg = message.substr(offset, length);
#ifdef IRC_MSG_DEBUG_SPLIT
std::cout << "ARG: arg[" << params.size() << "]='" << arg << std::endl;
#endif
params.push_back(arg);
offset += length;
offset ++;
length = message.find(' ', offset);
}
if (message.size() > offset)
{
params.push_back(message.substr(offset));
}
}
#ifdef IRC_MSG_DEBUG_SPLIT
std::cout << "DETAIL: prefix='" << prefix << "', command='" << command << "', arguments='" << params.size() << "'" << std::endl;
#endif
}
void IRC::Message::dump()
{
std::cout << "DUMP: '" << message << "'" << std::endl;
std::cout << "DETAIL: prefix='" << prefix << "', command='" << command << "', arguments='" << params.size() << "'" << std::endl;
}