diff --git a/II/Enc.pm b/II/Enc.pm index 6622713..5c0aed0 100644 --- a/II/Enc.pm +++ b/II/Enc.pm @@ -22,7 +22,8 @@ sub decrypt { my ( $self, $base64 ) = @_; # Decrypt message - my $dec = `echo "$base64" | base64 -d`; + my $dec = decode_base64($base64); + # my $dec = `echo "$base64" | base64 -d`; return $dec; } diff --git a/II/Get.pm b/II/Get.pm index 4a8bbc3..a2ffd96 100644 --- a/II/Get.pm +++ b/II/Get.pm @@ -36,6 +36,7 @@ sub get_echo { my $msgs; my $base64; + my @messages_hash; foreach my $echo (@$echoareas) { # Get echo message hashes @@ -60,7 +61,7 @@ sub get_echo { # Write new echo message $db->write_echo(%e_write); - + $msgs .= $_ . "\n"; push( @new, $echo_hash ); } } @@ -95,12 +96,15 @@ sub get_echo { $count++; } - # Populate $msgs and $base64 + # Populate hash while (<@msg_con>) { my @message = split /:/, $_; if ( defined( $message[1] ) ) { - $msgs .= $message[0] . "\n"; - $base64 .= $message[1] . "\n"; + my $h = { + hash => $message[0], + base64 => $message[1], + }; + push( @messages_hash, $h ); } } } @@ -108,15 +112,15 @@ sub get_echo { my $new_messages = "

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

\n"; if ( defined($msgs) ) { - my @msg_list = split /\n/, $base64; # Begin transaction print localtime() . ": writing messages\n"; $db->begin(); - while (<@msg_list>) { - my $mes_hash = $_; - my $text = II::Enc->decrypt($mes_hash); + my $c = 0; + while ( $c < @messages_hash ) { + my $mes_hash = $messages_hash[$c]->{hash}; + my $text = II::Enc->decrypt( $messages_hash[$c]->{base64} ); open my $m, "<", \$text or die "Cannot open message: $!\n"; @@ -154,6 +158,7 @@ sub get_echo { # Write message to DB $db->write(%data); + $c++; } # Commit transaction diff --git a/II/Send.pm b/II/Send.pm index 6b8e6ea..9dd3929 100644 --- a/II/Send.pm +++ b/II/Send.pm @@ -8,10 +8,12 @@ use Data::Dumper; sub new { my $class = shift; + my $db = II::DB->new(); my $self = { _config => shift, _echo => shift, _base64 => shift, + _db => $db, }; bless $self, $class; @@ -19,22 +21,23 @@ sub new { } sub send { - my ($self, $hash) = @_; + my ( $self, $hash ) = @_; my $config = $self->{_config}; my $echo = $self->{_echo}; my $base64 = $self->{_base64}; + my $db = $self->{_db}; # Push message to server my $host = $config->{host}; my $auth = $config->{key}; $host .= "u/point"; my $ua = LWP::UserAgent->new(); - my $response = $ua->post( $host, { 'pauth' => $auth, 'tmsg' => $base64 } ); + my $response + = $ua->post( $host, { 'pauth' => $auth, 'tmsg' => $base64 } ); - my $db = II::DB->new(); - if ($response->{_rc} == 200) { - $db->update_out($hash); - } + if ( $response->{_rc} == 200 ) { + $db->update_out($hash); + } } 1; diff --git a/ii.sql b/ii.sql index 78d1ccb..76e4940 100644 Binary files a/ii.sql and b/ii.sql differ