#!/usr/bin/perl
#
# Fritz Box PhoneMonitor using dbus-Notification
# - debian Package libnotification-bin
# - fritzBox Monitor has to be enabled (#96*5*)
# - Perl ( :-) )
# - unknown TelNumbers will be added to the local AdBook
#
# Tested with Ubuntu 6.10 (Gnome), FritzBox 7170
#
# The code may be not really pretty, but running.
#
# Ronny Becker, 10.2006

use IO::Socket;

# Params
$FRITZBOX="fritz.box";
$sleep_count="10";           # Seconds to wait before starting operation
$new_message_count="30";     # if dns fails, after X sleep_counts print a new message
$notification_count="25000"; # how long notifications displayed (milliseconds)
$notification_icon="/usr/share/pixmaps/gnome-grecord.png";
# ABOOK File: csv file with
# <NAME1>;<NAME2>(...), with Number in only NumberFormat in each line
# (no spaces, only Numbers !), the first two colums will be printed out
# (made with evolution-adressbook-export --format=csv)
$LOCAL_ABOOK="/home/ronnybecker/bin/telmon/abook.csv";


#######################################################################
# check notify-send
system("which notify-send >/dev/null");
if ( $? != 0 ) {
 print "notify-send nicht gefunden; Programm bricht ab.";
 exit;
}

# Check fritz.box
# if dns fails, wait and try again
$check_fritzbox=0;
$check_errmessage=0;
while ( $check_fritzbox == 0 ) {
 sleep $sleep_count;
 @digdata=`dig $FRITZBOX`;
 foreach $digout (@digdata) {
  if ( $digout =~ /NOERROR/ ) {
   $check_fritzbox=1;
  }
  if ( $digout =~ /NXDOMAIN/ ) {
   if ($check_errmessage == $new_message_count ) {
    $check_errmessage=0;
   }
   $check_errmessage++;
   if ( $check_errmessage == 1 ) {
    `notify-send -t $notification_count -u critical -i "$notification_icon" "Fehler in telmon.pl" "Host <b>$FRITZBOX</b> konnte\n nicht aufgeloest werden."`;
   }
  }
 }
}

# RUN
# Create Socket
my $sock = new IO::Socket::INET (
        PeerAddr => $FRITZBOX,
        PeerPort => '1012',
        Proto => 'tcp'
        );
        die "Could not create socket: $!\n" unless $sock;

# parse
while(<$sock>) 
 {
 if ($_ =~ /RING/){
  my @C = split(/;/);
  $nr="$C[3]";
  # local, then internet
  if ( $data=abook_lookup($nr) ) {
   `notify-send -t $notification_count -u normal -i "$notification_icon" "Anruf von: $nr" "$data"`;
  } else {
   if ( $data=reverse_lookup($nr) ) {
    `notify-send -t $notification_count -u normal -i "$notification_icon" "Anruf von: $nr" "$data"`;
   }
  }
  if ( ! $data ) {
   `notify-send -t $notification_count -u normal -i "$notification_icon" "Anruf von: $nr" "Anrufer absolut unbekannt - sorry."`;
  }
 }
}

# SUBs
# reverse_lookup sub
sub reverse_lookup {
 my $number = shift;
 system("wget -q --tries=3 --timeout=5 -O /tmp/reverse_lookup \"http://www1.dasoertliche.de/?form_name=search_inv&ph=$number\"");
 my $nameadr = `grep -e 'class=\"entry\".*' -e ';.*<br\/>' /tmp/reverse_lookup`;
 if (index($nameadr, "<a href") != -1) {
  #Name
  $nameadr =~ s/^.*\"entry\">//g;
  $nameadr =~ s/<\/a>.*/, /g;
  #Adresse
  $nameadr =~ s/\n//g;
  $nameadr =~ s/\&nbsp\;/\ /g;
  $nameadr =~ s/\t//g;
  $nameadr =~ s/\<br.*//g;
  # if abook, insert the new entry
  if ( -e "$LOCAL_ABOOK" ) {
   `echo "\"$nameadr\",\"\",\"$number\"" >> $LOCAL_ABOOK`;
  }
  return("$nameadr");
 }
}

sub abook_lookup {
 if ( $LOCAL_ABOOK && -e "$LOCAL_ABOOK" ) {
  my $number = shift;
  if ( -e $LOCAL_ABOOK ) {
   # +49 ? no problem
   $number =~ s/^0//;
   @abook_data=`grep $number $LOCAL_ABOOK | cut -d "," -f1,2 | sed 's/,"/, /' | sed 's/"//g'`;
   if ( $abook_data[0] ) {
    return("$abook_data[0]");
   } else {
    return("");
   }
  }
 }
}
