1 package FS::part_export::cpanel;
3 use vars qw(@ISA %info);
7 @ISA = qw(FS::part_export);
9 tie my %options, 'Tie::IxHash',
10 'user' => { label=>'Remote access username' },
11 'accesshash' => { label=>'Remote access key', type=>'textarea' },
12 'debug' => { label=>'Enable debugging', type=>'checkbox' },
17 'desc' => 'Real-time export to Cpanel control panel.',
18 'options' => \%options,
20 '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.',
23 sub rebless { shift; }
26 my($self, $svc_acct) = (shift, shift);
27 $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum, 'insert',
31 $svc_acct->cust_svc->part_svc->svc,
33 ref($err_or_queue) ? '' : $err_or_queue;
37 my( $self, $new, $old ) = (shift, shift, shift);
38 return "can't change username with cpanel"
39 if $old->username ne $new->username;
40 return "can't change password with cpanel"
41 if $old->_passsword ne $new->_password;
42 return "can't change domain with cpanel"
43 if $old->domain ne $new->domain;
47 ##return '' unless $old->_password ne $new->_password;
48 #$err_or_queue = $self->cpanel_queue( $new->svcnum,
49 # 'replace', $new->username, $new->_password );
50 #ref($err_or_queue) ? '' : $err_or_queue;
54 my( $self, $svc_acct ) = (shift, shift);
55 $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
56 'delete', $svc_acct->username
58 ref($err_or_queue) ? '' : $err_or_queue;
62 my( $self, $svc_acct ) = (shift, shift);
63 $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
64 'suspend', $svc_acct->username );
65 ref($err_or_queue) ? '' : $err_or_queue;
68 sub _export_unsuspend {
69 my( $self, $svc_acct ) = (shift, shift);
70 $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
71 'unsuspend', $svc_acct->username );
72 ref($err_or_queue) ? '' : $err_or_queue;
77 my( $self, $svcnum, $method ) = (shift, shift, shift);
78 my $queue = new FS::queue {
80 'job' => "FS::part_export::cpanel::cpanel_$method",
84 $self->option('user'),
85 $self->option('accesshash'),
86 $self->option('debug'),
92 sub cpanel_insert { #subroutine, not method
93 my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
95 # my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
96 # warn " cpanel->createacct ". join(', ', @_). "\n"
98 # my $response = $whm->createacct(@_);
99 # die $whm->{'error'} if $whm->{'error'};
100 # warn " cpanel response: $response\n"
103 warn "cpanel_insert: attempting web interface to add POP"
106 my($domain, $username, $password, $svc) = @_;
109 use HTTP::Request::Common qw(POST);
112 "http://$user:$accesshash\@$domain:2082/frontend/x/mail/addpop2.html";
114 my $ua = LWP::UserAgent->new();
116 #$req->authorization_basic($user, $accesshash);
118 my $res = $ua->request(
121 'email' => $username,
123 'password' => $password,
129 die "Error submitting data to $url: ". $res->status_line
130 unless $res->is_success;
132 die "Username in use"
133 if $res->content =~ /exists/;
135 die "Account not created: ". $res->content
136 if $res->content =~ /failure/;
140 #sub cpanel_replace { #subroutine, not method
143 sub cpanel_delete { #subroutine, not method
144 my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
145 my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
146 warn " cpanel->killacct ". join(', ', @_). "\n"
148 my $response = $whm->killacct(shift);
149 die $whm->{'error'} if $whm->{'error'};
150 warn " cpanel response: $response\n"
154 sub cpanel_suspend { #subroutine, not method
155 my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
156 my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
157 warn " cpanel->suspend ". join(', ', @_). "\n"
159 my $response = $whm->suspend(shift);
160 die $whm->{'error'} if $whm->{'error'};
161 warn " cpanel response: $response\n"
165 sub cpanel_unsuspend { #subroutine, not method
166 my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
167 my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
168 warn " cpanel->unsuspend ". join(', ', @_). "\n"
170 my $response = $whm->unsuspend(shift);
171 die $whm->{'error'} if $whm->{'error'};
172 warn " cpanel response: $response\n"
177 my( $host, $user, $accesshash, $debug ) = @_;
179 eval "use Cpanel::Accounting;";
182 warn "creating new Cpanel::Accounting connection to $user@$host\n"
185 my $whm = new Cpanel::Accounting;
186 $whm->{'host'} = $host;
187 $whm->{'user'} = $user;
188 $whm->{'accesshash'} = $accesshash;
189 $whm->{'usessl'} = 1;