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