| 1 | package Webqq::Client::Plugin::IrcSync; | ||
| 2 | use AnyEvent::IRC::Client; | ||
| 3 | use List::Util qw(first); | ||
| 4 | my $irc = new AnyEvent::IRC::Client; | ||
| 5 | my $once = 1; | ||
| 6 | my $join = 0; | ||
| 7 | sub call { | ||
| 8 | my $client = shift; | ||
| 9 | my $msg = shift; | ||
| 10 | my %p = @_; | ||
| 11 | |||
| 12 | my $channel = $p{channel} || "#ChinaPerl"; | ||
| 13 | return 1 if $msg->{type} ne "group_message"; | ||
| 14 | my $debug = $client->{debug}; | ||
| 15 | |||
| 16 | if ($once) { | ||
| 17 | my $server = $p{server} || "irc.freenode.net"; | ||
| 18 | my $port = $p{port} || 6667; | ||
| 19 | my $nick = $p{nick} or die "[Webqq::Client::Plugin::IrcSync] nick must be set\n"; | ||
| 20 | my $user = $p{user}; | ||
| 21 | my $real = $p{real}; | ||
| 22 | my $password = $p{password}; | ||
| 23 | $irc->reg_cb( | ||
| 24 | registered => sub { print "[Webqq::Client::Plugin::IrcSync] $nick has registered $server:$port\n" if $debug;}, | ||
| 25 | join => sub { | ||
| 26 | print "[Webqq::Client::Plugin::IRC] $nick has joined $channel\n" if $debug; | ||
| 27 | $join = 1; | ||
| 28 | }, | ||
| 29 | publicmsg => sub { | ||
| 30 | my($self,$channel, $ircmsg) = @_; | ||
| 31 | my $sender_nick = substr($ircmsg->{prefix},0,index($ircmsg->{prefix},"!~")) || "UnknownNick"; | ||
| 32 | my $msg_content = $ircmsg->{params}[1]; | ||
| 33 | return if $ircmsg->{command} ne "PRIVMSG"; | ||
| 34 | return if $msg_content =~/^[~ ]/; | ||
| 35 | #if($client->{debug}){ | ||
| 36 | # print "[Webqq::Client::Plugin::IrcSync] \@$sender_nick (in $channel) say: $msg_content\n"; | ||
| 37 | #} | ||
| 38 | my $group = first {$_->{name} eq $p{group_name}} @{$client->{qq_database}{group_list}} or return ; | ||
| 39 | $client->send_group_message( | ||
| 40 | to_uin => $group->{gid}, | ||
| 41 | content => "[${sender_nick}#irc] " . $msg_content | ||
| 42 | ); | ||
| 43 | }, | ||
| 44 | disconnect => sub { print "[Webqq::Client::Plugin::IrcSync] $nick has quit $server:$port\n" if $debug;}, | ||
| 45 | ); | ||
| 46 | $irc->send_srv(JOIN => $channel); | ||
| 47 | $irc->connect($server,$port,{nick=>$nick,user=>$user,real=>$real,password=>$password}); | ||
| 48 | $once = 0; | ||
| 49 | } | ||
| 50 | return 1 unless $join; | ||
| 51 | return 1 if ($msg->{msg_class} eq "send" and $msg->{content}=~/^\[.*#irc\]/); | ||
| 52 | my $group_name = $msg->group_name; | ||
| 53 | return 1 if $p{group_name} ne $group_name; | ||
| 54 | my $msg_sender_nick = $msg->from_nick; | ||
| 55 | my $msg_sender_card = $msg->from_card if $msg->{msg_class} eq 'recv'; | ||
| 56 | my $msg_sender = $msg_sender_card || $msg_sender_nick; | ||
| 57 | $msg_sender = "昵称未知" unless defined $msg_sender; | ||
| 58 | $msg_sender = $client->{qq_database}{user}{nick} if $msg_sender eq "我"; | ||
| 59 | for(split /\n/,$msg->{content}){ | ||
| 60 | $irc->send_msg(PRIVMSG => $channel, "[\@$msg_sender] ". $_); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | 1; |