]> ruin.nu Git - NDIRC.git/blobdiff - Commands/SMS.pm
Added .sendmail command
[NDIRC.git] / Commands / SMS.pm
index 1b28dc71f094b157c1879ad78ab084317b8fa73a..55405d03959001931f084c3d8e192580d4cbf43d 100644 (file)
 #   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;
-
+use MooseX::Declare;
+use NDIRC::Dispatcher;
 
-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;
+command sendsms => {
+       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),
+       acl => q(irc_sendsms),
+       type => q(member),
+}, class extends NDIRC::Command {
+       method execute($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}
-                       ,undef,$number);
-               $sms //= 'No number, or invalid user';
-               if ($sms =~ /^\+([1-9]\d+)$/){
+               if (length $message > 140){
+                       $c->reply("Message is too long");
+                       return;
+               }
+               if ($number =~ /^\+([1-9]\d+)$/){
                        $number = $1;
                }else{
-                       $c->reply("User $number has number '$sms' which is not a valid international number");
-                       return;
+                       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{
+               my $sms = $dbh->prepare(q{
 INSERT INTO sms (uid,number,message)
-VALUES((SELECT uid FROM users WHERE hostmask ilike $1),$2,$3)
+VALUES($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");
-}
+                       });
+               $sms->execute($c->uid,$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;
+command smsstatus => {
+       help => q(.smsstatus smsid | Gives information about a given sms),
+       acl => q(irc_smsstatus),
+       type => q(member),
+}, class extends NDIRC::Command {
+       method execute($c,$msg) {
+               my ($id) = $msg =~ /(\d+)/ or die 'ARGS';
+               my $dbh = $c->model;
 
-       my $sms = $dbh->selectrow_hashref(q{
+               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);
+                       }, 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");
+               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");
+               }
+       }
+};
+
+command sendmail => {
+       help => q(.sendmail <username or email addy> message),
+       acl => q(irc_sendmail),
+       type => q(member),
+}, class extends NDIRC::Command {
+       use Mail::Sendmail;
+       use Encode;
+       method execute($c,$msg) {
+               my ($user,$message) = $msg =~ /^(\S+) (.+)$/ or die 'ARGS';
+               my $dbh = $c->model;
+               my $mail;
+
+               if ($user =~ /@/){
+                       $mail = $user
+               }else{
+                       $mail = $dbh->selectrow_array(q{
+SELECT email FROM users WHERE username ilike $1
+       AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
+                               },undef,$user);
+                       $mail //= 'No mail, or invalid user';
+                       if ($mail =~ /^\S+@\S+$/){
+                       }else{
+                               $c->reply("User $user has email addy '$mail' which looks odd");
+                               return;
+                       }
+               }
+               my %mail = (
+                       smtp => 'ruin.nu:587',
+                       To      => $mail,
+                       From    => 'NewDawn Command <nd@ruin.nu>',
+                       'Content-type' => 'text/plain; charset="UTF-8"',
+                       Subject => "ND: " . (substr $message, 0, 60),
+                       Message => encode("UTF-8",$message),
+               );
+
+               if (sendmail %mail) {
+                       $c->reply("Mail sent to $user");
+               }else {
+                       $c->reply("Could not send mail to $user: " . $Mail::Sendmail::error);
+               }
        }
-}
+};
 
 1;