3 from datetime
import datetime
4 from collections
import deque
6 class LimitManager(object):
7 def __init__(self
, module
):
10 self
._delay
= self
.module
.options
.get('delay', int, 60)
13 def insert(self
, channel
):
14 """Someone joined or left a channel, wait DELAY seconds then fix the limit.
16 self
.module
.elog
.debug('Added: %s' % channel
)
17 self
._queue
.append({'channel': channel, 'time': datetime.now()}
)
19 def queue_monitor(self
):
20 """Loops over the queue every second to check for channels to be updated.
22 while self
._queue
and (datetime
.now() - self
._queue
[0]['time']).seconds
>= self
._delay
:
23 element
= self
._queue
.popleft()
24 self
.update_channel(element
['channel'])
27 """Starts the queue monitor.
29 self
._monitor
= task
.LoopingCall(self
.queue_monitor
)
30 self
._monitor
.start(1, True)
33 """Stops the queue monitor.
37 def set_delay(self
, new_delay
):
38 """Changes interval between channel checks.
40 if self
._delay
== new_delay
:
44 self
._delay
= new_delay
45 self
.module
.options
.set('delay', new_delay
)
48 def update_channel(self
, channel
):
49 """Updates the limit for a channel,
50 the new limit depends on the amount of users.
52 chan
= self
.module
.inter
.findChannel(channel
)
55 cname
= chan
.getName()
57 if not self
.module
.channels
.is_valid(cname
):
60 old_limit
= chan
.getLimit()
61 users
= chan
.getUsers().size()
68 new_limit
= users
+ 10
70 new_limit
= users
+ 12
72 if new_limit
== old_limit
or abs(new_limit
- old_limit
) < 2: #change mode only if the difference is >= 2
75 self
.module
.inter
.mode(self
.module
.nick
, cname
, "+l %d" % new_limit
)
76 self
.module
.elog
.command('%(chan)s [%(users)d users] Changed limit %(old_limit)d => %(new_limit)d' % {
79 'old_limit': old_limit
,
80 'new_limit': new_limit
})
83 """"Sets correct limits for all channels we joined,
84 only used when the bot is loaded.
86 for channel
in self
.module
.channels
.list_valid():
87 self
.update_channel(channel
.name
)
89 def __contains__(self
, channel
):
90 """Checks if the channel is already queued for updating.
92 for element
in self
._queue
:
93 if element
['channel'].lower() == channel
.lower():
99 return len(self
._queue
)