summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/cp.pm
blob: 2ae97e12d99e515f3a2f7a0f378dd66824d281b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package FS::part_export::cp;

use vars qw(@ISA %info);
use Tie::IxHash;
use FS::part_export;

@ISA = qw(FS::part_export);

tie my %options, 'Tie::IxHash',
  'port'      => { label=>'Port number' },
  'username'  => { label=>'Username' },
  'password'  => { label=>'Password' },
  'domain'    => { label=>'Domain' },
  'workgroup' => { label=>'Default Workgroup' },
;

%info = (
  'svc'    => 'svc_acct',
  'desc'   => 'Real-time export to Critical Path Account Provisioning Protocol',
  'options'=> \%options,
  'default_svc_class' => 'Email',
  'notes'  => <<'END'
Real-time export to
<a href="http://www.cp.net/">Critial Path Account Provisioning Protocol</a>.
Requires installation of
<a href="http://search.cpan.org/dist/Net-APP">Net::APP</a>
from CPAN.
END
);

sub rebless { shift; }

sub _export_insert {
  my( $self, $svc_acct ) = (shift, shift);
  $self->cp_queue( $svc_acct->svcnum, 'create_mailbox',
    'Mailbox'   => $svc_acct->username,
    'Password'  => $svc_acct->_password,
    'Workgroup' => $self->option('workgroup'),
    'Domain'    => $svc_acct->domain,
  );
}

sub _export_replace {
  my( $self, $new, $old ) = (shift, shift, shift);
  return "can't change domain with Critical Path"
    if $old->domain ne $new->domain;
  return "can't change username with Critical Path" #CP no longer supports this
    if $old->username ne $new->username;
  return '' unless $old->_password ne $new->_password;
  $self->cp_queue( $new->svcnum, 'replace', $new->domain,
    $old->username, $new->username, $old->_password, $new->_password );
}

sub _export_delete {
  my( $self, $svc_acct ) = (shift, shift);
  $self->cp_queue( $svc_acct->svcnum, 'delete_mailbox',
    'Mailbox'   => $svc_acct->username,
    'Domain'    => $svc_acct->domain,
  );
}

sub _export_suspend {
  my( $self, $svc_acct ) = (shift, shift);
  $self->cp_queue( $svc_acct->svcnum, 'set_mailbox_status',
    'Mailbox'       => $svc_acct->username,
    'Domain'        => $svc_acct->domain,
    'OTHER'         => 'T',
    'OTHER_SUSPEND' => 'T',
  );
}

sub _export_unsuspend {
  my( $self, $svc_acct ) = (shift, shift);
  $self->cp_queue( $svc_acct->svcnum, 'set_mailbox_status',
    'Mailbox'       => $svc_acct->username,
    'Domain'        => $svc_acct->domain,
    'PAYMENT'       => 'F',
    'OTHER'         => 'F',
    'OTHER_SUSPEND' => 'F',
    'OTHER_BOUNCE'  => 'F',
  );
}

sub cp_queue {
  my( $self, $svcnum, $method ) = (shift, shift, shift);
  my $queue = new FS::queue {
    'svcnum' => $svcnum,
    'job'    => 'FS::part_export::cp::cp_command',
  };
  $queue->insert(
    $self->machine,
    $self->option('port'),
    $self->option('username'),
    $self->option('password'),
    $self->option('domain'),
    $method,
    @_,
  );
}

sub cp_command { #subroutine, not method
  my($host, $port, $username, $password, $login_domain, $method, @args) = @_;

  #quelle hack
  if ( $method eq 'replace' ) {
  
    my( $domain, $old_username, $new_username, $old_password, $new_password)
      = @args;

    if ( $old_username ne $new_username ) {
      cp_command($host, $port, $username, $password, 'rename_mailbox',
        Domain        => $domain,
        Old_Mailbox   => $old_username,
        New_Mailbox   => $new_username,
      );
    }

    #my $other = 'F';
    if ( $new_password =~ /^\*SUSPENDED\* (.*)$/ ) {
      $new_password = $1;
    #  $other = 'T';
    }
    #cp_command($host, $port, $username, $password, $login_domain,
    #  'set_mailbox_status',
    #  Domain       => $domain,
    #  Mailbox      => $new_username,
    #  Other        => $other,
    #  Other_Bounce => $other,
    #);

    if ( $old_password ne $new_password ) {
      cp_command($host, $port, $username, $password, $login_domain,
        'change_mailbox',
        Domain    => $domain,
        Mailbox   => $new_username,
        Password  => $new_password,
      );
    }

    return;
  }
  #eof quelle hack

  eval "use Net::APP;";

  my $app = new Net::APP (
    "$host:$port",
    User     => $username,
    Password => $password,
    Domain   => $login_domain,
    Timeout  => 60,
    #Debug    => 1,
  ) or die "$@\n";

  $app->$method( @args );

  die $app->message."\n" unless $app->ok;

}

1;