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