c4750dd5d000e725282da57df6b5a5dbe7e6178a
[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, $login_domain,
100     #  'set_mailbox_status',
101     #  Domain       => $domain,
102     #  Mailbox      => $new_username,
103     #  Other        => $other,
104     #  Other_Bounce => $other,
105     #);
106
107     if ( $old_password ne $new_password ) {
108       cp_command($host, $port, $username, $password, $login_domain,
109         'change_mailbox',
110         Domain    => $domain,
111         Mailbox   => $new_username,
112         Password  => $new_password,
113       );
114     }
115
116     return;
117   }
118   #eof quelle hack
119
120   eval "use Net::APP;";
121
122   my $app = new Net::APP (
123     "$host:$port",
124     User     => $username,
125     Password => $password,
126     Domain   => $login_domain,
127     Timeout  => 60,
128     #Debug    => 1,
129   ) or die "$@\n";
130
131   $app->$method( @args );
132
133   die $app->message."\n" unless $app->ok;
134
135 }
136