finish moving signup server functions to self-service interface
[freeside.git] / fs_selfservice / FS-SelfService / SelfService.pm
1 package FS::SelfService;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $socket %autoload );
5 use Exporter;
6 use Socket;
7 use FileHandle;
8 #use IO::Handle;
9 use IO::Select;
10 use Storable qw(nstore_fd fd_retrieve);
11
12 $VERSION = '0.03';
13
14 @ISA = qw( Exporter );
15
16 $socket =  "/usr/local/freeside/selfservice_socket";
17
18 #maybe should ask ClientAPI for this list
19 %autoload = (
20   'passwd'          => 'passwd/passwd',
21   'chfn'            => 'passwd/passwd',
22   'chsh'            => 'passwd/passwd',
23   'login'           => 'MyAccount/login',
24   'customer_info'   => 'MyAccount/customer_info',
25   'invoice'         => 'MyAccount/invoice',
26   'cancel'          => 'MyAccount/cancel',
27   'signup_info'     => 'Signup/signup_info',
28   'new_customer'    => 'Signup/new_customer',
29 );
30 @EXPORT_OK = keys %autoload;
31
32 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
33 $ENV{'SHELL'} = '/bin/sh';
34 $ENV{'IFS'} = " \t\n";
35 $ENV{'CDPATH'} = '';
36 $ENV{'ENV'} = '';
37 $ENV{'BASH_ENV'} = '';
38
39 my $freeside_uid = scalar(getpwnam('freeside'));
40 die "not running as the freeside user\n" if $> != $freeside_uid;
41
42 =head1 NAME
43
44 FS::SelfService - Freeside self-service API
45
46 =head1 SYNOPSIS
47
48 =head1 DESCRIPTION
49
50 Use this API to implement your own client "self-service" module.
51
52 If you just want to customize the look of the existing "self-service" module,
53 see XXXX instead.
54
55 =head1 FUNCTIONS
56
57 =over 4
58
59 =item passwd
60
61 Returns the empty value on success, or an error message on errors.
62
63 =cut
64
65 foreach my $autoload ( keys %autoload ) {
66
67   my $eval =
68   "sub $autoload { ". '
69                    my $param;
70                    if ( ref($_[0]) ) {
71                      $param = shift;
72                    } else {
73                      $param = { @_ };
74                    }
75
76                    $param->{_packet} = \''. $autoload{$autoload}. '\';
77
78                    simple_packet($param);
79                  }';
80
81   eval $eval;
82   die $@ if $@;
83
84 }
85
86 sub simple_packet {
87   my $packet = shift;
88   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
89   connect(SOCK, sockaddr_un($socket)) or die "connect: $!";
90   nstore_fd($packet, \*SOCK) or die "can't send packet: $!";
91   SOCK->flush;
92
93   #shoudl trap: Magic number checking on storable file failed at blib/lib/Storable.pm (autosplit into blib/lib/auto/Storable/fd_retrieve.al) line 337, at /usr/local/share/perl/5.6.1/FS/SelfService.pm line 71
94
95   #block until there is a message on socket
96 #  my $w = new IO::Select;
97 #  $w->add(\*SOCK);
98 #  my @wait = $w->can_read;
99   my $return = fd_retrieve(\*SOCK) or die "error reading result: $!";
100   die $return->{'_error'} if defined $return->{_error} && $return->{_error};
101
102   $return;
103 }
104
105 =back
106
107 =head1 BUGS
108
109 =head1 SEE ALSO
110
111 L<freeside-selfservice-clientd>, L<freeside-selfservice-server>
112
113 =cut
114
115 1;
116