mIRC Homepage

Trying to convert a small Perl script into mIRC...

Posted By: dylan

Trying to convert a small Perl script into mIRC... - 13/06/03 10:07 PM

I've got a small script written in Perl that I'm trying to convert into a mIRC script. I'm having a bit of a hard time and I'm wondering if anyone out there who can script in both Perl and mIRC can help me out. The code is:

sub decode {
my $text = $_[0];
my $key = 'secretkey';
my $response = '';
my($i, $j, $num, $result);
my($text_len) = length($text);
my($key_len) = length($key);
for ($i = $text_len - 1; $i >= 0; --$i) {
$num = ord(chop($text));
if ($num == 127) { $num = 58 }
$num -= 33;
for ($j = $i; $j < $key_len; $j += $text_len) {
$num -= ord(substr($key, $j, 1)) + $key_len;
}
$num = $num % 95 + 33;
$result .= pack("c", $num);
}
return($result);
}

Is it possible to rewrite this in the mIRC scripting language? Or even make a DLL from it? If not, thanks for your time.
Posted By: kovert

Re: Trying to convert a small Perl script into mIRC... - 15/06/03 12:46 AM

It sure is possible...but its unlikely you will get anyone to paste code smile I would help you out...if you posted the encrypted part $_[0] and the $result so I can check my code and make sure it works correctly.
Posted By: codemastr

Re: Trying to convert a small Perl script into mIRC... - 15/06/03 01:23 AM

This should work:
Code:
alias perl_decode {
  var %text = $1-
  var %key = secretkey
  var %response
  var %i, %j, %num ,%result
  var %text_len = $len(%text)
  var %key_len = $len(%key)
  %i = $calc(%text_len -1)
  while (%i &gt;= 0) {
    var %chop = $right(%text,1)
    %text = $left(%text,-1)
    var %num = $asc(%chop)
    if (%num == 127) { %num = 58 }
    %num = $calc(%num - 33)
    %j = %i
    while (%j &lt; %key_len) {
      %num = $calc(%num - ($asc($mid(%key,$calc(%j +1),1)) + %key_len))
      inc %j %text_len
    }
    %num = $calc($perl_modulus(%num,95) + 33)

    .echo -a %num
    %result = %result $+ $chr(%num)
    dec %i
  }
  return %result
}

;Perl has a very odd way of handling modulus
alias -l perl_modulus {
  if ($1 &lt; 0) {
    var %i = 1
    while ($calc(%i * $2) &lt;= $abs($1)) {
      inc %i
    }
    var %m = $calc(%i * $2)
    return $calc(%m - $abs($1))
  }
  return $calc($1 % $2)
}


I renamed it $perl_decode because mIRC already has an identifier called $decode. It seems to work perfectly. The biggest issue with the code (if you tried to convert it) was that Perl's % operator behaves differently than well... every other % operator I've ever seen. I think I got it working though, it seemed to work when I compared the output of some of some runs with the perl command, but if it doesn't, let me know.
Posted By: dylan

Re: Trying to convert a small Perl script into mIRC... - 15/06/03 05:46 AM

Thank you so much, codemastr, that worked great. I really appreciate it! smile
Posted By: qwerty

Re: Trying to convert a small Perl script into mIRC... - 15/06/03 06:59 AM

Why on earth would you use a loop in the $perl_modulus alias? This would be considered inefficient even for a C program; it's unacceptably slow for mirc. Considering that the $perl_decode alias already does quite a lot of work, an optimized $perl_modulus is more than necessary.
Code:
alias perl_modulus {
  if $1 &lt; 0 { return $calc($int($calc($1 / $2 - 1)) * - $2 + $1) }
  return $calc($1 % $2)
}
I also got rid of the variable %m and $abs(), which made things even worse.
© 2021 mIRC Discussion Forums