[Otfbot-svn] r127 - in trunk: . modules

allo at BerliOS allo
Fr Jun 29 13:12:53 CEST 2007


Author: allo
Date: 2007-06-29 13:12:51 +0200 (Fri, 29 Jun 2007)
New Revision: 127

Modified:
   trunk/chatMod.py
   trunk/modules/commandsMod.py
   trunk/otfbot.py
Log:
added callback for !commands


Modified: trunk/chatMod.py
===================================================================
--- trunk/chatMod.py	2007-06-28 17:25:42 UTC (rev 126)
+++ trunk/chatMod.py	2007-06-29 11:12:51 UTC (rev 127)
@@ -30,6 +30,9 @@
 	def joined(self, channel):
 		"""we have joined a channel"""
 		pass
+	def command(self, user, channel, command, options):
+		"""a command message received"""
+		pass
 	def privmsg(self, user, channel, msg):
 		"""a private message received"""
 		pass

Modified: trunk/modules/commandsMod.py
===================================================================
--- trunk/modules/commandsMod.py	2007-06-28 17:25:42 UTC (rev 126)
+++ trunk/modules/commandsMod.py	2007-06-29 11:12:51 UTC (rev 127)
@@ -14,7 +14,7 @@
 # along with OtfBot; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 # 
-# (c) 2005, 2006 by Alexander Schier
+# (c) 2005, 2006, 2007 by Alexander Schier
 #
 
 import string, re
@@ -24,7 +24,6 @@
 	settings={};
 	settings['commandsMod.fileencoding']='iso-8859-15'
 	settings['commandsMod.file']=datadir+'/commands.txt'
-	settings['commandsmod.commandChar']='!'
 	return settings
 		
 class chatMod(chatMod.chatMod):
@@ -40,28 +39,17 @@
 		self.commands[channel]=functions.loadProperties(self.bot.getConfig("file", datadir+"/commands.txt", "commandsMod", self.bot.network, channel))
 		self.commandChar[channel]=self.bot.getConfig("commandChar", "!", "commandsMod", self.bot.network, channel)
 	
-	def msg(self, user, channel, msg):
+	def command(self, user, channel, command, options):
 		user = user.split("!")[0] #only nick
-		if self.commandChar.has_key(channel):
-			try:
-				char=msg[0].decode('UTF-8').encode('UTF-8')
-			except UnicodeDecodeError:
-				char=msg[0].decode('iso-8859-15').encode('UTF-8')
-			if char == self.commandChar[channel]:
-				#if msg == "!reload-commands": #TODO: global methodChar
-				#	self.logger.info("reloading")
-				#	self.reload()
-				#	return
-				answer = self.respond(channel, user, msg)
-				if answer != "":
-					if answer[0] == ":":
-						self.bot.sendmsg(channel, answer[1:], self.bot.getConfig("commandsMod.fileencoding", "iso-8859-15"))
-					else:
-						self.bot.sendme(channel, answer, self.bot.getConfig("commandsMod.fileencoding", "iso-8859-15"))
-			
+		answer=self.respond(channel, user, command, options)
+		if answer != "":
+			if answer[0] == ":":
+				self.bot.sendmsg(channel, answer[1:], self.bot.getConfig("commandsMod.fileencoding", "iso-8859-15"))
+			else:
+				self.bot.sendme(channel, answer, self.bot.getConfig("commandsMod.fileencoding", "iso-8859-15"))
+
 	def start(self):
 		self.commands={}
-		self.commandChar={}
 		self.commands["general"]=functions.loadProperties(self.bot.getConfig("file",datadir+"/commands.txt","commandsMod"))
 		self.commands["network"]=functions.loadProperties(self.bot.getConfig("file",datadir+"/commands.txt","commandsMod", self.bot.network))
 
@@ -80,18 +68,14 @@
 		else:
 			return ""
 
-	def respond(self, channel, user, command):
+	def respond(self, channel, user, command, options):
 		answer = ""
-		if command[0] == self.commandChar[channel]:
-			tmp=command[1:].split(" ", 1)
-			cmd=string.lower(tmp[0])
-			if len(tmp) >1:
-				param=tmp[1]
-				answer=self.getCommand(channel, cmd+"_")
-				answer = re.sub("OTHER", param, answer)
-			else:
-				answer=self.getCommand(channel, cmd)
-			answer = re.sub("USER", user, answer)
+		if len(options) >1:
+			answer=self.getCommand(channel, command+"_")
+			answer = re.sub("OTHER", options, answer)
+		else:
+			answer=self.getCommand(channel, command)
+		answer = re.sub("USER", user, answer)
 				
 		if len(answer)>0 and answer[-1] == "\n":
 			return answer[0:-1]

Modified: trunk/otfbot.py
===================================================================
--- trunk/otfbot.py	2007-06-28 17:25:42 UTC (rev 126)
+++ trunk/otfbot.py	2007-06-29 11:12:51 UTC (rev 127)
@@ -425,6 +425,18 @@
 		self._apirunner("left",{"channel":channel})
 		self.setConfig("enabled", "false", "main", self.network, channel) #disable the channel for the next start of the bot
 
+	def command(self, user, channel, command, options):
+		"""callback for !commands
+		@param user: the user, which issues the command
+		@type user: string
+		@param channel: the channel to which the message was sent or my nickname if it was a private message
+		@tpye channel: string
+		@param command: the !command without the !
+		@type command: string
+		@param options: eventual options specified after !command (e.g. "!command foo")
+		@type options: string"""
+		self._apirunner("command",{"user":user,"channel":channel,"command":command, "options":options})
+
 	def privmsg(self, user, channel, msg):
 		""" called by twisted,
 			if we received a message
@@ -435,6 +447,16 @@
 			@param msg: the message
 			@type msg: string
 		"""
+		if msg[0]==self.getConfig("commandChar", "!", "main"):
+			tmp=msg[1:].split(" ", 1)
+			command=tmp[0]
+			if len(tmp)==2:
+				options=tmp[1]
+			else:
+				options=""
+			self._apirunner("command",{"user":user,"channel":channel,"command":command,"options":options})
+			#return
+
 		self._apirunner("privmsg",{"user":user,"channel":channel,"msg":msg})
 		self._apirunner("msg",{"user":user,"channel":channel,"msg":msg})
 		#nick = user.split("!")[0]




Mehr Informationen über die Mailingliste Otfbot-dev