retrieve_extensions_from_mysql.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/perl -Tw
  2. # Use these commands to create the appropriate tables in MySQL
  3. # If flags is 1 then this record is not included in the output extensions file
  4. #
  5. #CREATE TABLE extensions (
  6. # context CHAR(20) DEFAULT 'default' NOT NULL,
  7. # extension CHAR(20) NOT NULL,
  8. # priority INT(2) DEFAULT '1' NOT NULL,
  9. # application CHAR(20) NOT NULL,
  10. # args CHAR(50),
  11. # descr TEXT,
  12. # flags INT(1) DEFAULT '0' NOT NULL,
  13. # PRIMARY KEY(context, extension, priority)
  14. #);
  15. #
  16. #CREATE TABLE globals (
  17. # variable CHAR(20) NOT NULL,
  18. # value CHAR(50) NOT NULL,
  19. # PRIMARY KEY(variable, value)
  20. #);
  21. use DBI;
  22. ################### BEGIN OF CONFIGURATION ####################
  23. # the name of the extensions table
  24. $table_name = "extensions";
  25. # the name of the globals table
  26. $global_table_name = "globals";
  27. # the path to the extensions.conf file
  28. # WARNING: this file will be substituted by the output of this program
  29. $extensions_conf = "/etc/asterisk/extensions.conf";
  30. # the name of the box the MySQL database is running on
  31. $hostname = "localhost";
  32. # the name of the database our tables are kept
  33. $database = "user";
  34. # username to connect to the database
  35. $username = "";
  36. # password to connect to the database
  37. $password = "";
  38. ################### END OF CONFIGURATION #######################
  39. open EXTEN, ">$extensions_conf" || die "Cannot create/overwrite extensions file: $extensions_conf\n";
  40. $dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password");
  41. $statement = "SELECT * from $global_table_name order by variable";
  42. my $result = $dbh->selectall_arrayref($statement);
  43. unless ($result) {
  44. # check for errors after every single database call
  45. print "dbh->selectall_arrayref($statement) failed!\n";
  46. print "DBI::err=[$DBI::err]\n";
  47. print "DBI::errstr=[$DBI::errstr]\n";
  48. exit;
  49. }
  50. my @resultSet = @{$result};
  51. if ( $#resultSet > -1 ) {
  52. print EXTEN "[globals]\n";
  53. foreach $row (@{ $result }) {
  54. my @result = @{ $row };
  55. print EXTEN "$result[0] = $result[1]\n";
  56. }
  57. print EXTEN "\n";
  58. }
  59. $statement = "SELECT context from $table_name group by context";
  60. $result = $dbh->selectall_arrayref($statement);
  61. unless ($result) {
  62. # check for errors after every single database call
  63. print "dbh->selectall_arrayref($statement) failed!\n";
  64. print "DBI::err=[$DBI::err]\n";
  65. print "DBI::errstr=[$DBI::errstr]\n";
  66. }
  67. @resultSet = @{$result};
  68. if ( $#resultSet == -1 ) {
  69. print "No extensions defined in $table_name\n";
  70. exit;
  71. }
  72. foreach my $row ( @{ $result } ) {
  73. my $context = @{ $row }[0];
  74. print EXTEN "[$context]\n";
  75. $statement = "SELECT * from $table_name where context='$context' order by extension, priority";
  76. my $result = $dbh->selectall_arrayref($statement);
  77. unless ($result) {
  78. # check for errors after every single database call
  79. print "dbh->selectall_arrayref($statement) failed!\n";
  80. print "DBI::err=[$DBI::err]\n";
  81. print "DBI::errstr=[$DBI::errstr]\n";
  82. exit;
  83. }
  84. my @resSet = @{$result};
  85. if ( $#resSet == -1 ) {
  86. print "no results\n";
  87. exit;
  88. }
  89. foreach my $row ( @{ $result } ) {
  90. my @result = @{ $row };
  91. if ($result[6] == 0) {
  92. print EXTEN "exten => $result[1],$result[2],$result[3]";
  93. print EXTEN "($result[4])" if defined $result[4];
  94. print EXTEN "\t" if not defined $result[4];
  95. print EXTEN "\t; $result[5]" if defined $result[5];
  96. print EXTEN "\n";
  97. }
  98. }
  99. print EXTEN "\n";
  100. }
  101. exit 0;