<commit_before>import exceptions
from twisted.internet import reactor
from commands import Command
import plugins as plugins
from data.clients import dbManager
import struct
import packetFactory
import json
import os


def write_file(filename, data, mode='wb'):
    with open(filename, mode) as f:
        f.write(data)

@plugins.on_start_hook
def on_start():
    print("!!! WARNING !!!")
    print("You have PacketLogger.py outside of the disabled plugins folder! This means it is ON!")
    print("This plugin can log sensitive data!")
    print("Please be careful!")
    print("!!! WARNING !!!")


@plugins.on_initial_connect_hook
def notify_and_config(client):
    """
    :type client: ShipProxy.ShipProxy
    """
    client_config = dbManager.get_data_for_sega_id(client.myUsername)
    if 'logPackets' not in dbManager:
        client_config['logPackets'] = False
    if client_config['logPackets']:
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {gre}You have opted-in to packet logging, Thank you! View your contributions on http://pso2proxy.cyberkitsune.net/redpill/ or use !optout to opt out", 0x3).build())
    else:
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {red}You have not opted-in to packet logging, and it has been disabled. Use !optin to opt in.", 0x3).build())


@plugins.CommandHook("optin", "Opts you into packet logging for the redpill project.")
class OptIn(Command):
    def call_from_client(self, client):
        client_config = dbManager.get_data_for_sega_id(client.myUsername)
        client_config['logPackets'] = True
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {gre}You have enabled packet logging! Thank you! Track your data at http://pso2proxy.cyberkitsune.net/redpill/", 0x3).build())


@plugins.CommandHook("optout", "Opts you out of the packet logging for the redpill project.")
class OptOut(Command):
    def call_from_client(self, client):
        archive_packets(client)
        client_config = dbManager.get_data_for_sega_id(client.myUsername)
        client_config['logPackets'] = True
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {red}You have disabled packet logging! :( If you change your mind, please use !optin to rejoin!", 0x3).build())


@plugins.raw_packet_hook
def on_packet_received(context, packet, packet_type, packet_subtype):
    """
    :type context: ShipProxy.ShipProxy
    """
    if context.myUsername is not None:
        client_config = dbManager.get_data_for_sega_id(context.myUsername)
        if 'logPackets' not in client_config:
            client_config['logPackets'] = False
        elif not client_config['logPackets']:
            if 'orphans' in context.extendedData:
                print("[PacketLogger] %s has opted out of packet logging. Deleting orphans..." % context.myUsername)
                del context.extendedData['orphans']
            return packet
    if packet_type == 0x11 and packet_subtype == 0x0:
        packet_data = bytearray(packet)
        struct.pack_into("64x", packet_data, 0x48)
        packet_data = str(packet_data)
    else:
        packet_data = packet
    if context.psoClient:
        sender = "C"
    else:
        sender = "S"
    if context.myUsername is not None:
        path = 'packets/%s/%s/%i.%x-%x.%s.bin' % (
            context.myUsername, context.connTimestamp, context.packetCount, packet_type, packet_subtype,
            sender)
        try:
            os.makedirs(os.path.dirname(path))
        except exceptions.OSError:
            pass
        reactor.callInThread(write_file, path, packet_data)
    else:
        if 'orphans' not in context.extendedData:
            context.extendedData['orphans'] = []
        context.extendedData['orphans'].append(
            {'data': packet_data, 'count': context.packetCount, 'type': packet_type, "sub": packet_subtype, "sender": sender})

    if context.myUsername is not None and 'orphans' in context.extendedData and len(context.extendedData['orphans']) > 0:
        count = 0<commit_msg>Fix up a few things here...<commit_after>import exceptions
from twisted.internet import reactor
from commands import Command
import plugins as plugins
from data.clients import dbManager
import struct
import packetFactory
import json
import os


def write_file(filename, data, mode='wb'):
    with open(filename, mode) as f:
        f.write(data)

@plugins.on_start_hook
def on_start():
    print("!!! WARNING !!!")
    print("You have PacketLogger.py outside of the disabled plugins folder! This means it is ON!")
    print("This plugin can log sensitive data!")
    print("Please be careful!")
    print("!!! WARNING !!!")


@plugins.on_initial_connect_hook
def notify_and_config(client):
    """
    :type client: ShipProxy.ShipProxy
    """
    client_config = dbManager.get_data_for_sega_id(client.myUsername)
    if 'logPackets' not in client_config:
        client_config['logPackets'] = False
    if client_config['logPackets']:
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {gre}You have opted-in to packet logging, Thank you! View your contributions on http://pso2proxy.cyberkitsune.net/redpill/ or use !optout to opt out", 0x3).build())
    else:
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {red}You have not opted-in to packet logging, and it has been disabled. Use !optin to opt in.", 0x3).build())


@plugins.CommandHook("optin", "Opts you into packet logging for the redpill project.")
class OptIn(Command):
    def call_from_client(self, client):
        client_config = dbManager.get_data_for_sega_id(client.myUsername)
        client_config['logPackets'] = True
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {gre}You have enabled packet logging! Thank you! Track your data at http://pso2proxy.cyberkitsune.net/redpill/", 0x3).build())


@plugins.CommandHook("optout", "Opts you out of the packet logging for the redpill project.")
class OptOut(Command):
    def call_from_client(self, client):
        archive_packets(client)
        client_config = dbManager.get_data_for_sega_id(client.myUsername)
        client_config['logPackets'] = True
        client.send_crypto_packet(packetFactory.SystemMessagePacket("[PacketLogging] {red}You have disabled packet logging! :( If you change your mind, please use !optin to rejoin!", 0x3).build())


@plugins.raw_packet_hook
def on_packet_received(context, packet, packet_type, packet_subtype):
    """
    :type context: ShipProxy.ShipProxy
    """
    if context.myUsername is not None:
        client_config = dbManager.get_data_for_sega_id(context.myUsername)
        if 'logPackets' not in client_config:
            client_config['logPackets'] = False
            return packet
        if not client_config['logPackets']:
            if 'orphans' in context.extendedData:
                print("[PacketLogger] %s has opted out of packet logging. Deleting orphans..." % context.myUsername)
                del context.extendedData['orphans']
            return packet
    if packet_type == 0x11 and packet_subtype == 0x0:
        packet_data = bytearray(packet)
        struct.pack_into("64x", packet_data, 0x48)
        packet_data = str(packet_data)
    else:
        packet_data = packet
    if context.psoClient:
        sender = "C"
    else:
        sender = "S"
    if context.myUsername is not None:
        path = 'packets/%s/%s/%i.%x-%x.%s.bin' % (
            context.myUsername, context.connTimestamp, context.packetCount, packet_type, packet_subtype,
            sender)
        try:
            os.makedirs(os.path.dirname(path))
        except exceptions.OSError:
            pass
        reactor.callInThread(write_file, path, packet_data)
    else:
        if 'orphans' not in context.extendedData:
            context.extendedData['orphans'] = []
        context.extendedData['orphans'].append(
            {'data': packet_data, 'count': context.packetCount, 'type': packet_type, "sub": packet_subtype, "sender": sender})

    if context.myUsername is not None and 'orphans' in context.extendedData and len(context.extendedData['orphans']) > 0:
        count = 0
{'fraction_non_alphanumeric': 0.10538387252535007, 'fraction_numerical': 0.005311443746982134, 'mean_word_length': 3.5397260273972604, 'pattern_counts': {'":': 4, '<': 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': '6631419', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2431, 'n_words': 640}