7628ff608fee1116b1de1caebdccdc5c55ee483f
[freeside.git] / FS / FS / part_export / cpanel.pm
1 package FS::part_export::cpanel;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 tie my %options, 'Tie::IxHash',
10   'user'       => { label=>'Remote access username' },
11   'accesshash' => { label=>'Remote access key', type=>'textarea' },
12 ;
13
14 %info = (
15   'svc'      => 'svc_acct',
16   'desc'     => 'Real-time export to Cpanel control panel.',
17   'options'  => \%options,
18   'nodomain' => 'Y',
19   'notes'    => 'Real time export to a the <a href="http://www.cpanel.net/">Cpanel</a> control panel software.  Service definition names are exported as Cpanel packages.  Requires installation of the Cpanel::Accounting perl module distributed with Cpanel.',
20 );
21
22 sub rebless { shift; }
23
24 sub _export_insert { 
25   my($self, $svc_acct) = (shift, shift);
26   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum, 'insert',
27     $svc_acct->domain,
28     $svc_acct->username,
29     $svc_acct->_password,
30     $svc_acct->cust_svc->part_svc->svc,
31   );
32   ref($err_or_queue) ? '' : $err_or_queue;
33 }
34
35 sub _export_replace {
36   my( $self, $new, $old ) = (shift, shift, shift);
37   return "can't change username with cpanel"
38     if $old->username ne $new->username;
39   return "can't change password with cpanel"
40     if $old->_passsword ne $new->_password;
41   return "can't change domain with cpanel"
42     if $old->domain ne $new->domain;
43
44   '';
45
46   ##return '' unless $old->_password ne $new->_password;
47   #$err_or_queue = $self->cpanel_queue( $new->svcnum,
48   #  'replace', $new->username, $new->_password );
49   #ref($err_or_queue) ? '' : $err_or_queue;
50 }
51
52 sub _export_delete {
53   my( $self, $svc_acct ) = (shift, shift);
54   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
55     'delete', $svc_acct->username
56   );
57   ref($err_or_queue) ? '' : $err_or_queue;
58 }
59
60 sub _export_suspend {
61   my( $self, $svc_acct ) = (shift, shift);
62   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
63     'suspend', $svc_acct->username );
64   ref($err_or_queue) ? '' : $err_or_queue;
65 }
66
67 sub _export_unsuspend {
68   my( $self, $svc_acct ) = (shift, shift);
69   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
70     'unsuspend', $svc_acct->username );
71   ref($err_or_queue) ? '' : $err_or_queue;
72 }
73
74
75 sub cpanel_queue {
76   my( $self, $svcnum, $method ) = (shift, shift, shift);
77   my $queue = new FS::queue {
78     'svcnum' => $svcnum,
79     'job'    => "FS::part_export::cpanel::cpanel_$method",
80   };
81   $queue->insert(
82     $self->machine,
83     $self->option('user'),
84     $self->option('accesshash'),
85     @_ 
86   ) or $queue;
87 }
88
89
90 sub cpanel_insert { #subroutine, not method
91   my $whm = cpanel_connect(shift, shift, shift);
92   my $response = $whm->createacct(@_);
93   die $whm->{'error'} if $whm->{'error'};
94 }
95
96 #sub cpanel_replace { #subroutine, not method
97 #}
98
99 sub cpanel_delete { #subroutine, not method
100   my $whm = cpanel_connect(shift, shift, shift);
101   my $response = $whm->killacct(shift);
102   die $whm->{'error'} if $whm->{'error'};
103 }
104
105 sub cpanel_suspend { #subroutine, not method
106   my $whm = cpanel_connect(shift, shift, shift);
107   my $response = $whm->suspend(shift);
108   die $whm->{'error'} if $whm->{'error'};
109 }
110
111 sub cpanel_unsuspend { #subroutine, not method
112   my $whm = cpanel_connect(shift, shift, shift);
113   my $response = $whm->unsuspend(shift);
114   die $whm->{'error'} if $whm->{'error'};
115 }
116
117 sub cpanel_connect {
118   my( $host, $user, $accesshash ) = @_;
119
120   eval "use Cpanel::Accounting;";
121   die $@ if $@;
122
123   my $whm = new Cpanel::Accounting;
124   $whm->{'host'}       = $host;
125   $whm->{'user'}       = $user;
126   $whm->{'accesshash'} = $accesshash;
127   $whm->{'usessl'}     = 1;
128
129   $whm;
130 }