Listing: callerinf.pl

callerinf.pl



#!/usr/bin/perl -w 
##
## This program is free software; you can redistribute it
## and/or modify it under the terms of the GNU General
## Public License as published by the Free Software Foundation
## (http://www.gnu.org/); either version 2 of the License, or
## (at your option) any later version.
##
## Author : Dirk Steinchen (dirk@atelier89.de)
## Webpage: http://www.atelier89.de/users/dirk/callerid.html
## Version: 1.0

use strict;

## edit this to your needs
    ## path and arguments to invoke the mailer
    my $mailer = '/usr/sbin/sendmail -em -i -t';
    ## Text for callers without entry in /etc/callerid.conf
    my $unknowncaller = 'unknown';
    ## ignore this amount of HANGUPs
    my $ignohup = 1;

## edit lines below this line shouldn't be needed
##
## $3 of isdnlog; exit if no caller-id is
## transmitted to prevent "useless" mails
my $callern = $ARGV[3];
exit if $callern eq '?';

## $19 of isdnlog; replace "?" width a human-readable alias
my $caller = $ARGV[1];
if ($caller eq '?') {$caller = $unknowncaller;}

## $2 of isdnlog
my $numbern = $ARGV[4];

## argument 1 in script-call in /etc/callerid.conf
## =user, who get's the mail
my $reciep = $ARGV[0];

## $20 of isdnlog
my $number = $ARGV[2];

## $1 of isdnlog=flag-status
my $status = $ARGV[5];

## flag-def's
my $hangup = 'IH';
my $ring = 'IR';
my $connect = 'IC';

## build name of status-file
my $statusfile = '/tmp/'.$callern.$numbern;

## data
my $hangups = 0;

## on hangup...
if ($status eq $hangup)
{
    ## tracked connection?
    if (-e $statusfile)
    {
	open (FILE,"< $statusfile");
            $hangups = <FILE>;
        close (FILE);
	$hangups++;
	if ($hangups > $ignohup)
	{
	    unlink ($statusfile);
	    ## get the time and date for mail-header and -body
	    use POSIX qw(strftime);
	    my $time1 = strftime "%a, %d. %b %Y %H:%M:%S %Z", localtime;
	    my $time2  = strftime "%d.%m.%Y %H:%M:%S %Z", localtime; 
	    ## ... and send info via mail
	    open (MAIL, "|$mailer -em -i -t") || die $!;
    	        print MAIL "From: $reciep\n";
		print MAIL "To: $reciep\n";
    		print MAIL "Date: $time1\n";
		print MAIL "Subject: Anruf von $callern\n";
    		print MAIL "Content-Type: text/plain; charset=iso-8859-1\n";
		print MAIL "Content-Transfer-Encoding: 8bit\n\n";
		print MAIL "$caller \($callern\) hat vergeblich versucht,\n";
		print MAIL "$number \($numbern\) anzurufen\n";
		print MAIL "- $time2 -\n\n";
		print MAIL "\n";
	    close (MAIL);
	}
	else
	{
	    open (FILE,"> $statusfile");
	     print FILE $hangups;
	    close (FILE);
	}
    }
}

## on ring...
if ($status eq $ring)
{
    ## write something to this file, so that it exists ;-)
    open (FILE,"> $statusfile");
     print FILE $hangups;
    close (FILE);
}

## on connect...
if ($status eq $connect)
{
    ## delete the status-file, we don't need any info cause
    ## we talked to the caller or vbox is doing the job ;-)
    unlink ($statusfile);
}
exit;