Changeset b6c067a for perlwrap.pm


Ignore:
Timestamp:
Feb 16, 2007, 1:37:44 AM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
6e6ded7
Parents:
3f21b8e
git-author:
Nelson Elhage <nelhage@mit.edu> (02/15/07 23:16:22)
git-committer:
Nelson Elhage <nelhage@mit.edu> (02/16/07 01:37:44)
Message:
Moving the default style into perl, and reorganizing things so we can
bootstrap the style into place in time.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perlwrap.pm

    r16138b0 rb6c067a  
    55#####################################################################
    66#####################################################################
     7# XXX NOTE: This file is sourced before almost any barnowl
     8# architecture is loaded. This means, for example, that it cannot
     9# execute any owl commands. Any code that needs to do so, should
     10# create a function wrapping it and push it onto @onStartSubs
     11
    712
    813use strict;
     
    293298#####################################################################
    294299################################################################################
    295 package owl;
     300package BarnOwl;
    296301
    297302################################################################################
     
    326331      BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
    327332  }
    328 
     333   
    329334  @BarnOwl::Hooks::onMainLoop = ();
    330335  @BarnOwl::Hooks::onStartSubs = ();
     
    335340      undef $@;
    336341      do $BarnOwl::configfile;
    337       owl::error("Error reloading $BarnOwl::configfile: $@") if $@;
     342      BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@;
    338343  }
    339344  BarnOwl::reload_hook(@_);
    340   package owl;
     345  package BarnOwl;
    341346}
    342347
     
    372377}
    373378
     379sub _load_owlconf {
     380    # Only do this the first time
     381    return if $BarnOwl::reload;
     382    # load the config  file
     383    if ( -r $BarnOwl::configfile ) {
     384        undef $@;
     385        do $BarnOwl::configfile;
     386        die $@ if $@;
     387    }
     388}
     389
     390push @BarnOwl::Hooks::onStartSubs, \&_load_owlconf;
     391
    374392package BarnOwl::Hooks;
    375393
     
    453471    return runHook_accumulate(\@onGetBuddyList);
    454472}
     473
     474################################################################################
     475# Built-in perl styles
     476################################################################################
     477package BarnOwl::Style::Default;
     478################################################################################
     479# Branching point for various formatting functions in this style.
     480################################################################################
     481sub format_message($)
     482{
     483    my $m = shift;
     484
     485    if ( $m->is_zephyr ) {
     486        return format_zephyr($m);
     487    }
     488    elsif ( $m->is_admin ) {
     489        return "\@bold(OWL ADMIN)\n" . indentBody($m);
     490    }
     491    elsif ( $m->is_aim ) {
     492        return format_aim($m);
     493    }
     494    elsif ( lc( $m->type ) eq 'loopback' ) {
     495        return format_loopback($m);
     496    }
     497    else {
     498        return "Unexpected message type.";
     499    }
     500}
     501
     502BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
     503
     504################################################################################
     505sub format_zephyr($)
     506{
     507    my $m = shift;
     508
     509    # Extract time from message
     510    my ($time) = $m->time =~ /(\d\d:\d\d)/;
     511
     512    # Deal with PING messages, assuming owl's rxping variable is true.
     513    if ( $m->is_ping && $m->recipient ne "" ) {
     514        return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" );
     515    }
     516
     517    # Deal with login/logout messages
     518    elsif ( $m->is_loginout ) {
     519        return sprintf(
     520            '@b<%s%s> for @b(%s) at %s %s %s',
     521            uc( $m->login ),
     522
     523            # This is a hack, owl does not export "pseudo"-ness
     524            ( $m->zsig ) ? "" : " (PSEUDO)",
     525            $m->pretty_sender,
     526            lc( $m->host ),
     527            $m->login_tty,
     528            $time
     529        );
     530    }
     531
     532    # Deal with outbound zephyrs (personal, we don't see outbound non-personal)
     533    elsif ( lc( $m->direction ) eq 'out' ) {
     534        my $user = $m->recipient;
     535        $user =~ s/\@ATHENA[.]MIT[.]EDU$//;
     536
     537        my $zsig = $m->zsig;
     538        $zsig =~ s/\n.*$//s;
     539
     540        return sprintf( "Zephyr sent to %s  %s  (Zsig: %s)\n%s",
     541            $user, $time, $zsig, indentBody($m) );
     542    }
     543
     544    # Deal with everything else
     545    else {
     546        my $zsig = $m->zsig;
     547        $zsig =~ s/\n.*$//s;
     548
     549        my $msg = sprintf(
     550            "%s / %s / \@b<%s>%s  %s    (%s)\n%s",
     551            $m->class, $m->instance, $m->pretty_sender,
     552            ( $m->opcode ? " [@{[$m->opcode]}]" : "" ),
     553            $time, $zsig, indentBody($m)
     554        );
     555        return BarnOwl::Style::boldify($msg) if ( $m->is_private );
     556        return $msg;
     557    }
     558}
     559
     560
     561sub format_aim($)
     562{
     563    my $m = shift;
     564
     565    # Extract time from message
     566    my ($time) = $m->time =~ /(\d\d:\d\d)/;
     567
     568    # Deal with login/logout messages
     569    if ( $m->is_loginout ) {
     570        return
     571          sprintf( "\@b(AIM %s) for %s %s",
     572                   uc( $m->login ),
     573                   $m->sender,
     574                   $time );
     575    }
     576    elsif ( lc( $m->direction ) eq 'out' ) {
     577        return sprintf( "AIM sent to %s  %s\n%s",
     578                        $m->recipient,
     579                        $time,
     580                        indentBody($m) );
     581    }
     582    else {
     583        return sprintf( "\@b(AIM from %s)  %s\n%s",
     584                        $m->sender,
     585                        $time,
     586                        BarnOwl::Style::boldify( indentBody($m) ) );
     587    }
     588}
     589
     590
     591sub format_loopback($)
     592{
     593    my $m = shift;
     594
     595    # Extract time from message
     596    my ($time) = $m->time =~ /(\d\d:\d\d)/;
     597
     598    return sprintf( "loopback from: %s to: %s  %s\n%s",
     599        $m->sender, $m->recipient, $time, indentBody($m) );
     600}
     601
     602
     603sub indentBody($)
     604{
     605    my $m = shift;
     606   
     607    my $body = $m->body;
     608    # replace newline followed by anything with
     609    # newline plus four spaces and that thing.
     610    $body =~ s/\n(.)/\n    $1/g;
     611
     612    return "    ".$body;
     613}
     614
     615
     616package BarnOwl::Style;
     617
     618# This takes a zephyr to be displayed and modifies it to be displayed
     619# entirely in bold.
     620sub boldify($)
     621{
     622    local $_ = shift;
     623    if ( !(/\)/) ) {
     624        return '@b(' . $_ . ')';
     625    } elsif ( !(/\>/) ) {
     626        return '@b<' . $_ . '>';
     627    } elsif ( !(/\}/) ) {
     628        return '@b{' . $_ . '}';
     629    } elsif ( !(/\]/) ) {
     630        return '@b[' . $_ . ']';
     631    } else {
     632        my $txt = "\@b($_";
     633        $txt =~ s/\)/\)\@b\[\)\]\@b\(/g;
     634        return $txt . ')';
     635    }
     636}
     637
    455638
    456639# switch to package main when we're done
     
    468651}
    469652
    470 # load the config  file
    471 if (-r $BarnOwl::configfile) {
    472     undef $@;
    473     do $BarnOwl::configfile;
    474     die $@ if $@;
    475 }
    476 
    4776531;
Note: See TracChangeset for help on using the changeset viewer.