| 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 | # Display fields -- overridden by subclasses when needed |
|---|
| 207 | sub login_type {""} |
|---|
| 208 | sub login_extra {""} |
|---|
| 209 | sub long_sender {""} |
|---|
| 210 | |
|---|
| 211 | # The context in which a non-personal message was sent, e.g. a chat or |
|---|
| 212 | # class |
|---|
| 213 | sub context {""} |
|---|
| 214 | |
|---|
| 215 | # Some indicator of context *within* $self->context. e.g. the zephyr |
|---|
| 216 | # instance |
|---|
| 217 | sub subcontext {""} |
|---|
| 218 | |
|---|
| 219 | ##################################################################### |
|---|
| 220 | ##################################################################### |
|---|
| 221 | |
|---|
| 222 | package BarnOwl::Message::Admin; |
|---|
| 223 | |
|---|
| 224 | use base qw( BarnOwl::Message ); |
|---|
| 225 | |
|---|
| 226 | sub header { return shift->{"header"}; } |
|---|
| 227 | |
|---|
| 228 | ##################################################################### |
|---|
| 229 | ##################################################################### |
|---|
| 230 | |
|---|
| 231 | package BarnOwl::Message::Generic; |
|---|
| 232 | |
|---|
| 233 | use base qw( BarnOwl::Message ); |
|---|
| 234 | |
|---|
| 235 | ##################################################################### |
|---|
| 236 | ##################################################################### |
|---|
| 237 | |
|---|
| 238 | package BarnOwl::Message::AIM; |
|---|
| 239 | |
|---|
| 240 | use base qw( BarnOwl::Message ); |
|---|
| 241 | |
|---|
| 242 | # all non-loginout AIM messages are personal for now... |
|---|
| 243 | sub is_personal { |
|---|
| 244 | return !(shift->is_loginout); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | ##################################################################### |
|---|
| 248 | ##################################################################### |
|---|
| 249 | |
|---|
| 250 | package BarnOwl::Message::Zephyr; |
|---|
| 251 | |
|---|
| 252 | use base qw( BarnOwl::Message ); |
|---|
| 253 | |
|---|
| 254 | sub login_type { |
|---|
| 255 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | sub login_extra { |
|---|
| 259 | my $m = shift; |
|---|
| 260 | return undef if (!$m->is_loginout); |
|---|
| 261 | my $s = lc($m->host); |
|---|
| 262 | $s .= " " . $m->login_tty if defined $m->login_tty; |
|---|
| 263 | return $s; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | sub long_sender { |
|---|
| 267 | my $m = shift; |
|---|
| 268 | return $m->zsig; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | sub context { |
|---|
| 272 | return shift->class; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | sub subcontext { |
|---|
| 276 | return shift->instance; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | sub login_tty { |
|---|
| 280 | my ($m) = @_; |
|---|
| 281 | return undef if (!$m->is_loginout); |
|---|
| 282 | return $m->fields->[2]; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | sub login_host { |
|---|
| 286 | my ($m) = @_; |
|---|
| 287 | return undef if (!$m->is_loginout); |
|---|
| 288 | return $m->fields->[0]; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | sub zwriteline { return shift->{"zwriteline"}; } |
|---|
| 292 | |
|---|
| 293 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
|---|
| 294 | |
|---|
| 295 | sub is_personal { |
|---|
| 296 | my ($m) = @_; |
|---|
| 297 | return ((lc($m->class) eq "message") |
|---|
| 298 | && (lc($m->instance) eq "personal") |
|---|
| 299 | && $m->is_private); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | sub is_mail { |
|---|
| 303 | my ($m) = @_; |
|---|
| 304 | return ((lc($m->class) eq "mail") && $m->is_private); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | sub pretty_sender { |
|---|
| 308 | my ($m) = @_; |
|---|
| 309 | my $sender = $m->sender; |
|---|
| 310 | my $realm = BarnOwl::zephyr_getrealm(); |
|---|
| 311 | $sender =~ s/\@$realm$//; |
|---|
| 312 | return $sender; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | sub pretty_recipient { |
|---|
| 316 | my ($m) = @_; |
|---|
| 317 | my $recip = $m->recipient; |
|---|
| 318 | my $realm = BarnOwl::zephyr_getrealm(); |
|---|
| 319 | $recip =~ s/\@$realm$//; |
|---|
| 320 | return $recip; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | # These are arguably zephyr-specific |
|---|
| 324 | sub class { return shift->{"class"}; } |
|---|
| 325 | sub instance { return shift->{"instance"}; } |
|---|
| 326 | sub realm { return shift->{"realm"}; } |
|---|
| 327 | sub opcode { return shift->{"opcode"}; } |
|---|
| 328 | sub host { return shift->{"hostname"}; } |
|---|
| 329 | sub hostname { return shift->{"hostname"}; } |
|---|
| 330 | sub header { return shift->{"header"}; } |
|---|
| 331 | sub auth { return shift->{"auth"}; } |
|---|
| 332 | sub fields { return shift->{"fields"}; } |
|---|
| 333 | sub zsig { return shift->{"zsig"}; } |
|---|
| 334 | |
|---|
| 335 | ##################################################################### |
|---|
| 336 | ##################################################################### |
|---|
| 337 | ################################################################################ |
|---|
| 338 | package BarnOwl; |
|---|
| 339 | |
|---|
| 340 | ################################################################################ |
|---|
| 341 | # Mainloop hook |
|---|
| 342 | ################################################################################ |
|---|
| 343 | |
|---|
| 344 | our $shutdown; |
|---|
| 345 | $shutdown = 0; |
|---|
| 346 | our $reload; |
|---|
| 347 | $reload = 0; |
|---|
| 348 | |
|---|
| 349 | #Run this on start and reload. Adds modules |
|---|
| 350 | sub onStart |
|---|
| 351 | { |
|---|
| 352 | _load_owlconf(); |
|---|
| 353 | reload_init(); |
|---|
| 354 | loadModules(); |
|---|
| 355 | } |
|---|
| 356 | ################################################################################ |
|---|
| 357 | # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl |
|---|
| 358 | ################################################################################ |
|---|
| 359 | sub reload_hook (@) |
|---|
| 360 | { |
|---|
| 361 | BarnOwl::Hooks::startup(); |
|---|
| 362 | return 1; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | sub reload |
|---|
| 366 | { |
|---|
| 367 | # Use $reload to tell modules that we're performing a reload. |
|---|
| 368 | { |
|---|
| 369 | local $reload = 1; |
|---|
| 370 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | @BarnOwl::Hooks::onMainLoop = (); |
|---|
| 374 | @BarnOwl::Hooks::onStartSubs = (); |
|---|
| 375 | |
|---|
| 376 | # Do reload |
|---|
| 377 | package main; |
|---|
| 378 | if (-r $BarnOwl::configfile) { |
|---|
| 379 | undef $@; |
|---|
| 380 | do $BarnOwl::configfile; |
|---|
| 381 | BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@; |
|---|
| 382 | } |
|---|
| 383 | BarnOwl::reload_hook(@_); |
|---|
| 384 | package BarnOwl; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | sub reload_init () |
|---|
| 388 | { |
|---|
| 389 | BarnOwl::command('alias reload perl BarnOwl::reload()'); |
|---|
| 390 | BarnOwl::command('bindkey global "C-x C-r" command reload'); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | ################################################################################ |
|---|
| 394 | # Loads modules from ~/.owl/modules and owl's data directory |
|---|
| 395 | ################################################################################ |
|---|
| 396 | |
|---|
| 397 | sub loadModules () { |
|---|
| 398 | my @modules; |
|---|
| 399 | my $rv; |
|---|
| 400 | foreach my $dir ( BarnOwl::get_data_dir() . "/modules", |
|---|
| 401 | $ENV{HOME} . "/.owl/modules" ) |
|---|
| 402 | { |
|---|
| 403 | opendir( MODULES, $dir ); |
|---|
| 404 | |
|---|
| 405 | # source ./modules/*.pl |
|---|
| 406 | @modules = sort grep( /\.pl$/, readdir(MODULES) ); |
|---|
| 407 | |
|---|
| 408 | foreach my $mod (@modules) { |
|---|
| 409 | unless ($rv = do "$dir/$mod") { |
|---|
| 410 | BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@; |
|---|
| 411 | BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv; |
|---|
| 412 | } |
|---|
| 413 | } |
|---|
| 414 | closedir(MODULES); |
|---|
| 415 | } |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | sub _load_owlconf { |
|---|
| 419 | # Only do this the first time |
|---|
| 420 | return if $BarnOwl::reload; |
|---|
| 421 | # load the config file |
|---|
| 422 | if ( -r $BarnOwl::configfile ) { |
|---|
| 423 | undef $@; |
|---|
| 424 | do $BarnOwl::configfile; |
|---|
| 425 | die $@ if $@; |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | package BarnOwl::Hooks; |
|---|
| 430 | |
|---|
| 431 | # Arrays of subrefs to be called at specific times. |
|---|
| 432 | our @onStartSubs = (); |
|---|
| 433 | our @onReceiveMsg = (); |
|---|
| 434 | our @onMainLoop = (); |
|---|
| 435 | our @onGetBuddyList = (); |
|---|
| 436 | |
|---|
| 437 | # Functions to call hook lists |
|---|
| 438 | sub runHook($@) |
|---|
| 439 | { |
|---|
| 440 | my $hook = shift; |
|---|
| 441 | my @args = @_; |
|---|
| 442 | $_->(@args) for (@$hook); |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | sub runHook_accumulate($@) |
|---|
| 446 | { |
|---|
| 447 | my $hook = shift; |
|---|
| 448 | my @args = @_; |
|---|
| 449 | return join("\n", map {$_->(@args)} @$hook); |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | ################################################################################ |
|---|
| 453 | # Startup and Shutdown code |
|---|
| 454 | ################################################################################ |
|---|
| 455 | sub startup |
|---|
| 456 | { |
|---|
| 457 | # Modern versions of owl provides a great place to have startup stuff. |
|---|
| 458 | # Put things in ~/.owl/startup |
|---|
| 459 | |
|---|
| 460 | #So that the user's .owlconf can have startsubs, we don't clear |
|---|
| 461 | #onStartSubs; reload does however |
|---|
| 462 | @onReceiveMsg = (); |
|---|
| 463 | @onMainLoop = (); |
|---|
| 464 | @onGetBuddyList = (); |
|---|
| 465 | |
|---|
| 466 | BarnOwl::onStart(); |
|---|
| 467 | |
|---|
| 468 | runHook(\@onStartSubs); |
|---|
| 469 | |
|---|
| 470 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | sub shutdown |
|---|
| 474 | { |
|---|
| 475 | # Modern versions of owl provides a great place to have shutdown stuff. |
|---|
| 476 | # Put things in ~/.owl/shutdown |
|---|
| 477 | |
|---|
| 478 | # use $shutdown to tell modules that that's what we're doing. |
|---|
| 479 | $BarnOwl::shutdown = 1; |
|---|
| 480 | BarnOwl::mainloop_hook(); |
|---|
| 481 | |
|---|
| 482 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | sub mainloop_hook |
|---|
| 486 | { |
|---|
| 487 | runHook(\@onMainLoop); |
|---|
| 488 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | ################################################################################ |
|---|
| 492 | # Hooks into receive_msg() |
|---|
| 493 | ################################################################################ |
|---|
| 494 | |
|---|
| 495 | sub receive_msg |
|---|
| 496 | { |
|---|
| 497 | my $m = shift; |
|---|
| 498 | runHook(\@onReceiveMsg, $m); |
|---|
| 499 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | ################################################################################ |
|---|
| 503 | # Hooks into get_blist() |
|---|
| 504 | ################################################################################ |
|---|
| 505 | |
|---|
| 506 | sub get_blist |
|---|
| 507 | { |
|---|
| 508 | return runHook_accumulate(\@onGetBuddyList); |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | ################################################################################ |
|---|
| 512 | # Built-in perl styles |
|---|
| 513 | ################################################################################ |
|---|
| 514 | package BarnOwl::Style::Default; |
|---|
| 515 | ################################################################################ |
|---|
| 516 | # Branching point for various formatting functions in this style. |
|---|
| 517 | ################################################################################ |
|---|
| 518 | sub format_message($) |
|---|
| 519 | { |
|---|
| 520 | my $m = shift; |
|---|
| 521 | |
|---|
| 522 | if ( $m->is_loginout) { |
|---|
| 523 | return format_login($m); |
|---|
| 524 | } elsif($m->is_ping) { |
|---|
| 525 | return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" ); |
|---|
| 526 | } elsif($m->is_admin) { |
|---|
| 527 | return "\@bold(OWL ADMIN)\n" . indentBody($m); |
|---|
| 528 | } else { |
|---|
| 529 | return format_chat($m); |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style"); |
|---|
| 534 | |
|---|
| 535 | ################################################################################ |
|---|
| 536 | |
|---|
| 537 | sub time_hhmm { |
|---|
| 538 | my $m = shift; |
|---|
| 539 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
|---|
| 540 | return $time; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | sub format_login($) { |
|---|
| 544 | my $m = shift; |
|---|
| 545 | return sprintf( |
|---|
| 546 | '@b<%s%s> for @b(%s) (%s) %s', |
|---|
| 547 | uc( $m->login ), |
|---|
| 548 | $m->login_type, |
|---|
| 549 | $m->pretty_sender, |
|---|
| 550 | $m->login_extra, |
|---|
| 551 | time_hhmm($m) |
|---|
| 552 | ); |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | sub format_chat($) { |
|---|
| 556 | my $m = shift; |
|---|
| 557 | my $header; |
|---|
| 558 | if ( $m->is_personal ) { |
|---|
| 559 | if ( $m->direction eq "out" ) { |
|---|
| 560 | $header = ucfirst $m->type . " sent to " . $m->pretty_recipient; |
|---|
| 561 | } else { |
|---|
| 562 | $header = ucfirst $m->type . " from " . $m->pretty_sender; |
|---|
| 563 | } |
|---|
| 564 | } else { |
|---|
| 565 | $header = $m->context; |
|---|
| 566 | if($m->subcontext) { |
|---|
| 567 | $header .= ' / ' . $m->subcontext; |
|---|
| 568 | } |
|---|
| 569 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | $header .= " " . time_hhmm($m); |
|---|
| 573 | my $sender = $m->long_sender; |
|---|
| 574 | $sender =~ s/\n.*$//s; |
|---|
| 575 | $header .= "\t(" . $sender . ")"; |
|---|
| 576 | my $message = $header . "\n". indentBody($m); |
|---|
| 577 | if($m->is_private && $m->direction eq "in") { |
|---|
| 578 | $message = BarnOwl::Style::boldify($message); |
|---|
| 579 | } |
|---|
| 580 | return $message; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | sub indentBody($) |
|---|
| 584 | { |
|---|
| 585 | my $m = shift; |
|---|
| 586 | |
|---|
| 587 | my $body = $m->body; |
|---|
| 588 | # replace newline followed by anything with |
|---|
| 589 | # newline plus four spaces and that thing. |
|---|
| 590 | $body =~ s/\n(.)/\n $1/g; |
|---|
| 591 | |
|---|
| 592 | return " ".$body; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | package BarnOwl::Style; |
|---|
| 597 | |
|---|
| 598 | # This takes a zephyr to be displayed and modifies it to be displayed |
|---|
| 599 | # entirely in bold. |
|---|
| 600 | sub boldify($) |
|---|
| 601 | { |
|---|
| 602 | local $_ = shift; |
|---|
| 603 | if ( !(/\)/) ) { |
|---|
| 604 | return '@b(' . $_ . ')'; |
|---|
| 605 | } elsif ( !(/\>/) ) { |
|---|
| 606 | return '@b<' . $_ . '>'; |
|---|
| 607 | } elsif ( !(/\}/) ) { |
|---|
| 608 | return '@b{' . $_ . '}'; |
|---|
| 609 | } elsif ( !(/\]/) ) { |
|---|
| 610 | return '@b[' . $_ . ']'; |
|---|
| 611 | } else { |
|---|
| 612 | my $txt = "\@b($_"; |
|---|
| 613 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
|---|
| 614 | return $txt . ')'; |
|---|
| 615 | } |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | |
|---|
| 619 | # switch to package main when we're done |
|---|
| 620 | package main; |
|---|
| 621 | # alias the hooks |
|---|
| 622 | { |
|---|
| 623 | no strict 'refs'; |
|---|
| 624 | foreach my $hook qw (onStartSubs |
|---|
| 625 | onReceiveMsg |
|---|
| 626 | onMainLoop |
|---|
| 627 | onGetBuddyList ) { |
|---|
| 628 | *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
|---|
| 629 | *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
|---|
| 630 | } |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | 1; |
|---|