]> the.earth.li Git - htag.git/blob - plugins/02catsig
62c5b72af57e1dfb53e3053f4853fcb537ac4343
[htag.git] / plugins / 02catsig
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2000-2001 Simon Huggins
4 # catsig deals with choosing a sig
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 # for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc., 59
18 # Temple Place, Suite 330, Boston, MA 02111-1307  USA
19
20
21 # You can either specify sigs with
22 #       $cfg{'sigs'}
23 #               An arrayref to an array of filenames of sigs.
24 #       $cfg{'sigdir'}
25 #               A scalar containing a directory of all your sig files.
26 #       $cfg{'sigmatch'}
27 #               A scalar containing a pattern to match the files in sigdir
28 #               (full path) against.
29 #
30 #       So specifying sigdir without sigmatch takes all the files in that
31 #       directory that don't start with a fullstop (period) and specifying
32 #       it with sigmatch only takes those that match.
33 #       Note that if you specify sigs *AND* sigdir you'll get all of sigs
34 #       and anything that sigdir/sigmatch matches.
35 #       The program dies if after trying to match it comes up with nothing.
36
37 use strict;
38
39 if (defined $cfg{'sigdir'}) {
40         opendir(DIR, $cfg{'sigdir'})
41                 or htagdie "Could not open directory $cfg{'sigdir'}: $!\n";
42         if (not defined $cfg{'sigmatch'}) {
43                 push @{$cfg{'sigs'}},   map  { $cfg{'sigdir'} . "/" . $_ }
44                                         grep { ! /^\./ }
45                                         readdir(DIR);
46         } else {
47                 # Check regexp wouldn't kill us
48                 exec { "" =~ $cfg{'sigmatch'};}
49                 htagdie "sigmatch contained bad pattern.  perl said: $@" if $@;
50                 push @{$cfg{'sigs'}}, grep { m,$cfg{'sigmatch'}, }
51                                         map  { $cfg{'sigdir'} . "/" . $_ }
52                                         readdir(DIR);
53                 print STDERR "Got sigmatch == $cfg{'sigmatch'}\n";
54         }
55         closedir(DIR);
56 }
57
58 htagdie <<EOF if (not defined $cfg{'sigs'} or scalar @{$cfg{'sigs'}} == 0);
59 No signatures found.  Either you didn't specify sigs, or there weren't any
60 files in sigdir or your sigmatch didn't match anything.
61 EOF
62
63 sub quit() {
64         return 255;
65 }
66
67 sub write_sig($) {
68         my $sig = shift;
69
70         open(OUT, ">$cfg{'tmpsigfile'}") or htagdie "Couldn't open $cfg{'tmpsigfile'}";
71         print OUT $sig;
72         close(OUT);
73         open(OUT, ">$cfg{'tmpsigfile'}.old") or htagdie "Couldn't open $cfg{'tmpsigfile'}.old";
74         print OUT $sig;
75         close(OUT);
76 }
77
78 sub choose_sig {
79 ### Choose a sig and read it into $sig for grepping for @72@ or whatever.
80         my ($sigfile,@oldsiglist,@siglist,$num,$sig);
81
82         @oldsiglist = @siglist = @{$cfg{'sigs'}};
83
84         do {    
85                 $sig="";
86
87                 if (@siglist) {
88                         # Find and delete
89                         $sigfile=splice(@siglist,rand(@siglist),1);
90                 } elsif (-r "$cfg{'HOME'}/.sig") {
91                         print STDERR "Falling back on ~/.sig\n" if $cfg{'debug'};
92                         $sigfile="$cfg{'HOME'}/.sig";
93                 } else {
94                         print STDERR "No sigs specified and no ~/.sig!  Using simple tag method\n";
95                 }
96                 
97                 if (not $sigfile) {
98                         $sig="";
99                         write_sig($sig);
100                         return;
101                 }
102                 
103                 print STDERR "Using $sigfile\n" if $cfg{'debug'};
104
105                 open(HANDLE, "<$sigfile") or htagdie "Could not open $sigfile: $!\n";
106                 while(<HANDLE>) { $sig .= $_ };
107                 close(HANDLE);
108
109                 if ($cfg{'asksig'}) {
110                         print STDERR $sig . "\nUse this sig? ([y]/n/q)";
111                         $_ = <STDIN>;
112                         if ($_ =~ /q(?:uit)?/i) { return &quit; }
113                         if (not @siglist and $_ =~ /no?/i) {
114                                 print STDERR "That was the last sig.  Reset siglist or quit? ([r]/q)";
115                                 $_ = <STDIN>;
116                                 if ($_ =~ /q(?:uit)?/i) { return &quit; }
117                                 @siglist = @oldsiglist;
118                                 $_ = 'n';
119                         }
120
121                 }
122
123         } while ($cfg{'asksig'} and $_ !~ /y(?:es)?/i and $_ !~ /^\n$/);
124         write_sig($sig);
125         return;
126 }
127
128 reg_deletion($cfg{'tmpsigfile'});
129 reg_deletion("$cfg{'tmpsigfile'}.old");
130 &choose_sig;