1 package FS::part_export::forward_shellcommands;
4 use vars qw(@ISA %info);
8 @ISA = qw(FS::part_export);
10 tie my %options, 'Tie::IxHash',
11 'user' => { label=>'Remote username', default=>'root' },
12 'useradd' => { label=>'Insert command',
15 'userdel' => { label=>'Delete command',
18 'usermod' => { label=>'Modify command',
24 'svc' => 'svc_forward',
25 'desc' => 'Run remote commands via SSH, for forwards',
26 'options' => \%options,
28 Run remote commands via SSH, for forwards. You will need to
29 <a href="../docs/ssh.html">setup SSH for unattended operation</a>.
30 <BR><BR>Use these buttons for some useful presets:
33 <INPUT TYPE="button" VALUE="text vpopmail maintenance" onClick='
34 this.form.useradd.value = "[ -d /home/vpopmail/domains/$domain/$username ] && { echo \"$destination\" > /home/vpopmail/domains/$domain/$username/.qmail; chown vpopmail:vchkpw /home/vpopmail/domains/$domain/$username/.qmail; }";
35 this.form.userdel.value = "rm /home/vpopmail/domains/$domain/$username/.qmail";
36 this.form.usermod.value = "mv /home/vpopmail/domains/$old_domain/$old_username/.qmail /home/vpopmail/domains/$new_domain/$new_username; [ \"$old_destination\" != \"$new_destination\" ] && { echo \"$new_destination\" > /home/vpopmail/domains/$new_domain/$new_username/.qmail; chown vpopmail:vchkpw /home/vpopmail/domains/$new_domain/$new_username/.qmail; }";
39 <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
40 this.form.useradd.value = "";
41 this.form.userdel.value = "";
42 this.form.usermod.value = "";
45 The following variables are available for interpolation (prefixed with
46 <code>new_</code> or <code>old_</code> for replace operations):
48 <LI><code>$username</code>
49 <LI><code>$domain</code>
50 <LI><code>$destination</code> - forward destination
51 <LI>All other fields in <a href="../docs/schema.html#svc_forward">svc_forward</a> are also available.
56 sub rebless { shift; }
60 $self->_export_command('useradd', @_);
65 $self->_export_command('userdel', @_);
69 my ( $self, $action, $svc_forward ) = (shift, shift, shift);
70 my $command = $self->option($action);
72 #set variable for the command
76 ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
79 my $svc_acct = $svc_forward->srcsvc_acct;
80 $username = $svc_acct->username;
81 $domain = $svc_acct->domain;
82 if ($svc_forward->dstsvc_acct) {
83 $destination = $svc_forward->dstsvc_acct->email;
85 $destination = $svc_forward->dst;
88 #done setting variables for the command
90 $self->shellcommands_queue( $svc_forward->svcnum,
91 user => $self->option('user')||'root',
92 host => $self->machine,
93 command => eval(qq("$command")),
98 my( $self, $new, $old ) = (shift, shift, shift);
99 my $command = $self->option('usermod');
101 #set variable for the command
105 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
106 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
109 my $old_svc_acct = $old->srcsvc_acct;
110 $old_username = $old_svc_acct->username;
111 $old_domain = $old_svc_acct->domain;
112 if ($old->dstsvc_acct) {
113 $old_destination = $old->dstsvc_acct->email;
115 $old_destination = $old->dst;
118 my $new_svc_acct = $new->srcsvc_acct;
119 $new_username = $new_svc_acct->username;
120 $new_domain = $new_svc_acct->domain;
122 $new_destination = $new->dstsvc_acct->email;
124 $new_destination = $new->dst;
127 #done setting variables for the command
129 $self->shellcommands_queue( $new->svcnum,
130 user => $self->option('user')||'root',
131 host => $self->machine,
132 command => eval(qq("$command")),
136 #a good idea to queue anything that could fail or take any time
137 sub shellcommands_queue {
138 my( $self, $svcnum ) = (shift, shift);
139 my $queue = new FS::queue {
141 'job' => "FS::part_export::forward_shellcommands::ssh_cmd",
143 $queue->insert( @_ );
146 sub ssh_cmd { #subroutine, not method
148 &Net::SSH::ssh_cmd( { @_ } );
151 #sub shellcommands_insert { #subroutine, not method
153 #sub shellcommands_replace { #subroutine, not method
155 #sub shellcommands_delete { #subroutine, not method