Skip to content

Instantly share code, notes, and snippets.

@wxg4net
Created December 23, 2014 07:35
Show Gist options
  • Save wxg4net/b143fc4e4228c6a7a8e9 to your computer and use it in GitHub Desktop.
Save wxg4net/b143fc4e4228c6a7a8e9 to your computer and use it in GitHub Desktop.
weechat webqq support test
#!/usr/bin/perl
use lib '/home/wxg/.weechat/perl/';
use Exporter 'import';
use Webqq::Client;
use Digest::MD5 qw(md5_hex);
use Encode;
use Encode::Locale;
use IO::Socket::UNIX;
use threads;
use threads::shared;
my $qq = 888888888;
my $pwd = md5_hex('888888888');
my $socket_path = '/tmp/webqq.sock';
unlink $socket_path if -e $socket_path;
our $socket = IO::Socket::UNIX->new(
Local => $socket_path,
Type => SOCK_STREAM,
Listen => 1,
);
our $client_socket = $socket->accept();
my $client = Webqq::Client->new(debug=>0);
my $thread = threads->new(\&thread,$client_socket,$client);
$thread->detach;
sub thread
{
$client_socket = shift;
$client = shift;
while(<$client_socket>)
{
my $line = $_;
chomp($line); # Remove newline
my @data = split(" ",$line);
$client_socket->send('暂时不支持回复消息,等待以下程序修正');
# $client->send_message(
# $client->create_msg(to_uin=>$data[0],content=>$data[1])
# );
}
}
$client->login( qq=> $qq, pwd => $pwd);
$client->load("Weechat");
#设置全局默认的发送消息后的回调函数,主要用于判断消息是否成功发送
$client->on_send_message = sub{
my ($msg,$is_success,$status) = @_;
#使用ShowMsg插件打印发送的消息
$client->call("Weechat",$msg);
};
my $autoReply = '暂时程序接管此QQ, 请访问 http://wxg.perhome.cn 或者使用 Skype 加 wxg4net 联系本人';
#设置接收到消息后的回调函数
$client->on_receive_message = sub{
#传递给回调的参数是一个包含接收到的消息的hash引用
#$msg = {
# type => message|group_message 消息类型
# msg_id => 系统生成的消息id
# from_uin => 消息发送者uin,回复消息时需要用到
# to_uin => 消息接受者uin,就是自己的qq
# content => 消息内容,采用UTF8编码
# msg_time => 消息的接收时间
# ttl
# msg_class
# allow_plugin
#}
my $msg = shift;
#使用ShowMsg插件打印接收到的消息
$client->call("Weechat",$msg);
#新的方式
$client->reply_message($msg, $autoReply);
};
$client->run;
#!/usr/bin/perl
use IO::Socket::UNIX;
my $socket_path = '/tmp/webqq.sock';
# auto-flush on socket
$| = 1;
system("perl /home/wxg/.weechat/perl/webqq.pl &");
sleep(1);
my $socket = IO::Socket::UNIX->new(
Peer => $socket_path,
Type => SOCK_STREAM,
);
sub buffer_input_cb()
{
my $data = shift,
my $buffer = shift,
my $input_data = shift,
$socket->say($input_data);
weechat::print($buffer, $input_data);
}
sub webqq_fd_cb {
my $p = shift;
my $fd = shift;
my $response = "";
$socket->recv($response, 1024);
if (length($response) == 0) {
weechat::print($buffer, 'QQ已退出');
weechat::unhook_all()
}
weechat::print($buffer, $response);
return weechat::WEECHAT_RC_OK
};
weechat::register('qq', "4dev", '0.1', "GPL3", "qq message with list of buffers", "", "");
our $buffer = weechat::buffer_new("QQ", "buffer_input_cb", "", "", "");
weechat::buffer_set($buffer, "title", "QQ buffer");
weechat::buffer_set($buffer, "localvar_set_no_log", "1");
my $fileno = $socket->fileno();
weechat::hook_fd($fileno, 1, 0, 0, "webqq_fd_cb", "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment