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