1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Interface::CLI;
55 our @EXPORT_OK = qw(CleanEnv GetCurrentUser GetMessageContent debug loc);
59 RT::Interface::CLI - helper functions for creating a commandline RT interface
63 use lib "/path/to/rt/libraries/";
65 use RT::Interface::CLI qw(CleanEnv
66 GetCurrentUser GetMessageContent loc);
68 #Clean out all the nasties from the environment
74 #Load RT's config file
77 # Connect to the database. set up loggign
80 #Get the current user all loaded
81 my $CurrentUser = GetCurrentUser();
83 print loc('Hello!'); # Synonym of $CuurentUser->loc('Hello!');
96 Removes some of the nastiest nasties from the user's environment.
101 $ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
102 $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
103 $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
104 $ENV{'ENV'} = '' if defined $ENV{'ENV'};
105 $ENV{'IFS'} = '' if defined $ENV{'IFS'};
113 my $CurrentUser; # shared betwen GetCurrentUser and loc
116 =head2 GetCurrentUser
118 Figures out the uid of the current user and returns an RT::CurrentUser object
119 loaded with that user. if the current user isn't found, returns a copy of RT::Nobody.
125 require RT::CurrentUser;
127 #Instantiate a user object
129 my $Gecos= ($^O eq 'MSWin32') ? Win32::LoginName() : (getpwuid($<))[0];
131 #If the current user is 0, then RT will assume that the User object
132 #is that of the currentuser.
134 $CurrentUser = RT::CurrentUser->new();
135 $CurrentUser->LoadByGecos($Gecos);
137 unless ($CurrentUser->Id) {
138 $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
141 return($CurrentUser);
148 Synonym of $CurrentUser->loc().
153 die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
154 return $CurrentUser->loc(@_);
161 =head2 GetMessageContent
163 Takes two arguments a source file and a boolean "edit". If the source file
164 is undef or "", assumes an empty file. Returns an edited file as an
169 sub GetMessageContent {
170 my %args = ( Source => undef,
173 CurrentUser => undef,
175 my $source = $args{'Source'};
177 my $edit = $args{'Edit'};
179 my $currentuser = $args{'CurrentUser'};
182 use File::Temp qw/ tempfile/;
184 #Load the sourcefile, if it's been handed to us
186 open( SOURCE, '<', $source ) or die $!;
187 @lines = (<SOURCE>) or die $!;
188 close (SOURCE) or die $!;
190 elsif ($args{'Content'}) {
191 @lines = split('\n',$args{'Content'});
194 my ($fh, $filename) = tempfile();
200 close ($fh) or die $!;
202 #Edit the file if we need to
205 unless ($ENV{'EDITOR'}) {
206 $RT::Logger->crit('No $EDITOR variable defined');
209 system ($ENV{'EDITOR'}, $filename);
212 open( READ, '<', $filename ) or die $!;
213 my @newlines = (<READ>);
214 close (READ) or die $!;
216 unlink ($filename) unless (debug());
227 $RT::Logger->debug($val);
229 print STDERR "$val\n";
241 Pod::Usage::pod2usage(
242 -message => $args{'Message'},
243 -exitval => $args{'ExitValue'} || 0,
245 -sections => $args{'Sections'} || ($args{'ExitValue'}
247 : 'NAME|USAGE|OPTIONS|DESCRIPTION'
252 RT::Base->_ImportOverlays();