| 1 |
#!/usr/bin/perl -w |
|---|
| 2 |
# Retrieves the sip user/peer entries from the database |
|---|
| 3 |
# Use these commands to create the appropriate tables in MySQL |
|---|
| 4 |
# |
|---|
| 5 |
#CREATE TABLE sip (id INT(11) DEFAULT -1 NOT NULL,keyword VARCHAR(20) NOT NULL,data VARCHAR(50) NOT NULL, flags INT(1) DEFAULT 0 NOT NULL,PRIMARY KEY (id,keyword)); |
|---|
| 6 |
# |
|---|
| 7 |
# if flags = 1 then the records are not included in the output file |
|---|
| 8 |
|
|---|
| 9 |
use FindBin; |
|---|
| 10 |
push @INC, "$FindBin::Bin"; |
|---|
| 11 |
|
|---|
| 12 |
use DBI; |
|---|
| 13 |
require "retrieve_parse_amportal_conf.pl"; |
|---|
| 14 |
|
|---|
| 15 |
################### BEGIN OF CONFIGURATION #################### |
|---|
| 16 |
|
|---|
| 17 |
######## STYLE INFO ######### |
|---|
| 18 |
$extenpos="2-40"; |
|---|
| 19 |
$trunkpos="52-60,71-80"; |
|---|
| 20 |
$confepos=""; |
|---|
| 21 |
$queuepos="42-50,61-70"; |
|---|
| 22 |
|
|---|
| 23 |
# Remove or add Zap trunks as needed |
|---|
| 24 |
# Note: ZAP/* will match any ZAP channel that *is not referenced* in another button (ie: extensions) |
|---|
| 25 |
@zaplines=(); # zap channel, description |
|---|
| 26 |
#@zaplines=(@zaplines,[ "Zap/*","PSTN" ]); |
|---|
| 27 |
#@zaplines=(@zaplines,[ "Zap/1","Zap 1" ]); |
|---|
| 28 |
#@zaplines=(@zaplines,[ "Zap/2","Zap 2" ]); |
|---|
| 29 |
#@zaplines=(@zaplines,[ "Zap/3","Zap 3" ]); |
|---|
| 30 |
#@zaplines=(@zaplines,[ "Zap/4","Zap 4" ]); |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
# LETS PARSE zapata.conf |
|---|
| 34 |
# Allowed format options |
|---|
| 35 |
# %c Zap Channel number |
|---|
| 36 |
# %n Line number |
|---|
| 37 |
# %N Line number, but restart counter |
|---|
| 38 |
# Example: |
|---|
| 39 |
# ;AMPLABEL:Channel %c - Button %n |
|---|
| 40 |
|
|---|
| 41 |
$zapataconf="/etc/asterisk/zapata.conf"; |
|---|
| 42 |
|
|---|
| 43 |
$ampwildcard=0; |
|---|
| 44 |
|
|---|
| 45 |
$zaplabel="Zap \%c"; |
|---|
| 46 |
$lastlabelnum=0; |
|---|
| 47 |
open ZAPATA, "<$zapataconf" || die "Cannot open config file: $zapataconf\n"; |
|---|
| 48 |
|
|---|
| 49 |
while( $line = <ZAPATA> ) { |
|---|
| 50 |
next if $line =~ /^(\s)*$/; |
|---|
| 51 |
chomp($line); |
|---|
| 52 |
if($line =~ /^;AMPWILDCARDLABEL\((\d+)\)\s*:\s*([\S\s]+)\s*$/) { |
|---|
| 53 |
@zaplines=(@zaplines,[ "Zap/*","$2",$1 ]); |
|---|
| 54 |
$ampwildcard=1; |
|---|
| 55 |
next; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
if($line =~ /^;AMPLABEL:\s*(\S+[\s\S]*)$/) { |
|---|
| 59 |
$zaplabel=$1; |
|---|
| 60 |
$line=~/\%N/ and $lastlabelnum=0; |
|---|
| 61 |
$ampwildcard=0; |
|---|
| 62 |
next; |
|---|
| 63 |
} |
|---|
| 64 |
if($line =~ /^[b]?channel\s*=\s*[>]?\s*([\d\,-]+)\s*$/) { |
|---|
| 65 |
$ampwildcard and next; |
|---|
| 66 |
@ranges=split(/,/,$1); |
|---|
| 67 |
foreach $ran(@ranges) { |
|---|
| 68 |
@range=split(/-/,$ran); |
|---|
| 69 |
$start=$range[0]; |
|---|
| 70 |
$end=$start; |
|---|
| 71 |
@range>1 and $end=$range[1]; |
|---|
| 72 |
foreach $c($start .. $end) { |
|---|
| 73 |
$lastlabelnum++; |
|---|
| 74 |
$newlabel=$zaplabel; |
|---|
| 75 |
$newlabel=~s/\%c/$c/; |
|---|
| 76 |
$newlabel=~s/\%n/$lastlabelnum/; |
|---|
| 77 |
$newlabel=~s/\%N/$lastlabelnum/; |
|---|
| 78 |
|
|---|
| 79 |
@zaplines=(@zaplines,[ "Zap/$c","$newlabel" ]); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
#Finished parsing zapata.conf |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
# Conference Rooms not yet implemented in AMP config |
|---|
| 89 |
@conferences=(); #### ext#, description |
|---|
| 90 |
#@conferences=(@conferences,[ "810","Conf.10" ]); |
|---|
| 91 |
#@conferences=(@conferences,[ "811","Conf.11" ]); |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
# the name of the database our tables are kept |
|---|
| 95 |
$database = "asterisk"; |
|---|
| 96 |
|
|---|
| 97 |
# cool hack by Julien BLACHE <jblache@debian.org> |
|---|
| 98 |
$ampconf = parse_amportal_conf( "/etc/amportal.conf" ); |
|---|
| 99 |
|
|---|
| 100 |
# WARNING: this file will be substituted by the output of this program |
|---|
| 101 |
$op_conf = $ampconf->{"AMPWEBROOT"}."/panel/op_buttons_additional.cfg"; |
|---|
| 102 |
# username to connect to the database |
|---|
| 103 |
$username = $ampconf->{"AMPDBUSER"}; |
|---|
| 104 |
# password to connect to the database |
|---|
| 105 |
$password = $ampconf->{"AMPDBPASS"}; |
|---|
| 106 |
# the name of the box the MySQL database is running on |
|---|
| 107 |
$hostname = $ampconf->{"AMPDBHOST"}; |
|---|
| 108 |
#sort option: extension or lastname |
|---|
| 109 |
$sortoption = $ampconf->{"FOPSORT"}; |
|---|
| 110 |
|
|---|
| 111 |
# the engine to be used for the SQL queries, |
|---|
| 112 |
# if none supplied, backfall to mysql |
|---|
| 113 |
$db_engine = "mysql"; |
|---|
| 114 |
if (exists($ampconf->{"AMPDBENGINE"})){ |
|---|
| 115 |
$db_engine = $ampconf->{"AMPDBENGINE"}; |
|---|
| 116 |
} |
|---|
| 117 |
################### END OF CONFIGURATION ####################### |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
if ( $db_engine eq "mysql" ) { |
|---|
| 121 |
$dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password"); |
|---|
| 122 |
} |
|---|
| 123 |
elsif ( $db_engine eq "pgsql" ) { |
|---|
| 124 |
$dbh = DBI->connect("dbi:pgsql:dbname=$database;host=$hostname", "$username", "$password"); |
|---|
| 125 |
} |
|---|
| 126 |
elsif ( $db_engine eq "sqlite" ) { |
|---|
| 127 |
if (!exists($ampconf->{"AMPDBFILE"})) { |
|---|
| 128 |
print "No AMPDBFILE set in /etc/amportal.conf\n"; |
|---|
| 129 |
exit; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
my $db_file = $ampconf->{"AMPDBFILE"}; |
|---|
| 133 |
$dbh = DBI->connect("dbi:SQLite2:dbname=$db_file","",""); |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
open EXTEN, ">$op_conf" || die "Cannot create/overwrite config file: $op_conf\n"; |
|---|
| 137 |
|
|---|
| 138 |
#First, populate extensions |
|---|
| 139 |
|
|---|
| 140 |
@extensionlist=(); |
|---|
| 141 |
|
|---|
| 142 |
if (table_exists($dbh,"devices")) { |
|---|
| 143 |
$statement = "SELECT description,id,dial from devices"; |
|---|
| 144 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 145 |
@resultSet = @{$result}; |
|---|
| 146 |
if ( $#resultSet == -1 ) { |
|---|
| 147 |
print "Notice: no devices defined\n"; |
|---|
| 148 |
} |
|---|
| 149 |
push(@extensionlist, @{ $result }); |
|---|
| 150 |
} |
|---|
| 151 |
else { print "Table does not exist: devices\n"; } |
|---|
| 152 |
|
|---|
| 153 |
# sort the extensions |
|---|
| 154 |
if (defined($sortoption) && ($sortoption eq "lastname")) { |
|---|
| 155 |
@extensionlist=sort by_lastname @extensionlist; |
|---|
| 156 |
} else { |
|---|
| 157 |
@extensionlist=sort {$a->[1] cmp $b->[1]}(@extensionlist); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
#Next, populate queues |
|---|
| 161 |
@queues=(); |
|---|
| 162 |
if (table_exists($dbh,"extensions")) { |
|---|
| 163 |
$statement = "SELECT extension,descr from extensions where application='Queue' and flags <> 1 order by extension"; |
|---|
| 164 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 165 |
@resultSet = @{$result}; |
|---|
| 166 |
if ( $#resultSet == -1 ) { |
|---|
| 167 |
print "Notice: no Queues defined\n"; |
|---|
| 168 |
} |
|---|
| 169 |
push(@queues, @{ $result }); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
#Next, populate trunks (sip and iax) |
|---|
| 173 |
@trunklist=(); |
|---|
| 174 |
foreach $table ("sip","iax") { |
|---|
| 175 |
if (table_exists($dbh,$table)) { |
|---|
| 176 |
$statement = "SELECT data,id,'$table' from $table where keyword='account' and flags <> 1 and id>9999 group by data order by id"; |
|---|
| 177 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 178 |
@resultSet = @{$result}; |
|---|
| 179 |
if ( $#resultSet == -1 ) { |
|---|
| 180 |
print "Notice: no $table trunks defined\n"; |
|---|
| 181 |
} |
|---|
| 182 |
push(@trunklist, @{ $result }); |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
#Determine AMP Users |
|---|
| 187 |
@ampusers=(["default","0","0"]); |
|---|
| 188 |
if (table_exists($dbh,"ampusers")) { |
|---|
| 189 |
$statement = 'SELECT deptname,extension_low,extension_high from ampusers WHERE NOT extension_low = "" AND NOT extension_high = ""'; |
|---|
| 190 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 191 |
@resultSet = @{$result}; |
|---|
| 192 |
if ( $#resultSet == -1 ) { |
|---|
| 193 |
print "Notice: no AMP Users defined\n"; |
|---|
| 194 |
} |
|---|
| 195 |
push(@ampusers, @{ $result }); |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
#Write a separate panel context from each AMP User's department |
|---|
| 199 |
foreach my $pcontext ( @ampusers ) { |
|---|
| 200 |
my $exten_low = @{$pcontext}[1]; |
|---|
| 201 |
my $exten_high = @{$pcontext}[2]; |
|---|
| 202 |
my $panelcontext = @{$pcontext}[0]; |
|---|
| 203 |
if ($panelcontext eq "") { $panelcontext = $exten_low."to".$exten_high; } |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
# WRITE EXTENSIONS |
|---|
| 207 |
|
|---|
| 208 |
$btn=0; |
|---|
| 209 |
if ($exten_low != 0 && $exten_high != 0) { #display only allowed range of extensions for panel_contexts |
|---|
| 210 |
@extensionrange = grep { @{ $_ }[1]+0 >= $exten_low && @{ $_ }[1]+0 <= $exten_high } @extensionlist; |
|---|
| 211 |
} else { |
|---|
| 212 |
@extensionrange = @extensionlist; |
|---|
| 213 |
} |
|---|
| 214 |
foreach my $row ( @extensionrange ) { |
|---|
| 215 |
my $description = @{ $row }[0]; |
|---|
| 216 |
my $id = @{ $row }[1]; |
|---|
| 217 |
my $dial = @{ $row }[2]; |
|---|
| 218 |
# # next if ($account eq ""); |
|---|
| 219 |
$btn=get_next_btn($extenpos,$btn); |
|---|
| 220 |
$icon='4'; |
|---|
| 221 |
print EXTEN "[$dial]\nPosition=$btn\nLabel=\"$id : $description\"\nExtension=$id\nContext=from-internal\nIcon=$icon\nVoicemail_Context=device\nPanel_Context=$panelcontext\n"; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
### NOW WRITE TRUNKS.. WE START WITH ZAP TRUNKS DEFINED ABOVE |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
$btn=0; |
|---|
| 231 |
foreach my $row ( @zaplines ) { |
|---|
| 232 |
$zapdef=@{$row}[0]; |
|---|
| 233 |
$zapdesc=@{$row}[1]; |
|---|
| 234 |
$icon='3'; |
|---|
| 235 |
$btn=get_next_btn($trunkpos,$btn); |
|---|
| 236 |
if ($zapdef eq "Zap/*") { |
|---|
| 237 |
$numbuttons=@{$row}[2]-1; |
|---|
| 238 |
print EXTEN "[$zapdef]\nLabel=\"$zapdesc\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\nPosition=".$btn; |
|---|
| 239 |
while($numbuttons-->0) { |
|---|
| 240 |
$btn=get_next_btn($trunkpos,$btn); |
|---|
| 241 |
print EXTEN ",".$btn; |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
print EXTEN "\n"; |
|---|
| 245 |
} else { |
|---|
| 246 |
print EXTEN "[$zapdef]\nPosition=$btn\nLabel=\"$zapdesc\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\n"; |
|---|
| 247 |
} |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
foreach my $row ( @trunklist ) { |
|---|
| 252 |
my $account = @{ $row }[0]; |
|---|
| 253 |
my $id = @{ $row }[1]; |
|---|
| 254 |
my $table = @{ $row }[2]; |
|---|
| 255 |
next if ($account eq ""); |
|---|
| 256 |
$btn=get_next_btn($trunkpos,$btn); |
|---|
| 257 |
$statement = "SELECT keyword,data from $table where id=$id and keyword <> 'account' and flags <> 1 order by keyword"; |
|---|
| 258 |
my $result = $dbh->selectall_arrayref($statement); |
|---|
| 259 |
unless ($result) { |
|---|
| 260 |
# check for errors after every single database call |
|---|
| 261 |
print "dbh->selectall_arrayref($statement) failed!\n"; |
|---|
| 262 |
print "DBI::err=[$DBI::err]\n"; |
|---|
| 263 |
print "DBI::errstr=[$DBI::errstr]\n"; |
|---|
| 264 |
exit; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
$tech="SIP" if $table eq "sip"; |
|---|
| 268 |
$tech="IAX2" if $table eq "iax"; |
|---|
| 269 |
#$tech="ZAP" if $table eq "zap"; #no zap trunks in db |
|---|
| 270 |
|
|---|
| 271 |
#my @resSet = @{$result}; |
|---|
| 272 |
|
|---|
| 273 |
$callerid = $account; #default callerid to account |
|---|
| 274 |
|
|---|
| 275 |
foreach my $drow ( @{ $result } ) { |
|---|
| 276 |
my @result = @{ $drow }; |
|---|
| 277 |
if ( $result[0] eq "callerid" ) { |
|---|
| 278 |
$callerid = $result[1]; |
|---|
| 279 |
@fields=split(/</,$callerid); |
|---|
| 280 |
$callerid=$fields[1] ." ". $fields[0]; |
|---|
| 281 |
$callerid =~ tr/\"<>//d; |
|---|
| 282 |
} |
|---|
| 283 |
} |
|---|
| 284 |
$icon='3'; |
|---|
| 285 |
print EXTEN "[$tech/$account]\nPosition=$btn\nLabel=\"$callerid\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\n"; |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
### Write conferences (meetme) |
|---|
| 290 |
|
|---|
| 291 |
$btn=0; |
|---|
| 292 |
if ($exten_low != 0 && $exten_high != 0) { #display only allowed range of extensions for panel_contexts |
|---|
| 293 |
@confrange = grep { @{ $_ }[0]+0 >= $exten_low && @{ $_ }[0]+0 <= $exten_high } @conferences; |
|---|
| 294 |
} else { |
|---|
| 295 |
@confrange = @conferences; |
|---|
| 296 |
} |
|---|
| 297 |
foreach my $row ( @confrange ) { |
|---|
| 298 |
$btn=get_next_btn($confepos,$btn); |
|---|
| 299 |
$confenum=@{$row}[0]; |
|---|
| 300 |
$confedesc=@{$row}[1]; |
|---|
| 301 |
$icon='6'; |
|---|
| 302 |
print EXTEN "[$confenum]\nPosition=$btn\nLabel=\"$confedesc\"\nExtension=$confenum\nContext=from-internal\nIcon=$icon\nPanel_Context=$panelcontext\n"; |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
### Write Queues |
|---|
| 306 |
|
|---|
| 307 |
$btn=0; |
|---|
| 308 |
if ($exten_low != 0 && $exten_high != 0) { #display only allowed range of extensions for panel_contexts |
|---|
| 309 |
@queuerange = grep { @{ $_ }[0]+0 >= $exten_low && @{ $_ }[0]+0 <= $exten_high } @queues; |
|---|
| 310 |
} else { |
|---|
| 311 |
@queuerange = @queues; |
|---|
| 312 |
} |
|---|
| 313 |
foreach my $row ( @queuerange ) { |
|---|
| 314 |
$btn=get_next_btn($queuepos,$btn); |
|---|
| 315 |
$queuename=@{$row}[0]; |
|---|
| 316 |
$queuedesc=@{$row}[1]; |
|---|
| 317 |
$icon='5'; |
|---|
| 318 |
print EXTEN "[$queuename]\nPosition=$btn\nLabel=\"$queuedesc\"\nExtension=-1\nContext=from-internal\nIcon=$icon\nPanel_Context=$panelcontext\n"; |
|---|
| 319 |
} |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
sub get_next_btn { |
|---|
| 323 |
my $data = shift; |
|---|
| 324 |
my $last = shift; |
|---|
| 325 |
|
|---|
| 326 |
@rangelist=split(",",$data); |
|---|
| 327 |
|
|---|
| 328 |
foreach $range (@rangelist) { |
|---|
| 329 |
@rangeval=split("-",$range); |
|---|
| 330 |
return $rangeval[0] if $last < $rangeval[0]; |
|---|
| 331 |
return $last+1 if $last < $rangeval[1]; |
|---|
| 332 |
#Need to try another range def... |
|---|
| 333 |
} |
|---|
| 334 |
#If we get here, we ran out of positions :( |
|---|
| 335 |
return 0; #????? |
|---|
| 336 |
} |
|---|
| 337 |
#this sub checks for the existance of a table |
|---|
| 338 |
sub table_exists { |
|---|
| 339 |
my $db = shift; |
|---|
| 340 |
my $table = shift; |
|---|
| 341 |
my @tables = $db->tables('','','','TABLE'); |
|---|
| 342 |
if (@tables) { |
|---|
| 343 |
for (@tables) { |
|---|
| 344 |
next unless $_; |
|---|
| 345 |
$_ =~ s/`//g; |
|---|
| 346 |
return 1 if $_ eq $table |
|---|
| 347 |
} |
|---|
| 348 |
} |
|---|
| 349 |
else { |
|---|
| 350 |
eval { |
|---|
| 351 |
local $db->{PrintError} = 0; |
|---|
| 352 |
local $db->{RaiseError} = 1; |
|---|
| 353 |
$db->do(qq{SELECT * FROM $table WHERE 1 = 0 }); |
|---|
| 354 |
}; |
|---|
| 355 |
return 1 unless $@; |
|---|
| 356 |
} |
|---|
| 357 |
return 0; |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
sub by_lastname { |
|---|
| 361 |
$a_var = $a->[0]; |
|---|
| 362 |
($a_firstname,$a_lastname)=$a_var=~/^\s*([0-9A-Za-z_.]*)\s+([0-9A-Za-z_.]*).*$/; |
|---|
| 363 |
($b_firstname,$b_lastname)=$b_var=~/^\s*([0-9A-Za-z_.]*)\s+([0-9A-Za-z_.]*).*$/; |
|---|
| 364 |
if (!$a_lastname) {$a_lastname=$a_var;} |
|---|
| 365 |
if (!$b_lastname) {$b_lastname=$b_var;} |
|---|
| 366 |
$sortResult=lc $a_lastname cmp lc $b_lastname; |
|---|
| 367 |
if ($sortResult == 0) |
|---|
| 368 |
{ $sortResult=lc $a_firstname cmp lc $b_firstname } |
|---|
| 369 |
return $sortResult; |
|---|
| 370 |
} |
|---|
| 371 |
|
|---|