X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=grml-tips;h=258c8c03877888ac3748c71641cd6c72bd1dcd3c;hb=691fd5b99dda8c70c8fd5443b4e0e57e29021008;hp=dfa7bd94f457b74762fa5a4d780c1d28ef910e84;hpb=a0382fb8ee619f123c1ebda9fdca7e9387c13437;p=grml-tips.git diff --git a/grml-tips b/grml-tips index dfa7bd9..258c8c0 100755 --- a/grml-tips +++ b/grml-tips @@ -4,16 +4,16 @@ # Authors: grml-team (grml.org), (c) Michael Prokop , (c) Alexander Wirt # Bug-Reports: see http://grml.org/bugs/ # License: This file is licensed under the GPL v2. -# Latest change: Sam Mär 03 15:35:38 CET 2007 [mika] ################################################################################ use strict; use Pod::Usage; -use feature 'say'; +use feature 'say'; use Term::ReadKey; -use Time::HiRes; +use Time::HiRes; use LWP::UserAgent; +use Getopt::Long; =head1 NAME @@ -21,7 +21,7 @@ B - query a signature file for a specific keyword and display results =head1 SYNOPSIS -B [OPTION] I +B [OPTION] I =head1 DESCRIPTION @@ -35,6 +35,14 @@ This manual page documents briefly the B command. Print this help and exit. +=item B<--tagsonly> + +Match on tags only instead of the whole tip + +=item B<--tipsfile TIPSFILE> + +Use TIPSFILE instead of /usr/share/grml-tips/grml_tips + =back =head1 EXAMPLES @@ -67,111 +75,146 @@ grml-tips was written by Alexander Wirt =cut - my $grml_tips = '/usr/share/grml-tips/grml_tips'; -my $pattern = shift; - -if ($pattern eq '') { - pod2usage( { - -message => 'No search pattern provided', - -exitval => -1, }); -} elsif ($pattern eq '--help') { - pod2usage(); -} + +my $help; +my $tagsonly; +my $tipsfile; + +my $result = GetOptions ( + "help" => \$help, + "tagsonly" => \$tagsonly, + "tipsfile=s" => \$grml_tips, +); + +my $pattern = shift; + +#help if pattern is missing; +pod2usage( + { -message => 'No search pattern provided', + -exitval => -1, + } +) unless $pattern; + +#help if help is wanted +pod2usage() if $help; my @tips; -if (! open (my $fh, '<', "$grml_tips")) { - say STDERR "Error: File \"$grml_tips\" not found."; - say STDERR "Exiting."; - exit -1; -} else { - my $tip = ''; - - my $tips_found = 0; - while (my $line = <$fh>) { - if ($line !~ /^-- $/) { - $tip .= $line; - } else { - $tips_found++; - if ( $tip =~ /$pattern/mi ) { - #$tip .= $line; - - my $header = "Grml Tip Number $tips_found\n"; - my $line = "-" x (length($header) -1 ) . "\n\n"; - - push @tips, $header . $line . $tip . "\n"; - $tip = ''; - } else { - $tip = ''; - } - } - } - close($fh); + +if ( !open( my $fh, '<', "$grml_tips" ) ) { + say STDERR "Error: File \"$grml_tips\" not found."; + say STDERR "Exiting."; + exit -1; +} +else { + my $tip = ''; + + my $tips_found = 0; + while ( my $line = <$fh> ) { + if ( $line !~ /^-- $/ ) { + $tip .= $line; + } + else { + my $header = "Grml Tip Number $tips_found\n"; + my $line = "-" x ( length($header) - 1 ) . "\n\n"; + + $tips_found++; + if ($tagsonly) { + #extract tags from tip + my ($tag) = $tip =~ /^Tags: (.*)$/m; + my @tags = split(/[, ]+/, $tag); + if (grep(/^$pattern$/i, @tags) ) { + push @tips, $header . $line . $tip . "\n"; + } + $tip = ''; + } else { + if ( $tip =~ /$pattern/mi ) { + push @tips, $header . $line . $tip . "\n"; + $tip = ''; + } + else { + $tip = ''; + } + } + } + } + close($fh); } if (@tips) { - if (! open (my $fh, '|-', 'less -FRX') ) { - say @tips; - } else { - say $fh @tips; - } -}else { - say "Sorry, could not find a tip for '$pattern'. :-(\n\n", - "Do you want to submit the keyword '$pattern' to grml's keyword database?\n", - "The grml team will write tips for the most requested and useful keywords.\n", - "To use and contribute to this feature you'll need a working networking connection.\n", - "No personal data will be transmitted to the database.\n\n", - "Send \"$pattern\" to grml's keyword database? [y|N] "; - - ReadMode 4; # Turn off controls keys - my $x; - while (not defined ($x = ReadKey(-1))) { - Time::HiRes::sleep(0.5); - } - ReadMode 0; # Reset tty mode before exiting - print "\n\n"; - if ($x =~ /(y|j)/i) { - my $version; - if ( -f '/etc/grml_version' ) { - open (my $fh, '<', '/etc/grml_version') or die "Could not open /etc/grml_version: $!"; - $version = <$fh>; - chomp $version; - close ($fh); - } elsif ( -f '/etc/debian_version') { - open (my $fh, '<', '/etc/debian_version') or die "Could not open /etc/debian_version: $!"; - $version = <$fh>; - chomp $version; - close ($fh); - } else { - $version = 'unknown'; - } - my $ua = new LWP::UserAgent; - $ua->agent("grml-tips 0.0 "); # set the HTTP 'browser' type - my $res = - $ua->post('http://deb.grml.org/~formorer/submissions/keyword.cgi', - [ 'version' => $version, - 'keyword' => $pattern - ], - ); - if ($res->is_success) { - my $content = $res->decoded_content; - if ($content =~ /Submission received/) { - say "Keyword '$pattern' has been submitted to grml's keyword database.\nThanks."; - } else { - say "Your pattern could not be submitted.\n", - "Please file a bug against grml-tips at ", - "http://bts.grml.org/\n", - "Thanks!"; - } - } else { - print "Could not submitt '$pattern': " . $res->status_line . "\n"; - } - - - } else { - print "'$pattern' has not been sent to grml's keyword database as requested.\n"; - print "If you want to submit a tip please mail it to tips\@grml.org - thank you!\n"; - } + if ( !open( my $fh, '|-', 'less -FRX' ) ) { + say @tips; + } + else { + say $fh @tips; + } +} +else { + say "Sorry, could not find a tip for '$pattern'. :-(\n\n", + "Do you want to submit the keyword '$pattern' to grml's keyword database?\n", + "The grml team will write tips for the most requested and useful keywords.\n", + "To use and contribute to this feature you'll need a working networking connection.\n", + "No personal data will be transmitted to the database.\n\n", + "Send \"$pattern\" to grml's keyword database? [y|N] "; + + ReadMode 4; # Turn off controls keys + my $x; + while ( not defined( $x = ReadKey(-1) ) ) { + Time::HiRes::sleep(0.5); + } + ReadMode 0; # Reset tty mode before exiting + print "\n\n"; + if ( $x =~ /(y|j)/i ) { + my $version; + if ( -f '/etc/grml_version' ) { + open( my $fh, '<', '/etc/grml_version' ) + or die "Could not open /etc/grml_version: $!"; + $version = <$fh>; + chomp $version; + close($fh); + } + elsif ( -f '/etc/debian_version' ) { + open( my $fh, '<', '/etc/debian_version' ) + or die "Could not open /etc/debian_version: $!"; + $version = <$fh>; + chomp $version; + close($fh); + } + else { + $version = 'unknown'; + } + my $ua = new LWP::UserAgent; + $ua->agent("grml-tips 0.0 "); # set the HTTP 'browser' type + my $res = $ua->post( + 'http://deb.grml.org/~formorer/submissions/keyword.cgi', + [ 'version' => $version, + 'keyword' => $pattern + ], + ); + if ( $res->is_success ) { + my $content = $res->decoded_content; + if ( $content =~ /Submission received/ ) { + say + "Keyword '$pattern' has been submitted to grml's keyword database.\nThanks."; + } + else { + say "Your pattern could not be submitted.\n", + "Please file a bug against grml-tips at ", + "http://bts.grml.org/\n", + "Thanks!"; + } + } + else { + print "Could not submitt '$pattern': " . $res->status_line . "\n"; + } + + } + else { + print + "'$pattern' has not been sent to grml's keyword database as requested.\n"; + print + "If you want to submit a tip please mail it to tips\@grml.org - thank you!\n"; + } } ## END OF FILE #################################################################