]> the.earth.li Git - htag.git/blob - plugins/10simple
Change to debhelper compat level 10
[htag.git] / plugins / 10simple
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2000-2003 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 my $sig="";
28 open(SIG, "<$cfg{'tmpsigfile'}") or htagdie "$0: Could not open $cfg{'tmpsigfile'}: $!\n";
29 while(<SIG>) {
30         $sig .= $_;
31 }
32 close(SIG);
33
34 if (grep { /\@NOTAG\@/ } $sig) {
35         $cfg{'notag'}=1;
36         return 15;
37 }
38
39 htagdie <<EOF if (not defined $cfg{'tagfile'} and not defined $cfg{'tagfiles'} and not defined $cfg{'fortune'} and not defined $cfg{'tagdir'}); 
40 No tagfile defined!  I'm good but I'm not psychic
41 Did you configure me at all?  Look at the sample.htrc
42 EOF
43
44 htagdie <<EOF if ($cfg{'tagfiles'} && $cfg{'tagfile'});
45 You have defined both \$cfg{'tagfile'} and \$cfg{'tagfiles'} in your config
46 file(s).  Please use one or the other.
47 EOF
48
49 if (defined $cfg{'tagfile'}) {
50         push @{$cfg{'tagfiles'}}, $cfg{'tagfile'};
51 }
52
53 if (defined $cfg{'tagdir'}) {
54         opendir(DIR, $cfg{'tagdir'})
55                 or htagdie "Could not open directory $cfg{'tagdir'}: $!\n";
56         if (not defined $cfg{'tagmatch'}) {
57                 push @{$cfg{'tagfiles'}},
58                         map  { $cfg{'tagdir'} . "/" . $_ }
59                         grep { ! /^\./ }
60                         readdir(DIR);
61         } else {
62                 # Check regexp wouldn't kill us
63                 exec { "" =~ $cfg{'tagmatch'};}
64                 htagdie "tagmatch contained bad pattern.  perl said: $@" if $@;
65                 push @{$cfg{'tagfiles'}},
66                         grep { m,$cfg{'tagmatch'}, }
67                         map  { $cfg{'tagdir'} . "/" . $_ }
68                         readdir(DIR);
69 htagdie <<EOF if (not @{$cfg{'tagfiles'}});
70 tagdir didn't match anything and you had no tagfiles defined.
71 EOF
72
73         }
74         closedir(DIR);
75 }
76
77 my $tag;
78
79 if ($cfg{'fortune'} && $cfg{'fortuneval'} && $cfg{'fortuneval'} > rand()) {
80         my $cmd = $cfg{'fortune'};
81         $cmd .= " ".$cfg{'fortuneargs'} if defined $cfg{'fortuneargs'};
82         
83         $tag = `$cmd`;
84         htagdie "fortune died: $!" if $?;
85 } else {
86         foreach (@{$cfg{'tagfiles'}}) {
87                 open(HANDLE, "<$_")
88                         or htagdie "Could not open $_: $!\n";
89                 push @tags, <HANDLE>;
90                 close(HANDLE);
91         }
92         if ($cfg{'tagline_comment_char'}) {
93                 @tags = grep { ! /^$cfg{'tagline_comment_char'}/ } @tags;
94         }
95         $tag = $tags[rand(@tags)];
96 }
97
98 open(OUT, ">$cfg{'tmptagfile'}") or htagdie "$0: Could not open $cfg{'tmptagfile'}: $!\n";
99 reg_deletion("$cfg{'tmptagfile'}");
100 chomp $tag;
101 print OUT $tag;
102 close(OUT);
103 return;