]> the.earth.li Git - htag.git/blob - plugins/10simple
46825d684a7a1dedd0363768dd3c5485e21afde1
[htag.git] / plugins / 10simple
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2000-2001 Simon Huggins
4 # simple just chooses a random tagline in the simplest possible way
5 # or uses fortune(1)
6
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 2 of the License, or (at your option)
10 # any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 # for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc., 59
19 # Temple Place, Suite 330, Boston, MA 02111-1307  USA
20
21 use strict;
22
23 my (@tags);
24
25 # srand( time() ^ ($$ + ($$ << 15) )); # Since 5.004 not required.
26
27
28 htagdie <<EOF if (not defined $cfg{'tagfile'} and not defined $cfg{'tagfiles'} and not defined $cfg{'fortune'} and not defined $cfg{'tagdir'}); 
29 No tagfile defined!  I'm good but I'm not psychic
30 Did you configure me at all?  Look at the sample.htrc
31 EOF
32
33 htagdie <<EOF if ($cfg{'tagfiles'} && $cfg{'tagfile'});
34 You have defined both \$cfg{'tagfile'} and \$cfg{'tagfiles'} in your config
35 file(s).  Please use one or the other.
36 EOF
37
38 if (defined $cfg{'tagfile'}) {
39         push @{$cfg{'tagfiles'}}, $cfg{'tagfile'};
40 }
41
42 if (defined $cfg{'tagdir'}) {
43         opendir(DIR, $cfg{'tagdir'})
44                 or htagdie "Could not open directory $cfg{'tagdir'}: $!\n";
45         if (not defined $cfg{'tagmatch'}) {
46                 push @{$cfg{'tagfiles'}},
47                         map  { $cfg{'tagdir'} . "/" . $_ }
48                         grep { ! /^\./ }
49                         readdir(DIR);
50         } else {
51                 # Check regexp wouldn't kill us
52                 exec { "" =~ $cfg{'tagmatch'};}
53                 htagdie "tagmatch contained bad pattern.  perl said: $@" if $@;
54                 push @{$cfg{'tagfiles'}},
55                         grep { m,$cfg{'tagmatch'}, }
56                         map  { $cfg{'tagdir'} . "/" . $_ }
57                         readdir(DIR);
58 htagdie <<EOF if (not @{$cfg{'tagfiles'}});
59 tagdir didn't match anything and you had no tagfiles defined.
60 EOF
61
62         }
63         closedir(DIR);
64 }
65
66 my $tag;
67
68 if ($cfg{'fortune'} && $cfg{'fortuneval'} && $cfg{'fortuneval'} > rand()) {
69         my $cmd = $cfg{'fortune'};
70         $cmd .= " ".$cfg{'fortuneargs'} if defined $cfg{'fortuneargs'};
71         
72         $tag = `$cmd`;
73         htagdie "fortune died: $!" if $?;
74 } else {
75         foreach (@{$cfg{'tagfiles'}}) {
76                 open(HANDLE, "<$_")
77                         or htagdie "Could not open $_: $!\n";
78                 push @tags, <HANDLE>;
79                 close(HANDLE);
80         }
81         if ($cfg{'tagline_comment_char'}) {
82                 @tags = grep { ! /^$cfg{'tagline_comment_char'}/ } @tags;
83         }
84         $tag = $tags[rand(@tags)];
85 }
86
87 open(OUT, ">$cfg{'tmptagfile'}") or htagdie "$0: Could not open $cfg{'tmptagfile'}: $!\n";
88 reg_deletion("$cfg{'tmptagfile'}");
89 chomp $tag;
90 print OUT $tag;
91 close(OUT);
92 return;