import rt 2.0.14
[freeside.git] / rt / lib / RT / Interface / CLI.pm
1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Interface/CLI.pm,v 1.1 2002-08-12 06:17:08 ivan Exp $
2 # RT is (c) 1996-2001 Jesse Vincent <jesse@fsck.com>
3
4 package RT::Interface::CLI;
5
6 use strict;
7
8
9 BEGIN {
10     use Exporter ();
11     use vars qw ($VERSION  @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12     
13     # set the version for version checking
14     $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
15     
16     @ISA         = qw(Exporter);
17     
18     # your exported package globals go here,
19     # as well as any optionally exported functions
20     @EXPORT_OK   = qw(&CleanEnv &LoadConfig &DBConnect 
21                       &GetCurrentUser &GetMessageContent &debug);
22 }
23
24 =head1 NAME
25
26   RT::Interface::CLI - helper functions for creating a commandline RT interface
27
28 =head1 SYNOPSIS
29
30   use lib "!!RT_LIB_PATH!!";
31   use lib "!!RT_ETC_PATH!!";
32
33   use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
34                            GetCurrentUser GetMessageContent);
35
36   #Clean out all the nasties from the environment
37   CleanEnv();
38
39   #Load etc/config.pm and drop privs
40   LoadConfig();
41
42   #Connect to the database and get RT::SystemUser and RT::Nobody loaded
43   DBConnect();
44
45
46   #Get the current user all loaded
47   my $CurrentUser = GetCurrentUser();
48
49 =head1 DESCRIPTION
50
51
52 =head1 METHODS
53
54 =begin testing
55
56 ok(require RT::TestHarness);
57 ok(require RT::Interface::CLI);
58
59 =end testing
60
61 =cut
62
63
64 =head2 CleanEnv
65
66 Removes some of the nastiest nasties from the user\'s environment.
67
68 =cut
69
70 sub CleanEnv {
71     $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
72     $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
73     $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
74     $ENV{'ENV'} = '' if defined $ENV{'ENV'};
75     $ENV{'IFS'} = ''            if defined $ENV{'IFS'};
76 }
77
78
79
80 =head2 LoadConfig
81
82 Loads RT's config file and then drops setgid privileges.
83
84 =cut
85
86 sub LoadConfig {
87     
88     #This drags in  RT's config.pm
89     use config;
90     
91 }       
92
93
94
95 =head2 DBConnect
96
97   Calls RT::Init, which creates a database connection and then creates $RT::Nobody
98   and $RT::SystemUser
99
100 =cut
101
102
103 sub DBConnect {
104     use RT;
105     RT::Init();
106 }
107
108
109
110 # {{{ sub GetCurrentUser 
111
112 =head2 GetCurrentUser
113
114   Figures out the uid of the current user and returns an RT::CurrentUser object
115 loaded with that user.  if the current user isn't found, returns a copy of RT::Nobody.
116
117 =cut
118 sub GetCurrentUser  {
119     
120     my ($Gecos, $CurrentUser);
121     
122     require RT::CurrentUser;
123     
124     #Instantiate a user object
125     
126     $Gecos=(getpwuid($<))[0];
127
128     #If the current user is 0, then RT will assume that the User object
129     #is that of the currentuser.
130
131     $CurrentUser = new RT::CurrentUser();
132     $CurrentUser->LoadByGecos($Gecos);
133     
134     unless ($CurrentUser->Id) {
135         $RT::Logger->debug("No user with a unix login of '$Gecos' was found. ");
136     }
137     return($CurrentUser);
138 }
139 # }}}
140
141 # {{{ sub GetMessageContent
142
143 =head2 GetMessageContent
144
145 Takes two arguments a source file and a boolean "edit".  If the source file
146 is undef or "", assumes an empty file.  Returns an edited file as an 
147 array of lines.
148
149 =cut
150
151 sub GetMessageContent {
152     my %args = (  Source => undef,
153                   Content => undef,
154                   Edit => undef,
155                   CurrentUser => undef,
156                  @_);
157     my $source = $args{'Source'};
158
159     my $edit = $args{'Edit'};
160     
161     my $currentuser = $args{'CurrentUser'};
162     my @lines;
163
164     use File::Temp qw/ tempfile/;
165     
166     #Load the sourcefile, if it's been handed to us
167     if ($source) {
168         open (SOURCE, "<$source");
169         @lines = (<SOURCE>);
170         close (SOURCE);
171     }
172     elsif ($args{'Content'}) {
173         @lines = split('\n',$args{'Content'});
174     }
175     #get us a tempfile.
176     my ($fh, $filename) = tempfile();
177         
178     #write to a tmpfile
179     for (@lines) {
180         print $fh $_;
181     }
182     close ($fh);
183     
184     #Edit the file if we need to
185     if ($edit) {        
186
187         unless ($ENV{'EDITOR'}) {
188             $RT::Logger->crit('No $EDITOR variable defined'. "\n");
189             return undef;
190         }
191         system ($ENV{'EDITOR'}, $filename);
192     }   
193     
194     open (READ, "<$filename");
195     my @newlines = (<READ>);
196     close (READ);
197
198     unlink ($filename) unless (debug());
199     return(\@newlines);
200     
201 }
202
203 # }}}
204
205 # {{{ sub debug
206
207 sub debug {
208     my $val = shift;
209     my ($debug);
210     if ($val) {
211         $RT::Logger->debug($val."\n");
212         if ($debug) {
213             print STDERR "$val\n";
214         }
215     }
216     if ($debug) {
217         return(1);
218     }   
219 }
220
221 # }}}
222
223
224 1;