#!/usr/bin/python # $Id: pybot.py,v 1.3 2000/02/09 21:04:52 stein Exp $ # # Copyright (c) 2000 - Sverre Stoltenberg # # 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 2 # 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, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # pybot - XSW client with bot features # import socket, string, re, traceback, time, whrandom class pybot: def __init__(self): self.version = "0.2.4" self.rand = whrandom.whrandom() self.user = 'Guest' self.passwd = 'Guest' self.host = 'localhost' self.port = 1701 self.myid = '' self.myname = '' self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.sock.connect(self.host, self.port) self.fd = self.sock.makefile() self.rename = re.compile('<(.*?)>', re.M) self.borgmsg = [ 'You missed again, #NAME#!', 'We will assimilate you #NAME#!', 'We will come after you, #NAME#!', 'Go home #NAME#', 'Disconnect now, #NAME#, or we will infiltrate you ISP', '#NAME# is a looser', '#NAME# is using Windoews 95', '#NAME# is a chicken', 'We will search and destroy you #NAME#!', 'You will regret that, #NAME#!', 'How do you like the runabaout, #NAME#', 'You can still push the F10 button, #NAME#', 'We are not afraid of you, #NAME#', 'Come to us, #NAME#, we have a present for you', 'We are looking for you too, #NAME#', 'Why do you shoot at us, #NAME#', 'We are your friend, #NAME#, we will not harm you. Lower your shields so we can talk', '#NAME#, take us to your leader - now!', 'We will rule this universe forever, #NAME#, disconnect or die!' ] self.helpmsg = [ 'PyBot Help: I am currently 100% automatic - no commands available' ] self.filename = "universe.xsw" self.playerfile = "players.xsw" self.insult_mode = 0 self.help_mode = 0 self.command_mode = 0 def sendfile(self): fn = open(self.filename, "r") all = fn.readlines() for i in all: self.command(i) ############################### # network komm. main loop def mainloop(self): print "pybot: Entering universe" self.login() print "pybot: Waiting for action" # Answers from xswserver goes here while 1: s = self.fd.readline() r = string.split(s,' ',1) #print s if r[0] == '13': f = string.split(r[1],';',0) self.myid = f[0] #self.myname = f[1] self.myname = re.sub('\n','',f[1]) print("I got id = %s, name = %s" % (self.myid, self.myname)) elif r[0] == '30': # Sys Message if string.find(r[1], '<', 0) >= 0: # Wall message g = string.split(s,':',1) if string.find(g[1],'borg',0) >= 0: # Insult mode name = self.rename.search(r[1]) #if name.group(1) <> self.myname: #self.randmsg(name.group(1), self.borgmsg) elif string.find(g[1],'help',0) >= 0: # Help mode name = self.rename.search(r[1]) #if name.group(1) <> self.myname: # self.randmsg(name.group(1), self.helpmsg) elif string.find(g[1],'sendfile',0) >= 0: self.sendfile() elif string.find(g[1], '&', 0) >= 0: # Command mode c = string.split(g[1],' ',0) name = self.rename.search(r[1]) #self.sendfile() #self.wall(re.sub('#NAME#',name.group(1),'PyBot: All players respawned, #NAME#')) elif string.find(r[1], 'disconnected', 0) >= 0: # Player disconnected g = string.split(r[1],' ',1) name = g[0] self.createplayers() #self.command(re.sub('#NAME#',name,'set #NAME#=visibility:0')) #self.wall(re.sub('#NAME#',name,'Hasta la vista ')) elif string.find(r[1], 'connected', 0) >= 0: # Player connected g = string.split(r[1],' ',1) name = g[0] #i#self.command(re.sub('#NAME#',name,'set #NAME#=visibility:1')) #self.wall(re.sub('#NAME#',name,'We have been waiting for you, #NAME#. Prepare to die')) #self.wall(re.sub('#NAME#',name,'Type \'e wall help\' for PyBot commands, #NAME#')) elif r[0] == '80': # SW Extension f = string.split(r[1],' ',1) #print s if f[0] == '40': l = string.split(f[1],';',1) l[0] = re.sub('\n','',l[0],0) l[1] = re.sub('\n','',l[1],0) if l[0] == self.myid: print("%s just entered scanner range (It's me...)" % (l[1])) else: print("%s just entered scanner range (id %s)" % (l[1], l[0])) elif f[0] == '103': # Comm Message l = string.split(f[1],' ',3) m = string.split(l[3],';',1) print("%s@%s: %s" % (l[0], m[0], m[1])) print s if int(l[0]) == 0: self.message('Nei!') elif int(l[0]) != int(self.myid): self.message("Just go home!") elif f[0] == '102': # Hail l = string.split(f[1],' ',0) print s print("Got hail from %s@%s" % (l[0],l[3])) if int(l[0]) != int(self.myid): self.hail() self.sock.close() self.fd.close() def wall(self,txt): print 'sendt wall %s' % txt self.sock.send('32 wall %s' % txt) def command(self,txt): #print 'command: %s' % txt self.sock.send('32 %s' % txt) def message(self,txt): print 'sendt message %s' % txt self.sock.send('80 103 %s -1 1.5708 31000;%s' % (self.myid, txt)) def hail(self): print 'sendt hail' self.sock.send('80 102 %s -1 1.5708 31000' % self.myid) def login(self): self.sock.send('11 %s;%s' % (self.user, self.passwd)) def randmsg(self,name,msg): rand = whrandom.whrandom() m = rand.randint(0,len(msg)-1) self.wall(re.sub('#NAME#',name,msg[m])) def sendfile(self): fn = open(self.filename, "r") all = fn.readlines() for i in all: self.command(i) def createplayers(self): fn = open(self.playerfile, "r") all = fn.readlines() for i in all: self.command(i) s = pybot() s.mainloop()