コード例 #1
0
ファイル: botctl.cpp プロジェクト: kikidong/avbot
static void iopost_msg(boost::asio::io_service & io_service,
	boost::function<void(std::string)> msg_sender,
	std::string msg, std::string groupid)
{
	io_service.post(boost::bind(msg_sender, msg));
	logfile.add_log(groupid, msg);
}
コード例 #2
0
ファイル: extension.cpp プロジェクト: JasonHuang/avbot
static void sender(avbot & mybot,std::string channel_name, std::string txt, bool logtohtml)
{
	mybot.broadcast_message(channel_name, txt);
	if (logtohtml){
		logfile.add_log(channel_name, avlog::html_escape(txt), 0);
	}
}
コード例 #3
0
ファイル: botctl.cpp プロジェクト: kikidong/avbot
// 命令控制, 所有的协议都能享受的命令控制在这里实现.
// msg_sender 是一个函数, on_command 用/*它发送消息.
void on_bot_command(avbot::av_message_tree jsonmessage, avbot & mybot)
{

	boost::regex ex;
	boost::smatch what;
	webqq::qqGroup_ptr  group;
	boost::function<void(std::string)> msg_sender = boost::bind(
		&avbot::broadcast_message, &mybot,
		jsonmessage.get<std::string>("channel"), _1
	);

	boost::function<void(std::string)> sendmsg = mybot.get_io_service().wrap(
		boost::bind(iopost_msg, boost::ref(mybot.get_io_service()),
			msg_sender, _1, jsonmessage.get<std::string>("channel"))
	);

	std::string message = boost::trim_copy(jsonmessage.get<std::string>("message.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"
			"以上!"
		);
	}

	if (message == ".qqbot ping")
	{
		sendmsg("我还活着!");
		return;
	}

	if (message == ".qqbot version")
	{
		sendmsg(boost::str(boost::format("我的版本是 %s (%s %s)")
			% QQBOT_VERSION % __DATE__ % __TIME__)
		);
		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 = jsonmessage.get<std::string>("channel");
		mrecoder.mybot = & mybot;
		mrecoder.sendmsg = msg_sender;
		mybot.on_message.connect(mrecoder);
		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("哈? 输入验证码干嘛?");
		}
	}

	// 向新人问候.

	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(
			jsonmessage.get<std::string>("channel") +
			"/welcome.txt"
		);

		question.add_to_list(list);
		question.on_handle_message(msg_sender);
	}

	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];

		std::string channelname =  jsonmessage.get<std::string>("channel");
		webqq::qqGroup_ptr groupptr =  mybot.get_qq()->get_Group_by_qq(channelname);

		if (groupptr)
		{
			webqq::qqBuddy * budyptr = groupptr->get_Buddy_by_uin(uin);

			if (!budyptr)
			{
				webqq::qqBuddy budy;
				budy.uin = uin;
				budy.nick = nick;
				groupptr->memberlist.insert(std::make_pair(uin, budy));
			}
			else
			{
				if (budyptr->nick.empty())
				{
					budyptr->nick = nick;
				}
			}
		}

	}

	if (jsonmessage.get<int>("op") != 1)
		return;

	// 开始讲座记录.
	ex.set_expression(".qqbot begin class ?\"(.*)?\"");

	if (boost::regex_match(message, what, ex))
	{
		std::string title = what[1];

		if (title.empty()) return ;

		group = mybot.get_qq()->get_Group_by_qq(jsonmessage.get<std::string>("channel"));

		if (group &&  !logfile.begin_lecture(group->qqnum, title))
		{
			sendmsg("lecture failed!\n");
		}

		return;
	}

	// 停止讲座记录.
	if (message == ".qqbot end class")
	{
		logfile.end_lecture();
		return;
	}

	if (message == ".qqbot exit")
	{
		exit(0);
	}

	ex.set_expression(".qqbot join group ([0-9]+)");

	if (boost::regex_match(message, what, ex))
	{
		mybot.get_qq()->search_group(
			what[1],
			"",
			boost::bind(
				handle_search_group,
				std::string(what[1]), _1, _2, _3,
				mybot.get_qq(), msg_sender
			)
		);
	}

	// 重新加载群成员列表.
	if (message == ".qqbot reload")
	{
		group = mybot.get_qq()->get_Group_by_qq(jsonmessage.get<std::string>("channel"));

		if (group)
			mybot.get_io_service().post(
				boost::bind(&webqq::webqq::update_group_member, mybot.get_qq() , group)
			);

		sendmsg("群成员列表重加载.");

		return;
	}
}