
#Written by pweaver
#last updated 5/13/2011

# run  
#startup perl do '/$path/replyun.pl' 
# in barnowl to add the command. 

#It binds it to M-r, you can change if by editing
#owl::command('bindkey recv "M-r" command reply-un');

package BarnOwl;
use constant WEBZEPHYR_PRINCIPAL => "daemon/webzephyr.mit.edu";
use constant WEBZEPHYR_CLASS     => "webzephyr";
use constant WEBZEPHYR_OPCODE    => "webzephyr";


sub do_reply_un
{
    my $func = shift;
    my $m = owl::getcurmsg();
    my $type = lc($m->type);
    if ($type eq 'zephyr') {
	#my $class = lc($m->class);
	#my $instance = lc($m->instance);
	#my $opcode = lc($m->opcode);
#	
	#if ($m->recipient ne "") {
	#    if($class eq "message"){
#
#	    }
	#    else{
	#	my $iarg = ($instance eq 'personal') ? '' :  ' -i ' . $instance;
	#	my $command = ($opcode eq 'crypt')? 'zcrypt' : 'zwrite';
	 #   }
	#} else {
	#    my $iarg = ($instance eq 'personal') ? '' :  ' -i ' . $instance; 
	#    my $command = ($opcode eq 'crypt')? 'zcrypt' : 'zwrite';
	    owl::command(replyuncmd($m));
    
    }
    return;
}
sub strip_realm {
    my $sender = shift;
    my $realm = BarnOwl::zephyr_getrealm();
    $sender =~ s/\@\Q$realm\E$//;
    return $sender;
}
sub principal_realm {
    my $principal = shift;
    my ($user, $realm) = split(/@/,$principal);
    return $realm;
}

sub context_replyun_cmd {
    my $mclass = shift;
    my $minstance = shift;
    my @class;
    my @instance;
    if (lc($mclass) ne "message") {
        @class = ('-c', "un" . $mclass);
	if (lc($minstance) ne "personal") {
	    @instance = ('-i', $minstance);
	}

    }
    else{
	if (lc($minstance) ne "personal") {
	    @instance = ('-i', "un" . $minstance);
	}
	else{
	    @instance = ('-i', "un");
	}
    }
    
    return (@class, @instance);
}


sub replyuncmd {
    my $self = shift;
    my $sender = shift;
    $sender = 0 unless defined $sender;
    my ($class, $instance, $to, $cc);

    if($sender && $self->opcode eq WEBZEPHYR_OPCODE) {
        $class = WEBZEPHYR_CLASS;
        $instance = $self->pretty_sender;
        $instance =~ s/-webzephyr$//;
        $to = WEBZEPHYR_PRINCIPAL;
    } elsif($self->class eq WEBZEPHYR_CLASS
            && $self->is_loginout) {
        $class = WEBZEPHYR_CLASS;
        $instance = $self->instance;
        $to = WEBZEPHYR_PRINCIPAL;
    } elsif($self->is_loginout) {
        $class = 'MESSAGE';
        $instance = 'PERSONAL';
        $to = $self->sender;
    } elsif($sender && !$self->is_private) {
        # Possible future feature: (Optionally?) include the class and/or
        # instance of the message being replied to in the instance of the 
        # outgoing personal reply
        $class = 'MESSAGE';
        $instance = 'PERSONAL';
        $to = $self->sender;
    } else {
        $class = $self->class;
        $instance = $self->instance;
        $to = $self->recipient;
        $cc = $self->zephyr_cc();
        if($to eq '*' || $to eq '') {
            $to = '';
        } elsif($to !~ /^@/) {
            $to = $self->is_outgoing ? $self->recipient : $self->sender;
        }
    }

    my @cmd;
    if(lc $self->opcode eq 'crypt' and ( not $sender or $self->is_private)) {
        # Responses to zcrypted messages should be zcrypted, so long as we
        # aren't switching to personals
        @cmd = ('zcrypt');
    } else {
        @cmd = ('zwrite');
    }

    push @cmd, context_replyun_cmd($class, $instance);

    if ($to ne '') {
        $to = strip_realm($to);
        if (defined $cc and not $sender) {
            my @cc = grep /^[^-]/, ($to, split /\s+/, $cc);
            my %cc = map {$_ => 1} @cc;
            # this isn't quite right - it doesn't strip off the
            # user if the message was addressed to them by fully qualified
            # name
            delete $cc{strip_realm(BarnOwl::zephyr_getsender())};
            @cc = keys %cc;

            my $sender_realm = principal_realm($self->sender);
            if (BarnOwl::zephyr_getrealm() ne $sender_realm) {
                @cc = map {
                    if($_ !~ /@/) {
                       "${_}\@${sender_realm}";
                    } else {
                        $_;
                    }
                } @cc;
            }
            push @cmd, '-C', @cc;
        } else {
            if(BarnOwl::getvar('smartstrip') eq 'on') {
                $to = BarnOwl::zephyr_smartstrip_user($to);
            }
            push @cmd, $to;
        }
    }
    return BarnOwl::quote(@cmd);
}

BarnOwl::new_command("reply-un",
    \&BarnOwl::do_reply_un,
		     {
        summary => "reply to the un class of the same instance",
        usage => "reply-un",
		     });

owl::command('bindkey recv "M-r" command reply-un');
