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