#**************************************************************************
#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
#                                                                         *
#   This program is free software; you can redistribute it and/or modify  *
#   it under the terms of the GNU General Public License as published by  *
#   the Free Software Foundation; either version 2 of the License, or     *
#   (at your option) any later version.                                   *
#                                                                         *
#   This program is distributed in the hope that it will be useful,       *
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#   GNU General Public License for more details.                          *
#                                                                         *
#   You should have received a copy of the GNU General Public License     *
#   along with this program; if not, write to the                         *
#   Free Software Foundation, Inc.,                                       *
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
#**************************************************************************/

package NDIRC::Commands::SMS;

use strict;
use warnings;
use feature ':5.10';

use Moose;
use MooseX::MethodAttributes;


sub sendsms
	: 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)
	: ACL(irc_sendsms)
	: Type(member)
{
	my ($self,$c,$msg) = @_;
	my ($number,$message) = $msg =~ /^(\S+) (.+)$/ or die 'ARGS';
	my $dbh = $c->model;

	if (length $message > 140){
		$c->reply("Message is too long");
		return;
	}
	if ($number =~ /^\+([1-9]\d+)$/){
		$number = $1;
	}else{
		my ($sms) = $dbh->selectrow_array(q{
SELECT sms FROM users WHERE username ilike $1
	AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
			},undef,$number);
		$sms //= 'No number, or invalid user';
		if ($sms =~ /^\+([1-9]\d+)$/){
			$number = $1;
		}else{
			$c->reply("User $number has number '$sms' which is not a valid international number");
			return;
		}
	}
	my $sms = $dbh->prepare(q{
INSERT INTO sms (uid,number,message)
VALUES((SELECT uid FROM users WHERE hostmask ilike $1),$2,$3)
RETURNING id
		});
	$sms->execute($c->host,$number,$message);
	my ($id) = $sms->fetchrow_array;
	$c->reply("Message added to queue, you can see the status with: .smsstatus $id");
}

sub smsstatus
	: Help(.smsstatus smsid | Gives information about a given sms)
	: ACL(irc_smsstatus)
	: Type(member)
{
	my ($self,$c,$msg) = @_;
	my ($id) = $msg =~ /(\d+)/ or die 'ARGS';
	my $dbh = $c->model;

	my $sms = $dbh->selectrow_hashref(q{
SELECT s.id, u.username, s.number, s.status, s.cost
	,s.time AT TIME ZONE 'GMT' AS time
FROM sms s
	JOIN users u USING (uid)
WHERE id = $1
		}, undef, $id);

	if($sms->{id}){
		$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}");
	}else{
		$c->reply("Could not find any sms with id: $id");
	}
}

1;
