merging RT 4.0.6
[freeside.git] / rt / lib / RT / Interface / CLI.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
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
16 # from www.gnu.org.
17 #
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.
22 #
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.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
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.)
37 #
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.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::Interface::CLI;
50 use strict;
51 use RT;
52
53 use base 'Exporter';
54 our @EXPORT_OK = qw(CleanEnv GetCurrentUser GetMessageContent debug loc);
55
56 =head1 NAME
57
58   RT::Interface::CLI - helper functions for creating a commandline RT interface
59
60 =head1 SYNOPSIS
61
62   use lib "/path/to/rt/libraries/";
63
64   use RT::Interface::CLI  qw(CleanEnv 
65                            GetCurrentUser GetMessageContent loc);
66
67   #Clean out all the nasties from the environment
68   CleanEnv();
69
70   #let's talk to RT'
71   use RT;
72
73   #Load RT's config file
74   RT::LoadConfig();
75
76   # Connect to the database. set up loggign
77   RT::Init();
78
79   #Get the current user all loaded
80   my $CurrentUser = GetCurrentUser();
81
82   print loc('Hello!'); # Synonym of $CuurentUser->loc('Hello!');
83
84 =head1 DESCRIPTION
85
86
87 =head1 METHODS
88
89
90 =cut
91
92
93 =head2 CleanEnv
94
95 Removes some of the nastiest nasties from the user\'s environment.
96
97 =cut
98
99 sub CleanEnv {
100     $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
101     $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
102     $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
103     $ENV{'ENV'} = '' if defined $ENV{'ENV'};
104     $ENV{'IFS'} = ''            if defined $ENV{'IFS'};
105 }
106
107
108
109
110 {
111
112     my $CurrentUser; # shared betwen GetCurrentUser and loc
113
114
115 =head2 GetCurrentUser
116
117   Figures out the uid of the current user and returns an RT::CurrentUser object
118 loaded with that user.  if the current user isn't found, returns a copy of RT::Nobody.
119
120 =cut
121
122 sub GetCurrentUser  {
123     
124     require RT::CurrentUser;
125     
126     #Instantiate a user object
127     
128     my $Gecos= ($^O eq 'MSWin32') ? Win32::LoginName() : (getpwuid($<))[0];
129
130     #If the current user is 0, then RT will assume that the User object
131     #is that of the currentuser.
132
133     $CurrentUser = RT::CurrentUser->new();
134     $CurrentUser->LoadByGecos($Gecos);
135     
136     unless ($CurrentUser->Id) {
137         $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
138     }
139
140     return($CurrentUser);
141 }
142
143
144
145 =head2 loc
146
147   Synonym of $CurrentUser->loc().
148
149 =cut
150
151 sub loc {
152     die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
153     return $CurrentUser->loc(@_);
154 }
155
156 }
157
158
159
160 =head2 GetMessageContent
161
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 
164 array of lines.
165
166 =cut
167
168 sub GetMessageContent {
169     my %args = (  Source => undef,
170                   Content => undef,
171                   Edit => undef,
172                   CurrentUser => undef,
173                  @_);
174     my $source = $args{'Source'};
175
176     my $edit = $args{'Edit'};
177     
178     my $currentuser = $args{'CurrentUser'};
179     my @lines;
180
181     use File::Temp qw/ tempfile/;
182     
183     #Load the sourcefile, if it's been handed to us
184     if ($source) {
185         open( SOURCE, '<', $source ) or die $!;
186         @lines = (<SOURCE>) or die $!;
187         close (SOURCE) or die $!;
188     }
189     elsif ($args{'Content'}) {
190         @lines = split('\n',$args{'Content'});
191     }
192     #get us a tempfile.
193     my ($fh, $filename) = tempfile();
194         
195     #write to a tmpfile
196     for (@lines) {
197         print $fh $_;
198     }
199     close ($fh) or die $!;
200     
201     #Edit the file if we need to
202     if ($edit) {        
203
204         unless ($ENV{'EDITOR'}) {
205             $RT::Logger->crit('No $EDITOR variable defined');
206             return undef;
207         }
208         system ($ENV{'EDITOR'}, $filename);
209     }   
210     
211     open( READ, '<', $filename ) or die $!;
212     my @newlines = (<READ>);
213     close (READ) or die $!;
214
215     unlink ($filename) unless (debug());
216     return(\@newlines);
217     
218 }
219
220
221
222 sub debug {
223     my $val = shift;
224     my ($debug);
225     if ($val) {
226         $RT::Logger->debug($val);
227         if ($debug) {
228             print STDERR "$val\n";
229         }
230     }
231     if ($debug) {
232         return(1);
233     }   
234 }
235
236 sub ShowHelp {
237     my $self = shift;
238     my %args = @_;
239     require Pod::Usage;
240     Pod::Usage::pod2usage(
241         -message => $args{'Message'},
242         -exitval => $args{'ExitValue'} || 0, 
243         -verbose => 99,
244         -sections => $args{'Sections'} || ($args{'ExitValue'}
245             ? 'NAME|USAGE'
246             : 'NAME|USAGE|OPTIONS|DESCRIPTION'
247         ),
248     );
249 }
250
251 RT::Base->_ImportOverlays();
252
253 1;