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