]> ruin.nu Git - NDIRC.git/blob - Commands/SMS.pm
Introduce a uid member for the context and use it intead of the host in commands
[NDIRC.git] / Commands / SMS.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 package NDIRC::Commands::SMS;
21
22 use strict;
23 use warnings;
24 use feature ':5.10';
25
26 use Moose;
27 use MooseX::MethodAttributes;
28
29
30 sub sendsms
31         : Help(.sendsms <username or number> message | number has to be an international number, like +44234234562 for a uk number or +1234528234 for a north american number)
32         : ACL(irc_sendsms)
33         : Type(member)
34 {
35         my ($self,$c,$msg) = @_;
36         my ($number,$message) = $msg =~ /^(\S+) (.+)$/ or die 'ARGS';
37         my $dbh = $c->model;
38
39         if (length $message > 140){
40                 $c->reply("Message is too long");
41                 return;
42         }
43         if ($number =~ /^\+([1-9]\d+)$/){
44                 $number = $1;
45         }else{
46                 my ($sms) = $dbh->selectrow_array(q{
47 SELECT sms FROM users WHERE username ilike $1
48         AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
49                         },undef,$number);
50                 $sms //= 'No number, or invalid user';
51                 if ($sms =~ /^\+([1-9]\d+)$/){
52                         $number = $1;
53                 }else{
54                         $c->reply("User $number has number '$sms' which is not a valid international number");
55                         return;
56                 }
57         }
58         my $sms = $dbh->prepare(q{
59 INSERT INTO sms (uid,number,message)
60 VALUES($1,$2,$3)
61 RETURNING id
62                 });
63         $sms->execute($c->uid,$number,$message);
64         my ($id) = $sms->fetchrow_array;
65         $c->reply("Message added to queue, you can see the status with: .smsstatus $id");
66 }
67
68 sub smsstatus
69         : Help(.smsstatus smsid | Gives information about a given sms)
70         : ACL(irc_smsstatus)
71         : Type(member)
72 {
73         my ($self,$c,$msg) = @_;
74         my ($id) = $msg =~ /(\d+)/ or die 'ARGS';
75         my $dbh = $c->model;
76
77         my $sms = $dbh->selectrow_hashref(q{
78 SELECT s.id, u.username, s.number, s.status, s.cost
79         ,s.time AT TIME ZONE 'GMT' AS time
80 FROM sms s
81         JOIN users u USING (uid)
82 WHERE id = $1
83                 }, undef, $id);
84
85         if($sms->{id}){
86                 $c->reply("SMS <b>$sms->{id}</b> was sent by <b>$sms->{username}</b> to <b>$sms->{number}</b> cost: <b>$sms->{cost}</b>. Last status at <b>$sms->{time}</b>: $sms->{status}");
87         }else{
88                 $c->reply("Could not find any sms with id: $id");
89         }
90 }
91
92 1;