Advertisement
Guest User

Untitled

a guest
May 4th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.87 KB | None | 0 0
  1. #Chatnet Bot - 1/25/2010 - CRe
  2. #This is just a chatnet bot client that will probably do something in
  3. #the future. It uses TCP sockets.
  4.  
  5. from socket import *
  6. from ConfigParser import *
  7. from os import *
  8. from collections import deque
  9. import threading
  10. import time
  11.  
  12. class BotAction:
  13.     global s, clientQueue
  14.    
  15.     def __init__(self):
  16.         pass
  17.    
  18.     def botLogin(self, username, password):
  19.         clientQueue.append("LOGIN:1:%s:%s\r\n" % (username, password))
  20.    
  21.     def botChangeArena(self, arena):
  22.         clientQueue.append("GO:%s\r\n" % (arena))
  23.                
  24.     def botChangeFreq(self, freq):
  25.         clientQueue.append("CHANGEFREQ:%d\r\n" % (self, freq))
  26.    
  27.     def sendPublicMsg(self, message):
  28.         clientQueue.append("SEND:PUB:%s\r\n" % (message))
  29.    
  30.     def sendPublicMacro(self, message):
  31.         clientQueue.append("SEND:PUBM:%s\r\n" % (message))
  32.    
  33.     def sendPrivateMsg(self, name, message):
  34.         clientQueue.append("SEND:PRIV:%s:%s\r\n" % (name,message))
  35.        
  36.     def sendFreqMsg(self, freq, message):
  37.         clientQueue.append("SEND:FREQ:%s:%s\r\n" % (freq, message))
  38.    
  39.     def sendModMsg(self, message):
  40.         clientQueue.append("SEND:MOD:%s\r\n" % (message))
  41.        
  42.     def sendCommand(self, command):
  43.         clientQueue.append("SEND:CMD:%s\r\n" % (command))
  44.    
  45.     def sendPrivateCommand(self, name, command):
  46.         clientQueue.append("SEND:PRIVCMD:%s:%s\r\n" % (name, command))
  47.    
  48.     def sendSquadMsg(self, squad, message):
  49.         clientQueue.append("SEND:SQUAD:%s:%s\r\n" % (squad, message))
  50.    
  51.     def sendKeepAlive(self):
  52.         clientQueue.append("NOOP\r\n")
  53.  
  54. class EventHandler:
  55.     global read, s, BotAction, BOT_INITIALARENA, cfg, clientQueue
  56.    
  57.     def __init__(self):
  58.         pass
  59.    
  60.     def handle(self,read):
  61.         type = read
  62.         type = type.split(":", 1)
  63.                
  64.         if type[0] == "LOGINBAD":
  65.             #LOGINBAD:message      
  66.             read = read.split(":", 2)
  67.             self.handleLoginBad(read[1])
  68.         elif type[0] == "LOGINOK":
  69.             #LOGINOK:yourname          
  70.             read = read.split(":", 2)
  71.             self.handleLoginOK(read[1])
  72.         elif type[0] == "INARENA":
  73.             #INARENA:arenaname:freq
  74.             read = read.split(":", 3)
  75.             self.handleBotEnteredArena(read[1], read[2])
  76.         elif type[0] == "PLAYER":
  77.             #PLAYER:name:ship:freq
  78.             read = read.split(":", 4)
  79.             self.handleGotPlayer(read[1], read[2], read[3])
  80.         elif type[0] == "ENTERING":
  81.             #ENTERING:name:ship:freq
  82.             read = read.split(":", 4)
  83.             self.handleGotPlayer(read[1], read[2], read[3])
  84.         elif type[0] == "LEAVING":
  85.             #LEAVING:name
  86.             read = read.split(":", 2)
  87.             self.handleGotPlayerLeaving(read[1])
  88.         elif type[0] == "KILL":
  89.             #KILL:killername:killedname:bounty:flagscarried
  90.             read = read.split(":", 5)
  91.             self.handleKill(read[1], read[2], read[3], read[4])
  92.         elif type[0] == "MSG":
  93.             #MSG:ARENA:msg
  94.             #MSG:CMD:msg
  95.             #MSG:PUB:name:msg
  96.             #MSG:PUBM:name:msg
  97.             #   (macroed pub message)
  98.             #MSG:PRIV:name:msg
  99.             #MSG:FREQ:name:msg
  100.             #MSG:CHAT:channelnum:msg
  101.             #MSG:MOD:name:msg
  102.             #MSG:SYSOP:msg
  103.             #MSG:SQUAD:squad:sender:msg
  104.            
  105.             self.handleMessage(read)
  106.         elif type[0] == "NOOP":
  107.             self.handleKeepAlive(read)
  108.         else:
  109.             self.handleUnknown(read)
  110.    
  111.     #message - Login fail message.
  112.     def handleLoginBad(self, message):
  113.         print "Login Failed: ", message
  114.    
  115.     def handleLoginOK(self, username):
  116.         print "Login Successful: ", username
  117.        
  118.         #Lets switch arenas!
  119.         if BOT_INITIALARENA == "":
  120.             BotAction.botChangeArena("")
  121.         else:
  122.             BotAction.botChangeArena(BOT_INITIALARENA)
  123.         #Yey switched
  124.                
  125.     def handleIgnore(self, read):
  126.         pass
  127.        
  128.     def handleBotEnteredArena(self, arenaname, freq):
  129.         BotAction.sendPrivateMsg("CRe", "Hi master.")
  130.         print "Entered Arena: ", arenaname, " FREQ: ", freq
  131.         BotAction.sendPrivateMsg("CRe", "Yo")
  132.         BotAction.sendPrivateMsg("CRe", "Yo")
  133.         BotAction.sendPrivateMsg("CRe", "Yo")
  134.         BotAction.sendPrivateMsg("CRe", "Yo")
  135.         BotAction.sendPrivateMsg("CRe", "Yo")
  136.         BotAction.sendPrivateMsg("CRe", "Yo")
  137.         BotAction.sendPrivateMsg("CRe", "Yo")
  138.         BotAction.sendPrivateMsg("CRe", "Yo")
  139.     def handleGotPlayer(self, name, ship, freq):
  140.         print "Player: ", name, "/", ship, "/", freq
  141.        
  142.     def handleGotPlayerEntering(self, name, ship, freq):
  143.         print "Player Entered: ", name, "/", ship, "/", freq
  144.    
  145.     def handleGotPlayerLeaving(self, name):
  146.         print "Player Left: ", name
  147.    
  148.     def handleKill(self, killer, killee, bounty, flags):
  149.         print "Kill: %s killed by %s | Bounty: %s | Flags: %s" % (killee, killer, bounty, flags)
  150.    
  151.     def handleMessage(self, read):
  152.         print read
  153.    
  154.     def handleKeepAlive(self, read):
  155.         print read
  156.    
  157.     def handleUnknown(self, read):
  158.         print "HANDLE [UNKNOWN RECIEVED]: %s" % read
  159.  
  160.  
  161. clientQueue = deque()
  162. clientQueueThread = clientQueueThread()
  163. clientQueueThread.start()
  164.  
  165. #Configurationz
  166. cfg = ConfigParser()
  167. cfg.read("crebot.cfg")
  168.  
  169. #Zone information
  170. ZONE_NAME = cfg.get("Zone", "Name")
  171. ZONE_IPADDR = cfg.get("Zone", "IP")
  172. ZONE_PORT = cfg.getint("Zone", "Port")
  173.  
  174. #Bot information
  175. BOT_USERNAME = cfg.get("Bot", "Username")
  176. BOT_PASSWORD = cfg.get("Bot", "Password")
  177. BOT_VERSION = cfg.getint("Bot", "Version")
  178. BOT_INITIALARENA = cfg.get("Bot", "InitialArena")
  179.  
  180. #Connect to socket
  181. print "Connecting to %s (%s:%d)" % (ZONE_NAME, ZONE_IPADDR, ZONE_PORT)
  182. print "Bot: %s Pass: %s \n Version %d" % (BOT_USERNAME, BOT_PASSWORD, BOT_VERSION)
  183.  
  184. s = socket(AF_INET, SOCK_STREAM)    # create a TCP socket
  185.  
  186. try:
  187.     s.connect((ZONE_IPADDR, ZONE_PORT)) # connect to server on the port
  188. except socket.error, e:
  189.     print socket.error
  190.  
  191. s.send("LOGIN:1:%s:%s\r\n" % (BOT_USERNAME, BOT_PASSWORD))  # send the data
  192.  
  193. EventHandler = EventHandler()
  194. BotAction = BotAction()
  195.  
  196.    
  197. while s:
  198.     data = s.recv(4096)
  199.     EventHandler.handle(data)
  200.  
  201. def clientQueuePop():
  202.     global timer, s
  203.     s.send(clientQueue.popleft())
  204.     timer = threading.Timer(2, clientQueuePop)
  205.     timer.start()
  206.  
  207. timer = threading.Timer(2, clientQueuePop)
  208. timer.start()
  209.  
  210.    
  211. class clientQueueThread(threading.Thread):
  212.    
  213.     global clientQueue, s
  214.    
  215.     def run(self):
  216.         try:
  217.             s.send(clientQueue.popleft())
  218.             time.wait(1000)
  219.         except IndexError, e:
  220.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement