Skip to content

Instantly share code, notes, and snippets.

@gdude2002
Created February 19, 2010 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdude2002/309277 to your computer and use it in GitHub Desktop.
Save gdude2002/309277 to your computer and use it in GitHub Desktop.
import sys
import os
import datetime
import struct
import urllib
import urllib2
import re
import socket
import cookielib
import threading
class bot (threading.Thread):
def __init__(self, sock=None):
threading.Thread.__init__(self)
self.username = "gdudebot"
self.passw = "password"
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def run(self):
self.rip(sys.argv[1], self.username, self.passw)
def rip(self, key, username, password):
login_url = 'http://minecraft.net/login.jsp'
play_url = 'http://minecraft.net/play.jsp?server=%s'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username': username, 'password': password})
print "Logging in..."
opener.open(login_url, login_data)
print "Fetching server info..."
html = opener.open(play_url % key).read()
ip = re.search(r'param name\="server" value="([0-9.]+)"', html).groups()[0]
port = int(re.search(r'param name\="port" value="([0-9]+)"', html).groups()[0])
mppass = re.search(r'param name\="mppass" value="([0-9a-zA-Z]+)"', html).groups()[0]
# print "Details:"
# print ip
# print port
# print mppass
self.login(ip, port, mppass)
def login(self, ip, port, mppass):
self.username = "gdudebot"
packet = "\0\7"+self.pack(self.username, "STRING")+self.pack(mppass, "STRING")+"\0"
# packet = "%c%c%.64s%.64s" % (0,7,self.username, mppass)
# print len(packet)
# print list(packet)
self.sock.connect((ip, port))
self.sock.send(packet)
botrecv(self.sock).start()
def pack(self, string, type):
if type == "BYTE":
return string
elif type == "SHORT" and (len(string) < 2):
string = string + "\x20"*(2-len(string))
return string
elif type == "SHORT" and not (len(string) < 2):
return string
elif type == "STRING":
string = string + "\x20"*(64-len(string))
return string
elif type == "ARRAY":
return "We're never, ever handling arrays!"
class botrecv(threading.Thread):
def pack(self, string, type):
if type == "BYTE":
return string
elif type == "SHORT" and (len(string) < 2):
string = string + "\x20"*(2-len(string))
return string
elif type == "SHORT" and not (len(string) < 2):
return string
elif type == "STRING":
string = string + "\x20"*(64-len(string))
return string
elif type == "ARRAY":
return "We're never, ever handling arrays!"
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
self.commandsend("d", "'WHAZZAA!?' 'The sky.'")
logged = False
self.id = 0
self. x = 0
self.y = 0
self.z = 0
self.h = 0
self.p = 0
# self.commandsend(type, parts)
try:
while True:
packet = self.packcheck(self.sock.recv(1028))
if packet[0] == 0 and not logged:
logged = True
print "Server name: "+self.colparse(packet[1])
print "Server MOTD: "+self.colparse(packet[2])
# elif packet[0] == 1:
# pass
elif packet[0] == 2:
print "Level sending started. Ignoring level file."
# elif packet[0] == 3:
# pass
elif packet[0] == 4:
print "Level sending finished. Level details:"
print "Size: "+packet[1]+"^"+packet[2]+"^"+packet[3]
# elif packet[0] == 6:
# pass
elif packet[0] == 7:
if self.colparse(packet[2].strip()) == "gdude2002bot":
self.id = packet[1]
self.x = packet[3]
self.y = packet[4]
self.z = packet[5]
self.h = packet[6]
self.p = packet[7]
print "Bot has joined the map."
self.commandsend(8, [self.x, self.y, ((self.z/32)+5)*32, self.h, self.p])
else:
print " ".join(("User "+self.colparse(packet[2].strip())+" has joined the map.").split())
# elif packet[0] == 8:
# pass
# elif packet[0] == 9:
# pass
# elif packet[0] == "a":
# pass
# elif packet[0] == "b":
# pass
elif packet[0] == "c":
print "User ID "+packet[1]+" has disappeared."
elif packet[0] == "d":
# self.commandsend(type, parts)
command = (packet[2].split()[1])
# print "DEBUG: command: "+command
args = (packet[2].split()[2:])
# print "DEBUG: args: "+" ".join(args)
if self.colparse(command) == "!where":
self.commandsend("d", "/where")
print self.colparse(packet[2])
elif packet[0] == "e":
print "Current op status has changed."
# elif packet[0] == None:
# print "Odd packet recieved and ignored."
except KeyboardInterrupt:
print "*** SHUTTING DOWN ***"
sock.close()
exit
def colparse(self, string):
i = 0
parsed = ""
codes = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f"]
for char in string:
if char == "&":
char = ""
i = 1
elif (i == 1) and (char.lower() in codes):
char = ""
i = 0
else:
parsed = str(parsed) + str(char)
return parsed
def commandsend(self, type, parts):
#ID packet, we already sent this.
if type == 0:
print "We should have already identified earlier!"
#Blokchange packet. Let's try not to grief!
elif type == 5:
print "Changing block."
X = parts[0:1]
Y = parts[2:3]
X = parts[4:5]
Mode = parts[6]
Type = parts[7]
X = self.pack(X, "SHORT")
Y = self.pack(Y, "SHORT")
Z = self.pack(Z, "SHORT")
packed = ("\x05"+X+Y+Z+Mode+Type)
self.sock.send(packed)
#Movement packet. Wheeeee!
elif type == 8:
Unused = "\x00"
X = parts[0:1]
Y = parts[2:3]
Z = parts[4:5]
H = parts[6]
P = parts[7]
X = self.pack(X, "SHORT")
Y = self.pack(Y, "SHORT")
Z = self.pack(Z, "SHORT")
packed = ("\x08"+Unused+X+Y+Z+H+P)
self.sock.send(packed)
#Hai, packet can take your message nao plzkthxbai.
elif type == "d":
Unused = "\x00"
Text = parts[0:]
Text = self.pack(Text, "STRING")
packed = ("\x0d"+Unused+Text)
self.sock.send(packed)
#WTF? Typo?
else:
print "WTF? What the hell is this packet? I think you typo'd!"
def packcheck(self, packet):
code = packet[0]
vaild = ["\x00", "\x01", "\x02", "\x03", "\x04", "\x06", "\x07", "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e"]
ignored = ["\x03", "\x06", "\x0e"]
if code == "\x00":
#Server ID packet.
protocol = packet[1]
name = packet[2:65]
motd = packet[66:130]
# mode = packet[131]
return [0, name, motd, protocol]
elif code == "\x01":
#Ping. We can ignore this.
return [1]
elif code == "\x02":
#Incoming level data! Good thing we don't need it.
return [2]
elif code == "\x03":
#This is the level data. We ignore it.
return [3, None]
elif code == "\x04":
#OK, level sending is finished. Yay!
x = packet[1:2]
y = packet[3:4]
z = packet[5:6]
return [4, x, y, z]
# There's no '\x05' packetto recieve. It's a client block change.
elif code == "\x06":
#Set block. We probably won't use it, but maybe we will.
#We'll parse it, just in case.
x = packet[1:2]
y = packet[3:4]
z = packet[5:6]
type = packet[7]
return [6, x, y, z, type]
elif code == "\x07":
#Oshit, a player joined. We'll parse this, just in case.
ID = [1]
name = packet[2:66]
x = packet[67:68]
y = packet[69:70]
z = packet[71:72]
head = packet[73]
pitch = packet[74]
return [7, ID, name, x, y, z, head, pitch]
elif code == "\x08":
#Whoops, teleport! We'll parse this too.
ID = packet[1]
x = packet[2:3]
y = packet[4:5]
z = packet[6:7]
head = packet[8]
pitch = packet[9]
return [8, ID, x, y, z, head, pitch]
elif code == "\x09":
#Someone moved AND turned. How dare they! >:D
ID = packet[1]
dx = packet[2]
dy = packet[3]
dz = packet[4]
dhead = packet[5]
dpitch = packet[6]
return [9, ID, dx, dy, dz, dhead, dpitch]
elif code == "\x0a":
#Someone moved without turning. Stiff neck?
ID = packet[1]
dx = packet[2]
dy = packet[3]
dz = packet[4]
return ["a", ID, dx, dy, dz]
elif code == "\x0b":
#Someone turned without moving. Diiiizzzzy....
ID = packet[1]
dhead = packet[2]
dpitch = packet[3]
return ["b", ID, dhead, dpitch]
elif code == "\x0c":
#Somone left. GTFO!
ID = packet[1]
return ["c", ID]
elif code == "\x0d":
#Message alert! You have new mail!
ID = packet[1]
text = packet[2:66]
return ["d", ID, text]
elif code == "\x0e":
#We've been opped or deoped. Meddling admins!
type = packet[1]
return ["e", type]
else:
#Someone sent us a weird packet. We can'e handle it.
#Meddling users..
return[None]
bot().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment