phone-moniter
authorTim Pope <code@tpope.net>
Fri, 23 Jul 2004 01:14:27 +0000 (01:14 +0000)
committerTim Pope <code@tpope.net>
Fri, 23 Jul 2004 01:14:27 +0000 (01:14 +0000)
perl/phone-monitor [new file with mode: 0755]

diff --git a/perl/phone-monitor b/perl/phone-monitor
new file mode 100755 (executable)
index 0000000..cb8d6eb
--- /dev/null
@@ -0,0 +1,90 @@
+#!/usr/bin/perl
+# Author: Tim Pope
+
+# Watches my modem on ttyS4 for new calls and reports the caller id.
+# Requires a $HOME/bin/phone-call to handle them.
+
+use strict;
+use Device::Modem;
+my $number;
+#my %status;
+my %values;
+
+my $modem = new Device::Modem( port => '/dev/ttyS4', baudrate => '38400', log => 'file,/dev/null');
+
+$SIG{INT}  = 'death';
+$SIG{TERM} = 'death';
+$SIG{QUIT} = 'death';
+
+sub initialize {
+    my $modem=shift;
+    $modem->connect() || return 0;
+    $modem->atsend( 'AT#CID=1' . Device::Modem::CR );
+    $modem->answer();
+    return 1;
+}
+
+daemonize() if (shift eq "-d");
+
+initialize($modem) || die "Could not initialize modem: $!";
+
+while(1) {
+       sleep 1;
+       %values = wait_for_ring();
+       #%values=("NMBR" => "2102130787", "NAME" => "WIRELESS CALLER");
+       if($values{"NMBR"}) {
+           print $values{"NMBR"}. " " .$values{"NAME"}, "\n";
+           ring($values{"NMBR"}.' "'.$values{"NAME"}.'"');
+       } elsif (!%values) {last;}
+}
+$modem->disconnect();
+
+sub wait_for_ring {
+    local $_;
+    #eval {
+       #local $SIG{ALRM} = sub { die "alarm\n" };
+       #alarm 15;
+       $_ = $modem->answer("NMBR = [0-9A-Z]*[^0-9A-Z]");
+       #alarm 0
+       #};
+    #if($@) {
+       #die unless $@ eq "alarm\n";
+       #return undef;
+    #} else {
+       #my $CR = Device::Modem::CR;
+       my %values = ("success" => 1);
+       #print ("$_\n"); # if ($_);
+       foreach (split /\r\n/) {
+           if(m/^(.*) = (.*)$/) {
+               $values{$1} = $2;
+           }
+       }
+       return %values;
+    #}
+}
+
+sub ring {
+    system($ENV{"HOME"} . "/bin/phone-call " . $_[0]);
+}
+
+sub daemonize {
+    chdir "/";
+    my $pid=fork();
+    if($pid) {
+       exit(0)
+    }
+    elsif(defined($pid))
+    {
+       close STDIN;
+       close STDOUT;
+       open(STDOUT,">/tmp/.phone.log") or die "$!";
+       close STDERR;
+    }
+    else { exit(1); }
+}
+
+sub death {
+    $modem->disconnect() if($modem);
+    unlink "/tmp/.phone.log";
+    exit(0);
+}