summaryrefslogtreecommitdiffstats
path: root/ircbot.bash
blob: e3fc56a2e218f0b50d756900b93d318a09aa1539 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash


IRCBOT_VERSION=0.1.0


# Modular IRC bot framework in bash
#
# Modules should be ./modules/*.bash
#
# Run as `bash ircbot.bash path/to/config.bash`


# Exposed variables:
#
# IRCBOT_MODULE
#     the name of the current module
#
# IRCBOT_VERSION
#     the version of the bot
#
# See config.bash for other exposed variables


# Exposed functions:
#
# verbose $format $args...
#     log a message prefixed with the current module name to stderr (and error
#     log if using modules/log); same arguments as printf
#
# parse_source_nick $source
#     prints nick from source; returns 1 when no nick
#
# parse_source_user $source
#     prints username from source; returns 1 when no username
#
# parse_source_host $source
#     prints hostname from source; returns 1 when no hostname
#
# parse_targeted_nick $msg
#     prints targeted nick (message prefixed with "nick: " or "nick, ") from a PRIVMSG message
#
# parse_targeted_msg $msg
#     prints message part of a targeted message (all but the "nick: " or "nick, " prefix) from a PRIVMSG message
#
# parse_ctcp_command $msg
#     prints the CTCP command from a PRIVMSG message; returns 1 if not valid CTCP message
#
# parse_ctcp_message $msg
#     prints the CTCP message from a PRIVMSG message; returns 1 if not valid CTCP message
#
# trimrn
#     removes all linebreaks and carriage returns from stdin
#
# indexof $haystack $needle
#     prints the index of $needle in $haystack or -1 if not found
#
# sendmsg $command $args...
#     sends an IRC command; only the last argument may contain spaces
# 


set -euo pipefail

IRCBOT_MODULE=ircbot

_events=(connect disconnect readmsg sendmsg privmsg dm ctcp self_join self_kick self_part self_invite)

_ircbot_cleanup=()

verbose() {
	if [ -n "$IRCBOT_VERBOSE" ] && [ "$IRCBOT_VERBOSE" -ne 0 ]; then
		printf "[%s] $1\n" "$IRCBOT_MODULE" "${@:2}" >&2
	fi
}

connect() {
	if [[ "$IRCBOT_TLS" -ne 0 ]]; then
		local fifo
		fifo="$(mktemp -u --tmpdir "ircbot.XXXXXX.fifo")" || return $?
		mkfifo "$fifo" || return $?
		_ircbot_cleanup+=("$fifo")
		openssl s_client -quiet -connect "$IRCBOT_HOST:$IRCBOT_PORT" <"$fifo" >"$fifo" &
		exec {_ircbot_fd}<>"$fifo"
	else
		if ! exec {_ircbot_fd}<>"/dev/tcp/$IRCBOT_HOST/$IRCBOT_PORT"; then
			return $?
		fi
	fi

	if [[ -n "$IRCBOT_PASS" ]]; then
		if ! sendmsg PASS "$IRCBOT_PASS"; then
			return $?
		fi
	fi
	if ! sendmsg NICK "$IRCBOT_NICK"; then
		return $?
	fi
	if ! sendmsg USER "$IRCBOT_LOGIN 8 *" "$IRCBOT_REALNAME"; then
		return $?
	fi

	return 0
}


#nick!login@host
# 1-nick, 2-login, 3-host
_source_regex='^\([^!]\+\)!\([^@]\+\)@\(.*\)$'

parse_source_nick() { # args: $1 - source
	sed "s/$_source_regex/\\1/" <<< "$1"
}

parse_source_login() { # args: $1 - source
	sed "s/$_source_regex/\\2/" <<< "$1"
}

parse_source_host() { # args: $1 - source
	sed "s/$_source_regex/\\3/" <<< "$1"
}


_ctcp_regex='^\x01\([A-Za-z]\+\) \?\([^\x01]*\)\x01$'

parse_ctcp_command() { # args: $1 - PRIVMSG message
	local ctcp
	ctcp="$(sed "s/$_ctcp_regex/\\U\\1/" <<< "$1")"
	if [[ $ctcp != "$1" ]]; then
		printf "%s\n" "$ctcp"
		return 0
	fi
	return 1
}

parse_ctcp_message() { # args: $1 - PRIVMSG message
	local msg
	msg="$(sed "s/$_ctcp_regex/\\2/" <<< "$1")"
	if [[ $msg != "$1" ]]; then
		printf "%s\n" "$ctcp"
		return 0
	fi
	return 1
}


_targeted_regex='^\([^ /|,!:]\+\)[:,] \(.\+\)'

parse_targeted_nick() { # args: $1 - PRIVMSG message
	local target
	target="$(sed "s/$_targeted_regex/\\1/" <<< "$1")"
	if [[ -n $target && $target != "$1" ]]; then
		sed : <<< "$target"
		return 0
	fi
	return 1
}

parse_targeted_msg() { # args: $1 - PRIVMSG message
	local target
	target="$(sed "s/$_targeted_regex/\\2/" <<< "$1")"
	if [[ -n $target && $target != "$1" ]]; then
		sed : <<< "$target"
		return 0
	fi
	return 1
}


trimrn() { # no args, put input to stdin
	#sed -e 's/\r//g' -e 's/\n//g' -e 1q
	tr -d '\r\n'
}

indexof() { # args: $1 - haystack, $2 - needle
	local str search i
	str="$1"
	search="$2"

	i=0;
	while :; do
		if ((i >= ${#str})); then
			i=-1
			break
		fi
		if [[ ${str:$i:1} == "$search" ]]; then
			break
		fi
		((i++))
	done

	echo "$i"
}


sendmsg() { # args: $1 - command, $2... - args (any number)
	local msg oldifs cmd args
	msg="$(sed 's/.*/\U&/' <<< "$1")"
	cmd="$msg"
	shift
	args=("$@")
	if [[ $# -gt 1 ]]; then
		oldifs="$IFS"
		IFS=' '
		msg="$msg ${*:1:(($#-1))}"
		IFS="$oldifs"
		shift $(($#-1))
	fi
	if [[ $# -gt 0 ]]; then
		msg="$msg :$1"
	fi
	msg="$(trimrn <<< "$msg" | sed 's/^\(.\{,'"$IRCBOT_MAX_LINE_LENGTH"'\}\).*/\1/')"
	if ! _trigger sendmsg "$msg" "$cmd" "${args[@]}"; then
		return $?
	fi
	printf "%s\r\n" "$msg" >&$_ircbot_fd
	return $?
}

_readmsg() {
	local line
	IFS= read -r -u $_ircbot_fd -t "${IRCBOT_READ_TIMEOUT:-300}" line
	success=$?
	printf "%s\n" "$line" | trimrn
	return $success
}


_readloop() {
	local orig msg src cmd args pos

	while :; do
		if ! msg="$(_readmsg)"; then
			verbose 'disconnected from server'
			return 1
		fi

		orig="$msg"

		# has source?
		src=
		if [[ ${msg:0:1} == : ]]; then
			pos="$(indexof "$msg" ' ')"
			if [[ $pos -lt 0 ]]; then
				return 1
			fi
			src="${msg:1:$((pos-1))}"
			((++pos))
			msg="${msg:$pos}"
		fi

		# grab the command
		pos="$(indexof "$msg" ' ')"
		if [[ $pos -lt 0 ]]; then
			cmd="$msg"
			msg=
		else
			cmd="${msg:0:$pos}"
			((++pos))
			msg="${msg:$pos}"
		fi

		# parse args, ending when no more spaces or on last arg starting with :
		args=()
		while :; do
			if [[ ${msg:0:1} = : ]]; then
				args+=("${msg:1}")
				break
			fi
			pos="$(indexof "$msg" ' ')"
			if [[ $pos -lt 0 ]]; then
				args+=("$msg")
				break
			fi
			args+=("${msg:0:$pos}")
			((++pos))
			msg="${msg:$pos}"
		done

		#printf "raw: '%s'\n" "$orig"
		#printf "source: '%s'\n" "$src"
		#printf "nick: '%s'\n" "$(parse_source_nick "$src")"
		#printf "command: '%s'\n" "$cmd"
		#printf "args:\n"
		#printf "  %s\n" "${args[@]}"
		#echo


		_handle_msg "$orig" "$src" "$cmd" "${args[@]}"
	done
}

_handle_msg() { # args: $1 - raw message, $2 - source, $3 - command, $4... - args
	local orig src cmd args nick where who why msg ctcp
	orig="$1"
	src="$2"
	cmd="$3"
	args=("${@:4}")


	_trigger readmsg "$orig" "$src" "$cmd" "${args[@]}"


	# XXX sometimes, you have to respond to a PING before registering
	if [[ $_pre_register != 0 && $cmd != NOTICE && $cmd != PING ]]; then
		_pre_register=0
		_trigger connect
	fi



	case "$cmd" in
		JOIN)
			nick="$(parse_source_nick "$src")"
			where="${args[0]:-}"
			if [[ $nick == "$IRCBOT_NICK" ]]; then
				_trigger self_join "$where"
			fi
			;;

		KICK)
			nick="$(parse_source_nick "$src")"
			who="${args[0]}"
			where="${args[1]}"
			why="${args[2]:-}"
			if [[ $who == "$IRCBOT_NICK" ]]; then
				_trigger self_kick "$src" "$where" "$why"
			fi
			;;

		PART)
			nick="$(parse_source_nick "$src")"
			where="${args[0]}"
			why="${args[1]:-}"
			if [[ $nick == "$IRCBOT_NICK" ]]; then
				_trigger self_part "$where" "$why"
			fi
			;;

		INVITE)
			nick="$(parse_source_nick "$src")"
			who="${args[0]}"
			where="${args[1]}"
			if [[ $who == "$IRCBOT_NICK" ]]; then
				_trigger self_invite "$src" "$where"
			fi
			;;

		433)
			verbose 'nick %s is already taken' "$IRCBOT_NICK"
			#exit 1
			return # as if disconnected
			;;

		PRIVMSG)
			where="${args[0]}"
			msg="${args[1]}"

			_trigger privmsg "$src" "$where" "$msg"

			if [[ $where == "$IRCBOT_NICK" ]]; then
				_trigger dm "$src" "$msg"

				if ctcp="$(parse_ctcp_command "$msg")"; then
					_trigger ctcp "$src" "$ctcp" "$(parse_ctcp_message "$msg")"
				fi
			fi
			;;
	esac

}

_trigger() { # args: $1 - event, $2... - event args
	local mod ev fn ret
	ev="$1"
	shift
	ret=0
	for mod in "${IRCBOT_MODULES[@]}"; do
		fn="_module_${mod}_on_${ev}"
		if declare -F "$fn" >/dev/null; then
			IRCBOT_MODULE="$mod"
			if ! "$fn" "$@"; then
				ret=$?
			fi
			IRCBOT_MODULE=ircbot
		fi
	done
	return "$ret"
}

_load_modules() {
	local mod ev
	for mod in "${IRCBOT_MODULES[@]}"; do
		IRCBOT_MODULE="$mod"
		source "modules/$mod.bash"
		IRCBOT_MODULE=ircbot
		verbose "sourced module %s" "$mod"
		for ev in "${_events[@]}"; do
			if declare -F "on_${ev}" >/dev/null; then
				# this is kinda evil
				eval "$(echo "_module_${mod}_on_${ev}()"; declare -f "on_${ev}" | tail -n +2)"
				unset -f "on_${ev}"
				verbose "module %s installed handler %s" "$mod" "$ev"
			fi
		done
	done
}

_main() { # args: $1 - config file
	source "$1"
	_load_modules
	while :; do
		if connect; then
			#_trigger connect
			verbose 'connected'
			sleep "${IRCBOT_SLEEP_CONNECT:-0}"
			_pre_register=1
			_readloop
		fi
		_trigger disconnect
		verbose 'reconnecting in %d seconds...' "${IRCBOT_SLEEP_RECONNECT:-10}"
		sleep "${IRCBOT_SLEEP_RECONNECT:-10}"
	done
	_cleanup
}

_cleanup() {
	local f
	for f in "${_ircbot_cleanup[@]}"; do
		rm -f "$f"
	done
}

_main "$@"