This commit was generated by cvs2svn to compensate for changes in r11022,
[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   'debug'      => { label=>'Enable debugging', type=>'checkbox' },
13 ;
14
15 %info = (
16   'svc'      => 'svc_acct',
17   'desc'     => 'Real-time export to Cpanel control panel.',
18   'options'  => \%options,
19   'nodomain' => 'Y',
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.',
21 );
22
23 sub rebless { shift; }
24
25 sub _export_insert { 
26   my($self, $svc_acct) = (shift, shift);
27   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum, 'insert',
28     $svc_acct->domain,
29     $svc_acct->username,
30     $svc_acct->_password,
31     $svc_acct->cust_svc->part_svc->svc,
32   );
33   ref($err_or_queue) ? '' : $err_or_queue;
34 }
35
36 sub _export_replace {
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;
44
45   '';
46
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;
51 }
52
53 sub _export_delete {
54   my( $self, $svc_acct ) = (shift, shift);
55   $err_or_queue = $self->cpanel_queue( $svc_acct->svcnum,
56     'delete', $svc_acct->username
57   );
58   ref($err_or_queue) ? '' : $err_or_queue;
59 }
60
61 sub _export_suspend {
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;
66 }
67
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;
73 }
74
75
76 sub cpanel_queue {
77   my( $self, $svcnum, $method ) = (shift, shift, shift);
78   my $queue = new FS::queue {
79     'svcnum' => $svcnum,
80     'job'    => "FS::part_export::cpanel::cpanel_$method",
81   };
82   $queue->insert(
83     $self->machine,
84     $self->option('user'),
85     $self->option('accesshash'),
86     $self->option('debug'),
87     @_ 
88   ) or $queue;
89 }
90
91
92 sub cpanel_insert { #subroutine, not method
93   my( $machine, $user, $accesshash, $debug ) = splice(@_,0,4);
94
95 #  my $whm = cpanel_connect($machine, $user, $accesshash, $debug);
96 #  warn "  cpanel->createacct ". join(', ', @_). "\n"
97 #    if $debug;
98 #  my $response = $whm->createacct(@_);
99 #  die $whm->{'error'} if $whm->{'error'};
100 #  warn "  cpanel response: $response\n"
101 #    if $debug;
102
103   warn "cpanel_insert: attempting web interface to add POP"
104     if $debug;
105
106   my($domain, $username, $password, $svc) = @_;
107
108   use LWP::UserAgent;
109   use HTTP::Request::Common qw(POST);
110
111   my $url =
112     "http://$user:$accesshash\@$domain:2082/frontend/x/mail/addpop2.html";
113
114   my $ua = LWP::UserAgent->new();
115
116   #$req->authorization_basic($user, $accesshash);
117
118   my $res = $ua->request(
119     POST( $url,
120           [ 
121             'email'    => $username,
122             'domain'   => $domain,
123             'password' => $password,
124             'quota'    => 10, #?
125           ] 
126         )
127   );
128
129   die "Error submitting data to $url: ". $res->status_line
130     unless $res->is_success;
131
132   die "Username in use"
133     if $res->content =~ /exists/;
134
135   die "Account not created: ". $res->content
136     if $res->content =~ /failure/;
137
138 }
139
140 #sub cpanel_replace { #subroutine, not method
141 #}
142
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"
147     if $debug;
148   my $response = $whm->killacct(shift);
149   die $whm->{'error'} if $whm->{'error'};
150   warn "  cpanel response: $response\n"
151     if $debug;
152 }
153
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"
158     if $debug;
159   my $response = $whm->suspend(shift);
160   die $whm->{'error'} if $whm->{'error'};
161   warn "  cpanel response: $response\n"
162     if $debug;
163 }
164
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"
169     if $debug;
170   my $response = $whm->unsuspend(shift);
171   die $whm->{'error'} if $whm->{'error'};
172   warn "  cpanel response: $response\n"
173     if $debug;
174 }
175
176 sub cpanel_connect {
177   my( $host, $user, $accesshash, $debug ) = @_;
178
179   eval "use Cpanel::Accounting;";
180   die $@ if $@;
181
182   warn "creating new Cpanel::Accounting connection to $user@$host\n"
183     if $debug;
184
185   my $whm = new Cpanel::Accounting;
186   $whm->{'host'}       = $host;
187   $whm->{'user'}       = $user;
188   $whm->{'accesshash'} = $accesshash;
189   $whm->{'usessl'}     = 1;
190
191   $whm;
192 }