iptracker v1.1.0 updating to helpers v2.0.0 functions; fixing bugs in data management and command execution

This commit is contained in:
BuildTools
2016-01-09 16:43:19 +01:00
parent cbc77de826
commit 3a2d1193ca

215
iptracker.py Normal file → Executable file
View File

@@ -1,107 +1,108 @@
import mysqlhack import mysqlhack
import org.bukkit as bukkit import org.bukkit as bukkit
import json import json
from java.util import UUID as UUID from java.util import UUID as UUID
from helpers import * from helpers import *
from org.bukkit import * from org.bukkit import *
from traceback import format_exc as trace from traceback import format_exc as trace
from iptracker_secrets import * from iptracker_secrets import *
iptrack_permission = "utils.iptrack" iptrack_permission = "utils.iptrack"
iptrack_version = "1.1.0"
@hook.event("player.PlayerJoinEvent", "low")
def on_player_join(event): @hook.event("player.PlayerJoinEvent", "low")
t = threading.Thread(target=on_player_join_thread, args=(event, )) def on_player_join(event):
t.daemon = True t = threading.Thread(target=on_player_join_thread, args=(event, ))
t.start() t.daemon = True
t.start()
def on_player_join_thread(event):
player = event.getPlayer() def on_player_join_thread(event):
ip = player.getAddress().getHostString() player = event.getPlayer()
uuid = uid(player) ip = player.getAddress().getHostString()
conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver") uuid = uid(player)
curs = conn.cursor() conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver")
curs.execute("SELECT ips FROM uuid2ips WHERE uuid = ?", (uuid, )) curs = conn.cursor()
results = curs.fetchall() curs.execute("SELECT ips FROM uuid2ips WHERE uuid = ?", (uuid, ))
if len(results) == 0: results = curs.fetchall()
ips = [] if len(results) == 0:
else: ips = []
ips = json.loads(results[0][0]) else:
curs.execute("SELECT uuids FROM ip2uuids WHERE ip = ?", (ip, )) ips = json.loads(results[0][0])
results = curs.fetchall() curs.execute("SELECT uuids FROM ip2uuids WHERE ip = ?", (ip, ))
if len(results) == 0: results = curs.fetchall()
uuids = [] if len(results) == 0:
else: uuids = []
uuids = json.loads(results[0][0]) else:
new_ip_entry = (len(ips) == 0) uuids = json.loads(results[0][0])
new_uuid_entry = (len(uuids) == 0) new_ip_entry = (len(ips) == 0)
if ip not in ips: new_uuid_entry = (len(uuids) == 0)
ips.append(ip) if ip not in ips:
if new_ip_entry: ips.append(ip)
curs.execute("INSERT INTO uuid2ips VALUES (?,?)", (uuid, json.dumps(ips), )) if new_ip_entry:
else: curs.execute("INSERT INTO uuid2ips VALUES (?,?)", (uuid, json.dumps(ips), ))
curs.execute("UPDATE uuid2ips SET ips = ? WHERE uuid = ?", (uuid, json.dumps(ips), )) else:
if uuid not in uuids: curs.execute("UPDATE uuid2ips SET ips = ? WHERE uuid = ?", (json.dumps(ips), uuid, ))
uuids.append(uuid) if uuid not in uuids:
if new_uuid_entry: uuids.append(uuid)
curs.execute("INSERT INTO ip2uuids VALUES (?,?)", (ip, json.dumps(uuids), )) if new_uuid_entry:
else: curs.execute("INSERT INTO ip2uuids VALUES (?,?)", (ip, json.dumps(uuids), ))
curs.execute("UPDATE ip2uuids SET uuids = ? WHERE uuid = ?", (ip, json.dumps(uuids), )) else:
conn.commit() curs.execute("UPDATE ip2uuids SET uuids = ? WHERE ip = ?", (json.dumps(uuids), ip, ))
curs.close() conn.commit()
conn.close() curs.close()
conn.close()
@hook.command("getinfo")
def on_getinfo_command(sender, args): @hook.command("getinfo")
t = threading.Thread(target=on_player_join_thread, args=(sender, args)) def on_getinfo_command(sender, args):
t.daemon = True t = threading.Thread(target=on_getinfo_command_thread, args=(sender, args))
t.start() t.daemon = True
t.start()
def on_getinfo_command_thread(sender, args):
if(sender.hasPermission(iptrack_permission)): def on_getinfo_command_thread(sender, args):
if not checkargs(sender, args, 1, 1): if(sender.hasPermission(iptrack_permission)):
return False if not checkargs(sender, args, 1, 1):
else: return False
if isIP(args[0]): else:
conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver") if is_ip(args[0]):
curs = conn.cursor() conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver")
curs.execute("SELECT uuids FROM ip2uuids WHERE ip = ?", (args[0], )) curs = conn.cursor()
results = curs.fetchall() curs.execute("SELECT uuids FROM ip2uuids WHERE ip = ?", (args[0], ))
curs.close() results = curs.fetchall()
conn.close() curs.close()
if len(results) == 0: conn.close()
msg(sender, "IP " + args[0] + " is not registered in the database, maybe you got a number wrong?") if len(results) == 0:
else: msg(sender, "IP " + args[0] + " is not registered in the database, maybe you got a number wrong?")
uuids = json.loads(results[0][0]) else:
msg(sender, "IP " + args[0] + " was seen with " + str(len(uuids)) + " different Accounts:") uuids = json.loads(results[0][0])
for i in range(0, len(uuids)): msg(sender, "IP " + args[0] + " was seen with " + str(len(uuids)) + " different Accounts:")
p=Bukkit.getOfflinePlayer(UUID.fromString(uuids[i])) for i in range(0, len(uuids)):
if is_player(sender): p=Bukkit.getOfflinePlayer(UUID.fromString(uuids[i]))
send_JSON_message(sender.getName(), '["",{"text":"' + p.getName() + ' - (uuid: ' + uuids[i] + '","color":"gold","clickEvent":{"action":"run_command","value":"/getinfo ' + p.getName() + '"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"To search for ' + p.getName() + ' in the database, simply click the name!","color":"gold"}]}}}]') if is_player(sender):
else: send_JSON_message(sender.getName(), '["",{"text":"' + p.getName() + ' - (uuid: ' + uuids[i] + '","color":"gold","clickEvent":{"action":"run_command","value":"/getinfo ' + p.getName() + '"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"To search for ' + p.getName() + ' in the database, simply click the name!","color":"gold"}]}}}]')
msg(sender,p.getName() + " - (uuid: " + uuids[i] + ")") else:
else: msg(sender,p.getName() + " - (uuid: " + uuids[i] + ")")
target = Bukkit.getOfflinePlayer(args[0]) else:
uuid = target.getUniqueId() target = Bukkit.getOfflinePlayer(args[0])
conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver") uuid = target.getUniqueId()
curs = conn.cursor() conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver")
curs.execute("SELECT ips FROM uuid2ips WHERE uuid = ?", (uuid.toString(), )) curs = conn.cursor()
results = curs.fetchall() curs.execute("SELECT ips FROM uuid2ips WHERE uuid = ?", (uuid.toString(), ))
curs.close() results = curs.fetchall()
conn.close() curs.close()
if len(results) == 0: conn.close()
msg(sender, "Player " + args[0] + " is not registered in the database, maybe you misspelled the name?") if len(results) == 0:
else: msg(sender, "Player " + args[0] + " is not registered in the database, maybe you misspelled the name?")
ips = json.loads(results[0][0]) else:
msg(sender, "Player " + args[0] + " was seen with " + str(len(ips)) + " different IPs:") ips = json.loads(results[0][0])
for i in range(0, len(ips)): msg(sender, "Player " + args[0] + " was seen with " + str(len(ips)) + " different IPs:")
if is_player(sender): for i in range(0, len(ips)):
send_JSON_message(sender.getName(), '["",{"text":"' + ips[i] + '","color":"gold","clickEvent":{"action":"run_command","value":"/getinfo ' + ips[i] + '"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"To search for the IP ' + ips[i] + ' in the database, simply click the IP!","color":"gold"}]}}}]') if is_player(sender):
else: send_JSON_message(sender.getName(), '["",{"text":"' + ips[i] + '","color":"gold","clickEvent":{"action":"run_command","value":"/getinfo ' + ips[i] + '"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"To search for the IP ' + ips[i] + ' in the database, simply click the IP!","color":"gold"}]}}}]')
msg(sender,ips[i]) else:
else: msg(sender,ips[i])
noperm(sender) else:
return True noperm(sender)
return True