diff --git a/II/DB.pm b/II/DB.pm index caeea1d..b060d42 100644 --- a/II/DB.pm +++ b/II/DB.pm @@ -20,6 +20,25 @@ sub new { return $self; } +sub check_hash { + my ( $self, $hash, $echo ) = @_; + my $dbh = $self->{_dbh}; + + my $q = "select hash from echo where hash='$hash' and echo='$echo'"; + my $sth = $dbh->prepare($q); + $sth->execute(); + + while ( my @h = $sth->fetchrow_array() ) { + my ($base_hash) = @h; + if ($hash eq $base_hash) { + return 1; + } + else { + return 0; + } + } +} + sub begin { my ($self) = @_; my $dbh = $self->{_dbh}; @@ -36,6 +55,18 @@ sub commit { $dbh->do('COMMIT'); } +sub write_echo { + my ( $self, %data ) = @_; + my $dbh = $self->{_dbh}; + my $sql = $self->{_sql}; + + my ( $stmt, @bind ) = $sql->insert( 'echo', \%data ); + + my $sth = $dbh->prepare($stmt); + $sth->execute(@bind); + $sth->finish(); +} + sub write_out { my ( $self, %data ) = @_; my $dbh = $self->{_dbh}; @@ -50,10 +81,10 @@ sub write_out { } sub update_out { - my ($self, $hash) = @_; + my ( $self, $hash ) = @_; my $dbh = $self->{_dbh}; - my $q = "update output set send=1 where hash='$hash'"; + my $q = "update output set send=1 where hash='$hash'"; my $sth = $dbh->prepare($q); $sth->execute(); } diff --git a/II/Enc.pm b/II/Enc.pm index 77e2065..6622713 100644 --- a/II/Enc.pm +++ b/II/Enc.pm @@ -19,19 +19,10 @@ sub new { } sub decrypt { - my ( $self, $file ) = @_; - - open my $fh, "<", $file or die "Cannot open file $file: $!\n"; - my $message; - while (<$fh>) { - $message .= $_; - } - close $fh; - - my @enc = split /:/, $message; + my ( $self, $base64 ) = @_; # Decrypt message - my $dec = `echo "$enc[1]" | base64 -d`; + my $dec = `echo "$base64" | base64 -d`; return $dec; } diff --git a/II/Get.pm b/II/Get.pm index 1dd3d78..4a8bbc3 100644 --- a/II/Get.pm +++ b/II/Get.pm @@ -1,5 +1,6 @@ package II::Get; -use LWP::Simple; +use LWP::UserAgent; +use HTTP::Request; use II::DB; use II::Enc; @@ -9,7 +10,14 @@ use Data::Dumper; sub new { my $class = shift; - my $self = { _config => shift, }; + my $ua = LWP::UserAgent->new(); + $ua->agent("iiplc/0.1rc1"); + my $db = II::DB->new(); + my $self = { + _config => shift, + _ua => $ua, + _db => $db, + }; bless $self, $class; return $self; @@ -20,57 +28,95 @@ sub get_echo { my $config = $self->{_config}; my $echoareas = $config->{echoareas}; my $host = $config->{host}; - - my $db = II::DB->new(); + my $ua = $self->{_ua}; + my $db = $self->{_db}; my $echo_url = 'u/e/'; my $msg_url = 'u/m/'; my $msgs; + my $base64; foreach my $echo (@$echoareas) { - # my @content = get( "$host" . "$echo_url" . "$echo" ); - my @content = `curl $host$echo_url$echo`; - # if ( is_success( getprint( "$host" . "$echo_url" . "$echo" ) ) ) { + # Get echo message hashes + my $req_echo = HTTP::Request->new( GET => "$host$echo_url$echo" ); + my $res_echo = $ua->request($req_echo); - # Write echoes file - open my $echo_fh, ">", "./echo/$echo" - or die "Cannot open file: $!\n"; - print $echo_fh @content; - close $echo_fh; + my @new; + $db->begin(); + if ( $res_echo->is_success ) { + my @mes = split /\n/, $res_echo->content(); + while (<@mes>) { + if ( $_ =~ /.{20}/ ) { + if ( $db->check_hash( $_, $echo ) == 0 ) { + my $echo_hash = { + echo => $echo, + hash => $_, + }; + my %e_write = ( + echo => $echo, + hash => $_, + ); - # Get messages - open my $echo_fh, "<", "./echo/$echo" - or die "Cannot open file: $!\n"; - while (<$echo_fh>) { - chomp($_); - if ($_ =~ /.{20}/) { - if ( !( -e "./msg/$_" ) ) { - $msgs .= $_ . "\n"; - # @w_cmd = ( 'wget', '-O', - # "./msg/$_", "$host" . "$msg_url" . "$_" ); - `curl $host$msg_url$_ > ./msg/$_`; - # system(@w_cmd) == 0 or die "Cannot download file: $!\n"; + # Write new echo message + $db->write_echo(%e_write); + + push( @new, $echo_hash ); + } } } } - close $echo_fh; + else { + print $res->status_line, "\n"; + } + $db->commit(); + # Make new messages url + # my $new_messages_url = "$host$msg_url"; + # my $count = 0; + # while ( $count < @new ) { + # $new_messages_url .= $new[$count]->{hash} . "/"; + # $count++; # } + + # Get messages + my @msg_con; + my $count = 0; + while ( $count < @new ) { + my $new_messages_url = "$host$msg_url" . $new[$count]->{hash}; + my $req_msg = HTTP::Request->new( GET => $new_messages_url ); + my $res_msg = $ua->request($req_msg); + if ( $res_msg->is_success() ) { + push( @msg_con, $res_msg->content() ); + } + else { + print $res->status_line, "\n"; + } + $count++; + } + + # Populate $msgs and $base64 + while (<@msg_con>) { + my @message = split /:/, $_; + if ( defined( $message[1] ) ) { + $msgs .= $message[0] . "\n"; + $base64 .= $message[1] . "\n"; + } + } } my $new_messages = "

Новые сообщения

\n"; if ( defined($msgs) ) { - my @msg_list = split /\n/, $msgs; + my @msg_list = split /\n/, $base64; # Begin transaction - print localtime().": writing messages\n"; + print localtime() . ": writing messages\n"; $db->begin(); while (<@msg_list>) { my $mes_hash = $_; - my $text = II::Enc->decrypt("./msg/$mes_hash"); + my $text = II::Enc->decrypt($mes_hash); open my $m, "<", \$text or die "Cannot open message: $!\n"; @@ -109,9 +155,10 @@ sub get_echo { # Write message to DB $db->write(%data); } + # Commit transaction $db->commit(); - print localtime().": messages writed to DB!\n"; + print localtime() . ": messages writed to DB!\n"; } return $msgs; }