]> git.vpit.fr Git - perl/scripts/xchat.git/blobdiff - clones.pl
Add better context handling helpers
[perl/scripts/xchat.git] / clones.pl
index 20aecfae044feabb10c3711294a00c6d66c89ed1..fded636bd38c8aee60a92426d5ddb911972026ab 100755 (executable)
--- a/clones.pl
+++ b/clones.pl
@@ -10,6 +10,7 @@ use Xchat qw<:all>;
 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,
@@ -168,11 +169,10 @@ hook_server '315', sub { # WHO end
 
  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;
 };
@@ -186,20 +186,18 @@ hook_server 'JOIN', sub {
  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;
 };
@@ -248,8 +246,8 @@ hook_server 'QUIT', sub {
                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) {
@@ -257,7 +255,6 @@ hook_server 'QUIT', sub {
     print $ph clone_leave_str $userhost, $nick, $clones;
    }
   }
-  set_context $oldctxt;
  } else {
   flush $serv, $_->[0] for @chans;
  }