Perl script for quotes
This script is one of my first ever perl script that where written by me.The script itself takes ksig, Fortune cookie and my own text file that contain many quotations, and return a random quote entry.
Project Name: QtsBank
Version: 0.3
Programming Language: Perl
License: [MIT
] Copyright (C) 2004-2006 under MIT License Ido Kanner Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Embbedded Source: [Yes
] #!/usr/bin/perl -w ############################################################################################# # Name QtsBank # # # # Propose: # # This script displays a random quote taken from ksig, fortune and my own collection. # # # # Copyright (C) 2004-2006 under MIT License Ido Kanner # # # # Permission is hereby granted, free of charge, to any person obtaining a copy of this # # software and associated documentation files (the "Software"), to deal in the Software # # without restriction, including without limitation the rights to use, copy, modify, merge, # # publish, distribute, sublicense, and/or sell copies of the Software, and to permit # # persons to whom the Software is furnished to do so, subject to the following conditions: # # # # The above copyright notice and this permission notice shall be included in all # # copies or substantial portions of the Software. # # # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR # # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT # # OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # # DEALINGS IN THE SOFTWARE. # # # # Version 0.3 # # # # History: # # 2006/12/07 - Fixed spelling issues found in this script file. # # 2006/10/05 - Changed license to MIT # # - Changed default of urandom seeding to true. # # - Added line fix that correct the signature credit if it's not 4 space from # # the beginning of the line. # # 2005/10/05 - Added support for /dev/urandom for seeding -> default is not to use # # it though. # # - Saw that the logic of Mooffie works with more then one "plain text". # # 2004/11/07 - Some fixes from Mooffie's comments. # # 2004/11/06 - Changed more things as Mooffie suggested... The script now is running # # much more faster then before !!! # # 2004/11/02 - Rewrite the code as Mooffie told me :) # # 2004/09/15 - Fixed the problem with empty quote file. # # Made a global call for the quote dump file name. # # 2004/08/24 - fixed a bug, mostly appeared in gmessage: without \n some of the time # # the text was not published (while in console it was still printed). # # 2004/08/21 - Added ability to set the result to a file. That way, I give crontab # # and Mozilla-Thunderbird a way to change my signature every one # # minute. # # 2004/06/08 - In ArraysPoped() the regex was changed from /^----/ to /^----\n$/ in # # order to give a better way to really find the qoute ending (if a # # quote line have in the beginning of the line 4 minus signs then it # # will also be considered as an end of a quote.. so that is the # # solution. # # 2004/06/08 - Moved the last chop in chooseQoute() to fortune and ksig. The prev # # statement cut also letters and not only \n. # # 2004/06/02 - Fixed spelling and license mistakes. # # 2004/06/02 - Program created. # # # ############################################################################################# use strict; use Carp qw(carp croak); #my $Sigfile = "$ENV{HOME}/.signature"; #Removed for now... our @sources = ("$ENV{HOME}/quotes.txt", # personal quote file "$ENV{HOME}/quotes2.txt", # personal quote file "ksig --random || /usr/bin/ksig --random;", # if /usr/bin/ is not on your path, then go there yourself "fortune || /usr/games/fortune;" # if /usr/games/ is not in your path, go there yourself ); our $randomSeed = 1; #/dev/urandom seed sub seed { my $seed = 0; open(RAND, "/dev/urandom") or return; read(RAND,$seed,4); close(RAND); srand(unpack("L", $seed)); return unpack("L", $seed); # if you really want to know it's content.. } #Extract the quote file and split it to array sub loadQuotes ($) { my ($QuoteFile) = @_; open (my $fh, "<$QuoteFile") or croak "Unable to open $QuoteFile."; #Open for read only ... local $/ = undef; return grep { /\S/ } split(/\n----\n/, <$fh>); # split lines by the \n----\n seperator } # return a random quote sub chooseQuote () { my $source = $sources[rand @sources]; # choose a random source if ($source =~ /;$/) { # does the selected source is a program ? return `$source`; # execute the source if the selected source is a program } my @quotes = loadQuotes($source); # a text file, lets load the quotes return $quotes[rand @quotes]; # lets return a random quote } # loads a text file and make trailing spaces as I like sub fixFile($) { my ($file) = @_; open FH, $file; my @lines = <FH> close FH; for (my $i=0; $i<=(scalar @lines); $i++) { if ($lines[$i] =~ /^\s{1,5}\S/) { $lines[$i] =~ s/^\s{1,5}(\S)/ $1/g; } elsif ($lines[$i] =~ /^\s{1,3}\S/) { $lines[$i] =~ s/^\s{1,3}(\S)/ $1/g; } elsif ($lines[$i] =~ /^\s{1,2}\S/) { $lines[$i] =~ s/^\s{1,2}(\S)/ $1/g; } } open FH, ">".$file; print FH @lines; close FH; print "Fixed. \n"; } # give me a quote please ! if (not scalar @ARGV) { seed () if ($randomSeed); my $quote = ""; $quote = chooseQuote () while $quote !~ /\S/; #loop until you will get any quote... $quote .= "\n" unless ($quote =~ /\n$/); #make sure there is a \n in the end... print $quote; } else # lets see what you want from me ... { if ($ARGV[1] =~ /--fix=(.*?)/) # lets fix a text file ... { my $file = $1; if (-e $file and -f $file and -r $file and -w $file) { fixFile($file); } } }Download: qtsbank.pl.gz
