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

cato- at BerliOS cato-
Sa Jun 30 02:45:49 CEST 2007


Author: cato-
Date: 2007-06-30 02:45:48 +0200 (Sat, 30 Jun 2007)
New Revision: 131

Modified:
   trunk/chatMod.py
   trunk/modules/answerMod.py
   trunk/modules/authMod.py
   trunk/modules/badwordsMod.py
   trunk/modules/controlIRCMod.py
   trunk/modules/controlTCPMod.py
   trunk/modules/eightBallMod.py
   trunk/modules/kursMod.py
   trunk/modules/logMod.py
   trunk/modules/mathMod.py
   trunk/modules/modeMod.py
   trunk/modules/reminderMod.py
   trunk/modules/weatherMod.py
   trunk/otfbot.py
Log:
* updated some modules to use the command- and the query-handler
* added command "debug" to TCPClient:
  * connect with telnet and type "debug" and see all traffic send between bot and server

Modified: trunk/chatMod.py
===================================================================
--- trunk/chatMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/chatMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -33,9 +33,12 @@
 	def command(self, user, channel, command, options):
 		"""a command message received"""
 		pass
+	def query(self, user, channel, msg):
+		"""a private message received (Obsolete method)"""
+		pass
 	def privmsg(self, user, channel, msg):
 		"""a private message received"""
-		pass
+		pass	
 	def msg(self, user, channel, msg):
 		"""message received"""
 		pass
@@ -97,3 +100,7 @@
 			put your initialization stuff in here insteadof __init__
 		"""
 		pass
+	def sendLine(self,line):
+		pass
+	def lineReceived(self,line):
+		pass

Modified: trunk/modules/answerMod.py
===================================================================
--- trunk/modules/answerMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/answerMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -30,20 +30,13 @@
 	def __init__(self, bot):
 		self.bot = bot
 
-		self.answersFile=bot.getConfig("file", datadir+"/answers.txt", "answerMod")
+	def start(self):
+		self.answersFile=self.bot.getConfig("file", datadir+"/answers.txt", "answerMod")
 		self.answers = functions.loadProperties(self.answersFile)
-		self.channels={}
-		
-	def joined(self, channel):
-		self.channels[channel]=1
-	
+
 	def msg(self, user, channel, msg):
 		user = user.split("!")[0] #only nick
-		if(self.channels.has_key(channel)): #Do not respond to server messages
-			if msg == "!reload-answers":
-				self.logger.info("reloading")
-				self.reload()
-				return
+		if channel in self.bot.channels: #Do not respond to server messages
 			answer = self.respond(user, msg)
 			if answer != "":
 				self.bot.sendmsg(channel, answer, self.bot.getConfig("fileencoding", "iso-8859-15","answerMod"))

Modified: trunk/modules/authMod.py
===================================================================
--- trunk/modules/authMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/authMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -34,13 +34,12 @@
 			self.whitelist.append(user)
 		self.password=self.bot.getConfig("password", "", "authMod")
 
-	def msg(self, user, channel, msg):
+	def query(self, user, channel, msg):
 		user=user.split("!")[0]
-		if channel == self.bot.nickname and msg[0:9]=="identify ":
-			if self.password != "" and msg[9:] == self.password:
-				self.bot.sendmsg(user, "Password accepted")
-				self.logger.info("User "+str(user)+" successfully identified with password")
-				self.whitelist.append(user)
+		if msg[0:9] == "identify " and self.password != "" and msg[9:] == self.password:
+			self.bot.sendmsg(user, "Password accepted")
+			self.logger.info("User "+str(user)+" successfully identified with password")
+			self.whitelist.append(user)
 
 	def auth(self, user):
 		user=user.split("!")[0]

Modified: trunk/modules/badwordsMod.py
===================================================================
--- trunk/modules/badwordsMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/badwordsMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -30,23 +30,12 @@
 		self.bot=bot
 		self.badwordsFile=bot.getConfig("file", datadir+"/badwords.txt","badwordsMod")
 		self.badwords=functions.loadList(self.badwordsFile)
-		self.channels = {}
-		
-	def joined(self, channel):
-		self.channels[channel]=1
-		
-	def part(self, channel):
-		self.channels[channel]=1
 
 	def reload(self):
 		self.badwords=functions.loadList(self.badwordsFile)
 
 	def msg(self, user, channel, msg):
-		if channel == self.bot.nickname:
-			if msg == "!reload-badwords":
-				self.reload()
-		else:
-			for word in self.badwords:
-				if self.channels.has_key(channel) and word != "" and re.search(word, msg, re.I) and user.split("!")[0]!=self.bot.nick:
-					self.logger.info("kicking "+user.split("!")[0]+" for badword: "+word)
-					self.bot.kick(channel, user.split("!")[0], "Badword: "+word)
+		for word in self.badwords:
+			if channel in self.bot.channels and word != "" and re.search(word, msg, re.I):
+				self.logger.info("kicking "+user.split("!")[0]+" for badword: "+word)
+				self.bot.kick(channel, user.split("!")[0], "Badword: "+word)

Modified: trunk/modules/controlIRCMod.py
===================================================================
--- trunk/modules/controlIRCMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/controlIRCMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -33,15 +33,9 @@
 				self.control[user]=controlInterface(self.bot)
 			self.bot.sendmsg(nick,self.control[user].input(msg))
 
-		#elif channel==self.bot.nickname:
-		#	self.bot.sendmsg(nick,"Not authorized")
-		elif self.bot.auth(user) > -1 and msg[0] == "!": #TODO: make "!" configurable
-			if msg[1:7] == "reload":
-				if len(msg.split(" ")) == 2:
-					for chatMod in self.bot.mods:
-						if chatMod.name == msg.split(" ")[1]:
-							try:
-								chatMod.reload()
-								self.logger.info("Reloading Settings of "+chatMod.name)
-							except Exception, e:
-								self.logger.error("Error while reloading "+chatMod.name)
+	def command(self, user, channel, command, options):
+		if self.bot.auth(user) > -1 and command == "reload": #TODO: make "!" configurable
+			for chatMod in self.bot.mods:
+				if chatMod.name == options:
+					chatMod.reload()
+					self.logger.info("Reloading Settings of "+chatMod.name)
\ No newline at end of file

Modified: trunk/modules/controlTCPMod.py
===================================================================
--- trunk/modules/controlTCPMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/controlTCPMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -30,7 +30,10 @@
 	PLAIN=0
 	TELNET=1
 	READLINE=2
+	NORMAL=3
+	DEBUG=4
 	terminal=PLAIN
+	state=NORMAL
 	def _output(self, data):
 		if self.terminal == self.READLINE:
 			self.sendLine("02 "+self.factory.bot.network)
@@ -52,6 +55,8 @@
 				self.terminal=self.TELNET
 			elif argument == "readline":
 				self.terminal=self.READLINE
+		elif command == "debug":
+			self.state=self.DEBUG
 		elif data != "":
 			self._output(self.control.input(data))
 		else:
@@ -68,13 +73,28 @@
 	def __init__(self, bot):
 		self.bot=bot #.getFactory()._getnetwork(factory._getnetworkslist()[0])
 		self.protocol=BotProtocol
+		self.proto=[]
+		
+	def buildProtocol(self,addr):
+		proto=protocol.ServerFactory.buildProtocol(self,addr)
+		self.proto.append(proto)
+		return proto
 
 class chatMod(chatMod.chatMod):
 	def __init__(self, bot):
 		self.bot=bot
 	def start(self):
-		f=BotProtocolFactory(self.bot)
+		self.f=BotProtocolFactory(self.bot)
 		try:
-			self.bot.getReactor().listenTCP(5022, f)
+			self.bot.getReactor().listenTCP(5022, self.f)
 		except (error.CannotListenError):
 			pass
+		
+	def debug(self,line):
+		for p in self.f.proto:
+			if p.state == p.DEBUG:
+				p._output(line)
+	def lineReceived(self, line):
+		self.debug("< "+line)
+	def sendLine(self,line):
+		self.debug("> "+line)
\ No newline at end of file

Modified: trunk/modules/eightBallMod.py
===================================================================
--- trunk/modules/eightBallMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/eightBallMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -46,7 +46,6 @@
 		"Ask again later (Frag spaeter nochmal)"
 		]
 
-	def msg(self, user, channel, msg):
-		#if channel == self.bot.nickname:
-		if len(msg) > 7 and msg[:7] == "!8ball ": #>7 = only if the user asked something.
+	def command(self, user, channel, command, options):
+		if command == "8ball" and options != "": #>7 = only if the user asked something.
 			self.bot.sendmsg(channel, random.choice(self.answers))

Modified: trunk/modules/kursMod.py
===================================================================
--- trunk/modules/kursMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/kursMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -28,13 +28,9 @@
 class chatMod(chatMod.chatMod):
 	def __init__(self, bot):
 		self.bot = bot
-		self.channels={}
 		self.time=time.time()
 		self.commands = ["kurs","wkn"]
-		
-	def joined(self, channel):
-		self.channels[channel]=1
-	
+
 	def command(self, user, channel, command, options):
 		nick=user.split("!")[0]
 		if command in self.commands and 0 < (time.time() - self.time) < 5:

Modified: trunk/modules/logMod.py
===================================================================
--- trunk/modules/logMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/logMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -110,8 +110,10 @@
 		elif len(channel)>0 and channel[0]=="#":
 			modesign=self.bot.users[channel][user]['modchar']
 			self.log(channel, "<"+modesign+user+"> "+msg)
-		else:
-			self.logPrivate(channel, "<"+modesign+self.bot.nickname+"> "+msg)
+
+	def query(self, user, channel, msg):
+		user=user.split("!")[0]
+		self.logPrivate(channel, "<"+self.bot.nickname+"> "+msg)
 	
 	def noticed(self, user, channel, msg):
 		if user != "":

Modified: trunk/modules/mathMod.py
===================================================================
--- trunk/modules/mathMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/mathMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -24,23 +24,23 @@
 	def __init__(self, bot):
 		self.bot=bot
 
-	def msg(self, user, channel, msg):
-		#if channel == self.bot.nickname:
-		if msg == "!wuerfel":
-			self.bot.sendme(channel, "wuerfelt. Das Ergebnis ist: "+str(random.randint(1,6)))
-		elif msg[0:8] == "!wuerfel":
-			num = 2
-			string = "wuerfelt. Die Ergebnisse sind: "
-			try:
-				num = int(msg[8:])
-			except ValueError:
+	def command(self, user, channel, command, options):
+		if command == "wuerfel":
+			if options == "":
+				self.bot.sendme(channel, "wuerfelt. Das Ergebnis ist: "+str(random.randint(1,6)))
+			else:
 				num = 2
-			if num > 10:
-				num = 10
-			for i in range(1,num+1):
-				zahl = random.randint(1,6)/s
-				if i < num:
-					string += str(zahl)+", "
-				else:
-					string += str(zahl)
-			self.bot.sendme(channel, string) 
+				string = "wuerfelt. Die Ergebnisse sind: "
+				try:
+					num = int(options)
+				except ValueError:
+					num = 2
+				if num > 10:
+					num = 10
+				for i in range(1,num+1):
+					zahl = random.randint(1,6)
+					if i < num:
+						string += str(zahl)+", "
+					else:
+						string += str(zahl)
+				self.bot.sendme(channel, string) 

Modified: trunk/modules/modeMod.py
===================================================================
--- trunk/modules/modeMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/modeMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -21,30 +21,30 @@
 import chatMod
 
 class chatMod(chatMod.chatMod):
-    def __init__(self, bot):
-        self.bot=bot
-        self.modes={}
-        self.modes["op"]={"char": "o", "set": 1}
-        self.modes["deop"]={"char": "o", "set": 0}
-        self.modes["voice"]={"char": "v", "set": 1}
-        self.modes["devoice"]={"char": "v", "set": 0}
-	self.modes["protect"]={"char": "a", "set": 1}
-	self.modes["unprotect"]={"char": "a", "set": 0}
+	def __init__(self, bot):
+		self.bot=bot
+		self.modes={}
+		self.modes["op"]={"char": "o", "set": 1}
+		self.modes["deop"]={"char": "o", "set": 0}
+		self.modes["voice"]={"char": "v", "set": 1}
+		self.modes["devoice"]={"char": "v", "set": 0}
+		self.modes["halfop"]={"char": "h", "set": 1}
+		self.modes["dehalfop"]={"char": "h", "set": 0}
 
-    def msg(self, user, channel, msg):
-        user=user.split("!")[0]
-        if self.bot.auth(user) > 2:
-            for mode in self.modes.keys():
-                if msg[0:len(mode)+1]=="!"+mode:
-                    msg=msg[len(mode)+1:]
-                    if msg != "":
-                        if msg[0]==" ":
-                            msg=msg[1:]
-                        self.bot.mode(channel, self.modes[mode]["set"], self.modes[mode]["char"], None, msg)
-                    else:
-                        self.bot.mode(channel, self.modes[mode]["set"], self.modes[mode]["char"], None, user)
-	if self.bot.auth(user) > 5 and msg[0:5] == "!kick":
-		if len(msg.split(" ")) == 2:
-			self.bot.kick(channel,msg.split(" ")[1])
-		elif len(msg.split(" ")) > 2:
-			self.bot.kick(channel,msg.split(" ")[1]," ".join(msg.split(" ")[2:]))
+	#def msg(self, user, channel, msg):
+	def command(self, user, channel, command, options):
+		user=user.split("!")[0]
+		if self.bot.auth(user) > 2 and command in self.modes.keys():
+			if options != "":
+				self.bot.mode(channel, self.modes[command]["set"], self.modes[command]["char"], None, options)
+			else:
+				self.bot.mode(channel, self.modes[command]["set"], self.modes[command]["char"], None, user)
+		if self.bot.auth(user) > 5 and command == "kick":
+			if options != "":
+				options=options.split(" ",1)
+				if len(options) == 1:
+					self.bot.kick(channel,options[0])
+				elif len(options) == 2:
+					self.bot.kick(channel,options[0],options[1])
+			else:
+				self.bot.kick(channel,user, "Requested.")

Modified: trunk/modules/reminderMod.py
===================================================================
--- trunk/modules/reminderMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/reminderMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -40,19 +40,16 @@
 		for message in messages:
 			self.bot.sendmsg(message[0], message[1]+": Reminder: "+message[2])
 
-	def msg(self, user, channel, msg):
-		#if channel == self.bot.nickname:
-		#if msg[0]=="!" or msg[:len(self.bot.nickname)]==self.bot.nickname:
-		if msg[0:10]=="!remindme ":
+	def command(self, user, channel, command, options):
+		if command == "remindme":
 			user=user.split("!")[0]
-
-			tmp=msg.split(" ", 2)
+			options=options.split(" ", 1)
 			try:
-				wait=float(tmp[1])
+				wait=float(options)
 			except ValueError:
-				self.bot.sendmsg(channel, user.split("!")[0]+": invalid number format \""+tmp[1]+"\".")
+				self.bot.sendmsg(channel, user.split("!")[0]+": invalid number format \""+options[0]+"\".")
 				return
-			text=str(tmp[2])
+			text=str(options[1])
 			
 			when=int( (time.time()+wait*60)/60 ) #when will this be executed? (minutes since 1.1.1970 ;-))
 			if self.messages.has_key(when):

Modified: trunk/modules/weatherMod.py
===================================================================
--- trunk/modules/weatherMod.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/modules/weatherMod.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -170,25 +170,23 @@
 		self.time=time.time()
 		self.commands = ["wetter"]
 		
-	def msg(self, user, channel, msg):
+	def command(self, user, channel, command, options):
 		nick=user.split("!")[0]
-		cmd=msg.split(" ")[0][1:]
-		if cmd in self.commands and ((time.time() - self.time) < 5 and (time.time() - self.time) > 0):
+		if channel in self.commands and 0 < (time.time() - self.time) < 5:
 			self.bot.sendmsg(channel,"Wait a minute ...")
-		elif cmd in self.commands:
-			self.time = time.time()
-			if cmd == "wetter":
-				c = getWeather(" ".join(msg.split(" ")[1:]))
-				if len(c) < 1:
-					self.bot.sendmsg(channel,"Keinen passenden Ort gefunden")
-				else:
-					#self.logger.debug(str(c))
-					answ = "Wetter f\xfcr "+str(c['location']['city'])
-					if len(c['location']['country'])>0: 
-						answ += " ("+str(c['location']['country'])+")"
-					answ += ": "+str(weathercodes[int(c['condition']['code'])])
-					answ += ", "+str(c['condition']['temp'])+"\xb0"+str(c['units']['temperature'])
-					answ += " gef\xfchlt "+str(c['wind']['chill'])+"\xb0"+str(c['units']['temperature'])
-					answ += ", Wind: "+str(c['wind']['speed'])+str(c['units']['speed'])+" aus "+str(getDirection(int(c['wind']['direction'])))
-					answ += ", Luftfeuchte: "+str(c['atmosphere']['humidity'])+"%"
-					self.bot.sendmsg(channel,answ,"UTF-8")
+			return
+		self.time = time.time()
+		if command == "wetter":
+			c = getWeather(options)
+			if len(c) < 1:
+				self.bot.sendmsg(channel,"Keinen passenden Ort gefunden")
+			else:
+				answ = "Wetter f\xfcr "+str(c['location']['city'])
+				if len(c['location']['country'])>0: 
+					answ += " ("+str(c['location']['country'])+")"
+				answ += ": "+str(weathercodes[int(c['condition']['code'])])
+				answ += ", "+str(c['condition']['temp'])+"\xb0"+str(c['units']['temperature'])
+				answ += " gef\xfchlt "+str(c['wind']['chill'])+"\xb0"+str(c['units']['temperature'])
+				answ += ", Wind: "+str(c['wind']['speed'])+str(c['units']['speed'])+" aus "+str(getDirection(int(c['wind']['direction'])))
+				answ += ", Luftfeuchte: "+str(c['atmosphere']['humidity'])+"%"
+				self.bot.sendmsg(channel,answ,"UTF-8")

Modified: trunk/otfbot.py
===================================================================
--- trunk/otfbot.py	2007-06-29 23:01:30 UTC (rev 130)
+++ trunk/otfbot.py	2007-06-30 00:45:48 UTC (rev 131)
@@ -461,7 +461,12 @@
 			self._apirunner("command",{"user":user,"channel":channel,"command":command,"options":options})
 			#return
 
+		if channel.lower() == self.nickname.lower():
+			self._apirunner("query",{"user":user,"channel":channel,"msg":msg})
+		
+		# to be removed
 		self._apirunner("privmsg",{"user":user,"channel":channel,"msg":msg})
+		# to be called for messages in channels
 		self._apirunner("msg",{"user":user,"channel":channel,"msg":msg})
 		#nick = user.split("!")[0]
 		#if channel == self.nickname and self.auth(nick) > 9:
@@ -593,6 +598,7 @@
 		"""
 		#self.logger.debug(str(line))
 		# adding a correct hostmask for join, part and quit
+		self._apirunner("lineReceived", {"line":line})
 		if line.split(" ")[1] == "JOIN" and line[1:].split(" ")[0].split("!")[0] != self.nickname:
 			self.userJoined(line[1:].split(" ")[0],string.lower(line.split(" ")[2][1:]))
 			#self.joined(line[1:].split(" ")[0],line.split(" ")[2][1:])
@@ -602,6 +608,10 @@
 			self.userQuit(line[1:].split(" ")[0],line.split("QUIT :")[1])
 		else:
 			irc.IRCClient.lineReceived(self,line)
+	
+	def sendLine(self, line):
+		self._apirunner("sendLine",{"line":line})
+		irc.IRCClient.sendLine(self, line)
 
 class BotFactory(protocol.ReconnectingClientFactory):
 	"""The Factory for the Bot"""




Mehr Informationen über die Mailingliste Otfbot-dev