#!/usr/bin/perl

# cidcall - caller ID report

# Created by John L. Chmielewski on Fri Sep 14, 2001
#
# Copyright (c) 2001-2015 by
#   John L. Chmielewski <jlc@users.sourceforge.net>
#   Todd Andrews <tandrews@users.sourceforge.net>
#   Aron Green

use strict;
use warnings;
use Pod::Usage;
use File::Basename;
use Getopt::Long qw(:config no_ignore_case_always);

my ($help, $man, $version);
my ($blk, $cid, $end, $hup, $msg, $not, $out, $pid, $wid);
my ($label, $date, $line, $name, $number);
my ($stime, $etime);
my $cidlog;
my $humanReadable = 1;
my $outputFormat = $humanReadable;

my $prog = basename($0);
my $VERSION = "(NCID) 1.3";

format STDOUT =
@<<< @<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<< @<<<<<<<
$label, $name,             $number,         $line,      $date,      $stime,  $etime
.

Getopt::Long::Configure ("bundling");
my ($result) = GetOptions(
    "help|h"     => \$help,
    "man|m"      => \$man,
    'version|V'  => \$version,
    "format|f=i" => \$outputFormat,
    "BLK|B"      => \$blk,
    "CID|C"      => \$cid,
    "END|E"      => \$end,
    "HUP|H"      => \$hup,
    "MSG|M"      => \$msg,
    "NOT|N"      => \$not,
    "OUT|O"      => \$out,
    "PID|P"      => \$pid,
    "WID|W"      => \$wid
 ) || pod2usage(2);
die "$prog $VERSION\n" if $version;
pod2usage(-verbose => 1, -exitval => 0) if $help;
pod2usage(-verbose => 2, -exitval => 0) if $man;

($cidlog = shift) || ($cidlog = "/var/log/cidcall.log");

if ( ($outputFormat < 0) || ($outputFormat > 1) ) { die "Format option must be in the range of 0-1.";}

open(CIDLOG, $cidlog) || die "Could not open $cidlog\n";

while (<CIDLOG>) {
  if (!$outputFormat) { print; }
  elsif (!$blk && !$cid && !$end && !$hup && !$msg && !$not && !$out && !$pid && !$wid) {
    if (/BLK:|CID:|HUP:|OUT:|PID:|WID:/) {&parseLine;}
  }
  else {
    if ($blk) { if (/BLK:/) {&parseLine;} }  
    if ($cid) { if (/CID:/) {&parseLine;} }
    if ($end) { if (/END:/) {&parseLine;} }
    if ($hup) { if (/HUP:/) {&parseLine;} }
    if ($msg) { if (/MSG:/) {print;} }
    if ($not) { if (/NOT:/) {print;} }
    if ($out) { if (/OUT:/) {&parseLine;} }
    if ($pid) { if (/PID:/) {&parseLine;} }
    if ($wid) { if (/WID:/) {&parseLine;} }
  }
}

sub parseLine {
    ($label, $date, $stime, $number, $name) = 
     /(\w+:).*\*DATE.(\d+).*\*TIME.(\d+).*\*NU*MBE*R.(\*?\??[-\w\s]+).*\*NAME.(.*)\*+$/;
    ($line) = /.*\*LINE.([-\w\d]+).*/;
    $line =~ s/-*//;
    $date =~ s/(\d\d)(\d\d)(\d\d\d\d)*/$1\/$2\/$3/;
    $date =~ s/\/$//;
    $stime =~ s/(\d\d)(\d\d)/$1:$2/;
    #$number =~ s/\d?(\d\d\d)(\d\d\d)(\d\d\d\d)/$1-$2-$3/;
    $etime = "";
    if (/END:/) {
        ($stime, $etime) =
         /.*SCALL.\d+\/\d+\/\d+ (\d\d:\d\d:\d\d).*ECALL.\d+\/\d+\/\d+ (\d\d:\d\d:\d\d).*$/;
    }
     write;
}

=head1 NAME

cidcall - view calls, hangups, messages, and end of calls in the NCID call file

=head1 SYNOPSIS

 cidcall [--help|-h] [--man|-m] [--version|-V]

 cidcall [--format |-f <0-1>]
         [--BLK    |-B]
         [--CID    |-C]
         [--END    |-E]
         [--HUP    |-H]
         [--MSG    |-M]
         [--NOT    |-N]
         [--OUT    |-O]
         [--PID    |-P]
         [--WID    |-W]
         [cidlog]

=head1 DESCRIPTION

The cidcall tool displays the cidcall.log file in one of
two different formats: raw and human readable.

The default is to display BLK, CID, HUP, OUT, PID, and WID lines in
a human readable format.

=head2 Options

=over 7

=item -h, --help

Displays the help message and exits.

=item -m, --man

Displays the manual page and exits.

=item -V, --version

Displays the version and exits.

=item -f <0-1>, --format <0-1>

Determines the output format used.

Output format 0 displays the call log file as-is.

Output format 1 displays the call log in human readable text.

The default output format is 1 (human readable).

=item -B, --BLK

Displays BLK lines (blocked calls) in the call file.

=item -C, --CID

Displays CID lines (incoming calls) in the call file.

=item -E, --END

Displays END lines (gateway end of call) in the call file.

=item -H, --HUP

Displays HUP lines (terminated calls) in the call file.

=item -M, --MSG

Displays MSG lines (messages) in the call file.

=item -N, --NOT

Displays NOT lines (smartphone note (message)) in the call file.

=item -O, --OUT

Displays OUT lines (outgoing calls) in the call file.

=item -P, --PID

Displays PID lines (smartphone Caller ID) in the call file.  Obsolete.

=item -W, --WID

Displays WID lines ("call waiting" calls) in the call file.

=back

=head2 Arguments

=over 7

=item cidlog

The NCID call file.

Default: /var/log/cidcall.log

=back

=head1 SEE ALSO

ncidd.conf.5

=cut
