3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
27 package RT::Interface::CLI;
33 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35 # set the version for version checking
36 $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
40 # your exported package globals go here,
41 # as well as any optionally exported functions
42 @EXPORT_OK = qw(&CleanEnv
43 &GetCurrentUser &GetMessageContent &debug &loc);
48 RT::Interface::CLI - helper functions for creating a commandline RT interface
52 use lib "/path/to/rt/libraries/";
54 use RT::Interface::CLI qw(CleanEnv
55 GetCurrentUser GetMessageContent loc);
57 #Clean out all the nasties from the environment
63 #Load RT's config file
66 # Connect to the database. set up loggign
69 #Get the current user all loaded
70 my $CurrentUser = GetCurrentUser();
72 print loc('Hello!'); # Synonym of $CuurentUser->loc('Hello!');
81 ok(require RT::Interface::CLI);
90 Removes some of the nastiest nasties from the user\'s environment.
95 $ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
96 $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
97 $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
98 $ENV{'ENV'} = '' if defined $ENV{'ENV'};
99 $ENV{'IFS'} = '' if defined $ENV{'IFS'};
107 my $CurrentUser; # shared betwen GetCurrentUser and loc
109 # {{{ sub GetCurrentUser
111 =head2 GetCurrentUser
113 Figures out the uid of the current user and returns an RT::CurrentUser object
114 loaded with that user. if the current user isn't found, returns a copy of RT::Nobody.
120 require RT::CurrentUser;
122 #Instantiate a user object
124 my $Gecos= ($^O eq 'MSWin32') ? Win32::LoginName() : (getpwuid($<))[0];
126 #If the current user is 0, then RT will assume that the User object
127 #is that of the currentuser.
129 $CurrentUser = new RT::CurrentUser();
130 $CurrentUser->LoadByGecos($Gecos);
132 unless ($CurrentUser->Id) {
133 $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
136 return($CurrentUser);
145 Synonym of $CurrentUser->loc().
150 die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
151 return $CurrentUser->loc(@_);
158 # {{{ sub GetMessageContent
160 =head2 GetMessageContent
162 Takes two arguments a source file and a boolean "edit". If the source file
163 is undef or "", assumes an empty file. Returns an edited file as an
168 sub GetMessageContent {
169 my %args = ( Source => undef,
172 CurrentUser => undef,
174 my $source = $args{'Source'};
176 my $edit = $args{'Edit'};
178 my $currentuser = $args{'CurrentUser'};
181 use File::Temp qw/ tempfile/;
183 #Load the sourcefile, if it's been handed to us
185 open (SOURCE, "<$source");
189 elsif ($args{'Content'}) {
190 @lines = split('\n',$args{'Content'});
193 my ($fh, $filename) = tempfile();
201 #Edit the file if we need to
204 unless ($ENV{'EDITOR'}) {
205 $RT::Logger->crit('No $EDITOR variable defined'. "\n");
208 system ($ENV{'EDITOR'}, $filename);
211 open (READ, "<$filename");
212 my @newlines = (<READ>);
215 unlink ($filename) unless (debug());
228 $RT::Logger->debug($val."\n");
230 print STDERR "$val\n";
241 eval "require RT::Interface::CLI_Vendor";
242 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/CLI_Vendor.pm});
243 eval "require RT::Interface::CLI_Local";
244 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/CLI_Local.pm});