]> ruin.nu Git - NDIRC.git/blobdiff - Misc.pm
r26 feudalism, late commit
[NDIRC.git] / Misc.pm
diff --git a/Misc.pm b/Misc.pm
index 2b479dd789e2a2ecf84c71a0bf590ffb7f08c237..f1bf8165d3e18a661feb3da0913b22947dd2bac2 100644 (file)
--- a/Misc.pm
+++ b/Misc.pm
 #   Free Software Foundation, Inc.,                                       *
 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 #**************************************************************************/
-package ND::IRC::Misc;
+package NDIRC::Misc;
 use strict;
 use warnings;
 require Exporter;
 
 our @ISA = qw/Exporter/;
 
-our @EXPORT = qw/valuecolor/;
+our @EXPORT = qw/valuecolor addCommand parseCommand commands/;
+
+our %channels;
+our %commands;
+
+$channels{'#def-ndawn'} = ['all','member','def'];
+$channels{'#nd'} = ['all','member'];
+$channels{'#ndef'} = ['all','member','scan'];
+$channels{'#nd-day'} = ['all','member'];
+$channels{'#ndintel'} = ['all','member'];
+$channels{'#nd-officers'} = ['all','member'];
+$channels{'#nd-ia'} = ['all','member'];
+$channels{'#ndawn'} = ['all'];
+$channels{'pm'} = ['pm'];
 
 $ND::defchan = "#def-ndawn";
 $ND::memchan = "#nd";
@@ -33,17 +46,64 @@ $ND::intelchan = "#ndintel";
 $ND::officerchan = "#nd-officers";
 $ND::communitychan = "#ndawn";
 $ND::pubchan = "#newdawn";
-$ND::xanchan = "#ViolatorS";
 
 sub valuecolor {
        my $s = $_;
-       $s = $_[1] if defined $_[1];
+       $s = $_[1] if $#_ >= 1;
        $s = "" unless defined $s;
-       return chr(3)."5$s".chr(3) if $s eq 'Hostile';
-       return chr(3)."3$s".chr(3) if $s eq 'Friendly';
-       return chr(3)."3$s".chr(3) if $s eq 'Nap' or $s eq 'NAP';
-       return chr(2)."$s".chr(2) if $_[0];
+       return chr(3)."5$s".chr(15) if $s eq 'Hostile';
+       return chr(3)."3$s".chr(15) if $s eq 'Friendly';
+       return chr(3)."3$s".chr(15) if $s eq 'Nap' or $s eq 'NAP';
+       return chr(2)."$s".chr(15) if $_[0];
        return $s;
 }
 
+sub addCommand {
+       my ($command, $list, $func) = @_;
+       $commands{$command} = {fun => $func, acc => $list};
+}
+
+sub parseCommand {
+       my ($msg,$channel) = @_;
+       if ($msg =~ /^(\S+)(?: (.+))?$/){
+               my $c = $1;
+               my $args = $2;
+               my @k = keys %commands;
+               if (exists $commands{$c}){
+                       my $a = $commands{$c}->{acc};
+                       my $b = (exists $channels{lc $channel} ? $channels{lc $channel} : ['all']);
+                       if (intersect($a,$b) > 0){
+                               $commands{$c}->{fun}->($args,$c);
+                               return 1;
+                       }
+               }
+       }
+       return 0;
+}
+
+sub commands {
+       my ($channel) = @_;
+       my @commands;
+       my $b = (exists $channels{lc $channel} ? $channels{lc $channel} : ['all']);
+       for my $c (sort keys %commands){
+               my $a = $commands{$c}->{acc};
+               if (intersect($a,$b) > 0){
+                       push @commands, $c;
+               }
+       }
+       return join ', ', @commands;
+}
+
+sub intersect {
+       my ($a, $b) = @_;
+       my %union;
+       foreach my $e (@{$a}) { $union{$e} = 1 }
+
+       my %isect;
+       foreach my $e (@{$b}) {
+            $isect{$e} = 1 if ( exists $union{$e} )
+       }
+       return keys %isect;
+}
+
 1;