[f1e629d] | 1 | # $Id$ |
---|
| 2 | # |
---|
| 3 | # This is all linked into the binary and evaluated when perl starts up... |
---|
| 4 | # |
---|
| 5 | ##################################################################### |
---|
| 6 | ##################################################################### |
---|
| 7 | |
---|
[c681337] | 8 | use strict; |
---|
| 9 | use warnings; |
---|
| 10 | |
---|
[8203afd] | 11 | package BarnOwl; |
---|
[f1e629d] | 12 | |
---|
[8862725] | 13 | |
---|
| 14 | BEGIN { |
---|
[f1e629d] | 15 | # bootstrap in C bindings and glue |
---|
[8203afd] | 16 | *owl:: = \*BarnOwl::; |
---|
| 17 | bootstrap BarnOwl 1.2; |
---|
[8862725] | 18 | }; |
---|
| 19 | |
---|
[3354cea5] | 20 | use lib(get_data_dir()."/lib"); |
---|
[8862725] | 21 | use lib($::ENV{'HOME'}."/.owl/lib"); |
---|
| 22 | |
---|
[00f9a7d] | 23 | our $configfile; |
---|
| 24 | |
---|
| 25 | $configfile ||= $::ENV{'HOME'}."/.owlconf"; |
---|
[d03091c] | 26 | |
---|
[f1e629d] | 27 | # populate global variable space for legacy owlconf files |
---|
| 28 | sub _format_msg_legacy_wrap { |
---|
| 29 | my ($m) = @_; |
---|
| 30 | $m->legacy_populate_global(); |
---|
[8203afd] | 31 | return &BarnOwl::format_msg($m); |
---|
[f1e629d] | 32 | } |
---|
| 33 | |
---|
| 34 | # populate global variable space for legacy owlconf files |
---|
| 35 | sub _receive_msg_legacy_wrap { |
---|
| 36 | my ($m) = @_; |
---|
| 37 | $m->legacy_populate_global(); |
---|
[8203afd] | 38 | return &BarnOwl::Hooks::receive_msg($m); |
---|
[f1e629d] | 39 | } |
---|
| 40 | |
---|
[8203afd] | 41 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
[f1e629d] | 42 | sub AUTOLOAD { |
---|
[c681337] | 43 | our $AUTOLOAD; |
---|
[f1e629d] | 44 | my $called = $AUTOLOAD; |
---|
| 45 | $called =~ s/.*:://; |
---|
[e7ac2b6] | 46 | $called =~ s/_/-/g; |
---|
[8203afd] | 47 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
[f1e629d] | 48 | } |
---|
| 49 | |
---|
[6922edd] | 50 | =head2 new_command NAME FUNC [{ARGS}] |
---|
| 51 | |
---|
| 52 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
| 53 | be called with the arguments passed to the command, with NAME as the |
---|
| 54 | first argument. |
---|
| 55 | |
---|
| 56 | ARGS should be a hashref containing any or all of C<summary>, |
---|
| 57 | C<usage>, or C<description> keys. |
---|
| 58 | |
---|
| 59 | =cut |
---|
| 60 | |
---|
| 61 | sub new_command { |
---|
| 62 | my $name = shift; |
---|
| 63 | my $func = shift; |
---|
| 64 | my $args = shift || {}; |
---|
| 65 | my %args = ( |
---|
| 66 | summary => undef, |
---|
| 67 | usage => undef, |
---|
| 68 | description => undef, |
---|
| 69 | %{$args} |
---|
| 70 | ); |
---|
| 71 | |
---|
[c681337] | 72 | no warnings 'uninitialized'; |
---|
[8203afd] | 73 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
[6922edd] | 74 | } |
---|
| 75 | |
---|
[f1e629d] | 76 | ##################################################################### |
---|
| 77 | ##################################################################### |
---|
| 78 | |
---|
[8203afd] | 79 | package BarnOwl::Message; |
---|
[f1e629d] | 80 | |
---|
[dd16bdd] | 81 | sub new { |
---|
| 82 | my $class = shift; |
---|
| 83 | my %args = (@_); |
---|
| 84 | if($class eq __PACKAGE__ && $args{type}) { |
---|
[8203afd] | 85 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
[dd16bdd] | 86 | } |
---|
| 87 | return bless {%args}, $class; |
---|
| 88 | } |
---|
| 89 | |
---|
[f1e629d] | 90 | sub type { return shift->{"type"}; } |
---|
| 91 | sub direction { return shift->{"direction"}; } |
---|
| 92 | sub time { return shift->{"time"}; } |
---|
| 93 | sub id { return shift->{"id"}; } |
---|
| 94 | sub body { return shift->{"body"}; } |
---|
| 95 | sub sender { return shift->{"sender"}; } |
---|
| 96 | sub recipient { return shift->{"recipient"}; } |
---|
| 97 | sub login { return shift->{"login"}; } |
---|
[216c734] | 98 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 99 | |
---|
| 100 | sub is_login { return shift->login eq "login"; } |
---|
| 101 | sub is_logout { return shift->login eq "logout"; } |
---|
| 102 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
| 103 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
| 104 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
| 105 | |
---|
| 106 | sub is_deleted { return shift->{"deleted"}; } |
---|
| 107 | |
---|
| 108 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
| 109 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
[421c8ef7] | 110 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
| 111 | sub is_aim { return (shift->{"type"} eq "aim"); } |
---|
[dd16bdd] | 112 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 113 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
| 114 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
| 115 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
| 116 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
[f1e629d] | 117 | |
---|
| 118 | # These are overridden by appropriate message types |
---|
| 119 | sub is_ping { return 0; } |
---|
| 120 | sub is_mail { return 0; } |
---|
[3c9012b] | 121 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 122 | sub class { return undef; } |
---|
| 123 | sub instance { return undef; } |
---|
| 124 | sub realm { return undef; } |
---|
| 125 | sub opcode { return undef; } |
---|
| 126 | sub header { return undef; } |
---|
| 127 | sub host { return undef; } |
---|
| 128 | sub hostname { return undef; } |
---|
| 129 | sub auth { return undef; } |
---|
| 130 | sub fields { return undef; } |
---|
| 131 | sub zsig { return undef; } |
---|
| 132 | sub zwriteline { return undef; } |
---|
[87c6ef1] | 133 | sub login_host { return undef; } |
---|
| 134 | sub login_tty { return undef; } |
---|
[f1e629d] | 135 | |
---|
| 136 | sub pretty_sender { return shift->sender; } |
---|
| 137 | |
---|
| 138 | sub delete { |
---|
| 139 | my ($m) = @_; |
---|
[8203afd] | 140 | &BarnOwl::command("delete --id ".$m->id); |
---|
[f1e629d] | 141 | } |
---|
| 142 | |
---|
| 143 | sub undelete { |
---|
| 144 | my ($m) = @_; |
---|
[8203afd] | 145 | &BarnOwl::command("undelete --id ".$m->id); |
---|
[f1e629d] | 146 | } |
---|
| 147 | |
---|
| 148 | # Serializes the message into something similar to the zwgc->vt format |
---|
| 149 | sub serialize { |
---|
| 150 | my ($this) = @_; |
---|
| 151 | my $s; |
---|
| 152 | for my $f (keys %$this) { |
---|
| 153 | my $val = $this->{$f}; |
---|
| 154 | if (ref($val) eq "ARRAY") { |
---|
| 155 | for my $i (0..@$val-1) { |
---|
| 156 | my $aval; |
---|
| 157 | $aval = $val->[$i]; |
---|
| 158 | $aval =~ s/\n/\n$f.$i: /g; |
---|
| 159 | $s .= "$f.$i: $aval\n"; |
---|
| 160 | } |
---|
| 161 | } else { |
---|
| 162 | $val =~ s/\n/\n$f: /g; |
---|
| 163 | $s .= "$f: $val\n"; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | return $s; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | # Populate the annoying legacy global variables |
---|
| 170 | sub legacy_populate_global { |
---|
| 171 | my ($m) = @_; |
---|
[8203afd] | 172 | $BarnOwl::direction = $m->direction ; |
---|
| 173 | $BarnOwl::type = $m->type ; |
---|
| 174 | $BarnOwl::id = $m->id ; |
---|
| 175 | $BarnOwl::class = $m->class ; |
---|
| 176 | $BarnOwl::instance = $m->instance ; |
---|
| 177 | $BarnOwl::recipient = $m->recipient ; |
---|
| 178 | $BarnOwl::sender = $m->sender ; |
---|
| 179 | $BarnOwl::realm = $m->realm ; |
---|
| 180 | $BarnOwl::opcode = $m->opcode ; |
---|
| 181 | $BarnOwl::zsig = $m->zsig ; |
---|
| 182 | $BarnOwl::msg = $m->body ; |
---|
| 183 | $BarnOwl::time = $m->time ; |
---|
| 184 | $BarnOwl::host = $m->host ; |
---|
| 185 | $BarnOwl::login = $m->login ; |
---|
| 186 | $BarnOwl::auth = $m->auth ; |
---|
[f1e629d] | 187 | if ($m->fields) { |
---|
[8203afd] | 188 | @BarnOwl::fields = @{$m->fields}; |
---|
[f1e629d] | 189 | @main::fields = @{$m->fields}; |
---|
| 190 | } else { |
---|
[8203afd] | 191 | @BarnOwl::fields = undef; |
---|
[f1e629d] | 192 | @main::fields = undef; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
[25729b2] | 196 | sub smartfilter { |
---|
| 197 | die("smartfilter not supported for this message"); |
---|
| 198 | } |
---|
| 199 | |
---|
[f1e629d] | 200 | ##################################################################### |
---|
| 201 | ##################################################################### |
---|
| 202 | |
---|
[8203afd] | 203 | package BarnOwl::Message::Admin; |
---|
[f1e629d] | 204 | |
---|
[8203afd] | 205 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 206 | |
---|
| 207 | sub header { return shift->{"header"}; } |
---|
| 208 | |
---|
| 209 | ##################################################################### |
---|
| 210 | ##################################################################### |
---|
| 211 | |
---|
[8203afd] | 212 | package BarnOwl::Message::Generic; |
---|
[f1e629d] | 213 | |
---|
[8203afd] | 214 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 215 | |
---|
| 216 | ##################################################################### |
---|
| 217 | ##################################################################### |
---|
| 218 | |
---|
[8203afd] | 219 | package BarnOwl::Message::AIM; |
---|
[f1e629d] | 220 | |
---|
[8203afd] | 221 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 222 | |
---|
| 223 | # all non-loginout AIM messages are personal for now... |
---|
| 224 | sub is_personal { |
---|
| 225 | return !(shift->is_loginout); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | ##################################################################### |
---|
| 229 | ##################################################################### |
---|
| 230 | |
---|
[8203afd] | 231 | package BarnOwl::Message::Zephyr; |
---|
[f1e629d] | 232 | |
---|
[8203afd] | 233 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 234 | |
---|
| 235 | sub login_tty { |
---|
| 236 | my ($m) = @_; |
---|
| 237 | return undef if (!$m->is_loginout); |
---|
| 238 | return $m->fields->[2]; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | sub login_host { |
---|
| 242 | my ($m) = @_; |
---|
| 243 | return undef if (!$m->is_loginout); |
---|
| 244 | return $m->fields->[0]; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
| 248 | |
---|
| 249 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
| 250 | |
---|
| 251 | sub is_personal { |
---|
| 252 | my ($m) = @_; |
---|
| 253 | return ((lc($m->class) eq "message") |
---|
| 254 | && (lc($m->instance) eq "personal") |
---|
| 255 | && $m->is_private); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | sub is_mail { |
---|
| 259 | my ($m) = @_; |
---|
| 260 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | sub pretty_sender { |
---|
| 264 | my ($m) = @_; |
---|
| 265 | my $sender = $m->sender; |
---|
[8203afd] | 266 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
[f1e629d] | 267 | $sender =~ s/\@$realm$//; |
---|
| 268 | return $sender; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | # These are arguably zephyr-specific |
---|
| 272 | sub class { return shift->{"class"}; } |
---|
| 273 | sub instance { return shift->{"instance"}; } |
---|
| 274 | sub realm { return shift->{"realm"}; } |
---|
| 275 | sub opcode { return shift->{"opcode"}; } |
---|
| 276 | sub host { return shift->{"hostname"}; } |
---|
| 277 | sub hostname { return shift->{"hostname"}; } |
---|
| 278 | sub header { return shift->{"header"}; } |
---|
| 279 | sub auth { return shift->{"auth"}; } |
---|
| 280 | sub fields { return shift->{"fields"}; } |
---|
| 281 | sub zsig { return shift->{"zsig"}; } |
---|
| 282 | |
---|
| 283 | ##################################################################### |
---|
| 284 | ##################################################################### |
---|
[7e470da] | 285 | ################################################################################ |
---|
| 286 | package owl; |
---|
| 287 | |
---|
| 288 | ################################################################################ |
---|
[f265f94] | 289 | # Mainloop hook |
---|
[7e470da] | 290 | ################################################################################ |
---|
| 291 | |
---|
[f265f94] | 292 | our $shutdown; |
---|
[7e470da] | 293 | $shutdown = 0; |
---|
[f265f94] | 294 | our $reload; |
---|
[7e470da] | 295 | $reload = 0; |
---|
| 296 | |
---|
[8203afd] | 297 | #Run this on start and reload. Adds modules |
---|
[7e470da] | 298 | sub onStart |
---|
| 299 | { |
---|
| 300 | reload_init(); |
---|
| 301 | loadModules(); |
---|
| 302 | } |
---|
| 303 | ################################################################################ |
---|
| 304 | # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl |
---|
| 305 | ################################################################################ |
---|
[f265f94] | 306 | sub reload_hook (@) |
---|
[7e470da] | 307 | { |
---|
[8203afd] | 308 | BarnOwl::Hooks::startup(); |
---|
[7e470da] | 309 | return 1; |
---|
| 310 | } |
---|
| 311 | |
---|
[f265f94] | 312 | sub reload |
---|
[7e470da] | 313 | { |
---|
[f265f94] | 314 | # Use $reload to tell modules that we're performing a reload. |
---|
[8203afd] | 315 | { |
---|
| 316 | local $reload = 1; |
---|
| 317 | BarnOwl::mainloop_hook(); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | @BarnOwl::Hooks::onMainLoop = (); |
---|
| 321 | @BarnOwl::Hooks::onStartSubs = (); |
---|
| 322 | |
---|
| 323 | # Do reload |
---|
| 324 | package main; |
---|
| 325 | if (do "$ENV{HOME}/.owlconf" && BarnOwl::reload_hook(@_)) |
---|
| 326 | { |
---|
| 327 | return "owlconf reloaded"; |
---|
| 328 | } |
---|
| 329 | else |
---|
| 330 | { |
---|
| 331 | return "$ENV{HOME}/.owlconf load attempted, but error encountered:\n$@"; |
---|
| 332 | } |
---|
| 333 | package owl; |
---|
[7e470da] | 334 | } |
---|
| 335 | |
---|
| 336 | sub reload_init () |
---|
| 337 | { |
---|
[8203afd] | 338 | BarnOwl::command('alias reload perl BarnOwl::reload()'); |
---|
| 339 | BarnOwl::command('bindkey global "C-x C-r" command reload'); |
---|
[7e470da] | 340 | } |
---|
| 341 | |
---|
| 342 | ################################################################################ |
---|
| 343 | # Loads modules from ~/.owl/modules and owl's data directory |
---|
| 344 | ################################################################################ |
---|
| 345 | |
---|
[f2f5815] | 346 | sub loadModules () { |
---|
| 347 | my @modules; |
---|
[23be736] | 348 | my $rv; |
---|
[8203afd] | 349 | foreach my $dir ( BarnOwl::get_data_dir() . "/modules", |
---|
[23be736] | 350 | $ENV{HOME} . "/.owl/modules" ) |
---|
[f2f5815] | 351 | { |
---|
| 352 | opendir( MODULES, $dir ); |
---|
| 353 | |
---|
| 354 | # source ./modules/*.pl |
---|
| 355 | @modules = grep( /\.pl$/, readdir(MODULES) ); |
---|
| 356 | |
---|
| 357 | foreach my $mod (@modules) { |
---|
[8203afd] | 358 | owl::error("Loading $dir/$mod..."); |
---|
[23be736] | 359 | unless ($rv = do "$dir/$mod") { |
---|
[8203afd] | 360 | BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@; |
---|
| 361 | BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv; |
---|
[23be736] | 362 | } |
---|
[f2f5815] | 363 | } |
---|
| 364 | closedir(MODULES); |
---|
| 365 | } |
---|
[7e470da] | 366 | } |
---|
| 367 | |
---|
[8203afd] | 368 | package BarnOwl::Hooks; |
---|
| 369 | |
---|
| 370 | # Arrays of subrefs to be called at specific times. |
---|
| 371 | our @onStartSubs = (); |
---|
| 372 | our @onReceiveMsg = (); |
---|
| 373 | our @onMainLoop = (); |
---|
| 374 | our @onGetBuddyList = (); |
---|
[7e470da] | 375 | |
---|
[8203afd] | 376 | # Functions to call hook lists |
---|
| 377 | sub runHook($@) |
---|
| 378 | { |
---|
| 379 | my $hook = shift; |
---|
| 380 | my @args = @_; |
---|
| 381 | $_->(@args) for (@$hook); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | sub runHook_accumulate($@) |
---|
| 385 | { |
---|
| 386 | my $hook = shift; |
---|
| 387 | my @args = @_; |
---|
| 388 | return join("\n", map {$_->(@args)} @$hook); |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | ################################################################################ |
---|
| 392 | # Startup and Shutdown code |
---|
| 393 | ################################################################################ |
---|
| 394 | sub startup |
---|
| 395 | { |
---|
| 396 | # Modern versions of owl provides a great place to have startup stuff. |
---|
| 397 | # Put things in ~/.owl/startup |
---|
| 398 | |
---|
| 399 | #So that the user's .owlconf can have startsubs, we don't clear |
---|
| 400 | #onStartSubs; reload does however |
---|
| 401 | @onReceiveMsg = (); |
---|
| 402 | @onMainLoop = (); |
---|
| 403 | @onGetBuddyList = (); |
---|
| 404 | |
---|
| 405 | BarnOwl::onStart(); |
---|
| 406 | |
---|
| 407 | runHook(\@onStartSubs); |
---|
| 408 | |
---|
| 409 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | sub shutdown |
---|
| 413 | { |
---|
| 414 | # Modern versions of owl provides a great place to have shutdown stuff. |
---|
| 415 | # Put things in ~/.owl/shutdown |
---|
| 416 | |
---|
| 417 | # use $shutdown to tell modules that that's what we're doing. |
---|
| 418 | $BarnOwl::shutdown = 1; |
---|
| 419 | BarnOwl::mainloop_hook(); |
---|
| 420 | |
---|
| 421 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | sub mainloop_hook |
---|
| 425 | { |
---|
| 426 | runHook(\@onMainLoop); |
---|
| 427 | BarnOwl::mainlook_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
| 428 | } |
---|
[f2f5815] | 429 | |
---|
[7e470da] | 430 | ################################################################################ |
---|
| 431 | # Hooks into receive_msg() |
---|
| 432 | ################################################################################ |
---|
| 433 | |
---|
[f2f5815] | 434 | sub receive_msg |
---|
[7e470da] | 435 | { |
---|
| 436 | my $m = shift; |
---|
[f2f5815] | 437 | runHook(\@onReceiveMsg, $m); |
---|
[8203afd] | 438 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
[7e470da] | 439 | } |
---|
| 440 | |
---|
| 441 | ################################################################################ |
---|
| 442 | # Hooks into get_blist() |
---|
| 443 | ################################################################################ |
---|
| 444 | |
---|
[f2f5815] | 445 | sub get_blist |
---|
[7e470da] | 446 | { |
---|
[8203afd] | 447 | return runHook_accumulate(\@onGetBuddyList); |
---|
[7e470da] | 448 | } |
---|
[dd16bdd] | 449 | |
---|
[f1e629d] | 450 | # switch to package main when we're done |
---|
| 451 | package main; |
---|
[7e470da] | 452 | # alias the hooks |
---|
[c681337] | 453 | { |
---|
| 454 | no strict 'refs'; |
---|
| 455 | foreach my $hook qw (onStartSubs |
---|
| 456 | onReceiveMsg |
---|
| 457 | onMainLoop |
---|
| 458 | onGetBuddyList ) { |
---|
[8203afd] | 459 | *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
---|
| 460 | *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook}; |
---|
[c681337] | 461 | } |
---|
[7e470da] | 462 | } |
---|
[f1e629d] | 463 | |
---|
[d03091c] | 464 | # load the config file |
---|
[8203afd] | 465 | if (-r $BarnOwl::configfile) { |
---|
[00f9a7d] | 466 | undef $@; |
---|
[8203afd] | 467 | do $BarnOwl::configfile; |
---|
[00f9a7d] | 468 | die $@ if $@; |
---|
[d03091c] | 469 | } |
---|
| 470 | |
---|
[f1e629d] | 471 | 1; |
---|