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