From c57485583eea3209e57b2d726de135ca81dea40e Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 7 Dec 2013 16:06:47 -0800 Subject: [PATCH 1/4] Timeout for voicing --- config.py | 10 +++++++ plugin.py | 86 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/config.py b/config.py index 9f1e3b8..b6e55eb 100644 --- a/config.py +++ b/config.py @@ -94,6 +94,16 @@ conf.registerChannelValue(TriviaTime.general, 'waitTime', """Time in between the end of one question and the start of another""") ) +conf.registerChannelValue(TriviaTime.voice, 'enableVoice', + registry.Boolean(True, + """Enable voicing of top players for week, month, and year""") + ) + +conf.registerChannelValue(TriviaTime.voice, 'timeoutVoice', + registry.Integer(60, + """The minimum amount of time between anouncing voicing of a user""") + ) + conf.registerChannelValue(TriviaTime.voice, 'numTopToVoice', registry.Integer(10, """The number of top players who are elligible for voice""") diff --git a/plugin.py b/plugin.py index 5872014..4a6cc5b 100644 --- a/plugin.py +++ b/plugin.py @@ -39,6 +39,7 @@ class TriviaTime(callbacks.Plugin): # games info self.games = {} # separate game for each channel + self.voiceTimeouts = self.TimeoutList(self.registryValue('voice.timeoutVoice')) #Database amend statements for outdated versions self.dbamends = {} #Formatted like this: : "; ;" (This IS valid SQL as long as we include the semicolons) @@ -112,16 +113,15 @@ class TriviaTime(callbacks.Plugin): # check the answer self.games[channelCanonical].checkAnswer(msg) - def doJoin(self,irc,msg): - username = msg.nick - # is it a user? - try: - # rootcoma!~rootcomaa@unaffiliated/rootcoma - user = ircdb.users.getUser(msg.prefix) - username = user.name - except KeyError: - pass - channel = msg.args[0] + def voiceUser(self, irc, username, channel): + irc.queueMsg(ircmsgs.voice(channel, username)) + + def handleVoice(self, irc, username, channel): + if not self.registryValue('voice.enableVoice'): + return + timeoutVoice = self.registryValue('voice.timeoutVoice') + self.voiceTimeouts.setTimeout(timeoutVoice) + usernameCanonical = ircutils.toLower(username) dbLocation = self.registryValue('admin.sqlitedb') threadStorage = self.Storage(dbLocation) if self.registryValue('general.globalStats'): @@ -134,26 +134,32 @@ class TriviaTime(callbacks.Plugin): minPointsVoiceWeek = self.registryValue('voice.minPointsVoiceWeek') if len(user) >= 1: if user[13] <= numTopToVoice and user[4] >= minPointsVoiceYear: - irc.sendMsg( - ircmsgs.privmsg(channel, - 'Giving MVP to %s for being top #%d this YEAR' % (username, user[13]) - ) - ) - irc.queueMsg(ircmsgs.voice(channel, username)) + if not self.voiceTimeouts.has(usernameCanonical): + self.voiceTimeouts.append(usernameCanonical) + irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this YEAR' % (username, user[13]))) + self.voiceUser(irc,channel, username) elif user[14] <= numTopToVoice and user[6] >= minPointsVoiceMonth: - irc.sendMsg( - ircmsgs.privmsg(channel, - 'Giving MVP to %s for being top #%d this MONTH' % (username, user[14]) - ) - ) - irc.queueMsg(ircmsgs.voice(channel, username)) + if not self.voiceTimeouts.has(usernameCanonical): + self.voiceTimeouts.append(usernameCanonical) + irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this MONTH' % (username, user[14]))) + self.voiceUser(irc,channel, username) elif user[15] <= numTopToVoice and user[8] >= minPointsVoiceWeek: - irc.sendMsg( - ircmsgs.privmsg(channel, - 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]) - ) - ) - irc.queueMsg(ircmsgs.voice(channel, username)) + if not self.voiceTimeouts.has(usernameCanonical): + self.voiceTimeouts.append(usernameCanonical) + irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]))) + self.voiceUser(irc,channel, username) + + def doJoin(self,irc,msg): + username = msg.nick + # is it a user? + try: + # rootcoma!~rootcomaa@unaffiliated/rootcoma + user = ircdb.users.getUser(msg.prefix) + username = user.name + except KeyError: + pass + channel = msg.args[0] + self.handleVoice(irc, username, channel) def doNotice(self,irc,msg): username = msg.nick @@ -3619,5 +3625,29 @@ class TriviaTime(callbacks.Plugin): except: pass + class TimeoutList: + def __init__(self, timeout): + self.timeout = timeout + self.dict = {} + + def setTimeout(self, timeout): + self.timeout = timeout + + def clearTimeout(self): + for k, t in self.dict.items(): + if t < (time.time() - self.timeout): + del self.dict[k] + + def append(self, value): + self.clearTimeout() + self.dict[value] = time.time() + + def has(self, value): + self.clearTimeout() + if value in self.dict: + return True + return False + + Class = TriviaTime # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: From 5d188395db32d8ab6d53e7e345a52eb9193183dc Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 7 Dec 2013 17:00:27 -0800 Subject: [PATCH 2/4] Adding check for ops before voicing --- plugin.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/plugin.py b/plugin.py index 4a6cc5b..afed5f3 100644 --- a/plugin.py +++ b/plugin.py @@ -114,7 +114,24 @@ class TriviaTime(callbacks.Plugin): self.games[channelCanonical].checkAnswer(msg) def voiceUser(self, irc, username, channel): + # irc.nick + prefix = irc.state.nickToHostmask(irc.nick) + cap = ircdb.canonicalCapability('op') + cap = ircdb.makeChannelCapability(channel, cap) + if not ircdb.checkCapability(prefix, cap): + log.error("Bot does not have op capability to voice user %s" % (username)) + return + + prefix = irc.state.nickToHostmask(username) + cap = ircdb.canonicalCapability('voice') + cap = ircdb.makeChannelCapability(channel, cap) + if ircdb.checkCapability(prefix, cap): + return + irc.queueMsg(ircmsgs.voice(channel, username)) + if not self.voiceTimeouts.has(usernameCanonical): + self.voiceTimeouts.append(usernameCanonical) + irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]))) def handleVoice(self, irc, username, channel): if not self.registryValue('voice.enableVoice'): @@ -134,20 +151,11 @@ class TriviaTime(callbacks.Plugin): minPointsVoiceWeek = self.registryValue('voice.minPointsVoiceWeek') if len(user) >= 1: if user[13] <= numTopToVoice and user[4] >= minPointsVoiceYear: - if not self.voiceTimeouts.has(usernameCanonical): - self.voiceTimeouts.append(usernameCanonical) - irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this YEAR' % (username, user[13]))) - self.voiceUser(irc,channel, username) + self.voiceUser(irc, username, channel) elif user[14] <= numTopToVoice and user[6] >= minPointsVoiceMonth: - if not self.voiceTimeouts.has(usernameCanonical): - self.voiceTimeouts.append(usernameCanonical) - irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this MONTH' % (username, user[14]))) - self.voiceUser(irc,channel, username) + self.voiceUser(irc, username, channel) elif user[15] <= numTopToVoice and user[8] >= minPointsVoiceWeek: - if not self.voiceTimeouts.has(usernameCanonical): - self.voiceTimeouts.append(usernameCanonical) - irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]))) - self.voiceUser(irc,channel, username) + self.voiceUser(irc, username, channel) def doJoin(self,irc,msg): username = msg.nick From f6a3471e0c2f52ab750b8fce0777c406d5826e67 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 7 Dec 2013 17:43:17 -0800 Subject: [PATCH 3/4] Adding voice check, voice on correctans --- plugin.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/plugin.py b/plugin.py index afed5f3..51f1c21 100644 --- a/plugin.py +++ b/plugin.py @@ -40,6 +40,7 @@ class TriviaTime(callbacks.Plugin): # games info self.games = {} # separate game for each channel self.voiceTimeouts = self.TimeoutList(self.registryValue('voice.timeoutVoice')) + self.voiceError = self.TimeoutList(120) #Database amend statements for outdated versions self.dbamends = {} #Formatted like this: : "; ;" (This IS valid SQL as long as we include the semicolons) @@ -114,20 +115,16 @@ class TriviaTime(callbacks.Plugin): self.games[channelCanonical].checkAnswer(msg) def voiceUser(self, irc, username, channel): - # irc.nick prefix = irc.state.nickToHostmask(irc.nick) cap = ircdb.canonicalCapability('op') cap = ircdb.makeChannelCapability(channel, cap) if not ircdb.checkCapability(prefix, cap): + if self.voiceError.has(ircutils.toLower(channel)): + return + self.voiceError.append(ircutils.toLower(channel)) log.error("Bot does not have op capability to voice user %s" % (username)) return - prefix = irc.state.nickToHostmask(username) - cap = ircdb.canonicalCapability('voice') - cap = ircdb.makeChannelCapability(channel, cap) - if ircdb.checkCapability(prefix, cap): - return - irc.queueMsg(ircmsgs.voice(channel, username)) if not self.voiceTimeouts.has(usernameCanonical): self.voiceTimeouts.append(usernameCanonical) @@ -136,6 +133,20 @@ class TriviaTime(callbacks.Plugin): def handleVoice(self, irc, username, channel): if not self.registryValue('voice.enableVoice'): return + + prefix = irc.state.nickToHostmask(username) + cap = ircdb.canonicalCapability('voice') + cap = ircdb.makeChannelCapability(channel, cap) + try: + u = ircdb.users.getUser(prefix) + except KeyError: + if ircdb.checkCapability(prefix, cap): + return + else: + for c in u.capabilities: + if cap == c: + return + timeoutVoice = self.registryValue('voice.timeoutVoice') self.voiceTimeouts.setTimeout(timeoutVoice) usernameCanonical = ircutils.toLower(username) @@ -1250,6 +1261,7 @@ class TriviaTime(callbacks.Plugin): self.unmaskedChars = " -'\"_=+&%$#@!~`[]{}?.,<>|\\/:;" # get utilities from base plugin + self.base = base self.games = base.games self.storage = base.storage self.Storage = base.Storage @@ -1289,6 +1301,7 @@ class TriviaTime(callbacks.Plugin): return username = msg.nick + channel = msg.args[0] # is it a user? try: user = ircdb.users.getUser(msg.prefix) @@ -1432,6 +1445,7 @@ class TriviaTime(callbacks.Plugin): log.error('waitTime was set too low (<2 seconds). Setting to 2 seconds') waitTime = time.time() + waitTime self.queueEvent(waitTime, self.nextQuestion) + self.base.handleVoice(self.irc, username, channel) def getHintString(self, hintNum=None): if hintNum == None: From 1243c7310af0dceec975fdeafabd4bdb76043389 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 7 Dec 2013 18:12:27 -0800 Subject: [PATCH 4/4] adding check for op halfop before voicing --- plugin.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugin.py b/plugin.py index 51f1c21..abd41cf 100644 --- a/plugin.py +++ b/plugin.py @@ -143,9 +143,17 @@ class TriviaTime(callbacks.Plugin): if ircdb.checkCapability(prefix, cap): return else: + capop = ircdb.canonicalCapability('op') + capop = ircdb.makeChannelCapability(channel, cap) + caphop = ircdb.canonicalCapability('halfop') + caphop = ircdb.makeChannelCapability(channel, cap) for c in u.capabilities: if cap == c: return + if capop == c: + return + if caphop == c: + return timeoutVoice = self.registryValue('voice.timeoutVoice') self.voiceTimeouts.setTimeout(timeoutVoice)