[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 |
---|
| 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 | |
---|
[f1e629d] | 12 | |
---|
[c681337] | 13 | use strict; |
---|
| 14 | use warnings; |
---|
| 15 | |
---|
[8203afd] | 16 | package BarnOwl; |
---|
[f1e629d] | 17 | |
---|
[8862725] | 18 | |
---|
| 19 | BEGIN { |
---|
[f1e629d] | 20 | # bootstrap in C bindings and glue |
---|
[8203afd] | 21 | *owl:: = \*BarnOwl::; |
---|
| 22 | bootstrap BarnOwl 1.2; |
---|
[8862725] | 23 | }; |
---|
| 24 | |
---|
[3354cea5] | 25 | use lib(get_data_dir()."/lib"); |
---|
[8862725] | 26 | use lib($::ENV{'HOME'}."/.owl/lib"); |
---|
| 27 | |
---|
[00f9a7d] | 28 | our $configfile; |
---|
| 29 | |
---|
| 30 | $configfile ||= $::ENV{'HOME'}."/.owlconf"; |
---|
[d03091c] | 31 | |
---|
[f1e629d] | 32 | # populate global variable space for legacy owlconf files |
---|
| 33 | sub _format_msg_legacy_wrap { |
---|
| 34 | my ($m) = @_; |
---|
| 35 | $m->legacy_populate_global(); |
---|
[8203afd] | 36 | return &BarnOwl::format_msg($m); |
---|
[f1e629d] | 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(); |
---|
[8203afd] | 43 | return &BarnOwl::Hooks::receive_msg($m); |
---|
[f1e629d] | 44 | } |
---|
| 45 | |
---|
[8203afd] | 46 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
[f1e629d] | 47 | sub AUTOLOAD { |
---|
[c681337] | 48 | our $AUTOLOAD; |
---|
[f1e629d] | 49 | my $called = $AUTOLOAD; |
---|
| 50 | $called =~ s/.*:://; |
---|
[e7ac2b6] | 51 | $called =~ s/_/-/g; |
---|
[8203afd] | 52 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
[f1e629d] | 53 | } |
---|
| 54 | |
---|
[6922edd] | 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 | |
---|
[c681337] | 77 | no warnings 'uninitialized'; |
---|
[8203afd] | 78 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
[6922edd] | 79 | } |
---|
| 80 | |
---|
[f1e629d] | 81 | ##################################################################### |
---|
| 82 | ##################################################################### |
---|
| 83 | |
---|
[8203afd] | 84 | package BarnOwl::Message; |
---|
[f1e629d] | 85 | |
---|
[dd16bdd] | 86 | sub new { |
---|
| 87 | my $class = shift; |
---|
| 88 | my %args = (@_); |
---|
| 89 | if($class eq __PACKAGE__ && $args{type}) { |
---|
[8203afd] | 90 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
[dd16bdd] | 91 | } |
---|
| 92 | return bless {%args}, $class; |
---|
| 93 | } |
---|
| 94 | |
---|
[f1e629d] | 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"}; } |
---|
[216c734] | 103 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 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"); } |
---|
[421c8ef7] | 115 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
[467aa16] | 116 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
[dd16bdd] | 117 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 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"); } |
---|
[f1e629d] | 122 | |
---|
| 123 | # These are overridden by appropriate message types |
---|
| 124 | sub is_ping { return 0; } |
---|
| 125 | sub is_mail { return 0; } |
---|
[3c9012b] | 126 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 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; } |
---|
[87c6ef1] | 138 | sub login_host { return undef; } |
---|
| 139 | sub login_tty { return undef; } |
---|
[f1e629d] | 140 | |
---|
[ae47efb] | 141 | sub pretty_sender { return shift->sender; } |
---|
| 142 | sub pretty_recipient { return shift->recipient; } |
---|
[f1e629d] | 143 | |
---|
| 144 | sub delete { |
---|
| 145 | my ($m) = @_; |
---|
[8203afd] | 146 | &BarnOwl::command("delete --id ".$m->id); |
---|
[f1e629d] | 147 | } |
---|
| 148 | |
---|
| 149 | sub undelete { |
---|
| 150 | my ($m) = @_; |
---|
[8203afd] | 151 | &BarnOwl::command("undelete --id ".$m->id); |
---|
[f1e629d] | 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) = @_; |
---|
[8203afd] | 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 ; |
---|
[f1e629d] | 193 | if ($m->fields) { |
---|
[8203afd] | 194 | @BarnOwl::fields = @{$m->fields}; |
---|
[f1e629d] | 195 | @main::fields = @{$m->fields}; |
---|
| 196 | } else { |
---|
[8203afd] | 197 | @BarnOwl::fields = undef; |
---|
[f1e629d] | 198 | @main::fields = undef; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[25729b2] | 202 | sub smartfilter { |
---|
| 203 | die("smartfilter not supported for this message"); |
---|
| 204 | } |
---|
| 205 | |
---|
[f1e629d] | 206 | ##################################################################### |
---|
| 207 | ##################################################################### |
---|
| 208 | |
---|
[8203afd] | 209 | package BarnOwl::Message::Admin; |
---|
[f1e629d] | 210 | |
---|
[8203afd] | 211 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 212 | |
---|
| 213 | sub header { return shift->{"header"}; } |
---|
| 214 | |
---|
| 215 | ##################################################################### |
---|
| 216 | ##################################################################### |
---|
| 217 | |
---|
[8203afd] | 218 | package BarnOwl::Message::Generic; |
---|
[f1e629d] | 219 | |
---|
[8203afd] | 220 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 221 | |
---|
| 222 | ##################################################################### |
---|
| 223 | ##################################################################### |
---|
| 224 | |
---|
[8203afd] | 225 | package BarnOwl::Message::AIM; |
---|
[f1e629d] | 226 | |
---|
[8203afd] | 227 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 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 | |
---|
[8203afd] | 237 | package BarnOwl::Message::Zephyr; |
---|
[f1e629d] | 238 | |
---|
[8203afd] | 239 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 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; |
---|
[8203afd] | 272 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
[f1e629d] | 273 | $sender =~ s/\@$realm$//; |
---|
| 274 | return $sender; |
---|
| 275 | } |
---|
| 276 | |
---|
[ae47efb] | 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 | |
---|
[f1e629d] | 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 | ##################################################################### |
---|
[7e470da] | 299 | ################################################################################ |
---|
[b6c067a] | 300 | package BarnOwl; |
---|
[7e470da] | 301 | |
---|
| 302 | ################################################################################ |
---|
[f265f94] | 303 | # Mainloop hook |
---|
[7e470da] | 304 | ################################################################################ |
---|
| 305 | |
---|
[f265f94] | 306 | our $shutdown; |
---|
[7e470da] | 307 | $shutdown = 0; |
---|
[f265f94] | 308 | our $reload; |
---|
[7e470da] | 309 | $reload = 0; |
---|
| 310 | |
---|
[8203afd] | 311 | #Run this on start and reload. Adds modules |
---|
[7e470da] | 312 | sub onStart |
---|
| 313 | { |
---|
| 314 | reload_init(); |
---|
| 315 | loadModules(); |
---|
| 316 | } |
---|
| 317 | ################################################################################ |
---|
| 318 | # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl |
---|
| 319 | ################################################################################ |
---|
[f265f94] | 320 | sub reload_hook (@) |
---|
[7e470da] | 321 | { |
---|
[8203afd] | 322 | BarnOwl::Hooks::startup(); |
---|
[7e470da] | 323 | return 1; |
---|
| 324 | } |
---|
| 325 | |
---|
[f265f94] | 326 | sub reload |
---|
[7e470da] | 327 | { |
---|
[f265f94] | 328 | # Use $reload to tell modules that we're performing a reload. |
---|
[8203afd] | 329 | { |
---|
| 330 | local $reload = 1; |
---|
[16138b0] | 331 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
[8203afd] | 332 | } |
---|
[b6c067a] | 333 | |
---|
[8203afd] | 334 | @BarnOwl::Hooks::onMainLoop = (); |
---|
| 335 | @BarnOwl::Hooks::onStartSubs = (); |
---|
| 336 | |
---|
| 337 | # Do reload |
---|
| 338 | package main; |
---|
[1a9c761] | 339 | if (-r $BarnOwl::configfile) { |
---|
| 340 | undef $@; |
---|
| 341 | do $BarnOwl::configfile; |
---|
[b6c067a] | 342 | BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@; |
---|
[8203afd] | 343 | } |
---|
[1a9c761] | 344 | BarnOwl::reload_hook(@_); |
---|
[b6c067a] | 345 | package BarnOwl; |
---|
[7e470da] | 346 | } |
---|
| 347 | |
---|
| 348 | sub reload_init () |
---|
| 349 | { |
---|
[8203afd] | 350 | BarnOwl::command('alias reload perl BarnOwl::reload()'); |
---|
| 351 | BarnOwl::command('bindkey global "C-x C-r" command reload'); |
---|
[7e470da] | 352 | } |
---|
| 353 | |
---|
| 354 | ################################################################################ |
---|
| 355 | # Loads modules from ~/.owl/modules and owl's data directory |
---|
| 356 | ################################################################################ |
---|
| 357 | |
---|
[f2f5815] | 358 | sub loadModules () { |
---|
| 359 | my @modules; |
---|
[23be736] | 360 | my $rv; |
---|
[8203afd] | 361 | foreach my $dir ( BarnOwl::get_data_dir() . "/modules", |
---|
[23be736] | 362 | $ENV{HOME} . "/.owl/modules" ) |
---|
[f2f5815] | 363 | { |
---|
| 364 | opendir( MODULES, $dir ); |
---|
| 365 | |
---|
| 366 | # source ./modules/*.pl |
---|
[4ee1cf4] | 367 | @modules = sort grep( /\.pl$/, readdir(MODULES) ); |
---|
[f2f5815] | 368 | |
---|
| 369 | foreach my $mod (@modules) { |
---|
[23be736] | 370 | unless ($rv = do "$dir/$mod") { |
---|
[8203afd] | 371 | BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@; |
---|
| 372 | BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv; |
---|
[23be736] | 373 | } |
---|
[f2f5815] | 374 | } |
---|
| 375 | closedir(MODULES); |
---|
| 376 | } |
---|
[7e470da] | 377 | } |
---|
| 378 | |
---|
[b6c067a] | 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 | |
---|
[8203afd] | 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 = (); |
---|
[7e470da] | 399 | |
---|
[8203afd] | 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); |
---|
[16138b0] | 451 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
[8203afd] | 452 | } |
---|
[f2f5815] | 453 | |
---|
[7e470da] | 454 | ################################################################################ |
---|
| 455 | # Hooks into receive_msg() |
---|
| 456 | ################################################################################ |
---|
| 457 | |
---|
[f2f5815] | 458 | sub receive_msg |
---|
[7e470da] | 459 | { |
---|
| 460 | my $m = shift; |
---|
[f2f5815] | 461 | runHook(\@onReceiveMsg, $m); |
---|
[8203afd] | 462 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
[7e470da] | 463 | } |
---|
| 464 | |
---|
| 465 | ################################################################################ |
---|
| 466 | # Hooks into get_blist() |
---|
| 467 | ################################################################################ |
---|
| 468 | |
---|
[f2f5815] | 469 | sub get_blist |
---|
[7e470da] | 470 | { |
---|
[8203afd] | 471 | return runHook_accumulate(\@onGetBuddyList); |
---|
[7e470da] | 472 | } |
---|
[dd16bdd] | 473 | |
---|
[b6c067a] | 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 | |
---|
[f1e629d] | 639 | # switch to package main when we're done |
---|
| 640 | package main; |
---|
[7e470da] | 641 | # alias the hooks |
---|
[c681337] | 642 | { |
---|
| 643 | no strict 'refs'; |
---|
| 644 | foreach my $hook qw (onStartSubs |
---|
| 645 | onReceiveMsg |
---|
| 646 | onMainLoop |
---|
| 647 | onGetBuddyList ) { |
---|
[8203afd] | 648 | *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
---|
| 649 | *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
---|
[c681337] | 650 | } |
---|
[7e470da] | 651 | } |
---|
[f1e629d] | 652 | |
---|
| 653 | 1; |
---|