#!/usr/local/bin/perl use Device::SerialPort; # This script looks for sms messages. These are stored in a sqlite db/file on the iphone. # Get the messages that arrived in the last minute. # Check the date. if it's older than 10 minutes, set old bit. (We'll use this to decide on execution) # Check the sender - if it's from a proper address, set authorized bit. # Parse the message for commands/auth # command structure: "command passwd" # # Interesting notes: at 9600, the iphone serial port sucks or something. At 19200 behaviour got better. # At 115200, everything works fine. This'll burn some resources on the arduino, but it's not the end of life. # $debug = 0; #List of authorized SMS command sources. @senders = ( "5731234567", "+15731234567", "15731234567" ); $password = "changeme"; #seconds to sleep between checks $sleep = 30; # Send confirmation SMS? $send_conf = 1; # path to sendsms. $sendsms = "/usr/bin/sendsms"; # path to log file. $logfile = "/var/log/SMSresponder"; $logmessage = ""; #define this for later. # Set up the Serial port my $Port = Device::SerialPort->new("/dev/tty.iap"); $Port->baudrate(115200); $Port->databits(8); $Port->parity(none); $Port->stopbits(1); $Port->buffers(4096, 4096); $Port->handshake("none"); $Port->write_settings; #log startup! $logmessage = "Started listening for SMS commands."; &logger; # Global message content for verificaiton SMS. $sms_reply = ""; # The sms database is located at /private/var/mobile/Library/SMS in ios 4.2.1. # The table is message. use DBI; my $dbh = DBI->connect("dbi:SQLite:dbname=/private/var/mobile/Library/SMS/sms.db","",""); # Get the time in secs since epoch. subtract 60 to find 1 minute ago. my $time = time; my $last = $time - 1360; if ($debug) { print "time:$time, last: $last \n"; } # Select address,date,rowid,text from messages from the last check. my $auth_pass = 0; my $auth_send = 0; while(1) { if ($debug) { print "getting messages since $last\n"; } my $sth = $dbh->prepare("Select address,date,rowid,text from message where date >= $last"); $sth->execute(); $last = time; #update query time for next round. while (my @result = $sth->fetchrow_array()) { $auth_pass = 0; $auth_send = 0; if($debug){ print "address: $result[0], date: $result[1], rowid: $result[2], text: $result[3] \n"; } ($command, $passwd) = split(/ /, $result[3]); if($debug){ print "sender: $result[0] command: $command, passwd:$passwd. \n"; } if( $passwd =~ $password ){ if($debug) { print "Authorized password \n" }; $auth_pass = 1; } foreach $sender (@senders){ if ( $sender eq $result[0] ) { if($debug){ print "Authorized sender $sender \n"; } $auth_send = 1; #set address for reply sms. $sms_address = $result[0]; } } if($auth_pass && $auth_send){ #apparently, switch.pm is missing or something. Sorry for the lame if then crap. if($debug) {print "Authorized password and Sender!\n";} if ( $command eq "Start" ){ print "Executing Start\n"; &send_start; $sms_reply = "Executed Start."; &send_conf_sms; $logmessage = "Exec Start request from $sms_address"; &logger; } else { print "No valid command!\n"; } } } $sth->finish; if($debug){ print "Sleeping $sleep seconds \n";} sleep $sleep; } #end while # sub send_start{ # Send start command via serial. $Port->write("b"); } sub send_conf_sms { if($send_conf){ # $sms_reply = ""; if ($debug) {print "Sending conf SMS to $sender";} my $command = $sendsms . " " . $sms_address . " " . $sms_reply; $output = `$command`; $logmessage = "sent SMS to $sms_address: $sms_reply"; &logger; } } sub logger { #write log message to file # file is $logfile #add datestamp: open (LOG, ">>$logfile"); my $datastring = `date`; chomp($datastring); $logmessage = $datastring . ": " . $logmessage . "\n"; print (LOG $logmessage); close (LOG); }