RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / phone_shellcommands.pm
1 package FS::part_export::phone_shellcommands;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Tie::IxHash;
6 use String::ShellQuote;
7 use FS::part_export;
8 use Carp qw(carp);
9
10 @ISA = qw(FS::part_export);
11
12 #TODO
13 #- modify command (get something from freepbx for changing PINs)
14 #- suspension/unsuspension
15
16 tie my %options, 'Tie::IxHash',
17   'user'       => { label=>'Remote username', default=>'root', },
18   'useradd'    => { label=>'Insert command', }, 
19   'userdel'    => { label=>'Delete command', }, 
20   'usermod'    => { label=>'Modify command', }, 
21   'suspend'    => { label=>'Suspension command', }, 
22   'unsuspend'  => { label=>'Unsuspension command', }, 
23   'mac_insert' => { label=>'Device MAC address insert command', },
24   'mac_delete' => { label=>'Device MAC address delete command', },
25 ;
26
27 %info = (
28   'svc'     => [qw( svc_phone part_device )],
29   'desc'    => 'Run remote commands via SSH, for phone numbers',
30   'options' => \%options,
31   'notes'   => <<'END'
32 Run remote commands via SSH, for phone numbers.  You will need to
33 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
34 <BR><BR>Use these buttons for some useful presets:
35 <UL>
36   <LI>
37     <INPUT TYPE="button" VALUE="FreePBX (build_exten CLI module needed)" onClick='
38       this.form.user.value = "root";
39       this.form.useradd.value = "build_exten.php --create --exten $phonenum --directdid 1$phonenum --sip-secret $sip_password --name $cust_name --vm-password $pin && /usr/share/asterisk/bin/module_admin reload";
40       this.form.userdel.value = "build_exten.php --delete --exten $phonenum && /usr/share/asterisk/bin/module_admin reload";
41       this.form.usermod.value = "build_exten.php --modify --exten $new_phonenum --directdid 1$new_phonenum --sip-secret $new_sip_password --name $new_cust_name --vm-password $new_pin && /usr/share/asterisk/bin/module_admin reload";
42       this.form.suspend.value = "";
43       this.form.unsuspend.value = "";
44     '> (Important note: Reduce freeside-queued "max_kids" to 1 when using FreePBX integration)
45   </UL>
46
47 The following variables are available for interpolation (prefixed with new_ or
48 old_ for replace operations):
49 <UL>
50   <LI><code>$countrycode</code> - Country code
51   <LI><code>$phonenum</code> - Phone number
52   <LI><code>$sip_password</code> - SIP secret (quoted for the shell)
53   <LI><code>$pin</code> - Personal identification number
54   <LI><code>$cust_name</code> - Customer name (quoted for the shell)
55   <LI><code>$pkgnum</code> - Internal package number
56   <LI><code>$custnum</code> - Internal customer number
57   <LI><code>$phone_name</code> - Phone name (quoted for the shell)
58   <LI><code>$mac_addr</code> - MAC address (Device MAC address insert and delete commands only)
59   <LI><code>$devicename</code> - Device type (Device type insert and delete commands only)
60 </UL>
61 END
62 );
63
64 sub rebless { shift; }
65
66 sub _export_insert {
67   my $self = shift;
68   $self->_export_command('useradd', @_);
69 }
70
71 sub _export_delete {
72   my $self = shift;
73   $self->_export_command('userdel', @_);
74 }
75
76 sub _export_suspend {
77   my $self = shift;
78   $self->_export_command('suspend', @_);
79 }
80
81 sub _export_unsuspend {
82   my $self = shift;
83   $self->_export_command('unsuspend', @_);
84 }
85
86 sub export_device_insert {
87   my( $self, $svc_phone, $phone_device ) = @_;
88   $self->_export_command('mac_insert', $svc_phone,
89                            mac_addr   => $phone_device->mac_addr,
90                            devicename => $phone_device->part_device->devicename,
91                         );
92 }
93
94 sub export_device_delete {
95   my( $self, $svc_phone, $phone_device ) = @_;
96   $self->_export_command('mac_delete', $svc_phone,
97                            mac_addr   => $phone_device->mac_addr,
98                            devicename => $phone_device->part_device->devicename,
99                         );
100 }
101
102 sub _export_command {
103   my ( $self, $action, $svc_phone, %addl_vars) = @_;
104   my $command = $self->option($action);
105   return '' if $command =~ /^\s*$/;
106
107   if ( $FS::svc_Common::noexport_hack ) {
108     carp "_export_command($action) suppressed by noexport_hack"
109       if $self->option('debug');
110     return;
111   }
112
113   #set variable for the command
114   no strict 'vars';
115   {
116     no strict 'refs';
117     ${$_} = $svc_phone->getfield($_) foreach $svc_phone->fields;
118     ${$_} = $addl_vars{$_} foreach keys %addl_vars;
119   }
120   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
121   my $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
122   my $custnum = $cust_pkg ? $cust_pkg->custnum : '';
123   my $cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
124   $cust_name = shell_quote $cust_name;
125   my $sip_password = shell_quote $svc_phone->sip_password;
126   my $phone_name = shell_quote $svc_phone->phone_name;
127   #done setting variables for the command
128
129   $self->shellcommands_queue( $svc_phone->svcnum,
130     user         => $self->option('user')||'root',
131     host         => $self->machine,
132     command      => eval(qq("$command")),
133   );
134 }
135
136 sub _export_replace {
137   my($self, $new, $old ) = (shift, shift, shift);
138   my $command = $self->option('usermod');
139   
140   #set variable for the command
141   no strict 'vars';
142   {
143     no strict 'refs';
144     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
145     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
146   }
147
148   my $old_cust_pkg = $old->cust_svc->cust_pkg;
149   my $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
150   my $old_custnum = $old_cust_pkg ? $old_cust_pkg->custnum : '';
151   my $cust_pkg = $new->cust_svc->cust_pkg;
152   my $new_pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
153   my $new_custnum = $new_cust_pkg ? $new_cust_pkg->custnum : '';
154   my $new_cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
155   $new_cust_name = shell_quote $new_cust_name;
156   #done setting variables for the command
157
158   $self->shellcommands_queue( $new->svcnum,
159     user         => $self->option('user')||'root',
160     host         => $self->machine,
161     command      => eval(qq("$command")),
162   );
163 }
164
165 #a good idea to queue anything that could fail or take any time
166 sub shellcommands_queue {
167   my( $self, $svcnum ) = (shift, shift);
168   my $queue = new FS::queue {
169     'svcnum' => $svcnum,
170     'job'    => "FS::part_export::phone_shellcommands::ssh_cmd",
171   };
172   $queue->insert( @_ );
173 }
174
175 sub ssh_cmd { #subroutine, not method
176   use Net::SSH '0.08';
177   &Net::SSH::ssh_cmd( { @_ } );
178 }
179
180 1;