From a22ee16ffbdba74f4a4c211d679684ec24897b5c Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Sun, 30 Jan 2011 13:31:16 -0700 Subject: [PATCH] * Added has_priv() to API::Std. * Finished the host-based privilege/permission system. * Fixed some typos. --- bin/auto | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ doc/CHANGELOG | 2 ++ src/API/Std.pm | 20 +++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/bin/auto b/bin/auto index 01c9c30..ee6e0da 100755 --- a/bin/auto +++ b/bin/auto @@ -9,6 +9,7 @@ use strict; use warnings; use POSIX; use locale; +use feature qw(switch); use Mouse; use IO::Socket; use IO::Select; @@ -146,6 +147,55 @@ API::Log::expire_logs(); # Load database. DB::load(); +# Parse privileges. +our (%PRIVILEGES); +# If there are any privsets. +if (conf_get("privset")) { + # Get them. + my %tcprivs = conf_get("privset"); + + foreach my $tckpriv (keys %tcprivs) { + # For each privset, get the inner values. + my %mcprivs = conf_get("privset:$tckpriv"); + + # Iterate through them. + foreach my $mckpriv (keys %mcprivs) { + # Switch statement for the values. + given ($mckpriv) { + # If it's 'priv', save it as a privilege. + when ("priv") { + if (defined $PRIVILEGES{$tckpriv}) { + # If this privset exists, push to it. + push(@{ $PRIVILEGES{$tckpriv} }, ($mcprivs{$mckpriv})[0][0]); + } + else { + # Otherwise, create it. + @{ $PRIVILEGES{$tckpriv} } = (($mcprivs{$mckpriv})[0][0]); + } + } + # If it's 'inherit', inherit the privileges of another privset. + when ("inherit") { + # If the privset we're inheriting exists, continue. + if (defined $PRIVILEGES{($mcprivs{$mckpriv})[0][0]}) { + # Iterate through each privilege. + foreach (@{ $PRIVILEGES{($mcprivs{$mckpriv})[0][0]} }) { + # And save them to the privset inheriting them + if (defined $PRIVILEGES{$tckpriv}) { + # If this privset exists, push to it. + push(@{ $PRIVILEGES{$tckpriv} }, $_); + } + else { + # Otherwise, create it. + @{ $PRIVILEGES{$tckpriv} } = ($_); + } + } + } + } + } + } + } +} + # Successful startup. our $STARTTIME = time; println "* Auto successfully started at ".POSIX::strftime("%Y-%m-%d %I:%M:%S %p", localtime)."."; @@ -234,6 +284,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::has_priv(API::Std::match_user(%data), "cmd.say")) { + 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 f96bab0..466bcde 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,8 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev ================================================================== + * Added has_priv() to API::Std. + * Finished the host-based privilege/permission system. * Added hook on_preconnect, this comes before USER (after PASS). * Added match_mask() to API::IRC and match_user() to API::Std, match_user checks config user:mask to find a user block, if diff --git a/src/API/Std.pm b/src/API/Std.pm index 2918b4f..873ce0d 100644 --- a/src/API/Std.pm +++ b/src/API/Std.pm @@ -331,7 +331,7 @@ sub trans } } -# Privilege subroutine. +# Match user subroutine. sub match_user { my (%user) = @_; @@ -359,6 +359,24 @@ sub match_user return 0; } +# Privilege subroutine. +sub has_priv +{ + my ($cuser, $cpriv) = @_; + + if (conf_get("user:$cuser:privs")) { + my $cups = (conf_get("user:$cuser:privs"))[0][0]; + + if (defined $Auto::PRIVILEGES{$cups}) { + foreach (@{ $Auto::PRIVILEGES{$cups} }) { + return 1 if ($_ eq $cpriv or $_ eq "ALL"); + } + } + } + + return 0; +} + # Error subroutine. sub err {