### The owlconf config file   -*- perl -*-  
###  $Id$

### !!!!!WARNING!!!!! !!!!!WARNING!!!!! !!!!!WARNING!!!!! !!!!!WARNING!!!!!
### This is an example file intended to demonstrate how to use 
### various features of owl.  Some of the key bindings, in particular,
### are more for examples than things you may actually want to use.
### Make sure to read through it first and understand it before just using it.
### Don't blame me if anything in here ends up vaporizing your dog.
### !!!!!WARNING!!!!! !!!!!WARNING!!!!! !!!!!WARNING!!!!! !!!!!WARNING!!!!!

###
### This file is interpreted by the perl interpreter.
### If you wish to execute an owl command use the
### function owl::command().  i.e.
###
###      owl::command("set zsigproc /mit/kretch/bin/getzsig");
###
### will set the owl variable zsigproc.  Subroutines created with
### the names below will be executed at the specified times:
###
###     subroutine name    properties
###     ---------------    ----------
###     owl::startup()     run when owl first starts
###     owl::shutdown()    run when owl exits
###     owl::format_msg()  run when a new message arrives, the return
###                           value is used to display the message on the
###                           screen
###     owl::receive_msg() run when a message is received, and after
###	  	           it has been added to the message list
###
###
### The following variables will be set each time a message is recevied:
###
###     $owl::class, $owl::instance, $owl::recipient,
###     $owl::sender, $owl::opcode, $owl::zsig,
###     $owl::msg, $owl::time, $owl::host, @owl::fields, $owl::id
###    

# tokens for sepbar are:
#    .username = ping
#    >username = login
#    <username = logout
#    :username = personal message
#    M         = mail received
my @sepbartokens = ();


# adds a sepbartoken and also updates the appendtosepbar variable
sub sepbartokens_add {
    my ($token) = @_;
    $token =~ s/"//g;  # "
    unshift @sepbartokens, $token;
    pop @sepbartokens if (@sepbartokens > 80);
    sepbartokens_set();
}

# trims a sepbartoken from the list also updates the appendtosepbar variable
sub sepbartokens_trim {
    my ($token) = @_;
    @sepbartokens = map { if ($_ ne $token) { $_; } else { (); } } @sepbartokens;
    sepbartokens_set();
}

# trims a sepbartoken from the list also updates the appendtosepbar variable
sub sepbartokens_set {
    owl::command(sprintf "set -q appendtosepbar \"%s\"", (join " ", @sepbartokens));
}

sub owl::startup {
    owl::command("set -q logging off");
    owl::command("set -q zsigproc /home/nygren/bin/owlzsigs");
    owl::command("set -q startuplogin off");
    owl::command("set -q shutdownlogout off");
    #owl::command("set personalbell on");
    owl::command("set -q rxping on");
    owl::command("set -q typewinsize 5");
    if ($ENV{"DISPLAY"} eq ":0") {
      owl::command("set -q webbrowser galeon");	
    }
    owl::command("filter me recipient ".$ENV{"USER"});
    owl::command("filter help class help");
    owl::command("filter quiet not ( class ^greed|geek|help|login$ or instance white-magic )");    

    owl::command("alias z zwrite");
    owl::command("alias zc zwrite -c");

    # Send zephyrs with "C-c C-c".  
    # Note that this will cause "C-c" to not work...
    owl::command("bindkey editmulti \"C-c C-c\" command editmulti:done");

    # Make "d" ignore current movement direction
    owl::command("bindkey recv d command ( delete --no-move ; next --skip-deleted )");

    # Useful keys for reading and deleting by class/instance
    owl::command("bindkey recv M-k command ( smartnarrow ; delete view )");
    owl::command("bindkey recv M-l command ( expunge ; view all )");
    owl::command("bindkey recv M-K command ( smartnarrow ; delete view ; expunge ; view all )");

    # Make 'M-s' insert a <scritchscritchscritch> sequence with a random
    # number of scritches.
    owl::command(q(bindkey edit M-s command perl owl::command("edit:insert-text <".("scritch"x int(1+rand(5))).">")));

}

sub owl::shutdown {
#    not doing anything at the moment...
}

# run to format a message
sub owl::format_msg {
    my $out, $tmp;

    $owl::sender=~s/\@ATHENA\.MIT\.EDU$//;
    $owl::sender=~s/\@local-realm$//;

    if (uc($owl::opcode) eq "PING") {
	return("\@bold(PING) from \@bold($owl::sender)\n");
    } elsif (uc($owl::class) eq "LOGIN") {
	if (uc($owl::opcode) eq "USER_LOGIN") {
	    $out="\@bold(LOGIN)";
	} elsif (uc($owl::opcode) eq "USER_LOGOUT") {
	    $out="\@bold(LOGOUT)";
	} else {
	    $out="\@bold(UNKNOWN)";
	}
	$out.=" for \@bold($owl::sender) at $fields[0] on $fields[2]\n";
	return($out);
    } elsif (uc($owl::class) eq "MAIL" and uc($owl::instance) eq "INBOX") {
	$out = "\@bold(MAIL) ";
	if ($owl::msg =~ /^From:\s+(.+)\s*$/m) { $out .= "From $1 "; }
	if ($owl::msg =~ /^To:\s+(.+)\s*$/m) { $out .= "To $1 "; }
	if ($owl::msg =~ /^Subject:\s+(.+)\s*$/m) { $out .= "Subject $1 "; }
	return($out."\n");
    }

    $out = sprintf "[mit,%s,%s] / %s / %s", lc($owl::class), 
                   lc($owl::instance), $owl::time, lc($owl::host);
    if ($owl::opcode ne "") {$out.=" op:$owl::opcode";}
    $out.="\n";
    $out.= "  \@bold($owl::sender)> ";
    if ($owl::zsig ne "") {
	my $zsig = $owl::zsig;
	$zsig =~ s/(\n.*)+$/ [...]/;
	if (length($zsig)+5+length($owl::sender) > 70) {
	    $out.="# ...";
	} else {
	    $out.="# $zsig";
	}
    }
    $out.="\n";		

    # indent it
    $tmp=$owl::msg;
    $tmp=~s/^/    /g;
    $tmp=~s/\n/\n    /g;
    $out.=$tmp;

    # make personal messages bold
    if (uc($owl::class) eq "MESSAGE" &&
	uc($owl::instance) eq "PERSONAL") {
	$out="\@bold{".$out."}";
    }

    return($out."\n");
}

# run when a message is received, and after
# it has been added to the message list.
sub owl::receive_msg() {
    my $out, $tmp;

    $owl::sender=~s/\@ATHENA\.MIT\.EDU$//;
    $owl::sender=~s/\@local-realm$//;

    if (uc($owl::opcode) eq "PING") {
	sepbartokens_add(".$owl::sender");
        owl::command("delete -id $owl::id");
    } elsif (uc($owl::class) eq "LOGIN") {
        owl::command("delete -id $owl::id");
	if (uc($owl::opcode) eq "USER_LOGIN") {
	    sepbartokens_add(">$owl::sender");
	    sepbartokens_trim("<$owl::sender");
	} elsif (uc($owl::opcode) eq "USER_LOGOUT") {
	    sepbartokens_add("<$owl::sender");
	    sepbartokens_trim(">$owl::sender");
	} 
    } elsif (uc($owl::class) eq "MAIL") {
        owl::command("delete -id $owl::id");
	sepbartokens_add("M");
    }


    # make personal messages bold
    if (uc($owl::class) eq "MESSAGE" &&
	uc($owl::instance) eq "PERSONAL") {
	sepbartokens_trim(".$owl::sender");
	sepbartokens_add(":$owl::sender");
    }

    return 1;
}
