Changeset 1cf32e7d for perlwrap.pm
- Timestamp:
- Mar 28, 2007, 9:32:11 PM (17 years ago)
- Branches:
- master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- b3a40c7
- Parents:
- 3066d23 (diff), a387d12e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perlwrap.pm
r37dd88c r1cf32e7d 7 7 # XXX NOTE: This file is sourced before almost any barnowl 8 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 9 # execute any owl commands. Any code that needs to do so should live 10 # in BarnOwl::Hooks::_startup 12 11 13 12 use strict; … … 15 14 16 15 package BarnOwl; 17 18 16 19 17 BEGIN { … … 44 42 my ($m) = @_; 45 43 $m->legacy_populate_global(); 46 return &BarnOwl::Hooks:: receive_msg($m);44 return &BarnOwl::Hooks::_receive_msg($m); 47 45 } 48 46 … … 204 202 205 203 sub smartfilter { 206 die("smartfilter not supported for this message ");204 die("smartfilter not supported for this message\n"); 207 205 } 208 206 … … 351 349 ##################################################################### 352 350 ################################################################################ 353 package BarnOwl; 354 355 ################################################################################ 356 # Mainloop hook 357 ################################################################################ 358 359 our $shutdown; 360 $shutdown = 0; 361 our $reload; 362 $reload = 0; 363 364 #Run this on start and reload. Adds modules 365 sub onStart 366 { 367 _load_owlconf(); 368 reload_init(); 369 loadModules(); 370 } 371 ################################################################################ 372 # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl 373 ################################################################################ 374 sub reload_hook (@) 375 { 376 BarnOwl::Hooks::startup(); 377 return 1; 378 } 379 380 sub reload 381 { 382 # Use $reload to tell modules that we're performing a reload. 383 { 384 local $reload = 1; 385 BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; 386 } 387 388 @BarnOwl::Hooks::onMainLoop = (); 389 @BarnOwl::Hooks::onStartSubs = (); 390 391 # Do reload 392 package main; 393 if (-r $BarnOwl::configfile) { 394 undef $@; 395 do $BarnOwl::configfile; 396 BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@; 397 } 398 BarnOwl::reload_hook(@_); 399 package BarnOwl; 400 } 401 402 sub reload_init () 403 { 404 BarnOwl::command('alias reload perl BarnOwl::reload()'); 405 BarnOwl::command('bindkey global "C-x C-r" command reload'); 406 } 407 408 ################################################################################ 409 # Loads modules from ~/.owl/modules and owl's data directory 410 ################################################################################ 411 412 sub loadModules () { 413 my @modules; 414 my $rv; 415 foreach my $dir ( BarnOwl::get_data_dir() . "/modules", 416 $ENV{HOME} . "/.owl/modules" ) 417 { 418 opendir( MODULES, $dir ); 419 420 # source ./modules/*.pl 421 @modules = sort grep( /\.pl$/, readdir(MODULES) ); 422 423 foreach my $mod (@modules) { 424 unless ($rv = do "$dir/$mod") { 425 BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@; 426 BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv; 427 } 428 } 429 closedir(MODULES); 430 } 431 } 351 352 package BarnOwl::Hook; 353 354 sub new { 355 my $class = shift; 356 return bless [], $class; 357 } 358 359 sub run { 360 my $self = shift; 361 my @args = @_; 362 return map {$_->(@args)} @$self; 363 } 364 365 sub add { 366 my $self = shift; 367 my $func = shift; 368 die("Not a coderef!") unless ref($func) eq 'CODE'; 369 push @$self, $func; 370 } 371 372 sub clear { 373 my $self = shift; 374 @$self = (); 375 } 376 377 package BarnOwl::Hooks; 378 379 use Exporter; 380 381 our @EXPORT_OK = qw($startup $shutdown 382 $receiveMessage $mainLoop 383 $getBuddyList); 384 385 our %EXPORT_TAGS = (all => [@EXPORT_OK]); 386 387 our $startup = BarnOwl::Hook->new; 388 our $shutdown = BarnOwl::Hook->new; 389 our $receiveMessage = BarnOwl::Hook->new; 390 our $mainLoop = BarnOwl::Hook->new; 391 our $getBuddyList = BarnOwl::Hook->new; 392 393 # Internal startup/shutdown routines called by the C code 432 394 433 395 sub _load_owlconf { 434 # Only do this the first time435 return if $BarnOwl::reload;436 396 # load the config file 437 397 if ( -r $BarnOwl::configfile ) { … … 451 411 } 452 412 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 = (); 460 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 413 sub _startup { 414 _load_owlconf(); 415 416 if(eval {require BarnOwl::ModuleLoader}) { 417 eval { 418 BarnOwl::ModuleLoader->load_all; 419 }; 420 BarnOwl::error("Error loading modules: $@") if $@; 421 } else { 422 BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); 423 } 424 425 $startup->run(0); 494 426 BarnOwl::startup() if *BarnOwl::startup{CODE}; 495 427 } 496 428 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; 429 sub _shutdown { 430 $shutdown->run; 431 432 BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; 433 } 434 435 sub _receive_msg { 436 my $m = shift; 437 438 $receiveMessage->run($m); 439 440 BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; 441 } 442 443 sub _mainloop_hook { 444 $mainLoop->run; 504 445 BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; 505 506 BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; 507 } 508 509 sub mainloop_hook 510 { 511 runHook(\@onMainLoop); 512 BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; 513 } 514 515 ################################################################################ 516 # Hooks into receive_msg() 517 ################################################################################ 518 519 sub receive_msg 520 { 521 my $m = shift; 522 runHook(\@onReceiveMsg, $m); 523 BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; 524 } 525 526 ################################################################################ 527 # Hooks into get_blist() 528 ################################################################################ 529 530 sub get_blist 531 { 532 return runHook_accumulate(\@onGetBuddyList); 446 } 447 448 sub _get_blist { 449 return join("\n", $getBuddyList->run); 533 450 } 534 451 … … 644 561 # switch to package main when we're done 645 562 package main; 646 # alias the hooks 647 { 648 no strict 'refs'; 649 foreach my $hook qw (onStartSubs 650 onReceiveMsg 651 onMainLoop 652 onGetBuddyList ) { 653 *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook}; 654 *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook}; 655 } 656 } 563 564 # Shove a bunch of fake entries into @INC so modules can use or 565 # require them without choking 566 $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm 567 BarnOwl/Message.pm BarnOwl/Style.pm)); 657 568 658 569 1; 570
Note: See TracChangeset
for help on using the changeset viewer.