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