add suspend/unsuspend capability to CP export
[freeside.git] / FS / FS / part_export / cp.pm
1 package FS::part_export::cp;
2
3 use vars qw(@ISA);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 sub rebless { shift; }
9
10 sub _export_insert {
11   my( $self, $svc_acct ) = (shift, shift);
12   $self->cp_queue( $svc_acct->svcnum, 'create_mailbox',
13     'Mailbox'   => $svc_acct->username,
14     'Password'  => $svc_acct->_password,
15     'Workgroup' => $self->option('workgroup'),
16     'Domain'    => $svc_acct->domain,
17   );
18 }
19
20 sub _export_replace {
21   my( $self, $new, $old ) = (shift, shift, shift);
22   return "can't change domain with Critical Path"
23     if $old->domain ne $new->domain;
24   return '' unless $old->username  ne $new->username
25                 || $old->_password ne $new->_password;
26   $self->cp_queue( $new->svcnum, 'replace', $new->domain,
27     $old->username, $new->username, $old->_password, $new->_password );
28 }
29
30 sub _export_delete {
31   my( $self, $svc_acct ) = (shift, shift);
32   $self->cp_queue( $svc_acct->svcnum, 'delete_mailbox',
33     'Mailbox'   => $svc_acct->username,
34     'Domain'    => $svc_acct->domain,
35   );
36 }
37
38 sub _export_suspend {
39   my( $self, $svc_acct ) = (shift, shift);
40   $self->cp_queue( $svc_acct->svcnum, 'set_mailbox_status',
41     'Mailbox'       => $svc_acct->username,
42     'Domain'        => $svc_acct->domain,
43     'OTHER'         => 'T',
44     'OTHER_SUSPEND' => 'T',
45   );
46 }
47
48 sub _export_unsuspend {
49   my( $self, $svc_acct ) = (shift, shift);
50   $self->cp_queue( $svc_acct->svcnum, 'set_mailbox_status',
51     'Mailbox'       => $svc_acct->username,
52     'Domain'        => $svc_acct->domain,
53     'PAYMENT'       => 'F',
54     'OTHER'         => 'F',
55     'OTHER_SUSPEND' => 'F',
56     'OTHER_BOUNCE'  => 'F',
57   );
58 }
59
60 sub cp_queue {
61   my( $self, $svcnum, $method ) = (shift, shift, shift);
62   my $queue = new FS::queue {
63     'svcnum' => $svcnum,
64     'job'    => 'FS::part_export::cp::cp_command',
65   };
66   $queue->insert(
67     $self->machine,
68     $self->option('port'),
69     $self->option('username'),
70     $self->option('password'),
71     $self->option('domain'),
72     $method,
73     @_,
74   );
75 }
76
77 sub cp_command { #subroutine, not method
78   my($host, $port, $username, $password, $login_domain, $method, @args) = @_;
79
80   #quelle hack
81   if ( $method eq 'replace' ) {
82   
83     my( $domain, $old_username, $new_username, $old_password, $new_password)
84       = @args;
85
86     if ( $old_username ne $new_username ) {
87       cp_command($host, $port, $username, $password, 'rename_mailbox',
88         Domain        => $domain,
89         Old_Mailbox   => $old_username,
90         New_Mailbox   => $new_username,
91       );
92     }
93
94     my $other = 'F';
95     if ( $new_password =~ /^\*SUSPENDED\* (.*)$/ ) {
96       $new_password = $1;
97       $other = 'T';
98     }
99     cp_command($host, $port, $username, $password, 'set_mailbox_status',
100       Domain       => $domain,
101       Mailbox      => $new_username,
102       Other        => $other,
103       Other_Bounce => $other,
104     );
105
106     if ( $old_password ne $new_password ) {
107       cp_command($host, $port, $username, $password, 'change_mailbox',
108         Domain    => $domain,
109         Mailbox   => $new_username,
110         Password  => $new_password,
111       );
112     }
113
114     return;
115   }
116   #eof quelle hack
117
118   eval "use Net::APP;";
119
120   my $app = new Net::APP (
121     "$host:$port",
122     User     => $username,
123     Password => $password,
124     Domain   => $login_domain,
125     Timeout  => 60,
126     #Debug    => 1,
127   ) or die "$@\n";
128
129   $app->$method( @args );
130
131   die $app->message."\n" unless $app->ok;
132
133 }
134