summaryrefslogtreecommitdiff
path: root/modules/m_ctcp.sh
blob: dfbe8746760c89caec7d1084800ad747f13d6dc8 (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
#!/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/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Handle CTCP
#---------------------------------------------------------------------

module_ctcp_INIT() {
	modinit_API='2'
	modinit_HOOKS='after_load on_PRIVMSG'
	helpentry_module_ctcp_description="Answers CTCP requests."
}

module_ctcp_UNLOAD() {
	return 0
}

module_ctcp_REHASH() {
	return 0
}

module_ctcp_after_load() {
	if [[ -z $config_module_ctcp_version_reply ]]; then
		log_error "VERSION reply (config_module_ctcp_version_reply) must be set in config to use CTCP module."
		return 1
	fi
}

# Called on a PRIVMSG
#
# $1 = from who (n!u@h)
# $2 = to who (channel or botnick)
# $3 = the message
module_ctcp_on_PRIVMSG() {
	local sender="$1"
	local query="$3"
	# We can't use regex here. For some unknown reason bash drops \001 from
	# regex.
	if [[ $query = $'\001'* ]]; then
		# Get rid of \001 in the string.
		local data="${query//$'\001'}"
		local ctcp_command ctcp_parameters
		# Split it up into command and any parameters.
		read -r ctcp_command ctcp_parameters <<< "$data"
		local sendernick
		parse_hostmask_nick "$sender" 'sendernick'
		case "$ctcp_command" in
			"CLIENTINFO")
				send_nctcp "$sendernick" "CLIENTINFO CLIENTINFO PING SOURCE TIME VERSION"
				;;
			"PING")
				send_nctcp "$sendernick" "PING $ctcp_parameters"
				;;
			"SOURCE")
				send_nctcp "$sendernick" "SOURCE http://envbot.org"
				;;
			"TIME")
				send_nctcp "$sendernick" "TIME $(date +'%Y-%m-%d %k:%M:%S')"
				;;
			"VERSION")
				send_nctcp "$sendernick" "VERSION $config_module_ctcp_version_reply"
				;;
			*)
				# So we didn't handle this CTCP? Return 0 then, someone else may want it.
				return 0
				;;
		esac
		# See above. We didn't fall back to not handle it and did not return
		# so therefore we must have handled it.
		return 1
	fi
	return 0
}