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