From a04e181378692e8bd9cd7c22b20fa4cef4341bc0 Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Sun, 30 Jan 2011 10:26:51 -0700 Subject: [PATCH] Added match_mask() to API::IRC and match_user() to API::Std, match_user checks config user:mask to find a user block, if a match is found, the configured username is returned. --- bin/auto | 12 ++++++++++++ doc/CHANGELOG | 3 +++ src/API/IRC.pm | 21 ++++++++++++++++++++- src/API/Std.pm | 28 +++++++++++++++++++++++----- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/bin/auto b/bin/auto index a94cb4f..81c91b8 100755 --- a/bin/auto +++ b/bin/auto @@ -230,6 +230,18 @@ else { } undef $it; +### NOTE: REMOVE THIS FROM MAINLINE CODE. IT IS FOR DEVELOPMENT PURPOSES ONLY. ### +sub hw { + my (%data) = @_; my @argv = @{ $data{args} }; + if (API::Std::match_user(( 'nick' => $data{nick}, 'user' => $data{user}, 'host' => $data{host} ))) { + API::IRC::privmsg($data{svr}, $data{chan}, @argv); + } + return 1; +} + +cmd_add("SAY", 0, 0, 0, \&Auto::hw) or exit; +### End ### + # Infinite while loop. while (1) { # Timer check. diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 357b693..cc427e1 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,9 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev ================================================================== + * Added match_mask() to API::IRC and match_user() to API::Std, + match_user checks config user:mask to find a user block, if + a match is found, the configured username is returned. * Added rchook_add() and rchook_del() to API::Std. * Parse NOTICE and added hook on_notice. * Added hooks on_uprivmsg and on_cprivmsg. diff --git a/src/API/IRC.pm b/src/API/IRC.pm index a7d872c..17f14bd 100644 --- a/src/API/IRC.pm +++ b/src/API/IRC.pm @@ -9,7 +9,7 @@ use warnings; use Exporter; our @ISA = qw(Exporter); -our @EXPORT_OK = qw(cjoin cpart cmode umode privmsg notice quit nick names usrc); +our @EXPORT_OK = qw(cjoin cpart cmode umode privmsg notice quit nick names usrc match_mask); # Join a channel. @@ -133,5 +133,24 @@ sub usrc ); } +# Match two IRC masks. +sub match_mask +{ + my ($mu, $mh) = @_; + + # Prepare the regex. + $mh =~ s/\./\\\./g; + $mh =~ s/\?/\./g; + $mh =~ s/\*/\.\*/g; + $mh = '^'.$mh.'$'; + + # Let's grep the user's mask. + if (grep(/$mh/, $mu)) { + return 1; + } + + return 0; +} + 1; diff --git a/src/API/Std.pm b/src/API/Std.pm index ca1a419..2f88141 100644 --- a/src/API/Std.pm +++ b/src/API/Std.pm @@ -11,7 +11,7 @@ use Exporter; our (%LANGE, %MODULE, %EVENTS, %HOOKS, %CMDS); our @ISA = qw(Exporter); our @EXPORT_OK = qw(conf_get trans err awarn timer_add timer_del cmd_add - cmd_del hook_add hook_del rchook_add rchook_del); + cmd_del hook_add hook_del rchook_add rchook_del match_user); # Initialize a module. @@ -332,13 +332,31 @@ sub trans } # Privilege subroutine. -sub has_priv +sub match_user { - my (%user, $priv) = @_; + my (%user) = @_; + + # Get data from config. + return 0 if (!conf_get("user")); + my %uhp = conf_get("user"); + foreach my $userkey (keys %uhp) { + # For each user block. + my %ulhp = %{ $uhp{$userkey} }; + foreach my $uhk (keys %ulhp) { + # For each user. + if ($uhk eq "mask") { + # Put together the user information. + my $mask = $user{nick}."!".$user{user}."@".$user{host}; + if (API::IRC::match_mask($mask, ($ulhp{$uhk})[0][0])) { + # We've got a host match. + return $userkey; + } + } + } + } - - return 1; + return 0; } # Error subroutine.