freebsd portability fix
[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   'payment_info'    => 'MyAccount/payment_info',
28   'process_payment' => 'MyAccount/process_payment',
29   'signup_info'     => 'Signup/signup_info',
30   'new_customer'    => 'Signup/new_customer',
31 );
32 @EXPORT_OK = keys %autoload;
33
34 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
35 $ENV{'SHELL'} = '/bin/sh';
36 $ENV{'IFS'} = " \t\n";
37 $ENV{'CDPATH'} = '';
38 $ENV{'ENV'} = '';
39 $ENV{'BASH_ENV'} = '';
40
41 my $freeside_uid = scalar(getpwnam('freeside'));
42 die "not running as the freeside user\n" if $> != $freeside_uid;
43
44 =head1 NAME
45
46 FS::SelfService - Freeside self-service API
47
48 =head1 SYNOPSIS
49
50 =head1 DESCRIPTION
51
52 Use this API to implement your own client "self-service" module.
53
54 If you just want to customize the look of the existing "self-service" module,
55 see XXXX instead.
56
57 =head1 FUNCTIONS
58
59 =over 4
60
61 =item passwd
62
63 Returns the empty value on success, or an error message on errors.
64
65 =cut
66
67 foreach my $autoload ( keys %autoload ) {
68
69   my $eval =
70   "sub $autoload { ". '
71                    my $param;
72                    if ( ref($_[0]) ) {
73                      $param = shift;
74                    } else {
75                      $param = { @_ };
76                    }
77
78                    $param->{_packet} = \''. $autoload{$autoload}. '\';
79
80                    simple_packet($param);
81                  }';
82
83   eval $eval;
84   die $@ if $@;
85
86 }
87
88 sub simple_packet {
89   my $packet = shift;
90   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
91   connect(SOCK, sockaddr_un($socket)) or die "connect: $!";
92   nstore_fd($packet, \*SOCK) or die "can't send packet: $!";
93   SOCK->flush;
94
95   #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
96
97   #block until there is a message on socket
98 #  my $w = new IO::Select;
99 #  $w->add(\*SOCK);
100 #  my @wait = $w->can_read;
101   my $return = fd_retrieve(\*SOCK) or die "error reading result: $!";
102   die $return->{'_error'} if defined $return->{_error} && $return->{_error};
103
104   $return;
105 }
106
107 =back
108
109 =head1 BUGS
110
111 =head1 SEE ALSO
112
113 L<freeside-selfservice-clientd>, L<freeside-selfservice-server>
114
115 =cut
116
117 1;
118