disable order package button until a package has been selected
[freeside.git] / fs_selfadmin / FS-MailAdminServer / MailAdminClient.pm
1 package FS::MailAdminClient;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $fs_mailadmind_socket);
5 use Exporter;
6 use Socket;
7 use FileHandle;
8 use IO::Handle;
9
10 $VERSION = '0.01';
11
12 @ISA = qw( Exporter );
13 @EXPORT_OK = qw( signup_info authenticate list_packages list_mailboxes delete_mailbox password_mailbox add_mailbox list_forwards list_pkg_forwards delete_forward add_forward new_customer );
14
15 $fs_mailadmind_socket = "/usr/local/freeside/fs_mailadmind_socket";
16
17 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
18 $ENV{'SHELL'} = '/bin/sh';
19 $ENV{'IFS'} = " \t\n";
20 $ENV{'CDPATH'} = '';
21 $ENV{'ENV'} = '';
22 $ENV{'BASH_ENV'} = '';
23
24 my $freeside_uid = scalar(getpwnam('freeside'));
25 die "not running as the freeside user\n" if $> != $freeside_uid;
26
27 =head1 NAME
28
29 FS::MailAdminClient - Freeside mail administration client API
30
31 =head1 SYNOPSIS
32
33   use FS::MailAdminClient qw( signup_info list_mailboxes  new_customer );
34
35   ( $locales, $packages, $pops ) = signup_info;
36
37   ( $accounts ) = list_mailboxes;
38
39   $error = new_customer ( {
40     'first'          => $first,
41     'last'           => $last,
42     'ss'             => $ss,
43     'comapny'        => $company,
44     'address1'       => $address1,
45     'address2'       => $address2,
46     'city'           => $city,
47     'county'         => $county,
48     'state'          => $state,
49     'zip'            => $zip,
50     'country'        => $country,
51     'daytime'        => $daytime,
52     'night'          => $night,
53     'fax'            => $fax,
54     'payby'          => $payby,
55     'payinfo'        => $payinfo,
56     'paydate'        => $paydate,
57     'payname'        => $payname,
58     'invoicing_list' => $invoicing_list,
59     'pkgpart'        => $pkgpart,
60     'username'       => $username,
61     '_password'       => $password,
62     'popnum'         => $popnum,
63   } );
64
65 =head1 DESCRIPTION
66
67 This module provides an API for a remote mail administration server.
68
69 It needs to be run as the freeside user.  Because of this, the program which
70 calls these subroutines should be written very carefully.
71
72 =head1 SUBROUTINES
73
74 =over 4
75
76 =item signup_info
77
78 Returns three array references of hash references.
79
80 The first set of hash references is of allowable locales.  Each hash reference
81 has the following keys:
82   taxnum
83   state
84   county
85   country
86
87 The second set of hash references is of allowable packages.  Each hash
88 reference has the following keys:
89   pkgpart
90   pkg
91
92 The third set of hash references is of allowable POPs (Points Of Presence).
93 Each hash reference has the following keys:
94   popnum
95   city
96   state
97   ac
98   exch
99
100 =cut
101
102 sub signup_info {
103   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
104   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
105   print SOCK "signup_info\n";
106   SOCK->flush;
107
108   chop ( my $n_cust_main_county = <SOCK> );
109   my @cust_main_county = map {
110     chop ( my $taxnum  = <SOCK> ); 
111     chop ( my $state   = <SOCK> ); 
112     chop ( my $county  = <SOCK> ); 
113     chop ( my $country = <SOCK> );
114     {
115       'taxnum'  => $taxnum,
116       'state'   => $state,
117       'county'  => $county,
118       'country' => $country,
119     };
120   } 1 .. $n_cust_main_county;
121
122   chop ( my $n_part_pkg = <SOCK> );
123   my @part_pkg = map {
124     chop ( my $pkgpart = <SOCK> ); 
125     chop ( my $pkg     = <SOCK> ); 
126     {
127       'pkgpart' => $pkgpart,
128       'pkg'     => $pkg,
129     };
130   } 1 .. $n_part_pkg;
131
132   chop ( my $n_svc_acct_pop = <SOCK> );
133   my @svc_acct_pop = map {
134     chop ( my $popnum = <SOCK> ); 
135     chop ( my $city   = <SOCK> ); 
136     chop ( my $state  = <SOCK> ); 
137     chop ( my $ac     = <SOCK> );
138     chop ( my $exch   = <SOCK> );
139     chop ( my $loc    = <SOCK> );
140     {
141       'popnum' => $popnum,
142       'city'   => $city,
143       'state'  => $state,
144       'ac'     => $ac,
145       'exch'   => $exch,
146       'loc'    => $loc,
147     };
148   } 1 .. $n_svc_acct_pop;
149
150   close SOCK;
151
152   \@cust_main_county, \@part_pkg, \@svc_acct_pop;
153 }
154
155 =item authenticate
156
157 Authentictes against a service on the remote Freeside system.  Requires a hash
158 reference as a parameter with the following keys:
159     authuser
160     _password
161
162 Returns a scalar error message of the form "authuser OK|FAILED" or an error
163 message.
164
165 =cut
166
167 sub authenticate {
168   my $hashref = shift;
169   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
170   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
171   print SOCK "authenticate", "\n";
172   SOCK->flush;
173
174   print SOCK join("\n", map { $hashref->{$_} } qw(
175     authuser _password
176   ) ), "\n";
177   SOCK->flush;
178
179   chop( my $error = <SOCK> );
180   close SOCK;
181
182   $error;
183 }
184
185 =item list_packages
186
187 Returns one array reference of hash references.
188
189 The set of hash references is of existing packages.  Each hash reference
190 has the following keys:
191   pkgnum
192   domain
193   account
194
195 =cut
196
197 sub list_packages {
198   my $user = shift;
199   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
200   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
201   print SOCK "list_packages\n", $user, "\n";
202   SOCK->flush;
203
204   chop ( my $n_packages = <SOCK> );
205   my @packages = map {
206     chop ( my $pkgnum  = <SOCK> ); 
207     chop ( my $domain  = <SOCK> ); 
208     chop ( my $account = <SOCK> ); 
209     {
210       'pkgnum'  => $pkgnum,
211       'domain'  => $domain,
212       'account' => $account,
213     };
214   } 1 .. $n_packages;
215
216   close SOCK;
217
218   \@packages;
219 }
220
221 =item list_mailboxes
222
223 Returns one array references of hash references.
224
225 The set of hash references is of existing accounts.  Each hash reference
226 has the following keys:
227   svcnum
228   username
229   _password
230
231 =cut
232
233 sub list_mailboxes {
234   my ($user, $package) = @_;
235   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
236   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
237   print SOCK "list_mailboxes\n", $user, "\n", $package, "\n";
238   SOCK->flush;
239
240   chop ( my $n_svc_acct = <SOCK> );
241   my @svc_acct = map {
242     chop ( my $svcnum  = <SOCK> ); 
243     chop ( my $username  = <SOCK> ); 
244     chop ( my $_password   = <SOCK> ); 
245     {
246       'svcnum'  => $svcnum,
247       'username'  => $username,
248       '_password'   => $_password,
249     };
250   } 1 .. $n_svc_acct;
251
252   close SOCK;
253
254   \@svc_acct;
255 }
256
257 =item delete_mailbox
258
259 Deletes a mailbox service from the remote Freeside system.  Requires a hash
260 reference as a paramater with the following keys:
261     authuser
262     account
263
264 Returns a scalar error message, or the empty string for success.
265
266 =cut
267
268 sub delete_mailbox {
269   my $hashref = shift;
270   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
271   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
272   print SOCK "delete_mailbox", "\n";
273   SOCK->flush;
274
275   print SOCK join("\n", map { $hashref->{$_} } qw(
276     authuser account
277   ) ), "\n";
278   SOCK->flush;
279
280   chop( my $error = <SOCK> );
281   close SOCK;
282
283   $error;
284 }
285
286 =item password_mailbox
287
288 Changes the password for a mailbox service on the remote Freeside system.
289   Requires a hash reference as a paramater with the following keys:
290     authuser
291     account
292     _password
293
294 Returns a scalar error message, or the empty string for success.
295
296 =cut
297
298 sub password_mailbox {
299   my $hashref = shift;
300   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
301   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
302   print SOCK "password_mailbox", "\n";
303   SOCK->flush;
304
305   print SOCK join("\n", map { $hashref->{$_} } qw(
306     authuser account _password
307   ) ), "\n";
308   SOCK->flush;
309
310   chop( my $error = <SOCK> );
311   close SOCK;
312
313   $error;
314 }
315
316 =item add_mailbox
317
318 Creates a mailbox service on the remote Freeside system.  Requires a hash
319 reference as a parameter with the following keys:
320     authuser
321     package
322     account
323     _password
324
325 Returns a scalar error message, or the empty string for success.
326
327 =cut
328
329 sub add_mailbox {
330   my $hashref = shift;
331   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
332   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
333   print SOCK "add_mailbox", "\n";
334   SOCK->flush;
335
336   print SOCK join("\n", map { $hashref->{$_} } qw(
337     authuser package account _password
338   ) ), "\n";
339   SOCK->flush;
340
341   chop( my $error = <SOCK> );
342   close SOCK;
343
344   $error;
345 }
346
347 =item list_forwards
348
349 Returns one array references of hash references.
350
351 The set of hash references is of existing forwards.  Each hash reference
352 has the following keys:
353   svcnum
354   dest
355
356 =cut
357
358 sub list_forwards {
359   my ($user, $service) = @_;
360   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
361   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
362   print SOCK "list_forwards\n", $user, "\n", $service, "\n";
363   SOCK->flush;
364
365   chop ( my $n_svc_forward = <SOCK> );
366   my @svc_forward = map {
367     chop ( my $svcnum  = <SOCK> ); 
368     chop ( my $dest  = <SOCK> ); 
369     {
370       'svcnum'  => $svcnum,
371       'dest'  => $dest,
372     };
373   } 1 .. $n_svc_forward;
374
375   close SOCK;
376
377   \@svc_forward;
378 }
379
380 =item list_pkg_forwards
381
382 Returns one array references of hash references.
383
384 The set of hash references is of existing forwards.  Each hash reference
385 has the following keys:
386   svcnum
387   srcsvc
388   dest
389
390 =cut
391
392 sub list_pkg_forwards {
393   my ($user, $package) = @_;
394   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
395   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
396   print SOCK "list_pkg_forwards\n", $user, "\n", $package, "\n";
397   SOCK->flush;
398
399   chop ( my $n_svc_forward = <SOCK> );
400   my @svc_forward = map {
401     chop ( my $svcnum  = <SOCK> ); 
402     chop ( my $srcsvc  = <SOCK> ); 
403     chop ( my $dest  = <SOCK> ); 
404     {
405       'svcnum'  => $svcnum,
406       'srcsvc'  => $srcsvc,
407       'dest'  => $dest,
408     };
409   } 1 .. $n_svc_forward;
410
411   close SOCK;
412
413   \@svc_forward;
414 }
415
416 =item delete_forward
417
418 Deletes a forward service from the remote Freeside system.  Requires a hash
419 reference as a paramater with the following keys:
420     authuser
421     svcnum
422
423 Returns a scalar error message, or the empty string for success.
424
425 =cut
426
427 sub delete_forward {
428   my $hashref = shift;
429   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
430   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
431   print SOCK "delete_forward", "\n";
432   SOCK->flush;
433
434   print SOCK join("\n", map { $hashref->{$_} } qw(
435     authuser svcnum
436   ) ), "\n";
437   SOCK->flush;
438
439   chop( my $error = <SOCK> );
440   close SOCK;
441
442   $error;
443 }
444
445 =item add_forward
446
447 Creates a forward service on the remote Freeside system.  Requires a hash
448 reference as a parameter with the following keys:
449     authuser
450     package
451     source
452     dest
453
454 Returns a scalar error message, or the empty string for success.
455
456 =cut
457
458 sub add_forward {
459   my $hashref = shift;
460   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
461   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
462   print SOCK "add_forward", "\n";
463   SOCK->flush;
464
465   print SOCK join("\n", map { $hashref->{$_} } qw(
466     authuser package source dest
467   ) ), "\n";
468   SOCK->flush;
469
470   chop( my $error = <SOCK> );
471   close SOCK;
472
473   $error;
474 }
475
476 =item new_customer HASHREF
477
478 Adds a customer to the remote Freeside system.  Requires a hash reference as
479 a paramater with the following keys:
480   first
481   last
482   ss
483   comapny
484   address1
485   address2
486   city
487   county
488   state
489   zip
490   country
491   daytime
492   night
493   fax
494   payby
495   payinfo
496   paydate
497   payname
498   invoicing_list
499   pkgpart
500   username
501   _password
502   popnum
503
504 Returns a scalar error message, or the empty string for success.
505
506 =cut
507
508 sub new_customer {
509   my $hashref = shift;
510
511   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
512   connect(SOCK, sockaddr_un($fs_mailadmind_socket)) or die "connect: $!";
513   print SOCK "new_customer\n";
514
515   print SOCK join("\n", map { $hashref->{$_} } qw(
516     first last ss company address1 address2 city county state zip country
517     daytime night fax payby payinfo paydate payname invoicing_list
518     pkgpart username _password popnum
519   ) ), "\n";
520   SOCK->flush;
521
522   chop( my $error = <SOCK> );
523   $error;
524 }
525
526 =back
527
528 =head1 VERSION
529
530 $Id: MailAdminClient.pm,v 1.1 2001-10-18 15:04:54 jeff Exp $
531
532 =head1 BUGS
533
534 =head1 SEE ALSO
535
536 L<fs_signupd>, L<FS::SignupServer>, L<FS::cust_main>
537
538 =cut
539
540 1;
541