[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 | |
---|
[5b75e8b] | 304 | =head2 quote STRING |
---|
| 305 | |
---|
| 306 | Return a version of STRING fully quoted to survive processing by |
---|
| 307 | BarnOwl's command parser. |
---|
| 308 | |
---|
| 309 | =cut |
---|
| 310 | |
---|
| 311 | sub quote { |
---|
| 312 | my $str = shift; |
---|
| 313 | if ($str !~ /'/ && $str !~ /"/) { |
---|
| 314 | return "$str"; |
---|
| 315 | } |
---|
| 316 | if ($str !~ /'/) { |
---|
| 317 | return "'$str'"; |
---|
| 318 | } |
---|
| 319 | $str =~ s/"/"'"'"/g; |
---|
| 320 | return '"' . $str . '"'; |
---|
| 321 | } |
---|
| 322 | |
---|
[f1e629d] | 323 | ##################################################################### |
---|
| 324 | ##################################################################### |
---|
| 325 | |
---|
[8203afd] | 326 | package BarnOwl::Message; |
---|
[f1e629d] | 327 | |
---|
[dd16bdd] | 328 | sub new { |
---|
| 329 | my $class = shift; |
---|
| 330 | my %args = (@_); |
---|
| 331 | if($class eq __PACKAGE__ && $args{type}) { |
---|
[8203afd] | 332 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
[dd16bdd] | 333 | } |
---|
| 334 | return bless {%args}, $class; |
---|
| 335 | } |
---|
| 336 | |
---|
[f1e629d] | 337 | sub type { return shift->{"type"}; } |
---|
| 338 | sub direction { return shift->{"direction"}; } |
---|
| 339 | sub time { return shift->{"time"}; } |
---|
| 340 | sub id { return shift->{"id"}; } |
---|
| 341 | sub body { return shift->{"body"}; } |
---|
| 342 | sub sender { return shift->{"sender"}; } |
---|
| 343 | sub recipient { return shift->{"recipient"}; } |
---|
| 344 | sub login { return shift->{"login"}; } |
---|
[216c734] | 345 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 346 | |
---|
| 347 | sub is_login { return shift->login eq "login"; } |
---|
| 348 | sub is_logout { return shift->login eq "logout"; } |
---|
| 349 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
| 350 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
| 351 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
| 352 | |
---|
| 353 | sub is_deleted { return shift->{"deleted"}; } |
---|
| 354 | |
---|
| 355 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
| 356 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
[421c8ef7] | 357 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
[467aa16] | 358 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
[dd16bdd] | 359 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 360 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
| 361 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
| 362 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
| 363 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
[f1e629d] | 364 | |
---|
| 365 | # These are overridden by appropriate message types |
---|
| 366 | sub is_ping { return 0; } |
---|
| 367 | sub is_mail { return 0; } |
---|
[3c9012b] | 368 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 369 | sub class { return undef; } |
---|
| 370 | sub instance { return undef; } |
---|
| 371 | sub realm { return undef; } |
---|
| 372 | sub opcode { return undef; } |
---|
| 373 | sub header { return undef; } |
---|
| 374 | sub host { return undef; } |
---|
| 375 | sub hostname { return undef; } |
---|
| 376 | sub auth { return undef; } |
---|
| 377 | sub fields { return undef; } |
---|
| 378 | sub zsig { return undef; } |
---|
| 379 | sub zwriteline { return undef; } |
---|
[87c6ef1] | 380 | sub login_host { return undef; } |
---|
| 381 | sub login_tty { return undef; } |
---|
[f1e629d] | 382 | |
---|
[57cf4f9] | 383 | # This is for back-compat with old messages that set these properties |
---|
| 384 | # New protocol implementations are encourages to user override these |
---|
| 385 | # methods. |
---|
| 386 | sub replycmd { return shift->{replycmd}}; |
---|
| 387 | sub replysendercmd { return shift->{replysendercmd}}; |
---|
| 388 | |
---|
[ae47efb] | 389 | sub pretty_sender { return shift->sender; } |
---|
| 390 | sub pretty_recipient { return shift->recipient; } |
---|
[f1e629d] | 391 | |
---|
| 392 | sub delete { |
---|
| 393 | my ($m) = @_; |
---|
[8203afd] | 394 | &BarnOwl::command("delete --id ".$m->id); |
---|
[f1e629d] | 395 | } |
---|
| 396 | |
---|
| 397 | sub undelete { |
---|
| 398 | my ($m) = @_; |
---|
[8203afd] | 399 | &BarnOwl::command("undelete --id ".$m->id); |
---|
[f1e629d] | 400 | } |
---|
| 401 | |
---|
| 402 | # Serializes the message into something similar to the zwgc->vt format |
---|
| 403 | sub serialize { |
---|
| 404 | my ($this) = @_; |
---|
| 405 | my $s; |
---|
| 406 | for my $f (keys %$this) { |
---|
| 407 | my $val = $this->{$f}; |
---|
| 408 | if (ref($val) eq "ARRAY") { |
---|
| 409 | for my $i (0..@$val-1) { |
---|
| 410 | my $aval; |
---|
| 411 | $aval = $val->[$i]; |
---|
| 412 | $aval =~ s/\n/\n$f.$i: /g; |
---|
[186cdc4] | 413 | $s .= "$f.$i: $aval\n"; |
---|
[f1e629d] | 414 | } |
---|
| 415 | } else { |
---|
| 416 | $val =~ s/\n/\n$f: /g; |
---|
| 417 | $s .= "$f: $val\n"; |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | return $s; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | # Populate the annoying legacy global variables |
---|
| 424 | sub legacy_populate_global { |
---|
| 425 | my ($m) = @_; |
---|
[8203afd] | 426 | $BarnOwl::direction = $m->direction ; |
---|
| 427 | $BarnOwl::type = $m->type ; |
---|
| 428 | $BarnOwl::id = $m->id ; |
---|
| 429 | $BarnOwl::class = $m->class ; |
---|
| 430 | $BarnOwl::instance = $m->instance ; |
---|
| 431 | $BarnOwl::recipient = $m->recipient ; |
---|
| 432 | $BarnOwl::sender = $m->sender ; |
---|
| 433 | $BarnOwl::realm = $m->realm ; |
---|
| 434 | $BarnOwl::opcode = $m->opcode ; |
---|
| 435 | $BarnOwl::zsig = $m->zsig ; |
---|
| 436 | $BarnOwl::msg = $m->body ; |
---|
| 437 | $BarnOwl::time = $m->time ; |
---|
| 438 | $BarnOwl::host = $m->host ; |
---|
| 439 | $BarnOwl::login = $m->login ; |
---|
| 440 | $BarnOwl::auth = $m->auth ; |
---|
[f1e629d] | 441 | if ($m->fields) { |
---|
[8203afd] | 442 | @BarnOwl::fields = @{$m->fields}; |
---|
[f1e629d] | 443 | @main::fields = @{$m->fields}; |
---|
| 444 | } else { |
---|
[8203afd] | 445 | @BarnOwl::fields = undef; |
---|
[f1e629d] | 446 | @main::fields = undef; |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | |
---|
[25729b2] | 450 | sub smartfilter { |
---|
[0337203] | 451 | die("smartfilter not supported for this message\n"); |
---|
[25729b2] | 452 | } |
---|
| 453 | |
---|
[6e6ded7] | 454 | # Display fields -- overridden by subclasses when needed |
---|
| 455 | sub login_type {""} |
---|
| 456 | sub login_extra {""} |
---|
| 457 | sub long_sender {""} |
---|
| 458 | |
---|
| 459 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
| 460 | # class |
---|
| 461 | sub context {""} |
---|
| 462 | |
---|
| 463 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
| 464 | # instance |
---|
| 465 | sub subcontext {""} |
---|
| 466 | |
---|
[f1e629d] | 467 | ##################################################################### |
---|
| 468 | ##################################################################### |
---|
| 469 | |
---|
[8203afd] | 470 | package BarnOwl::Message::Admin; |
---|
[f1e629d] | 471 | |
---|
[8203afd] | 472 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 473 | |
---|
| 474 | sub header { return shift->{"header"}; } |
---|
| 475 | |
---|
| 476 | ##################################################################### |
---|
| 477 | ##################################################################### |
---|
| 478 | |
---|
[8203afd] | 479 | package BarnOwl::Message::Generic; |
---|
[f1e629d] | 480 | |
---|
[8203afd] | 481 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 482 | |
---|
| 483 | ##################################################################### |
---|
| 484 | ##################################################################### |
---|
| 485 | |
---|
[186cdc4] | 486 | package BarnOwl::Message::Loopback; |
---|
| 487 | |
---|
| 488 | use base qw( BarnOwl::Message ); |
---|
| 489 | |
---|
[cb06a43] | 490 | # all loopback messages are private |
---|
| 491 | sub is_private { |
---|
[186cdc4] | 492 | return 1; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | ##################################################################### |
---|
| 496 | ##################################################################### |
---|
| 497 | |
---|
[8203afd] | 498 | package BarnOwl::Message::AIM; |
---|
[f1e629d] | 499 | |
---|
[8203afd] | 500 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 501 | |
---|
[cb06a43] | 502 | # all non-loginout AIM messages are private for now... |
---|
| 503 | sub is_private { |
---|
[f1e629d] | 504 | return !(shift->is_loginout); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | ##################################################################### |
---|
| 508 | ##################################################################### |
---|
| 509 | |
---|
[8203afd] | 510 | package BarnOwl::Message::Zephyr; |
---|
[f1e629d] | 511 | |
---|
[8203afd] | 512 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 513 | |
---|
[6e6ded7] | 514 | sub login_type { |
---|
| 515 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | sub login_extra { |
---|
| 519 | my $m = shift; |
---|
| 520 | return undef if (!$m->is_loginout); |
---|
| 521 | my $s = lc($m->host); |
---|
| 522 | $s .= " " . $m->login_tty if defined $m->login_tty; |
---|
| 523 | return $s; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | sub long_sender { |
---|
| 527 | my $m = shift; |
---|
| 528 | return $m->zsig; |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | sub context { |
---|
| 532 | return shift->class; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | sub subcontext { |
---|
| 536 | return shift->instance; |
---|
| 537 | } |
---|
| 538 | |
---|
[186cdc4] | 539 | sub login_tty { |
---|
[f1e629d] | 540 | my ($m) = @_; |
---|
| 541 | return undef if (!$m->is_loginout); |
---|
| 542 | return $m->fields->[2]; |
---|
| 543 | } |
---|
| 544 | |
---|
[186cdc4] | 545 | sub login_host { |
---|
[f1e629d] | 546 | my ($m) = @_; |
---|
| 547 | return undef if (!$m->is_loginout); |
---|
| 548 | return $m->fields->[0]; |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
| 552 | |
---|
| 553 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
| 554 | |
---|
[186cdc4] | 555 | sub is_personal { |
---|
[f1e629d] | 556 | my ($m) = @_; |
---|
| 557 | return ((lc($m->class) eq "message") |
---|
| 558 | && (lc($m->instance) eq "personal") |
---|
| 559 | && $m->is_private); |
---|
| 560 | } |
---|
| 561 | |
---|
[186cdc4] | 562 | sub is_mail { |
---|
[f1e629d] | 563 | my ($m) = @_; |
---|
| 564 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | sub pretty_sender { |
---|
| 568 | my ($m) = @_; |
---|
| 569 | my $sender = $m->sender; |
---|
[8203afd] | 570 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
[f1e629d] | 571 | $sender =~ s/\@$realm$//; |
---|
| 572 | return $sender; |
---|
| 573 | } |
---|
| 574 | |
---|
[ae47efb] | 575 | sub pretty_recipient { |
---|
| 576 | my ($m) = @_; |
---|
| 577 | my $recip = $m->recipient; |
---|
| 578 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
| 579 | $recip =~ s/\@$realm$//; |
---|
| 580 | return $recip; |
---|
| 581 | } |
---|
| 582 | |
---|
[f1e629d] | 583 | # These are arguably zephyr-specific |
---|
| 584 | sub class { return shift->{"class"}; } |
---|
| 585 | sub instance { return shift->{"instance"}; } |
---|
| 586 | sub realm { return shift->{"realm"}; } |
---|
| 587 | sub opcode { return shift->{"opcode"}; } |
---|
| 588 | sub host { return shift->{"hostname"}; } |
---|
| 589 | sub hostname { return shift->{"hostname"}; } |
---|
| 590 | sub header { return shift->{"header"}; } |
---|
| 591 | sub auth { return shift->{"auth"}; } |
---|
| 592 | sub fields { return shift->{"fields"}; } |
---|
| 593 | sub zsig { return shift->{"zsig"}; } |
---|
| 594 | |
---|
| 595 | ##################################################################### |
---|
| 596 | ##################################################################### |
---|
[7e470da] | 597 | ################################################################################ |
---|
| 598 | |
---|
[0337203] | 599 | package BarnOwl::Hook; |
---|
[7e470da] | 600 | |
---|
[1a64de6] | 601 | =head1 BarnOwl::Hook |
---|
| 602 | |
---|
| 603 | =head1 DESCRIPTION |
---|
| 604 | |
---|
| 605 | A C<BarnOwl::Hook> represents a list of functions to be triggered on |
---|
| 606 | some event. C<BarnOwl> exports a default set of these (see |
---|
| 607 | C<BarnOwl::Hooks>), but can also be created and used by module code. |
---|
| 608 | |
---|
| 609 | =head2 new |
---|
| 610 | |
---|
| 611 | Creates a new Hook object |
---|
| 612 | |
---|
| 613 | =cut |
---|
| 614 | |
---|
[0337203] | 615 | sub new { |
---|
| 616 | my $class = shift; |
---|
| 617 | return bless [], $class; |
---|
| 618 | } |
---|
[7e470da] | 619 | |
---|
[1a64de6] | 620 | =head2 run [ARGS] |
---|
| 621 | |
---|
| 622 | Calls each of the functions registered with this hook with the given |
---|
| 623 | arguments. |
---|
| 624 | |
---|
| 625 | =cut |
---|
| 626 | |
---|
[0337203] | 627 | sub run { |
---|
| 628 | my $self = shift; |
---|
| 629 | my @args = @_; |
---|
[167044b] | 630 | return map {$self->_run($_,@args)} @$self; |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | sub _run { |
---|
| 634 | my $self = shift; |
---|
| 635 | my $fn = shift; |
---|
| 636 | my @args = @_; |
---|
| 637 | no strict 'refs'; |
---|
| 638 | return $fn->(@args); |
---|
[7e470da] | 639 | } |
---|
[0337203] | 640 | |
---|
[1a64de6] | 641 | =head2 add SUBREF |
---|
| 642 | |
---|
| 643 | Registers a given subroutine with this hook |
---|
| 644 | |
---|
| 645 | =cut |
---|
| 646 | |
---|
[0337203] | 647 | sub add { |
---|
| 648 | my $self = shift; |
---|
| 649 | my $func = shift; |
---|
[167044b] | 650 | die("Not a coderef!") unless ref($func) eq 'CODE' || !ref($func); |
---|
| 651 | return if grep {$_ eq $func} @$self; |
---|
[0337203] | 652 | push @$self, $func; |
---|
[7e470da] | 653 | } |
---|
| 654 | |
---|
[1a64de6] | 655 | =head2 clear |
---|
| 656 | |
---|
| 657 | Remove all functions registered with this hook. |
---|
| 658 | |
---|
| 659 | =cut |
---|
| 660 | |
---|
[0337203] | 661 | sub clear { |
---|
| 662 | my $self = shift; |
---|
| 663 | @$self = (); |
---|
[7e470da] | 664 | } |
---|
| 665 | |
---|
[0337203] | 666 | package BarnOwl::Hooks; |
---|
[7e470da] | 667 | |
---|
[1a64de6] | 668 | =head1 BarnOwl::Hooks |
---|
| 669 | |
---|
| 670 | =head1 DESCRIPTION |
---|
| 671 | |
---|
| 672 | C<BarnOwl::Hooks> exports a set of C<BarnOwl::Hook> objects made |
---|
| 673 | available by BarnOwl internally. |
---|
| 674 | |
---|
| 675 | =head2 USAGE |
---|
| 676 | |
---|
| 677 | Modules wishing to respond to events in BarnOwl should register |
---|
| 678 | functions with these hooks. |
---|
| 679 | |
---|
| 680 | =head2 EXPORTS |
---|
| 681 | |
---|
| 682 | None by default. Either import the hooks you need explicitly, or refer |
---|
| 683 | to them with fully-qualified names. Available hooks are: |
---|
| 684 | |
---|
| 685 | =over 4 |
---|
| 686 | |
---|
| 687 | =item $startup |
---|
| 688 | |
---|
| 689 | Called on BarnOwl startup, and whenever modules are |
---|
| 690 | reloaded. Functions registered with the C<$startup> hook get a true |
---|
| 691 | argument if this is a reload, and false if this is a true startup |
---|
| 692 | |
---|
| 693 | =item $shutdown |
---|
| 694 | |
---|
| 695 | Called before BarnOwl shutdown |
---|
| 696 | |
---|
| 697 | =item $receiveMessage |
---|
| 698 | |
---|
| 699 | Called with a C<BarnOwl::Message> object every time BarnOwl appends a |
---|
| 700 | new message to its message list |
---|
| 701 | |
---|
| 702 | =item $mainLoop |
---|
| 703 | |
---|
[b07d8c8] | 704 | Called on every pass through the C<BarnOwl> main loop. This is |
---|
| 705 | guaranteed to be called at least once/sec and may be called more |
---|
| 706 | frequently. |
---|
[1a64de6] | 707 | |
---|
| 708 | =item $getBuddyList |
---|
| 709 | |
---|
| 710 | Called to display buddy lists for all protocol handlers. The result |
---|
| 711 | from every function registered with this hook will be appended and |
---|
| 712 | displayed in a popup window, with zephyr formatting parsed. |
---|
| 713 | |
---|
| 714 | =back |
---|
| 715 | |
---|
| 716 | =cut |
---|
| 717 | |
---|
[0337203] | 718 | use Exporter; |
---|
| 719 | |
---|
| 720 | our @EXPORT_OK = qw($startup $shutdown |
---|
[0f9eca7] | 721 | $receiveMessage $newMessage |
---|
| 722 | $mainLoop $getBuddyList); |
---|
[0337203] | 723 | |
---|
| 724 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
---|
| 725 | |
---|
| 726 | our $startup = BarnOwl::Hook->new; |
---|
| 727 | our $shutdown = BarnOwl::Hook->new; |
---|
| 728 | our $receiveMessage = BarnOwl::Hook->new; |
---|
[0f9eca7] | 729 | our $newMessage = BarnOwl::Hook->new; |
---|
[0337203] | 730 | our $mainLoop = BarnOwl::Hook->new; |
---|
| 731 | our $getBuddyList = BarnOwl::Hook->new; |
---|
| 732 | |
---|
| 733 | # Internal startup/shutdown routines called by the C code |
---|
[7e470da] | 734 | |
---|
[2650a10] | 735 | sub _load_perl_commands { |
---|
| 736 | # Load builtin perl commands |
---|
| 737 | BarnOwl::new_command(style => \&BarnOwl::Style::style_command, |
---|
| 738 | { |
---|
| 739 | summary => "creates a new style", |
---|
| 740 | usage => "style <name> perl <function_name>", |
---|
| 741 | description => |
---|
| 742 | "A style named <name> will be created that will\n" . |
---|
| 743 | "format messages using the perl function <function_name>.\n\n" . |
---|
| 744 | "SEE ALSO: show styles, view -s, filter -s\n\n" . |
---|
| 745 | "DEPRECATED in favor of BarnOwl::create_style(NAME, OBJECT)", |
---|
| 746 | }); |
---|
| 747 | } |
---|
| 748 | |
---|
[b6c067a] | 749 | sub _load_owlconf { |
---|
| 750 | # load the config file |
---|
| 751 | if ( -r $BarnOwl::configfile ) { |
---|
| 752 | undef $@; |
---|
[e8bc8ac] | 753 | package main; |
---|
[b6c067a] | 754 | do $BarnOwl::configfile; |
---|
[e2257be] | 755 | if($@) { |
---|
| 756 | BarnOwl::error("In startup: $@\n"); |
---|
| 757 | return; |
---|
| 758 | } |
---|
[e8bc8ac] | 759 | package BarnOwl; |
---|
[39dc159] | 760 | if(*BarnOwl::format_msg{CODE}) { |
---|
| 761 | # if the config defines a legacy formatting function, add 'perl' as a style |
---|
[b67ab6b] | 762 | BarnOwl::create_style("perl", BarnOwl::Style::Legacy->new( |
---|
| 763 | "BarnOwl::format_msg", |
---|
| 764 | "User-defined perl style that calls BarnOwl::format_msg" |
---|
| 765 | . " with legacy global variable support", |
---|
| 766 | 1)); |
---|
| 767 | BarnOwl::set("-q default_style perl"); |
---|
[39dc159] | 768 | } |
---|
[b6c067a] | 769 | } |
---|
| 770 | } |
---|
| 771 | |
---|
[b0c8011] | 772 | # These are the internal hooks called by the barnowl C code, which |
---|
| 773 | # take care of dispatching to the appropriate perl hooks, and deal |
---|
| 774 | # with compatibility by calling the old, fixed-name hooks. |
---|
| 775 | |
---|
[0337203] | 776 | sub _startup { |
---|
[2650a10] | 777 | _load_perl_commands(); |
---|
[0337203] | 778 | _load_owlconf(); |
---|
[8203afd] | 779 | |
---|
[0337203] | 780 | if(eval {require BarnOwl::ModuleLoader}) { |
---|
| 781 | eval { |
---|
| 782 | BarnOwl::ModuleLoader->load_all; |
---|
| 783 | }; |
---|
[f6b319c] | 784 | BarnOwl::error("$@") if $@; |
---|
| 785 | |
---|
[0337203] | 786 | } else { |
---|
| 787 | BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); |
---|
| 788 | } |
---|
| 789 | |
---|
[836e6263] | 790 | $startup->run(0); |
---|
[8203afd] | 791 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
| 792 | } |
---|
| 793 | |
---|
[0337203] | 794 | sub _shutdown { |
---|
| 795 | $shutdown->run; |
---|
| 796 | |
---|
[8203afd] | 797 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
---|
| 798 | } |
---|
| 799 | |
---|
[0337203] | 800 | sub _receive_msg { |
---|
[7e470da] | 801 | my $m = shift; |
---|
[0337203] | 802 | |
---|
| 803 | $receiveMessage->run($m); |
---|
| 804 | |
---|
[8203afd] | 805 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
[7e470da] | 806 | } |
---|
| 807 | |
---|
[0f9eca7] | 808 | sub _new_msg { |
---|
| 809 | my $m = shift; |
---|
| 810 | |
---|
| 811 | $newMessage->run($m); |
---|
| 812 | |
---|
| 813 | BarnOwl::new_msg($m) if *BarnOwl::new_msg{CODE}; |
---|
| 814 | } |
---|
| 815 | |
---|
[0337203] | 816 | sub _mainloop_hook { |
---|
| 817 | $mainLoop->run; |
---|
| 818 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
| 819 | } |
---|
[7e470da] | 820 | |
---|
[0337203] | 821 | sub _get_blist { |
---|
| 822 | return join("\n", $getBuddyList->run); |
---|
[7e470da] | 823 | } |
---|
[dd16bdd] | 824 | |
---|
[b6c067a] | 825 | ################################################################################ |
---|
| 826 | # Built-in perl styles |
---|
| 827 | ################################################################################ |
---|
| 828 | package BarnOwl::Style::Default; |
---|
| 829 | ################################################################################ |
---|
| 830 | # Branching point for various formatting functions in this style. |
---|
| 831 | ################################################################################ |
---|
[426735d] | 832 | sub format_message |
---|
[b6c067a] | 833 | { |
---|
[864ed35] | 834 | my $self = shift; |
---|
[811ad93] | 835 | my $m = shift; |
---|
| 836 | my $fmt; |
---|
[b6c067a] | 837 | |
---|
[6e6ded7] | 838 | if ( $m->is_loginout) { |
---|
[811ad93] | 839 | $fmt = $self->format_login($m); |
---|
[18fb3d4f] | 840 | } elsif($m->is_ping && $m->is_personal) { |
---|
[811ad93] | 841 | $fmt = $self->format_ping($m); |
---|
[6e6ded7] | 842 | } elsif($m->is_admin) { |
---|
[811ad93] | 843 | $fmt = $self->format_admin($m); |
---|
[6e6ded7] | 844 | } else { |
---|
[811ad93] | 845 | $fmt = $self->format_chat($m); |
---|
[b6c067a] | 846 | } |
---|
[811ad93] | 847 | $fmt = BarnOwl::Style::boldify($fmt) if $self->should_bold($m); |
---|
| 848 | return $fmt; |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | sub should_bold { |
---|
| 852 | my $self = shift; |
---|
| 853 | my $m = shift; |
---|
| 854 | return $m->is_personal && $m->direction eq "in"; |
---|
[b6c067a] | 855 | } |
---|
| 856 | |
---|
[864ed35] | 857 | sub description {"Default style";} |
---|
| 858 | |
---|
[b67ab6b] | 859 | BarnOwl::create_style("default", "BarnOwl::Style::Default"); |
---|
[b6c067a] | 860 | |
---|
| 861 | ################################################################################ |
---|
| 862 | |
---|
[426735d] | 863 | sub format_time { |
---|
[6b3878b] | 864 | my $self = shift; |
---|
[6e6ded7] | 865 | my $m = shift; |
---|
[b6c067a] | 866 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
---|
[6e6ded7] | 867 | return $time; |
---|
[b6c067a] | 868 | } |
---|
| 869 | |
---|
[426735d] | 870 | sub format_login { |
---|
[864ed35] | 871 | my $self = shift; |
---|
[b6c067a] | 872 | my $m = shift; |
---|
[6e6ded7] | 873 | return sprintf( |
---|
| 874 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
| 875 | uc( $m->login ), |
---|
| 876 | $m->login_type, |
---|
| 877 | $m->pretty_sender, |
---|
| 878 | $m->login_extra, |
---|
[426735d] | 879 | $self->format_time($m) |
---|
[6e6ded7] | 880 | ); |
---|
[b6c067a] | 881 | } |
---|
| 882 | |
---|
[864ed35] | 883 | sub format_ping { |
---|
| 884 | my $self = shift; |
---|
| 885 | my $m = shift; |
---|
[2017d07] | 886 | return "\@b(PING) from \@b(" . $m->pretty_sender . ")"; |
---|
[864ed35] | 887 | } |
---|
| 888 | |
---|
| 889 | sub format_admin { |
---|
| 890 | my $self = shift; |
---|
| 891 | my $m = shift; |
---|
[811ad93] | 892 | return "\@bold(OWL ADMIN)\n" . $self->indent_body($m); |
---|
[864ed35] | 893 | } |
---|
| 894 | |
---|
[426735d] | 895 | sub format_chat { |
---|
[864ed35] | 896 | my $self = shift; |
---|
[b6c067a] | 897 | my $m = shift; |
---|
[811ad93] | 898 | my $header = $self->chat_header($m); |
---|
| 899 | return $header . "\n". $self->indent_body($m); |
---|
| 900 | } |
---|
| 901 | |
---|
| 902 | sub chat_header { |
---|
| 903 | my $self = shift; |
---|
| 904 | my $m = shift; |
---|
[6e6ded7] | 905 | my $header; |
---|
| 906 | if ( $m->is_personal ) { |
---|
| 907 | if ( $m->direction eq "out" ) { |
---|
| 908 | $header = ucfirst $m->type . " sent to " . $m->pretty_recipient; |
---|
| 909 | } else { |
---|
| 910 | $header = ucfirst $m->type . " from " . $m->pretty_sender; |
---|
| 911 | } |
---|
| 912 | } else { |
---|
| 913 | $header = $m->context; |
---|
[37dd88c] | 914 | if(defined $m->subcontext) { |
---|
[c39999f] | 915 | $header .= ' / ' . $m->subcontext; |
---|
[6e6ded7] | 916 | } |
---|
[c39999f] | 917 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
[6e6ded7] | 918 | } |
---|
[b6c067a] | 919 | |
---|
[b51d257] | 920 | if($m->opcode) { |
---|
| 921 | $header .= " [" . $m->opcode . "]"; |
---|
| 922 | } |
---|
[426735d] | 923 | $header .= " " . $self->format_time($m); |
---|
[811ad93] | 924 | $header .= $self->format_sender($m); |
---|
| 925 | return $header; |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | sub format_sender { |
---|
| 929 | my $self = shift; |
---|
| 930 | my $m = shift; |
---|
[0449730] | 931 | my $sender = $m->long_sender; |
---|
| 932 | $sender =~ s/\n.*$//s; |
---|
[811ad93] | 933 | return " (" . $sender . '@color[default]' . ")"; |
---|
[b6c067a] | 934 | } |
---|
| 935 | |
---|
[426735d] | 936 | sub indent_body |
---|
[b6c067a] | 937 | { |
---|
[811ad93] | 938 | my $self = shift; |
---|
[b6c067a] | 939 | my $m = shift; |
---|
[186cdc4] | 940 | |
---|
[b6c067a] | 941 | my $body = $m->body; |
---|
[f6b319c] | 942 | if ($m->{should_wordwrap}) { |
---|
| 943 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-8); |
---|
| 944 | } |
---|
[186cdc4] | 945 | # replace newline followed by anything with |
---|
[b6c067a] | 946 | # newline plus four spaces and that thing. |
---|
| 947 | $body =~ s/\n(.)/\n $1/g; |
---|
[f2d72128] | 948 | # Trim trailing newlines. |
---|
| 949 | $body =~ s/\n*$//; |
---|
[b6c067a] | 950 | return " ".$body; |
---|
| 951 | } |
---|
| 952 | |
---|
[864ed35] | 953 | package BarnOwl::Style::Basic; |
---|
| 954 | our @ISA=qw(BarnOwl::Style::Default); |
---|
| 955 | |
---|
| 956 | sub description {"Compatability alias for the default style";} |
---|
| 957 | |
---|
[b67ab6b] | 958 | BarnOwl::create_style("basic", "BarnOwl::Style::Basic"); |
---|
[864ed35] | 959 | |
---|
[f2d72128] | 960 | package BarnOwl::Style::OneLine; |
---|
[811ad93] | 961 | # Inherit format_message to dispatch |
---|
| 962 | our @ISA = qw(BarnOwl::Style::Default); |
---|
[f2d72128] | 963 | |
---|
[811ad93] | 964 | use constant BASE_FORMAT => '%s %-13.13s %-11.11s %-12.12s '; |
---|
[f2d72128] | 965 | |
---|
[864ed35] | 966 | sub description {"Formats for one-line-per-message"} |
---|
| 967 | |
---|
[b67ab6b] | 968 | BarnOwl::create_style("oneline", "BarnOwl::Style::OneLine"); |
---|
[f2d72128] | 969 | |
---|
| 970 | ################################################################################ |
---|
| 971 | |
---|
[426735d] | 972 | sub format_login { |
---|
[864ed35] | 973 | my $self = shift; |
---|
[f2d72128] | 974 | my $m = shift; |
---|
| 975 | return sprintf( |
---|
| 976 | BASE_FORMAT, |
---|
| 977 | '<', |
---|
| 978 | $m->type, |
---|
| 979 | uc( $m->login ), |
---|
| 980 | $m->pretty_sender) |
---|
| 981 | . ($m->login_extra ? "at ".$m->login_extra : ''); |
---|
| 982 | } |
---|
| 983 | |
---|
[426735d] | 984 | sub format_ping { |
---|
[2017d07] | 985 | my $self = shift; |
---|
[f2d72128] | 986 | my $m = shift; |
---|
| 987 | return sprintf( |
---|
| 988 | BASE_FORMAT, |
---|
| 989 | '<', |
---|
| 990 | $m->type, |
---|
| 991 | 'PING', |
---|
| 992 | $m->pretty_sender) |
---|
| 993 | } |
---|
| 994 | |
---|
[426735d] | 995 | sub format_chat |
---|
[f2d72128] | 996 | { |
---|
[864ed35] | 997 | my $self = shift; |
---|
[f2d72128] | 998 | my $m = shift; |
---|
| 999 | my $dir = lc($m->{direction}); |
---|
| 1000 | my $dirsym = '-'; |
---|
| 1001 | if ($dir eq 'in') { |
---|
| 1002 | $dirsym = '<'; |
---|
| 1003 | } |
---|
| 1004 | elsif ($dir eq 'out') { |
---|
| 1005 | $dirsym = '>'; |
---|
| 1006 | } |
---|
| 1007 | |
---|
| 1008 | my $line; |
---|
| 1009 | if ($m->is_personal) { |
---|
| 1010 | $line= sprintf(BASE_FORMAT, |
---|
[811ad93] | 1011 | $dirsym, |
---|
| 1012 | $m->type, |
---|
| 1013 | '', |
---|
| 1014 | ($dir eq 'out' |
---|
| 1015 | ? $m->pretty_recipient |
---|
| 1016 | : $m->pretty_sender)); |
---|
[f2d72128] | 1017 | } |
---|
| 1018 | else { |
---|
| 1019 | $line = sprintf(BASE_FORMAT, |
---|
[811ad93] | 1020 | $dirsym, |
---|
| 1021 | $m->context, |
---|
| 1022 | $m->subcontext, |
---|
| 1023 | ($dir eq 'out' |
---|
| 1024 | ? $m->pretty_recipient |
---|
| 1025 | : $m->pretty_sender)); |
---|
[f2d72128] | 1026 | } |
---|
| 1027 | |
---|
| 1028 | my $body = $m->{body}; |
---|
| 1029 | $body =~ tr/\n/ /; |
---|
| 1030 | $line .= $body; |
---|
| 1031 | return $line; |
---|
| 1032 | } |
---|
| 1033 | |
---|
[811ad93] | 1034 | # Format owl admin messages |
---|
[426735d] | 1035 | sub format_admin |
---|
[f2d72128] | 1036 | { |
---|
[864ed35] | 1037 | my $self = shift; |
---|
[f2d72128] | 1038 | my $m = shift; |
---|
[811ad93] | 1039 | my $line = sprintf(BASE_FORMAT, '<', 'ADMIN', '', ''); |
---|
[f2d72128] | 1040 | my $body = $m->{body}; |
---|
| 1041 | $body =~ tr/\n/ /; |
---|
| 1042 | return $line.$body; |
---|
| 1043 | } |
---|
[b6c067a] | 1044 | |
---|
| 1045 | package BarnOwl::Style; |
---|
| 1046 | |
---|
| 1047 | # This takes a zephyr to be displayed and modifies it to be displayed |
---|
| 1048 | # entirely in bold. |
---|
[426735d] | 1049 | sub boldify |
---|
[b6c067a] | 1050 | { |
---|
| 1051 | local $_ = shift; |
---|
| 1052 | if ( !(/\)/) ) { |
---|
| 1053 | return '@b(' . $_ . ')'; |
---|
| 1054 | } elsif ( !(/\>/) ) { |
---|
| 1055 | return '@b<' . $_ . '>'; |
---|
| 1056 | } elsif ( !(/\}/) ) { |
---|
| 1057 | return '@b{' . $_ . '}'; |
---|
| 1058 | } elsif ( !(/\]/) ) { |
---|
| 1059 | return '@b[' . $_ . ']'; |
---|
| 1060 | } else { |
---|
| 1061 | my $txt = "\@b($_"; |
---|
| 1062 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
| 1063 | return $txt . ')'; |
---|
| 1064 | } |
---|
| 1065 | } |
---|
| 1066 | |
---|
[2650a10] | 1067 | sub style_command { |
---|
| 1068 | my $command = shift; |
---|
| 1069 | if(scalar @_ != 3 || $_[1] ne 'perl') { |
---|
| 1070 | die("Usage: style <name> perl <function>\n"); |
---|
| 1071 | } |
---|
| 1072 | my $name = shift; |
---|
| 1073 | my $perl = shift; |
---|
| 1074 | my $fn = shift; |
---|
| 1075 | { |
---|
[b441079] | 1076 | # For historical reasons, assume unqualified references are |
---|
| 1077 | # in main:: |
---|
| 1078 | package main; |
---|
[2650a10] | 1079 | no strict 'refs'; |
---|
| 1080 | unless(*{$fn}{CODE}) { |
---|
| 1081 | die("Unable to create style '$name': no perl function '$fn'\n"); |
---|
| 1082 | } |
---|
| 1083 | } |
---|
| 1084 | BarnOwl::create_style($name, BarnOwl::Style::Legacy->new($fn)); |
---|
| 1085 | } |
---|
| 1086 | |
---|
[b67ab6b] | 1087 | package BarnOwl::Style::Legacy; |
---|
| 1088 | |
---|
| 1089 | sub new { |
---|
| 1090 | my $class = shift; |
---|
| 1091 | my $func = shift; |
---|
| 1092 | my $desc = shift; |
---|
| 1093 | my $useglobals = shift; |
---|
| 1094 | $useglobals = 0 unless defined($useglobals); |
---|
| 1095 | return bless {function => $func, |
---|
| 1096 | description => $desc, |
---|
| 1097 | useglobals => $useglobals}, $class; |
---|
| 1098 | } |
---|
| 1099 | |
---|
[2650a10] | 1100 | sub description { |
---|
| 1101 | my $self = shift; |
---|
| 1102 | return $self->{description} || |
---|
| 1103 | ("User-defined perl style that calls " . $self->{function}); |
---|
| 1104 | }; |
---|
[b67ab6b] | 1105 | |
---|
| 1106 | sub format_message { |
---|
| 1107 | my $self = shift; |
---|
| 1108 | if($self->{useglobals}) { |
---|
| 1109 | $_[0]->legacy_populate_global(); |
---|
| 1110 | } |
---|
[b441079] | 1111 | { |
---|
| 1112 | package main; |
---|
| 1113 | no strict 'refs'; |
---|
| 1114 | goto \&{$self->{function}}; |
---|
| 1115 | } |
---|
[b67ab6b] | 1116 | } |
---|
| 1117 | |
---|
[b6c067a] | 1118 | |
---|
[f1e629d] | 1119 | # switch to package main when we're done |
---|
| 1120 | package main; |
---|
[0337203] | 1121 | |
---|
| 1122 | # Shove a bunch of fake entries into @INC so modules can use or |
---|
| 1123 | # require them without choking |
---|
| 1124 | $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
| 1125 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
[f1e629d] | 1126 | |
---|
| 1127 | 1; |
---|
[0337203] | 1128 | |
---|