#!/usr/bin/perl
use warnings;
use strict;
use Net::POP3;
use POSIX;
POSIX::setsid or die "setsid: $!"; # disociar de la terminal
my $pid = fork (); # ¡fork!
if ($pid < 0) { # si ha fallado el fork...
die "fork: $!"; # nos morimos... algo raro pasó
} elsif ($pid) { # pero, si somos el padre
exit 0; # nos morimos...
} # sigue el hijo
chdir "/"; # cambio de directorio de trabajo
umask 0; # máscara de creación de archivos a 0
foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024)) # cerramos todos los gestores
{ POSIX::close $_ } # que pudieran estar abiertos
open (STDIN, "</dev/null"); # cerramos la entrada estándar
open (STDOUT, ">/dev/null"); # y la salida estándar
open (STDERR, ">&STDOUT" ); # y la salida estándar de errores
#Variables Generals
my $ServerName = "mail";
my $UserNameG = "user";
my $UserNameA = "user1";
my $Password = "Clave";
#my @CelCD = ('996135835','985573207','996308079','984922695','944814800','999220055');
my @CelCD = ('942445991');
# Call to process
while('TRUE')
{
#print " Waiting for email...\n";
SMSGeneral;
}
# Process Gatheway General
sub SMSGeneral
{
#print "Waiting for email...\n";
# Connect to pop3 server
my $pop3 = Net::POP3->new($ServerName) || die "Error : Couldn't log on to server";
# Login to pop3 server
my $Num_Message = $pop3->login($UserNameG,$Password);
my $Messages = $pop3->list();
# Variables Locals
my ($MsgId, $MsgDate, $MsgFrom, $MsgTo, $MsgCc, $MsgSub);
my ($MsgAttach, $MsgSize, $MsgHeader, $MsgHeadFlg, $MsgBody,$MsgNo);
# proces each message
foreach $MsgNo (keys %$Messages)
{
my $MsgContent = $pop3->get($MsgNo);
my $count = 0;
$MsgHeadFlg = 0;
$MsgBody = "";
$MsgSize = $pop3->list($MsgNo);
# Process message data
while()
{
# Exit if last line of mail
if ($count >= scalar(@$MsgContent))
{
$MsgBody =~s/^\s*(.*?)\s*$/$1/;
$MsgBody = "SMS de-$MsgFrom:" . $MsgBody;
# check if Subj is Numeric
if ($MsgSub =~ /^[+]?\d+$/ )
{
# You can only send SMS in chunks of 160 chars Max according to Gammu.
# so breaking the body into chunks of 160 and sending them 1 at a time.
my @stringChunksArray = ($MsgBody =~ m/(.{1,160})/gs);
for(my $i=0;$i<@stringChunksArray;$i++)
{
open(Gammu, "| gnokii --sendsms $MsgSub") || die "Error starting gnokii failed: $!\n";
# Start gnokii and wait for the SMS body
print Gammu $stringChunksArray[$i];
# Print the SMS body in 160 Char chunks
close(Gammu);
# This is there so that the phone gets time to reset after each message. Otherwise the send fails
sleep(10);
}#end for sms chunks
}
else
{
#print "no es un Cel";
# if not is number exit while
last;
}
# before send sms exit
last;
}
# Check if end of mail header
if (@$MsgContent[$count] =~ /^\n/)
{
$MsgHeadFlg = 1;
}
# Proceed if message header not processed
if (not $MsgHeadFlg)
{
# Split the line
my @LineContent = split /: /, @$MsgContent[$count];
# Check Header Info
SWITCH:
{
# Get message date
$LineContent[0] =~ /Date/i && do
{
$MsgDate = $LineContent[1];
# print "Date : $MsgDate";
last SWITCH;
};
# Get message id
$LineContent[0] =~ /Message-ID/i && do
{
$MsgId = $LineContent[1];
# print "Message ID : $MsgId";
last SWITCH;
};
# Get message from
$LineContent[0] =~ /From/i && do
{
$MsgFrom = $LineContent[1];
# $MsgFrom = $1;
$MsgFrom =~ s/"|<.*>//g;
$MsgFrom = substr($MsgFrom, 0, 39);
# print "From : $MsgFrom";
last SWITCH;
};
# Get message to
$LineContent[0] =~ /To/i && do
{
$MsgTo = $LineContent[1]; #print "To : $MsgTo";
last SWITCH;
};
# Get message cc
$LineContent[0] =~ /Cc/i && do
{
$MsgCc = $LineContent[1]; # print "Cc : $MsgCc";
last SWITCH;
};
# Get message subject
$LineContent[0] =~ /Subject/i && do
{
$MsgSub = $LineContent[1];# print "Subject : $MsgSub";
last SWITCH;
};
}
}
else
{
# Process message body
$MsgBody .= @$MsgContent[$count];
}
$count++;
} #end while, end proces message
$pop3->delete ($MsgNo); # despues de procesarlo: eliminarlo
} #end foreach, end process all message of server
# Disconnect from pop3 server
$pop3->quit();
sleep(20); # Sleep for 20 seconds
}# fin de rutina SMS general