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