File Coverage

blib/lib/Net/DirectConnect/nmdc.pm
Criterion Covered Total %
statement 27 92 29.3
branch 0 26 0.0
condition 0 37 0.0
subroutine 9 18 50.0
pod 0 1 0.0
total 36 174 20.6


line stmt bran cond sub pod time code
1             #$Id: adc.pm 594 2010-01-30 23:10:17Z pro $ $URL: svn://svn.setun.net/dcppp/trunk/lib/Net/DirectConnect/adc.pm $
2             #UNFINISHED
3             package #hide from cpan
4             Net::DirectConnect::nmdc;
5 1     1   1131 use strict;
  1         2  
  1         34  
6 1     1   4 no strict qw(refs);
  1         2  
  1         34  
7 1     1   6 use warnings "NONFATAL" => "all";
  1         1  
  1         47  
8 1     1   4 no warnings qw(uninitialized);
  1         1  
  1         74  
9 1     1   5 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  1         2  
  1         9  
10 1     1   46 use utf8;
  1         1  
  1         8  
11             #use Time::HiRes qw(time sleep);
12 1     1   20 use Data::Dumper; #dev only
  1         2  
  1         73  
13             $Data::Dumper::Sortkeys = $Data::Dumper::Useqq = $Data::Dumper::Indent = 1;
14 1     1   5 use Net::DirectConnect;
  1         2  
  1         51  
15             #use Net::DirectConnect::clicli;
16             #use Net::DirectConnect::http;
17             #use Net::DirectConnect::httpcli;
18             our $VERSION = ( split( ' ', '$Revision: 594 $' ) )[1];
19 1     1   5 use base 'Net::DirectConnect';
  1         2  
  1         1368  
20              
21             sub init {
22 0     0 0   my $self = shift;
23             #shift if $_[0] eq __PACKAGE__;
24             #print "nmdcinit SELF=", $self, "REF=", ref $self, " P=", @_, "package=", __PACKAGE__, "\n\n";
25             #$self->SUPER::new();
26             #%$self = (
27             #%$self,
28 0           local %_ = (
29             'Nick' => 'NetDCBot',
30             'port' => 411,
31             'host' => 'localhost',
32             'protocol' => 'nmdc',
33             'nmdc' => 1,
34             #'Pass' => '',
35             #'key' => 'zzz',
36             #'auto_wait' => 1,
37             #'search_every' => 10, 'search_every_min' => 10, 'auto_connect' => 1,
38             #ADC
39             #'connect_protocol' => 'ADC/0.10', 'message_type' => 'H',
40             #@_,
41             'incomingclass' => 'Net::DirectConnect::clicli',
42             #'incomingclass' => __PACKAGE__, #'Net::DirectConnect::adc',
43             #no_print => { 'INF' => 1, 'QUI' => 1, 'SCH' => 1, },
44             #http://www.dcpp.net/wiki/index.php/%24MyINFO
45             'description' => 'perl ' . __PACKAGE__ . ' bot',
46             #====MOVE TO NMDC
47             'connection' => 'LAN(T3)',
48             #NMDC1: 28.8Kbps, 33.6Kbps, 56Kbps, Satellite, ISDN, DSL, Cable, LAN(T1), LAN(T3)
49             #NMDC2: Modem, DSL, Cable, Satellite, LAN(T1), LAN(T3)
50             'flag' => '1', # User status as ascii char (byte)
51             #1 normal
52             #2, 3 away
53             #4, 5 server The server icon is used when the client has
54             #6, 7 server away uptime > 2 hours, > 2 GB shared, upload > 200 MB.
55             #8, 9 fireball The fireball icon is used when the client
56             #10, 11 fireball away has had an upload > 100 kB/s.
57             'email' => 'billgates@microsoft.com', 'sharesize' => 10 * 1024 * 1024 * 1024 + int rand( 1024 * 1024 ), #10GB
58             'client' => 'perl', #'dcp++', #++: indicates the client
59             #'protocol' => 'nmdc', # or 'adc'
60             'V' => $Net::DirectConnect::VERSION, #. '_' . ( split( ' ', '$Revision: 656 $' ) )[1], #V: tells you the version number
61             #'M' => 'A', #M: tells if the user is in active (A), passive (P), or SOCKS5 (5) mode
62             'H' => '0/1/0'
63             , #H: tells how many hubs the user is on and what is his status on the hubs. The first number means a normal user, second means VIP/registered hubs and the last one operator hubs (separated by the forward slash ['/']).
64             'S' => '3', #S: tells the number of slots user has opened
65             'O' => undef, #O: shows the value of the "Automatically open slot if speed is below xx KiB/s" setting, if non-zero
66             'lock' => 'EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.668ABCABC',
67             'cmd_bef' => '$',
68             'cmd_aft' => '|',
69             'auto_say_cmd' => [qw(welcome chatline To)],
70             );
71             #$self->{$_} ||= $_{$_} for keys %_;
72             #$self->log('dev', 's0',$self->{'sharesize'});
73 0 0 0       !exists $self->{$_} ? $self->{$_} ||= $_{$_} : () for keys %_;
74             #$self->log('dev', 's1',$self->{'sharesize'});
75 0           %_ = (
76             #charset_chat => 'cp1251',
77             #charset_nick => 'cp1251',
78             charset_protocol => 'cp1251',
79             );
80 0           $self->{$_} = $_{$_} for keys %_;
81             #$self->log('dev', 'chPROTO:',$self->{'charset_protocol'});
82             #print 'adc init now=',Dumper $self;
83             #$self->{'periodic'}{ __FILE__ . __LINE__ } = sub { my $self = shift if ref $_[0]; $self->cmd( 'search_buffer', ) if $self->{'socket'}; };
84             #http://www.dcpp.net/wiki/index.php/LockToKey :
85             $self->{'lock2key'} ||= sub {
86 0 0   0     my $self = shift if ref $_[0];
87             #return $self->{lock};
88 0           my ($lock) = @_;
89             #$self->{'log'}->( 'dev', 'making lock from', $lock );
90 0 0         $lock = Encode::encode $self->{charset_protocol}, $lock, Encode::FB_WARN if $self->{charset_protocol};
91             #$self->{'log'}->( 'dev', 'making lock from2:', $lock );
92 0           my @lock = split( //, $lock );
93 0           my $i;
94 0           my @key = ();
95 0           foreach (@lock) { $_ = ord; }
  0            
96 0           push( @key, $lock[0] ^ 5 );
97 0           for ( $i = 1 ; $i < @lock ; $i++ ) { push( @key, ( $lock[$i] ^ $lock[ $i - 1 ] ) ); }
  0            
98 0           for ( $i = 0 ; $i < @key ; $i++ ) { $key[$i] = ( ( ( $key[$i] << 4 ) & 240 ) | ( ( $key[$i] >> 4 ) & 15 ) ) & 0xff; }
  0            
99 0           $key[0] = $key[0] ^ $key[ @key - 1 ];
100              
101 0           foreach (@key) {
102 0 0 0       if ( $_ == 0 || $_ == 5 || $_ == 36 || $_ == 96 || $_ == 124 || $_ == 126 ) { $_ = sprintf( '/%%DCN%03i%%/', $_ ); }
  0   0        
      0        
      0        
      0        
103 0           else { $_ = chr; }
104             }
105 0           local $_ = join( '', @key );
106 0 0         $_ = Encode::decode $self->{charset_protocol}, $_ if $self->{charset_protocol};
107 0           return $_;
108 0   0       };
109             $self->{'tag'} ||= sub {
110 0     0     my $self = shift;
111 0           $self->{'client'} . ' ' . join( ',', map $_ . ':' . $self->{$_}, grep defined( $self->{$_} ), qw(V M H S O) );
112 0   0       };
113             $self->{'myinfo'} ||= sub {
114 0     0     my $self = shift;
115             return
116 0 0         $self->{'Nick'} . ' '
117             . $self->{'description'} . '<'
118             . $self->tag() . '>' . '$' . ' ' . '$'
119             . $self->{'connection'}
120             . ( length( $self->{'flag'} ) ? chr( $self->{'flag'} ) : '' ) . '$'
121             . $self->{'email'} . '$'
122             . $self->{'sharesize'} . '$';
123 0   0       };
124             $self->{'supports'} ||= sub {
125 0     0     my $self = shift;
126 0           return join ' ', grep $self->{$_}, @{ $self->{'supports_avail'} };
  0            
127 0   0       };
128             $self->{'supports_parse'} ||= sub {
129 0     0     my $self = shift;
130 0           my ( $str, $save ) = @_;
131 0           $save->{$_} = 1 for split /\s+/, $str;
132 0           delete $save->{$_} for grep !length $save->{$_}, keys %$save;
133 0 0         return wantarray ? %$save : $save;
134 0   0       };
135             $self->{'info_parse'} ||= sub {
136 0     0     my $self = shift;
137 0           my ( $info, $save ) = @_;
138 0           $save->{'info'} = $info;
139 0 0         $save->{'description'} = $1 if $info =~ s/^([^<\$]+)(<|\$)/$2/;
140 0           ( $save->{'tag'}, $save->{'M'}, $save->{'connection'}, $save->{'email'}, $save->{'sharesize'} ) = split /\s*\$\s*/, $info;
141 0 0         $save->{'flag'} = ord($1) if $save->{'connection'} =~ s/([\x00-\x1F])$//e;
142 0           $self->tag_parse( $save->{'tag'}, $save );
143 0           delete $save->{$_} for grep !length $save->{$_}, keys %$save;
144 0 0         return wantarray ? %$save : $save;
145 0   0       };
146             $self->{'tag_parse'} ||= sub {
147 0     0     my $self = shift;
148 0           my ( $tag, $save ) = @_;
149 0           $save->{'tag'} = $tag;
150 0           $tag =~ s/(^\s*<\s*)|(\s*>\s*$)//g;
151 0 0         $save->{'client'} = $1 if $tag =~ s/^(\S+)\s*//;
152 0           /(.+):(.+)/, $save->{$1} = $2 for split /,/, $tag;
153 0 0         return wantarray ? %$save : $save;
154 0   0       };
155             $self->{'make_hub'} ||= sub {
156 0 0   0     my $self = shift if ref $_[0];
157 0   0       $self->{'hub_name'} ||=
158             $self->{'host'}; # . ( ( $self->{'port'} and $self->{'port'} != 411 ) ? ':' . $self->{'port'} : '' );
159 0           $self->{'hub_name'} =~ s/:411$//;
160             #$self->log('dev', $self->{'hub_name'});
161 0   0       },;
162             }
163             1;