]> ruin.nu Git - NDIRC.git/blob - Commands/SMS.pm
Sms send and status 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{SELECT sms FROM users WHERE username ilike $1}
47                         ,undef,$number);
48                 $sms //= 'No number, or invalid user';
49                 if ($sms =~ /^\+([1-9]\d+)$/){
50                         $number = $1;
51                 }else{
52                         $c->reply("User $number has number '$sms' which is not a valid international number");
53                         return;
54                 }
55         }
56         my $sms = $dbh->prepare(q{
57 INSERT INTO sms (uid,number,message)
58 VALUES((SELECT uid FROM users WHERE hostmask ilike $1),$2,$3)
59 RETURNING id
60                 });
61         $sms->execute($c->host,$number,$message);
62         my ($id) = $sms->fetchrow_array;
63         $c->reply("Message added to queue, you can see the status with: .smsstatus $id");
64 }
65
66 sub smsstatus
67         : Help(.smsstatus smsid | Gives information about a given sms)
68         : ACL(irc_smsstatus)
69         : Type(member)
70 {
71         my ($self,$c,$msg) = @_;
72         my ($id) = $msg =~ /(\d+)/ or die 'ARGS';
73         my $dbh = $c->model;
74
75         my $sms = $dbh->selectrow_hashref(q{
76 SELECT s.id, u.username, s.number, s.status, s.cost
77         ,s.time AT TIME ZONE 'GMT' AS time
78 FROM sms s
79         JOIN users u USING (uid)
80 WHERE id = $1
81                 }, undef, $id);
82
83         if($sms->{id}){
84                 $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}");
85         }else{
86                 $c->reply("Could not find any sms with id: $id");
87         }
88 }
89
90 1;