Simplify code on getting active channel modes.

This commit is contained in:
Krytarik Raido 2022-08-03 22:34:04 +02:00
parent a2aa1fa9ea
commit 0d257df7d6

View File

@ -282,7 +282,7 @@ def getBestPattern(n, irc, useIp=False, resolve=True):
if '/' in h: if '/' in h:
host = 'nat/%s/*' % h.split('/')[0] host = 'nat/%s/*' % h.split('/')[0]
k = '*!%s@%s' % (ident, host) k = '*!%s@%s' % (ident, host)
if not k in results: if k not in results:
results.append(k) results.append(k)
extprefix = '' extprefix = ''
extmodes = '' extmodes = ''
@ -347,7 +347,7 @@ class Ircd(object):
if not (channel and irc): if not (channel and irc):
return None return None
self.irc = irc self.irc = irc
if not channel in self.channels: if channel not in self.channels:
self.channels[channel] = Chan(self, channel) self.channels[channel] = Chan(self, channel)
return self.channels[channel] return self.channels[channel]
@ -355,7 +355,7 @@ class Ircd(object):
if not (nick and irc): if not (nick and irc):
return None return None
self.irc = irc self.irc = irc
if not nick in self.nicks: if nick not in self.nicks:
self.nicks[nick] = Nick(self.logsSize) self.nicks[nick] = Nick(self.logsSize)
return self.nicks[nick] return self.nicks[nick]
@ -1019,7 +1019,7 @@ class Chan(object):
return self._lists return self._lists
def getItemsFor(self, mode): def getItemsFor(self, mode):
if not mode in self._lists: if mode not in self._lists:
self._lists[mode] = ircutils.IrcDict() self._lists[mode] = ircutils.IrcDict()
return self._lists[mode] return self._lists[mode]
@ -1033,7 +1033,7 @@ class Chan(object):
if len(L): if len(L):
for item in L: for item in L:
(uid, oper, kind, removed_at) = item (uid, oper, kind, removed_at) = item
if not kind in total: if kind not in total:
total[kind] = {} total[kind] = {}
total[kind]['active'] = 0 total[kind]['active'] = 0
total[kind]['removed'] = 0 total[kind]['removed'] = 0
@ -1041,9 +1041,9 @@ class Chan(object):
total[kind]['active'] += 1 total[kind]['active'] += 1
else: else:
total[kind]['removed'] += 1 total[kind]['removed'] += 1
if not oper in opers: if oper not in opers:
opers[oper] = {} opers[oper] = {}
if not kind in opers[oper]: if kind not in opers[oper]:
opers[oper][kind] = {} opers[oper][kind] = {}
opers[oper][kind]['active'] = 0 opers[oper][kind]['active'] = 0
opers[oper][kind]['removed'] = 0 opers[oper][kind]['removed'] = 0
@ -1075,7 +1075,7 @@ class Chan(object):
l = {} l = {}
if not self.syn: if not self.syn:
checkUser = False checkUser = False
if not value in l: if value not in l:
i = Item() i = Item()
i.channel = self.name i.channel = self.name
i.mode = mode i.mode = mode
@ -2826,29 +2826,25 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
def getIrc(self, irc): def getIrc(self, irc):
# init irc db # init irc db
if not irc.network in self._ircs: if irc.network not in self._ircs:
self._ircs[irc.network] = Ircd( self._ircs[irc.network] = Ircd(
irc, self.registryValue('logsSize')) irc, self.registryValue('logsSize'))
return self._ircs[irc.network] return self._ircs[irc.network]
def getChan(self, irc, channel): def getChan(self, irc, channel):
i = self.getIrc(irc) i = self.getIrc(irc)
if not channel in i.channels: if channel not in i.channels:
# restore channel state, load lists # restore channel state, load lists
modesToAsk = ''.join(self.registryValue('modesToAsk', channel=channel, network=irc.network)) modesToAsk = ''.join(self.registryValue('modesToAsk', channel=channel, network=irc.network))
modesWhenOpped = ''.join(self.registryValue('modesToAskWhenOpped', channel=channel, network=irc.network)) modesWhenOpped = ''.join(self.registryValue('modesToAskWhenOpped', channel=channel, network=irc.network))
if channel in irc.state.channels: if channel in irc.state.channels:
if irc.state.channels[channel].isHalfopPlus(irc.nick): if len(modesWhenOpped) and irc.state.channels[channel].isHalfopPlus(irc.nick):
if len(modesToAsk) or len(modesWhenOpped):
for m in modesWhenOpped: for m in modesWhenOpped:
i.queue.enqueue(ircmsgs.mode(channel, args=(m,))) i.queue.enqueue(ircmsgs.mode(channel, args=(m,)))
if len(modesToAsk):
for m in modesToAsk: for m in modesToAsk:
i.lowQueue.enqueue(ircmsgs.mode(channel, args=(m,))) i.lowQueue.enqueue(ircmsgs.mode(channel, args=(m,)))
elif len(modesToAsk): if not (self.starting or i.whoxpending):
for m in modesToAsk:
i.lowQueue.enqueue(ircmsgs.mode(channel, args=(m,)))
if not self.starting:
if not i.whoxpending:
i.whoxpending = True i.whoxpending = True
i.lowQueue.enqueue(ircmsgs.who(channel, args=('%tuhnairf,1',))) i.lowQueue.enqueue(ircmsgs.who(channel, args=('%tuhnairf,1',)))
self.forceTickle = True self.forceTickle = True
@ -3194,7 +3190,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
if irc.isChannel(channel) and channel in irc.state.channels: if irc.isChannel(channel) and channel in irc.state.channels:
chan = self.getChan(irc, channel) chan = self.getChan(irc, channel)
b = False b = False
if not mode in chan.dones: if mode not in chan.dones:
chan.dones.append(mode) chan.dones.append(mode)
b = True b = True
i = self.getIrc(irc) i = self.getIrc(irc)
@ -3325,7 +3321,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
u = {} u = {}
L = [] L = []
for item in r: for item in r:
if not item[4][0] in u: if item[4][0] not in u:
u[item[4][0]] = item[4][0] u[item[4][0]] = item[4][0]
L.append(item[4][0]) L.append(item[4][0])
if len(L) == 1: if len(L) == 1:
@ -3360,7 +3356,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
self.forceTickle = True self.forceTickle = True
self._tickle(irc) self._tickle(irc)
return return
if not '/' in msg.prefix.split('@')[1] and n.ip is None: if '/' not in msg.prefix.split('@')[1] and n.ip is None:
if self.registryValue('resolveIp'): if self.registryValue('resolveIp'):
t = world.SupyThread(target=self.resolve, name=format( t = world.SupyThread(target=self.resolve, name=format(
'Resolving %s for %s', msg.prefix, channels), args=(irc, channels, msg.prefix)) 'Resolving %s for %s', msg.prefix, channels), args=(irc, channels, msg.prefix))
@ -4025,9 +4021,9 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
else: else:
prop = 'Pattern%s' % pattern.uid prop = 'Pattern%s' % pattern.uid
key = best key = best
if not prop in chan.spam: if prop not in chan.spam:
chan.spam[prop] = {} chan.spam[prop] = {}
if not key in chan.spam[prop] \ if key not in chan.spam[prop] \
or chan.spam[prop][key].timeout != pattern.life: or chan.spam[prop][key].timeout != pattern.life:
chan.spam[prop][key] = utils.structures.TimeoutQueue(pattern.life) chan.spam[prop][key] = utils.structures.TimeoutQueue(pattern.life)
chan.spam[prop][key].enqueue(key) chan.spam[prop][key].enqueue(key)
@ -4200,7 +4196,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
def addToAsked(self, irc, prefix, data, nick): def addToAsked(self, irc, prefix, data, nick):
toAsk = False toAsk = False
i = self.getIrc(irc) i = self.getIrc(irc)
if not prefix in i.askedItems: if prefix not in i.askedItems:
i.askedItems[prefix] = {} i.askedItems[prefix] = {}
toAsk = True toAsk = True
i.askedItems[prefix][data[0]] = data i.askedItems[prefix][data[0]] = data
@ -4449,7 +4445,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
asked = ''.join(asked) asked = ''.join(asked)
asked = asked.replace(',', '') asked = asked.replace(',', '')
for k in asked: for k in asked:
if not k in chan.dones: if k not in chan.dones:
irc.queueMsg(ircmsgs.mode(channel, args=(k,))) irc.queueMsg(ircmsgs.mode(channel, args=(k,)))
# flush pending queue, if items are waiting # flush pending queue, if items are waiting
self.forceTickle = True self.forceTickle = True
@ -4718,9 +4714,9 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
return False return False
chan = self.getChan(irc, channel) chan = self.getChan(irc, channel)
life = self.registryValue('%sLife' % prop, channel=channel, network=irc.network) life = self.registryValue('%sLife' % prop, channel=channel, network=irc.network)
if not prop in chan.spam: if prop not in chan.spam:
chan.spam[prop] = {} chan.spam[prop] = {}
if not key in chan.spam[prop] or chan.spam[prop][key].timeout != life: if key not in chan.spam[prop] or chan.spam[prop][key].timeout != life:
chan.spam[prop][key] = utils.structures.TimeoutQueue(life) chan.spam[prop][key] = utils.structures.TimeoutQueue(life)
chan.spam[prop][key].enqueue(key) chan.spam[prop][key].enqueue(key)
if len(chan.spam[prop][key]) > limit: if len(chan.spam[prop][key]) > limit:
@ -4785,7 +4781,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
life = self.registryValue('repeatPatternLife', channel=channel, network=irc.network) life = self.registryValue('repeatPatternLife', channel=channel, network=irc.network)
key = 'pattern%s' % channel key = 'pattern%s' % channel
chan = self.getChan(irc, channel) chan = self.getChan(irc, channel)
if not key in chan.repeatLogs or chan.repeatLogs[key].timeout != life: if key not in chan.repeatLogs or chan.repeatLogs[key].timeout != life:
chan.repeatLogs[key] = utils.structures.TimeoutQueue(life) chan.repeatLogs[key] = utils.structures.TimeoutQueue(life)
if self.registryValue('announceRepeatPattern', channel=channel, network=irc.network): if self.registryValue('announceRepeatPattern', channel=channel, network=irc.network):
if self.registryValue('useColorForAnnounces', channel=channel, network=irc.network): if self.registryValue('useColorForAnnounces', channel=channel, network=irc.network):
@ -4827,7 +4823,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
return False return False
chan = self.getChan(irc, channel) chan = self.getChan(irc, channel)
timeout = self.registryValue('repeatLife', channel=channel, network=irc.network) timeout = self.registryValue('repeatLife', channel=channel, network=irc.network)
if not key in chan.repeatLogs or chan.repeatLogs[key].timeout != timeout: if key not in chan.repeatLogs or chan.repeatLogs[key].timeout != timeout:
chan.repeatLogs[key] = utils.structures.TimeoutQueue(timeout) chan.repeatLogs[key] = utils.structures.TimeoutQueue(timeout)
count = self.registryValue('repeatCount', channel=channel, network=irc.network) count = self.registryValue('repeatCount', channel=channel, network=irc.network)
probability = self.registryValue('repeatPercent', channel=channel, network=irc.network) probability = self.registryValue('repeatPercent', channel=channel, network=irc.network)
@ -4848,7 +4844,7 @@ class ChanTracker(callbacks.Plugin, plugins.ChannelDBHandler):
if pattern: if pattern:
self._addTemporaryPattern(irc, channel, pattern, 'single src', False, False) self._addTemporaryPattern(irc, channel, pattern, 'single src', False, False)
return result return result
if not channel in chan.repeatLogs or chan.repeatLogs[channel].timeout != timeout: if channel not in chan.repeatLogs or chan.repeatLogs[channel].timeout != timeout:
chan.repeatLogs[channel] = utils.structures.TimeoutQueue(timeout) chan.repeatLogs[channel] = utils.structures.TimeoutQueue(timeout)
logs = chan.repeatLogs[channel] logs = chan.repeatLogs[channel]
(flag, pattern) = self._computePattern(message, logs, probability, patternLength) (flag, pattern) = self._computePattern(message, logs, probability, patternLength)