summaryrefslogtreecommitdiff
path: root/modules/m_modules.sh
blob: 62b391730de4323c70fb502a55d363695238a477 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
# -*- coding: utf-8 -*-
###########################################################################
#                                                                         #
#  envbot - an IRC bot in bash                                            #
#  Copyright (C) 2007-2008  Arvid Norlander                               #
#                                                                         #
#  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/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Manage (load/unload/list) modules.
#---------------------------------------------------------------------

module_modules_INIT() {
	modinit_API='2'
	modinit_HOOKS=''
	commands_register "$1" 'modload'   || return 1
	commands_register "$1" 'modunload' || return 1
	commands_register "$1" 'modreload' || return 1
	commands_register "$1" 'modlist'   || return 1
	helpentry_module_modules_description="Exposes the internal module loading and unloading support to owners."

	helpentry_modules_modload_syntax='<module name>'
	helpentry_modules_modload_description='Try to load a module.'

	helpentry_modules_modunload_syntax='<module name>'
	helpentry_modules_modunload_description='Try to unload a module.'

	helpentry_modules_modreload_syntax='<module name>'
	helpentry_modules_modreload_description='Try to unload and reload a module.'

	helpentry_modules_modlist_syntax=''
	helpentry_modules_modlist_description='List currently loaded moudules.'

}

module_modules_UNLOAD() {
	unset module_modules_doload module_modules_dounload
}

module_modules_REHASH() {
	return 0
}

#---------------------------------------------------------------------
## Load a module
## @param Module to load
## @param Sender nick
#---------------------------------------------------------------------
module_modules_doload() {
	local target_module="$1"
	local sendernick="$2"
	modules_load "$target_module"
	local status_message status=$?
	case $status in
		0) status_message="Loaded \"$target_module\" successfully" ;;
		2) status_message="Module \"$target_module\" is already loaded" ;;
		3) status_message="Failed to source \"$target_module\" in safe subshell, see log for details" ;;
		4) status_message="Failed to source \"$target_module\"" ;;
		5) status_message="Module \"$target_module\" could not be found" ;;
		6) status_message="Getting hooks from \"$target_module\" failed" ;;
		7) status_message="after_load failed for \"$target_module\", see log for details" ;;
		*) status_message="Unknown error (code $status) for \"$target_module\"" ;;
	esac
	send_notice "$sendernick" "$status_message"
	return $status
}

#---------------------------------------------------------------------
## Unload a module
## @param Module to unload
## @param Sender nick
#---------------------------------------------------------------------
module_modules_dounload() {
	local target_module="$1"
	local sendernick="$2"
	if [[ $target_module == modules ]]; then
		send_msg "$sendernick" \
			"You can't unload/reload the modules module using itself. (The hackish way would be to use the eval module for this.)"
		return 1
	fi
	modules_unload "$target_module"
	local status_message status=$?
	case $status in
		0) status_message="Unloaded \"$target_module\" successfully" ;;
		2) status_message="Module \"$target_module\" is not loaded" ;;
		3) status_message="Module \"$target_module\" can't be unloaded, some these module(s) depend(s) on it: $(modules_depends_list_deps "$target_module")" ;;
		*) status_message="Unknown error (code $status) for \"$target_module\"" ;;
	esac
	send_notice "$sendernick" "$status_message"
	return $status
}

module_modules_handler_modload() {
	# Accept this anywhere, unless someone can give a good reason not to.
	local sender="$1"
	local sendernick
	parse_hostmask_nick "$sender" 'sendernick'
	local parameters="$3"
	if [[ "$parameters" =~ ^([^ ]+) ]]; then
		local target_module="${BASH_REMATCH[1]}"
		if access_check_owner "$sender"; then
			access_log_action "$sender" "loaded the module $target_module"
			module_modules_doload "$target_module" "$sendernick"
		else
			access_fail "$sender" "load a module" "owner"
		fi
	else
		feedback_bad_syntax "$sendernick" "modload" "<module name>"
	fi
}

module_modules_handler_modunload() {
	local sender="$1"
	local sendernick
	parse_hostmask_nick "$sender" 'sendernick'
	local parameters="$3"
	if [[ "$parameters" =~ ^([^ ]+) ]]; then
		local target_module="${BASH_REMATCH[1]}"
		if access_check_owner "$sender"; then
			access_log_action "$sender" "unloaded the module $target_module"
			module_modules_dounload "$target_module" "$sendernick"
		else
			access_fail "$sender" "unload a module" "owner"
		fi
	else
		feedback_bad_syntax "$sendernick" "modunload" "<module name>"
	fi
}

module_modules_handler_modreload() {
	local sender="$1"
	local sendernick
	parse_hostmask_nick "$sender" 'sendernick'
	local parameters="$3"
	if [[ "$parameters" =~ ^([^ ]+) ]]; then
		local target_module="${BASH_REMATCH[1]}"
		if access_check_owner "$sender"; then
			access_log_action "$sender" "reloaded the module $target_module"
			module_modules_dounload "$target_module" "$sendernick"
			if [[ $? = 0 ]]; then
				module_modules_doload "$target_module" "$sendernick"
			else
				send_notice "$sendernick" "Reload of $target_module failed because it could not be unloaded."
			fi
		else
			access_fail "$sender" "reload a module" "owner"
		fi
	else
		feedback_bad_syntax "$sendernick" "modreload" "<module name>"
	fi
}

module_modules_handler_modlist() {
	local sender="$1"
	local parameters="$3"
	local target
	if [[ $2 =~ ^# ]]; then
		target="$2"
	else
		parse_hostmask_nick "$sender" 'target'
	fi
	local modlist="${modules_loaded## }"
	modlist="${modlist%% }"
	send_msg "$target" "${format_bold}Modules currently loaded${format_bold}: ${modlist//  / }"
}