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 %>"/>
39 'desc' => 'Provision phone services to FreeSWITCH XML configuration files',
40 'options' => \%options,
42 Export XML account configuration files to FreeSWITCH, one per domain.
44 You will need to enable the svc_phone-domain configuration setting and
45 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
49 sub rebless { shift; }
52 my( $self, $svc_phone ) = ( shift, shift );
54 $self->_export_rebuild_domain($svc_phone);
59 my( $self, $new, $old ) = ( shift, shift, shift );
61 my $error = $self->_export_rebuild_domain($new);
62 return $error if $error;
64 if ( $new->domsvc ne $old->domsvc && $old->domsvc ) {
65 $error = $self->_export_rebuild_domain($old);
66 return $error if $error;
73 my( $self, $svc_phone ) = ( shift, shift );
75 $self->_export_rebuild_domain($svc_phone);
78 sub _export_rebuild_domain {
79 my($self, $svc_phone) = ( shift, shift );
84 #create and copy over file
86 my $tempdir = '%%%FREESIDE_CONF%%%/cache.'. $FS::UID::datasrc;
88 my $domain = $svc_phone->domain or return "domain required";
90 my $fh = new File::Temp(
91 TEMPLATE => "$tempdir/freeswitch.$domain.XXXXXXXX",
96 print $fh qq(<domain name="$domain">\n);
98 my @dom_svc_phone = qsearch( 'svc_phone', { 'domsvc'=>$svc_phone->domsvc } );
100 foreach my $dom_svc_phone (@dom_svc_phone) {
102 print $fh $self->freeswitch_template_fillin( $dom_svc_phone, 'user' )
103 or die "print to freeswitch template failed: $!";
107 print $fh qq(</domain>\n);
110 my $scp = new Net::SCP;
111 my $user = $self->option('user')||'root';
112 my $host = $self->machine;
113 my $dir = $self->option('directory');
115 $scp->scp( $fh->filename, "$user\@$host:$dir/$domain.xml" )
116 or return $scp->{errstr};
118 #signal freeswitch to reload config
119 $self->freeswitch_ssh( command => $self->option('reload') );
125 sub freeswitch_template_fillin {
126 my( $self, $svc_phone, $template ) = (shift, shift, shift);
128 $template ||= 'user'; #?
131 my $tt = new Text::Template (
133 SOURCE => $self->option($template.'_template'),
134 DELIMITERS => [ '<%', '%>' ],
137 #false lazinessish w/phone_shellcommands::_export_command
139 map { $_ => $svc_phone->getfield($_) } $svc_phone->fields
142 #might as well do em all, they're all going in an XML file as attribs
143 foreach ( keys %hash ) {
144 $hash{$_} =~ s/'/'/g;
145 $hash{$_} =~ s/"/"/g;
153 ##a good idea to queue anything that could fail or take any time
154 #sub shellcommands_queue {
155 # my( $self, $svcnum ) = (shift, shift);
156 # my $queue = new FS::queue {
157 # 'svcnum' => $svcnum,
158 # 'job' => "FS::part_export::freeswitch::ssh_cmd",
160 # $queue->insert( @_ );
163 sub freeswitch_ssh { #method
165 ssh_cmd( user => $self->option('user')||'root',
166 host => $self->machine,
171 sub ssh_cmd { #subroutine, not method
174 open my $def_in, '<', '/dev/null' or die "unable to open /dev/null";
175 my $ssh = Net::OpenSSH->new( $opt->{'user'}.'@'.$opt->{'host'},
176 default_stdin_fh => $def_in,
178 die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
179 my ($output, $errput) = $ssh->capture2( #{stdin_discard => 1},
182 die "Error running SSH command: ". $ssh->error if $ssh->error;
184 #who the fuck knows what freeswitch reload outputs, probably a fucking
185 # ascii advertisement for cluecon
186 #die $errput if $errput;
187 #die $output if $output;