[1043] in BarnOwl Developers
[D-O-H] r1026 - trunk/owl
daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:12:25 2009
Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@MIT.EDU
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Tue, 29 Apr 2008 01:21:13 -0400 (EDT)
Author: nelhage
Date: 2008-04-29 01:21:13 -0400 (Tue, 29 Apr 2008)
New Revision: 1026
Removed:
trunk/owl/stylefunc.c
Modified:
trunk/owl/Makefile.in
trunk/owl/commands.c
trunk/owl/owl.c
trunk/owl/perlglue.xs
trunk/owl/perlwrap.pm
trunk/owl/style.c
Log:
Initial step of moving styles from the current mishmash of different
options to a unified object interface.
No backwards-compatibility support yet.
Modified: trunk/owl/Makefile.in
===================================================================
--- trunk/owl/Makefile.in 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/Makefile.in 2008-04-29 05:21:13 UTC (rev 1026)
@@ -24,7 +24,7 @@
perlconfig.c keys.c functions.c zwrite.c viewwin.c help.c filter.c \
regex.c history.c view.c dict.c variable.c filterelement.c pair.c \
keypress.c keymap.c keybinding.c cmd.c context.c zcrypt.c \
- aim.c buddy.c buddylist.c timer.c style.c stylefunc.c errqueue.c \
+ aim.c buddy.c buddylist.c timer.c style.c errqueue.c \
zbuddylist.c muxevents.c popexec.c obarray.c select.c
OWL_SRC = owl.c
TESTER_SRC = tester.c
Modified: trunk/owl/commands.c
===================================================================
--- trunk/owl/commands.c 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/commands.c 2008-04-29 05:21:13 UTC (rev 1026)
@@ -101,14 +101,6 @@
"Use 'show keymaps' to see the existing keymaps.\n"
"Key sequences may be things like M-C-t or NPAGE.\n"),
- OWLCMD_ARGS("style", owl_command_style, OWL_CTX_ANY,
- "creates a new style",
- "style <name> perl <function_name>",
- "Creates a new style for formatting messages.\n"
- "A style named <name> will be created that will\n"
- "format messages using the perl function <function_name>.\n\n"
- "SEE ALSO: show styles, view -s, filter -s\n"),
-
OWLCMD_ARGS("zwrite", owl_command_zwrite, OWL_CTX_INTERACTIVE,
"send a zephyr",
"zwrite [-n] [-C] [-c class] [-i instance] [-r realm] [-O opcde] [<user> ...] [-m <message...>]",
@@ -1675,27 +1667,6 @@
return NULL;
}
-char *owl_command_style(int argc, char **argv, char *buff) {
- owl_style *s;
-
- /* Usage: style <name> perl <function> */
- if (argc != 4 || strcmp(argv[2], "perl")) {
- owl_function_makemsg("Usage: style <name> perl <function>");
- return NULL;
- }
- if (!owl_perlconfig_is_function(argv[3])) {
- owl_function_makemsg("Unable to create style '%s': no perl function '%s'",
- argv[1], argv[3]);
- return NULL;
- }
- s=owl_malloc(sizeof(owl_style));
- owl_style_create_perl(s, argv[1], argv[3], NULL);
- owl_global_add_style(&g, s);
-
- return NULL;
-}
-
-
void owl_command_quit()
{
owl_function_quit();
Modified: trunk/owl/owl.c
===================================================================
--- trunk/owl/owl.c 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/owl.c 2008-04-29 05:21:13 UTC (rev 1026)
@@ -264,13 +264,6 @@
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, "basic", &owl_stylefunc_basic, "Basic message formatting.");
- owl_global_add_style(&g, s);
-
/* setup the default filters */
/* the personal filter will need to change again when AIM chat's are
* included. Also, there should be an %aimme% */
Modified: trunk/owl/perlglue.xs
===================================================================
--- trunk/owl/perlglue.xs 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/perlglue.xs 2008-04-29 05:21:13 UTC (rev 1026)
@@ -255,10 +255,9 @@
}
void
-_create_style(name, function, description)
+_create_style(name, object)
char *name
- char *function
- char *description
+ SV *object
PREINIT:
/* This is to allow us to bootstrap the default style before the
command architecture has been initialized */
@@ -266,7 +265,7 @@
CODE:
{
s = owl_malloc(sizeof(owl_style));
- owl_style_create_perl(s, name, function, description);
+ owl_style_create_perl(s, name, object);
owl_global_add_style(&g, s);
}
Modified: trunk/owl/perlwrap.pm
===================================================================
--- trunk/owl/perlwrap.pm 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/perlwrap.pm 2008-04-29 05:21:13 UTC (rev 1026)
@@ -701,10 +701,10 @@
package BarnOwl;
if(*BarnOwl::format_msg{CODE}) {
# if the config defines a legacy formatting function, add 'perl' as a style
- BarnOwl::_create_style("perl", "BarnOwl::_format_msg_legacy_wrap",
- "User-defined perl style that calls BarnOwl::format_msg"
- . " with legacy global variable support");
- BarnOwl::set("-q default_style perl");
+ # BarnOwl::_create_style("perl", "BarnOwl::_format_msg_legacy_wrap",
+ # "User-defined perl style that calls BarnOwl::format_msg"
+ # . " with legacy global variable support");
+ # BarnOwl::set("-q default_style perl");
}
}
}
@@ -762,21 +762,24 @@
################################################################################
sub format_message($)
{
+ my $self = shift;
my $m = shift;
if ( $m->is_loginout) {
- return format_login($m);
+ return $self->format_login($m);
} elsif($m->is_ping && $m->is_personal) {
- return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" );
+ return $self->format_ping($m);
} elsif($m->is_admin) {
- return "\@bold(OWL ADMIN)\n" . indentBody($m);
+ return $self->format_admin($m);
} else {
- return format_chat($m);
+ return $self->format_chat($m);
}
}
-BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
+sub description {"Default style";}
+BarnOwl::_create_style("default", "BarnOwl::Style::Default");
+
################################################################################
sub time_hhmm {
@@ -786,6 +789,7 @@
}
sub format_login($) {
+ my $self = shift;
my $m = shift;
return sprintf(
'@b<%s%s> for @b(%s) (%s) %s',
@@ -797,7 +801,20 @@
);
}
+sub format_ping {
+ my $self = shift;
+ my $m = shift;
+ return "\@b(PING) from \@b(" . $m->pretty_sender . ")\n";
+}
+
+sub format_admin {
+ my $self = shift;
+ my $m = shift;
+ return "\@bold(OWL ADMIN)\n" . indentBody($m);
+}
+
sub format_chat($) {
+ my $self = shift;
my $m = shift;
my $header;
if ( $m->is_personal ) {
@@ -845,36 +862,45 @@
return " ".$body;
}
+package BarnOwl::Style::Basic;
+
+our @ISA=qw(BarnOwl::Style::Default);
+
+sub description {"Compatability alias for the default style";}
+
+BarnOwl::_create_style("basic", "BarnOwl::Style::Basic");
+
package BarnOwl::Style::OneLine;
################################################################################
# Branching point for various formatting functions in this style.
################################################################################
use constant BASE_FORMAT => '%s %-13.13s %-11.11s %-12.12s ';
sub format_message($) {
+ my $self = shift;
my $m = shift;
-# if ( $m->is_zephyr ) {
-# return format_zephyr($m);
-# }
if ( $m->is_loginout ) {
- return format_login($m);
+ return $self->format_login($m);
}
elsif ( $m->is_ping) {
- return format_ping($m);
+ return $self->format_ping($m);
}
elsif ( $m->is_admin || $m->is_loopback) {
- return format_local($m);
+ return $self->format_local($m);
}
else {
- return format_chat($m);
+ return $self->format_chat($m);
}
}
-BarnOwl::_create_style("oneline", "BarnOwl::Style::OneLine::format_message", "Formats for one-line-per-message");
+sub description {"Formats for one-line-per-message"}
+BarnOwl::_create_style("oneline", "BarnOwl::Style::OneLine");
+
################################################################################
sub format_login($) {
+ my $self = shift;
my $m = shift;
return sprintf(
BASE_FORMAT,
@@ -897,6 +923,7 @@
sub format_chat($)
{
+ my $self = shift;
my $m = shift;
my $dir = lc($m->{direction});
my $dirsym = '-';
@@ -937,6 +964,7 @@
# Format locally generated messages
sub format_local($)
{
+ my $self = shift;
my $m = shift;
my $type = uc($m->{type});
my $line = sprintf(BASE_FORMAT, '<', $type, '', '');
Modified: trunk/owl/style.c
===================================================================
--- trunk/owl/style.c 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/style.c 2008-04-29 05:21:13 UTC (rev 1026)
@@ -1,3 +1,4 @@
+#define OWL_PERL
#include "owl.h"
static const char fileIdent[] = "$Id$";
@@ -2,29 +3,8 @@
-void owl_style_create_internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description)
+void owl_style_create_perl(owl_style *s, char *name, SV *obj)
{
- s->type=OWL_STYLE_TYPE_INTERNAL;
s->name=owl_strdup(name);
- if (description) {
- s->description=owl_strdup(description);
- } else {
- s->description=owl_sprintf("Owl internal style %s", name);
- }
- s->perlfuncname=NULL;
- s->formatfunc=formatfunc;
+ s->perlobj = SvREFCNT_inc(obj);
}
-void owl_style_create_perl(owl_style *s, char *name, char *perlfuncname, char *description)
-{
- s->type=OWL_STYLE_TYPE_PERL;
- s->name=owl_strdup(name);
- s->perlfuncname=owl_strdup(perlfuncname);
- if (description) {
- s->description=owl_strdup(description);
- } else {
- s->description=owl_sprintf("User-defined perl style that calls %s",
- perlfuncname);
- }
- s->formatfunc=NULL;
-}
-
int owl_style_matches_name(owl_style *s, char *name)
@@ -42,7 +22,19 @@
char *owl_style_get_description(owl_style *s)
{
- return(s->description);
+ SV *sv = NULL;
+ OWL_PERL_CALL_METHOD(s->perlobj,
+ "description",
+ /* no args */,
+ "Error in style_get_description: %s",
+ 0,
+ sv = SvREFCNT_inc(POPs);
+ );
+ if(sv) {
+ return SvPV_nolen(sv_2mortal(sv));
+ } else {
+ return "[error getting description]";
+ }
}
/* Use style 's' to format message 'm' into fmtext 'fm'.
@@ -50,55 +42,52 @@
*/
void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m)
{
- if (s->type==OWL_STYLE_TYPE_INTERNAL) {
- (* s->formatfunc)(fm, m);
- } else if (s->type==OWL_STYLE_TYPE_PERL) {
- char *body, *indent;
- int curlen;
+ char *body, *indent;
+ int curlen;
- /* run the perl function */
- body=owl_perlconfig_getmsg(m, 1, s->perlfuncname);
- if (!strcmp(body, "")) {
- owl_free(body);
- body=owl_strdup("<unformatted message>");
- }
-
- /* indent and ensure ends with a newline */
- indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10);
- owl_text_indent(indent, body, OWL_TAB);
- curlen = strlen(indent);
- if (curlen==0 || indent[curlen-1] != '\n') {
- indent[curlen] = '\n';
- indent[curlen+1] = '\0';
- }
+ SV *sv = NULL;
+
+ /* Call the perl object */
+ OWL_PERL_CALL_METHOD(s->perlobj,
+ "format_message",
+ XPUSHs(owl_perlconfig_message2hashref(m));,
+ "Error in format_message: %s",
+ 0,
+ sv = SvREFCNT_inc(POPs);
+ );
- /* fmtext_append. This needs to change */
- owl_fmtext_append_ztext(fm, indent);
-
- owl_free(indent);
- owl_free(body);
+ if(sv) {
+ body = SvPV_nolen(sv);
+ } else {
+ body = "<unformatted message>";
}
+
+ /* indent and ensure ends with a newline */
+ indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10);
+ owl_text_indent(indent, body, OWL_TAB);
+ curlen = strlen(indent);
+ if (curlen==0 || indent[curlen-1] != '\n') {
+ indent[curlen] = '\n';
+ indent[curlen+1] = '\0';
+ }
+
+ /* fmtext_append. This needs to change */
+ owl_fmtext_append_ztext(fm, indent);
+
+ owl_free(indent);
+ if(sv)
+ SvREFCNT_dec(body);
}
int owl_style_validate(owl_style *s) {
- if (!s) {
+ if (!s || !s->perlobj || !SvOK(s->perlobj)) {
return -1;
- } else if (s->type==OWL_STYLE_TYPE_INTERNAL) {
- return 0;
- } else if (s->type==OWL_STYLE_TYPE_PERL
- && s->perlfuncname
- && owl_perlconfig_is_function(s->perlfuncname)) {
- return 0;
- } else {
- return -1;
}
+ return 0;
}
void owl_style_free(owl_style *s)
{
if (s->name) owl_free(s->name);
- if (s->description) owl_free(s->description);
- if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) {
- owl_free(s->perlfuncname);
- }
+ SvREFCNT_dec(s->perlobj);
}
Deleted: trunk/owl/stylefunc.c
===================================================================
--- trunk/owl/stylefunc.c 2008-04-29 05:21:12 UTC (rev 1025)
+++ trunk/owl/stylefunc.c 2008-04-29 05:21:13 UTC (rev 1026)
@@ -1,221 +0,0 @@
-#include "owl.h"
-
-static const char fileIdent[] = "$Id$";
-
-/* In all of these functions, 'fm' is expected to already be
- * initialized.
- */
-
-void owl_style_basic_format_body(owl_fmtext *fm, owl_message *m) {
- char *indent, *body;
- owl_filter *f;
- int wrap = 0;
-
- /* get the body */
- body=owl_strdup(owl_message_get_body(m));
-
- f = owl_global_get_filter(&g, "wordwrap");
- if(f && owl_filter_message_match(f, m))
- wrap = 1;
-
- if(wrap) {
- int cols, i, width, word;
- char *tab, *tok, *ws = " \t\n\r";
- cols = owl_global_get_cols(&g) - OWL_MSGTAB - 1;
-
- tab = owl_malloc(OWL_MSGTAB+1);
- for(i = 0; i < OWL_MSGTAB; i++) {
- tab[i] = ' ';
- }
- tab[OWL_MSGTAB] = 0;
-
- tok = strtok(body, ws);
- tab[OWL_MSGTAB-1] = 0;
- owl_fmtext_append_normal(fm, tab);
- tab[OWL_MSGTAB-1] = ' ';
- width = 0;
-
- while(tok) {
- word = strlen(tok);
- if(word + width + 1 < cols) {
- owl_fmtext_append_normal(fm, " ");
- owl_fmtext_append_normal(fm, tok);
- width += word + 1;
- } else {
- owl_fmtext_append_normal(fm, "\n");
- owl_fmtext_append_normal(fm, tab);
- owl_fmtext_append_normal(fm, tok);
- width = word;
- }
- tok = strtok(NULL, ws);
- }
- owl_fmtext_append_normal(fm, "\n");
-
- owl_free(tab);
- } else {
- /* do the indenting into indent */
- indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10);
- owl_text_indent(indent, body, OWL_MSGTAB);
- owl_fmtext_append_ztext(fm, indent);
- if(body[strlen(body)-1] != '\n')
- owl_fmtext_append_ztext(fm, "\n");
- owl_free(indent);
- }
-
- owl_free(body);
-}
-
-void owl_stylefunc_basic(owl_fmtext *fm, owl_message *m)
-{
-#ifdef HAVE_LIBZEPHYR
- char *ptr, *zsigbuff, frombuff[LINE];
- ZNotice_t *n;
-#endif
-
- if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
-#ifdef HAVE_LIBZEPHYR
- n=owl_message_get_notice(m);
-
- /* edit the from addr for printing */
- strcpy(frombuff, owl_message_get_sender(m));
- ptr=strchr(frombuff, '@');
- if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
- *ptr='\0';
- }
-
- /* set the message for printing */
- owl_fmtext_append_normal(fm, OWL_TABSTR);
-
- if (owl_message_is_ping(m)) {
- owl_fmtext_append_bold(fm, "PING");
- owl_fmtext_append_normal(fm, " from ");
- owl_fmtext_append_bold(fm, frombuff);
- owl_fmtext_append_normal(fm, "\n");
- } else if (owl_message_is_loginout(m)) {
- char *host, *tty;
-
- host=owl_message_get_attribute_value(m, "loginhost");
- tty=owl_message_get_attribute_value(m, "logintty");
-
- if (owl_message_is_login(m)) {
- owl_fmtext_append_bold(fm, "LOGIN");
- } else if (owl_message_is_logout(m)) {
- owl_fmtext_append_bold(fm, "LOGOUT");
- }
- if (owl_message_is_pseudo(m)) {
- owl_fmtext_append_bold(fm, " (PSEUDO)");
- }
- owl_fmtext_append_normal(fm, " for ");
- ptr=short_zuser(owl_message_get_instance(m));
- owl_fmtext_append_bold(fm, ptr);
- owl_free(ptr);
- owl_fmtext_append_normal(fm, " at ");
- owl_fmtext_append_normal(fm, host ? host : "");
- owl_fmtext_append_normal(fm, " ");
- owl_fmtext_append_normal(fm, tty ? tty : "");
- owl_fmtext_append_normal(fm, "\n");
- } else {
- owl_fmtext_append_normal(fm, "From: ");
- if (strcasecmp(owl_message_get_class(m), "message")) {
- owl_fmtext_append_normal(fm, "Class ");
- owl_fmtext_append_normal(fm, owl_message_get_class(m));
- owl_fmtext_append_normal(fm, " / Instance ");
- owl_fmtext_append_normal(fm, owl_message_get_instance(m));
- owl_fmtext_append_normal(fm, " / ");
- }
- owl_fmtext_append_normal(fm, frombuff);
- if (strcasecmp(owl_message_get_realm(m), owl_zephyr_get_realm())) {
- owl_fmtext_append_normal(fm, " {");
- owl_fmtext_append_normal(fm, owl_message_get_realm(m));
- owl_fmtext_append_normal(fm, "} ");
- }
-
- /* stick on the zsig */
- zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
- owl_message_pretty_zsig(m, zsigbuff);
- owl_fmtext_append_normal(fm, " (");
- owl_fmtext_append_ztext(fm, zsigbuff);
- owl_fmtext_append_normal(fm, ")");
- owl_fmtext_append_normal(fm, "\n");
- owl_free(zsigbuff);
-
- /* then the indented message */
- owl_style_basic_format_body(fm, m);
-
- /* make personal messages bold for smaat users */
- if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
- if (owl_message_is_personal(m)) {
- owl_fmtext_addattr(fm, OWL_FMTEXT_ATTR_BOLD);
- }
- }
- }
-
-#endif
- } else if (owl_message_is_type_zephyr(m) && owl_message_is_direction_out(m)) {
- char *zsigbuff, *foo;
- owl_fmtext_append_normal(fm, OWL_TABSTR);
- owl_fmtext_append_normal(fm, "To: ");
- foo=short_zuser(owl_message_get_recipient(m));
- owl_fmtext_append_normal(fm, foo);
- owl_free(foo);
- owl_fmtext_append_normal(fm, " (Zsig: ");
-
- zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30);
- owl_message_pretty_zsig(m, zsigbuff);
- owl_fmtext_append_ztext(fm, zsigbuff);
- owl_free(zsigbuff);
-
- owl_fmtext_append_normal(fm, ")");
- owl_fmtext_append_normal(fm, "\n");
- owl_style_basic_format_body(fm, m);
- } else if (owl_message_is_type_aim(m)) {
- if (owl_message_is_loginout(m)) {
- owl_fmtext_append_normal(fm, OWL_TABSTR);
- if (owl_message_is_login(m)) {
- owl_fmtext_append_bold(fm, "AIM LOGIN");
- } else {
- owl_fmtext_append_bold(fm, "AIM LOGOUT");
- }
- owl_fmtext_append_normal(fm, " for ");
- owl_fmtext_append_normal(fm, owl_message_get_sender(m));
- owl_fmtext_append_normal(fm, "\n");
- } else if (owl_message_is_direction_in(m)) {
- owl_fmtext_append_bold(fm, OWL_TABSTR);
- owl_fmtext_append_bold(fm, "AIM from ");
- owl_fmtext_append_bold(fm, owl_message_get_sender(m));
- owl_fmtext_append_bold(fm, "\n");
- owl_style_basic_format_body(fm, m);
- } else if (owl_message_is_direction_out(m)) {
- owl_fmtext_append_normal(fm, OWL_TABSTR);
- owl_fmtext_append_normal(fm, "AIM sent to ");
- owl_fmtext_append_normal(fm, owl_message_get_recipient(m));
- owl_fmtext_append_normal(fm, "\n");
- owl_style_basic_format_body(fm, m);
- }
- } else if (owl_message_is_type_admin(m)) {
- char *text, *header;
-
- text=owl_message_get_body(m);
- header=owl_message_get_attribute_value(m, "adminheader");
-
- owl_fmtext_append_normal(fm, OWL_TABSTR);
- owl_fmtext_append_bold(fm, "OWL ADMIN ");
- owl_fmtext_append_ztext(fm, header);
- owl_fmtext_append_normal(fm, "\n");
- owl_style_basic_format_body(fm, m);
- } else {
- char *header;
-
- header=owl_sprintf("%s from: %s to: %s",
- owl_message_get_type(m),
- owl_message_get_sender(m),
- owl_message_get_recipient(m));
-
- owl_fmtext_append_normal(fm, OWL_TABSTR);
- owl_fmtext_append_normal(fm, header);
- owl_fmtext_append_normal(fm, "\n");
- owl_style_basic_format_body(fm, m);
-
- owl_free(header);
- }
-}