58ac85e8a5b67259f631804bd969fc8f8050c5ff
[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 cp_queue {
39   my( $self, $svcnum, $method ) = (shift, shift, shift);
40   my $queue = new FS::queue {
41     'svcnum' => $svcnum,
42     'job'    => 'FS::part_export::cp::cp_command',
43   };
44   $queue->insert(
45     $self->option('host'),
46     $self->option('port'),
47     $self->option('username'),
48     $self->option('password'),
49     $self->option('domain'),
50     $method,
51     @_,
52   );
53 }
54
55 sub cp_command { #subroutine, not method
56   my($host, $port, $username, $password, $login_domain, $method, @args) = @_;
57
58   #quelle hack
59   if ( $method eq 'replace' ) {
60   
61     my( $domain, $old_username, $new_username, $old_password, $new_password)
62       = @args;
63
64     if ( $old_username ne $new_username ) {
65       cp_command($host, $port, $username, $password, 'rename_mailbox',
66         Domain        => $domain,
67         Old_Mailbox   => $old_username,
68         New_Mailbox   => $new_username,
69       );
70     }
71
72     if ( $new_password =~ /^\*SUSPENDED\* (.*)$/ ) {
73       $new_password = $1;
74       cp_command($host, $port, $username, $password, 'set_mailbox_status',
75         Domain       => $domain,
76         Mailbox      => $new_username,
77         Other        => 'T',
78         Other_Bounce => 'T',
79       );
80     } else {
81       cp_command($host, $port, $username, $password, 'set_mailbox_status',
82         Domain       => $domain,
83         Mailbox      => $new_username,
84         Other        => 'F',
85         Other_Bounce => 'F',
86       );
87     }
88
89     if ( $old_password ne $new_password ) {
90       cp_command($host, $port, $username, $password, 'change_mailbox',
91         Domain    => $domain,
92         Mailbox   => $new_username,
93         Password  => $new_password,
94       );
95     }
96
97     return;
98   }
99   #eof quelle hack
100
101   eval "use Net::APP;";
102
103   my $app = new Net::APP (
104     "$host:$port",
105     User     => $username,
106     Password => $password,
107     Domain   => $login_domain,
108     Timeout  => 60,
109     #Debug    => 1,
110   ) or die "$@\n";
111
112   $app->$method( @args );
113
114   die $app->message."\n" unless $app->ok;
115
116 }
117