no DISTINCT ON in MySQL makes kittens cry
[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 and proper <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.7:Documentation:Administration:acct_plesk.pm">configuration</a>.
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   eval "use Net::Plesk;";
51   return $@ if $@;
52   
53   local($Net::Plesk::DEBUG) = 1
54     if $self->option('debug');
55
56   my $plesk = new Net::Plesk (
57     'POST'              => $self->option('URL'),
58     ':HTTP_AUTH_LOGIN'  => $self->option('login'),
59     ':HTTP_AUTH_PASSWD' => $self->option('password'),
60   );
61
62   my $dresponse = $plesk->domain_get( $domain );
63   return $dresponse->errortext unless $dresponse->is_success;
64   my $domainID = $dresponse->id;
65
66   my $response = $plesk->$method($dresponse->id, @args);
67   return $response->errortext unless $response->is_success;
68   '';
69
70 }
71
72 sub _export_replace {
73   my( $self, $new, $old ) = (shift, shift, shift);
74
75   return "can't change domain with Plesk"
76     if $old->domain ne $new->domain;
77   return "can't change username with Plesk"
78     if $old->username ne $new->username;
79   return '' unless $old->_password ne $new->_password;
80
81   $self->_plesk_command( 'mail_set',
82                        $new->domain,
83                        $new->username,
84                        $new->_password,
85                        $old->cust_svc->cust_pkg->susp ? 0 : 1,
86                      );
87 }
88
89 sub _export_delete {
90   my( $self, $svc_acct ) = (shift, shift);
91
92   $self->_plesk_command( 'mail_remove',
93                        $svc_acct->domain,
94                        $svc_acct->username,
95                      );
96 }
97
98 sub _export_suspend {
99   my( $self, $svc_acct ) = (shift, shift);
100
101   $self->_plesk_command( 'mail_set',
102                        $svc_acct->domain,
103                        $svc_acct->username,
104                        $svc_acct->_password,
105                        0,
106                      );
107 }
108
109 sub _export_unsuspend {
110   my( $self, $svc_acct ) = (shift, shift);
111
112   $self->_plesk_command( 'mail_set',
113                        $svc_acct->domain,
114                        $svc_acct->username,
115                        $svc_acct->_password,
116                        1,
117                      );
118 }
119
120 1;
121