Esempio n. 1
0
	// 由 avbot 的 on_message 调用.
	void operator()(avbot::av_message_tree jsonmessage)
	{
		static boost::regex ex(".qqbot mail subject \"?(.*)\"?");
		boost::cmatch what;

		try
		{
			if (jsonmessage.get<std::string>("channel") != channel)
				return;

			std::string tmsg = boost::trim_copy(jsonmessage.get<std::string>("message.text"));

			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) += avbot::format_message(jsonmessage);
				}
			}
			else
			{
				mybot->get_mx()->async_send_mail(*pimf, *this);
			}
		}
		catch (...)
		{
			boost::get<std::string>(pimf->body) += avbot::format_message(jsonmessage);
		}
	}
Esempio n. 2
0
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);
	}
}
Esempio n. 3
0
void new_channel_set_extension(avbot& mybot, avchannel& channel, std::string channel_name)
{
	auto & io_service = mybot.get_io_service();

	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			joke(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 0)),
				channel_name,
				boost::posix_time::seconds(600)
			)
		)
	);

	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			urlpreview(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
			)
		)
	);
#ifdef ENABLE_LUA
	channel.handle_extra_message.connect(
		make_luascript(
			channel_name,
			io_service,
			io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
		)
	);
#endif
	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			::bulletin(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1)),
				channel_name
			)
		)
	);
	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			make_metalprice(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
			)
		)
	);
	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			make_stockprice(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
			)
		)
	);
	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			::exchangerate(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
			)
		)
	);

	static std::shared_ptr<iplocationdetail::ipdb_mgr> ipdb_mgr;

	if (!ipdb_mgr)
	{
		// check for file "qqwry.dat"
		// if not exist, then download that file
		// after download that file, construct ipdb
		ipdb_mgr.reset(new  iplocationdetail::ipdb_mgr(mybot.get_io_service(), uncompress));
		ipdb_mgr->search_and_build_db();
	}

	channel.handle_extra_message.connect(
		avbot_extension(
			channel_name,
			make_iplocation(
				io_service,
				io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1)),
				ipdb_mgr
			)
		)
	);

	channel.handle_extra_message.connect(
		make_static_content(io_service, channel_name)
	);

#ifdef ENABLE_PYTHON
	channel.handle_extra_message.connect(
		make_python_script_engine(
			io_service,
			channel_name,
			io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
		)
	);
#endif

#ifdef _WIN32
	channel.handle_extra_message.connect(
		make_dllextention(
			io_service,
			channel_name,
			io_service.wrap(std::bind(sender, std::ref(mybot), channel_name, std::placeholders::_1, 1))
		)
	);
#endif
}
Esempio n. 4
0
// 命令控制, 所有的协议都能享受的命令控制在这里实现.
// 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;
	}
}