| 1 | # $Id$ |
|---|
| 2 | # |
|---|
| 3 | # This is all linked into the binary and evaluated when perl starts up... |
|---|
| 4 | # |
|---|
| 5 | ##################################################################### |
|---|
| 6 | ##################################################################### |
|---|
| 7 | # XXX NOTE: This file is sourced before almost any barnowl |
|---|
| 8 | # architecture is loaded. This means, for example, that it cannot |
|---|
| 9 | # execute any owl commands. Any code that needs to do so, should |
|---|
| 10 | # create a function wrapping it and push it onto @onStartSubs |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | use strict; |
|---|
| 14 | use warnings; |
|---|
| 15 | |
|---|
| 16 | package BarnOwl; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | BEGIN { |
|---|
| 20 | # bootstrap in C bindings and glue |
|---|
| 21 | *owl:: = \*BarnOwl::; |
|---|
| 22 | bootstrap BarnOwl 1.2; |
|---|
| 23 | }; |
|---|
| 24 | |
|---|
| 25 | use lib(get_data_dir()."/lib"); |
|---|
| 26 | use lib($::ENV{'HOME'}."/.owl/lib"); |
|---|
| 27 | |
|---|
| 28 | our $configfile; |
|---|
| 29 | |
|---|
| 30 | $configfile ||= $::ENV{'HOME'}."/.owlconf"; |
|---|
| 31 | |
|---|
| 32 | # populate global variable space for legacy owlconf files |
|---|
| 33 | sub _format_msg_legacy_wrap { |
|---|
| 34 | my ($m) = @_; |
|---|
| 35 | $m->legacy_populate_global(); |
|---|
| 36 | return &BarnOwl::format_msg($m); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | # populate global variable space for legacy owlconf files |
|---|
| 40 | sub _receive_msg_legacy_wrap { |
|---|
| 41 | my ($m) = @_; |
|---|
| 42 | $m->legacy_populate_global(); |
|---|
| 43 | return &BarnOwl::Hooks::receive_msg($m); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
|---|
| 47 | sub AUTOLOAD { |
|---|
| 48 | our $AUTOLOAD; |
|---|
| 49 | my $called = $AUTOLOAD; |
|---|
| 50 | $called =~ s/.*:://; |
|---|
| 51 | $called =~ s/_/-/g; |
|---|
| 52 | return &BarnOwl::command("$called ".join(" ",@_)); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | =head2 new_command NAME FUNC [{ARGS}] |
|---|
| 56 | |
|---|
| 57 | Add a new owl command. When owl executes the command NAME, FUNC will |
|---|
| 58 | be called with the arguments passed to the command, with NAME as the |
|---|
| 59 | first argument. |
|---|
| 60 | |
|---|
| 61 | ARGS should be a hashref containing any or all of C<summary>, |
|---|
| 62 | C<usage>, or C<description> keys. |
|---|
| 63 | |
|---|
| 64 | =cut |
|---|
| 65 | |
|---|
| 66 | sub new_command { |
|---|
| 67 | my $name = shift; |
|---|
| 68 | my $func = shift; |
|---|
| 69 | my $args = shift || {}; |
|---|
| 70 | my %args = ( |
|---|
| 71 | summary => undef, |
|---|
| 72 | usage => undef, |
|---|
| 73 | description => undef, |
|---|
| 74 | %{$args} |
|---|
| 75 | ); |
|---|
| 76 | |
|---|
| 77 | no warnings 'uninitialized'; |
|---|
| 78 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | ##################################################################### |
|---|
| 82 | ##################################################################### |
|---|
| 83 | |
|---|
| 84 | package BarnOwl::Message; |
|---|
| 85 | |
|---|
| 86 | sub new { |
|---|
| 87 | my $class = shift; |
|---|
| 88 | my %args = (@_); |
|---|
| 89 | if($class eq __PACKAGE__ && $args{type}) { |
|---|
| 90 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
|---|
| 91 | } |
|---|
| 92 | return bless {%args}, $class; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | sub type { return shift->{"type"}; } |
|---|
| 96 | sub direction { return shift->{"direction"}; } |
|---|
| 97 | sub time { return shift->{"time"}; } |
|---|
| 98 | sub id { return shift->{"id"}; } |
|---|
| 99 | sub body { return shift->{"body"}; } |
|---|
| 100 | sub sender { return shift->{"sender"}; } |
|---|
| 101 | sub recipient { return shift->{"recipient"}; } |
|---|
| 102 | sub login { return shift->{"login"}; } |
|---|
| 103 | sub is_private { return shift->{"private"}; } |
|---|
| 104 | |
|---|
| 105 | sub is_login { return shift->login eq "login"; } |
|---|
| 106 | sub is_logout { return shift->login eq "logout"; } |
|---|
| 107 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
|---|
| 108 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
|---|
| 109 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
|---|
| 110 | |
|---|
| 111 | sub is_deleted { return shift->{"deleted"}; } |
|---|
| 112 | |
|---|
| 113 | sub is_admin { return (shift->{"type"} eq "admin"); } |
|---|
| 114 | sub is_generic { return (shift->{"type"} eq "generic"); } |
|---|
| 115 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
|---|
| 116 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
|---|
| 117 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
|---|
| 118 | sub is_icq { return (shift->{"type"} eq "icq"); } |
|---|
| 119 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
|---|
| 120 | sub is_msn { return (shift->{"type"} eq "msn"); } |
|---|
| 121 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
|---|
| 122 | |
|---|
| 123 | # These are overridden by appropriate message types |
|---|
| 124 | sub is_ping { return 0; } |
|---|
| 125 | sub is_mail { return 0; } |
|---|
| 126 | sub is_personal { return shift->is_private; } |
|---|
| 127 | sub class { return undef; } |
|---|
| 128 | sub instance { return undef; } |
|---|
| 129 | sub realm { return undef; } |
|---|
| 130 | sub opcode { return undef; } |
|---|
| 131 | sub header { return undef; } |
|---|
| 132 | sub host { return undef; } |
|---|
| 133 | sub hostname { return undef; } |
|---|
| 134 | sub auth { return undef; } |
|---|
| 135 | sub fields { return undef; } |
|---|
| 136 | sub zsig { return undef; } |
|---|
| 137 | sub zwriteline { return undef; } |
|---|
| 138 | sub login_host { return undef; } |
|---|
| 139 | sub login_tty { return undef; } |
|---|
| 140 | |
|---|
| 141 | sub pretty_sender { return shift->sender; } |
|---|
| 142 | sub pretty_recipient { return shift->recipient; } |
|---|
| 143 | |
|---|
| 144 | sub delete { |
|---|
| 145 | my ($m) = @_; |
|---|
| 146 | &BarnOwl::command("delete --id ".$m->id); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | sub undelete { |
|---|
| 150 | my ($m) = @_; |
|---|
| 151 | &BarnOwl::command("undelete --id ".$m->id); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | # Serializes the message into something similar to the zwgc->vt format |
|---|
| 155 | sub serialize { |
|---|
| 156 | my ($this) = @_; |
|---|
| 157 | my $s; |
|---|
| 158 | for my $f (keys %$this) { |
|---|
| 159 | my $val = $this->{$f}; |
|---|
| 160 | if (ref($val) eq "ARRAY") { |
|---|
| 161 | for my $i (0..@$val-1) { |
|---|
| 162 | my $aval; |
|---|
| 163 | $aval = $val->[$i]; |
|---|
| 164 | $aval =~ s/\n/\n$f.$i: /g; |
|---|
| 165 | $s .= "$f.$i: $aval\n"; |
|---|
| 166 | } |
|---|
| 167 | } else { |
|---|
| 168 | $val =~ s/\n/\n$f: /g; |
|---|
| 169 | $s .= "$f: $val\n"; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | return $s; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | # Populate the annoying legacy global variables |
|---|
| 176 | sub legacy_populate_global { |
|---|
| 177 | my ($m) = @_; |
|---|
| 178 | $BarnOwl::direction = $m->direction ; |
|---|
| 179 | $BarnOwl::type = $m->type ; |
|---|
| 180 | $BarnOwl::id = $m->id ; |
|---|
| 181 | $BarnOwl::class = $m->class ; |
|---|
| 182 | $BarnOwl::instance = $m->instance ; |
|---|
| 183 | $BarnOwl::recipient = $m->recipient ; |
|---|
| 184 | $BarnOwl::sender = $m->sender ; |
|---|
| 185 | $BarnOwl::realm = $m->realm ; |
|---|
| 186 | $BarnOwl::opcode = $m->opcode ; |
|---|
| 187 | $BarnOwl::zsig = $m->zsig ; |
|---|
| 188 | $BarnOwl::msg = $m->body ; |
|---|
| 189 | $BarnOwl::time = $m->time ; |
|---|
| 190 | $BarnOwl::host = $m->host ; |
|---|
| 191 | $BarnOwl::login = $m->login ; |
|---|
| 192 | $BarnOwl::auth = $m->auth ; |
|---|
| 193 | if ($m->fields) { |
|---|
| 194 | @BarnOwl::fields = @{$m->fields}; |
|---|
| 195 | @main::fields = @{$m->fields}; |
|---|
| 196 | } else { |
|---|
| 197 | @BarnOwl::fields = undef; |
|---|
| 198 | @main::fields = undef; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | sub smartfilter { |
|---|
| 203 | die("smartfilter not supported for this message"); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | ##################################################################### |
|---|
| 207 | ##################################################################### |
|---|
| 208 | |
|---|
| 209 | package BarnOwl::Message::Admin; |
|---|
| 210 | |
|---|
| 211 | use base qw( BarnOwl::Message ); |
|---|
| 212 | |
|---|
| 213 | sub header { return shift->{"header"}; } |
|---|
| 214 | |
|---|
| 215 | ##################################################################### |
|---|
| 216 | ##################################################################### |
|---|
| 217 | |
|---|
| 218 | package BarnOwl::Message::Generic; |
|---|
| 219 | |
|---|
| 220 | use base qw( BarnOwl::Message ); |
|---|
| 221 | |
|---|
| 222 | ##################################################################### |
|---|
| 223 | ##################################################################### |
|---|
| 224 | |
|---|
| 225 | package BarnOwl::Message::AIM; |
|---|
| 226 | |
|---|
| 227 | use base qw( BarnOwl::Message ); |
|---|
| 228 | |
|---|
| 229 | # all non-loginout AIM messages are personal for now... |
|---|
| 230 | sub is_personal { |
|---|
| 231 | return !(shift->is_loginout); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | ##################################################################### |
|---|
| 235 | ##################################################################### |
|---|
| 236 | |
|---|
| 237 | package BarnOwl::Message::Zephyr; |
|---|
| 238 | |
|---|
| 239 | use base qw( BarnOwl::Message ); |
|---|
| 240 | |
|---|
| 241 | sub login_tty { |
|---|
| 242 | my ($m) = @_; |
|---|
| 243 | return undef if (!$m->is_loginout); |
|---|
| 244 | return $m->fields->[2]; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | sub login_host { |
|---|
| 248 | my ($m) = @_; |
|---|
| 249 | return undef if (!$m->is_loginout); |
|---|
| 250 | return $m->fields->[0]; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | sub zwriteline { return shift->{"zwriteline"}; } |
|---|
| 254 | |
|---|
| 255 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
|---|
| 256 | |
|---|
| 257 | sub is_personal { |
|---|
| 258 | my ($m) = @_; |
|---|
| 259 | return ((lc($m->class) eq "message") |
|---|
| 260 | && (lc($m->instance) eq "personal") |
|---|
| 261 | && $m->is_private); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | sub is_mail { |
|---|
| 265 | my ($m) = @_; |
|---|
| 266 | return ((lc($m->class) eq "mail") && $m->is_private); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | sub pretty_sender { |
|---|
| 270 | my ($m) = @_; |
|---|
| 271 | my $sender = $m->sender; |
|---|
| 272 | my $realm = BarnOwl::zephyr_getrealm(); |
|---|
| 273 | $sender =~ s/\@$realm$//; |
|---|
| 274 | return $sender; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | sub pretty_recipient { |
|---|
| 278 | my ($m) = @_; |
|---|
| 279 | my $recip = $m->recipient; |
|---|
| 280 | my $realm = BarnOwl::zephyr_getrealm(); |
|---|
| 281 | $recip =~ s/\@$realm$//; |
|---|
| 282 | return $recip; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | # These are arguably zephyr-specific |
|---|
| 286 | sub class { return shift->{"class"}; } |
|---|
| 287 | sub instance { return shift->{"instance"}; } |
|---|
| 288 | sub realm { return shift->{"realm"}; } |
|---|
| 289 | sub opcode { return shift->{"opcode"}; } |
|---|
| 290 | sub host { return shift->{"hostname"}; } |
|---|
| 291 | sub hostname { return shift->{"hostname"}; } |
|---|
| 292 | sub header { return shift->{"header"}; } |
|---|
| 293 | sub auth { return shift->{"auth"}; } |
|---|
| 294 | sub fields { return shift->{"fields"}; } |
|---|
| 295 | sub zsig { return shift->{"zsig"}; } |
|---|
| 296 | |
|---|
| 297 | ##################################################################### |
|---|
| 298 | ##################################################################### |
|---|
| 299 | ################################################################################ |
|---|
| 300 | package BarnOwl; |
|---|
| 301 | |
|---|
| 302 | ################################################################################ |
|---|
| 303 | # Mainloop hook |
|---|
| 304 | ################################################################################ |
|---|
| 305 | |
|---|
| 306 | our $shutdown; |
|---|
| 307 | $shutdown = 0; |
|---|
| 308 | our $reload; |
|---|
| 309 | $reload = 0; |
|---|
| 310 | |
|---|
| 311 | #Run this on start and reload. Adds modules |
|---|
| 312 | sub onStart |
|---|
| 313 | { |
|---|
| 314 | reload_init(); |
|---|
| 315 | loadModules(); |
|---|
| 316 | } |
|---|
| 317 | ################################################################################ |
|---|
| 318 | # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl |
|---|
| 319 | ################################################################################ |
|---|
| 320 | sub reload_hook (@) |
|---|
| 321 | { |
|---|
| 322 | BarnOwl::Hooks::startup(); |
|---|
| 323 | return 1; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | sub reload |
|---|
| 327 | { |
|---|
| 328 | # Use $reload to tell modules that we're performing a reload. |
|---|
| 329 | { |
|---|
| 330 | local $reload = 1; |
|---|
| 331 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | @BarnOwl::Hooks::onMainLoop = (); |
|---|
| 335 | @BarnOwl::Hooks::onStartSubs = (); |
|---|
| 336 | |
|---|
| 337 | # Do reload |
|---|
| 338 | package main; |
|---|
| 339 | if (-r $BarnOwl::configfile) { |
|---|
| 340 | undef $@; |
|---|
| 341 | do $BarnOwl::configfile; |
|---|
| 342 | BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@; |
|---|
| 343 | } |
|---|
| 344 | BarnOwl::reload_hook(@_); |
|---|
| 345 | package BarnOwl; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | sub reload_init () |
|---|
| 349 | { |
|---|
| 350 | BarnOwl::command('alias reload perl BarnOwl::reload()'); |
|---|
| 351 | BarnOwl::command('bindkey global "C-x C-r" command reload'); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | ################################################################################ |
|---|
| 355 | # Loads modules from ~/.owl/modules and owl's data directory |
|---|
| 356 | ################################################################################ |
|---|
| 357 | |
|---|
| 358 | sub loadModules () { |
|---|
| 359 | my @modules; |
|---|
| 360 | my $rv; |
|---|
| 361 | foreach my $dir ( BarnOwl::get_data_dir() . "/modules", |
|---|
| 362 | $ENV{HOME} . "/.owl/modules" ) |
|---|
| 363 | { |
|---|
| 364 | opendir( MODULES, $dir ); |
|---|
| 365 | |
|---|
| 366 | # source ./modules/*.pl |
|---|
| 367 | @modules = sort grep( /\.pl$/, readdir(MODULES) ); |
|---|
| 368 | |
|---|
| 369 | foreach my $mod (@modules) { |
|---|
| 370 | unless ($rv = do "$dir/$mod") { |
|---|
| 371 | BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@; |
|---|
| 372 | BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv; |
|---|
| 373 | } |
|---|
| 374 | } |
|---|
| 375 | closedir(MODULES); |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | sub _load_owlconf { |
|---|
| 380 | # Only do this the first time |
|---|
| 381 | return if $BarnOwl::reload; |
|---|
| 382 | # load the config file |
|---|
| 383 | if ( -r $BarnOwl::configfile ) { |
|---|
| 384 | undef $@; |
|---|
| 385 | do $BarnOwl::configfile; |
|---|
| 386 | die $@ if $@; |
|---|
| 387 | } |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | push @BarnOwl::Hooks::onStartSubs, \&_load_owlconf; |
|---|
| 391 | |
|---|
| 392 | package BarnOwl::Hooks; |
|---|
| 393 | |
|---|
| 394 | # Arrays of subrefs to be called at specific times. |
|---|
| 395 | our @onStartSubs = (); |
|---|
| 396 | our @onReceiveMsg = (); |
|---|
| 397 | our @onMainLoop = (); |
|---|
| 398 | our @onGetBuddyList = (); |
|---|
| 399 | |
|---|
| 400 | # Functions to call hook lists |
|---|
| 401 | sub runHook($@) |
|---|
| 402 | { |
|---|
| 403 | my $hook = shift; |
|---|
| 404 | my @args = @_; |
|---|
| 405 | $_->(@args) for (@$hook); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | sub runHook_accumulate($@) |
|---|
| 409 | { |
|---|
| 410 | my $hook = shift; |
|---|
| 411 | my @args = @_; |
|---|
| 412 | return join("\n", map {$_->(@args)} @$hook); |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | ################################################################################ |
|---|
| 416 | # Startup and Shutdown code |
|---|
| 417 | ################################################################################ |
|---|
| 418 | sub startup |
|---|
| 419 | { |
|---|
| 420 | # Modern versions of owl provides a great place to have startup stuff. |
|---|
| 421 | # Put things in ~/.owl/startup |
|---|
| 422 | |
|---|
| 423 | #So that the user's .owlconf can have startsubs, we don't clear |
|---|
| 424 | #onStartSubs; reload does however |
|---|
| 425 | @onReceiveMsg = (); |
|---|
| 426 | @onMainLoop = (); |
|---|
| 427 | @onGetBuddyList = (); |
|---|
| 428 | |
|---|
| 429 | BarnOwl::onStart(); |
|---|
| 430 | |
|---|
| 431 | runHook(\@onStartSubs); |
|---|
| 432 | |
|---|
| 433 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | sub shutdown |
|---|
| 437 | { |
|---|
| 438 | # Modern versions of owl provides a great place to have shutdown stuff. |
|---|
| 439 | # Put things in ~/.owl/shutdown |
|---|
| 440 | |
|---|
| 441 | # use $shutdown to tell modules that that's what we're doing. |
|---|
| 442 | $BarnOwl::shutdown = 1; |
|---|
| 443 | BarnOwl::mainloop_hook(); |
|---|
| 444 | |
|---|
| 445 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | sub mainloop_hook |
|---|
| 449 | { |
|---|
| 450 | runHook(\@onMainLoop); |
|---|
| 451 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | ################################################################################ |
|---|
| 455 | # Hooks into receive_msg() |
|---|
| 456 | ################################################################################ |
|---|
| 457 | |
|---|
| 458 | sub receive_msg |
|---|
| 459 | { |
|---|
| 460 | my $m = shift; |
|---|
| 461 | runHook(\@onReceiveMsg, $m); |
|---|
| 462 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | ################################################################################ |
|---|
| 466 | # Hooks into get_blist() |
|---|
| 467 | ################################################################################ |
|---|
| 468 | |
|---|
| 469 | sub get_blist |
|---|
| 470 | { |
|---|
| 471 | return runHook_accumulate(\@onGetBuddyList); |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | ################################################################################ |
|---|
| 475 | # Built-in perl styles |
|---|
| 476 | ################################################################################ |
|---|
| 477 | package BarnOwl::Style::Default; |
|---|
| 478 | ################################################################################ |
|---|
| 479 | # Branching point for various formatting functions in this style. |
|---|
| 480 | ################################################################################ |
|---|
| 481 | sub format_message($) |
|---|
| 482 | { |
|---|
| 483 | my $m = shift; |
|---|
| 484 | |
|---|
| 485 | if ( $m->is_zephyr ) { |
|---|
| 486 | return format_zephyr($m); |
|---|
| 487 | } |
|---|
| 488 | elsif ( $m->is_admin ) { |
|---|
| 489 | return "\@bold(OWL ADMIN)\n" . indentBody($m); |
|---|
| 490 | } |
|---|
| 491 | elsif ( $m->is_aim ) { |
|---|
| 492 | return format_aim($m); |
|---|
| 493 | } |
|---|
| 494 | elsif ( lc( $m->type ) eq 'loopback' ) { |
|---|
| 495 | return format_loopback($m); |
|---|
| 496 | } |
|---|
| 497 | else { |
|---|
| 498 | return "Unexpected message type."; |
|---|
| 499 | } |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style"); |
|---|
| 503 | |
|---|
| 504 | ################################################################################ |
|---|
| 505 | sub format_zephyr($) |
|---|
| 506 | { |
|---|
| 507 | my $m = shift; |
|---|
| 508 | |
|---|
| 509 | # Extract time from message |
|---|
| 510 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
|---|
| 511 | |
|---|
| 512 | # Deal with PING messages, assuming owl's rxping variable is true. |
|---|
| 513 | if ( $m->is_ping && $m->recipient ne "" ) { |
|---|
| 514 | return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" ); |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | # Deal with login/logout messages |
|---|
| 518 | elsif ( $m->is_loginout ) { |
|---|
| 519 | return sprintf( |
|---|
| 520 | '@b<%s%s> for @b(%s) at %s %s %s', |
|---|
| 521 | uc( $m->login ), |
|---|
| 522 | |
|---|
| 523 | # This is a hack, owl does not export "pseudo"-ness |
|---|
| 524 | ( $m->zsig ) ? "" : " (PSEUDO)", |
|---|
| 525 | $m->pretty_sender, |
|---|
| 526 | lc( $m->host ), |
|---|
| 527 | $m->login_tty, |
|---|
| 528 | $time |
|---|
| 529 | ); |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | # Deal with outbound zephyrs (personal, we don't see outbound non-personal) |
|---|
| 533 | elsif ( lc( $m->direction ) eq 'out' ) { |
|---|
| 534 | my $user = $m->recipient; |
|---|
| 535 | $user =~ s/\@ATHENA[.]MIT[.]EDU$//; |
|---|
| 536 | |
|---|
| 537 | my $zsig = $m->zsig; |
|---|
| 538 | $zsig =~ s/\n.*$//s; |
|---|
| 539 | |
|---|
| 540 | return sprintf( "Zephyr sent to %s %s (Zsig: %s)\n%s", |
|---|
| 541 | $user, $time, $zsig, indentBody($m) ); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | # Deal with everything else |
|---|
| 545 | else { |
|---|
| 546 | my $zsig = $m->zsig; |
|---|
| 547 | $zsig =~ s/\n.*$//s; |
|---|
| 548 | |
|---|
| 549 | my $msg = sprintf( |
|---|
| 550 | "%s / %s / \@b<%s>%s %s (%s)\n%s", |
|---|
| 551 | $m->class, $m->instance, $m->pretty_sender, |
|---|
| 552 | ( $m->opcode ? " [@{[$m->opcode]}]" : "" ), |
|---|
| 553 | $time, $zsig, indentBody($m) |
|---|
| 554 | ); |
|---|
| 555 | return BarnOwl::Style::boldify($msg) if ( $m->is_private ); |
|---|
| 556 | return $msg; |
|---|
| 557 | } |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | sub format_aim($) |
|---|
| 562 | { |
|---|
| 563 | my $m = shift; |
|---|
| 564 | |
|---|
| 565 | # Extract time from message |
|---|
| 566 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
|---|
| 567 | |
|---|
| 568 | # Deal with login/logout messages |
|---|
| 569 | if ( $m->is_loginout ) { |
|---|
| 570 | return |
|---|
| 571 | sprintf( "\@b(AIM %s) for %s %s", |
|---|
| 572 | uc( $m->login ), |
|---|
| 573 | $m->sender, |
|---|
| 574 | $time ); |
|---|
| 575 | } |
|---|
| 576 | elsif ( lc( $m->direction ) eq 'out' ) { |
|---|
| 577 | return sprintf( "AIM sent to %s %s\n%s", |
|---|
| 578 | $m->recipient, |
|---|
| 579 | $time, |
|---|
| 580 | indentBody($m) ); |
|---|
| 581 | } |
|---|
| 582 | else { |
|---|
| 583 | return sprintf( "\@b(AIM from %s) %s\n%s", |
|---|
| 584 | $m->sender, |
|---|
| 585 | $time, |
|---|
| 586 | BarnOwl::Style::boldify( indentBody($m) ) ); |
|---|
| 587 | } |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | sub format_loopback($) |
|---|
| 592 | { |
|---|
| 593 | my $m = shift; |
|---|
| 594 | |
|---|
| 595 | # Extract time from message |
|---|
| 596 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
|---|
| 597 | |
|---|
| 598 | return sprintf( "loopback from: %s to: %s %s\n%s", |
|---|
| 599 | $m->sender, $m->recipient, $time, indentBody($m) ); |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | sub indentBody($) |
|---|
| 604 | { |
|---|
| 605 | my $m = shift; |
|---|
| 606 | |
|---|
| 607 | my $body = $m->body; |
|---|
| 608 | # replace newline followed by anything with |
|---|
| 609 | # newline plus four spaces and that thing. |
|---|
| 610 | $body =~ s/\n(.)/\n $1/g; |
|---|
| 611 | |
|---|
| 612 | return " ".$body; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | package BarnOwl::Style; |
|---|
| 617 | |
|---|
| 618 | # This takes a zephyr to be displayed and modifies it to be displayed |
|---|
| 619 | # entirely in bold. |
|---|
| 620 | sub boldify($) |
|---|
| 621 | { |
|---|
| 622 | local $_ = shift; |
|---|
| 623 | if ( !(/\)/) ) { |
|---|
| 624 | return '@b(' . $_ . ')'; |
|---|
| 625 | } elsif ( !(/\>/) ) { |
|---|
| 626 | return '@b<' . $_ . '>'; |
|---|
| 627 | } elsif ( !(/\}/) ) { |
|---|
| 628 | return '@b{' . $_ . '}'; |
|---|
| 629 | } elsif ( !(/\]/) ) { |
|---|
| 630 | return '@b[' . $_ . ']'; |
|---|
| 631 | } else { |
|---|
| 632 | my $txt = "\@b($_"; |
|---|
| 633 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
|---|
| 634 | return $txt . ')'; |
|---|
| 635 | } |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | # switch to package main when we're done |
|---|
| 640 | package main; |
|---|
| 641 | # alias the hooks |
|---|
| 642 | { |
|---|
| 643 | no strict 'refs'; |
|---|
| 644 | foreach my $hook qw (onStartSubs |
|---|
| 645 | onReceiveMsg |
|---|
| 646 | onMainLoop |
|---|
| 647 | onGetBuddyList ) { |
|---|
| 648 | *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
|---|
| 649 | *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
|---|
| 650 | } |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | 1; |
|---|