Author: Not specified Language: text
Description: Not specified Timestamp: 2012-11-29 12:35:05 -0500
View raw paste Reply
  1. # Ibid Greeter Plugin
  2. # Released under the GNU GPL License. Compatible with the MIT/X/Expat.
  3. # Wesley Werner
  4. #
  5. # This plugin watches public channel activity, it marks the channel as
  6. # idle if there is no chat after idle_timeout seconds. When a stranger
  7. # joins within this idle period, the plugin will hilight them with a
  8. # choice greeter message.
  9. #
  10. # plugin config values (with defaults):
  11. #
  12. # idle_timeout = 300
  13. #   Seconds to pass without channel activity to be idle.
  14. #
  15. # Known Issues:
  16. # The known list of nicks is not persisted between plugin loads. It will
  17. # thus greet old timers when they enter (after a first load), if the
  18. # channel is considered idle.
  19.  
  20. import random
  21. from time import time
  22. from ibid.event import Event
  23. from ibid.plugins import match
  24. from ibid.core import Dispatcher
  25. from ibid.plugins import handler
  26. from ibid.plugins import Processor
  27. from ibid.config import IntOption, BoolOption
  28.  
  29. features = {}
  30.  
  31. features['greeter'] = {
  32.     'description': u'Greet strangers who join while the channel is idle.'
  33.     ,'categories': ('monitor',)
  34. }
  35.  
  36. greeter_msgs = (
  37.     u'Hello, I am a robot greeter. This channel is a bit quiet at the ' \
  38.     'moment, but say hi anyway and hang around. Someone is bound to ' \
  39.     'respond :)'
  40.     ,
  41. )
  42.  
  43. class Greeter(Processor):
  44.     usage = u"""
  45.         Provider for a greeter for new nicks who enter if the channel is idle.
  46.     """
  47.     feature = ('greeter',)
  48.     addressed = False
  49.     processed = True
  50.    
  51.     idle_timeout = IntOption('idle_timeout'
  52.                     , u'Seconds to pass without channel activity ' \
  53.                     'to consider the channel as idle.'
  54.                     , 120)
  55.     event_types=('state', 'message')
  56.     known_nicks = []
  57.     idle_since = time()
  58.    
  59.     def isIdle(self):
  60.         """
  61.             Return if the idle timeout has passed.
  62.         """
  63.         return int(time() - self.idle_since) >= self.idle_timeout
  64.  
  65.     def remember(self, nick):
  66.         """
  67.             Remember the given nick.
  68.             Returns False if the nick is unknown, i.e. a stranger.
  69.         """
  70.         if not nick in self.known_nicks:
  71.             self.known_nicks.append(nick)
  72.             return False
  73.         return True
  74.  
  75.     @handler
  76.     def watchChannel(self, event):
  77.         """
  78.             Ibid handler watches for channel activity.
  79.         """
  80.         nick = event.sender['nick']
  81.         if event.public:
  82.             if event.type == u'state':
  83.                 if event.state == u'online':
  84.                     if self.isIdle():
  85.                         if not self.remember(nick):
  86.                             event.addresponse(random.choice(greeter_msgs))
  87.                     else:
  88.                                                 # if the channels is not idle, consider newcomers as familiar.
  89.                         self.remember(nick)
  90.             elif event.type == u'message':
  91.                 # conversations resets the idle timer
  92.                 self.idle_since = time()
View raw paste Reply