Debian Codegraph @

xpp/perl_modules/Dahdi/Config/Gen/Freepbxdb.pm

raw
1 package Dahdi::Config::Gen::Freepbxdb;
2
3 # Written by Tzafrir Cohen <[email protected]>
4 # Copyright (C) 2011, Xorcom
5 # This program is free software; you can redistribute and/or
6 # modify it under the same terms as Perl itself.
7
8 use strict;
9 use Socket;
10 use Dahdi::Config::Gen qw(is_true);
11
12 sub new($$$) {
13 my $pack = shift || die;
14 my $gconfig = shift || die;
15 my $genopts = shift || die;
16 my $self = {
17 GCONFIG => $gconfig,
18 GENOPTS => $genopts,
19 };
20 bless $self, $pack;
21 return $self;
22 }
23
24 sub gen_channel($$) {
25 my $self = shift || die;
26 my $chan = shift || die;
27 my $gconfig = $self->{GCONFIG};
28 my $type = $chan->type;
29 my $num = $chan->num;
30 die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
31 my $exten = $gconfig->{'base_exten'} + $num;
32 my $callerid = sprintf "\"Channel %d\" <%04d>", $num, $exten;
33 my @cmds = ();
34 #push @cmds, "database put DEVICE/$exten default_user $exten";
35 #push @cmds, "database put DEVICE/$exten dial ZAP/$num";
36 push @cmds, "database put DEVICE/$exten dial DAHDI/$num";
37 #push @cmds, "database put DEVICE/$exten type fixed";
38 push @cmds, "database put DEVICE/$exten user $exten";
39 push @cmds, "database put AMPUSER/$exten device $exten";
40 push @cmds, "database put AMPUSER/$exten cidname $callerid";
41 return @cmds;
42 }
43
44 sub generate($) {
45 my $self = shift || die;
46 my $gconfig = $self->{GCONFIG};
47 my $genopts = $self->{GENOPTS};
48 #$gconfig->dump;
49 my $ast_sock = '/var/run/asterisk/asterisk.ctl';
50 my @spans = @_;
51 my @cmds = ();
52 warn "Empty configuration -- no spans\n" unless @spans;
53 print "Configuring FXSs for FreePBX\n" if $genopts->{verbose};
54 foreach my $span (@spans) {
55 next if $span->is_digital;
56 foreach my $chan ($span->chans()) {
57 next unless ($chan->type eq 'FXS');
58 push @cmds, $self->gen_channel($chan);
59 }
60 }
61 #open(CMDS,"|$command >/dev/null") or
62 socket(SOCK, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
63 connect(SOCK, sockaddr_un($ast_sock)) ||
64 die "$0: Freepbxdb: Failed connecting to $ast_sock\n: $!";
65 foreach (@cmds) {
66 # Note: commands are NULL-terminated:
67 print SOCK "$_\0";
68 sleep 0.001;
69 }
70 close(SOCK) or
71 die "$0: Freepbxdb: Failed sending commands ($ast_sock): $!\n";
72 }
73
74 1;
75
76 __END__
77
78 =head1 NAME
79
80 freepbxdb - Generate astdb configuration required by FreePBX
81
82 =head1 SYNOPSIS
83
84 use Dahdi::Config::Gen::Freepbxdb;
85
86 my $cfg = new Dahdi::Config::Gen::Freepbxdb(\%global_config, \%genopts);
87 $cfg->generate(@span_list);
88
89 =head1 DESCRIPTION
90
91 Updates the Asterisk DB entries for FXS channels detected. Requires
92 Asterisk running.
93
94 The configuration generated here bypasses FreePBX's standard configuration
95 and allows using a simple dialplan snippet such as:
96
97 [from-internal-custom](+)
98 exten => _4XXX,1,Dial(DAHDI/${EXTEN:1})
99
100 This may come in handy in testing. At least until FreePBX will provide a
101 simple automated interface to do the same.
102
103 =head1 OPTIONS
104
105 None, so far.
106
107 =head1 FILES
108
109 =over
110
111 =item C</var/run/asterisk/asterisk.sock>
112
113 The socket to which commands are sent. FIXME: make this a parameter.
114
115 =back
116