Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Chatnet Bot - 1/25/2010 - CRe
- #This is just a chatnet bot client that will probably do something in
- #the future. It uses TCP sockets.
- from socket import *
- from ConfigParser import *
- from os import *
- from collections import deque
- import threading
- import time
- class BotAction:
- global s, clientQueue
- def __init__(self):
- pass
- def botLogin(self, username, password):
- clientQueue.append("LOGIN:1:%s:%s\r\n" % (username, password))
- def botChangeArena(self, arena):
- clientQueue.append("GO:%s\r\n" % (arena))
- def botChangeFreq(self, freq):
- clientQueue.append("CHANGEFREQ:%d\r\n" % (self, freq))
- def sendPublicMsg(self, message):
- clientQueue.append("SEND:PUB:%s\r\n" % (message))
- def sendPublicMacro(self, message):
- clientQueue.append("SEND:PUBM:%s\r\n" % (message))
- def sendPrivateMsg(self, name, message):
- clientQueue.append("SEND:PRIV:%s:%s\r\n" % (name,message))
- def sendFreqMsg(self, freq, message):
- clientQueue.append("SEND:FREQ:%s:%s\r\n" % (freq, message))
- def sendModMsg(self, message):
- clientQueue.append("SEND:MOD:%s\r\n" % (message))
- def sendCommand(self, command):
- clientQueue.append("SEND:CMD:%s\r\n" % (command))
- def sendPrivateCommand(self, name, command):
- clientQueue.append("SEND:PRIVCMD:%s:%s\r\n" % (name, command))
- def sendSquadMsg(self, squad, message):
- clientQueue.append("SEND:SQUAD:%s:%s\r\n" % (squad, message))
- def sendKeepAlive(self):
- clientQueue.append("NOOP\r\n")
- class EventHandler:
- global read, s, BotAction, BOT_INITIALARENA, cfg, clientQueue
- def __init__(self):
- pass
- def handle(self,read):
- type = read
- type = type.split(":", 1)
- if type[0] == "LOGINBAD":
- #LOGINBAD:message
- read = read.split(":", 2)
- self.handleLoginBad(read[1])
- elif type[0] == "LOGINOK":
- #LOGINOK:yourname
- read = read.split(":", 2)
- self.handleLoginOK(read[1])
- elif type[0] == "INARENA":
- #INARENA:arenaname:freq
- read = read.split(":", 3)
- self.handleBotEnteredArena(read[1], read[2])
- elif type[0] == "PLAYER":
- #PLAYER:name:ship:freq
- read = read.split(":", 4)
- self.handleGotPlayer(read[1], read[2], read[3])
- elif type[0] == "ENTERING":
- #ENTERING:name:ship:freq
- read = read.split(":", 4)
- self.handleGotPlayer(read[1], read[2], read[3])
- elif type[0] == "LEAVING":
- #LEAVING:name
- read = read.split(":", 2)
- self.handleGotPlayerLeaving(read[1])
- elif type[0] == "KILL":
- #KILL:killername:killedname:bounty:flagscarried
- read = read.split(":", 5)
- self.handleKill(read[1], read[2], read[3], read[4])
- elif type[0] == "MSG":
- #MSG:ARENA:msg
- #MSG:CMD:msg
- #MSG:PUB:name:msg
- #MSG:PUBM:name:msg
- # (macroed pub message)
- #MSG:PRIV:name:msg
- #MSG:FREQ:name:msg
- #MSG:CHAT:channelnum:msg
- #MSG:MOD:name:msg
- #MSG:SYSOP:msg
- #MSG:SQUAD:squad:sender:msg
- self.handleMessage(read)
- elif type[0] == "NOOP":
- self.handleKeepAlive(read)
- else:
- self.handleUnknown(read)
- #message - Login fail message.
- def handleLoginBad(self, message):
- print "Login Failed: ", message
- def handleLoginOK(self, username):
- print "Login Successful: ", username
- #Lets switch arenas!
- if BOT_INITIALARENA == "":
- BotAction.botChangeArena("")
- else:
- BotAction.botChangeArena(BOT_INITIALARENA)
- #Yey switched
- def handleIgnore(self, read):
- pass
- def handleBotEnteredArena(self, arenaname, freq):
- BotAction.sendPrivateMsg("CRe", "Hi master.")
- print "Entered Arena: ", arenaname, " FREQ: ", freq
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- BotAction.sendPrivateMsg("CRe", "Yo")
- def handleGotPlayer(self, name, ship, freq):
- print "Player: ", name, "/", ship, "/", freq
- def handleGotPlayerEntering(self, name, ship, freq):
- print "Player Entered: ", name, "/", ship, "/", freq
- def handleGotPlayerLeaving(self, name):
- print "Player Left: ", name
- def handleKill(self, killer, killee, bounty, flags):
- print "Kill: %s killed by %s | Bounty: %s | Flags: %s" % (killee, killer, bounty, flags)
- def handleMessage(self, read):
- print read
- def handleKeepAlive(self, read):
- print read
- def handleUnknown(self, read):
- print "HANDLE [UNKNOWN RECIEVED]: %s" % read
- clientQueue = deque()
- clientQueueThread = clientQueueThread()
- clientQueueThread.start()
- #Configurationz
- cfg = ConfigParser()
- cfg.read("crebot.cfg")
- #Zone information
- ZONE_NAME = cfg.get("Zone", "Name")
- ZONE_IPADDR = cfg.get("Zone", "IP")
- ZONE_PORT = cfg.getint("Zone", "Port")
- #Bot information
- BOT_USERNAME = cfg.get("Bot", "Username")
- BOT_PASSWORD = cfg.get("Bot", "Password")
- BOT_VERSION = cfg.getint("Bot", "Version")
- BOT_INITIALARENA = cfg.get("Bot", "InitialArena")
- #Connect to socket
- print "Connecting to %s (%s:%d)" % (ZONE_NAME, ZONE_IPADDR, ZONE_PORT)
- print "Bot: %s Pass: %s \n Version %d" % (BOT_USERNAME, BOT_PASSWORD, BOT_VERSION)
- s = socket(AF_INET, SOCK_STREAM) # create a TCP socket
- try:
- s.connect((ZONE_IPADDR, ZONE_PORT)) # connect to server on the port
- except socket.error, e:
- print socket.error
- s.send("LOGIN:1:%s:%s\r\n" % (BOT_USERNAME, BOT_PASSWORD)) # send the data
- EventHandler = EventHandler()
- BotAction = BotAction()
- while s:
- data = s.recv(4096)
- EventHandler.handle(data)
- def clientQueuePop():
- global timer, s
- s.send(clientQueue.popleft())
- timer = threading.Timer(2, clientQueuePop)
- timer.start()
- timer = threading.Timer(2, clientQueuePop)
- timer.start()
- class clientQueueThread(threading.Thread):
- global clientQueue, s
- def run(self):
- try:
- s.send(clientQueue.popleft())
- time.wait(1000)
- except IndexError, e:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement