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.h
167 lines (135 loc) · 5.63 KB
/
message.h
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
#pragma once
#include "./common.h"
#include "./target.h"
#include "./utils/string.h"
namespace cq::message {
/**
* Escape special characters in the given string.
*/
std::string escape(std::string str, bool escape_comma = true);
/**
* Unescape special characters in the given string.
*/
std::string unescape(std::string str);
struct MessageSegment {
std::string type;
std::map<std::string, std::string> data;
static MessageSegment text(const std::string &text) { return {"text", {{"text", text}}}; }
static MessageSegment emoji(const uint32_t id) { return {"emoji", {{"id", std::to_string(id)}}}; }
static MessageSegment face(const int id) { return {"face", {{"id", std::to_string(id)}}}; }
static MessageSegment image(const std::string &file) { return {"image", {{"file", file}}}; }
static MessageSegment record(const std::string &file, const bool magic = false) {
return {"record", {{"file", file}, {"magic", std::to_string(magic)}}};
}
static MessageSegment at(const int64_t user_id) { return {"at", {{"qq", std::to_string(user_id)}}}; }
static MessageSegment rps() { return {"rps", {}}; }
static MessageSegment dice() { return {"dice", {}}; }
static MessageSegment shake() { return {"shake", {}}; }
static MessageSegment anonymous(const bool ignore_failure = false) {
return {"anonymous", {{"ignore", std::to_string(ignore_failure)}}};
}
static MessageSegment share(const std::string &url, const std::string &title, const std::string &content = "",
const std::string &image_url = "") {
return {"share", {{"url", url}, {"title", title}, {"content", content}, {"image", image_url}}};
}
enum class ContactType { USER, GROUP };
static MessageSegment contact(const ContactType &type, const int64_t id) {
return {
"contact",
{
{"type", type == ContactType::USER ? "qq" : "group"},
{"id", std::to_string(id)},
},
};
}
static MessageSegment location(const double latitude, const double longitude, const std::string &title = "",
const std::string &content = "") {
return {
"location",
{
{"lat", std::to_string(latitude)},
{"lon", std::to_string(longitude)},
{"title", title},
{"content", content},
},
};
}
static MessageSegment music(const std::string &type, const int64_t id) {
return {"music", {{"type", type}, {"id", std::to_string(id)}}};
}
static MessageSegment music(const std::string &type, const int64_t id, const int32_t style) {
return {"music", {{"type", type}, {"id", std::to_string(id)}, {"style", std::to_string(style)}}};
}
static MessageSegment music(const std::string &url, const std::string &audio_url, const std::string &title,
const std::string &content = "", const std::string &image_url = "") {
return {
"music",
{
{"type", "custom"},
{"url", url},
{"audio", audio_url},
{"title", title},
{"content", content},
{"image", image_url},
},
};
}
};
struct Message : std::list<MessageSegment> {
Message() = default;
/**
* Split a string to a Message object.
*/
Message(const std::string &msg_str);
Message(const char *msg_str) : Message(std::string(msg_str)) {}
Message(const MessageSegment &seg) { this->push_back(seg); }
/**
* Merge all segments to a string.
*/
operator std::string() const;
Message &operator+=(const Message &other) {
this->insert(this->end(), other.begin(), other.end());
this->reduce();
return *this;
}
template <typename T>
Message &operator+=(const T &other) {
return this->operator+=(Message(other));
}
Message operator+(const Message &other) const {
auto result = *this;
result += other; // use operator+=
return result;
}
template <typename T>
Message operator+(const T &other) const {
return this->operator+(Message(other));
}
/**
* Send the message to a given target.
*/
int64_t send(const Target &target) const;
/**
* Extract and merge plain text segments in the message.
*/
std::string extract_plain_text() const;
std::list<MessageSegment> &segments() { return *this; }
const std::list<MessageSegment> &segments() const { return *this; }
/**
* Merge adjacent "text" segments.
*/
void reduce();
};
template <typename T>
Message operator+(const T &lhs, const Message &rhs) {
return Message(lhs) + rhs;
}
/**
* Send a message to the given target.
* Thanks to the auto type conversion, a Segment object can also be passed in.
*/
inline int64_t send(const Target &target, const Message &msg) { return msg.send(target); }
} // namespace cq::message
namespace std {
inline string to_string(const cq::message::Message &msg) { return string(msg); }
} // namespace std