#!/usr/bin/perl
########################################################################
# File:           rootfiles/cfedit.pl                                  #
# myLinux Server: Copyright (c) 2004 Michael Oberg                     #
# Version:        0.98                                                 #
# Author:         Michael Oberg <michael.oberg@mylinuxproject.de>      #
#                                                                      #
# 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; either version 2 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# This program is distributed in the hope that it will be useful,      #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU Public License along      #
# with this package; if not, write to the Free Software Foundation,    #
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.       #
########################################################################

#
# read config files
#

sub readconfig {
  my $list = `cat locationlist.conf | sed -e 's/#.*//' -e 's/ *\$//' | grep -v '^\$'`;
  $list =~ /locationlist[ \t]*=[ \t]*"([^"]*)"/;
  $list = $1;
  my $i = 0;
  foreach my $entry (split /\\n/, $list) {
    $location[$i++] = $entry;
  }
  $numlocations = $i;

  for ($i = 0; $i < $numlocations; $i++) {
    $dirname[$i] = &umlaut ($location[$i]);
    $list = `cat $dirname[$i].conf | sed -e 's/#.*//' -e 's/ *\$//' | grep -v '^\$'`;
    $list =~ /hdrzaptel[ \t]*=[ \t]*"([^"]*)"/; $hdrzaptel[$i] = $1;
    $list =~ /hdrzapata[ \t]*=[ \t]*"([^"]*)"/; $hdrzapata[$i] = $1;
    $list =~ /hdrsip[ \t]*=[ \t]*"([^"]*)"/; $hdrsip[$i] = $1;
    $list =~ /hdriax[ \t]*=[ \t]*"([^"]*)"/; $hdriax[$i] = $1;
    $list =~ /hdrexten[ \t]*=[ \t]*"([^"]*)"/; $hdrexten[$i] = $1;
    $list =~ /iprefix[ \t]*=[ \t]*"([^"]*)"/; $iprefix[$i] = $1;
    $list =~ /lprefix[ \t]*=[ \t]*"([^"]*)"/; $prefix[$i] = $1;
    $list =~ /hostname[ \t]*=[ \t]*"([^"]*)"/; $hostname[$i] = $1;
    $list =~ /localcbc[ \t]*=[ \t]*"([^"]*)"/; $localcbc[$i] = $1;
    $list =~ /nationalcbc[ \t]*=[ \t]*"([^"]*)"/; $nationalcbc[$i] = $1;
    $list =~ /mobilecbc[ \t]*=[ \t]*"([^"]*)"/; $mobilecbc[$i] = $1;
    $list =~ /internationalcbc[ \t]*=[ \t]*"([^"]*)"/; $internationalcbc[$i] = $1;

    $hdrzaptel[$i] =~ s/\\n/\n/g;
    $hdrzapata[$i] =~ s/\\n/\n/g;
    $hdrsip[$i] =~ s/\\n/\n/g;
    $hdriax[$i] =~ s/\\n/\n/g;
    $hdrexten[$i] =~ s/\\n/\n/g;

    $list =~ /phones[ \t]*=[ \t]*"([^"]*)"/;
    my $plist = $1;
    $numntbas[$i] = 0;
    $numpbxs[$i] = 0;
    foreach my $entry (split /\\n/, $plist) {
      $entry =~ /^([1-9][0-9]*)\/([0-9]+)\/([0-9]+)$/;
      if ($numpbxs[$i] < $2+1) {
        $numpbxs[$i] = $2+1;
        $numphones[$i][$2+0] = 0;
      }
      $phone[$i][$2+0][$numphones[$i][$2+0]] = "$1";
      $phonentba[$i][$2+0][$numphones[$i][$2+0]++] = $3+0;
      $numntbas[$i] = $3+1 if ($numntbas[$i] < $3+1);
    }

    $list =~ /locprefix[ \t]*=[ \t]*"([^"]*)"/;
    my $plist = $1;
    $locprefnum[$i] = 0;
    foreach my $entry (split /\\n/, $plist) {
      $entry =~ /^([1-9][0-9]*)$/;
      $locprefix[$i][$locprefnum[$i]++] = "$1";
    }
  }
}

#
# subroutines
#

sub umlaut {
  my ($str) = @_;
  $str =~ s/Ä/Ae/g;
  $str =~ s/Ö/Oe/g;
  $str =~ s/Ü/Ue/g;
  $str =~ s/ä/ae/g;
  $str =~ s/ö/oe/g;
  $str =~ s/ü/ue/g;
  $str =~ s/ß/ss/g;
  $str =~ s/ /_/g;
  return $str;
}

sub randompw {
  my $pw = `dd if=/dev/urandom bs=24 count=1 2> /dev/null | base64 | sed 's/[^a-zA-Z0-9]//g'`;
  $pw =~ /([a-zA-Z0-9]*)/;
  return $1;
}

sub init {
  for (my $i = 0; $i < $numlocations; $i++) {
    system ("mkdir -m 700 '" . $dirname[$i] . "'");
    for (my $j = 0; $j < $numpbxs[$i]; $j++) {
      $pbxgroup[$i][$j] = $j+1;
    }
    for (my $j = 0; $j < $numntbas[$i]; $j++) {
      $ntbagroup[$i][$j] = $j+$numpbxs[$i]+1;
    }
    $iaxpw[$i] = &randompw;
    for (my $j = 0; $j < $numpbxs[$i]; $j++) {
      for (my $k = 0; $k < $numphones[$i][$j]; $k++) {
	$phonepw[$i][$j][$k] = &randompw;
      }
    }
    for (my $j = 0; $j < $locprefnum[$i]; $j++) {
      $isprefixof[$i]{$locprefix[$i][$j]} = "yes";
    }
    $isprefixof[$i]{$prefix[$i]} = "yes";
  }
}

sub zaptel {
  my ($loc) = @_;
  open FILE, "> " . $dirname[$loc] . "/zaptel.conf";
  print FILE $hdrzaptel[$loc] . "\n";
  for (my $i = 0; $i < $numpbxs[$loc]; $i++) {
    print FILE "span=" . $pbxgroup[$loc][$i] . ",0,3,ccs,ami,yellow\n";
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "span=" . $ntbagroup[$loc][$i] . ","
	       . ($i+1) . ",3,ccs,ami,yellow\n";
  }
  print FILE "\n";
  for (my $i = 0; $i < $numpbxs[$loc]+$numntbas[$loc]; $i++) {
    print FILE "bchan=" . ($i*3+1) . "," . ($i*3+2)
	       . "\ndchan=" . ($i*3+3) . "\n";
  }
  close FILE;
}

sub zapata {
  my ($loc) = @_;
  open FILE, "> " . $dirname[$loc] . "/zapata.conf";
  print FILE $hdrzapata[$loc] . "\n";
  print FILE "signalling = bri_net_ptmp\n";
  my $c = 0;
  for (my $i = 0; $i < $numpbxs[$loc]; $i++) {
    print FILE "group=" . $pbxgroup[$loc][$i] . "\ncontext=PBX" . ($i+1)
	       . "\nChannel => " . ($c*3+1) . "-" . ($c*3+2) . "\n";
    $c++;
  }
  print FILE "\nsignalling = bri_cpe_ptmp\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "group=" . $ntbagroup[$loc][$i] . "\ncontext=NTBA" . ($i+1)
	       . "\nChannel => " . ($c*3+1) . "-" . ($c*3+2) . "\n";
    $c++;
  }
  close FILE;
}

sub iax {
  my ($loc) = @_;
  open FILE, "> " . $dirname[$loc] . "/iax.conf";
  print FILE $hdriax[$loc] . "\n";
  for (my $i = 0; $i < $numlocations; $i++) {
    if ($i != $loc) {
      print FILE "register => iax" . $i . ":" . $iaxpw[$i] . "@"
		 . $hostname[$i] . "\n";
    }
  }
  print FILE "\n; Authentiaction entry for * to * IAX trunk connection\n";
  print FILE "\n[iax" . $loc . "]\n"
	     . "type=friend\n"
	     . "auth=md5\n"
	     . "secret=" . $iaxpw[$loc] . "\n"
	     . "context=iax\n"
	     . "host=dynamic\n"
	     . "trunk=yes\n\n";
  print FILE "; The following authentiaction entries are for IAX Softphones only\n\n";
  for (my $i = 0; $i < $numpbxs[$loc]; $i++) {
    for (my $j = 0; $j < $numphones[$loc][$i]; $j++) {
      print FILE "[" . $phone[$loc][$i][$j] . "]\n"
		 . "type=friend\n"
		 . "auth=md5\n"
		 . "secret=" . $phonepw[$loc][$i][$j] . "\n"
		 . "context=iax\n"
		 . "host=dynamic\n"
		 . "callerid=" . $phone[$loc][$i][$j] . "\n\n";
    }
  }
  close FILE;
}

sub sip {
  my ($loc) = @_;
  open FILE, "> " . $dirname[$loc] . "/sip.conf";
  print FILE $hdrsip[$loc] . "\n";
  for (my $i = 0; $i < $numpbxs[$loc]; $i++) {
    for (my $j = 0; $j < $numphones[$loc][$i]; $j++) {
      print FILE "[" . $phone[$loc][$i][$j] . "]\n"
		 . "type=friend\n"
		 . "auth=md5\n"
		 . "secret=" . $phonepw[$loc][$i][$j] . "\n"
		 . "context=sip\n"
		 . "host=dynamic\n"
		 . "callerid=" . $phone[$loc][$i][$j] . "\n"
		 . "nat=no\n"
		 . "invite=no\n"
		 . "canreinvite=no\n"
		 . "username=" . $phone[$loc][$i][$j] . "\n\n";
    }
  }
  close FILE;
}

sub zapcall {
  my ($number, $group, $exten) = @_;
  my $str = "exten => "
	    . $number
	    . ",1,Dial(ZAP/g"
	    . $group
	    . "/$exten,90)\n"
	    . "exten => "
	    . $number
	    . ",2,Hangup\n";
  return $str;
}

sub sipzapcall {
  my ($number, $group, $exten) = @_;
  my $str = "exten => "
	    . $number
	    . ",1,Dial(SIP/$exten,60,tr)\n"
	    . "exten => "
	    . $number
	    . ",2,Dial(ZAP/g"
	    . $group
	    . "/$exten,90)\n"
	    . "exten => "
	    . $number
	    . ",3,Hangup\n";
  return $str;
}

sub iaxcall {
  my ($number, $loc, $exten) = @_;
  my $str = "exten => "
	    . $number
	    . ",1,Dial(IAX2/iax"
	    . $loc
	    . ":"
	    . $iaxpw[$loc]
	    . "@"
	    . $hostname[$loc]
	    . "/$exten,60,tr)\n"
	    . "exten => "
	    . $number
	    . ",2,Hangup\n";
  return $str;
}

sub exten {
  my ($loc) = @_;
  open FILE, "> " . $dirname[$loc] . "/extensions.conf";
  print FILE $hdrexten[$loc] . "\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "[NTBA" . ($i+1) . "]\n";
    for (my $j = 0; $j < $numpbxs[$loc]; $j++) {
      for (my $k = 0; $k < $numphones[$loc][$j]; $k++) {
	if ($phonentba[$loc][$j][$k] == $i) {
	  print FILE &sipzapcall ($phone[$loc][$j][$k],
				  $pbxgroup[$loc][$j],
				  "\${EXTEN}");
	  print FILE &sipzapcall ("0"
				  . $prefix[$loc]
				  . $phone[$loc][$j][$k],
				  $pbxgroup[$loc][$j],
				  "\${EXTEN:"
				  . (length($prefix[$loc])+1)
				  . "}");
	}
      }
    }
    print FILE "\n";
  }
  print FILE "[iaxlocations]\n";
  for (my $i = 0; $i < $numlocations; $i++) {
    if ($i != $loc) {
      for (my $j = 0; $j < $numpbxs[$i]; $j++) {
	for (my $k = 0; $k < $numphones[$i][$j]; $k++) {
	  print FILE &iaxcall ($phone[$i][$j][$k],
			       $i,
			       "\${EXTEN}")
		if (($prefix[$i] == $prefix[$loc])
		    and ($iprefix[$i] == $iprefix[$loc]));
	  print FILE &iaxcall ("0"
			       . $prefix[$i]
			       . $phone[$i][$j][$k],
			       $i,
			       "\${EXTEN:"
			       . (length($prefix[$i])+1)
			       . "}")
		if ($iprefix[$i] == $iprefix[$loc]);
	  print FILE &iaxcall ("00"
			       . $iprefix[$i]
			       . $prefix[$i]
			       . $phone[$i][$j][$k],
			       $i,
			       "\${EXTEN:"
			       . (length($iprefix[$i])
				  +length($prefix[$i])
				  +2)
			       . "}");
	}
      }
    }
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "\n[localcalls" . ($i+1) . "]\n";
    print FILE "exten => s,1,Digittimeout(5)\n";
    print FILE "exten => s,2,ResponsetimeOut(10)\n\n";
    for (my $j = 0; $j < $locprefnum[$loc]; $j++) {
      print FILE &zapcall ("_0"
			   . $locprefix[$loc][$j]
			   . "Z.",
			   $ntbagroup[$loc][$i],
			   $localcbc[$loc]
			   . "\${EXTEN}");
      print FILE &zapcall ("_00"
			   . $iprefix[$loc]
			   . $locprefix[$loc][$j]
			   . "Z.",
			   $ntbagroup[$loc][$i],
			   $localcbc[$loc]
			   . "0"
			   . "\${EXTEN:"
			   . (length($iprefix[$loc])
			      +2)
			   . "}");
    }
  }
  print FILE "\n[iaxnational]\n";
  print FILE "exten => s,1,Digittimeout(5)\n";
  print FILE "exten => s,2,ResponsetimeOut(10)\n\n";
  for (my $i = 0; $i < $numlocations; $i++) {
    if ($iprefix[$i] == $iprefix[$loc]) {
      for (my $j = 0; $j < $locprefnum[$i]; $j++) {
	if (!($isprefixof[$loc]{$locprefix[$i][$j]} eq "yes")) {
	  print FILE &iaxcall ("_0"
			       . $locprefix[$i][$j]
			       . "Z.",
			       $i,
			       "\${EXTEN}");
	  print FILE &iaxcall ("_00"
			       . $iprefix[$loc]
			       . $locprefix[$i][$j]
			       . "Z.",
			       $i,
			       "0\${EXTEN:"
			       . (length($iprefix[$i])+2)
			       . "}");
	}
      }
    }
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "\n[mobile" . ($i+1) . "]\n";
    print FILE "exten => s,1,Digittimeout(5)\n";
    print FILE "exten => s,2,ResponsetimeOut(10)\n\n";
    print FILE &zapcall ("_01[5-7]XZ.",
			 $ntbagroup[$loc][$i],
			 $mobilecbc[$loc]
			 . "\${EXTEN}");
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "\n[national" . ($i+1) . "]\n";
    print FILE "exten => s,1,Digittimeout(5)\n";
    print FILE "exten => s,2,ResponsetimeOut(10)\n\n";
    print FILE &zapcall ("_0Z.",
			 $ntbagroup[$loc][$i],
			 $nationalcbc[$loc]
			 . "\${EXTEN}");
    print FILE &zapcall ("_00"
			 . $iprefix[$loc]
			 . "Z.",
			 $ntbagroup[$loc][$i],
			 $nationalcbc[$loc]
			 . "0"
			 . "\${EXTEN:"
			 . (length($iprefix[$loc])
			    +2)
			 . "}");
  }
  print FILE "\n[iaxinternational]\n";
  print FILE "exten => s,1,Digittimeout(5)\n";
  print FILE "exten => s,2,ResponsetimeOut(10)\n\n";
  for (my $i = 0; $i < $numlocations; $i++) {
    if ($iprefix[$i] != $iprefix[$loc]) {
      print FILE &iaxcall ("_00"
			   . $iprefix[$i]
			   . "X.",
			   $i,
			   "0\${EXTEN:"
			   . (length($iprefix[$i])+2)
			   . "}");
    }
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "\n[international" . ($i+1) . "]\n";
    print FILE "exten => s,1,Digittimeout(5)\n";
    print FILE "exten => s,2,ResponsetimeOut(10)\n\n";
    print FILE &zapcall ("_00Z.",
			 $ntbagroup[$loc][$i],
			 $internationalcbc[$loc]
			 . "\${EXTEN}");
  }
  print FILE "\n[iax]\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => NTBA" . ($i+1) . "\n";
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => localcalls" . ($i+1) . "\n";
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => mobile" . ($i+1) . "\n";
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => national" . ($i+1) . "\n";
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => international" . ($i+1) . "\n";
  }
  print FILE "\n[sip]\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => NTBA" . ($i+1) . "\n";
  }
  print FILE "include => iaxlocations\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => localcalls" . ($i+1) . "\n";
  }
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => mobile" . ($i+1) . "\n";
  }
  print FILE "include => iaxnational\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => national" . ($i+1) . "\n";
  }
  print FILE "include => iaxinternational\n";
  for (my $i = 0; $i < $numntbas[$loc]; $i++) {
    print FILE "include => international" . ($i+1) . "\n";
  }
  for (my $i = 0; $i < $numpbxs[$loc]; $i++) {
    print FILE "\n[PBX" . ($i+1) ."]\n";
    for (my $j = 0; $j < $numntbas[$loc]; $j++) {
      print FILE "include => NTBA" . ($j+1) . "\n";
    }
    print FILE "include => iaxlocations\n";
    for (my $i = 0; $i < $numntbas[$loc]; $i++) {
      print FILE "include => localcalls" . ($i+1) . "\n";
    }
    for (my $i = 0; $i < $numntbas[$loc]; $i++) {
      print FILE "include => mobile" . ($i+1) . "\n";
    }
    print FILE "include => iaxnational\n";
    for (my $i = 0; $i < $numntbas[$loc]; $i++) {
      print FILE "include => national" . ($i+1) . "\n";
    }
    print FILE "include => iaxinternational\n";
    for (my $i = 0; $i < $numntbas[$loc]; $i++) {
      print FILE "include => international" . ($i+1) . "\n";
    }
  }
  close FILE;
}

#
# main program
#

&readconfig;
&init;

for (my $i = 0; $i < $numlocations; $i++) {
  # write config files for this location
  &zaptel ($i);
  &zapata ($i);
  &iax ($i);
  &sip ($i);
  &exten ($i);
}
