preliminary plesk support
[freeside.git] / FS / FS / part_export / acct_plesk.pm
1 package FS::part_export::acct_plesk;
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   'URL'       => { label=>'URL' },
11   'login'     => { label=>'Login' },
12   'password'  => { label=>'Password' },
13   'debug'     => { label=>'Enable debugging',
14                     type=>'checkbox'          },
15 ;
16
17 %info = (
18   'svc'    => 'svc_acct',
19   'desc'   => 'Real-time export to Plesk managed mail service',
20   'options'=> \%options,
21   'notes'  => <<'END'
22 Real-time export to
23 <a href="http://www.swsoft.com/">Plesk</a> managed server.
24 Requires installation of
25 <a href="http://search.cpan.org/dist/Net-Plesk">Net::Plesk</a>
26 from CPAN.
27 END
28 );
29
30 sub rebless { shift; }
31
32 # experiment: want the status of these right away (don't want account to
33 # create or whatever and then get error in the queue from dup username or
34 # something), so no queueing
35
36 sub _export_insert {
37   my( $self, $svc_acct ) = (shift, shift);
38
39   $self->_plesk_command( 'mail_add',
40                                     $svc_acct->domain,
41                                     $svc_acct->username,
42                                     $svc_acct->_password,
43                                    ) ||
44   $self->_export_unsuspend($svc_acct);
45 }
46
47 sub _plesk_command {
48   my( $self, $method, $domain, @args ) = @_;
49   
50   local($Net::Plesk::DEBUG) = 1
51     if $self->option('debug');
52
53   my $plesk = new Net::Plesk (
54     'POST'              => $self->option('URL'),
55     ':HTTP_AUTH_LOGIN'  => $self->option('login'),
56     ':HTTP_AUTH_PASSWD' => $self->option('password'),
57   );
58
59   my $dresponse = $plesk->domain_get( $domain );
60   return $dresponse->error unless $dresponse->is_success;
61   my $domainID = $dresponse->id;
62
63   my $response = $plesk->$method($dresponse->id, @args);
64   return $response->errortext unless $response->is_success;
65   '';
66
67 }
68
69 sub _export_replace {
70   my( $self, $new, $old ) = (shift, shift, shift);
71
72   return "can't change domain with Plesk"
73     if $old->domain ne $new->domain;
74   return "can't change username with Plesk"
75     if $old->username ne $new->username;
76   return '' unless $old->_password ne $new->_password;
77
78   $self->_plesk_command( 'mail_set',
79                        $new->domain,
80                        $new->username,
81                        $new->_password,
82                        $old->cust_svc->cust_pkg->susp ? 0 : 1,
83                      );
84 }
85
86 sub _export_delete {
87   my( $self, $svc_acct ) = (shift, shift);
88
89   $self->_plesk_command( 'mail_remove',
90                        $svc_acct->domain,
91                        $svc_acct->username,
92                      );
93 }
94
95 sub _export_suspend {
96   my( $self, $svc_acct ) = (shift, shift);
97
98   $self->_plesk_command( 'mail_set',
99                        $svc_acct->domain,
100                        $svc_acct->username,
101                        $svc_acct->_password,
102                        0,
103                      );
104 }
105
106 sub _export_unsuspend {
107   my( $self, $svc_acct ) = (shift, shift);
108
109   $self->_plesk_command( 'mail_set',
110                        $svc_acct->domain,
111                        $svc_acct->username,
112                        $svc_acct->_password,
113                        1,
114                      );
115 }
116
117 1;
118