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