<commit_before>import struct
import timeit
from constants import PacketIDs, PacketSizes, PacketFormats, Blocks, TransparentBlocks, TreeShape
from math import floor
from util import notch_to_string, string_to_notch
from md5 import md5
from twisted.internet.protocol import Protocol

def unpack_byte(byte):
    return struct.unpack("B",byte[0])[0]

class SchnitzelProtocol(Protocol):
    def __init__(self):
        self.buf = b""
        self.packetsize = None

        self.handlers = {
            PacketIDs["Identification"]: self.identify,
            PacketIDs["ClientSetBlock"]: self.setblock,
            PacketIDs["PositionAndOrientation"]: self.posandort,
            PacketIDs["Message"]: self.message
        }
        
        self.name = None
        self.ID = None
        self.op = False
        self.identified = False
        
        self.x = 0
        self.y = 0
        self.z = 0
        self.yaw = 0
        self.pitch = 0
        
        print "SchnitzelProtocol created"
    
    def dataReceived(self, data):
        self.buf += data
        while self.buf:
            if not self.packetsize:
                byte = unpack_byte(self.buf[0])
                if byte in self.handlers:
                    self.packetsize = PacketSizes[byte]
                else:
                    # print "Error! Unhandled packet! (%s)" % byte
                    self.transport.loseConnection()
            else:
                if len(self.buf) >= self.packetsize:
                    self.handlers[byte](self.buf[:PacketSizes[byte]]) # Handle packet
                    self.buf = self.buf[PacketSizes[byte]:]
                    self.packetsize = None
                else:
                    break
                    
    def connectionLost(self, reason):
        print "%s disconnected." % self.name
        if self.ID and self.identified:
            del self.factory.protocols[self.ID]
            self.factory.usedIDs.remove(self.ID)
            self.factory.sendPacketSkip(self, PacketIDs["DespawnPlayer"], self.ID)
            if self.op:
                self.factory.sendMessage("[OP] %s disconnected" % self.name)
            else:
                self.factory.sendMessage("%s disconnected" % self.name)
            args = (self.name, self.x/32, self.y/32, self.z/32)
                
    def sendPacket(self, *packet):
        format = PacketFormats[packet[0]]
        packet = struct.pack(format, *packet)
        self.transport.write(packet)
        
    def unpackPacket(self, data):
        format = PacketFormats[unpack_byte(data[0])]
        return struct.unpack(format, data)
    
    def identify(self, data):
        packet = self.unpackPacket(data)
        self.name = notch_to_string(packet[2])
        key = notch_to_string(packet[3])
        print "%s connected." % self.name
        
        if not self.factory.config["noverify"]:
            if key != "--" and key == md5().hexdigest("%s%s" % (self.factory.salt, self.name)):
                print " +- verified with minecraft.net"
            else:
                print " +- player is forging the username"
        
        if self.name in self.factory.config["ops"]:
            self.op = True
            print " +- player is op"
        <commit_msg>Fix incorrect md5() usage<commit_after>import struct
import timeit
from constants import PacketIDs, PacketSizes, PacketFormats, Blocks, TransparentBlocks, TreeShape
from math import floor
from util import notch_to_string, string_to_notch
from md5 import md5
from twisted.internet.protocol import Protocol

def unpack_byte(byte):
    return struct.unpack("B",byte[0])[0]

class SchnitzelProtocol(Protocol):
    def __init__(self):
        self.buf = b""
        self.packetsize = None

        self.handlers = {
            PacketIDs["Identification"]: self.identify,
            PacketIDs["ClientSetBlock"]: self.setblock,
            PacketIDs["PositionAndOrientation"]: self.posandort,
            PacketIDs["Message"]: self.message
        }
        
        self.name = None
        self.ID = None
        self.op = False
        self.identified = False
        
        self.x = 0
        self.y = 0
        self.z = 0
        self.yaw = 0
        self.pitch = 0
        
        print "SchnitzelProtocol created"
    
    def dataReceived(self, data):
        self.buf += data
        while self.buf:
            if not self.packetsize:
                byte = unpack_byte(self.buf[0])
                if byte in self.handlers:
                    self.packetsize = PacketSizes[byte]
                else:
                    # print "Error! Unhandled packet! (%s)" % byte
                    self.transport.loseConnection()
            else:
                if len(self.buf) >= self.packetsize:
                    self.handlers[byte](self.buf[:PacketSizes[byte]]) # Handle packet
                    self.buf = self.buf[PacketSizes[byte]:]
                    self.packetsize = None
                else:
                    break
                    
    def connectionLost(self, reason):
        print "%s disconnected." % self.name
        if self.ID and self.identified:
            del self.factory.protocols[self.ID]
            self.factory.usedIDs.remove(self.ID)
            self.factory.sendPacketSkip(self, PacketIDs["DespawnPlayer"], self.ID)
            if self.op:
                self.factory.sendMessage("[OP] %s disconnected" % self.name)
            else:
                self.factory.sendMessage("%s disconnected" % self.name)
            args = (self.name, self.x/32, self.y/32, self.z/32)
                
    def sendPacket(self, *packet):
        format = PacketFormats[packet[0]]
        packet = struct.pack(format, *packet)
        self.transport.write(packet)
        
    def unpackPacket(self, data):
        format = PacketFormats[unpack_byte(data[0])]
        return struct.unpack(format, data)
    
    def identify(self, data):
        packet = self.unpackPacket(data)
        self.name = notch_to_string(packet[2])
        key = notch_to_string(packet[3])
        print "%s connected." % self.name
        
        if not self.factory.config["noverify"]:
            if key != "--" and key == md5("%s%s" % (self.factory.salt, self.name)).hexdigest():
                print " +- verified with minecraft.net"
            else:
                print " +- player is forging the username"
        
        if self.name in self.factory.config["ops"]:
            self.op = True
            print " +- player is op"
        
{'fraction_non_alphanumeric': 0.10547355473554736, 'fraction_numerical': 0.006611316113161131, 'mean_word_length': 1.7424114671163575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '351116', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1805, 'n_words': 411}