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