1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Interface/CLI.pm,v 1.1 2002-08-12 06:17:08 ivan Exp $
2 # RT is (c) 1996-2001 Jesse Vincent <jesse@fsck.com>
4 package RT::Interface::CLI;
11 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
13 # set the version for version checking
14 $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
18 # your exported package globals go here,
19 # as well as any optionally exported functions
20 @EXPORT_OK = qw(&CleanEnv &LoadConfig &DBConnect
21 &GetCurrentUser &GetMessageContent &debug);
26 RT::Interface::CLI - helper functions for creating a commandline RT interface
30 use lib "!!RT_LIB_PATH!!";
31 use lib "!!RT_ETC_PATH!!";
33 use RT::Interface::CLI qw(CleanEnv LoadConfig DBConnect
34 GetCurrentUser GetMessageContent);
36 #Clean out all the nasties from the environment
39 #Load etc/config.pm and drop privs
42 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
46 #Get the current user all loaded
47 my $CurrentUser = GetCurrentUser();
56 ok(require RT::TestHarness);
57 ok(require RT::Interface::CLI);
66 Removes some of the nastiest nasties from the user\'s environment.
71 $ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
72 $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
73 $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
74 $ENV{'ENV'} = '' if defined $ENV{'ENV'};
75 $ENV{'IFS'} = '' if defined $ENV{'IFS'};
82 Loads RT's config file and then drops setgid privileges.
88 #This drags in RT's config.pm
97 Calls RT::Init, which creates a database connection and then creates $RT::Nobody
110 # {{{ sub GetCurrentUser
112 =head2 GetCurrentUser
114 Figures out the uid of the current user and returns an RT::CurrentUser object
115 loaded with that user. if the current user isn't found, returns a copy of RT::Nobody.
120 my ($Gecos, $CurrentUser);
122 require RT::CurrentUser;
124 #Instantiate a user object
126 $Gecos=(getpwuid($<))[0];
128 #If the current user is 0, then RT will assume that the User object
129 #is that of the currentuser.
131 $CurrentUser = new RT::CurrentUser();
132 $CurrentUser->LoadByGecos($Gecos);
134 unless ($CurrentUser->Id) {
135 $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
137 return($CurrentUser);
141 # {{{ sub GetMessageContent
143 =head2 GetMessageContent
145 Takes two arguments a source file and a boolean "edit". If the source file
146 is undef or "", assumes an empty file. Returns an edited file as an
151 sub GetMessageContent {
152 my %args = ( Source => undef,
155 CurrentUser => undef,
157 my $source = $args{'Source'};
159 my $edit = $args{'Edit'};
161 my $currentuser = $args{'CurrentUser'};
164 use File::Temp qw/ tempfile/;
166 #Load the sourcefile, if it's been handed to us
168 open (SOURCE, "<$source");
172 elsif ($args{'Content'}) {
173 @lines = split('\n',$args{'Content'});
176 my ($fh, $filename) = tempfile();
184 #Edit the file if we need to
187 unless ($ENV{'EDITOR'}) {
188 $RT::Logger->crit('No $EDITOR variable defined'. "\n");
191 system ($ENV{'EDITOR'}, $filename);
194 open (READ, "<$filename");
195 my @newlines = (<READ>);
198 unlink ($filename) unless (debug());
211 $RT::Logger->debug($val."\n");
213 print STDERR "$val\n";