This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / rt / lib / RT / Interface / CLI.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 use strict;
25
26 use RT;
27 package RT::Interface::CLI;
28
29
30
31 BEGIN {
32     use Exporter ();
33     use vars qw ($VERSION  @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
34     
35     # set the version for version checking
36     $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
37     
38     @ISA         = qw(Exporter);
39     
40     # your exported package globals go here,
41     # as well as any optionally exported functions
42     @EXPORT_OK   = qw(&CleanEnv 
43                       &GetCurrentUser &GetMessageContent &debug &loc);
44 }
45
46 =head1 NAME
47
48   RT::Interface::CLI - helper functions for creating a commandline RT interface
49
50 =head1 SYNOPSIS
51
52   use lib "/path/to/rt/libraries/";
53
54   use RT::Interface::CLI  qw(CleanEnv 
55                            GetCurrentUser GetMessageContent loc);
56
57   #Clean out all the nasties from the environment
58   CleanEnv();
59
60   #let's talk to RT'
61   use RT;
62
63   #Load RT's config file
64   RT::LoadConfig();
65
66   # Connect to the database. set up loggign
67   RT::Init();
68
69   #Get the current user all loaded
70   my $CurrentUser = GetCurrentUser();
71
72   print loc('Hello!'); # Synonym of $CuurentUser->loc('Hello!');
73
74 =head1 DESCRIPTION
75
76
77 =head1 METHODS
78
79 =begin testing
80
81 ok(require RT::Interface::CLI);
82
83 =end testing
84
85 =cut
86
87
88 =head2 CleanEnv
89
90 Removes some of the nastiest nasties from the user\'s environment.
91
92 =cut
93
94 sub CleanEnv {
95     $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
96     $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
97     $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
98     $ENV{'ENV'} = '' if defined $ENV{'ENV'};
99     $ENV{'IFS'} = ''            if defined $ENV{'IFS'};
100 }
101
102
103
104
105 {
106
107     my $CurrentUser; # shared betwen GetCurrentUser and loc
108
109 # {{{ sub GetCurrentUser 
110
111 =head2 GetCurrentUser
112
113   Figures out the uid of the current user and returns an RT::CurrentUser object
114 loaded with that user.  if the current user isn't found, returns a copy of RT::Nobody.
115
116 =cut
117
118 sub GetCurrentUser  {
119     
120     require RT::CurrentUser;
121     
122     #Instantiate a user object
123     
124     my $Gecos= ($^O eq 'MSWin32') ? Win32::LoginName() : (getpwuid($<))[0];
125
126     #If the current user is 0, then RT will assume that the User object
127     #is that of the currentuser.
128
129     $CurrentUser = new RT::CurrentUser();
130     $CurrentUser->LoadByGecos($Gecos);
131     
132     unless ($CurrentUser->Id) {
133         $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
134     }
135
136     return($CurrentUser);
137 }
138 # }}}
139
140
141 # {{{ sub loc 
142
143 =head2 loc
144
145   Synonym of $CurrentUser->loc().
146
147 =cut
148
149 sub loc {
150     die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
151     return $CurrentUser->loc(@_);
152 }
153 # }}}
154
155 }
156
157
158 # {{{ sub GetMessageContent
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");
186         @lines = (<SOURCE>);
187         close (SOURCE);
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);
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'. "\n");
206             return undef;
207         }
208         system ($ENV{'EDITOR'}, $filename);
209     }   
210     
211     open (READ, "<$filename");
212     my @newlines = (<READ>);
213     close (READ);
214
215     unlink ($filename) unless (debug());
216     return(\@newlines);
217     
218 }
219
220 # }}}
221
222 # {{{ sub debug
223
224 sub debug {
225     my $val = shift;
226     my ($debug);
227     if ($val) {
228         $RT::Logger->debug($val."\n");
229         if ($debug) {
230             print STDERR "$val\n";
231         }
232     }
233     if ($debug) {
234         return(1);
235     }   
236 }
237
238 # }}}
239
240
241 eval "require RT::Interface::CLI_Vendor";
242 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/CLI_Vendor.pm});
243 eval "require RT::Interface::CLI_Local";
244 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/CLI_Local.pm});
245
246 1;