MegaHAL module, lets the bot partake in inane conversations. doesn't
assume a learning file or anything, starts with an empty brain and learns from IRC
This commit is contained in:
		
							parent
							
								
									71e545b908
								
							
						
					
					
						commit
						1f27078f82
					
				
							
								
								
									
										123
									
								
								modules/MegaHAL.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								modules/MegaHAL.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,123 @@ | ||||
| """ | ||||
| MegaHAL - MegaHAL bot for IRC | ||||
| Copyright (C) 2010  Brian S. Stephan | ||||
| 
 | ||||
| This program is free software: you can redistribute it and/or modify | ||||
| it under the terms of the GNU General Public License as published by | ||||
| the Free Software Foundation, either version 3 of the License, or | ||||
| (at your option) any later version. | ||||
| 
 | ||||
| This program is distributed in the hope that it will be useful, | ||||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
| GNU General Public License for more details. | ||||
| 
 | ||||
| You should have received a copy of the GNU General Public License | ||||
| along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
| """ | ||||
| 
 | ||||
| import os | ||||
| import re | ||||
| 
 | ||||
| from extlib import irclib | ||||
| from extlib import megahal | ||||
| 
 | ||||
| from Module import Module | ||||
| 
 | ||||
| class MegaHAL(Module): | ||||
|     """Have an annoying, dumb, and hilarious chatterbot on IRC.""" | ||||
| 
 | ||||
|     def register_handlers(self, server): | ||||
|         server.add_global_handler('welcome', self.on_connect) | ||||
|         server.add_global_handler('pubmsg', self.on_pubmsg) | ||||
|         server.add_global_handler('privmsg', self.on_privmsg) | ||||
| 
 | ||||
|     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): | ||||
|         """ | ||||
|         Upon connecting, open the MegaHAL brain and get ready for doing actual stuff. | ||||
|         """ | ||||
| 
 | ||||
|         self.megahal = megahal.MegaHAL() | ||||
| 
 | ||||
|     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. | ||||
|         """ | ||||
| 
 | ||||
|         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) | ||||
| 
 | ||||
|         need_prefix = True | ||||
|         try: | ||||
|             need_prefix = self.config.getboolean(self.__class__.__name__, 'meta.pubmsg_needs_bot_prefix') | ||||
|         except NoOptionError: pass | ||||
|         except NoSectionError: pass | ||||
| 
 | ||||
|         re_add_nick = False | ||||
|         if not addressed_re.match(what) and need_prefix: | ||||
|             return | ||||
|         else: | ||||
|             if addressed_re.match(what): | ||||
|                 re_add_nick = True | ||||
|                 what = addressed_re.sub('', what) | ||||
| 
 | ||||
|         if replypath is not None: | ||||
|             what = self.try_recursion(connection, event, nick, userhost, replypath, what, admin_unlocked) | ||||
| 
 | ||||
|         # try reloading | ||||
|         self.reload(connection, event, nick, userhost, replypath, what, admin_unlocked) | ||||
| 
 | ||||
|         if re_add_nick: | ||||
|             self.do(connection, event, nick, userhost, replypath, connection.get_nickname() + ": " + what, admin_unlocked) | ||||
|         else: | ||||
|             self.do(connection, event, nick, userhost, replypath, what, admin_unlocked) | ||||
| 
 | ||||
|     def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): | ||||
|         """ | ||||
|         Say something on IRC if the bot is being addressed, or at least learn what | ||||
|         was said. | ||||
|         """ | ||||
| 
 | ||||
|         line = what | ||||
|         reply = "" | ||||
|         append_nick = False | ||||
| 
 | ||||
|         if re.search(connection.get_nickname(), line, re.IGNORECASE) is not None: | ||||
|             addressed_pattern = '^' + connection.get_nickname() + '[:,]\s+' | ||||
|             addressed_re = re.compile(addressed_pattern) | ||||
|             if addressed_re.match(line): | ||||
|                 line = addressed_re.sub('', line) | ||||
|                 append_nick = True | ||||
| 
 | ||||
|             reply = self.megahal.get_reply(line) | ||||
|         else: | ||||
|             self.megahal.learn(line) | ||||
| 
 | ||||
|         self.megahal.sync() | ||||
|         if reply is not "": | ||||
|             if append_nick: | ||||
|                 reply = nick + ": " + reply | ||||
|             return self.reply(connection, replypath, reply) | ||||
| 
 | ||||
| # vi:tabstop=4:expandtab:autoindent | ||||
| # kate: indent-mode python;indent-width 4;replace-tabs on; | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user