]> ruin.nu Git - NDIRC.git/blob - Commands/SMS.pm
452e31754d21e472fc182d93d9c7a957f99537c9
[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 use strict;
21 use warnings;
22 use feature ':5.10';
23
24 use MooseX::Declare;
25 use NDIRC::Dispatcher;
26
27 command sendsms => {
28         help => q(.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),
29         acl => q(irc_sendsms),
30         type => q(member),
31 }, class extends NDIRC::Command {
32         method execute($c,$msg) {
33                 my ($number,$message) = $msg =~ /^(\S+) (.+)$/ or die 'ARGS';
34                 my $dbh = $c->model;
35
36                 if (length $message > 140){
37                         $c->reply("Message is too long");
38                         return;
39                 }
40                 if ($number =~ /^\+([1-9]\d+)$/){
41                         $number = $1;
42                 }else{
43                         my ($sms) = $dbh->selectrow_array(q{
44 SELECT sms FROM users WHERE username ilike $1
45         AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
46                                 },undef,$number);
47                         $sms //= 'No number, or invalid user';
48                         if ($sms =~ /^\+([1-9]\d+)$/){
49                                 $number = $1;
50                         }else{
51                                 $c->reply("User $number has number '$sms' which is not a valid international number");
52                                 return;
53                         }
54                 }
55                 my $sms = $dbh->prepare(q{
56 INSERT INTO sms (uid,number,message)
57 VALUES($1,$2,$3)
58 RETURNING id
59                         });
60                 $sms->execute($c->uid,$number,$message);
61                 my ($id) = $sms->fetchrow_array;
62                 $c->reply("Message added to queue, you can see the status with: .smsstatus $id");
63         }
64 };
65
66 command smsstatus => {
67         help => q(.smsstatus smsid | Gives information about a given sms),
68         acl => q(irc_smsstatus),
69         type => q(member),
70 }, class extends NDIRC::Command {
71         method execute($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
91 1;