# Ibid Greeter Plugin
# Released under the GNU GPL License. Compatible with the MIT/X/Expat.
# Wesley Werner
#
# This plugin watches public channel activity, it marks the channel as
# idle if there is no chat after idle_timeout seconds. When a stranger
# joins within this idle period, the plugin will hilight them with a
# choice greeter message.
#
# plugin config values (with defaults):
#
# idle_timeout = 300
# Seconds to pass without channel activity to be idle.
#
# Known Issues:
# The known list of nicks is not persisted between plugin loads. It will
# thus greet old timers when they enter (after a first load), if the
# channel is considered idle.
import random
from time import time
from ibid.event import Event
from ibid.plugins import match
from ibid.core import Dispatcher
from ibid.plugins import handler
from ibid.plugins import Processor
from ibid.config import IntOption, BoolOption
features = {}
features['greeter'] = {
'description': u'Greet strangers who join while the channel is idle.'
,'categories': ('monitor',)
}
greeter_msgs = (
u'Hello, I am a robot greeter. This channel is a bit quiet at the ' \
'moment, but say hi anyway and hang around. Someone is bound to ' \
'respond :)'
,
)
class Greeter(Processor):
usage = u"""
Provider for a greeter for new nicks who enter if the channel is idle.
"""
feature = ('greeter',)
addressed = False
processed = True
idle_timeout = IntOption('idle_timeout'
, u'Seconds to pass without channel activity ' \
'to consider the channel as idle.'
, 120)
event_types=('state', 'message')
known_nicks = []
idle_since = time()
def isIdle(self):
"""
Return if the idle timeout has passed.
"""
return int(time() - self.idle_since) >= self.idle_timeout
def remember(self, nick):
"""
Remember the given nick.
Returns False if the nick is unknown, i.e. a stranger.
"""
if not nick in self.known_nicks:
self.known_nicks.append(nick)
return False
return True
@handler
def watchChannel(self, event):
"""
Ibid handler watches for channel activity.
"""
nick = event.sender['nick']
if event.public:
if event.type == u'state':
if event.state == u'online':
if self.isIdle():
if not self.remember(nick):
event.addresponse(random.choice(greeter_msgs))
else:
# if the channels is not idle, consider newcomers as familiar.
self.remember(nick)
elif event.type == u'message':
# conversations resets the idle timer
self.idle_since = time()