#! /usr/bin/perl -w 
# 
# mail2voddy 1.2-1
#
# Inspired to mail2sms of <jan.sorensen@aragost.com>
# Author: Emanuele Vecchio <emanu@linux.it>
#
# Questo script invia un SMS tramite VodaSMS, usando i parametri
# passati attraverso una mail, che dovra` contenere i campi:
#	To: <username>@sms.tuodominio.com
#	Subject: <numero_cellulare>:<testo_del_messaggio>
#
# Questo script e` inutile se non usato con procmail, per esempio
# con una configurazione del tipo:
# 	:0:
# 	* To.*@sms.tuodominio.com
# 	| mail2voddy  
#
# oppure, per inoltrare qualsiasi messaggio dal presidente operaio
# 	:0c:
# 	* From.*berlusconi@gov.it
# 	| mail2sms <your number>
#
# Check man pages for procmail, procmailrc, and procmailex
# for explanation of the above procmail code.
#
# 
# NOTA TECNICA:
# L'autenticazione avviene controllando il destinatario della mail
# in arrivo, ed usando le sue credenziali per accedere al sito di
# 190.it.
# Per esempio, se la mail e` inviata a emanu@sms.tuodominio.com
# verranno usate le credenziali dell'utente `emanu`.
# So che non è molto affidabile, ma e` almeno un inizio.
#
# Si potrebbe usare il pacchetto debian `libmail-box-perl` che dà
# il modulo Mail::Internet, ma proviamo a farne senza, visto che
# sarebbe un macello con i client email che mandano la posta in
# in HTML per default.

use strict;

my $to = shift;
my $user = shift;
my $subject = "ERROR IN $0: NO SUBJECT FOUND";
my $hostname = "gnube.homeip.net"
my $test = 0;

while(<>) {
    if (m/^To: *(.*)\@sms\.$hostname/) { $user = $1 unless $user; };
    if (m/^Subject: *([0-9a-zA-Z]*):(.*)/) {
	$to = $1;
	$subject = $2;
    }
    if (m/^Testing/) { $test = 1; } # useful when testing with stdin on command line
}

$to || die "$0: Receiver for sms could not be extracted" ;
my $cmd = "echo \"$subject\" | vodasms -c /home/$user/.vodasmsrc -n $to";

if ($test) {
    print "cmd: $cmd";
} else {
    system $cmd;
}
