iiplc/II/Enc.pm

76 lines
1.5 KiB
Perl
Raw Normal View History

2014-06-11 15:55:00 +04:00
package II::Enc;
use II::DB;
use MIME::Base64;
sub new {
my $class = shift;
my $db = II::DB->new();
my $self = {
_config => shift,
_data => shift,
_db => $db,
};
bless $self, $class;
return $self;
}
2014-06-16 12:02:34 +04:00
sub decrypt {
2014-06-17 13:22:18 +04:00
my ( $self, $base64 ) = @_;
2014-06-16 12:02:34 +04:00
2014-08-06 12:16:16 +04:00
return decode_base64($base64);
2014-06-16 12:02:34 +04:00
}
2014-06-11 15:55:00 +04:00
sub encode {
my ($self) = @_;
my $config = $self->{_config};
my $data = $self->{_data};
my $db = $self->{_db};
my $hash = II::Enc->new_hash();
# Make base64 message
2014-06-16 12:02:34 +04:00
my $message = $data->{echo} . "\n";
$message .= $data->{to} . "\n";
$message .= $data->{subg} . "\n\n";
$message .= '@repto:' . $data->{hash} . "\n" if defined( $data->{hash} );
2014-06-11 15:55:00 +04:00
$message .= $data->{post};
2014-08-06 12:16:16 +04:00
# my $encoded = `echo "$message" | base64`;
my $encoded = encode_base64($message);
2014-06-11 15:55:00 +04:00
$encoded =~ s/\//_/g;
$encoded =~ s/\+/-/g;
2014-07-01 11:27:24 +04:00
# Preparsing
my $post = II::T->in_pre($data->{post});
2014-06-11 15:55:00 +04:00
# Make data
my %out = (
hash => $hash,
time => $data->{time},
echo => $data->{echo},
from_user => $data->{from},
to_user => $data->{to},
subg => $data->{subg},
2014-07-01 11:27:24 +04:00
post => $post,
2014-06-11 15:55:00 +04:00
base64 => $encoded,
send => 0,
);
$db->write_out(%out);
return 0;
}
sub new_hash {
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 );
my $string;
$string .= $chars[ rand @chars ] for 1 .. 21;
return $string;
}
1;