return sprintf "\003%d%s", $rcolors[$col % @rcolors], $nick;
}
+sub save_context {
+ my $cur_cxt = get_context;
+ bless \$cur_cxt, 'Xchat::XPI::Utils::ContextGuard';
+}
+
+sub local_context {
+ return unless @_ >= 2;
+ my $code = pop;
+ my $new_cxt = (@_ == 1) ? $_[0] : find_context(@_);
+ my $cur_cxt = get_context;
+ if (set_context $new_cxt) {
+ my $guard = bless \$cur_cxt, 'Xchat::XPI::Utils::ContextGuard';
+ return $code->();
+ } else {
+ return undef;
+ }
+}
+
+sub Xchat::XPI::Utils::ContextGuard::DESTROY {
+ set_context ${$_[0]};
+}
+
use base qw<Exporter>;
our @EXPORT = ();
-our %EXPORT_TAGS = ('funcs' => [ qw<dye_nick> ]);
+our %EXPORT_TAGS = ('funcs' => [ qw<
+ dye_nick
+ save_context local_context
+> ]);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
use lib get_info 'xchatdir';
use Xchat::XPI;
use Xchat::XPI::Events qw<delay>;
+use Xchat::XPI::Utils qw<save_context local_context>;
use constant {
JOIN_DELAY => 1000,
return EAT_NONE if $fetched{$serv}{$chan};
- my $oldctxt = get_context;
- set_context $chan, $serv or return EAT_NONE;
- fetch $serv, $chan;
- scan $serv, $chan;
- set_context $oldctxt;
+ local_context $chan, $serv => sub {
+ fetch $serv, $chan;
+ scan $serv, $chan;
+ };
return EAT_NONE;
};
my $chan = lc substr $_[0][2], 1; # starts with colon
my $clones = add $nick, host($userhost), $serv, $chan;
- if ($clones > 0) {
- my $oldctxt = get_context;
- if (set_context $chan, $serv) {
- print $ph clone_enter_str $userhost, $nick, $clones;
- set_context $oldctxt;
- } else {
- delay JOIN_DELAY, sub {
- my $oldctxt = get_context;
- return unless set_context $chan, $serv;
+ return EAT_NONE unless $clones > 0;
+
+ local_context $chan, $serv => sub {
+ print $ph clone_enter_str $userhost, $nick, $clones;
+ 1
+ } or do {
+ delay JOIN_DELAY, sub {
+ local_context $chan, $serv => sub {
print $ph clone_enter_str $userhost, $nick, $clones;
- set_context $oldctxt;
- };
+ }
}
- }
+ };
return EAT_NONE;
};
get_list 'channels';
if (nickcmp get_info('nick'), $nick) {
- my $oldctxt = get_context;
- my $host = host $userhost;
+ my $guard = save_context;
+ my $host = host $userhost;
for (@chans) {
my $clones = remove $nick, $host, $serv, $_->[0];
if ($clones > 0) {
print $ph clone_leave_str $userhost, $nick, $clones;
}
}
- set_context $oldctxt;
} else {
flush $serv, $_->[0] for @chans;
}
use lib get_info 'xchatdir';
use Xchat::XPI;
-use Xchat::XPI::Net qw<resolve whois>;
+use Xchat::XPI::Net qw<resolve whois>;
+use Xchat::XPI::Utils qw<save_context>;
our $VERSION = '0.02';
sub dig_print {
my $p = $_[0];
my ($context, $req) = @{$_[1]};
- my $oldctxt = get_context;
+ my $guard = save_context;
set_context $context;
if ($p) {
my @a = $p->answer;
} else {
print $ph 'Request ' . $req->{host} . " timed out\n";
}
- set_context $oldctxt;
}
hook_command 'NETWHOIS', sub {
sub netwhois_print {
my $raw = $_[0];
my ($context, $req) = @{$_[1]};
- my $oldctxt = get_context;
+ my $guard = save_context;
set_context $context;
if ($raw) {
$raw =~ s/.*(Domain|inetnum)/$1/s;
} else {
output '*', 'No results for ' . $req->{host};
}
- set_context $oldctxt;
}
hook_command 'TLD', sub {
use lib get_info 'xchatdir';
use Xchat::XPI;
+use Xchat::XPI::Utils qw<save_context>;
our $VERSION = '0.03';
push @{$chans{$_->{type}}}, $_ for grep $_->{server} eq $serv,
get_list 'channels';
- my $oldctxt = get_context;
+ my $guard = save_context;
my $onachan;
for (@{$chans{2}}) {
print_op $nick, $host;
}
- set_context $oldctxt;
-
return EAT_NONE;
};
use lib get_info 'xchatdir';
use Xchat::XPI;
+use Xchat::XPI::Utils qw<save_context>;
our $VERSION = '0.03';
hook_command 'AID', sub {
my $forcepasswd = $_[0][1];
- my $oldctxt = get_context;
- my @contexts = get_servers_ctxt;
+ my @contexts = get_servers_ctxt;
+ my $guard = save_context;
for (@contexts) {
set_context $_;
my $passwd = $forcepasswd || get_info 'nickserv';
next unless $passwd;
command 'ID ' . $passwd;
}
- set_context $oldctxt;
return EAT_ALL;
}, {
help_text => 'AID [password], identify you on all servers'
my $target = $_[0][1] || get_prefs 'irc_nick1';
return EAT_ALL unless $target;
my $forcepasswd = $_[0][2];
- my $oldctxt = get_context;
- my @contexts = get_servers_ctxt;
+ my @contexts = get_servers_ctxt;
+ my $guard = save_context;
for (@contexts) {
set_context $_;
my $passwd = $forcepasswd || get_info 'nickserv';
command join ' ', 'GHOST', $target, $passwd;
}
}
- set_context $oldctxt;
return EAT_ALL;
}, {
help_text => 'AGHOST [nick] [password], kill usurping clients on all servers'
use lib get_info 'xchatdir';
use Xchat::XPI;
+use Xchat::XPI::Utils qw<save_context>;
use constant MAX_MATCHES => 10;
}
hook_command 'TC', sub {
- my $oldctxt = get_context;
+ my $guard = save_context;
for (get_list 'channels') {
set_context $_->{context};
command 'GUI COLOR 0';
}
- set_context $oldctxt;
return EAT_ALL;
}, {
help_text => 'TC, reset all tab color indicators'