package BarnOwl;

# Sub unclasses to some depth
sub do_sub_unclasses
{
    my $func = shift;
    my $depth = shift;
    my $class = shift;
    if (not ($depth and $class))
    {
        owl::help('subun');
        return;
    }
    my $msgtext = "Tried to sub $class to depth $depth:";
    my $ret;
    for ($i = 0; $i < $depth; $i++)
    {
        $ret = owl::sub($class);
        $msgtext .= "\n" . $class;
        $class = "un" . $class;
    }
    #BarnOwl::admin_message("Zephyr", $msgtext);
    return;
}

BarnOwl::new_command("subun",
    \&BarnOwl::do_sub_unclasses,
    {
        summary => "sub class and unclasses",
        usage => "subunclasses <depth> <class_name>",
    });

# Unsub unclasses to some depth
sub do_unsub_unclasses
{
    my $func = shift;
    my $depth = shift;
    my $class = shift;
    if (not ($depth and $class))
    {
        owl::help('unsubun');
        return;
    }
    my $msgtext = "Tried to unsub $class to depth $depth:";
    my $ret;
    for ($i = 0; $i < $depth; $i++)
    {
        $ret = owl::unsub($class);
        $msgtext .= "\n" . $class;
        $class = "un" . $class;
    }
    #BarnOwl::admin_message("Zephyr", $msgtext);
    return;
}

BarnOwl::new_command("unsubun",
    \&BarnOwl::do_unsub_unclasses,
    {
        summary => "sub class and unclasses",
        usage => "unsubun <depth> <class_name>",
    });

sub do_sub_file
{
    my $func = shift;
    my $file = shift;
    if (not ($file))
    {
        owl::help('subfile');
        return;
    }
    unless(open(fh, '<', $file)){
	BarnOwl::admin_message("Zephyr", "Can't open $file: $! \n");
	return
    }
    
    
    my $msgtext = "Tried to sub to classes in file $file:\n";
    #my $ret;
    while (<fh>) { 
	chomp;
	$ret = owl::sub($_);
	$msgtext .= "$_ :" . $ret . "\n";
    }
    close($fh);
    BarnOwl::admin_message("Zephyr", $msgtext);
    return;
}

BarnOwl::new_command("subfile",
    \&BarnOwl::do_sub_file,
		     {
        summary => "sub all classes in a file",
        usage => "subfile <file name>",
		     });
use IPC::Open2;
BarnOwl::new_command(decrypt => sub {
    my $msg = BarnOwl::getcurmsg();
    my $cmd = shift;
    my @args = @_;
    if (scalar @args == 0) {
	@args = ('-c', $msg->class, '-i', $msg->instance);
    }
    my ($zo, $zi);
    my $pid = open2($zo, $zi, '/mit/barnowl/bin/zcrypt', '-D', @args) or die "Couldn't launch zcrypt\n";
    my $decrypted;
    print $zi $msg->body . "\n";
    close $zi;
    while (<$zo>) {
	chomp;
	last if $_ eq "**END**";
	$decrypted .= "$_\n";
    }
    BarnOwl::popless_ztext($decrypted);
    waitpid $pid, 0;
		     },
		     {summary => "Decrypt a zcrypted message once",
    usage => "decrypt [args]",
    description => "Invokes /mit/outland/bin/zcrypt on the current message,\n
using the class and instance to find the crypt key, and pops up the\n
decrypted output. If args are specified, they are passed to zcrypt and the\n
class and instance are ignored.\n\n
SEE ALSO: zcrypt(1)"});

BarnOwl::new_command(zsend => sub {
    my $cmd = shift;
    my @args = @_;
    if (scalar @args == 0) {
	die "zsend requires at least one arg";
    }
    my $pid = open2($zo, $zi, '/mit/pweaver/bin/zsend', @args) or die "Couldn't launch zsend\n";
    my $output;
    close $zi;
		     
    waitpid $pid, 0;},
		     {summary => "Send a zsend message. -S is sender",
    usage => "zsend [args]",
    description => "Invokes /mit/pweaver/bin/zsend"});


