Debian Codegraph @

demos/perlbug

raw
1 #!/usr/bin/perl -w
2 # $Id: perlbug,v 1.6 2013/07/17 19:34:06 tom Exp $
3
4 #
5 # Purpose:
6 # This 'rips' off Larry's perlbug from the utils directory.
7
8 use strict;
9
10 # Determine if the user has one of the mail modules.
11 BEGIN {
12 eval "use Mail::Send;";
13 $::HaveSend = ( $@ eq "" );
14 }
15
16 use Config;
17 use Sys::Hostname;
18 use Getopt::Std;
19
20 our ( $opt_a, $opt_c, $opt_f, $opt_h, $opt_r, $opt_s, $opt_v );
21 our $cc;
22
23 #
24 # Load in the Cdk Extension.
25 #
26 use Cdk;
27 Cdk::init();
28
29 # Create global variables.
30 my ($Version) = "1.00";
31 my $address = "perlbug\@perl.com";
32
33 # Check the command line arguments.
34 getopts("c:a:f:r:s:vh");
35
36 # Did they ask for help?
37 if ( defined $opt_h ) {
38 my @help = (
39 "<C></B/5>Perl Bug Reporting Facility Help Window.",
40 "</B/5>Usage:<!B!5> $0 [-c CC] [-a Admin Account]",
41 " [-r Reply Account] [-s subject]",
42 " [-f filename] [-v] [-h]",
43 "",
44 "<B=-c> The account to carbon copy to.",
45 "<B=-a> The perl admin account.",
46 "<B=-r> The account to reply to.",
47 "<B=-s> The subject of the bug report.",
48 "<B=-f> The file to read in as the bug report.",
49 "<B=-v> Turns on verbose output for the bug report.",
50 "<B=-h> Pops up this help window.",
51 "",
52 "<C></B/5>Press Any Key To Continue."
53 );
54 popupLabel( \@help );
55 }
56
57 # Create a program information message.
58 my @progInfo = (
59 "<C></5/B>Perl Bug Reporting Facility",
60 "<C></24/B>Version $Version",
61 "",
62 "This program allows you to create a bug report which will be",
63 "mailed to </B>$address<!B> once the report has been filled out.",
64 "",
65 "<C>Hit any key when you are ready to start."
66 );
67 popupLabel( \@progInfo );
68
69 # Create the generic label.
70 my @mesg = (
71 "******************************************************",
72 "******************************************************",
73 "******************************************************",
74 "******************************************************"
75 );
76 my $mainTitle = new Cdk::Label( 'Message' => \@mesg, 'Xpos' => "TOP" );
77
78 # Get the subject to the mail message.
79 my $subject = $opt_s || getSubject($mainTitle);
80 my $verbose = $opt_v;
81
82 # Get the reply address.
83 @mesg =
84 ( "<C></B/24>Return Email Address", "<C>Enter your return e-mail address." );
85 my $defaultReplyAddress = getlogin() . "@" . hostname() . ".";
86 my $replyAddress = $opt_r
87 || getEmailAddress( $mainTitle, $defaultReplyAddress, @mesg );
88
89 # Get the perl admin address.
90 @mesg = (
91 "<C></B/24>Perl Admin Email Address",
92 "<C>Enter the email address of the perl admin."
93 );
94 my $defaultAdminAddress = $::Config{perladmin};
95 my $adminAddress = $opt_a
96 || getEmailAddress( $mainTitle, $defaultAdminAddress, @mesg );
97
98 # Create the bug report.
99 my @report = createBugReport( $mainTitle, $subject, $opt_f );
100
101 # View the bug report.
102 viewBugReport( $subject, $replyAddress, $adminAddress, $cc, @report );
103 exit;
104
105 ############################################################################
106 #
107 # This gets the subject to the bug report.
108 #
109 sub getSubject {
110 my $mainTitle = shift;
111
112 # Create the subject entry field.
113 my $entry = new Cdk::Entry(
114 'Label' => "</B/5>Subject: ",
115 'Width' => 35,
116 'Min' => 3,
117 'Max' => 256
118 );
119
120 # Set the main title info.
121 @mesg = (
122 "<C></24/B>Enter Subject",
123 "<C>Please provide a subject for the message. It",
124 "<C>should be as a concise description of the bug",
125 "<C>as is possible."
126 );
127 $mainTitle->set( 'Message' => \@mesg );
128 $mainTitle->draw();
129
130 # Get the subject.
131 while (1) {
132 my $subject = $entry->activate();
133 last if defined $subject;
134
135 # No subject, prompt them for one...
136 popupLabel(
137 [
138 "<C></16/B>Error",
139 "<C>You must have a subject line for the mail message.",
140 "", "<C>Please try again."
141 ]
142 );
143 }
144 return $subject;
145 }
146
147 ############################################################################
148 #
149 # This gets an emial address.
150 #
151 sub getEmailAddress {
152 my ( $mainTitle, $entryValue, @mesg ) = @_;
153 my $info;
154
155 # Set the main title info.
156 $mainTitle->set( 'Message' => \@mesg );
157
158 # Create the entry field to get the email address.
159 my $entry = new Cdk::Entry(
160 'Label' => "</B/5>email Address: ",
161 'Min' => 3,
162 'Max' => 256,
163 'Width' => 35
164 );
165
166 # Put the user name in the entry field.
167 $entry->set( 'Value' => "$entryValue" );
168
169 # Get the emial address
170 while (1) {
171 $info = $entry->activate();
172 last if defined $info;
173
174 # No subject, prompt them for one...
175 popupLabel(
176 [
177 "<C></B/16>Error", "<C>You must provide an email address.",
178 "", "<C>Please Try again."
179 ]
180 );
181 }
182 return $info;
183 }
184
185 ############################################################################
186 #
187 # This gets the bug report from the user.
188 #
189 sub createBugReport {
190 my ( $mainTitle, $subject, $filename ) = @_;
191 my @bugReport = ();
192 my @info = ();
193 my $info;
194
195 # If a filename has been speicifed, then we will use the contents of the
196 # file for the bug report.
197 if ( defined $filename && -e $filename ) {
198 open( FILE, $filename );
199 my @tmp = <FILE>;
200 chomp(@tmp);
201 return @tmp;
202 }
203
204 # Create the title.
205 my @mesg = (
206 "<C></24/B>Bug Report",
207 "<C>Enter a description of the bug you are submitting."
208 );
209
210 # Set the main title info.
211 $mainTitle->set( 'Message' => \@mesg );
212
213 # Create the entry field to get the email address.
214 my $entry = new Cdk::Mentry(
215 'Label' => "</B/5>Description: ",
216 'Prows' => 8,
217 'Lrows' => 15,
218 'Width' => 50
219 );
220
221 # Get the bug report.
222 while (1) {
223 $info = $entry->activate();
224 last if defined $info;
225
226 # No subject, prompt them for one...
227 popupLabel(
228 [
229 "<C></B/16>Error",
230 "<C>You must provide a description of the bug.",
231 "", "<C>Please Try again."
232 ]
233 );
234 }
235
236 # Split the string into a list.
237 @info = Cdk::scalar2List( $info, 40 );
238
239 # Create the bug report.
240 my $from = `whoami`;
241 push( @bugReport,
242 "This is a bug report for perl from $from generated with" );
243 push( @bugReport,
244 "the help of the Cdk version of perlbug running under perl $]." );
245 push( @bugReport, "" );
246 push( @bugReport, "Subject: $subject" );
247 push( @bugReport, "" );
248 for ( my $x = 0 ; $x <= $#info ; $x++ ) {
249 push( @bugReport, $info[$x] );
250 }
251 push( @bugReport, "" );
252 push( @bugReport, "Site configuration information for perl $]:" );
253
254 if ( $::Config{cf_by} and $::Config{cf_time} ) {
255 push( @bugReport,
256 "Configured by $::Config{cf_by} at $::Config{cf_time}." );
257 }
258 push( @bugReport, "" );
259 foreach ( split( /\n/, Config::myconfig ) ) {
260 push( @bugReport, $_ );
261 }
262
263 # Do they want a verbose bug report?
264 if ($::opt_v) {
265 push( @bugReport, "" );
266 push( @bugReport, "Complete configuration data for perl $]:" );
267 push( @bugReport, "" );
268 foreach ( sort keys %::Config ) {
269 my $value = $::Config{$_};
270 $value =~ s/'/\\'/g;
271 push( @bugReport, "$_='$value'" );
272 }
273 }
274 return @bugReport;
275 }
276
277 ############################################################################
278 #
279 # This views the bug report.
280 #
281 sub viewBugReport {
282 my ( $subject, $replyAddress, $adminAddress, @bugReport ) = @_;
283 my @buttons = ("</B/24>OK");
284
285 # Get the height and width of the screen.
286 my ( $height, $width ) = Cdk::getCdkScreenDim();
287 $height -= 3;
288 $width -= 3;
289
290 # Create the file viewer.
291 my $viewer = new Cdk::Viewer(
292 'Buttons' => \@buttons,
293 'Height' => $height,
294 'Width' => $width
295 );
296
297 # Fill the viewer with the contents of the bug report.
298 $viewer->set(
299 'Title' => "<C></5>Bug Report",
300 'Highlight' => "</B/5>",
301 'Info' => \@bugReport
302 );
303 $viewer->activate();
304
305 # Ask them what they want to do with the bug report.
306 my @mesg = (
307 "<C>Now that the bug report has been created, you can",
308 "<C>send the bug report to </R>$replyAddress<!R> and </R>$adminAddress<!R>,",
309 "<C>or you can save the report to a file and send it later",
310 "<C>on your own, or you can quit without saving or sending",
311 "<C>the bug report."
312 );
313 @buttons = ( "</B/24>Send", "</B/8>Save", "</B/16>Cancel" );
314 my $choice = popupDialog( \@mesg, \@buttons );
315
316 # Redraw the viewer widget.
317 $viewer->draw();
318
319 # Check what they want to do.
320 if ( $choice == 0 ) {
321
322 # Mail to bug report.
323 sendBugReport( $subject, $replyAddress, $adminAddress, @bugReport );
324 }
325 elsif ( $choice == 1 ) {
326
327 # Save to a file.
328 saveBugReport(@bugReport);
329 }
330 else {
331 popupLabel( ["<C></B/24>Send Bug Report Canceled."] );
332 }
333 }
334
335 #
336 # This saves the bug report to a file.
337 #
338 sub saveBugReport {
339 my @bugReport = @_;
340 my $filename;
341
342 # Get the filename to save to.
343 my $entry = new Cdk::Entry(
344 'Label' => "</B/5>Filename: ",
345 'Width' => 30,
346 'Min' => 2,
347 'Max' => 256
348 );
349
350 # Make sure we can write to the file.
351 while (1) {
352
353 # Get the filename.
354 $filename = $entry->activate();
355
356 # Try to open the filename.
357 last if open( FILE, ">$filename" );
358
359 popupLabel(
360 [ "<C></B/16>Error", "<C>Can not save to the file $filename" ] );
361 }
362
363 # Save the bug report to the file.
364 foreach (@bugReport) {
365 print FILE "$_\n";
366 }
367 close(FILE);
368
369 # Tell the user the file has been saved.
370 popupLabel(
371 [
372 "The bug report has been saved to $filename",
373 "",
374 "<C>Press any key to continue."
375 ]
376 );
377 }
378
379 #
380 # This sends the bug report to the given addresses.
381 #
382 sub sendBugReport {
383 my ( $subject, $replyAddress, $adminAddress, $cc, @bugReport ) = @_;
384 my $address = "perlbug\@perl.com";
385
386 # Do we have the sendmail module?
387 if ($::HaveSend) {
388
389 # Create a mail object.
390 my $mailMessage = new Mail::Send(
391 'Subject' => "$subject",
392 'To' => "$address"
393 );
394
395 # Add a carbon copy, if we have one.
396 $mailMessage->cc($cc) if $cc;
397
398 # Add a from line.
399 $mailMessage->add( "Reply-To" => $replyAddress ) if $replyAddress;
400
401 # Open the mail message and write the contents.
402 my $fh = $mailMessage->open;
403 foreach (@bugReport) {
404 print $fh "$_\n";
405 }
406
407 # Close the mail message (aka send it.)
408 $fh->close;
409
410 # Popup a little message.
411 popupLabel( ["<C>The bug report has been sent."] );
412 return;
413 }
414 else {
415
416 # No, Okay, let's try to use sendmail normally. (normally????)
417 my $sendmail = "";
418
419 # Where oh where are you you today...
420 foreach (qw(/usr/lib/sendmail /usr/sbin/sendmail /usr/ucblib/sendmail))
421 {
422 $sendmail = $_, last if -e $_;
423 }
424
425 # Can we even send the bug report?
426 if ( $sendmail eq "" ) {
427
428 # We can't send the bug report, maybe we can save it to a file.
429 my @mesg = (
430 "</B/5>Hmmmm.",
431 "<C>I'm terribly sorry but I can't find sendmail and the package",
432 "<C>Mail::Send has not been installed, so I can't send your bug",
433 "<C>report. Since I can't send the bug report, would you like to",
434 "<C>save it to a file and send it yourself?"
435 );
436 if ( popupDialog( \@mesg, [ "Yep", "Nope" ] ) == 0 ) {
437 saveBugReport(@bugReport);
438 }
439 return;
440 }
441
442 # Send the message via sendmail.
443 open( SENDMAIL, "|$sendmail -t" );
444 print SENDMAIL "To: $address\n";
445 print SENDMAIL "Subject: $subject\n";
446 print SENDMAIL "Cc: $cc\n" if $cc;
447 print SENDMAIL "Reply-To: $replyAddress\n" if $replyAddress;
448 print SENDMAIL "\n\n";
449 foreach (@bugReport) {
450 print SENDMAIL "$_\n";
451 }
452 close(SENDMAIL);
453
454 # Popup a little message.
455 popupLabel( ["<C>The bug report has been sent."] );
456 }
457 }