This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.cpp
185 lines (167 loc) · 6.46 KB
/
message.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
#include "./message.h"
#include <sstream>
#include "./api.h"
using namespace std;
namespace cq::message {
string escape(string str, const bool escape_comma) {
boost::replace_all(str, "&", "&");
boost::replace_all(str, "[", "[");
boost::replace_all(str, "]", "]");
if (escape_comma) boost::replace_all(str, ",", ",");
return str;
}
string unescape(string str) {
boost::replace_all(str, ",", ",");
boost::replace_all(str, "[", "[");
boost::replace_all(str, "]", "]");
boost::replace_all(str, "&", "&");
return str;
}
Message::Message(const string &msg_str) {
// implement a DFA manually, because the regex lib of VC++ will throw stack overflow in some cases
const static auto TEXT = 0;
const static auto FUNCTION_NAME = 1;
const static auto PARAMS = 2;
auto state = TEXT;
const auto end = msg_str.cend();
stringstream text_s, function_name_s, params_s;
auto curr_cq_start = end;
for (auto it = msg_str.cbegin(); it != end; ++it) {
const auto curr = *it;
switch (state) {
case TEXT: {
text:
if (curr == '[' && end - 1 - it >= 5 /* [CQ:a] at least 5 chars behind */
&& *(it + 1) == 'C' && *(it + 2) == 'Q' && *(it + 3) == ':' && *(it + 4) != ']') {
state = FUNCTION_NAME;
curr_cq_start = it;
it += 3;
} else {
text_s << curr;
}
break;
}
case FUNCTION_NAME: {
if ((curr >= 'A' && curr <= 'Z') || (curr >= 'a' && curr <= 'z') || (curr >= '0' && curr <= '9')) {
function_name_s << curr;
} else if (curr == ',') {
// function name out, params in
state = PARAMS;
} else if (curr == ']') {
// CQ code end, with no params
goto params;
} else {
// unrecognized character
text_s << string(curr_cq_start, it); // mark as text
curr_cq_start = end;
function_name_s = stringstream();
params_s = stringstream();
state = TEXT;
// because the current char may be '[', we goto text part
goto text;
}
break;
}
case PARAMS: {
params:
if (curr == ']') {
// CQ code end
MessageSegment seg;
seg.type = function_name_s.str();
vector<string> params;
boost::split(params, params_s.str(), boost::is_any_of(","));
for (const auto ¶m : params) {
const auto idx = param.find_first_of('=');
if (idx != string::npos) {
seg.data[boost::trim_copy(param.substr(0, idx))] = unescape(param.substr(idx + 1));
}
}
if (!text_s.str().empty()) {
// there is a text segment before this CQ code
this->push_back(MessageSegment{"text", {{"text", unescape(text_s.str())}}});
text_s = stringstream();
}
this->push_back(seg);
curr_cq_start = end;
text_s = stringstream();
function_name_s = stringstream();
params_s = stringstream();
state = TEXT;
} else {
params_s << curr;
}
}
default:
break;
}
}
// iterator end, there may be some rest of message we haven't put into segments
switch (state) {
case FUNCTION_NAME:
case PARAMS:
// we are in CQ code, but it ended with no ']', so it's a text segment
text_s << string(curr_cq_start, end);
// should fall through
case TEXT:
if (!text_s.str().empty()) {
this->push_back(MessageSegment{"text", {{"text", unescape(text_s.str())}}});
}
default:
break;
}
}
Message::operator string() const {
stringstream ss;
for (auto seg : *this) {
if (seg.type.empty()) {
continue;
}
if (seg.type == "text") {
if (const auto it = seg.data.find("text"); it != seg.data.end()) {
ss << escape((*it).second, false);
}
} else {
ss << "[CQ:" << seg.type;
for (const auto &item : seg.data) {
ss << "," << item.first << "=" << escape(item.second, true);
}
ss << "]";
}
}
return ss.str();
}
int64_t Message::send(const Target &target) const { return api::send_msg(target, *this); }
string Message::extract_plain_text() const {
string result;
for (const auto &seg : *this) {
if (seg.type == "text") {
result += seg.data.at("text") + " ";
}
}
if (!result.empty()) {
result.erase(result.end() - 1); // remove the trailing space
}
return result;
}
void Message::reduce() {
if (this->empty()) {
return;
}
auto last_seg_it = this->begin();
for (auto it = this->begin(); ++it != this->end();) {
if (it->type == "text" && last_seg_it->type == "text" && it->data.find("text") != it->data.end()
&& last_seg_it->data.find("text") != last_seg_it->data.end()) {
// found adjacent "text" segments
last_seg_it->data["text"] += it->data["text"];
// remove the current element and continue
this->erase(it);
it = last_seg_it;
} else {
last_seg_it = it;
}
}
if (this->size() == 1 && this->front().type == "text" && this->extract_plain_text().empty()) {
this->clear(); // the only item is an empty text segment, we should remove it
}
}
} // namespace cq::message