diff options
Diffstat (limited to 'utils/rhrd-group')
-rwxr-xr-x | utils/rhrd-group | 321 |
1 files changed, 321 insertions, 0 deletions
diff --git a/utils/rhrd-group b/utils/rhrd-group new file mode 100755 index 0000000..246d450 --- /dev/null +++ b/utils/rhrd-group @@ -0,0 +1,321 @@ +#!/usr/bin/perl -w +# +# rhrdlibs +# +# Copyright (C) 2015 Christian Pointner <equinox@helsinki.at> +# +# This file is part of rhrdlibs. +# +# rhrdlibs is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# any later version. +# +# rhrdlibs is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with rhrdlibs. If not, see <http://www.gnu.org/licenses/>. +# + +use strict; +use lib "../lib/"; +use RHRD::rddb; + +# this is ridiculous but makes it a little harder to create/remove users... +# if ($> != 0 ) { +# print STDERR "this must be run as root!\n"; +# exit 1; +# } + +sub print_usage +{ + print STDERR "Usage: rd-group list\n" . + " rd-group (check|remove|get-members|get-carts|get-reports) <groupname>\n" . + " rd-group add <groupname> [ <description> ]\n" . + " rd-group (add-member|remove-member|is-member) <groupname> <user>\n" . + " rd-group set-carts <groupname> [ <low> [ <high> [ <type> [ <enforce range> ]]]\n" . + " rd-group set-reports <groupname> [ <nownext> [ <traffic> [ <music> ]]]\n"; +} + +sub list +{ + my ($ctx) = @_; + + my @groups = RHRD::rddb::list_groups($ctx); + if(!defined $groups[0] && defined $groups[1]) { + print STDERR "$groups[1]: $groups[2]"; + return 1; + } + for my $group (@groups) { + print $group . "\n"; + } + return 0; +} + +sub check +{ + my ($ctx, $groupname) = @_; + + my ($result, $status, $errorstring) = RHRD::rddb::check_group($ctx, $groupname); + print STDERR "$result, $status: $errorstring\n"; + return $result; +} + +sub add +{ + my ($ctx, $groupname, $description) = @_; + + my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($ctx, $groupname, $description); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub remove +{ + my ($ctx, $groupname) = @_; + + my @results = RHRD::rddb::remove_group($ctx, $groupname); + if(!defined $results[0] && defined $results[2]) { + print STDERR "$results[2]\n"; + return 1; + } + for my $href (@results) { + print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; + } + return 0; +} + +sub get_members +{ + my ($ctx, $groupname) = @_; + + my @users = RHRD::rddb::get_group_members($ctx, $groupname); + if(!defined $users[0] && defined $users[1]) { + print STDERR "$users[2]\n"; + return 1; + } + for my $user (sort @users) { + print "$user\n"; + } + return 0; +} + +sub add_member +{ + my ($ctx, $groupname, $username) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::add_group_member($ctx, $groupname, $username); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub remove_member +{ + my ($ctx, $groupname, $username) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($ctx, $groupname, $username); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub is_member +{ + my ($ctx, $groupname, $username) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::is_group_member($ctx, $groupname, $username); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n"; + return (($cnt) ? 0 : 1); +} + +sub get_carts +{ + my ($ctx, $groupname) = @_; + + my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($ctx, $groupname); + unless(defined $low) { + print STDERR "$type\n"; + return 1; + } + print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n"; + return 0; +} + +sub set_carts +{ + my ($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub get_reports +{ + my ($ctx, $groupname) = @_; + + my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $groupname); + unless(defined $nownext) { + print STDERR "$music\n"; + return 1; + } + print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n"; + return 0; +} + +sub set_reports +{ + my ($ctx, $groupname, $now_next, $traffic, $music) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, $now_next, $traffic, $music); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + + return 0; +} + + +my $num_args = $#ARGV + 1; +if($num_args < 1) { + print_usage(); + exit(1); +} +my $cmd = $ARGV[0]; +my $groupname = $ARGV[1]; +my $ret = 0; + +my ($ctx, $status, $errorstring) = RHRD::rddb::init(); +if(defined $ctx) { + if($cmd eq "list") { + if($num_args != 1) { + print_usage(); + $ret = 1; + } else { + $ret = list($ctx); + } + } + elsif($cmd eq "check") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = check($ctx, $groupname); + } + } + elsif($cmd eq "add") { + if($num_args < 2 || $num_args > 3) { + print_usage(); + $ret = 1; + } else { + $ret = add($ctx, $groupname, $ARGV[2]); + } + } + elsif($cmd eq "remove") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = remove($ctx, $groupname); + } + } + elsif($cmd eq "get-members") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = get_members($ctx, $groupname); + } + } + elsif($cmd eq "add-member") { + if($num_args != 3) { + print_usage(); + $ret = 1; + } else { + $ret = add_member($ctx, $groupname, $ARGV[2]); + } + } + elsif($cmd eq "remove-member") { + if($num_args != 3) { + print_usage(); + $ret = 1; + } else { + $ret = remove_member($ctx, $groupname, $ARGV[2]); + } + } + elsif($cmd eq "is-member") { + if($num_args != 3) { + print_usage(); + $ret = 1; + } else { + $ret = is_member($ctx, $groupname, $ARGV[2]); + } + } + elsif($cmd eq "get-carts") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = get_carts($ctx, $groupname); + } + } + elsif($cmd eq "set-carts") { + if($num_args < 2 || $num_args > 6) { + print_usage(); + $ret = 1; + } else { + $ret = set_carts($ctx, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]); + } + } + elsif($cmd eq "get-reports") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = get_reports($ctx, $groupname); + } + } + elsif($cmd eq "set-reports") { + if($num_args < 2 || $num_args > 5) { + print_usage(); + $ret = 1; + } else { + $ret = set_reports($ctx, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]); + } + } + else { + print_usage(); + $ret = 1; + } + + RHRD::rddb::destroy($ctx); +} else { + print STDERR "$errorstring\n"; + $ret = 1; +} + +exit $ret; |