# Sending messages to pesonal classes and on zaway has become popular.
# I also want to hide, and set my location ($tty) when I return, so
# these two commands do everything for me.  The optional init which
# follows binds zaway_on to 'C-a' and zaway_off to 'A'

# contact info to add to outgoing messages
my $zaway_contact;

# run zaway and send the message to -c $USER and also zephyr hide
# call as "zaway_on q[message ...]" q[] is for quoting the message
# until we have a real argument handler in user commands
sub zaway_on {
    my $msg;
    if (@_) {
	$msg = join ' ', @_;
    } else { 
	$msg = owl::command("getvar zaway_msg_default");
    }
    my $time = localtime();
    owl::command("zwrite -c $ENV{USER} -i away -m $msg");
    owl::command("zlog out");
    owl::command("away $msg\n$zaway_contact\nzaway message set at $time");
#   aim_logout();
    return "zaway started";
}

# optional argument will be set as the tty and look like the zephyr location
# arg must be quoted if it contains things like - or it will be ignored
sub zaway_off (;$) {
    my $tty = shift || owl::command("getvar tty");
    my $msg = "back now";
    $msg = "back online from $tty" if $tty;
    owl::command("zwrite -c $ENV{USER} -i away -m $msg");
    owl::command("exec zctl flush_locs");
    owl::command("set tty $tty") if $tty;
    owl::command("zlog in");
    owl::command("away off");
    aim_logout();
    aim_login();
    return "zaway stopped";
}

sub zaway_init () {
    owl::command('alias zaway_on aperl zaway_on');
    owl::command('alias zaway_off aperl zaway_off');
    owl::command('bindkey recv "C-a" command start-command zaway_on q[');
    # display the current value of tty so the user can determine if a
    # new one is desired
    owl::command('bindkey recv "A" command ( print tty ; start-command zaway_off )');
}

# set the contact info to be added to outgoing messages
sub zaway_set_contact ($) {
    $zaway_contact = shift;
}

1;
