| 1 | package Webqq::Client::Plugin::MsgSync; | ||
| 2 | use strict; | ||
| 3 | use AnyEvent::IRC::Client; | ||
| 4 | use AnyEvent::IRC::Util qw(prefix_nick); | ||
| 5 | use List::Util qw(first); | ||
| 6 | use Webqq::Client::Util qw(truncate); | ||
| 7 | my $irc_client = new AnyEvent::IRC::Client; | ||
| 8 | my $once = 1; | ||
| 9 | my $is_irc_join = 0; | ||
| 10 | my $debug = 0; | ||
| 11 | |||
| 12 | my $irc = {}; | ||
| 13 | my $group_name = []; | ||
| 14 | |||
| 15 | my @group_list ; | ||
| 16 | sub call { | ||
| 17 | my $client = shift; | ||
| 18 | my $msg = shift; | ||
| 19 | $debug = $client->{debug}; | ||
| 20 | my %p = @_; | ||
| 21 | $group_name = $p{group}; | ||
| 22 | if($group_name eq "all"){ | ||
| 23 | @group_list = @{$client->{qq_database}{group_list}}; | ||
| 24 | } | ||
| 25 | elsif(ref $group_name eq "ARRAY"){ | ||
| 26 | @group_list = (); | ||
| 27 | for my $g (@{$client->{qq_database}{group_list}}){ | ||
| 28 | push @group_list,$g if first {$g->{name} eq $_} @$group_name; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | if ($once) { | ||
| 33 | $irc = $p{irc}; | ||
| 34 | if(defined $irc){ | ||
| 35 | $irc->{server} = "irc.freenode.net" unless defined $irc->{server}; | ||
| 36 | $irc->{port} = 6667 unless defined $irc->{port}; | ||
| 37 | $irc->{channel} = "#ChinaPerl" unless defined $irc->{channel}; | ||
| 38 | die "[".__PACKAGE__."] irc nick must be set\n" unless defined $irc->{nick}; | ||
| 39 | $irc_client->reg_cb( | ||
| 40 | registered => sub { | ||
| 41 | print "[".__PACKAGE__."] $irc->{nick} has registered $irc->{server}:$irc->{port}\n" if $debug; | ||
| 42 | $irc_client->send_msg(JOIN=>$irc->{channel}); | ||
| 43 | }, | ||
| 44 | join => sub { | ||
| 45 | print "[".__PACKAGE__."] $irc->{nick} has joined $irc->{channel}\n" if $debug; | ||
| 46 | $is_irc_join = 1; | ||
| 47 | }, | ||
| 48 | publicmsg => sub { | ||
| 49 | my($self,$channel, $ircmsg) = @_; | ||
| 50 | my $sender_nick = prefix_nick($ircmsg) || "UnknownNick"; | ||
| 51 | my $msg_content = $ircmsg->{params}[1]; | ||
| 52 | return if $ircmsg->{command} ne "PRIVMSG"; | ||
| 53 | return if $msg_content =~/^[~ ]/; | ||
| 54 | #if($debug){ | ||
| 55 | # print "[".__PACKAGE__."] \@$sender_nick (in $channel) say: $msg_content\n"; | ||
| 56 | #} | ||
| 57 | for(@group_list){ | ||
| 58 | $client->send_group_message( | ||
| 59 | to_uin => $_->{gid}, | ||
| 60 | content => "[${sender_nick}#irc] " . $msg_content | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | }, | ||
| 64 | disconnect => sub { | ||
| 65 | print "[".__PACKAGE__."] $irc->{nick} has quit $irc->{server}:$irc->{port}\n" if $debug; | ||
| 66 | $irc_client->connect( | ||
| 67 | $irc->{server}, | ||
| 68 | $irc->{port}, | ||
| 69 | {nick=>$irc->{nick},user=>$irc->{user},real=>$irc->{real},password=>$irc->{password}}, | ||
| 70 | ); | ||
| 71 | }, | ||
| 72 | ); | ||
| 73 | $irc_client->connect( | ||
| 74 | $irc->{server}, | ||
| 75 | $irc->{port}, | ||
| 76 | {nick=>$irc->{nick},user=>$irc->{user},real=>$irc->{real},password=>$irc->{password}}, | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | $client->{watchers}{rand()} = AE::timer 600,60,sub { | ||
| 80 | if($irc_client->registered()){ | ||
| 81 | unless(defined $irc_client->channel_list($irc->{channel})){ | ||
| 82 | $is_irc_join = 0; | ||
| 83 | $irc_client->send_msg(JOIN=>$irc->{channel}) ; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | else{ | ||
| 87 | $is_irc_join = 0; | ||
| 88 | $irc_client->connect( | ||
| 89 | $irc->{server}, | ||
| 90 | $irc->{port}, | ||
| 91 | {nick=>$irc->{nick},user=>$irc->{user},real=>$irc->{real},password=>$irc->{password}}, | ||
| 92 | ) | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | $once = 0; | ||
| 96 | } | ||
| 97 | return 1 if ($msg->{msg_class} eq "send" and $msg->{content}=~/^\[.*?#.+?\]/); | ||
| 98 | return 1 if $msg->{type} ne 'group_message'; | ||
| 99 | my $gn = $msg->group_name; | ||
| 100 | return 1 unless first {$gn eq $_} @$group_name; | ||
| 101 | my $msg_sender_nick = $msg->from_nick; | ||
| 102 | my $msg_sender_card = $msg->from_card if $msg->{msg_class} eq 'recv'; | ||
| 103 | my $msg_sender = $msg_sender_card || $msg_sender_nick; | ||
| 104 | $msg_sender = "昵称未知" unless defined $msg_sender; | ||
| 105 | $msg_sender = $client->{qq_database}{user}{nick} if $msg_sender eq "我" and $msg->{msg_class} eq 'send'; | ||
| 106 | |||
| 107 | |||
| 108 | for(grep {$gn ne $_->{name}} @group_list){ | ||
| 109 | $client->send_group_message( | ||
| 110 | to_uin => $_->{gid}, | ||
| 111 | content => "[${msg_sender}#$gn] " . $msg->{content} | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | if($is_irc_join){ | ||
| 116 | for(split /\n/,truncate($msg->{content},max_bytes=>2000,max_lines=>10) ){ | ||
| 117 | $irc_client->send_msg(PRIVMSG => $irc->{channel}, "[$msg_sender] ". $_); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | 1; |