]> ruin.nu Git - NDIRC.git/blob - Commands/Misc.pm
Converted Misc
[NDIRC.git] / Commands / Misc.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 use strict;
21 use warnings;
22
23 use MooseX::Declare;
24 use NDIRC::Dispatcher;
25
26 command nosmoke => {
27         help => q(.nosmoke nick | shows how long the person has been smoke free),
28 }, class extends NDIRC::Command {
29         method execute($c,$msg) {
30                 my ($nick) = $msg =~ /^(\w+)?/;
31                 my $dbh = $c->model;
32
33                 my $time = $dbh->selectrow_hashref(q{
34 SELECT date_part('day',time) AS days, date_part('hour',time) AS hours, date_part('minute',time) AS mins
35 FROM (SELECT now() - time AS time FROM last_smokes WHERE nick = $1) l
36                         },undef,$nick);
37                 if ($time){
38                         my $weeks = int($time->{days} / 7);
39                         if ($weeks){
40                                 $weeks = "$weeks weeks";
41                         }else{
42                                 $weeks = "";
43                         }
44                         my $days = $time->{days} % 7;
45                         $c->reply("$nick has been smoke free for: $weeks $days days $time->{hours} hours and $time->{mins} minutes");
46                 }else{
47                         my $nonsmokers = $dbh->prepare(q{SELECT nick FROM last_smokes ORDER BY nick});
48                         $nonsmokers->execute;
49                         my @nonsmokers;
50                         while (my $nonsmoker = $nonsmokers->fetchrow_hashref){
51                                 push @nonsmokers,$nonsmoker->{nick};
52                         }
53                         $c->reply("I keep track of the following ppl: @nonsmokers");
54                 }
55         }
56 };
57
58 1;
59