]> ruin.nu Git - NDIRC.git/blob - Commands/Channel.pm
62eae4f9bebd59e6eb353ad7bff927217cfcabdd
[NDIRC.git] / Commands / Channel.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 use strict;
21 use warnings;
22 use feature ':5.10';
23
24 use MooseX::Declare;
25 use NDIRC::Dispatcher;
26
27 my $access = class extends NDIRC::Command {
28         method execute ($c,$msg) {
29                 $self->name =~ /(de)?(op|voice)/;
30                 my @access = ($2);
31                 my $mod = $1 ? '-' : '';
32
33                 if ($msg =~ /^\s*$/){
34                         $msg = $c->nick;
35                         push @access, "auto_$access[0]";
36                 }
37
38                 my $mode = q{
39 SELECT DISTINCT LOWER(flag) FROM users u
40         JOIN groupmembers g USING (uid)
41         JOIN channel_group_flags gf USING (gid)
42         JOIN channel_flags f USING (flag)
43 WHERE uid = $1 AND channel = $2 AND f.name = ANY($3);
44                 };
45                 if ($c->check_user_roles(qw/irc_masterop/)){
46                         $mode = substr $access[0], 0,1;
47                 }else{
48                         ($mode) = $c->model->selectrow_array($mode,undef,$c->uid,$c->channel,\@access);
49                 }
50                 if ($mode){
51                         $c->command(mode => $c->channel, "$mod$mode", $msg);
52                 }else{
53                         $c->reply("No access to " . $self->name . " in this channel");
54                 }
55         }
56 };
57
58 command op => {
59         help => q(syntax: .op [nicks] | Gives op to the specified nicks, or yourself if no command is given),
60         type => q(channel),
61 }, $access;
62
63 command deop => {
64         help => q(syntax: .deop [nicks] | Removes op from the specified nicks, or yourself if no command is given),
65         type => q(channel),
66 }, $access;
67
68 command voice => {
69         help => q(syntax: .voice [nicks] | Gives voice to the specified nicks, or yourself if no command is given),
70         type => q(channel),
71 }, $access;
72
73 command devoice => {
74         help => q(syntax: .voice [nicks] | Removes voice from the specified nicks, or yourself if no command is given),
75         type => q(channel),
76 }, $access;
77
78 command invite => {
79         help => q(Syntax: invite [channel] | If no channel is specified it invites you to all channel you have auto invite access on),
80         type => q(pm)
81 }, class extends NDIRC::Command {
82         method execute ($c,$msg) {
83                 my ($channel) = $msg =~ /^\s*(\S+)?\s*$/ or die 'ARGS';
84
85                 my @channels;
86                 if ($channel && $c->check_user_roles('irc_masterinvite')){
87                         push @channels,$channel;
88                 }else{
89                         my @access = ('i');
90                         push @access, 'I' if $channel;
91                         my $channels = $c->model->prepare(q{
92 SELECT DISTINCT channel FROM users u
93         JOIN groupmembers g USING (uid)
94         JOIN channel_group_flags gf USING (gid)
95 WHERE uid = $1 AND COALESCE(channel = $2,TRUE)
96         AND (flag = ANY($3) )
97                                 });
98                         $channels->execute($c->uid,$channel,\@access);
99                         while (my ($channel) = $channels->fetchrow()){
100                                 push @channels,$channel;
101                         }
102                 }
103                 for (@channels){
104                         $c->command(invite => $c->nick, $_);
105                 }
106         }
107 };
108
109 command hostname => {
110         help => q(Shows your hostname, as seen by the bots.)
111 }, class extends NDIRC::Command {
112         method execute ($c,$msg) {
113                 $c->reply('Your hostname is: '.$c->host);
114         }
115 };
116
117 command getpass => {
118         help => q(Gives new users a random password.),
119         type => q(pm)
120 }, class extends NDIRC::Command {
121
122 ###########################################################
123 # Written by Guy Malachi http://guymal.com
124 # 18 August, 2002
125 ###########################################################
126         sub generate_random_string
127         {
128                 my $length_of_randomstring=shift;# the length of
129                 # the random string to generate
130
131                 my @chars=('a'..'z','A'..'Z','0'..'9','_');
132                 my $random_string;
133                 foreach (1..$length_of_randomstring)
134                 {
135                         # rand @chars will generate a random
136                         # number between 0 and scalar @chars
137                         $random_string .= $chars[rand @chars];
138                 }
139                 return $random_string;
140         }
141
142         method execute ($c,$msg) {
143                 my $dbh = $c->model;
144
145                 my $password = generate_random_string 16;
146                 my $update = $dbh->do(q{
147 UPDATE users SET password = $1
148 WHERE uid = $2 AND password IS NULL
149                         },undef,$password,$c->uid);
150                 if ($update > 0){
151                         $c->reply("Password set to: $password (you can change it on webbie)");
152                 }else{
153                         $c->reply("Couldn't set password. Either it has already been set or you don't have an account");
154                 }
155         }
156 };
157
158 1;