summaryrefslogtreecommitdiff
path: root/contrib/modules/m_helloworld.sh
blob: 25b6a1058a7450864892c37c18a7fdb40729b551 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/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/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Example module meant to help people who want to make modules for envbot
#---------------------------------------------------------------------

#---------------------------------------------------------------------
## This is called to get a list of hooks that the module provides.
## Use the hook after_load to do other things
## @Type   Module hook
## @Stdout A list of hooks.
#---------------------------------------------------------------------
module_helloworld_INIT() {
	modinit_API='2'
	# Set modinit_HOOKS to the hooks we have.
	modinit_HOOKS='after_load'
	# Register commands, each command handler will have a name like:
	# module_modulename_handler_function
	# Example: module_helloworld_handler_hi
	# If command name and function name are the same you can skip
	# command name.
	commands_register "$1" 'hi' || return 1
	# Here the function name and command name can't be the same,
	# as the command name got space in it. Note that a command can
	# be at most two words.
	commands_register "$1" 'hello_world' 'hello world' || return 1
	helpentry_module_helloworld_description="This is an example module."

	helpentry_helloworld_hi_syntax='<target> <message>'
	helpentry_helloworld_hi_description='Send a greeting to <target> (nick or channel) with the <message>.'

	helpentry_helloworld_helloworld_syntax='<message>'
	helpentry_helloworld_helloworld_description='Send a greeting to the current scope with the one word <message>.'
}

#---------------------------------------------------------------------
## Here we do anything needed to unload the module.
## @Type   Module hook
## @return 0 Unloaded correctly
## @return 1 Failed to unload. On this the bot will quit.
## @Note   This function is NOT called when the bot is exiting. To check for that
## @Note   use the FINALISE hook!
#---------------------------------------------------------------------
module_helloworld_UNLOAD() {
	# Here we unset any functions and variables that we have defined
	# except the hook functions.
	unset module_helloworld_variable module_helloworld_function
}

#---------------------------------------------------------------------
## Here do anything needed at rehash
## @Type   Module hook
## @return 0 Rehashed correctly
## @return 1 Non fatal error for the bot itself. The bot will call UNLOAD on the module.
## @return 2 Fatal error of some kind. On this the bot will quit.
#---------------------------------------------------------------------
module_helloworld_REHASH() {
	# We don't have anything to do here.
	return 0
}

#---------------------------------------------------------------------
## Called after all the hooks are added for the module.
## @Type   Module hook
## @return 0 Unloaded correctly
## @return 1 Failed. On this the bot will call unload on the module.
#---------------------------------------------------------------------
module_helloworld_after_load() {
	# Set a global variable, this can't be done in INIT.
	# Remember to unset all global variables on UNLOAD!
	module_helloworld_variable="world!"
}

#---------------------------------------------------------------------
## This logs "hello world" as an informative level log item
## when called
## @Type Private
## @Note Note that this is a custom function used by
## @Note some other part of the script
#---------------------------------------------------------------------
module_helloworld_function() {
	# Lets use the variable defined above!
	log_info "Hello $module_helloworld_variable"
}

#---------------------------------------------------------------------
## Called on the command "hello world"
## @Type  Function handler
## @param From who (n!u@h)
## @param To who (channel or botnick)
## @param The parameters to the command
#---------------------------------------------------------------------
module_helloworld_handler_hello_world() {
	local sender="$1"
	local target
	# If it isn't in a channel send message back to person who send it,
	# otherwise send in channel
	if [[ $2 =~ ^# ]]; then
		target="$2"
	else
		# parse_hostmask_nick gets the nick from a hostmask.
		parse_hostmask_nick "$sender" 'target'
	fi

	local parameters="$3"
	# Check if the syntax for the parameters is correct!
	# Lets check for one parameter without spaces
	if [[ "$parameters" =~ ^([^ ]+) ]]; then
		# Store the bit in the first group of the regex into
		# the variable message
		local message="${BASH_REMATCH[1]}"
		# Send a hello world message:
		send_msg "$target" "Hello world! I had the parameter $message"
	else
		# So the regex for matching parameters didn't work, lets provide
		# the user with some feedback!
		local sendernick
		parse_hostmask_nick "$sender" 'sendernick'
		feedback_bad_syntax "$sendernick" "hello world" "<message> # Where message is one word!"
	fi
}

#---------------------------------------------------------------------
## Called on the command "hi"
## @Type  Function handler
## @param From who (n!u@h)
## @param To who (channel or botnick)
## @param The parameters to the command
#---------------------------------------------------------------------
module_helloworld_handler_hi() {
	local sender="$1"

	local parameters="$3"
	# Two parameters, one is single word, the other matches to
	# end of line.
	if [[ "$parameters" =~ ^([^ ]+)\ (.+) ]]; then
		# Store the groups in some variables.
		local target_channel="${BASH_REMATCH[1]}"
		local message="${BASH_REMATCH[2]}"
		# This is used for the access check below.
		# Check if target is a channel or nick.
		local scope
		if [[ $target_channel =~ ^# ]]; then
			scope="$target_channel"
		else
			scope="MSG"
		fi

		# Lets check for access.
		# First variable is capability to check for
		# Second variable is the hostmask of the sender of the message
		# Third variable is the scope, that we set above.
		if access_check_capab "hi" "$sender" "$scope"; then
			# Such important events for security as a "hi" should
			# really get logged even if it fails! ;)
			access_log_action "$sender" "made the hi channel \"$message\" in/to \"$target_channel\""
			local sendernick
			parse_hostmask_nick "$sender" 'sendernick'
			send_msg "${target_channel}" "Hi $target_channel! $sendernick wants you to know ${message}"
			# As an example also call our function.
			module_helloworld_function
		else
			# Lets tell the sender they lack access!
			# access_fail will send a PRIVMSG to the sender saying permission denied
			# and also log the failed attempt.
			access_fail "$sender" "make the bot hi" "hi"
		fi
	else
		# As above, provide feedback about bad syntax.
		local sendernick
		parse_hostmask_nick "$sender" 'sendernick'
		feedback_bad_syntax "$sendernick" "hi" "<target> <message> # Where target is a nick or channel"
	fi
}