forked from qtdevs/qtassistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtassistant.cpp
288 lines (241 loc) · 8.35 KB
/
qtassistant.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
#include "qtassistant.h"
#include "qtassistant_p.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QPixmap>
#include <QStringBuilder>
#include <QTextStream>
#include <QTranslator>
#include <QUuid>
#include "sqldatas/masterlevels.h"
#include "sqldatas/memberwelcome.h"
#include "sqldatas/memberblacklist.h"
#include "sqldatas/memberdeathhouse.h"
#include "htmlfeedback/htmlfeedback.h"
// class QtAssistant
QtAssistant::QtAssistant(QObject *parent)
: CqPortal(*new QtAssistantPrivate(), parent)
{
Q_D(QtAssistant);
QTranslator *translator = new QTranslator(qApp);
if (translator->load("qtassistant_zh.qm", ":/translations")) {
qApp->installTranslator(translator);
}
d->levels = new MasterLevels(this);
d->welcome = new MemberWelcome(this);
d->blacklist = new MemberBlacklist(this);
d->deathHouse = new MemberDeathHouse(this);
d->htmlFeedback = new HtmlFeedback(this);
d->checkTimerId = startTimer(10000);
}
QtAssistant::~QtAssistant()
{
Q_D(QtAssistant);
killTimer(d->checkTimerId);
}
void QtAssistant::timerEvent(QTimerEvent *)
{
Q_D(QtAssistant);
// 检查新手名单。
do {
MemberList members;
d->welcome->expiredMembers(members);
for (const auto &member : members) {
CqMemberInfo mi = memberInfo(member.first, member.second, false);
if (mi.isValid() && mi.lastSent().isNull()) {
kickGroupMember(member.first, member.second, false);
}
}
} while (false);
// 检查踢出名单。
do {
MemberList members;
d->deathHouse->expiredMembers(members);
for (const auto &member : members) {
kickGroupMember(member.first, member.second, false);
}
} while (false);
}
void QtAssistant::permissionDenied(qint64 gid, qint64 uid, MasterLevel level, const QString &reason)
{
Q_UNUSED(uid);
Q_D(QtAssistant);
QString content = reason.isEmpty() ? tr("作为一个%1,你没有权限执行此操作。").arg(MasterLevels::levelName(level)) : reason;
QString html = QString("<html><body><span class=\"t\">%1</span><p class=\"c\">%2</p></body></html>").arg(tr("没有相关权限"), content);
QPixmap feedback = d->htmlFeedback->drawDanger(html, 400);
QString fileName = saveImage(feedback);
sendGroupMessage(gid, cqImage(fileName));
}
void QtAssistant::showPrimary(qint64 gid, const QString &title, const QString &content)
{
feedback(gid, title, content, HtmlFeedback::Primary);
}
void QtAssistant::showDanger(qint64 gid, const QString &title, const QString &content)
{
feedback(gid, title, content, HtmlFeedback::Danger);
}
void QtAssistant::showWarning(qint64 gid, const QString &title, const QString &content)
{
feedback(gid, title, content, HtmlFeedback::Warning);
}
void QtAssistant::showPrompt(qint64 gid, const QString &title, const QString &content)
{
feedback(gid, title, content, HtmlFeedback::Prompt);
}
void QtAssistant::showSuccess(qint64 gid, const QString &title, const QString &content)
{
feedback(gid, title, content, HtmlFeedback::Success);
}
void QtAssistant::feedback(qint64 gid, const QString &title, const QString &content, HtmlFeedback::Style style)
{
Q_D(QtAssistant);
QString html = QString("<html><body><span class=\"t\">%1</span><p class=\"c\">%2</p></body></html>").arg(title, content);
QPixmap feedback = d->htmlFeedback->draw(html, 400, style);
QString fileName = saveImage(feedback);
sendGroupMessage(gid, cqImage(fileName));
}
void QtAssistant::showPrimaryList(qint64 gid, const QString &title, const LevelInfoList &members, bool level)
{
feedbackList(gid, title, members, level, HtmlFeedback::Primary);
}
void QtAssistant::showDangerList(qint64 gid, const QString &title, const LevelInfoList &members, bool level)
{
feedbackList(gid, title, members, level, HtmlFeedback::Danger);
}
void QtAssistant::showWarningList(qint64 gid, const QString &title, const LevelInfoList &members, bool level)
{
feedbackList(gid, title, members, level, HtmlFeedback::Warning);
}
void QtAssistant::showPromptList(qint64 gid, const QString &title, const LevelInfoList &members, bool level)
{
feedbackList(gid, title, members, level, HtmlFeedback::Prompt);
}
void QtAssistant::showSuccessList(qint64 gid, const QString &title, const LevelInfoList &members, bool level)
{
feedbackList(gid, title, members, level, HtmlFeedback::Success);
}
void QtAssistant::feedbackList(qint64 gid, const QString &title, const LevelInfoList &members, bool level, HtmlFeedback::Style style)
{
Q_D(QtAssistant);
for (int i = 0, part = 0; i < members.count();) {
QString html;
do {
int cc = i + 4;
QTextStream ds(&html);
ds << "<html><body><span class=\"t\">" << title;
if (members.count() > 5) {
ds << tr("(Part %1)").arg(++part);
}
ds << "</span><div>";
for (; i < members.count(); ++i) {
const LevelInfo &li = members.at(i);
ds << "<p class=\"c\">";
if (level) {
ds << MasterLevels::levelName(li.level) << tr(": ");
}
CqMemberInfo mi = memberInfo(gid, li.uid);
if (mi.isValid()) {
if (!mi.nameCard().isEmpty()) {
ds << mi.nameCard();
} else {
ds << mi.nickName();
}
} else {
CqPersonInfo pi = personInfo(li.uid);
if (pi.isValid()) {
ds << pi.nickName();
} else {
ds << QString::number(li.uid);
}
ds << "<span>(" << tr("不在本群") << ")</span>";
}
ds << "</p>";
if (i == cc) {
++i;
break;
}
}
ds << "</div></body></html>";
} while (false);
QPixmap feedback = d->htmlFeedback->draw(html, 400, style);
QString fileName = saveImage(feedback);
sendGroupMessage(gid, cqImage(fileName));
}
}
// class QtAssistantPrivate
QtAssistantPrivate::QtAssistantPrivate()
: levels(Q_NULLPTR)
, welcome(Q_NULLPTR)
, blacklist(Q_NULLPTR)
, deathHouse(Q_NULLPTR)
, htmlFeedback(Q_NULLPTR)
, checkTimerId(-1)
{
}
QtAssistantPrivate::~QtAssistantPrivate()
{
}
LevelInfoList QtAssistantPrivate::findUsers(const QStringList &args)
{
QListIterator<QString> i(args);
LevelInfoList levels;
i.toBack();
while (i.hasPrevious()) {
const QString &arg = i.previous();
if (arg.startsWith(QStringLiteral("[CQ:at"))) {
qint64 uid = arg.mid(10, arg.count() - 11).toLongLong();
if (100000 > uid) {
break;
}
bool noFound = true;
for (const LevelInfo &li : levels) {
if (li.uid == uid) {
noFound = false;
break;
}
}
if (noFound) {
levels.prepend(LevelInfo(uid, MasterLevel::Unknown));
}
} else {
qint64 uid = arg.toLongLong();
if (100000 > uid) {
break;
}
bool noFound = true;
for (const LevelInfo &li : levels) {
if (li.uid == uid) {
noFound = false;
break;
}
}
if (noFound) {
levels.prepend(LevelInfo(uid, MasterLevel::Unknown));
}
}
}
return levels;
}
void QtAssistantPrivate::safetyNameCard(QString &nameCard)
{
// 屏蔽 emoji 表情
do {
int s = nameCard.indexOf("[CQ:emoji");
if (s < 0) {
break;
}
int e = nameCard.indexOf(']', s);
if (e < 0) {
break;
}
nameCard.replace(s, e - s + 1, "{?}");
} while (true);
}
void QtAssistantPrivate::formatNameCard(QString &nameCard)
{
// 在这里,我们对新名片做规范化处理。
nameCard.remove(' '); // 消除空格,不允许有空格。
nameCard.replace("【", "["); // 替换全角方括号,用半角方括号替代。
nameCard.replace("】", "]"); // 替换全角方括号,用半角方括号替代。
}