iiplc/II/Get.pm

167 lines
4.2 KiB
Perl
Raw Normal View History

2014-06-11 15:55:00 +04:00
package II::Get;
2014-06-17 13:22:18 +04:00
use LWP::UserAgent;
use HTTP::Request;
2014-06-11 15:55:00 +04:00
use II::DB;
2014-06-16 12:02:34 +04:00
use II::Enc;
2014-06-11 15:55:00 +04:00
use Data::Dumper;
sub new {
my $class = shift;
2014-06-17 13:22:18 +04:00
my $ua = LWP::UserAgent->new();
$ua->agent("iiplc/0.1rc1");
my $db = II::DB->new();
my $self = {
_config => shift,
_ua => $ua,
_db => $db,
};
2014-06-11 15:55:00 +04:00
bless $self, $class;
return $self;
}
sub get_echo {
my ($self) = @_;
my $config = $self->{_config};
my $echoareas = $config->{echoareas};
my $host = $config->{host};
2014-06-17 13:22:18 +04:00
my $ua = $self->{_ua};
my $db = $self->{_db};
2014-06-11 15:55:00 +04:00
2014-06-16 12:02:34 +04:00
my $echo_url = 'u/e/';
my $msg_url = 'u/m/';
2014-06-11 15:55:00 +04:00
my $msgs;
2014-06-17 13:22:18 +04:00
my $base64;
2014-06-11 15:55:00 +04:00
foreach my $echo (@$echoareas) {
2014-06-17 13:22:18 +04:00
# Get echo message hashes
my $req_echo = HTTP::Request->new( GET => "$host$echo_url$echo" );
my $res_echo = $ua->request($req_echo);
2014-06-11 15:55:00 +04:00
2014-06-17 13:22:18 +04:00
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 => $_,
);
# Write new echo message
$db->write_echo(%e_write);
push( @new, $echo_hash );
}
2014-06-16 12:02:34 +04:00
}
2014-06-11 15:55:00 +04:00
}
}
2014-06-17 13:22:18 +04:00
else {
print $res->status_line, "\n";
}
$db->commit();
2014-06-11 15:55:00 +04:00
2014-06-17 13:22:18 +04:00
# Make new messages url
# my $new_messages_url = "$host$msg_url";
# my $count = 0;
# while ( $count < @new ) {
# $new_messages_url .= $new[$count]->{hash} . "/";
# $count++;
2014-06-11 15:55:00 +04:00
# }
2014-06-17 13:22:18 +04:00
# 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";
}
}
2014-06-11 15:55:00 +04:00
}
my $new_messages
= "<!DOCTYPE html><meta charset=utf8><body><h1>Новые сообщения</h1>\n";
if ( defined($msgs) ) {
2014-06-17 13:22:18 +04:00
my @msg_list = split /\n/, $base64;
2014-06-16 12:49:05 +04:00
# Begin transaction
2014-06-17 13:22:18 +04:00
print localtime() . ": writing messages\n";
2014-06-16 12:49:05 +04:00
$db->begin();
2014-06-11 15:55:00 +04:00
while (<@msg_list>) {
my $mes_hash = $_;
2014-06-16 12:02:34 +04:00
2014-06-17 13:22:18 +04:00
my $text = II::Enc->decrypt($mes_hash);
2014-06-16 12:02:34 +04:00
open my $m, "<", \$text
2014-06-11 15:55:00 +04:00
or die "Cannot open message: $!\n";
my @mes;
while (<$m>) {
push( @mes, $_ );
}
close $m;
my $count = 7;
my $post;
while ( $count < @mes ) {
$post .= $mes[$count];
$count++;
}
chomp( $mes[2] );
chomp( $mes[1] );
chomp( $mes[3] );
chomp( $mes[5] );
chomp( $mes[6] );
# Make data
my %data = (
hash => $mes_hash,
time => $mes[2],
echo => $mes[1],
from_user => $mes[3],
to_user => $mes[5],
subg => $mes[6],
post => "$post",
read => 0,
);
# Write message to DB
$db->write(%data);
}
2014-06-17 13:22:18 +04:00
2014-06-16 12:49:05 +04:00
# Commit transaction
$db->commit();
2014-06-17 13:22:18 +04:00
print localtime() . ": messages writed to DB!\n";
2014-06-11 15:55:00 +04:00
}
return $msgs;
}
1;