1 package FS::part_export::freeswitch;
2 use base qw( FS::part_export );
4 use vars qw( %info ); # $DEBUG );
8 use FS::Record qw( qsearch ); #qsearchs );
10 #use FS::Schema qw( dbdef );
14 tie my %options, 'Tie::IxHash',
15 'user' => { label => 'SSH username', default=>'root', },
16 'directory' => { label => 'Directory to store FreeSWITCH account XML files',
17 default => '/usr/local/freeswitch/conf/directory/',
19 #'domain' => { label => 'Optional fixed SIP domain to use, overrides svc_phone domain', },
20 'reload' => { label => 'Reload command',
21 default => '/usr/local/freeswitch/bin/fs_cli -x reloadxml',
23 'user_template' => { label => 'User XML configuration template',
26 <domain name="<% $domain %>">
27 <user id="<% $phonenum %>">
29 <param name="password" value="<% $sip_password %>"/>
30 <param name="nibble_account" value="<% $phonenum %>"/>
31 <param name="nibble_rate" value="<% $nibble_rate %>"/>
41 'desc' => 'Provision phone services to FreeSWITCH XML configuration files',
42 'options' => \%options,
44 Export XML account configuration files to FreeSWITCH, one per domain.
46 You will need to enable the svc_phone-domain configuration setting and
47 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
51 sub rebless { shift; }
54 my( $self, $svc_phone ) = ( shift, shift );
56 $self->_export_rebuild_domain($svc_phone);
61 my( $self, $new, $old ) = ( shift, shift, shift );
63 my $error = $self->_export_rebuild_domain($new);
64 return $error if $error;
66 if ( $new->domsvc ne $old->domsvc && $old->domsvc ) {
67 $error = $self->_export_rebuild_domain($old);
68 return $error if $error;
75 my( $self, $svc_phone ) = ( shift, shift );
77 $self->_export_rebuild_domain($svc_phone);
80 sub _export_rebuild_domain {
81 my($self, $svc_phone) = ( shift, shift );
86 #create and copy over file
88 my $tempdir = '%%%FREESIDE_CONF%%%/cache.'. $FS::UID::datasrc;
90 my $domain = $svc_phone->domain or return "domain required";
92 my $fh = new File::Temp(
93 TEMPLATE => "$tempdir/freeswitch.$domain.XXXXXXXX",
98 print $fh qq(<domain name="$domain">\n);
100 my @dom_svc_phone = qsearch( 'svc_phone', { 'domsvc'=>$svc_phone->domsvc } );
102 foreach my $dom_svc_phone (@dom_svc_phone) {
104 print $fh $self->freeswitch_template_fillin( $dom_svc_phone, 'user' )
105 or die "print to freeswitch template failed: $!";
109 print $fh qq(</domain>\n);
112 my $scp = new Net::SCP;
113 my $user = $self->option('user')||'root';
114 my $host = $self->machine;
115 my $dir = $self->option('directory');
117 $scp->scp( $fh->filename, "$user\@$host:$dir/$domain.xml" )
118 or return $scp->{errstr};
120 #signal freeswitch to reload config
121 $self->freeswitch_ssh( command => $self->option('reload') );
127 sub freeswitch_template_fillin {
128 my( $self, $svc_phone, $template ) = (shift, shift, shift);
130 $template ||= 'user'; #?
133 my $tt = new Text::Template (
135 SOURCE => $self->option($template.'_template'),
136 DELIMITERS => [ '<%', '%>' ],
139 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
140 my $nibble_rate = $cust_pkg ? $cust_pkg->part_pkg->option('nibble_rate')
143 #false lazinessish w/phone_shellcommands::_export_command
145 'nibble_rate' => $nibble_rate,
146 map { $_ => $svc_phone->getfield($_) } $svc_phone->fields
149 #might as well do em all, they're all going in an XML file as attribs
150 foreach ( keys %hash ) {
151 $hash{$_} =~ s/'/'/g;
152 $hash{$_} =~ s/"/"/g;
160 ##a good idea to queue anything that could fail or take any time
161 #sub shellcommands_queue {
162 # my( $self, $svcnum ) = (shift, shift);
163 # my $queue = new FS::queue {
164 # 'svcnum' => $svcnum,
165 # 'job' => "FS::part_export::freeswitch::ssh_cmd",
167 # $queue->insert( @_ );
170 sub freeswitch_ssh { #method
172 ssh_cmd( user => $self->option('user')||'root',
173 host => $self->machine,
178 sub ssh_cmd { #subroutine, not method
181 open my $def_in, '<', '/dev/null' or die "unable to open /dev/null";
182 my $ssh = Net::OpenSSH->new( $opt->{'user'}.'@'.$opt->{'host'},
183 default_stdin_fh => $def_in,
185 die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
186 my ($output, $errput) = $ssh->capture2( #{stdin_discard => 1},
189 die "Error running SSH command: ". $ssh->error if $ssh->error;
191 #who the fuck knows what freeswitch reload outputs, probably a fucking
192 # ascii advertisement for cluecon
193 #die $errput if $errput;
194 #die $output if $output;