big signup server cleanups. uses Storable for network protocol now.
[freeside.git] / fs_signup / FS-SignupClient / SignupClient.pm
1 package FS::SignupClient;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $fs_signupd_socket);
5 use Exporter;
6 use Socket;
7 use FileHandle;
8 use IO::Handle;
9 use Storable qw(nstore_fd fd_retrieve);
10
11 $VERSION = '0.02';
12
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw( signup_info new_customer );
15
16 $fs_signupd_socket = "/usr/local/freeside/fs_signupd_socket";
17
18 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
19 $ENV{'SHELL'} = '/bin/sh';
20 $ENV{'IFS'} = " \t\n";
21 $ENV{'CDPATH'} = '';
22 $ENV{'ENV'} = '';
23 $ENV{'BASH_ENV'} = '';
24
25 my $freeside_uid = scalar(getpwnam('freeside'));
26 die "not running as the freeside user\n" if $> != $freeside_uid;
27
28 =head1 NAME
29
30 FS::SignupClient - Freeside signup client API
31
32 =head1 SYNOPSIS
33
34   use FS::SignupClient qw( signup_info new_customer );
35
36   ( $locales, $packages, $pops ) = signup_info;
37
38   $error = new_customer ( {
39     'first'            => $first,
40     'last'             => $last,
41     'ss'               => $ss,
42     'comapny'          => $company,
43     'address1'         => $address1,
44     'address2'         => $address2,
45     'city'             => $city,
46     'county'           => $county,
47     'state'            => $state,
48     'zip'              => $zip,
49     'country'          => $country,
50     'daytime'          => $daytime,
51     'night'            => $night,
52     'fax'              => $fax,
53     'payby'            => $payby,
54     'payinfo'          => $payinfo,
55     'paydate'          => $paydate,
56     'payname'          => $payname,
57     'invoicing_list'   => $invoicing_list,
58     'referral_custnum' => $referral_custnum,
59     'pkgpart'          => $pkgpart,
60     'username'         => $username,
61     '_password'        => $password,
62     'sec_phrase'       => $sec_phrase,
63     'popnum'           => $popnum,
64   } );
65
66 =head1 DESCRIPTION
67
68 This module provides an API for a remote signup server.
69
70 It needs to be run as the freeside user.  Because of this, the program which
71 calls these subroutines should be written very carefully.
72
73 =head1 SUBROUTINES
74
75 =over 4
76
77 =item signup_info
78
79 Returns three array references of hash references.
80
81 The first set of hash references is of allowable locales.  Each hash reference
82 has the following keys:
83   taxnum
84   state
85   county
86   country
87
88 The second set of hash references is of allowable packages.  Each hash
89 reference has the following keys:
90   pkgpart
91   pkg
92
93 The third set of hash references is of allowable POPs (Points Of Presence).
94 Each hash reference has the following keys:
95   popnum
96   city
97   state
98   ac
99   exch
100
101 =cut
102
103 sub signup_info {
104   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
105   connect(SOCK, sockaddr_un($fs_signupd_socket)) or die "connect: $!";
106   print SOCK "signup_info\n";
107   SOCK->flush;
108
109   my $init_data = fd_retrieve(\*SOCK);
110   close SOCK;
111
112   (map { $init_data->{$_} } qw( cust_main_county part_pkg svc_acct_pop ) ),
113   $init_data;
114
115 }
116
117 =item new_customer HASHREF
118
119 Adds a customer to the remote Freeside system.  Requires a hash reference as
120 a paramater with the following keys:
121   first
122   last
123   ss
124   comapny
125   address1
126   address2
127   city
128   county
129   state
130   zip
131   country
132   daytime
133   night
134   fax
135   payby
136   payinfo
137   paydate
138   payname
139   invoicing_list
140   referral_custnum
141   pkgpart
142   username
143   _password
144   popnum
145
146 Returns a scalar error message, or the empty string for success.
147
148 =cut
149
150 sub new_customer {
151   my $hashref = shift;
152
153   #things that aren't necessary in base class, but are for signup server
154 #  return "Passwords don't match"
155 #    if $hashref->{'_password'} ne $hashref->{'_password2'}
156   return "Empty password" unless $hashref->{'_password'};
157   return "No POP selected" unless $hashref->{'popnum'};
158
159   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
160   connect(SOCK, sockaddr_un($fs_signupd_socket)) or die "connect: $!";
161   print SOCK "new_customer\n";
162
163   my $signup_data = { map { $_ => $hashref->{$_} } qw(
164     first last ss company address1 address2 city county state zip country
165     daytime night fax payby payinfo paydate payname invoicing_list
166     referral_custnum pkgpart username _password popnum
167   ) };
168
169   #
170   nstore_fd($signup_data, \*SOCK) or die "can't send customer signup: $!";
171   SOCK->flush;
172
173   chop( my $error = <SOCK> );
174   $error;
175 }
176
177 =back
178
179 =head1 BUGS
180
181 =head1 SEE ALSO
182
183 L<fs_signupd>, L<FS::cust_main>
184
185 =cut
186
187 1;
188