summaryrefslogtreecommitdiff
path: root/modules/m_help.sh
blob: 3fdd9b92bd36bbc5eb0997a33629908d0a00ef01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# -*- coding: utf-8 -*-
###########################################################################
#                                                                         #
#  envbot - an IRC bot in bash                                            #
#  Copyright (C) 2007-2008  Arvid Norlander                               #
#  Copyright (C) 2007-2008  Vsevolod Kozlov                               #
#                                                                         #
#  This program is free software: you can redistribute it and/or modify   #
#  it under the terms of the GNU General Public License as published by   #
#  the Free Software Foundation, either version 3 of the License, or      #
#  (at your option) any later version.                                    #
#                                                                         #
#  This program is distributed in the hope that it will be useful,        #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of         #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
#  GNU General Public License for more details.                           #
#                                                                         #
#  You should have received a copy of the GNU General Public License      #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Provides help command.
#---------------------------------------------------------------------

module_help_INIT() {
	modinit_API='2'
	modinit_HOOKS=''
	commands_register "$1" 'help' || return 1
	commands_register "$1" 'modinfo' || return 1
	helpentry_module_help_description="Provides help and information for commands and modules."

	helpentry_help_help_syntax='<command>'
	helpentry_help_help_description='Displays help for command <command>'

	helpentry_help_modinfo_syntax='<module>'
	helpentry_help_modinfo_description='Displays a description for module <module>'
}

module_help_UNLOAD() {
	unset module_help_fetch_module_function_data
	unset module_help_fetch_module_data
}

module_help_REHASH() {
	return 0
}

module_help_fetch_module_function_data() {
	local module_name="$1"
	local function_name="$2"
	local target_syntax="$3"
	local target_description="$4"

	local varname_syntax="helpentry_${module_name}_${function_name}_syntax"
	local varname_description="helpentry_${module_name}_${function_name}_description"
	if [[ -z ${!varname_description} ]]; then
		return 1
	fi

	printf -v "$target_description" '%s' "${!varname_description}"

	if [[ ${!varname_syntax} ]]; then
		printf -v "$target_syntax" '%s' " ${!varname_syntax}"
	fi
}

module_help_fetch_module_data() {
	local module_name="$1"
	local target_description="$2"

	local varname_description="helpentry_module_${module_name}_description"
	if [[ -z ${!varname_description} ]]; then
		return 1
	fi

	printf -v "$target_description" '%s' "${!varname_description}"
}

module_help_handler_help() {
	local sender="$1"
	local parameters="$3"
	if [[ $parameters =~ ^([a-zA-Z0-9][^ ]*)( [^ ]+)? ]]; then
		local command_name="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
		# Look where we will reply to. We will not reply in the channel, even if the request was made in a channel, unless appropriate option is set
		local target
		if [[ $2 =~ ^# && $config_module_help_reply_in_channel == 1 ]]; then
			target="$2"
		else
			parse_hostmask_nick "$sender" 'target'
		fi
		# Get the module name the command belongs to.
		local module_name=
		commands_provides "$command_name" 'module_name'
		# Extract the function name.
		local function_name=
		hash_get 'commands_list' "$command_name" 'function_name'
		if [[ $function_name =~ ^module_${module_name}_handler_(.+)$ ]]; then
			function_name="${BASH_REMATCH[1]}"
		fi
		# Finally get the data for a specific function in specific module.
		local syntax=
		local description=
		module_help_fetch_module_function_data "$module_name" "$function_name" syntax description || {
			send_notice "$target" "Sorry, no help for ${format_bold}${command_name}${format_bold}"
			return
		}
		# And send it back to the user.
		if [[ $config_module_help_reply_in_one_line == 1 ]]; then
			send_notice "$target" "${format_bold}${command_name}${format_bold}$syntax -- $description"
		else
			send_notice "$target" "${format_bold}${command_name}${format_bold}$syntax"
			send_notice "$target" "$description"
		fi
	else
		local sendernick=
		parse_hostmask_nick "$sender" 'sendernick'
		feedback_bad_syntax "$sendernick" "help" "<command>"
	fi
}

module_help_handler_modinfo() {
	local sender="$1"
	local parameters="$3"
	if [[ $parameters =~ ^([^ ]+) ]]; then
		local module_name="${BASH_REMATCH[1]}"
		# See module_help_handler_help
		local target
		if [[ $2 =~ ^# && $config_module_help_reply_in_channel == 1 ]]; then
			target="$2"
		else
			parse_hostmask_nick "$sender" 'target'
		fi
		local description=
		module_help_fetch_module_data "$module_name" description || {
			send_notice "$target" "Sorry, no information for module ${format_bold}${module_name}${format_bold}"
			return
		}
		if [[ $config_module_help_reply_in_one_line == 1 ]]; then
			send_notice "$target" "${format_bold}${module_name}${format_bold} -- $description"
		else
			send_notice "$target" "${format_bold}${module_name}${format_bold}"
			send_notice "$target" "$description"
		fi
	else
		local sendernick=
		parse_hostmask_nick "$sender" sendernick
		feedback_bad_syntax "$sendernick" "modinfo" "<module>"
	fi
}