[f1e629d] | 1 | # $Id$ |
---|
| 2 | # |
---|
| 3 | # This is all linked into the binary and evaluated when perl starts up... |
---|
| 4 | # |
---|
| 5 | ##################################################################### |
---|
| 6 | ##################################################################### |
---|
[b6c067a] | 7 | # XXX NOTE: This file is sourced before almost any barnowl |
---|
| 8 | # architecture is loaded. This means, for example, that it cannot |
---|
[0337203] | 9 | # execute any owl commands. Any code that needs to do so should live |
---|
| 10 | # in BarnOwl::Hooks::_startup |
---|
[f1e629d] | 11 | |
---|
[c681337] | 12 | use strict; |
---|
| 13 | use warnings; |
---|
| 14 | |
---|
[8203afd] | 15 | package BarnOwl; |
---|
[f1e629d] | 16 | |
---|
[74fc22a] | 17 | =head1 NAME |
---|
| 18 | |
---|
| 19 | BarnOwl |
---|
| 20 | |
---|
| 21 | =head1 DESCRIPTION |
---|
| 22 | |
---|
| 23 | The BarnOwl module contains the core of BarnOwl's perl |
---|
| 24 | bindings. Source in this module is also run at startup to bootstrap |
---|
| 25 | barnowl by defining things like the default style. |
---|
| 26 | |
---|
| 27 | =for NOTE |
---|
| 28 | These following functions are defined in perlglue.xs. Keep the |
---|
| 29 | documentation here in sync with the user-visible commands defined |
---|
| 30 | there! |
---|
| 31 | |
---|
| 32 | =head2 command STRING |
---|
| 33 | |
---|
| 34 | Executes a BarnOwl command in the same manner as if the user had |
---|
| 35 | executed it at the BarnOwl command prompt. If the command returns a |
---|
| 36 | value, return it as a string, otherwise return undef. |
---|
| 37 | |
---|
| 38 | =head2 getcurmsg |
---|
| 39 | |
---|
| 40 | Returns the current message as a C<BarnOwl::Message> subclass, or |
---|
| 41 | undef if there is no message selected |
---|
| 42 | |
---|
| 43 | =head2 getnumcols |
---|
| 44 | |
---|
| 45 | Returns the width of the display window BarnOwl is currently using |
---|
| 46 | |
---|
| 47 | =head2 getidletime |
---|
| 48 | |
---|
| 49 | Returns the length of time since the user has pressed a key, in |
---|
| 50 | seconds. |
---|
| 51 | |
---|
| 52 | =head2 zephyr_getrealm |
---|
| 53 | |
---|
| 54 | Returns the zephyr realm barnowl is running in |
---|
| 55 | |
---|
| 56 | =head2 zephyr_getsender |
---|
| 57 | |
---|
| 58 | Returns the fully-qualified name of the zephyr sender barnowl is |
---|
| 59 | running as, e.g. C<nelhage@ATHENA.MIT.EDU> |
---|
| 60 | |
---|
| 61 | =head2 zephyr_zwrite COMMAND MESSAGE |
---|
| 62 | |
---|
| 63 | Sends a zephyr programmatically. C<COMMAND> should be a C<zwrite> |
---|
| 64 | command line, and C<MESSAGE> is the zephyr body to send. |
---|
| 65 | |
---|
| 66 | =head2 ztext_stylestrip STRING |
---|
| 67 | |
---|
| 68 | Strips zephyr formatting from a string and returns the result |
---|
| 69 | |
---|
| 70 | =head2 queue_message MESSAGE |
---|
| 71 | |
---|
| 72 | Enqueue a message in the BarnOwl message list, logging it and |
---|
| 73 | processing it appropriately. C<MESSAGE> should be an instance of |
---|
| 74 | BarnOwl::Message or a subclass. |
---|
| 75 | |
---|
| 76 | =head2 admin_message HEADER BODY |
---|
| 77 | |
---|
| 78 | Display a BarnOwl B<Admin> message, with the given header and body. |
---|
| 79 | |
---|
| 80 | =head2 start_question PROMPT CALLBACK |
---|
| 81 | |
---|
| 82 | Displays C<PROMPT> on the screen and lets the user enter a line of |
---|
| 83 | text, and calls C<CALLBACK>, which must be a perl subroutine |
---|
| 84 | reference, with the text the user entered |
---|
| 85 | |
---|
| 86 | =head2 start_password PROMPT CALLBACK |
---|
| 87 | |
---|
| 88 | Like C<start_question>, but echoes the user's input as C<*>s when they |
---|
| 89 | input. |
---|
| 90 | |
---|
[9815e2e] | 91 | =head2 start_edit_win PROMPT CALLBACK |
---|
[74fc22a] | 92 | |
---|
| 93 | Like C<start_question>, but displays C<PROMPT> on a line of its own |
---|
| 94 | and opens the editwin. If the user cancels the edit win, C<CALLBACK> |
---|
| 95 | is not invoked. |
---|
| 96 | |
---|
| 97 | =head2 get_data_dir |
---|
| 98 | |
---|
| 99 | Returns the BarnOwl system data directory, where system libraries and |
---|
| 100 | modules are stored |
---|
| 101 | |
---|
| 102 | =head2 get_config_dir |
---|
| 103 | |
---|
| 104 | Returns the BarnOwl user configuration directory, where user modules |
---|
| 105 | and configuration are stored (by default, C<$HOME/.owl>) |
---|
| 106 | |
---|
| 107 | =head2 popless_text TEXT |
---|
| 108 | |
---|
| 109 | Show a popup window containing the given C<TEXT> |
---|
| 110 | |
---|
| 111 | =head2 popless_ztext TEXT |
---|
| 112 | |
---|
| 113 | Show a popup window containing the provided zephyr-formatted C<TEXT> |
---|
| 114 | |
---|
| 115 | =head2 error STRING |
---|
| 116 | |
---|
| 117 | Reports an error and log it in `show errors'. Note that in any |
---|
| 118 | callback or hook called in perl code from BarnOwl, a C<die> will be |
---|
| 119 | caught and passed to C<error>. |
---|
| 120 | |
---|
| 121 | =head2 getnumcolors |
---|
| 122 | |
---|
| 123 | Returns the number of colors this BarnOwl is capable of displaying |
---|
| 124 | |
---|
[b07d8c8] | 125 | =head2 add_dispatch FD CALLBACK |
---|
| 126 | |
---|
| 127 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
| 128 | loop. C<CALLBACK> will be invoked whenever data is available to be |
---|
| 129 | read from C<FD>. |
---|
| 130 | |
---|
| 131 | =head2 remove_dispatch FD |
---|
| 132 | |
---|
| 133 | Remove a file descriptor previously registered via C<add_dispatch> |
---|
| 134 | |
---|
[74fc22a] | 135 | =cut |
---|
| 136 | |
---|
| 137 | |
---|
[8862725] | 138 | BEGIN { |
---|
[f1e629d] | 139 | # bootstrap in C bindings and glue |
---|
[8203afd] | 140 | *owl:: = \*BarnOwl::; |
---|
| 141 | bootstrap BarnOwl 1.2; |
---|
[8862725] | 142 | }; |
---|
| 143 | |
---|
[b363d83] | 144 | use lib(get_data_dir() . "/lib"); |
---|
| 145 | use lib(get_config_dir() . "/lib"); |
---|
[8862725] | 146 | |
---|
[b363d83] | 147 | # perlconfig.c will set this to the value of the -c command-line |
---|
| 148 | # switch, if present. |
---|
[00f9a7d] | 149 | our $configfile; |
---|
| 150 | |
---|
[2e3b9c2] | 151 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
---|
| 152 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
| 153 | } |
---|
| 154 | $configfile ||= $ENV{HOME}."/.owlconf"; |
---|
[d03091c] | 155 | |
---|
[186cdc4] | 156 | # populate global variable space for legacy owlconf files |
---|
[f1e629d] | 157 | sub _receive_msg_legacy_wrap { |
---|
| 158 | my ($m) = @_; |
---|
| 159 | $m->legacy_populate_global(); |
---|
[0337203] | 160 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
[f1e629d] | 161 | } |
---|
| 162 | |
---|
[74fc22a] | 163 | =head2 AUTOLOAD |
---|
| 164 | |
---|
| 165 | BarnOwl.pm has a C<AUTOLOAD> method that translates unused names in |
---|
| 166 | the BarnOwl:: namespace to a call to BarnOwl::command() with that |
---|
| 167 | command. Underscores are also translated to C<->s, so you can do |
---|
| 168 | e.g. C<BarnOwl::start_command()> and it will be translated into |
---|
| 169 | C<start-command>. |
---|
| 170 | |
---|
| 171 | So, if you're looking for functionality that you can't find in the |
---|
| 172 | perl interface, check C<:show commands> or C<commands.c> in the |
---|
| 173 | BarnOwl source tree -- there's a good chance it exists as a BarnOwl |
---|
| 174 | command. |
---|
| 175 | |
---|
| 176 | =head3 BUGS |
---|
| 177 | |
---|
| 178 | There are horrible quoting issues here. The AUTOLOAD simple joins your |
---|
| 179 | commands with spaces and passes them unmodified to C<::command> |
---|
| 180 | |
---|
| 181 | =cut |
---|
| 182 | |
---|
[8203afd] | 183 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
[f1e629d] | 184 | sub AUTOLOAD { |
---|
[c681337] | 185 | our $AUTOLOAD; |
---|
[f1e629d] | 186 | my $called = $AUTOLOAD; |
---|
| 187 | $called =~ s/.*:://; |
---|
[e7ac2b6] | 188 | $called =~ s/_/-/g; |
---|
[8203afd] | 189 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
[f1e629d] | 190 | } |
---|
| 191 | |
---|
[6922edd] | 192 | =head2 new_command NAME FUNC [{ARGS}] |
---|
| 193 | |
---|
| 194 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
| 195 | be called with the arguments passed to the command, with NAME as the |
---|
| 196 | first argument. |
---|
| 197 | |
---|
| 198 | ARGS should be a hashref containing any or all of C<summary>, |
---|
[74fc22a] | 199 | C<usage>, or C<description> keys: |
---|
| 200 | |
---|
| 201 | =over 4 |
---|
| 202 | |
---|
| 203 | =item summary |
---|
| 204 | |
---|
| 205 | A one-line summary of the purpose of the command |
---|
| 206 | |
---|
| 207 | =item usage |
---|
| 208 | |
---|
| 209 | A one-line usage synopsis, showing available options and syntax |
---|
| 210 | |
---|
| 211 | =item description |
---|
| 212 | |
---|
| 213 | A longer description of the syntax and semantics of the command, |
---|
| 214 | explaining usage and options |
---|
| 215 | |
---|
| 216 | =back |
---|
[6922edd] | 217 | |
---|
| 218 | =cut |
---|
| 219 | |
---|
| 220 | sub new_command { |
---|
| 221 | my $name = shift; |
---|
| 222 | my $func = shift; |
---|
| 223 | my $args = shift || {}; |
---|
| 224 | my %args = ( |
---|
[8757122] | 225 | summary => "", |
---|
| 226 | usage => "", |
---|
| 227 | description => "", |
---|
[6922edd] | 228 | %{$args} |
---|
| 229 | ); |
---|
| 230 | |
---|
[8203afd] | 231 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
[6922edd] | 232 | } |
---|
| 233 | |
---|
[cd57601] | 234 | =head2 new_variable_int NAME [{ARGS}] |
---|
| 235 | |
---|
| 236 | =head2 new_variable_bool NAME [{ARGS}] |
---|
| 237 | |
---|
| 238 | =head2 new_variable_string NAME [{ARGS}] |
---|
| 239 | |
---|
| 240 | Add a new owl variable, either an int, a bool, or a string, with the |
---|
| 241 | specified name. |
---|
| 242 | |
---|
| 243 | ARGS can optionally contain the following keys: |
---|
| 244 | |
---|
| 245 | =over 4 |
---|
| 246 | |
---|
| 247 | =item default |
---|
| 248 | |
---|
| 249 | The default and initial value for the variable |
---|
| 250 | |
---|
| 251 | =item summary |
---|
| 252 | |
---|
| 253 | A one-line summary of the variable's purpose |
---|
| 254 | |
---|
| 255 | =item description |
---|
| 256 | |
---|
| 257 | A longer description of the function of the variable |
---|
| 258 | |
---|
| 259 | =back |
---|
| 260 | |
---|
| 261 | =cut |
---|
| 262 | |
---|
[a695a68] | 263 | sub new_variable_int { |
---|
| 264 | unshift @_, \&BarnOwl::new_variable_int_internal, 0; |
---|
| 265 | goto \&_new_variable; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | sub new_variable_bool { |
---|
| 269 | unshift @_, \&BarnOwl::new_variable_bool_internal, 0; |
---|
| 270 | goto \&_new_variable; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | sub new_variable_string { |
---|
| 274 | unshift @_, \&BarnOwl::new_variable_string_internal, ""; |
---|
| 275 | goto \&_new_variable; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | sub _new_variable { |
---|
| 279 | my $func = shift; |
---|
| 280 | my $default_default = shift; |
---|
| 281 | my $name = shift; |
---|
| 282 | my $args = shift || {}; |
---|
| 283 | my %args = ( |
---|
| 284 | summary => "", |
---|
| 285 | description => "", |
---|
| 286 | default => $default_default, |
---|
| 287 | %{$args}); |
---|
| 288 | $func->($name, $args{default}, $args{summary}, $args{description}); |
---|
| 289 | } |
---|
| 290 | |
---|
[f1e629d] | 291 | ##################################################################### |
---|
| 292 | ##################################################################### |
---|
| 293 | |
---|
[8203afd] | 294 | package BarnOwl::Message; |
---|
[f1e629d] | 295 | |
---|
[dd16bdd] | 296 | sub new { |
---|
| 297 | my $class = shift; |
---|
| 298 | my %args = (@_); |
---|
| 299 | if($class eq __PACKAGE__ && $args{type}) { |
---|
[8203afd] | 300 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
[dd16bdd] | 301 | } |
---|
| 302 | return bless {%args}, $class; |
---|
| 303 | } |
---|
| 304 | |
---|
[f1e629d] | 305 | sub type { return shift->{"type"}; } |
---|
| 306 | sub direction { return shift->{"direction"}; } |
---|
| 307 | sub time { return shift->{"time"}; } |
---|
| 308 | sub id { return shift->{"id"}; } |
---|
| 309 | sub body { return shift->{"body"}; } |
---|
| 310 | sub sender { return shift->{"sender"}; } |
---|
| 311 | sub recipient { return shift->{"recipient"}; } |
---|
| 312 | sub login { return shift->{"login"}; } |
---|
[216c734] | 313 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 314 | |
---|
| 315 | sub is_login { return shift->login eq "login"; } |
---|
| 316 | sub is_logout { return shift->login eq "logout"; } |
---|
| 317 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
| 318 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
| 319 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
| 320 | |
---|
| 321 | sub is_deleted { return shift->{"deleted"}; } |
---|
| 322 | |
---|
| 323 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
| 324 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
[421c8ef7] | 325 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
[467aa16] | 326 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
[dd16bdd] | 327 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 328 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
| 329 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
| 330 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
| 331 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
[f1e629d] | 332 | |
---|
| 333 | # These are overridden by appropriate message types |
---|
| 334 | sub is_ping { return 0; } |
---|
| 335 | sub is_mail { return 0; } |
---|
[3c9012b] | 336 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 337 | sub class { return undef; } |
---|
| 338 | sub instance { return undef; } |
---|
| 339 | sub realm { return undef; } |
---|
| 340 | sub opcode { return undef; } |
---|
| 341 | sub header { return undef; } |
---|
| 342 | sub host { return undef; } |
---|
| 343 | sub hostname { return undef; } |
---|
| 344 | sub auth { return undef; } |
---|
| 345 | sub fields { return undef; } |
---|
| 346 | sub zsig { return undef; } |
---|
| 347 | sub zwriteline { return undef; } |
---|
[87c6ef1] | 348 | sub login_host { return undef; } |
---|
| 349 | sub login_tty { return undef; } |
---|
[f1e629d] | 350 | |
---|
[ae47efb] | 351 | sub pretty_sender { return shift->sender; } |
---|
| 352 | sub pretty_recipient { return shift->recipient; } |
---|
[f1e629d] | 353 | |
---|
| 354 | sub delete { |
---|
| 355 | my ($m) = @_; |
---|
[8203afd] | 356 | &BarnOwl::command("delete --id ".$m->id); |
---|
[f1e629d] | 357 | } |
---|
| 358 | |
---|
| 359 | sub undelete { |
---|
| 360 | my ($m) = @_; |
---|
[8203afd] | 361 | &BarnOwl::command("undelete --id ".$m->id); |
---|
[f1e629d] | 362 | } |
---|
| 363 | |
---|
| 364 | # Serializes the message into something similar to the zwgc->vt format |
---|
| 365 | sub serialize { |
---|
| 366 | my ($this) = @_; |
---|
| 367 | my $s; |
---|
| 368 | for my $f (keys %$this) { |
---|
| 369 | my $val = $this->{$f}; |
---|
| 370 | if (ref($val) eq "ARRAY") { |
---|
| 371 | for my $i (0..@$val-1) { |
---|
| 372 | my $aval; |
---|
| 373 | $aval = $val->[$i]; |
---|
| 374 | $aval =~ s/\n/\n$f.$i: /g; |
---|
[186cdc4] | 375 | $s .= "$f.$i: $aval\n"; |
---|
[f1e629d] | 376 | } |
---|
| 377 | } else { |
---|
| 378 | $val =~ s/\n/\n$f: /g; |
---|
| 379 | $s .= "$f: $val\n"; |
---|
| 380 | } |
---|
| 381 | } |
---|
| 382 | return $s; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | # Populate the annoying legacy global variables |
---|
| 386 | sub legacy_populate_global { |
---|
| 387 | my ($m) = @_; |
---|
[8203afd] | 388 | $BarnOwl::direction = $m->direction ; |
---|
| 389 | $BarnOwl::type = $m->type ; |
---|
| 390 | $BarnOwl::id = $m->id ; |
---|
| 391 | $BarnOwl::class = $m->class ; |
---|
| 392 | $BarnOwl::instance = $m->instance ; |
---|
| 393 | $BarnOwl::recipient = $m->recipient ; |
---|
| 394 | $BarnOwl::sender = $m->sender ; |
---|
| 395 | $BarnOwl::realm = $m->realm ; |
---|
| 396 | $BarnOwl::opcode = $m->opcode ; |
---|
| 397 | $BarnOwl::zsig = $m->zsig ; |
---|
| 398 | $BarnOwl::msg = $m->body ; |
---|
| 399 | $BarnOwl::time = $m->time ; |
---|
| 400 | $BarnOwl::host = $m->host ; |
---|
| 401 | $BarnOwl::login = $m->login ; |
---|
| 402 | $BarnOwl::auth = $m->auth ; |
---|
[f1e629d] | 403 | if ($m->fields) { |
---|
[8203afd] | 404 | @BarnOwl::fields = @{$m->fields}; |
---|
[f1e629d] | 405 | @main::fields = @{$m->fields}; |
---|
| 406 | } else { |
---|
[8203afd] | 407 | @BarnOwl::fields = undef; |
---|
[f1e629d] | 408 | @main::fields = undef; |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
[25729b2] | 412 | sub smartfilter { |
---|
[0337203] | 413 | die("smartfilter not supported for this message\n"); |
---|
[25729b2] | 414 | } |
---|
| 415 | |
---|
[6e6ded7] | 416 | # Display fields -- overridden by subclasses when needed |
---|
| 417 | sub login_type {""} |
---|
| 418 | sub login_extra {""} |
---|
| 419 | sub long_sender {""} |
---|
| 420 | |
---|
| 421 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
| 422 | # class |
---|
| 423 | sub context {""} |
---|
| 424 | |
---|
| 425 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
| 426 | # instance |
---|
| 427 | sub subcontext {""} |
---|
| 428 | |
---|
[f1e629d] | 429 | ##################################################################### |
---|
| 430 | ##################################################################### |
---|
| 431 | |
---|
[8203afd] | 432 | package BarnOwl::Message::Admin; |
---|
[f1e629d] | 433 | |
---|
[8203afd] | 434 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 435 | |
---|
| 436 | sub header { return shift->{"header"}; } |
---|
| 437 | |
---|
| 438 | ##################################################################### |
---|
| 439 | ##################################################################### |
---|
| 440 | |
---|
[8203afd] | 441 | package BarnOwl::Message::Generic; |
---|
[f1e629d] | 442 | |
---|
[8203afd] | 443 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 444 | |
---|
| 445 | ##################################################################### |
---|
| 446 | ##################################################################### |
---|
| 447 | |
---|
[186cdc4] | 448 | package BarnOwl::Message::Loopback; |
---|
| 449 | |
---|
| 450 | use base qw( BarnOwl::Message ); |
---|
| 451 | |
---|
[cb06a43] | 452 | # all loopback messages are private |
---|
| 453 | sub is_private { |
---|
[186cdc4] | 454 | return 1; |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | ##################################################################### |
---|
| 458 | ##################################################################### |
---|
| 459 | |
---|
[8203afd] | 460 | package BarnOwl::Message::AIM; |
---|
[f1e629d] | 461 | |
---|
[8203afd] | 462 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 463 | |
---|
[cb06a43] | 464 | # all non-loginout AIM messages are private for now... |
---|
| 465 | sub is_private { |
---|
[f1e629d] | 466 | return !(shift->is_loginout); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | ##################################################################### |
---|
| 470 | ##################################################################### |
---|
| 471 | |
---|
[8203afd] | 472 | package BarnOwl::Message::Zephyr; |
---|
[f1e629d] | 473 | |
---|
[8203afd] | 474 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 475 | |
---|
[6e6ded7] | 476 | sub login_type { |
---|
| 477 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | sub login_extra { |
---|
| 481 | my $m = shift; |
---|
| 482 | return undef if (!$m->is_loginout); |
---|
| 483 | my $s = lc($m->host); |
---|
| 484 | $s .= " " . $m->login_tty if defined $m->login_tty; |
---|
| 485 | return $s; |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | sub long_sender { |
---|
| 489 | my $m = shift; |
---|
| 490 | return $m->zsig; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | sub context { |
---|
| 494 | return shift->class; |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | sub subcontext { |
---|
| 498 | return shift->instance; |
---|
| 499 | } |
---|
| 500 | |
---|
[186cdc4] | 501 | sub login_tty { |
---|
[f1e629d] | 502 | my ($m) = @_; |
---|
| 503 | return undef if (!$m->is_loginout); |
---|
| 504 | return $m->fields->[2]; |
---|
| 505 | } |
---|
| 506 | |
---|
[186cdc4] | 507 | sub login_host { |
---|
[f1e629d] | 508 | my ($m) = @_; |
---|
| 509 | return undef if (!$m->is_loginout); |
---|
| 510 | return $m->fields->[0]; |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
| 514 | |
---|
| 515 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
| 516 | |
---|
[186cdc4] | 517 | sub is_personal { |
---|
[f1e629d] | 518 | my ($m) = @_; |
---|
| 519 | return ((lc($m->class) eq "message") |
---|
| 520 | && (lc($m->instance) eq "personal") |
---|
| 521 | && $m->is_private); |
---|
| 522 | } |
---|
| 523 | |
---|
[186cdc4] | 524 | sub is_mail { |
---|
[f1e629d] | 525 | my ($m) = @_; |
---|
| 526 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | sub pretty_sender { |
---|
| 530 | my ($m) = @_; |
---|
| 531 | my $sender = $m->sender; |
---|
[8203afd] | 532 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
[f1e629d] | 533 | $sender =~ s/\@$realm$//; |
---|
| 534 | return $sender; |
---|
| 535 | } |
---|
| 536 | |
---|
[ae47efb] | 537 | sub pretty_recipient { |
---|
| 538 | my ($m) = @_; |
---|
| 539 | my $recip = $m->recipient; |
---|
| 540 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
| 541 | $recip =~ s/\@$realm$//; |
---|
| 542 | return $recip; |
---|
| 543 | } |
---|
| 544 | |
---|
[f1e629d] | 545 | # These are arguably zephyr-specific |
---|
| 546 | sub class { return shift->{"class"}; } |
---|
| 547 | sub instance { return shift->{"instance"}; } |
---|
| 548 | sub realm { return shift->{"realm"}; } |
---|
| 549 | sub opcode { return shift->{"opcode"}; } |
---|
| 550 | sub host { return shift->{"hostname"}; } |
---|
| 551 | sub hostname { return shift->{"hostname"}; } |
---|
| 552 | sub header { return shift->{"header"}; } |
---|
| 553 | sub auth { return shift->{"auth"}; } |
---|
| 554 | sub fields { return shift->{"fields"}; } |
---|
| 555 | sub zsig { return shift->{"zsig"}; } |
---|
| 556 | |
---|
| 557 | ##################################################################### |
---|
| 558 | ##################################################################### |
---|
[7e470da] | 559 | ################################################################################ |
---|
| 560 | |
---|
[0337203] | 561 | package BarnOwl::Hook; |
---|
[7e470da] | 562 | |
---|
[1a64de6] | 563 | =head1 BarnOwl::Hook |
---|
| 564 | |
---|
| 565 | =head1 DESCRIPTION |
---|
| 566 | |
---|
| 567 | A C<BarnOwl::Hook> represents a list of functions to be triggered on |
---|
| 568 | some event. C<BarnOwl> exports a default set of these (see |
---|
| 569 | C<BarnOwl::Hooks>), but can also be created and used by module code. |
---|
| 570 | |
---|
| 571 | =head2 new |
---|
| 572 | |
---|
| 573 | Creates a new Hook object |
---|
| 574 | |
---|
| 575 | =cut |
---|
| 576 | |
---|
[0337203] | 577 | sub new { |
---|
| 578 | my $class = shift; |
---|
| 579 | return bless [], $class; |
---|
| 580 | } |
---|
[7e470da] | 581 | |
---|
[1a64de6] | 582 | =head2 run [ARGS] |
---|
| 583 | |
---|
| 584 | Calls each of the functions registered with this hook with the given |
---|
| 585 | arguments. |
---|
| 586 | |
---|
| 587 | =cut |
---|
| 588 | |
---|
[0337203] | 589 | sub run { |
---|
| 590 | my $self = shift; |
---|
| 591 | my @args = @_; |
---|
| 592 | return map {$_->(@args)} @$self; |
---|
[7e470da] | 593 | } |
---|
[0337203] | 594 | |
---|
[1a64de6] | 595 | =head2 add SUBREF |
---|
| 596 | |
---|
| 597 | Registers a given subroutine with this hook |
---|
| 598 | |
---|
| 599 | =cut |
---|
| 600 | |
---|
[0337203] | 601 | sub add { |
---|
| 602 | my $self = shift; |
---|
| 603 | my $func = shift; |
---|
| 604 | die("Not a coderef!") unless ref($func) eq 'CODE'; |
---|
| 605 | push @$self, $func; |
---|
[7e470da] | 606 | } |
---|
| 607 | |
---|
[1a64de6] | 608 | =head2 clear |
---|
| 609 | |
---|
| 610 | Remove all functions registered with this hook. |
---|
| 611 | |
---|
| 612 | =cut |
---|
| 613 | |
---|
[0337203] | 614 | sub clear { |
---|
| 615 | my $self = shift; |
---|
| 616 | @$self = (); |
---|
[7e470da] | 617 | } |
---|
| 618 | |
---|
[0337203] | 619 | package BarnOwl::Hooks; |
---|
[7e470da] | 620 | |
---|
[1a64de6] | 621 | =head1 BarnOwl::Hooks |
---|
| 622 | |
---|
| 623 | =head1 DESCRIPTION |
---|
| 624 | |
---|
| 625 | C<BarnOwl::Hooks> exports a set of C<BarnOwl::Hook> objects made |
---|
| 626 | available by BarnOwl internally. |
---|
| 627 | |
---|
| 628 | =head2 USAGE |
---|
| 629 | |
---|
| 630 | Modules wishing to respond to events in BarnOwl should register |
---|
| 631 | functions with these hooks. |
---|
| 632 | |
---|
| 633 | =head2 EXPORTS |
---|
| 634 | |
---|
| 635 | None by default. Either import the hooks you need explicitly, or refer |
---|
| 636 | to them with fully-qualified names. Available hooks are: |
---|
| 637 | |
---|
| 638 | =over 4 |
---|
| 639 | |
---|
| 640 | =item $startup |
---|
| 641 | |
---|
| 642 | Called on BarnOwl startup, and whenever modules are |
---|
| 643 | reloaded. Functions registered with the C<$startup> hook get a true |
---|
| 644 | argument if this is a reload, and false if this is a true startup |
---|
| 645 | |
---|
| 646 | =item $shutdown |
---|
| 647 | |
---|
| 648 | Called before BarnOwl shutdown |
---|
| 649 | |
---|
| 650 | =item $receiveMessage |
---|
| 651 | |
---|
| 652 | Called with a C<BarnOwl::Message> object every time BarnOwl appends a |
---|
| 653 | new message to its message list |
---|
| 654 | |
---|
| 655 | =item $mainLoop |
---|
| 656 | |
---|
[b07d8c8] | 657 | Called on every pass through the C<BarnOwl> main loop. This is |
---|
| 658 | guaranteed to be called at least once/sec and may be called more |
---|
| 659 | frequently. |
---|
[1a64de6] | 660 | |
---|
| 661 | =item $getBuddyList |
---|
| 662 | |
---|
| 663 | Called to display buddy lists for all protocol handlers. The result |
---|
| 664 | from every function registered with this hook will be appended and |
---|
| 665 | displayed in a popup window, with zephyr formatting parsed. |
---|
| 666 | |
---|
| 667 | =back |
---|
| 668 | |
---|
| 669 | =cut |
---|
| 670 | |
---|
[0337203] | 671 | use Exporter; |
---|
| 672 | |
---|
| 673 | our @EXPORT_OK = qw($startup $shutdown |
---|
| 674 | $receiveMessage $mainLoop |
---|
| 675 | $getBuddyList); |
---|
| 676 | |
---|
| 677 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
---|
| 678 | |
---|
| 679 | our $startup = BarnOwl::Hook->new; |
---|
| 680 | our $shutdown = BarnOwl::Hook->new; |
---|
| 681 | our $receiveMessage = BarnOwl::Hook->new; |
---|
| 682 | our $mainLoop = BarnOwl::Hook->new; |
---|
| 683 | our $getBuddyList = BarnOwl::Hook->new; |
---|
| 684 | |
---|
| 685 | # Internal startup/shutdown routines called by the C code |
---|
[7e470da] | 686 | |
---|
[2650a10] | 687 | sub _load_perl_commands { |
---|
| 688 | # Load builtin perl commands |
---|
| 689 | BarnOwl::new_command(style => \&BarnOwl::Style::style_command, |
---|
| 690 | { |
---|
| 691 | summary => "creates a new style", |
---|
| 692 | usage => "style <name> perl <function_name>", |
---|
| 693 | description => |
---|
| 694 | "A style named <name> will be created that will\n" . |
---|
| 695 | "format messages using the perl function <function_name>.\n\n" . |
---|
| 696 | "SEE ALSO: show styles, view -s, filter -s\n\n" . |
---|
| 697 | "DEPRECATED in favor of BarnOwl::create_style(NAME, OBJECT)", |
---|
| 698 | }); |
---|
| 699 | } |
---|
| 700 | |
---|
[b6c067a] | 701 | sub _load_owlconf { |
---|
| 702 | # load the config file |
---|
| 703 | if ( -r $BarnOwl::configfile ) { |
---|
| 704 | undef $@; |
---|
[e8bc8ac] | 705 | package main; |
---|
[b6c067a] | 706 | do $BarnOwl::configfile; |
---|
| 707 | die $@ if $@; |
---|
[e8bc8ac] | 708 | package BarnOwl; |
---|
[39dc159] | 709 | if(*BarnOwl::format_msg{CODE}) { |
---|
| 710 | # if the config defines a legacy formatting function, add 'perl' as a style |
---|
[b67ab6b] | 711 | BarnOwl::create_style("perl", BarnOwl::Style::Legacy->new( |
---|
| 712 | "BarnOwl::format_msg", |
---|
| 713 | "User-defined perl style that calls BarnOwl::format_msg" |
---|
| 714 | . " with legacy global variable support", |
---|
| 715 | 1)); |
---|
| 716 | BarnOwl::set("-q default_style perl"); |
---|
[39dc159] | 717 | } |
---|
[b6c067a] | 718 | } |
---|
| 719 | } |
---|
| 720 | |
---|
[b0c8011] | 721 | # These are the internal hooks called by the barnowl C code, which |
---|
| 722 | # take care of dispatching to the appropriate perl hooks, and deal |
---|
| 723 | # with compatibility by calling the old, fixed-name hooks. |
---|
| 724 | |
---|
[0337203] | 725 | sub _startup { |
---|
[2650a10] | 726 | _load_perl_commands(); |
---|
[0337203] | 727 | _load_owlconf(); |
---|
[8203afd] | 728 | |
---|
[0337203] | 729 | if(eval {require BarnOwl::ModuleLoader}) { |
---|
| 730 | eval { |
---|
| 731 | BarnOwl::ModuleLoader->load_all; |
---|
| 732 | }; |
---|
[f6b319c] | 733 | BarnOwl::error("$@") if $@; |
---|
| 734 | |
---|
[0337203] | 735 | } else { |
---|
| 736 | BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); |
---|
| 737 | } |
---|
| 738 | |
---|
[836e6263] | 739 | $startup->run(0); |
---|
[8203afd] | 740 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
| 741 | } |
---|
| 742 | |
---|
[0337203] | 743 | sub _shutdown { |
---|
| 744 | $shutdown->run; |
---|
| 745 | |
---|
[8203afd] | 746 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
---|
| 747 | } |
---|
| 748 | |
---|
[0337203] | 749 | sub _receive_msg { |
---|
[7e470da] | 750 | my $m = shift; |
---|
[0337203] | 751 | |
---|
| 752 | $receiveMessage->run($m); |
---|
| 753 | |
---|
[8203afd] | 754 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
[7e470da] | 755 | } |
---|
| 756 | |
---|
[0337203] | 757 | sub _mainloop_hook { |
---|
| 758 | $mainLoop->run; |
---|
| 759 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
| 760 | } |
---|
[7e470da] | 761 | |
---|
[0337203] | 762 | sub _get_blist { |
---|
| 763 | return join("\n", $getBuddyList->run); |
---|
[7e470da] | 764 | } |
---|
[dd16bdd] | 765 | |
---|
[b6c067a] | 766 | ################################################################################ |
---|
| 767 | # Built-in perl styles |
---|
| 768 | ################################################################################ |
---|
| 769 | package BarnOwl::Style::Default; |
---|
| 770 | ################################################################################ |
---|
| 771 | # Branching point for various formatting functions in this style. |
---|
| 772 | ################################################################################ |
---|
| 773 | sub format_message($) |
---|
| 774 | { |
---|
[864ed35] | 775 | my $self = shift; |
---|
[b6c067a] | 776 | my $m = shift; |
---|
| 777 | |
---|
[6e6ded7] | 778 | if ( $m->is_loginout) { |
---|
[864ed35] | 779 | return $self->format_login($m); |
---|
[18fb3d4f] | 780 | } elsif($m->is_ping && $m->is_personal) { |
---|
[864ed35] | 781 | return $self->format_ping($m); |
---|
[6e6ded7] | 782 | } elsif($m->is_admin) { |
---|
[864ed35] | 783 | return $self->format_admin($m); |
---|
[6e6ded7] | 784 | } else { |
---|
[864ed35] | 785 | return $self->format_chat($m); |
---|
[b6c067a] | 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
[864ed35] | 789 | sub description {"Default style";} |
---|
| 790 | |
---|
[b67ab6b] | 791 | BarnOwl::create_style("default", "BarnOwl::Style::Default"); |
---|
[b6c067a] | 792 | |
---|
| 793 | ################################################################################ |
---|
| 794 | |
---|
[6e6ded7] | 795 | sub time_hhmm { |
---|
| 796 | my $m = shift; |
---|
[b6c067a] | 797 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
---|
[6e6ded7] | 798 | return $time; |
---|
[b6c067a] | 799 | } |
---|
| 800 | |
---|
[6e6ded7] | 801 | sub format_login($) { |
---|
[864ed35] | 802 | my $self = shift; |
---|
[b6c067a] | 803 | my $m = shift; |
---|
[6e6ded7] | 804 | return sprintf( |
---|
| 805 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
| 806 | uc( $m->login ), |
---|
| 807 | $m->login_type, |
---|
| 808 | $m->pretty_sender, |
---|
| 809 | $m->login_extra, |
---|
| 810 | time_hhmm($m) |
---|
| 811 | ); |
---|
[b6c067a] | 812 | } |
---|
| 813 | |
---|
[864ed35] | 814 | sub format_ping { |
---|
| 815 | my $self = shift; |
---|
| 816 | my $m = shift; |
---|
| 817 | return "\@b(PING) from \@b(" . $m->pretty_sender . ")\n"; |
---|
| 818 | } |
---|
| 819 | |
---|
| 820 | sub format_admin { |
---|
| 821 | my $self = shift; |
---|
| 822 | my $m = shift; |
---|
| 823 | return "\@bold(OWL ADMIN)\n" . indentBody($m); |
---|
| 824 | } |
---|
| 825 | |
---|
[6e6ded7] | 826 | sub format_chat($) { |
---|
[864ed35] | 827 | my $self = shift; |
---|
[b6c067a] | 828 | my $m = shift; |
---|
[6e6ded7] | 829 | my $header; |
---|
| 830 | if ( $m->is_personal ) { |
---|
| 831 | if ( $m->direction eq "out" ) { |
---|
| 832 | $header = ucfirst $m->type . " sent to " . $m->pretty_recipient; |
---|
| 833 | } else { |
---|
| 834 | $header = ucfirst $m->type . " from " . $m->pretty_sender; |
---|
| 835 | } |
---|
| 836 | } else { |
---|
| 837 | $header = $m->context; |
---|
[37dd88c] | 838 | if(defined $m->subcontext) { |
---|
[c39999f] | 839 | $header .= ' / ' . $m->subcontext; |
---|
[6e6ded7] | 840 | } |
---|
[c39999f] | 841 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
[6e6ded7] | 842 | } |
---|
[b6c067a] | 843 | |
---|
[b51d257] | 844 | if($m->opcode) { |
---|
| 845 | $header .= " [" . $m->opcode . "]"; |
---|
| 846 | } |
---|
[6e6ded7] | 847 | $header .= " " . time_hhmm($m); |
---|
[0449730] | 848 | my $sender = $m->long_sender; |
---|
| 849 | $sender =~ s/\n.*$//s; |
---|
[cc5b906] | 850 | $header .= " " x (4 - ((length $header) % 4)); |
---|
[42947f1] | 851 | $header .= "(" . $sender . '@color[default]' . ")"; |
---|
[6e6ded7] | 852 | my $message = $header . "\n". indentBody($m); |
---|
[cb06a43] | 853 | if($m->is_personal && $m->direction eq "in") { |
---|
[6e6ded7] | 854 | $message = BarnOwl::Style::boldify($message); |
---|
| 855 | } |
---|
| 856 | return $message; |
---|
[b6c067a] | 857 | } |
---|
| 858 | |
---|
| 859 | sub indentBody($) |
---|
| 860 | { |
---|
| 861 | my $m = shift; |
---|
[186cdc4] | 862 | |
---|
[b6c067a] | 863 | my $body = $m->body; |
---|
[f6b319c] | 864 | if ($m->{should_wordwrap}) { |
---|
| 865 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-8); |
---|
| 866 | } |
---|
[186cdc4] | 867 | # replace newline followed by anything with |
---|
[b6c067a] | 868 | # newline plus four spaces and that thing. |
---|
| 869 | $body =~ s/\n(.)/\n $1/g; |
---|
[f2d72128] | 870 | # Trim trailing newlines. |
---|
| 871 | $body =~ s/\n*$//; |
---|
[b6c067a] | 872 | return " ".$body; |
---|
| 873 | } |
---|
| 874 | |
---|
[864ed35] | 875 | package BarnOwl::Style::Basic; |
---|
| 876 | |
---|
| 877 | our @ISA=qw(BarnOwl::Style::Default); |
---|
| 878 | |
---|
| 879 | sub description {"Compatability alias for the default style";} |
---|
| 880 | |
---|
[b67ab6b] | 881 | BarnOwl::create_style("basic", "BarnOwl::Style::Basic"); |
---|
[864ed35] | 882 | |
---|
[f2d72128] | 883 | package BarnOwl::Style::OneLine; |
---|
| 884 | ################################################################################ |
---|
| 885 | # Branching point for various formatting functions in this style. |
---|
| 886 | ################################################################################ |
---|
| 887 | use constant BASE_FORMAT => '%s %-13.13s %-11.11s %-12.12s '; |
---|
| 888 | sub format_message($) { |
---|
[864ed35] | 889 | my $self = shift; |
---|
[f2d72128] | 890 | my $m = shift; |
---|
| 891 | |
---|
[5ff830a] | 892 | if ( $m->is_loginout ) { |
---|
[864ed35] | 893 | return $self->format_login($m); |
---|
[f2d72128] | 894 | } |
---|
| 895 | elsif ( $m->is_ping) { |
---|
[864ed35] | 896 | return $self->format_ping($m); |
---|
[f2d72128] | 897 | } |
---|
| 898 | elsif ( $m->is_admin || $m->is_loopback) { |
---|
[864ed35] | 899 | return $self->format_local($m); |
---|
[f2d72128] | 900 | } |
---|
| 901 | else { |
---|
[864ed35] | 902 | return $self->format_chat($m); |
---|
[f2d72128] | 903 | } |
---|
| 904 | } |
---|
| 905 | |
---|
[864ed35] | 906 | sub description {"Formats for one-line-per-message"} |
---|
| 907 | |
---|
[b67ab6b] | 908 | BarnOwl::create_style("oneline", "BarnOwl::Style::OneLine"); |
---|
[f2d72128] | 909 | |
---|
| 910 | ################################################################################ |
---|
| 911 | |
---|
| 912 | sub format_login($) { |
---|
[864ed35] | 913 | my $self = shift; |
---|
[f2d72128] | 914 | my $m = shift; |
---|
| 915 | return sprintf( |
---|
| 916 | BASE_FORMAT, |
---|
| 917 | '<', |
---|
| 918 | $m->type, |
---|
| 919 | uc( $m->login ), |
---|
| 920 | $m->pretty_sender) |
---|
| 921 | . ($m->login_extra ? "at ".$m->login_extra : ''); |
---|
| 922 | } |
---|
| 923 | |
---|
| 924 | sub format_ping($) { |
---|
| 925 | my $m = shift; |
---|
| 926 | return sprintf( |
---|
| 927 | BASE_FORMAT, |
---|
| 928 | '<', |
---|
| 929 | $m->type, |
---|
| 930 | 'PING', |
---|
| 931 | $m->pretty_sender) |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | sub format_chat($) |
---|
| 935 | { |
---|
[864ed35] | 936 | my $self = shift; |
---|
[f2d72128] | 937 | my $m = shift; |
---|
| 938 | my $dir = lc($m->{direction}); |
---|
| 939 | my $dirsym = '-'; |
---|
| 940 | if ($dir eq 'in') { |
---|
| 941 | $dirsym = '<'; |
---|
| 942 | } |
---|
| 943 | elsif ($dir eq 'out') { |
---|
| 944 | $dirsym = '>'; |
---|
| 945 | } |
---|
| 946 | |
---|
| 947 | my $line; |
---|
| 948 | if ($m->is_personal) { |
---|
| 949 | $line= sprintf(BASE_FORMAT, |
---|
| 950 | $dirsym, |
---|
| 951 | $m->type, |
---|
| 952 | '', |
---|
| 953 | ($dir eq 'out' |
---|
| 954 | ? $m->pretty_recipient |
---|
| 955 | : $m->pretty_sender)); |
---|
| 956 | } |
---|
| 957 | else { |
---|
| 958 | $line = sprintf(BASE_FORMAT, |
---|
| 959 | $dirsym, |
---|
| 960 | $m->context, |
---|
| 961 | $m->subcontext, |
---|
| 962 | ($dir eq 'out' |
---|
| 963 | ? $m->pretty_recipient |
---|
| 964 | : $m->pretty_sender)); |
---|
| 965 | } |
---|
| 966 | |
---|
| 967 | my $body = $m->{body}; |
---|
| 968 | $body =~ tr/\n/ /; |
---|
| 969 | $line .= $body; |
---|
| 970 | $line = BarnOwl::Style::boldify($line) if ($m->is_personal && lc($m->direction) eq 'in'); |
---|
| 971 | return $line; |
---|
| 972 | } |
---|
| 973 | |
---|
| 974 | # Format locally generated messages |
---|
| 975 | sub format_local($) |
---|
| 976 | { |
---|
[864ed35] | 977 | my $self = shift; |
---|
[f2d72128] | 978 | my $m = shift; |
---|
| 979 | my $type = uc($m->{type}); |
---|
| 980 | my $line = sprintf(BASE_FORMAT, '<', $type, '', ''); |
---|
| 981 | my $body = $m->{body}; |
---|
| 982 | $body =~ tr/\n/ /; |
---|
| 983 | return $line.$body; |
---|
| 984 | } |
---|
[b6c067a] | 985 | |
---|
| 986 | package BarnOwl::Style; |
---|
| 987 | |
---|
| 988 | # This takes a zephyr to be displayed and modifies it to be displayed |
---|
| 989 | # entirely in bold. |
---|
| 990 | sub boldify($) |
---|
| 991 | { |
---|
| 992 | local $_ = shift; |
---|
| 993 | if ( !(/\)/) ) { |
---|
| 994 | return '@b(' . $_ . ')'; |
---|
| 995 | } elsif ( !(/\>/) ) { |
---|
| 996 | return '@b<' . $_ . '>'; |
---|
| 997 | } elsif ( !(/\}/) ) { |
---|
| 998 | return '@b{' . $_ . '}'; |
---|
| 999 | } elsif ( !(/\]/) ) { |
---|
| 1000 | return '@b[' . $_ . ']'; |
---|
| 1001 | } else { |
---|
| 1002 | my $txt = "\@b($_"; |
---|
| 1003 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
| 1004 | return $txt . ')'; |
---|
| 1005 | } |
---|
| 1006 | } |
---|
| 1007 | |
---|
[2650a10] | 1008 | sub style_command { |
---|
| 1009 | my $command = shift; |
---|
| 1010 | if(scalar @_ != 3 || $_[1] ne 'perl') { |
---|
| 1011 | die("Usage: style <name> perl <function>\n"); |
---|
| 1012 | } |
---|
| 1013 | my $name = shift; |
---|
| 1014 | my $perl = shift; |
---|
| 1015 | my $fn = shift; |
---|
| 1016 | { |
---|
| 1017 | no strict 'refs'; |
---|
| 1018 | unless(*{$fn}{CODE}) { |
---|
| 1019 | die("Unable to create style '$name': no perl function '$fn'\n"); |
---|
| 1020 | } |
---|
| 1021 | } |
---|
| 1022 | BarnOwl::create_style($name, BarnOwl::Style::Legacy->new($fn)); |
---|
| 1023 | } |
---|
| 1024 | |
---|
[b67ab6b] | 1025 | package BarnOwl::Style::Legacy; |
---|
| 1026 | |
---|
| 1027 | sub new { |
---|
| 1028 | my $class = shift; |
---|
| 1029 | my $func = shift; |
---|
| 1030 | my $desc = shift; |
---|
| 1031 | my $useglobals = shift; |
---|
| 1032 | $useglobals = 0 unless defined($useglobals); |
---|
| 1033 | return bless {function => $func, |
---|
| 1034 | description => $desc, |
---|
| 1035 | useglobals => $useglobals}, $class; |
---|
| 1036 | } |
---|
| 1037 | |
---|
[2650a10] | 1038 | sub description { |
---|
| 1039 | my $self = shift; |
---|
| 1040 | return $self->{description} || |
---|
| 1041 | ("User-defined perl style that calls " . $self->{function}); |
---|
| 1042 | }; |
---|
[b67ab6b] | 1043 | |
---|
| 1044 | sub format_message { |
---|
| 1045 | my $self = shift; |
---|
| 1046 | if($self->{useglobals}) { |
---|
| 1047 | $_[0]->legacy_populate_global(); |
---|
| 1048 | } |
---|
| 1049 | no strict 'refs'; |
---|
| 1050 | goto \&{$self->{function}}; |
---|
| 1051 | } |
---|
| 1052 | |
---|
[b6c067a] | 1053 | |
---|
[f1e629d] | 1054 | # switch to package main when we're done |
---|
| 1055 | package main; |
---|
[0337203] | 1056 | |
---|
| 1057 | # Shove a bunch of fake entries into @INC so modules can use or |
---|
| 1058 | # require them without choking |
---|
| 1059 | $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
| 1060 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
[f1e629d] | 1061 | |
---|
| 1062 | 1; |
---|
[0337203] | 1063 | |
---|