fix sending msgs to g1tlh-2 (and not to g1tlh, - and 2)
[spider.git] / cmd / apropos.pl
1
2 # the help subsystem
3 #
4 # apropos - this does a grep on the command file and returns the commands
5 # that contain the string searched for
6 #
7 # Copyright (c) 1998 - Dirk Koopman G1TLH
8 #
9 # $Id$
10 #
11
12 my ($self, $line) = @_;
13 my @out;
14
15 my $lang = $self->lang;
16 $lang = 'en' if !$lang;
17
18 my $in;
19 $line = 'help' unless $line;
20 $line =~ s/\W//g;   # remove dubious characters
21
22 my ($priv, $cmd, $desc);
23 my %cmd;
24
25 my $defh = new IO::File;
26 unless ($defh->open("$main::localcmd/Commands_en.hlp")) {
27         unless($defh->open("$main::cmd/Commands_en.hlp")) {
28                 return (1, $self->msg('helpe1'));
29         }
30 }
31
32 my $h;
33 unless ($lang ne 'en') {
34         $h = new IO::File;
35         unless ($h->open("$main::localcmd/Commands_$lang.hlp")) {
36                 unless($h->open("$main::cmd/Commands_$lang.hlp")) {
37                         undef $h;
38                 }
39         }
40 }
41
42 # do english help
43 my $include;
44 foreach $in (<$defh>) {
45         next if $in =~ /^\#/;
46         chomp $in;
47         if ($in =~ /^===/) {
48                 $cmd{$cmd} = "$cmd $desc" if $include;
49                 $include = 0;
50                 $in =~ s/=== //;
51                 ($priv, $cmd, $desc) = split /\^/, $in;
52                 next if $priv > $self->priv;             # ignore subcommands that are of no concern
53                 next unless $cmd =~ /$line/i || $desc =~ /$line/i;
54                 next if $cmd =~ /-$/o;
55                 $include = 1;
56                 next;
57         }
58         $include =~ 1 if $cmd =~ /$line/i;
59 }
60 $cmd{$cmd} = "$cmd $desc" if $include;
61 $defh->close;
62
63 # override with any not english help
64 if ($h) {
65         my $include;
66         foreach $in (<$h>) {
67                 next if $in =~ /^\#/;
68                 chomp $in;
69                 if ($in =~ /^===/) {
70                         $cmd{$cmd} = "$cmd $desc" if $include;
71                         $include = 0;
72                         $in =~ s/=== //;
73                         ($priv, $cmd, $desc) = split /\^/, $in;
74                         next if $priv > $self->priv;             # ignore subcommands that are of no concern
75                         next unless $cmd =~ /$line/i || $desc =~ /$line/i;
76                         next if $cmd =~ /-$/o;
77                         $include = 1;
78                         next;
79                 }
80                 $include =~ 1 if $cmd =~ /$line/i;
81         }
82         $cmd{$cmd} = "$cmd $desc" if $include;
83         $h->close;
84 }
85
86 push @out, map {$cmd{$_}} sort keys %cmd;
87
88 push @out, $self->msg('helpe2', $line) if @out == 0;
89
90 return (1, @out);