Skip to content

Commit d56c4dc

Browse files
committed
New strings, karma penalization for flooding, Greetings probability
1 parent 4467115 commit d56c4dc

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

bot_core/irc_bot.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import sys
33
import re
4+
import random
45
from twisted.words.protocols import irc
56

67
from message_logger import MessageLogger
@@ -97,31 +98,35 @@ def evaluate_command(self, user, channel, msg):
9798
fetch_user = user
9899
elif len(msg_splits) == 2:
99100
fetch_user = msg_splits[1]
101+
else: return
100102
self.msg(channel, self.karma_manager.fetch_karma(fetch_user))
101-
elif msg.startswith('!ciao'):
102-
if len(msg_splits) == 2:
103-
self.msg(channel, self.welcome_machine.ciao(msg_splits[1]))
104103
elif msg.startswith( ('!commands', '!help') ):
105104
if len(msg_splits) == 1:
106105
self.msg(channel, self._help_command() )
107106
elif len(msg_splits) == 2:
108107
self.msg(channel, self._help_command(msg_splits[1]) )
109108

110109
def userJoined(self, user, channel):
111-
"""Called when a user joins the channel"""
110+
"""Called when a user joins the channel (10% chance)"""
112111
ciao_msg = self.welcome_machine.ciao(user)
113-
self.msg(channel, ciao_msg)
112+
if random.randint(1,100) <= self.factory.cm.greeting_probability:
113+
self.msg(channel, ciao_msg)
114114

115115
def karma_update(self, user, channel, msg):
116116
"""Try to modify the Karma for a given nickname"""
117117
receiver_nickname = msg[:-2]
118-
# TODO (sentenza) Check if the given nick is present on DB or if is on chan with /WHO command
119118
if receiver_nickname == user:
120119
self.msg(channel, "%s: you can't alter your own karma!" % user)
121120
return
122-
if self.karmrator.is_rate_limited(user):
123-
waiting_minutes = self.karmrator.user_timeout(user) / 60
124-
self.msg(channel, "%s: you have to wait %s min for your next karmic request!" % (user, waiting_minutes))
121+
limit = self.karmrator.rate_limit(user)
122+
if limit == 2:
123+
waiting_minutes = self.karmrator.user_timeout(user)
124+
self.msg(user, "%s: you have to wait %s sec for your next karmic request!" % (user, waiting_minutes))
125+
return
126+
if limit == 1:
127+
# Penalize User
128+
self.msg(channel, "%s: I warned you... Now you have lost karma. :(" % user )
129+
self.karma_manager.update_karma(user, plus=False)
125130
return
126131
if msg.endswith('++'):
127132
self.karma_manager.update_karma(receiver_nickname, plus=True)

config.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self):
3737
self._bot_nick = self._config_parser.get('pyircbot', 'nick')
3838
self._verbose = False
3939
self._update_data_path()
40+
self.greeting_probability = 30
4041

4142
def _update_data_path(self):
4243
"""Internal method called to update information about data folder. Data folder path depends on channel name."""

karma/karma_manager.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ def fetch_karma(self, cursor, nick=None):
4040
if nick:
4141
cursor.execute("SELECT score FROM %s WHERE nick=?" % self.karma_table, (nick,))
4242
karma_score = cursor.fetchone()
43-
if not karma_score:
43+
if karma_score is None:
4444
return "Karma is inscrutable for %s" % nick
45-
else:
46-
return "%s has karma of %s" % (nick, karma_score[0])
45+
return "%s: %s" % (nick, karma_score[0])
4746

4847
@db_commit
4948
def update_karma(self, cursor, nick, plus=True):

karma/karma_rate.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@
33

44
class KarmaRateLimiter(object):
55

6-
def __init__(self, timeout=3600):
7-
"""timeout in seconds - default 1 h"""
6+
def __init__(self, timeout=360, penalty=3):
7+
"""timeout in seconds - default 6 min"""
88
self.timeout = timeout
9+
self.penalty = penalty
910
# http://goo.gl/ZFmFX
1011
# http://stackoverflow.com/a/5900628
11-
self.user_last_request = defaultdict(int)
12+
self.user_last_request = defaultdict(lambda:[int, int])
13+
# defaultdict needs callable obj
1214

13-
def is_rate_limited(self, nick):
15+
def rate_limit(self, nick):
16+
"""Return 0 if not rate_limited, 1 if has penalization, 2 otherwise"""
1417
now = int(time.time())
1518
if nick not in self.user_last_request:
16-
self.user_last_request[nick] = now
17-
return False
18-
elif (now - self.user_last_request[nick]) < self.timeout:
19-
return True
19+
self.user_last_request[nick] = [now,0]
20+
return 0
21+
elif (now - self.user_last_request[nick][0]) < self.timeout:
22+
# Timeout not expired, so increase the counter
23+
self.user_last_request[nick][1] += 1
24+
# User is rate limited
25+
if self.user_last_request[nick][1] % self.penalty == 0:
26+
# give him the penalization!
27+
return 1
28+
else:
29+
return 2
2030
else:
21-
self.user_last_request[nick] = now
22-
return False
31+
# > timeout OK
32+
self.user_last_request[nick] = [now, 0]
33+
return 0
2334

2435
def user_timeout(self, nick):
2536
"""Return the user specific timeout"""
2637
if nick not in self.user_last_request:
2738
return 0
2839
else:
29-
wait_time = self.timeout - (int(time.time()) - self.user_last_request[nick])
40+
wait_time = self.timeout - (int(time.time()) - self.user_last_request[nick][0])
3041
return wait_time

pyircbot.py

+8
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@
3636
action="store",
3737
default = config_manager.bot_nick,
3838
help="Bot nickname %s" % config_manager.bot_nick)
39+
parser.add_option("-g", "--greeting", dest="greeting_probability",
40+
action="store",
41+
type="int",
42+
default = 30,
43+
help="Greeting probability [1 - 100]")
3944
parser.add_option("-v", "--verbose", dest="verbose",
4045
action="store_true",
4146
help="Print a lot of stuff...")
47+
48+
4249
options, args = parser.parse_args()
4350

4451
# Set options to ConfigManager
@@ -47,6 +54,7 @@
4754
config_manager.channel = options.channel
4855
config_manager.bot_nick = options.nick
4956
config_manager.verbose = options.verbose
57+
config_manager.greeting_probability = options.greeting_probability
5058

5159
#if not options.<something>:
5260
# parser.error('Must choose one option try -n or -c or --help')

0 commit comments

Comments
 (0)