don't attach to pubmsg/privmsg events by default anymore.
this is possible because now the alias stuff in DrBotIRC calls each module's do() on a pubmsg/privmsg. this also gets rid of all the meta options (so remove them from your config file!), and IrcAdmin now only needs to connect to welcome.
This commit is contained in:
		
							parent
							
								
									0bd681c324
								
							
						
					
					
						commit
						fc2814e57c
					
				
							
								
								
									
										163
									
								
								Module.py
									
									
									
									
									
								
							
							
						
						
									
										163
									
								
								Module.py
									
									
									
									
									
								
							| @ -32,10 +32,8 @@ class Module(object): | ||||
|         return 50 | ||||
| 
 | ||||
|     def __init__(self, irc, config, server): | ||||
|         """ | ||||
|         Construct a feature module. Inheritors should not do anything special | ||||
|         here, instead they should implement register_handlers and do, or else this will | ||||
|         be a very uneventful affair. | ||||
|         """Construct a feature module. Inheritors can do special things here, but | ||||
|         should be sure to call Module.__init__. | ||||
|         """ | ||||
| 
 | ||||
|         self.irc = irc | ||||
| @ -60,149 +58,40 @@ class Module(object): | ||||
|         print("loaded " + self.__class__.__name__) | ||||
| 
 | ||||
|     def register_handlers(self, server): | ||||
|         """ | ||||
|         Hook handler functions into the IRC library. This is called by __init__ and sets | ||||
|         up server.add_global_handlers. Classes inheriting from Module could implement this | ||||
|         """Hook handler functions into the IRC library. This is called when the | ||||
|         module is loaded. Classes with special stuff to do could implement this | ||||
|         and set up the appropriate handlers, e.g.: | ||||
| 
 | ||||
|         server.add_global_handler('privmsg', self.on_privmsg) | ||||
|         self.server.add_global_handler('welcome', self.on_connect) | ||||
| 
 | ||||
|         Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your | ||||
|         own. | ||||
|         Unless you're sure what you're doing, don't register pubmsg or privmsg. | ||||
|         They are handled by the alias support in DrBotIRC, and will call your | ||||
|         module's do(). | ||||
|         """ | ||||
| 
 | ||||
|         server.add_global_handler('pubmsg', self.on_pubmsg, self.priority()) | ||||
|         server.add_global_handler('privmsg', self.on_privmsg, self.priority()) | ||||
| 
 | ||||
|     def unregister_handlers(self): | ||||
|         """ | ||||
|         Unhook handler functions from the IRC library. Inverse of the above. | ||||
|         """Unhook handler functions from the IRC library. Inverse of the above. | ||||
|         This is called by reload, to remove the soon-to-be old object from the server | ||||
|         global handlers (or whatever has been added via register_handlers). Classes | ||||
|         inheriting from Module could implement this, e.g.: | ||||
| 
 | ||||
|         server.remove_global_handler('privmsg', self.on_privmsg) | ||||
|         self.server.remove_global_handler('welcome', self.on_connect) | ||||
|         """ | ||||
| 
 | ||||
|         self.server.remove_global_handler('pubmsg', self.on_pubmsg) | ||||
|         self.server.remove_global_handler('privmsg', self.on_privmsg) | ||||
| 
 | ||||
|     def save(self): | ||||
|         """ | ||||
|         Save whatever the module may need to save. Sync files, etc. | ||||
|         """Save whatever the module may need to save. Sync files, etc. | ||||
| 
 | ||||
|         Implement this if you need it. | ||||
|         """ | ||||
| 
 | ||||
|     def shutdown(self): | ||||
|         """ | ||||
|         Do pre-deletion type cleanup. | ||||
|         """Do pre-deletion type cleanup. | ||||
| 
 | ||||
|         Implement this to close databases, write to disk, etc. | ||||
|         """ | ||||
| 
 | ||||
|     def on_pubmsg(self, connection, event): | ||||
|         """ | ||||
|         Handle pubmsg events. Does some variable setup and initial sanity checking before | ||||
|         calling Module.do, which should be implemented by subclasses and what can be | ||||
|         ultimately responsible for the work. | ||||
| 
 | ||||
|         Of course, you are free to reimplement on_pubmsg on your own too. | ||||
|         """ | ||||
| 
 | ||||
|         nick = irclib.nm_to_n(event.source()) | ||||
|         userhost = irclib.nm_to_uh(event.source()) | ||||
|         replypath = event.target() | ||||
|         what = event.arguments()[0] | ||||
| 
 | ||||
|         admin_unlocked = False | ||||
| 
 | ||||
|         try: | ||||
|             if userhost == self.config.get('dr.botzo', 'admin_userhost'): | ||||
|                 admin_unlocked = True | ||||
|         except NoOptionError: pass | ||||
| 
 | ||||
|         # only do commands if the bot has been addressed directly | ||||
|         addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+' | ||||
|         addressed_re = re.compile(addressed_pattern) | ||||
| 
 | ||||
|         # see if we should just skip msg stuff entirely (common for stuff run via alias) | ||||
|         internal_only = True | ||||
|         try: | ||||
|             internal_only = self.config.getboolean(self.__class__.__name__, 'meta.internal_only') | ||||
|         except NoOptionError: pass | ||||
|         except NoSectionError: pass | ||||
|         if internal_only and replypath is not None: | ||||
|             return | ||||
| 
 | ||||
|         need_prefix = True | ||||
|         try: | ||||
|             need_prefix = self.config.getboolean(self.__class__.__name__, 'meta.pubmsg_needs_bot_prefix') | ||||
|         except NoOptionError: pass | ||||
|         except NoSectionError: pass | ||||
| 
 | ||||
|         ignore_prefix = False | ||||
|         try: | ||||
|             ignore_prefix = self.config.getboolean(self.__class__.__name__, 'meta.pubmsg_ignore_bot_prefix') | ||||
|         except NoOptionError: pass | ||||
|         except NoSectionError: pass | ||||
| 
 | ||||
|         strip_bot_name_from_input = True | ||||
|         try: | ||||
|             strip_bot_name_from_input = self.config.getboolean(self.__class__.__name__, 'meta.strip_bot_name_from_input') | ||||
|         except NoOptionError: pass | ||||
|         except NoSectionError: pass | ||||
| 
 | ||||
|         if not addressed_re.match(what) and need_prefix: | ||||
|             return | ||||
|         elif addressed_re.match(what) and ignore_prefix: | ||||
|             return | ||||
|         elif strip_bot_name_from_input: | ||||
|             what = addressed_re.sub('', what) | ||||
| 
 | ||||
|         try: | ||||
|             return self.do(connection, event, nick, userhost, replypath, what, admin_unlocked) | ||||
|         except Exception as e: | ||||
|             print('EXCEPTION: ' + str(e)) | ||||
| 
 | ||||
|     def on_privmsg(self, connection, event): | ||||
|         """ | ||||
|         Handle privmsg events. Does some variable setup and initial sanity checking before | ||||
|         calling Module.do, which should be implemented by subclasses and what can be | ||||
|         ultimately responsible for the work. | ||||
| 
 | ||||
|         Of course, you are free to reimplement on_privmsg on your own too. | ||||
|         """ | ||||
| 
 | ||||
|         nick = irclib.nm_to_n(event.source()) | ||||
|         userhost = irclib.nm_to_uh(event.source()) | ||||
|         replypath = nick | ||||
|         what = event.arguments()[0] | ||||
| 
 | ||||
|         admin_unlocked = False | ||||
| 
 | ||||
|         try: | ||||
|             if userhost == self.config.get('dr.botzo', 'admin_userhost'): | ||||
|                 admin_unlocked = True | ||||
|         except NoOptionError: pass | ||||
| 
 | ||||
|         # see if we should just skip msg stuff entirely (common for stuff run via alias) | ||||
|         internal_only = True | ||||
|         try: | ||||
|             internal_only = self.config.getboolean(self.__class__.__name__, 'meta.internal_only') | ||||
|         except NoOptionError: pass | ||||
|         except NoSectionError: pass | ||||
|         if internal_only and replypath is not None: | ||||
|             return | ||||
| 
 | ||||
|         try: | ||||
|             return self.do(connection, event, nick, userhost, replypath, what, admin_unlocked) | ||||
|         except Exception as e: | ||||
|             print('EXCEPTION: ' + str(e)) | ||||
| 
 | ||||
|     def reply(self, connection, replypath, replystr, stop_responding=False): | ||||
|         """ | ||||
|         Reply over IRC to replypath or return a string with the reply. | ||||
|         """Reply over IRC to replypath or return a string with the reply. | ||||
|         Utility method to do the proper type of reply (either to IRC, or as a return | ||||
|         to caller) depending on the target. Pretty simple, and included in the base | ||||
|         class for convenience. It should be the last step for callers: | ||||
| @ -222,23 +111,6 @@ class Module(object): | ||||
|         """Remove metaoptions from provided list, which was probably from a config file.""" | ||||
| 
 | ||||
|         list.remove('debug') | ||||
|         # if this option has been provided, don't print it | ||||
|         try: | ||||
|             self.config.get(self.__class__.__name__, 'meta.pubmsg_needs_bot_prefix') | ||||
|             list.remove('meta.pubmsg_needs_bot_prefix') | ||||
|         except NoOptionError: pass | ||||
|         try: | ||||
|             self.config.get(self.__class__.__name__, 'meta.pubmsg_ignore_bot_prefix') | ||||
|             list.remove('meta.pubmsg_ignore_bot_prefix') | ||||
|         except NoOptionError: pass | ||||
|         try: | ||||
|             self.config.get(self.__class__.__name__, 'meta.strip_bot_name_from_input') | ||||
|             list.remove('meta.strip_bot_name_from_input') | ||||
|         except NoOptionError: pass | ||||
|         try: | ||||
|             self.config.get(self.__class__.__name__, 'meta.internal_only') | ||||
|             list.remove('meta.internal_only') | ||||
|         except NoOptionError: pass | ||||
| 
 | ||||
|     def retransmit_event(self, event): | ||||
|         """ | ||||
| @ -289,13 +161,14 @@ class Module(object): | ||||
|         """ | ||||
| 
 | ||||
|     def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): | ||||
|         """ | ||||
|         Do the primary thing this module was intended to do. | ||||
|         """Do the primary thing this module was intended to do, in most cases. | ||||
| 
 | ||||
|         Implement this method in your subclass to have a fairly-automatic hook into | ||||
|         IRC functionality. This is called by the default on_pubmsg and on_privmsg | ||||
|         IRC functionality. This is called by DrBotIRC during pubmsg and privmsg | ||||
|         events. | ||||
|         """ | ||||
| 
 | ||||
|         print "looks like someone forgot to implement do!" | ||||
|         print("looks like someone forgot to implement do!") | ||||
| 
 | ||||
| # vi:tabstop=4:expandtab:autoindent | ||||
| # kate: indent-mode python;indent-width 4;replace-tabs on; | ||||
|  | ||||
| @ -30,13 +30,9 @@ class IrcAdmin(Module): | ||||
| 
 | ||||
|     def register_handlers(self, server): | ||||
|         server.add_global_handler('welcome', self.on_connect, self.priority()) | ||||
|         server.add_global_handler('pubmsg', self.on_pubmsg, self.priority()) | ||||
|         server.add_global_handler('privmsg', self.on_privmsg, self.priority()) | ||||
| 
 | ||||
|     def unregister_handlers(self): | ||||
|         self.server.remove_global_handler('welcome', self.on_connect) | ||||
|         self.server.remove_global_handler('pubmsg', self.on_pubmsg) | ||||
|         self.server.remove_global_handler('privmsg', self.on_privmsg) | ||||
| 
 | ||||
|     def on_connect(self, connection, event): | ||||
|         """Set up handlers when the bot has connected to IRC.""" | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user