RelayNext: semi-invasive optimizations Part 1

This commit is contained in:
James Lu 2015-02-01 14:13:54 -05:00
parent a6c7d078b4
commit 45879709e5

View File

@ -115,27 +115,12 @@ class RelayNext(callbacks.Plugin):
Returns a colorized version of <text> based on a simple hash algorithm Returns a colorized version of <text> based on a simple hash algorithm
(sum of all characters).""" (sum of all characters)."""
colors = ("05", "04", "03", "09", "02", "12", "06", "13", "10", "11", colors = ('02', '03', '04', '05', '06', '07', '08', '09', '10', '11',
"07") '12', '13')
num = 0 num = sum([ord(char) for char in s])
for i in s:
num += ord(i)
num = num % len(colors) num = num % len(colors)
return "\x03%s%s\x03" % (colors[num], s) return "\x03%s%s\x03" % (colors[num], s)
# This part keeps track of the IrcStates as mentioned above
def __call__(self, irc, msg):
if irc not in self.ircstates:
self.ircstates[irc] = irclib.IrcState()
try:
self.ircstates[irc].addMsg(irc, self.lastmsg[irc])
except KeyError:
self.ircstates[irc].addMsg(irc,
ircmsgs.ping("placeholder message"))
finally:
self.lastmsg[irc] = msg
self.__parent.__call__(irc, msg)
def initializeNetworks(self): def initializeNetworks(self):
for IRC in world.ircs: for IRC in world.ircs:
self.networks[IRC.network.lower()] = IRC self.networks[IRC.network.lower()] = IRC
@ -222,11 +207,19 @@ class RelayNext(callbacks.Plugin):
def checkFlood(self, channel, source, command): def checkFlood(self, channel, source, command):
maximum = self.registryValue("antiflood.maximum", channel) maximum = self.registryValue("antiflood.maximum", channel)
enabled = self.registryValue("antiflood.enable", channel) return len(self.msgcounters[(source, command)]) > maximum
if enabled and len(self.msgcounters[(source, command)]) > maximum:
return True
def relay(self, irc, msg, channel=None): def relay(self, irc, msg, channel=None):
# Keep track of our IRC state files
if irc not in self.ircstates:
self.ircstates[irc] = irclib.IrcState()
try:
self.ircstates[irc].addMsg(irc, self.lastmsg[irc])
except KeyError:
self.ircstates[irc].addMsg(irc,
ircmsgs.ping("placeholder message"))
finally:
self.lastmsg[irc] = msg
channel = channel or msg.args[0] channel = channel or msg.args[0]
ignoredevents = map(str.upper, self.registryValue('events.userIgnored')) ignoredevents = map(str.upper, self.registryValue('events.userIgnored'))
if msg.command in ignoredevents and ircdb.checkIgnored(msg.prefix): if msg.command in ignoredevents and ircdb.checkIgnored(msg.prefix):
@ -239,37 +232,36 @@ class RelayNext(callbacks.Plugin):
out_s = self._format(irc, msg) out_s = self._format(irc, msg)
if out_s: if out_s:
### Begin Flood checking clause ### Begin Flood checking clause
timeout = self.registryValue("antiflood.timeout", channel) if self.registryValue("antiflood.enable", channel):
seconds = self.registryValue("antiflood.seconds", channel) timeout = self.registryValue("antiflood.timeout", channel)
maximum = self.registryValue("antiflood.maximum", channel) seconds = self.registryValue("antiflood.seconds", channel)
try: maximum = self.registryValue("antiflood.maximum", channel)
self.msgcounters[(source, msg.command)].enqueue(msg.prefix) try:
except KeyError: self.msgcounters[(source, msg.command)].enqueue(msg.prefix)
self.msgcounters[(source, msg.command)] = TimeoutQueue(seconds) except KeyError:
if self.checkFlood(channel, source, msg.command): self.msgcounters[(source, msg.command)] = TimeoutQueue(seconds)
self.log.debug("RelayNext (%s): message from %s blocked by " if self.checkFlood(channel, source, msg.command):
"flood preotection.", irc.network, channel) self.log.debug("RelayNext (%s): message from %s blocked by "
if self.floodTriggered: "flood preotection.", irc.network, channel)
return if self.floodTriggered:
c = msg.command.lower() return
e = format("Flood detected on %s (%s %ss/%s seconds), " c = msg.command.lower()
"not relaying %ss for %s seconds!", channel, e = format("Flood detected on %s (%s %ss/%s seconds), "
maximum, c, seconds, c, timeout) "not relaying %ss for %s seconds!", channel,
out_s = self._format(irc, msg, announcement=e) maximum, c, seconds, c, timeout)
self.log.info("RelayNext (%s): %s", irc.network, e) out_s = self._format(irc, msg, announcement=e)
self.floodTriggered = True self.log.info("RelayNext (%s): %s", irc.network, e)
else: self.floodTriggered = True
self.floodTriggered = False else:
self.floodTriggered = False
### End Flood checking clause ### End Flood checking clause
for relay in self.db.values(): for relay in self.db.values():
if source in relay: # If our channel is in a relay if source in relay: # If our channel is in a relay
# Remove ourselves so we don't get duplicated messages # Remove ourselves so we don't get duplicated messages
targets = deepcopy(relay) targets = list(relay)
targets.remove(source) targets.remove(source)
for cn in targets: for cn in targets:
target, net = cn.split("@") target, net = cn.split("@")
if net not in self.networks.keys():
self.initializeNetworks()
try: try:
otherIrc = self.networks[net] otherIrc = self.networks[net]
except KeyError: except KeyError:
@ -283,11 +275,16 @@ class RelayNext(callbacks.Plugin):
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
self.relay(irc, msg) self.relay(irc, msg)
def doJoin(self, irc, msg): def doNonPrivmsg(self, irc, msg):
if self.registryValue("events.relay%ss" % msg.command, msg.args[0]): if self.registryValue("events.relay%ss" % msg.command, msg.args[0]):
self.relay(irc, msg) self.relay(irc, msg)
doTopic = doPart = doKick = doMode = doJoin def doJoin(self, irc, msg):
if msg.nick == irc.nick:
self.initializeNetworks()
self.doNonPrivmsg(irc, msg)
doTopic = doPart = doKick = doMode = doNonPrivmsg
# NICK and QUIT aren't channel specific, so they require a bit # NICK and QUIT aren't channel specific, so they require a bit
# of extra handling # of extra handling