<commit_before>package Mojo::Webqq::Plugin::MsgSync;
$Mojo::Webqq::Plugin::MsgSync::PRIORITY = 99;
use strict;
use Encode;
use List::Util qw(first);
$Mojo::Webqq::Plugin::MsgSync::is_hold_mojo_irc;
BEGIN{
    local $SIG{__WARN__}=sub{};
    eval{require Mojo::IRC;};
    $Mojo::Webqq::Plugin::MsgSync::is_hold_mojo_irc = 1 unless $@;
}
my @pairs;
my %group_status;
my %irc_channel_status;
my $irc = undef;
sub call{
    my $client = shift;
    my $data = shift;
    $client->die(__PACKAGE__ . "依赖Mojo::IRC模块，请先安装该模块") if !$Mojo::Webqq::Plugin::MsgSync::is_hold_mojo_irc;
    return if ref $data ne "HASH";
    return if ref $data->{pairs} ne "ARRAY";
    if(exists $data->{irc}){
        $irc = {};
        $irc->{nick} = $data->{irc}->{nick};
        $irc->{user} = $data->{irc}->{user};
        $irc->{pass} = $data->{irc}->{pass};
        $irc->{server} = "irc.freenode.net" unless defined $data->{irc}->{server};
        $irc->{port}   = 6667 unless defined $data->{irc}->{port};
    }
    for my $pair (@{ $data->{pairs} }){
        my @p;
        for(@{$pair}){
            if(ref $_ eq "Mojo::Webqq::Group"){
                push @p,{type=>"group",name=>$_->gname} ;
                $group_status{$_->gname}=1;
            }
            else {push @p,{type=>"channel",name=>$_}; $irc_channel_status{$_}=0; }
        }
        push @pairs,\@p;
    }
    
    if(defined $irc){
        $irc->{client} = Mojo::IRC->new(
            nick=>$irc->{nick},
            user=>$irc->{user},
            pass=>$irc->{pass},
            server=>"$irc->{server}:$irc->{port}",
        ); 

        $irc->{client}->on(irc_join =>sub{
            my(undef, $m) = @_;
            my ($command,$nick,$channel,$content) = 
                ($m->{command},substr($m->{prefix},0,index($m->{prefix},"!~")),$m->{params}[0],$m->{params}[1]);
            for(keys %irc_channel_status){
                if(lc($_) eq lc($channel)){
                    $irc_channel_status{$_} = 1;
                    last;
                }
            }

            $client->debug("$nick 已加入频道 $channel|$irc->{server}:$irc->{port}") if $command eq "JOIN";
        });
        $irc->{client}->on(irc_privmsg=>sub{
            my(undef, $m) = @_; 
            my ($command,$nick,$channel,$content) =
                ($m->{command},substr($m->{prefix},0,index($m->{prefix},"!~")),$m->{params}[0],$m->{params}[1]);
            for my $pair (@pairs){
                next unless first {$_->{type} eq "channel" and lc($_->{name}) eq lc($channel) } @$pair;
                for(grep { $_->{type} eq "group"} @$pair){ 
                    my $g = $client->search_group(gname=>$_->{name});
                    next unless defined $g;
                    $client->send_group_message($g,"$nick: " . encode("utf8",$content),sub{        
                    #$client->send_group_message($g,encode("utf8",$content) . " (来自 $nick)",sub{        
                        my $msg = $_[1];
                        $msg->cb(sub{
                            my($client,$msg,$status)=@_;
                            return if $status->is_success;
                            $irc->{client}->write(PRIVMSG => $channel,":$nick: " . $content . decode("utf8","[QQ群同步失败]"));
                        });
                        $msg->msg_from("irc");
                    });
                }
            }
        });
        $irc->{client}->on(close=>sub{
            $irc->{is_connect} = 0;
            for(keys %irc_channel_status){$irc_channel_status{$_}=0}
            $client->debug("irc[ $irc->{nick}|$irc->{server}:$irc->{port} ]已断开连接，尝试重新连接");
            $irc->{client}->connect(sub{
                if($_[1]){
                    $client->error("irc[ $irc->{nick}|$irc->{server}:$irc->{port} ]连接失败: $_[1]");
                    return;
                }
                $irc->{is_connect} = 1;
                $client->debug("$irc->{nick} 已连接 $irc->{server}:$irc->{port}");
                for my $channel( keys %irc_channel_status){
                    $_[0]->write(join => $channel,sub{<commit_msg>完善MsgSync插件<commit_after>package Mojo::Webqq::Plugin::MsgSync;
$Mojo::Webqq::Plugin::MsgSync::PRIORITY = 99;
use strict;
use Encode;
use List::Util qw(first);
$Mojo::Webqq::Plugin::MsgSync::is_hold_mojo_irc;
BEGIN{
    local $SIG{__WARN__}=sub{};
    eval{require Mojo::IRC;};
    $Mojo::Webqq::Plugin::MsgSync::is_hold_mojo_irc = 1 unless $@;
}
my @pairs;
my %group_status;
my %irc_channel_status;
my $irc = undef;
sub call{
    my $client = shift;
    my $data = shift;
    $client->die(__PACKAGE__ . "依赖Mojo::IRC模块，请先安装该模块") if !$Mojo::Webqq::Plugin::MsgSync::is_hold_mojo_irc;
    return if ref $data ne "HASH";
    return if ref $data->{pairs} ne "ARRAY";
    if(exists $data->{irc}){
        $irc = {};
        $irc->{nick} = $data->{irc}->{nick};
        $irc->{user} = $data->{irc}->{user};
        $irc->{pass} = $data->{irc}->{pass};
        $irc->{server} = "irc.freenode.net" unless defined $data->{irc}->{server};
        $irc->{port}   = 6667 unless defined $data->{irc}->{port};
    }
    for my $pair (@{ $data->{pairs} }){
        my @p;
        for(@{$pair}){
            if(ref $_ eq "Mojo::Webqq::Group"){
                push @p,{type=>"group",name=>$_->gname} ;
                $group_status{$_->gname}=1;
            }
            else {push @p,{type=>"channel",name=>$_}; $irc_channel_status{$_}=0; }
        }
        push @pairs,\@p;
    }
    
    if(defined $irc){
        $irc->{client} = Mojo::IRC->new(
            nick=>$irc->{nick},
            user=>$irc->{user},
            pass=>$irc->{pass},
            server=>"$irc->{server}:$irc->{port}",
        ); 

        $irc->{client}->on(irc_join =>sub{
            my(undef, $m) = @_;
            my ($command,$nick,$channel,$content) = 
                ($m->{command},substr($m->{prefix},0,index($m->{prefix},"!~")),$m->{params}[0],$m->{params}[1]);
            for(keys %irc_channel_status){
                if(lc($_) eq lc($channel)){
                    $irc_channel_status{$_} = 1;
                    last;
                }
            }

            $client->debug("$nick 已加入频道 $channel|$irc->{server}:$irc->{port}") if $command eq "JOIN";
        });
        $irc->{client}->on(irc_privmsg=>sub{
            my(undef, $m) = @_; 
            my ($command,$nick,$channel,$content) =
                ($m->{command},substr($m->{prefix},0,index($m->{prefix},"!~")),$m->{params}[0],$m->{params}[1]);

            $content =~ s/^([^:]+): /\@$1 /;
            for my $pair (@pairs){
                next unless first {$_->{type} eq "channel" and lc($_->{name}) eq lc($channel) } @$pair;
                for(grep { $_->{type} eq "group"} @$pair){ 
                    my $g = $client->search_group(gname=>$_->{name});
                    next unless defined $g;
                    $client->send_group_message($g,"$nick: " . encode("utf8",$content),sub{        
                    #$client->send_group_message($g,encode("utf8",$content) . " (来自 $nick)",sub{        
                        my $msg = $_[1];
                        $msg->cb(sub{
                            my($client,$msg,$status)=@_;
                            return if $status->is_success;
                            $irc->{client}->write(PRIVMSG => $channel,":$nick: " . $content . decode("utf8","[QQ群同步失败]"));
                        });
                        $msg->msg_from("irc");
                    });
                }
            }
        });
        $irc->{client}->on(close=>sub{
            $irc->{is_connect} = 0;
            for(keys %irc_channel_status){$irc_channel_status{$_}=0}
            $client->debug("irc[ $irc->{nick}|$irc->{server}:$irc->{port} ]已断开连接，尝试重新连接");
            $irc->{client}->connect(sub{
                if($_[1]){
                    $client->error("irc[ $irc->{nick}|$irc->{server}:$irc->{port} ]连接失败: $_[1]");
                    return;
                }
                $irc->{is_connect} = 1;
                $client->debug("$irc->{nick} 已连接 $irc->{server}:$irc->{port}");
                for my $channel( keys %irc_channel_status){
                    $_[0]->write(join => $channel,sub{
{'fraction_non_alphanumeric': 0.2175141242937853, 'fraction_numerical': 0.00650945713583886, 'mean_word_length': 1.8845200141693235, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1188625', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2792, 'n_words': 400}