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