Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- sub encode_base64 ($;$)
- {
- if ($] >= 5.006) {
- require bytes;
- if (bytes::length($_[0]) > length($_[0]) ||
- ($] >= 5.008 && $_[0] =~ /[^\0-\xFF]/))
- {
- require Carp;
- Carp::croak("The Base64 encoding is only defined for bytes");
- }
- }
- use integer;
- my $eol = $_[1];
- $eol = "\n" unless defined $eol;
- my $res = pack("u", $_[0]);
- # Remove first character of each line, remove newlines
- $res =~ s/^.//mg;
- $res =~ s/\n//g;
- $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
- # fix padding at the end
- my $padding = (3 - length($_[0]) % 3) % 3;
- $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
- # break encoded string into lines of no more than 76 characters each
- if (length $eol) {
- $res =~ s/(.{1,76})/$1$eol/g;
- }
- return $res;
- }
- sub decode_base64 ($)
- {
- local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123]
- use integer;
- my $str = shift;
- $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars
- if (length($str) % 4) {
- require Carp;
- Carp::carp("Length of base64 data not a multiple of 4")
- }
- $str =~ s/=+$//; # remove padding
- $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format
- return "" unless length $str;
- ## I guess this could be written as
- #return unpack("u", join('', map( chr(32 + length($_)*3/4) . $_,
- # $str =~ /(.{1,60})/gs) ) );
- ## but I do not like that...
- my $uustr = '';
- my ($i, $l);
- $l = length($str) - 60;
- for ($i = 0; $i <= $l; $i += 60) {
- $uustr .= "M" . substr($str, $i, 60);
- }
- $str = substr($str, $i);
- # and any leftover chars
- if ($str ne "") {
- $uustr .= chr(32 + length($str)*3/4) . $str;
- }
- return unpack ("u", $uustr);
- }
- IRC::print "Loading llrcombs's Binary/Hex/ASCII converter script";
- #Replace a string without using RegExp.
- sub str_replace
- {
- my $search = shift;
- my $replace = shift;
- my $subject = shift;
- if (! defined $subject) { return -1; }
- my $count = shift;
- if (! defined $count) { $count = -1; }
- my ($i,$pos) = (0,0);
- while ( (my $idx = index( $subject, $search, $pos )) != -1 )
- {
- substr( $subject, $idx, length($search) ) = $replace;
- $pos=$idx+length($replace);
- if ($count>0 && ++$i>=$count) { last; }
- }
- return $subject;
- }
- sub convert_binary
- {
- my $query = shift;
- $l=(length $query)*8;@a=unpack "B$l",$query;
- $binary = "";
- foreach $line (@a){
- $binary = $binary.$line;
- }
- $binary = "$binary";
- $i = 0;
- $binary2 = "";
- foreach $digit (split(undef,$binary)){
- $i++;
- $binary2 = $binary2.$digit;
- if($i == 8){
- $binary2 = $binary2." ";
- $i = 0;
- }
- }
- IRC::print ("Sent binary: $query");
- if(length($binary2)>422){
- while(length($binary2)){
- $binary_432 = substr($binary2,0,422);
- $binary2 = substr($binary2,423);
- IRC::command ("/say $binary_432");
- }
- }else{
- IRC::command ("/say $binary2");
- }
- return 1;
- }
- sub convert_hex {
- $query = shift;
- $hex = unpack("H*",$query);
- IRC::print ("Sent hex: $query");
- IRC::command ("/say $hex");
- }
- sub convert_ascii{
- $query = shift;
- @ascii = unpack("C*",$query);
- IRC::print ("Sent ASCII: $query");
- IRC::command ("/say @ascii");
- }
- sub convert_base64{
- $query = shift;
- $base64 = encode_base64($query);
- IRC::print ("Sent Base64: $query");
- IRC::command ("/say $base64");
- }
- sub watch_binary {
- foreach $line (@_) {
- $line =~ m/\:(.*?)\!(.*?)\sPRIVMSG\s(.*?)\s\:(.*)?/;
- $m_nick = $1;
- $m_send = $2;
- $m_chan = $3;
- $m_line = $4;
- $m_line =~ s/^\s+//; # Remove trailing whitespace
- $m_line =~ s/\s+$//;
- $m_line_original = $m_line;
- $m_line = str_replace(" ","",$m_line);
- if(int($m_line) && length($m_line) % 8 == 0 && $m_line =~ m/^[10]*$/) {
- $s=$m_line;$l=length $s;@a=pack "B$l",$s;
- IRC::print "Binary from $m_nick: @a";
- }elsif(int($m_line) && $m_line =~ m/^[0-9]*$/){
- foreach(split(" ",$m_line_original)){
- if(!length($_) >= 2 || !length($_) <= 3 || int($_) > 255){
- return;
- }
- }
- $a = pack("C*",split(" ",$m_line_original));
- IRC::print "ASCII from $m_nick: $a";
- }elsif($m_line =~ m/^[0-9A-F]*$/ && length($m_line) % 2 == 0){
- $a = pack("H*",$m_line);
- IRC::print "Hex from $m_nick: $a";
- }elsif($m_line =~ m{^(?: [A-Za-z0-9+/]{4} )*(?:[A-Za-z0-9+/]{2} [AEIMQUYcgkosw048] =|[A-Za-z0-9+/] [AQgw] ==)?\z}x){
- $a = decode_base64($m_line);
- $str = $a;
- if(!$a || !($str=~tr/\200-\377/?/) == $a){
- return;
- }
- IRC::print "Base64 from $m_nick: $a";
- }
- }
- }
- IRC::add_message_handler("PRIVMSG", "watch_binary");
- IRC::add_command_handler ("base64", "convert_base64");
- IRC::add_command_handler ("hex", "convert_hex");
- IRC::add_command_handler ("ascii", "convert_ascii");
- IRC::add_command_handler ("binary", "convert_binary");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement