retrieve_extensions_from_sql.pl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/perl -Tw
  2. # Author: Peter Nixon <codemonkey@peternixon.net>
  3. # Date: April 2004
  4. # Copy Policy: GNU Public Licence Version 2 or later
  5. # URL: http://www.peternixon.net/code/
  6. # Supported: PostgreSQL, Oracle, MySQL
  7. # Copyright: 2004 Peter Nixon <codemonkey@petenixon.net>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # $Id$
  20. #
  21. # Use these commands to create the appropriate SQL tables
  22. # If flags is 1 then the record is not included in the output extensions file
  23. #
  24. #CREATE TABLE extensions (
  25. # context VARCHAR(20) DEFAULT 'default' NOT NULL,
  26. # extension VARCHAR(20) NOT NULL,
  27. # priority INTEGER DEFAULT '1' NOT NULL,
  28. # application VARCHAR(20) NOT NULL,
  29. # args VARCHAR(50),
  30. # descr TEXT,
  31. # flags BOOLEAN DEFAULT '0' NOT NULL,
  32. # PRIMARY KEY(context, extension, priority)
  33. #);
  34. #CREATE TABLE globals (
  35. # variable VARCHAR(20) NOT NULL,
  36. # value VARCHAR(50) NOT NULL,
  37. # PRIMARY KEY(variable, value)
  38. #);
  39. use strict; # Make sure we write decent perl code
  40. require DBI; # We need database drivers for this thing to work
  41. ################### BEGIN OF CONFIGURATION ####################
  42. my $table_name = "extensions"; # name of the extensions table
  43. my $global_table_name = "globals"; # name of the globals table
  44. my $extensions_conf = "/etc/asterisk/extensions.conf"; # path to extensions.conf
  45. # WARNING: this file will be substituted by the output of this program
  46. my $dbbrand = "Pg"; # Hint: "mysql" or any other Perl DBI driver.
  47. my $hostname = "localhost"; # The SQL server's hostname or IP
  48. my $database = "peter"; # the name of the database our tables are kept
  49. my $username = "peter"; # username to connect to the database
  50. my $password = ""; # password to connect to the database
  51. my $verbose = 1; # Verbosity Level (0 - 2)
  52. ################### END OF CONFIGURATION #######################
  53. # You should not need to edit anything below here
  54. my $dbh;
  55. sub db_connect {
  56. if ($verbose > 1) { print "DEBUG: Connecting to Database Host: $hostname\n" }
  57. if ($hostname eq 'localhost') {
  58. if ($verbose > 1) { print "DEBUG: SQL server is on localhost so using UNIX socket instead of network socket.\n" }
  59. $dbh = DBI->connect("DBI:$dbbrand:dbname=$database", "$username", "$password")
  60. or die "Couldn't connect to database: " . DBI->errstr;
  61. }
  62. else {
  63. $dbh = DBI->connect("DBI:$dbbrand:dbname=$database;host=$hostname", "$username", "$password")
  64. or die "Couldn't connect to database: " . DBI->errstr;
  65. }
  66. }
  67. sub db_disconnect {
  68. if ($verbose > 1) { print "DEBUG: Disconnecting from Database Host: $hostname\n" }
  69. $dbh->disconnect
  70. or warn "Disconnection failed: $DBI::errstr\n";
  71. }
  72. sub get_globals {
  73. if ($verbose > 0) { print "Checking Database for [global] variables\n"; }
  74. my $sth = $dbh->prepare("SELECT variable, value FROM $global_table_name ORDER BY variable")
  75. or die "Couldn't prepare statement: " . $dbh->errstr;
  76. $sth->execute() # Execute the query
  77. or die "Couldn't execute SELECT statement: " . $sth->errstr;
  78. if ($sth->rows > 0) {
  79. print EXTEN "[globals]\n";
  80. while (my @global = $sth->fetchrow_array()) {
  81. print EXTEN "$global[0] = $global[1]\n";
  82. }
  83. print EXTEN "\n";
  84. } else {
  85. print "WARNING: You have no global variables set\n";
  86. }
  87. $sth->finish;
  88. }
  89. sub get_contexts {
  90. if ($verbose > 0) { print "Checking Database for contexts\n"; }
  91. my $sth = $dbh->prepare("SELECT context FROM $table_name GROUP BY context")
  92. or die "Couldn't prepare statement: " . $dbh->errstr;
  93. $sth->execute() # Execute the query
  94. or die "Couldn't execute SELECT statement: " . $sth->errstr;
  95. if ($sth->rows > 0) {
  96. while (my @context = $sth->fetchrow_array()) {
  97. print EXTEN "[$context[0]]\n";
  98. &get_extensions($context[0]);
  99. print EXTEN "\n";
  100. }
  101. print EXTEN "\n";
  102. } else {
  103. print "WARNING: You have no contexts defined in the $table_name table\n";
  104. }
  105. $sth->finish;
  106. }
  107. sub get_extensions {
  108. my $context = $_[0]; my @extension;
  109. if ($verbose > 0) { print " Checking Database for [$context] extensions\n"; }
  110. my $sth = $dbh->prepare("SELECT extension, priority, application, args, descr FROM $table_name WHERE context='$context' AND flags = '0' ORDER BY extension, priority")
  111. or die "Couldn't prepare statement: " . $dbh->errstr;
  112. $sth->execute() # Execute the query
  113. or die "Couldn't execute SELECT statement: " . $sth->errstr;
  114. if ($sth->rows > 0) {
  115. while (@extension = $sth->fetchrow_array()) {
  116. print EXTEN "exten => $extension[0],$extension[1],$extension[2]";
  117. print EXTEN "($extension[3])" if defined $extension[3];
  118. print EXTEN " ; $extension[4]" if defined $extension[4];
  119. print EXTEN "\n";
  120. }
  121. } else {
  122. print "WARNING: You have no extensions for [$context]\n";
  123. }
  124. $sth->finish;
  125. }
  126. sub main {
  127. open EXTEN, ">$extensions_conf" || die "Cannot create/overwrite extensions file: $extensions_conf\n";
  128. &db_connect;
  129. &get_globals;
  130. &get_contexts;
  131. &db_disconnect;
  132. close EXTEN; # Close the file handle
  133. if ($verbose > 0) { print "New $extensions_conf successfully written.\n"; }
  134. return 1;
  135. }
  136. exit &main();