]> xenbits.xensource.com Git - people/aperard/emesinae.git/commitdiff
bug.pl: Refactor to handle mbox vs normal output more obviously.
authorIan Campbell <ian.campbell@citrix.com>
Mon, 1 Jul 2013 13:28:30 +0000 (14:28 +0100)
committerIan Campbell <ian.campbell@citrix.com>
Mon, 1 Jul 2013 13:28:30 +0000 (14:28 +0100)
CGI/bug.pl

index 3dc66c8dc0d8d28cc9ae5e926c56f857963360e8..9b2ffc2778d40c942eb37a9f260351db9739ad31 100755 (executable)
@@ -51,68 +51,76 @@ $mode = $2 if $2;
 my $bug = Emesinae::Bug->new( $dbh, ID => $bugid );
 fourohfour() unless $bug;
 
-if ( $mode eq "mbox" ) {
-    print header( -type => 'application/mbox', -charset => 'utf-8', );
-
-    foreach my $m ( sort msgcmp $bug->messages ) {
-        eval {
-            my $fh;
-
-            # Could forge a placeholder for messages which aren't present here.
-
-            $fh = $m->headers;
-            print for (<$fh>);
-
+my %handlers = (
+    mbox => {
+        Header => sub () { print header( -type => 'application/mbox', -charset => 'utf-8', ); },
+        Message => sub ($) {
+            my $m = shift;
+            eval {
+                my $fh;
+
+                # Could forge a placeholder for messages which aren't present here.
+
+                $fh = $m->headers;
+                print for (<$fh>);
+
+                print "\n";
+
+                $fh = $m->body;
+                print for (<$fh>);
+            };
+            # Ignore errors...
+        },
+        Footer => sub () { }
+    },
+    normal => {
+        Header => sub () {
+            print header( -charset => 'utf-8', );
+
+            print start_html(
+                -title => $c{TrackerName} . ": #$bugid - " . htmlsanit( $bug->{title} ),
+                -style => { 'src' => $c{StyleSheet} }
+            );
+
+            print h1( "#$bugid - " . htmlsanit( $bug->{title} ) );
             print "\n";
 
-            $fh = $m->body;
-            print for (<$fh>);
-        };
+            sub fmtdate ($) {
+                return htmlsanit( strftime( "%a %b %e %H:%M:%S %Y", gmtime(shift) ) );
+            }
+
+            my $creationdate   = fmtdate( $bug->{creationdate} );
+            my $lastchangedate = fmtdate( $bug->{lastchangedate} );
+            print div(
+                { -class => "bugmetadata" },
+                p(
+                    [
+                        "Owner: " . htmlsanit( $bug->{owner} ),
+                        "Date: " . htmlsanit($creationdate),
+                        "Last Update: " . htmlsanit($lastchangedate),
+                        "Severity: " . htmlsanit( $bug->{severity} ),
+                        "Affects: " . htmlsanit( join( ", ", $bug->tags("affects") ) ),
+                        "State: " . b( $bug->{open} ? "Open" : "Closed" )
+                    ]
+                )
+            );
+
+            print p( { -class => "bugcontrol" },
+                "[ " . buglink( $bug, "Retrieve as mbox", "/mbox" ) . " ]" )
+              . "\n";
+
+            print hr;
 
-        # Ignore errors...
+            print "\n";
+        },
+        Message => sub ($) { format_html(shift, $bug); print hr . "\n" },
+        Footer => sub () { print end_html }
     }
-
-    exit(0);
-}
-
-print header( -charset => 'utf-8', );
-print start_html(
-    -title => $c{TrackerName} . ": #$bugid - " . htmlsanit( $bug->{title} ),
-    -style => { 'src' => $c{StyleSheet} }
 );
 
-print h1( "#$bugid - " . htmlsanit( $bug->{title} ) );
-print "\n";
-
-sub fmtdate ($) {
-    return htmlsanit( strftime( "%a %b %e %H:%M:%S %Y", gmtime(shift) ) );
-}
-
-my $creationdate   = fmtdate( $bug->{creationdate} );
-my $lastchangedate = fmtdate( $bug->{lastchangedate} );
-print div(
-    { -class => "bugmetadata" },
-    p(
-        [
-            "Owner: " . htmlsanit( $bug->{owner} ),
-            "Date: " . htmlsanit($creationdate),
-            "Last Update: " . htmlsanit($lastchangedate),
-            "Severity: " . htmlsanit( $bug->{severity} ),
-            "Affects: " . htmlsanit( join( ", ", $bug->tags("affects") ) ),
-            "State: " . b( $bug->{open} ? "Open" : "Closed" )
-        ]
-    )
-);
-
-print p( { -class => "bugcontrol" },
-    "[ " . buglink( $bug, "Retrieve as mbox", "/mbox" ) . " ]" )
-  . "\n";
-
-print hr;
-
-print "\n\n";
+die "Bad/unknown mode $mode" unless defined $handlers{$mode};
 
-print "\n";
+$handlers{$mode}{Header}();
 
 sub msgcmp {
     my $atime = $a->{present} ? $a->{date}->time : 0;
@@ -121,8 +129,7 @@ sub msgcmp {
 }
 
 foreach my $m ( sort msgcmp $bug->messages ) {
-    format_html($m, $bug);
-    print hr . "\n";
+    $handlers{$mode}{Message}($m);
 }
 
-print end_html;
+$handlers{$mode}{Footer}();