forked from kikidong/avbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botctl.cpp
411 lines (340 loc) · 9.68 KB
/
botctl.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* botcmd.cpp 实现 .qqbot 命令控制
*/
#include <string>
#include <algorithm>
#include <vector>
#include <memory>
#include <iostream>
#include <boost/regex.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <boost/locale.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <locale.h>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
#if defined(_MSC_VER)
#include <direct.h>
#endif
#include "boost/stringencodings.hpp"
#include "libavlog/avlog.hpp"
#include "libavbot/avbot.hpp"
#include "libavbot/avchannel.hpp"
#ifndef NO_WEBQQ
#include "libwebqq/webqq.hpp"
#endif
#include "auto_welcome.hpp"
#include "botctl.hpp"
extern "C" const char * avbot_version();
extern "C" const char * avbot_version_build_time();
extern avlog logfile; // 用于记录日志文件.
static void iopost_msg(boost::asio::io_service & io_service,
std::function<void(std::string)> msg_sender,
std::string msg, std::string groupid)
{
io_service.post(std::bind(msg_sender, msg));
logfile.add_log(groupid, msg, 0);
}
static void write_vcode(const std::string & vc_img_data)
{
std::ofstream vcode("vercode.jpeg", std::ofstream::out);
vcode.write(vc_img_data.data(), vc_img_data.size());
vcode.close();
}
static std::function<void (std::string)> do_vc_code;
#ifndef NO_WEBQQ
static void handle_join_group(webqq::qqGroup_ptr group, bool needvc,
const std::string & vc_img_data, std::shared_ptr<webqq::webqq> qqclient,
std::function<void(std::string)> msg_sender)
{
if (needvc && group)
{
// 写入验证码.
write_vcode(vc_img_data);
std::string msg = boost::str(
boost::format(
"哎呀,加入群%s的过程中要输入验证码,请使用 .qqbot vc XXXX 输入。文件为 qqlog 目录下的 vercode.jpeg"
)
% group->qqnum
);
std::cout << utf8_to_local_encode(msg) << std::endl;
msg_sender(msg);
webqq::webqq::join_group_handler join_group_handler;
join_group_handler = std::bind(
handle_join_group, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, qqclient, msg_sender
);
do_vc_code = std::bind(
&webqq::webqq::join_group, qqclient,
group, std::placeholders::_1, join_group_handler
);
}
else if (group && !needvc)
{
msg_sender("哎呦妈呀,群加入了呢~等待对方管理员通过\n记得修改 qqbotrc 将群添加到频道组哦~");
}
else
{
msg_sender("哎呦妈呀,群加不了!");
}
}
static void handle_search_group(std::string groupqqnum, webqq::qqGroup_ptr group,
bool needvc, const std::string & vc_img_data, std::shared_ptr<webqq::webqq> qqclient,
std::function<void(std::string)> msg_sender)
{
static webqq::qqGroup_ptr _group;
if (needvc)
{
// 写入验证码.
write_vcode(vc_img_data);
// 向大家吵闹输入验证码.
std::string msg = boost::str(
boost::format(
"哎呀,查找群%s的过程中要输入验证码,请使用 .qqbot vc XXXX 输入。文件为 qqlog 目录下的 vercode.jpeg"
)
% groupqqnum
);
std::cout << utf8_to_local_encode(msg) << std::endl;
msg_sender(msg);
webqq::webqq::search_group_handler search_group_handler;
search_group_handler = std::bind(
handle_search_group, groupqqnum, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, qqclient, msg_sender
);
do_vc_code = std::bind(
&webqq::webqq::search_group, qqclient,
groupqqnum,
std::placeholders::_1,
search_group_handler
);
}
else if (group)
{
// 很好,加入群吧!
msg_sender("哈呀,验证码正确了个去的,申请加入ing");
qqclient->join_group(
group,
"",
std::bind(handle_join_group, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, qqclient, msg_sender)
);
}
else
{
msg_sender("没找到没找到!");
}
}
#endif
struct mail_recoder
{
typedef void result_type;
std::string channel;
std::shared_ptr<InternetMailFormat> pimf;
avbot * mybot;
std::function<void(std::string)> sendmsg;
// 由 avbot 的 on_message 调用.
//channel_identifier, avbotmsg, send_avbot_message_t, boost::asio::yield_context
void operator()(channel_identifier cid, avbotmsg message, send_avchannel_message_t, boost::asio::yield_context, const boost::signals2::connection & con)
{
static boost::regex ex(".qqbot mail subject \"?(.*)\"?");
boost::cmatch what;
try
{
std::string tmsg = mybot->format_message_for_textIM(message);
if (tmsg != ".qqbot end mail" && tmsg != ".qqbot mail end")
{
if (boost::regex_match(tmsg.c_str(), what, ex))
{
pimf->header["subject"] = what[1];
}
else
{
boost::get<std::string>(pimf->body) += mybot->format_message_for_textIM(message);
}
}
else
{
mybot->get_mx()->async_send_mail(*pimf, *this);
con.disconnect();
}
}
catch (...)
{
boost::get<std::string>(pimf->body) += mybot->format_message_for_textIM(message);
}
}
void operator()(const boost::system::error_code & ec)
{
if (ec)
sendmsg("邮件发送失败!以上!");
else
sendmsg("邮件发送成功!以上!");
}
};
//-------------
// 命令控制, 所有的协议都能享受的命令控制在这里实现.
// msg_sender 是一个函数, on_command 用/*它发送消息.
void on_bot_command(channel_identifier cid, avbotmsg avmessage, send_avchannel_message_t sendmsg, boost::asio::yield_context yield_context, avbot& mybot, avchannel& channel)
{
boost::regex ex;
boost::smatch what;
#ifndef NO_WEBQQ
webqq::qqGroup_ptr group;
#endif
std::string channelname = channel.name();
auto msg_sender = boost::bind(&avbot::send_broadcast_message, &mybot,
channelname, _1
);
std::string message = boost::trim_copy(avmessage.to_plain_text());
if (message == ".qqbot help")
{
sendmsg(
"可用的命令\n"
"\t.qqbot help\n"
"\t.qqbot version\n"
"\t.qqbot ping\n"
"\t.qqbot joke off 关闭笑话\n"
"\t.qqbot joke on 开启笑话\n"
"\t.qqbot joke interval 80 设定笑话间隔到80s\n"
"\t.qqbot 给大爷讲个笑话 讲个笑话\n"
"\t.qqbot mail to \"emailaddress\"\n"
"\t 将命令中间的聊天内容发送到邮件 emailaddress, 注意引号\n"
"\t 使用 .qqbot mail subject 设置主题\n"
"\t.qqbot mail end\n"
"\t.qqbot welcome newbie 欢迎新人\n"
"== 以下命令需要管理员才能使用==\n"
"\t.qqbot relogin 强制重新登录qq\n\t.qqbot reload 重新加载群成员列表\n"
"\t.qqbot begin class XXX\t开课\n\t.qqbot end class 下课\n"
"以上!", yield_context
);
}
if (message == ".qqbot ping")
{
sendmsg("我还活着!", yield_context);
return;
}
if (message == ".qqbot version")
{
sendmsg(boost::str(boost::format("我的版本是 %s (%s %s)")
% avbot_version() % __DATE__ % __TIME__), yield_context
);
return;
}
ex.set_expression(".qqbot mail to \"?(.*)\"?");
if (boost::regex_match(message, what, ex) && mybot.get_mx())
{
//if (mybot.get_mx())
// 进入邮件记录模式.
mail_recoder mrecoder;
mrecoder.pimf.reset(new InternetMailFormat);
mrecoder.pimf->header["from"] = mybot.get_mx()->mailaddres();
mrecoder.pimf->header["to"] = what[1];
mrecoder.pimf->header["subject"] = "send by avbot";
mrecoder.pimf->body = std::string("");
mrecoder.channel = channelname;
mrecoder.mybot = & mybot;
mrecoder.sendmsg = msg_sender;
avchannel::handle_extra_message_type::extended_slot_type mrecoder_slot(mrecoder, _2, _3, _4, _5, _1);
channel.handle_extra_message.connect_extended(mrecoder_slot);
return;
}
ex.set_expression(".vc (.*)");
if (boost::regex_match(message, what, ex))
{
mybot.feed_login_verify_code(what[1]);
}
ex.set_expression(".qqbot vc (.*)");
if (boost::regex_match(message, what, ex))
{
if (do_vc_code)
do_vc_code(what[1]);
else
{
sendmsg("哈? 输入验证码干嘛?", yield_context);
}
}
// 向新人问候.
ex.set_expression(".qqbot (newbee|newbie|welcome) (.*)");
if (boost::regex_match(message, what, ex))
{
std::string nick = what[2];
if (nick.empty())
return;
auto_welcome::value_qq_list list;
list.push_back(nick);
auto_welcome question(
channelname + "/welcome.txt"
);
question.add_to_list(list);
question.on_handle_message(msg_sender);
}
#ifndef NO_WEBQQ
ex.set_expression("^\\.qqbot qqnickmap +([\\d]+) +([^ ]+)");
if (boost::regex_search(message, what, ex))
{
// map what[1] to what[2];
std::string uin = what[1];
std::string nick = what[2];
webqq::qqGroup_ptr groupptr ; // TODO 9= mybot.get_qq()->get_Group_by_qq(channelname);
if (groupptr)
{
webqq::qqBuddy_ptr budyptr = groupptr->get_Buddy_by_uin(uin);
if (!budyptr)
{
groupptr->add_new_buddy(uin, "", nick);
}
}
}
#endif
if (!avmessage.sender.is_op)
return;
// 开始讲座记录.
ex.set_expression(".qqbot begin class ?\"(.*)?\"");
if (boost::regex_match(message, what, ex))
{
std::string title = what[1];
if (title.empty()) return ;
if (!logfile.begin_lecture(channelname, title))
{
sendmsg("lecture failed!\n", yield_context);
}
return;
}
// 停止讲座记录.
if (message == ".qqbot end class")
{
logfile.end_lecture();
return;
}
if (message == ".qqbot exit")
{
exit(0);
}
// 重新加载群成员列表.
if (message == ".qqbot reload")
{
// group = mybot.get_qq()->get_Group_by_qq(jsonmessage.get<std::string>("channel"));
//
// if (group)
// mybot.get_io_service().post(
// std::bind(&webqq::webqq::update_group_member, mybot.get_qq() , group)
// );
sendmsg("群成员列表重加载.", yield_context);
return;
}
}
void set_do_vc(std::function<void(std::string)> f)
{
do_vc_code = f;
}
void set_do_vc()
{
do_vc_code = std::function<void(std::string)>();
}