summaryrefslogtreecommitdiff
path: root/modules/m_karma.sh
blob: 8d2aed8cddd3f5ee5beac8330e5aee51029a1212 (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
#!/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/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Karma module
#---------------------------------------------------------------------

module_karma_INIT() {
	modinit_API='2'
	modinit_HOOKS='after_load on_PRIVMSG'
	commands_register "$1" 'karma' || return 1
	helpentry_module_karma_description="Provides karma support. Use ++ and -- after a string in a channel to change karma."
	helpentry_karma_karma_syntax='<string>'
	helpentry_karma_karma_description='Get current karma for <string>.'
}

module_karma_UNLOAD() {
	unset module_karma_SELECT
	unset module_karma_INSERT module_karma_UPDATE module_karma_set_INSERT_or_UPDATE
	unset module_karma_substract module_karma_add module_karma_check
	unset module_karma_is_nick
	unset module_karma_check_return
	return 0
}

module_karma_REHASH() {
	return 0
}

module_karma_after_load() {
	modules_depends_register "karma" "sqlite3" || {
		# This error reporting is hackish, will fix later.
		if ! list_contains "modules_loaded" "sqlite3"; then
			log_error "The karma module depends upon the SQLite3 module being loaded."
		fi
		return 1
	}
	if [[ -z $config_module_karma_table ]]; then
		log_error "Karma table (config_module_karma_table) must be set in config to use the karma module."
		return 1
	fi
	if ! module_sqlite3_table_exists "$config_module_karma_table"; then
		log_error "karma module: $config_module_karma_table does not exist in the database file."
		log_error "karma module: See comment in doc/karma.sql for how to create the table."
	fi
}

#---------------------------------------------------------------------
## Get an item from DB
## @Type Private
## @param key
## @Stdout The result of the database query.
#---------------------------------------------------------------------
module_karma_SELECT() {
	module_sqlite3_exec_sql "SELECT rating FROM $config_module_karma_table WHERE target='$(module_sqlite3_clean_string "$1")';"
}


#---------------------------------------------------------------------
## Insert a new item into DB
## @Type Private
## @param key
## @param karma
#---------------------------------------------------------------------
module_karma_INSERT() {
	module_sqlite3_exec_sql \
		"INSERT INTO $config_module_karma_table (target, rating) VALUES('$(module_sqlite3_clean_string "$1")', '$(module_sqlite3_clean_string "$2")');"
}


#---------------------------------------------------------------------
## Change the item in DB
## @Type Private
## @param key
## @param karma
#---------------------------------------------------------------------
module_karma_UPDATE() {
	module_sqlite3_exec_sql \
		"UPDATE $config_module_karma_table SET rating='$(module_sqlite3_clean_string "$2")' WHERE target='$(module_sqlite3_clean_string "$1")';"
}

#---------------------------------------------------------------------
## Wrapper, call either INSERT or UPDATE
## @Type Private
## @param key
## @param karma
#---------------------------------------------------------------------
module_karma_set_INSERT_or_UPDATE() {
	if [[ $(module_karma_SELECT "$1") ]]; then
		module_karma_UPDATE "$1" "$2"
	else
		module_karma_INSERT "$1" "$2"
	fi
}

#---------------------------------------------------------------------
## Remove 1 from key
## @Type Private
## @param key to remove from.
#---------------------------------------------------------------------
module_karma_substract() {
	# Clean spaces and convert to lower case
	local keyarray
	read -ra keyarray <<< "$1"
	local key="$(tr '[:upper:]' '[:lower:]' <<< "${keyarray[*]}")"
	local old="$(module_karma_SELECT "$key")"
	# -1 + any old value (yes looks backwards but works)
	local new=-1
	if [[ "$old" ]]; then
		(( new += old ))
	fi
	module_karma_set_INSERT_or_UPDATE "$key" "$new"
}

#---------------------------------------------------------------------
## Add 1 from key
## @Type Private
## @param key to add to.
#---------------------------------------------------------------------
module_karma_add() {
	# Clean spaces and convert to lower case
	local keyarray
	read -ra keyarray <<< "$1"
	local key="$(tr '[:upper:]' '[:lower:]' <<< "${keyarray[*]}")"
	local old="$(module_karma_SELECT "$key")"
	# 1 + any old value
	local new=1
	if [[ "$old" ]]; then
		(( new += old ))
	fi
	module_karma_set_INSERT_or_UPDATE "$key" "$new"
}

#---------------------------------------------------------------------
## Return karma value for key
## The result is returned in $module_karma_check_return
## @Type Private
## @param key to return karma for
## @Globals $module_karma_check_return
#---------------------------------------------------------------------
module_karma_check() {
	# Clean spaces and convert to lower case
	local keyarray
	read -ra keyarray <<< "$1"
	local key="$(tr '[:upper:]' '[:lower:]' <<< "${keyarray[*]}")"
	module_karma_check_return="$(module_karma_SELECT "$key")"
	if [[ -z "$module_karma_check_return" ]]; then
		module_karma_check_return=0
	fi
}

#---------------------------------------------------------------------
## Check if the key is the nick of sender.
## @Type Private
## @param key
## @param sender
## @return 0 If nick and key are same
## @return 1 Otherwise
#---------------------------------------------------------------------
module_karma_is_nick() {
	local keyarray
	read -ra keyarray <<< "$1"
	local key="$(tr '[:upper:]' '[:lower:]' <<< "${keyarray[*]}")"
	local sendernick
	parse_hostmask_nick "$2" 'sendernick'
	local nickarray
	read -ra nickarray <<< "$(tr '[:upper:]' '[:lower:]' <<< "$sendernick")"
	local nick="${nickarray[*]}"
	if [[ "$key" = "$nick" ]]; then
		return 0
	fi
	return 1
}


# Called on a PRIVMSG
#
# $1 = from who (n!u@h)
# $2 = to who (channel or botnick)
# $3 = the message
module_karma_on_PRIVMSG() {
	local sender="$1"
	local query="$3"
	local sendon_channel
	# If it isn't in a channel send message back to person who sent it,
	# otherwise send in channel
	if [[ $2 =~ ^# ]]; then
		sendon_channel="$2"
		# An item must begin with an alphanumeric char.
		if [[ "$query" =~ ^([a-zA-Z0-9].*)\+\+$ ]]; then
			local key="${BASH_REMATCH[1]}"
			if module_karma_is_nick "$key" "$sender"; then
				send_msg "$sendon_channel" "You can't change karma of yourself."
			else
				module_karma_add "$key"
			fi
		elif [[ "$query" =~ ^([a-zA-Z0-9].*)--$ ]]; then
			local key="${BASH_REMATCH[1]}"
			if module_karma_is_nick "$key" "$sender"; then
				send_msg "$sendon_channel" "You can't change karma of yourself."
			else
				module_karma_substract "$key"
			fi
		fi
	else
		parse_hostmask_nick "$sender" 'sendon_channel'
		# Karma is only possible in channels
		if [[ "$query" =~ ^[a-zA-Z0-9].*(--|\+\+)$ ]]; then
			send_notice "$sendon_channel" "You can only change karma in channels."
			return 1
		fi
	fi
	return 0
}

module_karma_handler_karma() {
	local sender="$1"
	local sendon_channel
	if [[ $2 =~ ^# ]]; then
		sendon_channel="$2"
	else
		parse_hostmask_nick "$sender" 'sendon_channel'
	fi
	local parameters="$3"
	if [[ $parameters =~ ^(.+)$ ]]; then
		local key="${BASH_REMATCH[1]}"
		module_karma_check "$key"
		send_msg "$sendon_channel" "Karma for $key is $module_karma_check_return"
	else
		local sendernick
		parse_hostmask_nick "$sender" 'sendernick'
		feedback_bad_syntax "$sendernick" "karma" "<string>"
	fi
}