#!/usr/bin/perl
#
# by Jan Krüger aka jast

# quick help:
# /mooify [<charset> [<min length> [<max length>]]]
# charsets (i.e. all chars left to the number will be used):
#  012 3 4 5
# o0O{}()[]

use strict;

use vars qw($VERSION %IRSSI);
$VERSION = "2003041019";
%IRSSI = (
    authors     => "Jan Krueger",
    contact     => "mooify.teh.jast\@neverbox.com",
    name        => "Mooify",
    description => "moos at command",
    license     => "GPLv2",
    url         => "http://www.jast-dev.net.tc/",
    modules     => "",
    changed     => "$VERSION",
);


use Irssi;
use POSIX;

sub generatemoo {
    my ($charset, $min, $max) = @_[0..2];
    my $actual;
    my @chars;

    $charset = 3 if(!$charset);
    $min = 8 if(!$min);
    $max = 16 if(!$max);

    if($min >= $max) {
        $actual = $min;
    } else {
        $actual = $min + floor(rand($max - $min));
    }

    push @chars, "o";
    push @chars, "0"  if($charset > 0);
    push @chars, "O"  if($charset > 1);
    push @chars, "{}" if($charset > 2);
    push @chars, "()" if($charset > 3);
    push @chars, "[]" if($charset > 4);

    my $result = "m";
    for (my $i = 0; $i < $actual; $i++) {
        $result .= $chars[floor(rand($charset + 1))];
    }

    return $result;
}

sub cmd_mooify ($$$) {
    my ($arg, $server, $witem) = @_;
    if ($witem && ($witem->{type} eq 'CHANNEL' || $witem->{type} eq 'QUERY')) {
	$witem->command('MSG '.$witem->{name}.' '.generatemoo($arg ? split(/ /, $arg) : ''));
    } else {
	print CLIENTCRAP "%B>>%n ".generatemoo($arg ? split(/ /, $arg) : '');
    }
}

Irssi::command_bind('mooify', \&cmd_mooify);

