summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/acct_opensrs.pm
blob: c131900d3a97c7183dcee068381a3a6ec4e956b7 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package FS::part_export::acct_opensrs;

use strict;
use vars qw( %info $DEBUG );
use base qw( FS::part_export );
use Tie::IxHash;
use Data::Dumper;

tie my %options, 'Tie::IxHash',
  'Environment' => { label    => 'Environment',
                     type     => 'select',
                     options  => [ 'test', 'production' ],
                     default  => 'test'
                   },
  'Domain'      => { label    => 'Administrative domain',
                     type     => 'text',
                   },
  'User'        => { label    => 'Administrative user',
                     type     => 'text',
                   },
  'Password'    => { label    => 'Password',
                     type     => 'text',
                   },
  'Debug'       => { label    => 'Debug level',
                     type     => 'select',
                     options  => [ 0, 1, 2, 3, 4 ],
                   },
;

%info = (
  'svc'     => 'svc_acct',
  'desc'    => 'Configure OpenSRS hosted email services',
  'options' => \%options,
  'no_machine' => 1,
  'notes'   => <<'END'
<p>
Provision email services (POP3/IMAP boxes) through an OpenSRS reseller account.
This requires the <b>Net::OpenSRS::Email_APP</b> library.
</p>
<p>
The <I>Domain</i>, <i>User</i>, and <i>Password</i> accounts are for an
account with company-level admin privileges (or domain-level, if you will
only manage a single domain with each export). <i>Environment</i> determines 
whether to manage test accounts or live email accounts.
</p>
<p>
OpenSRS requires every account to be assigned to a workgroup (within its
domain).  This export will create a workgroup for each service definition,
named "svc" + the I<svcpart> value.  This is somewhat arbitrary and may
change in the future.
</p>
END
);

=head2 METHODS

=item app

Returns a L<Net::OpenSRS::Email_APP> handle to the OpenSRS API.

=cut

sub app {
  my $self = shift;
  $DEBUG ||= $self->option('Debug');
  local $@;
  eval "use Net::OpenSRS::Email_APP";
  if ($@) {
    if ($@ =~ /^Can't locate/) {
      die "Net::OpenSRS::Email_APP must be installed to configure accounts.\n";
    } else {
      die $@;
    }
  }
  my %args = map { $_ => $self->option($_) } qw(
    Environment User Domain Password
  );
  warn "Creating APP session.\n" if $DEBUG;
  warn Dumper \%args if $DEBUG > 1;
  my $app = Net::OpenSRS::Email_APP->new(%args);
  if ($app) {
    $app->debug( $DEBUG - 2 ) if $DEBUG > 2;
    warn "Logging in.\n" if $DEBUG;
    my $error = $app->safe_login;
    return $error || $app;
  }
  return;
}

sub _export_insert {
  my $self = shift;
  my $new = shift;
  my $app = $self->app;
  return $app if !ref($app);
  if ($new->isa('FS::svc_acct')) {
    # this may at some point support svc_forward and svc_domain
    my $domain = $new->domain;
    my $username = $new->username;
    warn "Checking mailbox availability: $username\@$domain\n" if $DEBUG;
    my $result = $app->get_mailbox_availability(
      Domain => $domain,
      Mailbox_List => $username,
    );
    if ($app->last_status_code) {
      return $app->last_status_text . ' (checking mailbox availability)';
    }
    if ($result->{AVAILABLE_LIST} eq 'T') {
      return "mailbox unavailable";
    }

    # check existence of workgroup named for the part_svc
    my $svcname = 'svc'.$new->cust_svc->svcpart;
    $result = $app->get_domain_workgroups( Domain => $domain );
    if (! grep {$_->{WORKGROUP} eq $svcname} @$result) {
      warn "Creating workgroup '$svcname'\n" if $DEBUG;
      $result = $app->create_workgroup(
        Domain => $domain,
        Workgroup => $svcname,
      );
      if ($app->last_status_code) {
        return $app->last_status_text . ' (creating workgroup)';
      }
    }
    my %args = $self->mailbox_args($new);
    warn "Creating mailbox\n" if $DEBUG;
    warn Dumper \%args if $DEBUG > 1;
    $result = $app->create_mailbox(%args);
    if ($app->last_status_code) {
      return $app->last_status_text . ' (creating mailbox)';
    }
    return;
  } else {
    return "OpenSRS export doesn't support this service type";
  }
}

sub _export_delete {
  my $self = shift;
  my $old = shift;
  my $app = $self->app;
  return $app if !ref($app);
  if ( $old->isa('FS::svc_acct') ) {
    # does it exist?
    my $domain = $old->domain;
    my $username = $old->username;
    warn "Checking existence of mailbox $username\@$domain\n" if $DEBUG;
    my $result = $app->get_mailbox( Domain => $domain, Mailbox => $username );
    if (!$result) {
      warn "Mailbox not found\n" if $DEBUG;
      return; # nothing to delete
    }
    warn "Deleting mailbox\n" if $DEBUG;
    $result = $app->delete_mailbox( Domain => $domain, Mailbox => $username );
    if ($app->last_status_code) {
      return $app->last_status_text . ' (deleting mailbox)';
    }
    return;
  } else {
    return "OpenSRS export doesn't support this service type";
  }
}

sub _export_replace {
  my $self = shift;
  my ($new, $old) = @_;
  my $app = $self->app;
  return $app if !ref($app);
  if ($new->isa('FS::svc_acct')) {
    my $domain = $old->domain;
    my $username = $old->username;
    warn "Checking existence of mailbox $username\@$domain\n" if $DEBUG;
    my $result = $app->get_mailbox( Domain => $domain, Mailbox => $username );
    if ($app->last_status_code) {
      return $app->last_status_text . ' (checking existence of mailbox)';
    }
    if (!$result) {
      # then the old mailbox was never created; just handle this as an insert
      return $self->export_insert($new);
    }
    # check validity of the change
    if ($new->domain ne $domain) {
      # OpenSRS doesn't allow moving a mailbox across domains.  We could 
      # delete the old account and create a new one but that risks losing 
      # mail, so we're going to just refuse the request.
      return "can't move mailbox across domains";
    }
    # rename account if necessary
    if ($new->username ne $username) {
      warn "Checking mailbox availability: ".$new->username."\@$domain\n"
        if $DEBUG;
      my $result = $app->get_mailbox_availability(
        Domain => $domain,
        Mailbox_List => $new->username,
      );
      if ($app->last_status_code) {
        return $app->last_status_text . ' (checking mailbox availability)';
      }
      if ($result->{AVAILABLE_LIST} eq 'T') {
        return "mailbox unavailable";
      }
      warn "Renaming mailbox $username to ".$new->username."\n" if $DEBUG;
      $app->rename_mailbox(
        Domain => $domain,
        Old_Mailbox => $old->username,
        New_Mailbox => $new->username,
      );
      if ($app->last_status_code) {
        return $app->last_status_text . ' (renaming mailbox)';
      }
    }
    # then make other changes
    warn "Modifying mailbox\n" if $DEBUG;
    my %args = $self->mailbox_args($new);
    warn Dumper \%args if $DEBUG > 1;
    $app->change_mailbox(%args);
    if ($app->last_status_code) {
      return $app->last_status_text . ' (changing mailbox properties)';
    }
    return;
  } else {
    return "OpenSRS export doesn't support this service type";
  }
}

sub _export_suspend {
  my $self = shift;
  my $svc = shift;
  my $unsuspend = shift || 0;
  my $app = $self->app;
  return $app if !ref($app);
  # XXX apply this to all mail services? or should we have an option
  # to restrict it?
  warn "Changing mailbox suspension state\n" if $DEBUG;
  my %args = ( Domain  => $svc->domain, Mailbox => $svc->username );
  foreach (qw(SMTPIn SMTPRelay IMAP POP Webmail)) {
    $args{$_} = $unsuspend ? 'F' : 'T'; # True = suspended
  }
  warn Dumper \%args if $DEBUG > 1;
  $app->set_mailbox_suspension(%args);
  if ($app->last_status_code) {
    return $app->last_status_text . ' (setting mailbox suspension)';
  }
  return;
}

sub _export_unsuspend {
  my ($self, $svc) = @_;
  $self->export_suspend($svc, 1);
}

=item mailbox_args SVC_ACCT

Returns a list of arguments to the C<create_mailbox> or C<change_mailbox>
methods for the supplied service.

=cut

sub mailbox_args {
  my ($self, $svc) = @_;
  my $cust_pkg = $svc->cust_svc->cust_pkg;
  my $cust = $cust_pkg->contact_obj || $cust_pkg->cust_main;
  return (
    Domain        => $svc->domain,
    Workgroup     => 'svc'.$svc->cust_svc->svcpart,
    Mailbox       => $svc->username,
    Password      => $svc->_password,
    First_Name    => $cust->first,
    Last_Name     => $cust->last,
    # other optional fields: FilterOnly, Title, Timezone, Lang,
    # Phone, Fax, Spam_Tag, Spam_Folder, Spam_Level
    # can add these if necessary...
  );
}

# convenience methods on $app

sub Net::OpenSRS::Email_APP::last_status_code {
  my $self = shift;
  $self->{status_code};
}

sub Net::OpenSRS::Email_APP::last_status_text {
  my $self = shift;
  $self->{status_text};
}

# workaround for a serious bug
sub Net::OpenSRS::Email_APP::safe_login {
  my $self = shift;
  local $Net::OpenSRS::Email_APP::Debug = 1;
  local $Net::OpenSRS::Email_APP::Emit_Debug = sub {
    if ($_[0] =~ /^read: \[ER (\d+) (.*)\r/) {
      die "$2\n";
    }
  };
  local $@ = '';
  local $SIG{__DIE__};
  eval { $self->login; };
  return $@;
}

1;