[490] in BarnOwl Developers
[D-O-H] r612 - in trunk: . owl
daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:06:44 2009
Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
Date: Fri, 16 Feb 2007 01:37:45 -0500
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-To: dirty-owl-hackers@MIT.EDU
Author: nelhage
Date: 2007-02-16 01:37:44 -0500 (Fri, 16 Feb 2007)
New Revision: 612
Modified:
trunk/
trunk/owl/owl.c
trunk/owl/perlconfig.c
trunk/owl/perlglue.xs
trunk/owl/perlwrap.pm
Log:
r18948@phanatique: nelhage | 2007-02-15 23:16:22 -0500
Moving the default style into perl, and reorganizing things so we can
bootstrap the style into place in time.
Property changes on: trunk
___________________________________________________________________
Name: svk:merge
- bb873fd7-8e23-0410-944a-99ec44c633eb:/branches/owl/filter-rewrite:15925
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/trunk:18636
+ bb873fd7-8e23-0410-944a-99ec44c633eb:/branches/owl/filter-rewrite:15925
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/trunk:18948
Modified: trunk/owl/owl.c
===================================================================
--- trunk/owl/owl.c 2007-02-15 23:21:13 UTC (rev 611)
+++ trunk/owl/owl.c 2007-02-16 06:37:44 UTC (rev 612)
@@ -226,11 +226,22 @@
owl_global_set_tty(&g, owl_util_get_default_tty());
}
+ /* Initialize perl */
+ owl_function_debugmsg("startup: processing config file");
+ owl_context_set_readconfig(owl_global_get_context(&g));
+ perlerr=owl_perlconfig_initperl(configfile);
+ if (perlerr) {
+ endwin();
+ owl_function_error("Internal perl error: %s\n", perlerr);
+ fprintf(stderr, "Internal perl error: %s\n", perlerr);
+ fflush(stderr);
+ printf("Internal perl error: %s\n", perlerr);
+ fflush(stdout);
+ exit(1);
+ }
+
/* setup the built-in styles */
owl_function_debugmsg("startup: creating built-in styles");
- s=owl_malloc(sizeof(owl_style));
- owl_style_create_internal(s, "default", &owl_stylefunc_default, "Default message formatting");
- owl_global_add_style(&g, s);
s=owl_malloc(sizeof(owl_style));
owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting.");
@@ -304,20 +315,6 @@
owl_function_debugmsg("startup: doing AIM initialization");
owl_aim_init();
- /* read the config file */
- owl_function_debugmsg("startup: processing config file");
- owl_context_set_readconfig(owl_global_get_context(&g));
- perlerr=owl_perlconfig_readconfig(configfile);
- if (perlerr) {
- endwin();
- owl_function_error("Error parsing configfile: %s\n", perlerr);
- fprintf(stderr, "\nError parsing configfile: %s\n", perlerr);
- fflush(stderr);
- printf("\nError parsing configfile: %s\n", perlerr);
- fflush(stdout);
- exit(1);
- }
-
/* if the config defines a formatting function, add 'perl' as a style */
if (owl_global_is_config_format(&g)) {
owl_function_debugmsg("Found perl formatting");
Modified: trunk/owl/perlconfig.c
===================================================================
--- trunk/owl/perlconfig.c 2007-02-15 23:21:13 UTC (rev 611)
+++ trunk/owl/perlconfig.c 2007-02-16 06:37:44 UTC (rev 612)
@@ -269,14 +269,13 @@
}
-char *owl_perlconfig_readconfig(char * file)
+char *owl_perlconfig_initperl(char * file)
{
int ret;
PerlInterpreter *p;
char *err;
char *args[4] = {"", "-e", "0;", NULL};
-
/* create and initialize interpreter */
p=perl_alloc();
owl_global_set_perlinterp(&g, (void*)p);
Modified: trunk/owl/perlglue.xs
===================================================================
--- trunk/owl/perlglue.xs 2007-02-15 23:21:13 UTC (rev 611)
+++ trunk/owl/perlglue.xs 2007-02-16 06:37:44 UTC (rev 612)
@@ -245,3 +245,19 @@
{
owl_function_error("%s", text);
}
+
+void
+_create_style(name, function, description)
+ char *name
+ char *function
+ char *description
+ PREINIT:
+ /* This is to allow us to bootstrap the default style before the
+ command architecture has been initialized */
+ owl_style *s;
+ CODE:
+ {
+ s = owl_malloc(sizeof(owl_style));
+ owl_style_create_perl(s, name, function, description);
+ owl_global_add_style(&g, s);
+ }
Modified: trunk/owl/perlwrap.pm
===================================================================
--- trunk/owl/perlwrap.pm 2007-02-15 23:21:13 UTC (rev 611)
+++ trunk/owl/perlwrap.pm 2007-02-16 06:37:44 UTC (rev 612)
@@ -4,7 +4,12 @@
#
#####################################################################
#####################################################################
+# XXX NOTE: This file is sourced before almost any barnowl
+# architecture is loaded. This means, for example, that it cannot
+# execute any owl commands. Any code that needs to do so, should
+# create a function wrapping it and push it onto @onStartSubs
+
use strict;
use warnings;
@@ -292,7 +297,7 @@
#####################################################################
#####################################################################
################################################################################
-package owl;
+package BarnOwl;
################################################################################
# Mainloop hook
@@ -325,7 +330,7 @@
local $reload = 1;
BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
}
-
+
@BarnOwl::Hooks::onMainLoop = ();
@BarnOwl::Hooks::onStartSubs = ();
@@ -334,10 +339,10 @@
if (-r $BarnOwl::configfile) {
undef $@;
do $BarnOwl::configfile;
- owl::error("Error reloading $BarnOwl::configfile: $@") if $@;
+ BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@;
}
BarnOwl::reload_hook(@_);
- package owl;
+ package BarnOwl;
}
sub reload_init ()
@@ -371,6 +376,19 @@
}
}
+sub _load_owlconf {
+ # Only do this the first time
+ return if $BarnOwl::reload;
+ # load the config file
+ if ( -r $BarnOwl::configfile ) {
+ undef $@;
+ do $BarnOwl::configfile;
+ die $@ if $@;
+ }
+}
+
+push @BarnOwl::Hooks::onStartSubs, \&_load_owlconf;
+
package BarnOwl::Hooks;
# Arrays of subrefs to be called at specific times.
@@ -453,6 +471,171 @@
return runHook_accumulate(\@onGetBuddyList);
}
+################################################################################
+# Built-in perl styles
+################################################################################
+package BarnOwl::Style::Default;
+################################################################################
+# Branching point for various formatting functions in this style.
+################################################################################
+sub format_message($)
+{
+ my $m = shift;
+
+ if ( $m->is_zephyr ) {
+ return format_zephyr($m);
+ }
+ elsif ( $m->is_admin ) {
+ return "\@bold(OWL ADMIN)\n" . indentBody($m);
+ }
+ elsif ( $m->is_aim ) {
+ return format_aim($m);
+ }
+ elsif ( lc( $m->type ) eq 'loopback' ) {
+ return format_loopback($m);
+ }
+ else {
+ return "Unexpected message type.";
+ }
+}
+
+BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
+
+################################################################################
+sub format_zephyr($)
+{
+ my $m = shift;
+
+ # Extract time from message
+ my ($time) = $m->time =~ /(\d\d:\d\d)/;
+
+ # Deal with PING messages, assuming owl's rxping variable is true.
+ if ( $m->is_ping && $m->recipient ne "" ) {
+ return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" );
+ }
+
+ # Deal with login/logout messages
+ elsif ( $m->is_loginout ) {
+ return sprintf(
+ '@b<%s%s> for @b(%s) at %s %s %s',
+ uc( $m->login ),
+
+ # This is a hack, owl does not export "pseudo"-ness
+ ( $m->zsig ) ? "" : " (PSEUDO)",
+ $m->pretty_sender,
+ lc( $m->host ),
+ $m->login_tty,
+ $time
+ );
+ }
+
+ # Deal with outbound zephyrs (personal, we don't see outbound non-personal)
+ elsif ( lc( $m->direction ) eq 'out' ) {
+ my $user = $m->recipient;
+ $user =~ s/\@ATHENA[.]MIT[.]EDU$//;
+
+ my $zsig = $m->zsig;
+ $zsig =~ s/\n.*$//s;
+
+ return sprintf( "Zephyr sent to %s %s (Zsig: %s)\n%s",
+ $user, $time, $zsig, indentBody($m) );
+ }
+
+ # Deal with everything else
+ else {
+ my $zsig = $m->zsig;
+ $zsig =~ s/\n.*$//s;
+
+ my $msg = sprintf(
+ "%s / %s / \@b<%s>%s %s (%s)\n%s",
+ $m->class, $m->instance, $m->pretty_sender,
+ ( $m->opcode ? " [@{[$m->opcode]}]" : "" ),
+ $time, $zsig, indentBody($m)
+ );
+ return BarnOwl::Style::boldify($msg) if ( $m->is_private );
+ return $msg;
+ }
+}
+
+
+sub format_aim($)
+{
+ my $m = shift;
+
+ # Extract time from message
+ my ($time) = $m->time =~ /(\d\d:\d\d)/;
+
+ # Deal with login/logout messages
+ if ( $m->is_loginout ) {
+ return
+ sprintf( "\@b(AIM %s) for %s %s",
+ uc( $m->login ),
+ $m->sender,
+ $time );
+ }
+ elsif ( lc( $m->direction ) eq 'out' ) {
+ return sprintf( "AIM sent to %s %s\n%s",
+ $m->recipient,
+ $time,
+ indentBody($m) );
+ }
+ else {
+ return sprintf( "\@b(AIM from %s) %s\n%s",
+ $m->sender,
+ $time,
+ BarnOwl::Style::boldify( indentBody($m) ) );
+ }
+}
+
+
+sub format_loopback($)
+{
+ my $m = shift;
+
+ # Extract time from message
+ my ($time) = $m->time =~ /(\d\d:\d\d)/;
+
+ return sprintf( "loopback from: %s to: %s %s\n%s",
+ $m->sender, $m->recipient, $time, indentBody($m) );
+}
+
+
+sub indentBody($)
+{
+ my $m = shift;
+
+ my $body = $m->body;
+ # replace newline followed by anything with
+ # newline plus four spaces and that thing.
+ $body =~ s/\n(.)/\n $1/g;
+
+ return " ".$body;
+}
+
+
+package BarnOwl::Style;
+
+# This takes a zephyr to be displayed and modifies it to be displayed
+# entirely in bold.
+sub boldify($)
+{
+ local $_ = shift;
+ if ( !(/\)/) ) {
+ return '@b(' . $_ . ')';
+ } elsif ( !(/\>/) ) {
+ return '@b<' . $_ . '>';
+ } elsif ( !(/\}/) ) {
+ return '@b{' . $_ . '}';
+ } elsif ( !(/\]/) ) {
+ return '@b[' . $_ . ']';
+ } else {
+ my $txt = "\@b($_";
+ $txt =~ s/\)/\)\@b\[\)\]\@b\(/g;
+ return $txt . ')';
+ }
+}
+
+
# switch to package main when we're done
package main;
# alias the hooks
@@ -467,11 +650,4 @@
}
}
-# load the config file
-if (-r $BarnOwl::configfile) {
- undef $@;
- do $BarnOwl::configfile;
- die $@ if $@;
-}
-
1;