use Xchat;
+use lib Xchat::get_info('xchatdir');
+use Xchat::XPI::Utils qw<called_from_script>;
+
our $VERSION = '0.05';
use constant { COLOR_TAG => "\00307" };
$class = ref($class) || $class;
my %opts = @_;
- my ($caller, $file) = (caller 0)[0, 1];
+ my $caller = (caller 0)[0];
unless (defined $opts{tag}) {
$opts{tag} = ($caller =~ /([^:]*):*$/) ? $1 : 'UNKNOWN';
*$ph->{$_} = $opts{$_} for @attributes;
*$ph->{buf} = '';
- my $internal_pkg = Xchat::Embed::file2pkg($file);
- no warnings 'redefine';
- local *Xchat::Embed::find_pkg = sub { $internal_pkg, $caller };
- local *HexChat::Embed::find_pkg = sub { $internal_pkg, $caller };
- Xchat::register(@opts{qw<name version banner>}, sub {
- $ph->flush;
- if (*$ph->{atexit_id}) {
- my @callbacks = sort { *$ph->{atexit}{$b} <=> *$ph->{atexit}{$a} }
- keys %{*$ph->{atexit}};
- $_->($ph) for @callbacks;
- }
- undef $ph;
- });
+ called_from_script {
+ Xchat::register(@opts{qw<name version banner>}, sub {
+ $ph->flush;
+ if (*$ph->{atexit_id}) {
+ my @callbacks = sort { *$ph->{atexit}{$b} <=> *$ph->{atexit}{$a} }
+ keys %{*$ph->{atexit}};
+ $_->($ph) for @callbacks;
+ }
+ undef $ph;
+ })
+ };
$ph->add_atexit($opts{unload}) if $opts{unload};
$ph->print("\002", $ph->name, ' v', $ph->version,
use Xchat qw<:all>;
+use lib get_info 'xchatdir';
+use Xchat::XPI::Utils qw<called_from_script>;
+
use constant DEFAULT_DELAY => 1000;
our $VERSION = '0.04';
$delay = DEFAULT_DELAY;
}
- hook_timer $delay, \&_delay_cb, { data => \@_ };
+ called_from_script {
+ hook_timer $delay, \&_delay_cb, { data => \@_ }
+ }
}
sub _filter_cb {
sub filter {
my $from = shift;
- hook_print $from, \&_filter_cb, { data => \@_ };
+ called_from_script {
+ hook_print $from, \&_filter_cb, { data => \@_ }
+ }
}
use base qw<Exporter>;
use strict;
use warnings;
+use Xchat qw<:all>;
+
+use lib get_info 'xchatdir';
+use Xchat::XPI::Utils qw<called_from_script>;
+
use IO::Socket::INET;
use Net::DNS;
-use Xchat qw<:all>;
-
our $VERSION = '0.03';
my $res;
return unless $sock;
$sock->autoflush(1);
- return Xchat::hook_fd($sock, \&_dns_recv, {
- flags => FD_READ,
- data => [ $callback, $args ],
- });
+ return called_from_script {
+ Xchat::hook_fd($sock, \&_dns_recv, {
+ flags => FD_READ,
+ data => [ $callback, $args ],
+ });
+ }
}
sub _dns_recv {
print $sock "$host\x0D\x0A";
$sock->shutdown(1); # stop writing
- return Xchat::hook_fd($sock, \&_whois_recv, {
- flags => FD_READ,
- data => [ $callback, $args ],
- });
+ return called_from_script {
+ Xchat::hook_fd($sock, \&_whois_recv, {
+ flags => FD_READ,
+ data => [ $callback, $args ],
+ });
+ }
}
sub _whois_recv {
set_context ${$_[0]};
}
+sub called_from_script (&) {
+ my $code = shift;
+ my $level = 0;
+ my ($package, $filename);
+ while (1) {
+ my @frame = caller $level;
+ last unless @frame;
+ if ($frame[0] !~ /^(?:Xchat|HexChat)::XPI\b/) {
+ ($package, $filename) = @frame[0, 1];
+ last;
+ }
+ ++$level;
+ }
+ if (defined $filename) {
+ my $internal_package = Xchat::Embed::file2pkg($filename);
+ my $mock = sub { $internal_package, $package };
+ no warnings 'redefine';
+ local *Xchat::Embed::find_pkg = $mock;
+ local *HexChat::Embed::find_pkg = $mock;
+ $code->();
+ }
+}
+
use base qw<Exporter>;
our @EXPORT = ();
our %EXPORT_TAGS = ('funcs' => [ qw<
dye_nick
save_context local_context
+ called_from_script
> ]);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];