Debian Codegraph @

plugins/help

raw
1 #!perl -w
2
3 =head1 NAME
4
5 help - default help plugin for qpsmtpd
6
7 =head1 DESCRIPTION
8
9 The B<help> plugin gives the answers for the help command. It can be configured
10 to return C<502 Not implemented>.
11
12 Without any arguments, the C<help_dir> is set to F<./help/>.
13
14 =head1 OPTIONS
15
16 =over 4
17
18 =item not_implemented (1|0)
19
20 If this option is set (and the next argument is true), the plugin answers,
21 that the B<HELP> command is not implemented
22
23 =item help_dir /path/to/help/files/
24
25 When a client requests help for C<COMMAND> the file F</path/to/help/files/
26 . lc(COMMAND)> is dumped to the client if it exists.
27
28 =item COMMAND HELPFILE
29
30 Any other argument pair is treated as command / help file pair. The file is
31 expexted in the F<help/> sub directory. If the client calls C<HELP COMMAND>
32 the contents of HELPFILE are dumped to him.
33
34 =back
35
36 =head1 NOTES
37
38 The hard coded F<help/> path should be changed.
39
40 =cut
41
42 my %config = ();
43
44 sub register {
45 my ($self, $qp, %args) = @_;
46 my ($file, $cmd);
47 unless (%args) {
48 $config{help_dir} = './help/';
49 }
50 foreach (keys %args) {
51 /^(\w+)$/
52 or $self->log(LOGWARN, "Invalid argument for the 'help' plugin $_"),
53 next;
54 $cmd = $1;
55 if ($cmd eq 'not_implemented') {
56 $config{'not_implemented'} = $args{'not_implemented'};
57 }
58 elsif ($cmd eq 'help_dir') {
59 $file = $args{$cmd};
60 $file =~ m#^([\w\.\-/]+)$#
61 or $self->log(LOGERROR,
62 "Invalid charachters in filename for command $cmd"),
63 next;
64 $config{'help_dir'} = $1;
65 }
66 else {
67 $file = $args{$cmd};
68 $file =~ m#^([\w\.\-/]+)$#
69 or $self->log(LOGERROR,
70 "Invalid charachters in filename for command $cmd"),
71 next;
72 $file = $1;
73 if ($file =~ m#/#) {
74 -e $file
75 or $self->log(LOGWARN, "No help file for command '$cmd'"),
76 next;
77 }
78 else {
79 $file = "help/$file";
80 if (-e "help/$file") { ## FIXME: path
81 $file = "help/$file";
82 }
83 else {
84 $self->log(LOGWARN, "No help file for command '$cmd'");
85 next;
86 }
87 }
88 $config{lc $cmd} = $file;
89 }
90 }
91 return DECLINED;
92 }
93
94 sub hook_help {
95 my ($self, $transaction, @args) = @_;
96 my ($help, $cmd);
97
98 if ($config{not_implemented}) {
99 $self->qp->respond(502, "Not implemented.");
100 return DONE;
101 }
102
103 return OK, "Try 'HELP COMMAND' for getting help on COMMAND"
104 unless $args[0];
105
106 $cmd = lc $args[0];
107
108 unless ($cmd =~ /^(\w+)$/) { # else someone could request
109 # "HELP ../../../../../../../../etc/passwd"
110 $self->qp->respond(502, "Invalid command name");
111 return DONE;
112 }
113 $cmd = $1;
114
115 if (exists $config{$cmd}) {
116 $help = read_helpfile($config{$cmd}, $cmd)
117 or $self->log(LOGERROR, "failed to open help file for $cmd: $!"),
118 return OK, "No help available for SMTP command: $cmd";
119 }
120 elsif (exists $config{'help_dir'} && -e $config{'help_dir'} . "/$cmd") {
121 $help = read_helpfile($config{help_dir} . "/$cmd", $cmd)
122 or $self->log(LOGERROR, "failed to open help file for $cmd: $!"),
123 return OK, "No help available for SMTP command: $cmd";
124 }
125 $help = "No help available for SMTP command: $cmd" # empty file
126 unless $help;
127 return OK, split(/\n/, $help);
128 }
129
130 sub read_helpfile {
131 my ($file, $cmd) = @_;
132 my $help;
133 open HELP, $file
134 or return undef;
135 {
136 local $/ = undef;
137 $help = <HELP>;
138 };
139 close HELP;
140 return $help;
141 }
142