Sfoglia il codice sorgente

Patch from Phil:
Add $debug support to Common.pm and improve find_alias()

Morten Brix Pedersen 25 anni fa
parent
commit
77c04d651c
2 ha cambiato i file con 20 aggiunte e 4 eliminazioni
  1. 19 4
      modules/Pisg/Common.pm
  2. 1 0
      pisg.pl

+ 19 - 4
modules/Pisg/Common.pm

@@ -7,7 +7,13 @@ use Exporter;
 use strict;
 $^W = 1;
 
-my (%aliases, %aliaswilds, %ignored);
+my ($conf, $debug);
+my (%aliases, %aliaswilds, %ignored, %aliasseen);
+
+sub init_common {
+#    $conf = shift;
+    $debug = shift;
+}
 
 # add_alias assumes that the first argument is the true nick and the second is
 # the alias, but will accomidate other arrangements if necessary.
@@ -25,8 +31,10 @@ sub add_alias {
         }
     } elsif (not defined $aliases{$lcalias}) {
         $aliases{$lcalias} = $aliases{$lcnick};
+    } elsif ($aliases{$lcnick} ne $aliases{$lcalias}) {
+	$debug->("Alias collision: alias $alias -> $aliases{$lcalias} but nick $nick -> $aliases{$lcnick}");
     }
-    #$debug->("Alias added: $alias -> $aliases{$lcalias}");
+    $debug->("Alias added: $alias -> $aliases{$lcalias}");
 }
 
 sub add_aliaswild {
@@ -38,7 +46,7 @@ sub add_aliaswild {
         $aliases{$lcnick}  = $nick;
     }
     $aliaswilds{$lcalias} = $nick;
-    #$debug->("Aliaswild added: $alias -> $aliaswilds{$lcalias}");
+    $debug->("Aliaswild added: $alias -> $aliaswilds{$lcalias}");
 }
 
 sub add_ignore {
@@ -53,12 +61,19 @@ sub is_ignored {
     }
 }
 
+# For efficiency reasons, find_alias() caches aliases when it finds them,
+# because the regexp search through %aliaswilds is *really* expensive.
+# %aliasseen is used to mark nicks for which nothing matches--we can't add
+# such nicks to an actual alias, though, because they might be aliased (e.g.
+# by a nick change) later.
 sub find_alias {
     my ($nick) = @_;
     my $lcnick = lc($nick);
 
     if ($aliases{$lcnick}) {
         return $aliases{$lcnick};
+    } elsif ($aliasseen{$lcnick}) {
+	return $aliasseen{$lcnick};
     } else {
         foreach (keys %aliaswilds) {
             if ($nick =~ /^$_$/i) {
@@ -67,7 +82,7 @@ sub find_alias {
             }
         }
     }
-    add_alias($nick, $nick);
+    $aliasseen{$lcnick} = $nick;
     return $nick;
 }
 

+ 1 - 0
pisg.pl

@@ -118,6 +118,7 @@ sub load_modules {
     push @INC, $conf->{modules_dir};
     require Pisg::Common;
     Pisg::Common->import();
+    Pisg::Common::init_common($debug);
 }
 
 sub main {