]> ruin.nu Git - NDIRC.git/blob - Commands/Usermgm.pm
Converted the chattr command
[NDIRC.git] / Commands / Usermgm.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package NDIRC::Commands::Usermgm;
21
22 use strict;
23 use warnings;
24 use feature ':5.10';
25
26 use Moose;
27 use MooseX::MethodAttributes;
28
29 sub adduser
30         : Help(syntax: .+user username [pnick] | username must be alphanum characters, if no pnick is given then it will be set to the same as username)
31         : Alias(+user)
32         : ACL(irc_adduser)
33 {
34         my ($self,$c,$msg) = @_;
35         my ($nick,$pnick) = $msg =~ /^(\w+)(?: ([^.\s]+))?$/ or die 'ARGS';
36         $pnick //= $nick;
37
38         my $dbh = $c->model;
39
40         my $host = "$pnick.users.netgamers.org";
41         my ($username,$hostname,$p_nick) = $dbh->selectrow_array(q{
42 SELECT username, hostmask,pnick
43 FROM users WHERE username ILIKE ? OR hostmask ILIKE ? OR pnick ILIKE ?
44                 },undef,$nick,$host,$pnick);
45
46         if (defined $username){
47                 $c->reply("<b>$username ($p_nick)</b> already exists with host: <b>$hostname</b>");
48         }else{
49                 $dbh->do(q{
50 INSERT INTO users (username,hostmask,pnick,password) VALUES(?,?,?,'')
51                 },undef,$nick,$host,$pnick);
52                 $c->reply("Added <b>$nick(/$pnick)</b> with host: <b>$host</b>");
53         }
54 }
55
56 sub deactivateuser
57         : Help(syntax: .-user nick | nick must be alphanum characters, if no pnick is given then it will be set to nick)
58         : Alias(-user)
59         : ACL(irc_deactivateuser)
60 {
61         my ($self,$c,$msg) = @_;
62
63         my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
64
65         my $dbh = $c->model;
66         my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
67         $f->execute($nick);
68         my ($uid,$username) = $f->fetchrow();
69
70         if ($f->rows == 1){
71                 my $updated = $dbh->do(q{
72 UPDATE users SET hostmask = ?, password = '' WHERE uid = ?
73                 },undef,$username,$uid);
74                 if ($updated > 0){
75                         my $groups = $dbh->do(q{DELETE FROM groupmembers WHERE uid = ?},undef,$uid);
76                         $groups += 0;
77                         $c->reply("<b>$username</b> has been deactivated. Removed from $groups groups.");
78                 }else{
79                         $c->reply("Something went wrong when trying to modify <b>$username</b>");
80                 }
81         }elsif ($f->rows == 0){
82                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
83         }else{
84                 $c->reply("More than 1 user matched, please refine the search");
85         }
86         $f->finish;
87 }
88
89 sub chattr
90         : Help(syntax: .chattr username [-]flags | % can be used for wildcards \%arro% will match barrow, if a - is given then flags will be removed, otherwise added)
91         : ACL(irc_chattr)
92 {
93         my ($self,$c,$msg) = @_;
94
95         my ($nick, $flags) = $msg =~ /^(\S+) ((\+|-)?\w+)$/ or die 'ARGS';
96         my $dbh = $c->model;
97
98         my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
99         $f->execute($nick);
100         my $user = $f->fetchrow_hashref;
101         if ($f->rows == 1){
102                 my $update;
103                 if ($flags =~ /^(-)/){
104                         $update = $dbh->prepare(q{
105 DELETE FROM groupmembers WHERE uid = $1 AND
106         gid IN (SELECT gid FROM groups WHERE flag = ANY($2))
107                         });
108                 }else{
109                         $update = $dbh->prepare(q{
110 INSERT INTO groupmembers (uid,gid) (SELECT $1,gid FROM groups
111         WHERE flag = ANY($2) AND gid NOT IN (SELECT gid FROM groupmembers WHERE uid = $1))
112                         });
113                 }
114                 my @flags = split /\W*/,$flags;
115                 $update->execute($user->{uid},\@flags);
116                 $update = $dbh->prepare(q{
117 SELECT concat(flag)
118 FROM (SELECT uid,flag FROM groupmembers NATURAL JOIN groups ORDER BY uid,flag ) g
119 WHERE uid = ?
120                 });
121                 $flags = $dbh->selectrow_array($update,undef,$user->{uid});
122                 $c->reply("Flags for <b>$user->{username}</b> are now: $flags");
123         }elsif ($f->rows == 0){
124                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
125         }else{
126                 $c->reply("More than 1 user matched, please refine the search");
127         }
128         $f->finish;
129 }
130
131 1;