import rt 3.8.10
[freeside.git] / rt / lib / RT / Interface / CLI.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2011 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 use strict;
50
51 use RT;
52 package RT::Interface::CLI;
53
54
55
56 BEGIN {
57     use base 'Exporter';
58     use vars qw ($VERSION  @EXPORT @EXPORT_OK %EXPORT_TAGS);
59     
60     # set the version for version checking
61     $VERSION = do { my @r = (q$Revision: 1.1.1.10 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
62
63     # your exported package globals go here,
64     # as well as any optionally exported functions
65     @EXPORT_OK   = qw(&CleanEnv 
66                       &GetCurrentUser &GetMessageContent &debug &loc);
67 }
68
69 =head1 NAME
70
71   RT::Interface::CLI - helper functions for creating a commandline RT interface
72
73 =head1 SYNOPSIS
74
75   use lib "/path/to/rt/libraries/";
76
77   use RT::Interface::CLI  qw(CleanEnv 
78                            GetCurrentUser GetMessageContent loc);
79
80   #Clean out all the nasties from the environment
81   CleanEnv();
82
83   #let's talk to RT'
84   use RT;
85
86   #Load RT's config file
87   RT::LoadConfig();
88
89   # Connect to the database. set up loggign
90   RT::Init();
91
92   #Get the current user all loaded
93   my $CurrentUser = GetCurrentUser();
94
95   print loc('Hello!'); # Synonym of $CuurentUser->loc('Hello!');
96
97 =head1 DESCRIPTION
98
99
100 =head1 METHODS
101
102
103 =cut
104
105
106 =head2 CleanEnv
107
108 Removes some of the nastiest nasties from the user\'s environment.
109
110 =cut
111
112 sub CleanEnv {
113     $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
114     $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
115     $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
116     $ENV{'ENV'} = '' if defined $ENV{'ENV'};
117     $ENV{'IFS'} = ''            if defined $ENV{'IFS'};
118 }
119
120
121
122
123 {
124
125     my $CurrentUser; # shared betwen GetCurrentUser and loc
126
127 # {{{ sub GetCurrentUser 
128
129 =head2 GetCurrentUser
130
131   Figures out the uid of the current user and returns an RT::CurrentUser object
132 loaded with that user.  if the current user isn't found, returns a copy of RT::Nobody.
133
134 =cut
135
136 sub GetCurrentUser  {
137     
138     require RT::CurrentUser;
139     
140     #Instantiate a user object
141     
142     my $Gecos= ($^O eq 'MSWin32') ? Win32::LoginName() : (getpwuid($<))[0];
143
144     #If the current user is 0, then RT will assume that the User object
145     #is that of the currentuser.
146
147     $CurrentUser = new RT::CurrentUser();
148     $CurrentUser->LoadByGecos($Gecos);
149     
150     unless ($CurrentUser->Id) {
151         $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
152     }
153
154     return($CurrentUser);
155 }
156 # }}}
157
158
159 # {{{ sub loc 
160
161 =head2 loc
162
163   Synonym of $CurrentUser->loc().
164
165 =cut
166
167 sub loc {
168     die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
169     return $CurrentUser->loc(@_);
170 }
171 # }}}
172
173 }
174
175
176 # {{{ sub GetMessageContent
177
178 =head2 GetMessageContent
179
180 Takes two arguments a source file and a boolean "edit".  If the source file
181 is undef or "", assumes an empty file.  Returns an edited file as an 
182 array of lines.
183
184 =cut
185
186 sub GetMessageContent {
187     my %args = (  Source => undef,
188                   Content => undef,
189                   Edit => undef,
190                   CurrentUser => undef,
191                  @_);
192     my $source = $args{'Source'};
193
194     my $edit = $args{'Edit'};
195     
196     my $currentuser = $args{'CurrentUser'};
197     my @lines;
198
199     use File::Temp qw/ tempfile/;
200     
201     #Load the sourcefile, if it's been handed to us
202     if ($source) {
203     open( SOURCE, '<', $source ) or die $!;
204         @lines = (<SOURCE>) or die $!;
205         close (SOURCE) or die $!;
206     }
207     elsif ($args{'Content'}) {
208         @lines = split('\n',$args{'Content'});
209     }
210     #get us a tempfile.
211     my ($fh, $filename) = tempfile();
212         
213     #write to a tmpfile
214     for (@lines) {
215         print $fh $_;
216     }
217     close ($fh) or die $!;
218     
219     #Edit the file if we need to
220     if ($edit) {        
221
222         unless ($ENV{'EDITOR'}) {
223             $RT::Logger->crit('No $EDITOR variable defined');
224             return undef;
225         }
226         system ($ENV{'EDITOR'}, $filename);
227     }   
228     
229     open( READ, '<', $filename ) or die $!;
230     my @newlines = (<READ>);
231     close (READ) or die $!;
232
233     unlink ($filename) unless (debug());
234     return(\@newlines);
235     
236 }
237
238 # }}}
239
240 # {{{ sub debug
241
242 sub debug {
243     my $val = shift;
244     my ($debug);
245     if ($val) {
246         $RT::Logger->debug($val);
247         if ($debug) {
248             print STDERR "$val\n";
249         }
250     }
251     if ($debug) {
252         return(1);
253     }   
254 }
255
256 # }}}
257
258
259 RT::Base->_ImportOverlays();
260
261 1;