Switch to clean SQL database

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-06-17 13:22:18 +04:00
parent fa0826d592
commit 05436b1496
3 changed files with 111 additions and 42 deletions

View File

@ -20,6 +20,25 @@ sub new {
return $self; 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 { sub begin {
my ($self) = @_; my ($self) = @_;
my $dbh = $self->{_dbh}; my $dbh = $self->{_dbh};
@ -36,6 +55,18 @@ sub commit {
$dbh->do('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 { sub write_out {
my ( $self, %data ) = @_; my ( $self, %data ) = @_;
my $dbh = $self->{_dbh}; my $dbh = $self->{_dbh};
@ -50,7 +81,7 @@ sub write_out {
} }
sub update_out { sub update_out {
my ($self, $hash) = @_; my ( $self, $hash ) = @_;
my $dbh = $self->{_dbh}; my $dbh = $self->{_dbh};
my $q = "update output set send=1 where hash='$hash'"; my $q = "update output set send=1 where hash='$hash'";

View File

@ -19,19 +19,10 @@ sub new {
} }
sub decrypt { sub decrypt {
my ( $self, $file ) = @_; my ( $self, $base64 ) = @_;
open my $fh, "<", $file or die "Cannot open file $file: $!\n";
my $message;
while (<$fh>) {
$message .= $_;
}
close $fh;
my @enc = split /:/, $message;
# Decrypt message # Decrypt message
my $dec = `echo "$enc[1]" | base64 -d`; my $dec = `echo "$base64" | base64 -d`;
return $dec; return $dec;
} }

105
II/Get.pm
View File

@ -1,5 +1,6 @@
package II::Get; package II::Get;
use LWP::Simple; use LWP::UserAgent;
use HTTP::Request;
use II::DB; use II::DB;
use II::Enc; use II::Enc;
@ -9,7 +10,14 @@ use Data::Dumper;
sub new { sub new {
my $class = shift; 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; bless $self, $class;
return $self; return $self;
@ -20,57 +28,95 @@ sub get_echo {
my $config = $self->{_config}; my $config = $self->{_config};
my $echoareas = $config->{echoareas}; my $echoareas = $config->{echoareas};
my $host = $config->{host}; my $host = $config->{host};
my $ua = $self->{_ua};
my $db = II::DB->new(); my $db = $self->{_db};
my $echo_url = 'u/e/'; my $echo_url = 'u/e/';
my $msg_url = 'u/m/'; my $msg_url = 'u/m/';
my $msgs; my $msgs;
my $base64;
foreach my $echo (@$echoareas) { 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 my @new;
open my $echo_fh, ">", "./echo/$echo" $db->begin();
or die "Cannot open file: $!\n"; if ( $res_echo->is_success ) {
print $echo_fh @content; my @mes = split /\n/, $res_echo->content();
close $echo_fh; while (<@mes>) {
if ( $_ =~ /.{20}/ ) {
if ( $db->check_hash( $_, $echo ) == 0 ) {
my $echo_hash = {
echo => $echo,
hash => $_,
};
my %e_write = (
echo => $echo,
hash => $_,
);
# Write new echo message
$db->write_echo(%e_write);
push( @new, $echo_hash );
}
}
}
}
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 # Get messages
open my $echo_fh, "<", "./echo/$echo" my @msg_con;
or die "Cannot open file: $!\n"; my $count = 0;
while (<$echo_fh>) { while ( $count < @new ) {
chomp($_); my $new_messages_url = "$host$msg_url" . $new[$count]->{hash};
if ($_ =~ /.{20}/) { my $req_msg = HTTP::Request->new( GET => $new_messages_url );
if ( !( -e "./msg/$_" ) ) { my $res_msg = $ua->request($req_msg);
$msgs .= $_ . "\n"; if ( $res_msg->is_success() ) {
# @w_cmd = ( 'wget', '-O', push( @msg_con, $res_msg->content() );
# "./msg/$_", "$host" . "$msg_url" . "$_" );
`curl $host$msg_url$_ > ./msg/$_`;
# system(@w_cmd) == 0 or die "Cannot download file: $!\n";
} }
else {
print $res->status_line, "\n";
} }
$count++;
} }
close $echo_fh;
# } # 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 my $new_messages
= "<!DOCTYPE html><meta charset=utf8><body><h1>Новые сообщения</h1>\n"; = "<!DOCTYPE html><meta charset=utf8><body><h1>Новые сообщения</h1>\n";
if ( defined($msgs) ) { if ( defined($msgs) ) {
my @msg_list = split /\n/, $msgs; my @msg_list = split /\n/, $base64;
# Begin transaction # Begin transaction
print localtime().": writing messages\n"; print localtime() . ": writing messages\n";
$db->begin(); $db->begin();
while (<@msg_list>) { while (<@msg_list>) {
my $mes_hash = $_; my $mes_hash = $_;
my $text = II::Enc->decrypt("./msg/$mes_hash"); my $text = II::Enc->decrypt($mes_hash);
open my $m, "<", \$text open my $m, "<", \$text
or die "Cannot open message: $!\n"; or die "Cannot open message: $!\n";
@ -109,9 +155,10 @@ sub get_echo {
# Write message to DB # Write message to DB
$db->write(%data); $db->write(%data);
} }
# Commit transaction # Commit transaction
$db->commit(); $db->commit();
print localtime().": messages writed to DB!\n"; print localtime() . ": messages writed to DB!\n";
} }
return $msgs; return $msgs;
} }