#!/usr/bin/perl -w
#
# Extract Phonenumbers and/or Names from the
# University of Rostock Phonedirectory Website
# and format it for Cisco7960 IP phones
#
# Usage: expects a query string "nummer" or "name" contained
#        in a "GET" request, or simply a 4-digit number or
#        part of a name when invoked from the command line
#
#
# Author: alexander.noack@web.de, Oct. 2003
#
#
# "curl" needs to be installed in order for this to work
#
use strict;

#
# This is the URL of this script as seen from the web
#
my $thisurl = "http://lingua.comlab.uni-rostock.de/cgi-bin/get_phone_number";

#
# This is the max. number of rows a CiscoIPPhoneDirectory can contain
#
my $maxrows = 32;

####
## Nothing to be done below
####

my($value, $name, $tel, $einr, $out, $search, $c, @name, @num);
my $cstart = 0;

#
# Get the query strings
#
if(defined($ENV{'REQUEST_METHOD'}))
{
 if($ENV{'REQUEST_METHOD'} eq "GET")
 {
  my $input = $ENV{'QUERY_STRING'};
  my @input = split( /&/, $input );
  my(%data, $i);
  foreach $i (0..$#input)
  {
   $input[$i]=~ s/\+/ /g;
   $input[$i]=~ s/%(..)/pack("c",hex($1))/ge;
   ($name,$value)=split(/=/,$input[$i],2);
   print "$name = $value";
   $data{$name}=$value;
   $data{$name}=~s/`/'/g;
   $data{$name}=~s/"/\"/g;
  }
  if(defined($data{nummer})){ $search="$data{nummer}"; }else{ $search="$data{name}"; }
  if(defined($data{start})){ $cstart="$data{start}"; }
 }
}
#
# Or simply read $ARGV[]
#
else
{
 $search="$ARGV[0]";
}

#
# Searching for a number? go to the number search
#
if( $search =~ /\d+/  )
{
 $search =~ s/(\d+)/$1/g;
 $out=`/usr/bin/curl -s -d "nummer=$search" webapp.uni-rostock.de/PhoneSearch/searchnr.asp`;
}
#
# use the name search otherwise
#
else
{
 $search =~ s/\/|\s//g;
 $out=`/usr/bin/curl -s -d "name=$search" webapp.uni-rostock.de/PhoneSearch/search.asp`;
}

$out =~ s/ä/ae/gm;
$out =~ s/o/oe/gm;
$out =~ s/ü/ue/gm;

#
# The resulting page is the same for either number or name search
# simply take the HTML table apart as long as something's still matching
#
while($out =~ s/^\s*<tr>\s*<td style=\"padding-left:2mm;\"><b>([^<]*)<\/b><\/td>\s*<td style=\"padding-left:3mm;\">([^<]*)\s*<\/td>\s*<td align=center>([^<]*)<\/td>\s*<\/tr>\s*$//mo)
{
 #
 # $1 is the name of the person
 # $2 is the department
 # $3 is the phonenumber
 #
 $name=$1;
 $einr=$2;
 $tel =$3;
 #
 # take out spaces, tabs and linebreaks, so that the result is just one line
 #
 $name =~ s/\f|\t|\r|\n|&nbsp;/ /mg;
 $einr =~ s/\f|\t|\r|\n|&nbsp;/ /mg;
 $tel  =~ s/\f|\t|\r|\n|&nbsp;/ /mg;
 #
 # substitute concordant spaces by just one space
 #
 $name =~ tr/ / /s;
 $einr =~ tr/ / /s;
 $tel  =~ tr/ / /s;
 #
 # remove leading or trailing spaces
 #
 $name =~ s/^\s*|\s*$//g;
 $einr =~ s/^\s*|\s*$//g;
 $tel  =~ s/^\s*|\s*$//g;
 #
 # finally save the names in @name and the numbers in @num
 #
 push(@name, $name);
 push(@num, $tel);
}

#
# print the HTML headers (we're printing XML)
#
print "Connection: close\r\n";
print "Content-Type: text/xml\r\n";

#
# due to the limitation that a CiscoIPPhoneDirectory entry can only
# hold $maxrows items, we need to split the results in groups by 
# $maxrows. The "Next" button on the phone will execute the "Refresh:"
# header which points to the next group to display using "start".
# This is not very efficient, since we do the whole information 
# retrieval each time and only skip the first "start" entries...
#

my $cend = $cstart + $maxrows;
if($cend > $#name + 1)
{
 $cend = $#name + 1;
 print "Refresh: 0; url=${thisurl}?start=1&name=$search\r\n";
}
else
{
 print "Refresh: 0; url=${thisurl}?start=$cend&name=$search\r\n";
}

print "\r\n";
print "<CiscoIPPhoneDirectory>\n";
print " <Title>Search Results</Title>\n";
if($#name+1 >= $maxrows){ print " <Prompt>Use \'Next\' for more !</Prompt>\n"; }
else{ print " <Prompt> </Prompt>\n"; }
#
# skip the first "start" entries and display what we found
#
for( $c=$cstart; $c<$cend; $c++ )
{
 print " <DirectoryEntry>\n  <Name>$name[$c]</Name>\n  <Telephone>$num[$c]</Telephone>\n </DirectoryEntry>\n";
}

print " <SoftKeyItem>\n  <Name>Wahl</Name>\n  <URL>SoftKey:Dial</URL>\n  <Position>1</Position>\n </SoftKeyItem>\n";
print " <SoftKeyItem>\n  <Name>EditDial</Name>\n  <URL>SoftKey:EditDial</URL>\n  <Position>2</Position>\n </SoftKeyItem>\n";
print " <SoftKeyItem>\n  <Name>Next</Name>\n  <URL>SoftKey:Next</URL>\n  <Position>3</Position>\n </SoftKeyItem>\n";
print " <SoftKeyItem>\n  <Name>Exit</Name>\n  <URL>SoftKey:Exit</URL>\n  <Position>4</Position>\n </SoftKeyItem>\n";
print "</CiscoIPPhoneDirectory>\n";

exit 0;
