Merge pull request #25 from msoucy/ref/python/lists

Use Python list comprehensions and literals when possible
This commit is contained in:
polaris-
2014-05-27 08:02:11 -04:00
8 changed files with 207 additions and 244 deletions

View File

@@ -79,12 +79,12 @@ class Gamestats(LineReceiver):
self.challenge = utils.generate_random_str(10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# The first command sent to the client is always a login challenge containing the server challenge key.
msg_d = []
msg_d.append(('__cmd__', "lc"))
msg_d.append(('__cmd_val__', "1"))
msg_d.append(('challenge', self.challenge))
msg_d.append(('id', "1"))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "lc"),
('__cmd_val__', "1"),
('challenge', self.challenge),
('id', "1"),
])
self.log(logging.DEBUG, "SENDING: '%s'..." % msg)
@@ -103,25 +103,22 @@ class Gamestats(LineReceiver):
commands, self.remaining_message = gs_query.parse_gamespy_message(msg)
#logger.log(logging.DEBUG, "STATS RESPONSE: %s" % msg)
for data_parsed in commands:
print data_parsed
cmds = {
"auth": self.perform_auth,
"authp": self.perform_authp,
"ka": self.perform_ka,
"setpd": self.perform_setpd,
"getpd": self.perform_getpd,
"newgame": self.perform_newgame,
"updgame": self.perform_updgame,
}
def cmd_err(data_parsed):
logger.log(logging.DEBUG, "Found unknown command, don't know how to handle '%s'.", data_parsed['__cmd__'])
if data_parsed['__cmd__'] == "auth":
self.perform_auth(data_parsed)
elif data_parsed['__cmd__'] == "authp":
self.perform_authp(data_parsed)
elif data_parsed['__cmd__'] == "ka":
self.perform_ka(data_parsed)
elif data_parsed['__cmd__'] == "setpd":
self.perform_setpd(data_parsed, msg)
elif data_parsed['__cmd__'] == "getpd":
self.perform_getpd(data_parsed)
elif data_parsed['__cmd__'] == "newgame":
self.perform_newgame(data_parsed)
elif data_parsed['__cmd__'] == "updgame":
self.perform_updgame(data_parsed)
else:
logger.log(logging.DEBUG, "Found unknown command, don't know how to handle '%s'." % data_parsed['__cmd__'])
for data_parsed in commands:
print(data_parsed)
cmds.get(data_parsed['__cmd__'], cmd_err)(data_parsed)
def perform_auth(self, data_parsed):
self.log(logging.DEBUG, "Parsing 'auth'...")
@@ -131,13 +128,13 @@ class Gamestats(LineReceiver):
self.session = utils.generate_random_number_str(10)
msg_d = []
msg_d.append(('__cmd__', "lc"))
msg_d.append(('__cmd_val__', "2"))
msg_d.append(('sesskey', self.session))
msg_d.append(('proof', 0))
msg_d.append(('id', "1"))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "lc"),
('__cmd_val__', "2"),
('sesskey', self.session),
('proof', 0),
('id', "1"),
])
self.log(logging.DEBUG, "SENDING: '%s'..." % msg)
@@ -217,11 +214,11 @@ class Gamestats(LineReceiver):
self.sessions[profileid] = self
msg_d = []
msg_d.append(('__cmd__', "pauthr"))
msg_d.append(('__cmd_val__', profileid))
msg_d.append(('lid', self.lid))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "pauthr"),
('__cmd_val__', profileid),
('lid', self.lid),
])
self.profileid = int(profileid)
@@ -234,10 +231,10 @@ class Gamestats(LineReceiver):
pass
def perform_ka(self, data_parsed):
msg_d = []
msg_d.append(('__cmd__', "ka"))
msg_d.append(('__cmd_val__', ""))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "ka"),
('__cmd_val__', ""),
])
self.log(logging.DEBUG, "SENDING: '%s'..." % msg)
@@ -246,13 +243,13 @@ class Gamestats(LineReceiver):
return
def perform_setpd(self, data_parsed, data):
msg_d = []
msg_d.append(('__cmd__', "setpdr"))
msg_d.append(('__cmd_val__', 1))
msg_d.append(('lid', self.lid))
msg_d.append(('pid', self.profileid))
msg_d.append(('mod', int(time.time())))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "setpdr"),
('__cmd_val__', 1),
('lid', self.lid),
('pid', self.profileid),
('mod', int(time.time())),
])
self.log(logging.DEBUG, "SENDING: '%s'..." % msg)
@@ -280,7 +277,6 @@ class Gamestats(LineReceiver):
def perform_getpd(self, data_parsed):
profile = self.db.pd_get(self.profileid, data_parsed['dindex'], data_parsed['ptype'])
data = ""
keys = data_parsed['keys'].split('\x01')
profile_data = None
@@ -291,25 +287,24 @@ class Gamestats(LineReceiver):
if profile_data != None:
profile_data = profile_data[0][0]
for key in keys:
if key != "__cmd__" and key != "__cmd_val__" and key != "":
data += "\\"
data += key
data += "\\"
if key in profile_data:
data += profile_data[key]
data = ""
for key in (key for key in keys if key not in ("__cmd__", "__cmd_val__", "")):
data += "\\" + key + "\\"
# this WILL error if profile_data isn't properly set above
if key in profile_data:
data += profile_data[key]
modified = int(time.time())
msg_d = []
msg_d.append(('__cmd__', "getpdr"))
msg_d.append(('__cmd_val__', 1))
msg_d.append(('lid', self.lid))
msg_d.append(('pid', self.profileid))
msg_d.append(('mod', modified))
msg_d.append(('length', len(data)))
msg_d.append(('data', data))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "getpdr"),
('__cmd_val__', 1),
('lid', self.lid),
('pid', self.profileid),
('mod', modified),
('length', len(data)),
('data', data),
])
self.log(logging.DEBUG, "SENDING: '%s'..." % msg)

View File

@@ -75,9 +75,10 @@ class PlayerSearch(LineReceiver):
# Example from: filtered-mkw-log-2014-01-01-ct1310.eth
# \otherslist\\o\146376154\uniquenick\2m0isbjmvRMCJ2i5321j\o\192817284\uniquenick\1jhggtmghRMCJ2jrsh23\o\302594991\uniquenick\7dkjp51v5RMCJ2nr3vs9\o\368031897\uniquenick\1v7p3qmkpRMCJ1o8f56p\o\447214276\uniquenick\7dkt0p6gtRMCJ2ljh72h\o\449615791\uniquenick\4puvrm1g4RMCJ00ho3v1\o\460250854\uniquenick\4rik5l1u1RMCJ0tc3fii\o\456284963\uniquenick\1unitvi86RMCJ1b10u02\o\453830866\uniquenick\7de3q52dbRMCJ2877ss2\o\450197498\uniquenick\3qtutr1ikRMCJ38gem1n\o\444241868\uniquenick\67tp53bs9RMCJ1abs7ej\o\420030955\uniquenick\5blesqia3RMCJ322bbd6\o\394609454\uniquenick\0hddp7mq2RMCJ30uv7r7\o\369478991\uniquenick\59de9c2bhRMCJ0re0fii\o\362755626\uniquenick\5tte2lif7RMCJ0cscgtg\o\350951571\uniquenick\7aeummjlaRMCJ3li4ls2\o\350740680\uniquenick\484uiqhr4RMCJ18opoj0\o\349855648\uniquenick\5blesqia3RMCJ1c245dn\o\324078642\uniquenick\62go5gpt0RMCJ0v0uhc9\o\304111337\uniquenick\4lcg6ampvRMCJ1gjre51\o\301273266\uniquenick\1dhdpjhn8RMCJ2da6f9h\o\193178453\uniquenick\3pcgu0299RMCJ3nhu50f\o\187210028\uniquenick\3tau15a9lRMCJ2ar247h\o\461622261\uniquenick\59epddrnkRMCJ1t2ge7l\oldone\\final\
msg_d = []
msg_d.append(('__cmd__', "otherslist"))
msg_d.append(('__cmd_val__', ""))
msg_d = [
('__cmd__', "otherslist"),
('__cmd_val__', ""),
]
if "numopids" in data_parsed and "opids" in data_parsed:
numopids = int(data_parsed['numopids'])
@@ -92,7 +93,6 @@ class PlayerSearch(LineReceiver):
msg_d.append(('o', opid))
if profile != None:
msg_d.append(('uniquenick', profile['uniquenick']))
else:
msg_d.append(('uniquenick', ''))

View File

@@ -100,12 +100,12 @@ class PlayerSession(LineReceiver):
self.challenge = utils.generate_random_str(10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# The first command sent to the client is always a login challenge containing the server challenge key.
msg_d = []
msg_d.append(('__cmd__', "lc"))
msg_d.append(('__cmd_val__', "1"))
msg_d.append(('challenge', self.challenge))
msg_d.append(('id', "1"))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "lc"),
('__cmd_val__', "1"),
('challenge', self.challenge),
('id', "1"),
])
self.log(logging.DEBUG, "SENDING: '%s'..." % msg)
self.transport.write(bytes(msg))
@@ -142,33 +142,26 @@ class PlayerSession(LineReceiver):
commands, self.remaining_message = gs_query.parse_gamespy_message(data)
cmds = {
"login": self.perform_login,
"logout": self.perform_logout,
"getprofile": self.perform_getprofile,
"updatepro": self.perform_updatepro,
"ka": self.perform_ka,
"status": self.perform_status,
"bm": self.perform_bm,
"addbuddy": self.perform_addbuddy,
"delbuddy": self.perform_delbuddy,
"authadd": self.perform_authadd,
}
def cmd_err(data_parsed):
# Maybe write unknown commands to a separate file later so new data can be collected more easily?
self.log(logging.ERROR, "Found unknown command, don't know how to handle '%s'." % data_parsed['__cmd__'])
for data_parsed in commands:
#self.log(-1, data_parsed)
self.log(logging.DEBUG, data_parsed)
if data_parsed['__cmd__'] == "login":
self.perform_login(data_parsed)
elif data_parsed['__cmd__'] == "logout":
self.perform_logout(data_parsed)
elif data_parsed['__cmd__'] == "getprofile":
self.perform_getprofile(data_parsed)
elif data_parsed['__cmd__'] == "updatepro":
self.perform_updatepro(data_parsed)
elif data_parsed['__cmd__'] == "ka":
self.perform_ka(data_parsed)
elif data_parsed['__cmd__'] == "status":
self.perform_status(data_parsed)
elif data_parsed['__cmd__'] == "bm":
self.perform_bm(data_parsed)
elif data_parsed['__cmd__'] == "addbuddy":
self.perform_addbuddy(data_parsed)
elif data_parsed['__cmd__'] == "delbuddy":
self.perform_delbuddy(data_parsed)
elif data_parsed['__cmd__'] == "authadd":
self.perform_authadd(data_parsed)
else:
# Maybe write unknown commands to a separate file later so new data can be collected more easily?
self.log(logging.ERROR, "Found unknown command, don't know how to handle '%s'." % data_parsed['__cmd__'])
cmds.get(data_parsed['__cmd__'], cmd_err)(data_parsed)
def perform_login(self, data_parsed):
authtoken_parsed = gs_utils.parse_authtoken(data_parsed['authtoken'], self.db)
@@ -249,17 +242,18 @@ class PlayerSession(LineReceiver):
self.sessions[profileid] = self
msg_d = []
msg_d.append(('__cmd__', "lc"))
msg_d.append(('__cmd_val__', "2"))
msg_d.append(('sesskey', self.sesskey))
msg_d.append(('proof', proof))
msg_d.append(('userid', userid))
msg_d.append(('profileid', profileid))
msg_d.append(('uniquenick', uniquenick))
msg_d.append(('lt', loginticket)) # Some kind of token... don't know it gets used or generated, but it doesn't seem to have any negative effects if it's not properly generated.
msg_d.append(('id', data_parsed['id']))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "lc"),
('__cmd_val__', "2"),
('sesskey', self.sesskey),
('proof', proof),
('userid', userid),
('profileid', profileid),
('uniquenick', uniquenick),
# Some kind of token... don't know it gets used or generated, but it doesn't seem to have any negative effects if it's not properly generated.
('lt', loginticket),
('id', data_parsed['id']),
])
# Take the first 4 letters of gsbrcd instead of gamecd because they should be consistent across game
# regions. For example, the US version of Metroid Prime Hunters has the gamecd "AMHE" and the first 4 letters
@@ -268,7 +262,7 @@ class PlayerSession(LineReceiver):
# Japanese version (ATRJ) while the gamecd is region specific (ATRE for US and ATRJ for JP).
# gameid is used to send all people on the player's friends list a status updates, so don't make it region
# specific.
self.gameid = gsbrcd[0:4]
self.gameid = gsbrcd[:4]
self.profileid = int(profileid)
self.log(logging.DEBUG, "SENDING: %s" % msg)
@@ -306,16 +300,17 @@ class PlayerSession(LineReceiver):
# Wii example: \pi\\profileid\474888031\nick\5pde5vhn1WR9E2g1t533\userid\442778352\email\5pde5vhn1WR9E2g1t533@nds\sig\b126556e5ee62d4da9629dfad0f6b2a8\uniquenick\5pde5vhn1WR9E2g1t533\pid\11\lon\0.000000\lat\0.000000\loc\\id\2\final\
sig = utils.generate_random_hex_str(32)
msg_d = []
msg_d.append(('__cmd__', "pi"))
msg_d.append(('__cmd_val__', ""))
msg_d.append(('profileid', profile['profileid']))
msg_d.append(('nick', profile['uniquenick']))
msg_d.append(('userid', profile['userid']))
msg_d.append(('email', profile['email']))
msg_d.append(('sig', sig))
msg_d.append(('uniquenick', profile['uniquenick']))
msg_d.append(('pid', profile['pid']))
msg_d = [
('__cmd__', "pi"),
('__cmd_val__', ""),
('profileid', profile['profileid']),
('nick', profile['uniquenick']),
('userid', profile['userid']),
('email', profile['email']),
('sig', sig),
('uniquenick', profile['uniquenick']),
('pid', profile['pid']),
]
if profile['firstname'] != "":
msg_d.append(('firstname', profile['firstname'])) # Wii gets a firstname
@@ -323,10 +318,12 @@ class PlayerSession(LineReceiver):
if profile['lastname'] != "":
msg_d.append(('lastname', profile['lastname']))
msg_d.append(('lon', profile['lon']))
msg_d.append(('lat', profile['lat']))
msg_d.append(('loc', profile['loc']))
msg_d.append(('id', data_parsed['id']))
msg_d.extend([
('lon', profile['lon']),
('lat', profile['lat']),
('loc', profile['loc']),
('id', data_parsed['id']),
])
msg = gs_query.create_gamespy_message(msg_d)
self.log(logging.DEBUG, "SENDING: %s" % msg)
@@ -356,10 +353,10 @@ class PlayerSession(LineReceiver):
def perform_ka(self, data_parsed):
self.keepalive = int(time.time())
msg_d = []
msg_d.append(('__cmd__', "ka"))
msg_d.append(('__cmd_val__', ""))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "ka"),
('__cmd_val__', ""),
])
self.transport.write(msg)
@@ -371,10 +368,11 @@ class PlayerSession(LineReceiver):
self.statstring = data_parsed['statstring']
self.locstring = data_parsed['locstring']
# fields = []
# #fields.append(("status", self.status))
# fields.append(("stat", self.statstring))
# fields.append(("loc", self.locstring))
# fields = [
# #("status", self.status),
# ("stat", self.statstring),
# ("loc", self.locstring),
# ]
#
# for f in fields:
# self.db.update_profile(self.sesskey, f)
@@ -389,7 +387,7 @@ class PlayerSession(LineReceiver):
def perform_bm(self, data_parsed):
if data_parsed['__cmd_val__'] == "1" or data_parsed['__cmd_val__'] == "5" or data_parsed['__cmd_val__'] == "102" or data_parsed['__cmd_val__'] == "103": # Message to/from clients?
if data_parsed['__cmd_val__'] in ("1", "5", "102", "103"): # Message to/from clients?
if "t" in data_parsed:
# Send message to the profile id in "t"
dest_profileid = int(data_parsed['t'])
@@ -413,23 +411,23 @@ class PlayerSession(LineReceiver):
# Send error to user if they tried to send a message to someone who isn't a buddy.
if not_buddies:
msg_d = []
msg_d.append(('__cmd__', "error"))
msg_d.append(('__cmd_val__', ""))
msg_d.append(('err', 2305))
msg_d.append(('errmsg', "The profile the message was to be sent to is not a buddy."))
msg_d.append(('id', 1))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "error"),
('__cmd_val__', ""),
('err', 2305),
('errmsg', "The profile the message was to be sent to is not a buddy."),
('id', 1),
])
logger.log(logging.DEBUG, "Trying to send message to someone who isn't a buddy: %s" % msg)
self.transport.write(msg)
return
msg_d = []
msg_d.append(('__cmd__', "bm"))
msg_d.append(('__cmd_val__', "1"))
msg_d.append(('f', self.profileid))
msg_d.append(('msg', dest_msg))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "1"),
('f', self.profileid),
('msg', dest_msg),
])
if dest_profileid in self.sessions:
self.log(logging.DEBUG, "SENDING TO %s:%s: %s" % (self.sessions[dest_profileid].address.host, self.sessions[dest_profileid].address.port, msg))
@@ -439,13 +437,13 @@ class PlayerSession(LineReceiver):
self.log(logging.DEBUG, "Saving message to %d: %s" % (dest_profileid, msg))
self.db.save_pending_message(self.profileid, dest_profileid, msg)
else:
msg_d = []
msg_d.append(('__cmd__', "error"))
msg_d.append(('__cmd_val__', ""))
msg_d.append(('err', 2307))
msg_d.append(('errmsg', "The buddy to send a message to is offline."))
msg_d.append(('id', 1))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "error"),
('__cmd_val__', ""),
('err', 2307),
('errmsg', "The buddy to send a message to is offline."),
('id', 1),
])
logger.log(logging.DEBUG, "Trying to send message to someone who isn't online: %s" % msg)
self.transport.write(msg)
@@ -494,12 +492,12 @@ class PlayerSession(LineReceiver):
else:
status_msg = "|s|%s|ss|%s|ls|%s|ip|%d|p|0|qm|0" % (self.status, self.statstring, self.locstring, self.get_ip_as_int(self.address.host))
msg_d = []
msg_d.append(('__cmd__', "bm"))
msg_d.append(('__cmd_val__', "100"))
msg_d.append(('f', self.profileid))
msg_d.append(('msg', status_msg))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "100"),
('f', self.profileid),
('msg', status_msg),
])
for buddy in self.buddies:
if buddy['buddyProfileId'] in self.sessions:
@@ -519,12 +517,12 @@ class PlayerSession(LineReceiver):
else:
status_msg = "|s|0|ss|Offline"
msg_d = []
msg_d.append(('__cmd__', "bm"))
msg_d.append(('__cmd_val__', "100"))
msg_d.append(('f', buddy['buddyProfileId']))
msg_d.append(('msg', status_msg))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "100"),
('f', buddy['buddyProfileId']),
('msg', status_msg),
])
self.transport.write(bytes(msg))
@@ -532,12 +530,12 @@ class PlayerSession(LineReceiver):
buddies = self.db.buddy_need_auth_message(self.profileid)
for buddy in buddies:
msg_d = []
msg_d.append(('__cmd__', "bm"))
msg_d.append(('__cmd_val__', "1"))
msg_d.append(('f', buddy['userProfileId']))
msg_d.append(('msg', "I have authorized your request to add me to your list"))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "1"),
('f', buddy['userProfileId']),
('msg', "I have authorized your request to add me to your list"),
])
self.transport.write(bytes(msg))
self.db.buddy_sent_auth_message(buddy['userProfileId'], buddy['buddyProfileId'])
@@ -557,13 +555,13 @@ class PlayerSession(LineReceiver):
if senttime == None:
senttime = int(time.time())
msg_d = []
msg_d.append(('__cmd__', "bm"))
msg_d.append(('__cmd_val__', "2"))
msg_d.append(('f', profileid))
msg_d.append(('date', senttime))
msg_d.append(('msg', msg))
msg = gs_query.create_gamespy_message(msg_d)
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "2"),
('f', profileid),
('date', senttime),
('msg', msg),
])
session.transport.write(bytes(msg))

View File

@@ -328,7 +328,7 @@ class Session(LineReceiver):
for _server in self.server_list:
server = _server
if len(server) > 0 and len(fields) > 0 and 'requested' in server and server['requested'] == {}:
if server and fields and 'requested' in server and server['requested'] == {}:
# If the requested fields weren't found then don't return a server.
# This fixes a bug with Mario Kart DS.
#print "Requested was empty"

View File

@@ -29,7 +29,7 @@ class StatsPage(resource.Resource):
if server_list != None:
for game in server_list:
if len(server_list[game]) == 0:
if not server_list[game]:
continue
output += "<tr>"

View File

@@ -93,13 +93,11 @@ class NasHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
authtoken = self.server.db.generate_authtoken(post["userid"], post)
if 'svc' in post:
if post["svc"] == "9000" or post["svc"] == "9001": # DLC host = 9000
if post["svc"] in ("9000", "9001"): # DLC host = 9000
ret["svchost"] = self.headers['host'] # in case the client's DNS isn't redirecting dls1.nintendowifi.net
# Brawl has 2 host headers which Apache chokes on, so only return the first one or else it won't work
cindex = ret["svchost"].find(',')
if cindex != -1:
ret["svchost"] = ret["svchost"][:cindex]
ret["svchost"] = ret["svchost"].split(',')[0]
if post["svc"] == 9000:
ret["token"] = authtoken
@@ -257,17 +255,8 @@ class NasHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def filter_list_random_files(self, data, count):
# Get [count] random files from the filelist
lines = data.splitlines()
samples = random.sample(lines, count)
output = ''
for sample in samples:
output += sample + '\r\n'
if output == '':
output = '\r\n'
return output
samples = random.sample(data.splitlines(), count)
return '\r\n'.join(samples) + '\r\n'
def filter_list(self, data, attr1 = None, attr2 = None, attr3 = None):
if attr1 == None and attr2 == None and attr3 == None:
@@ -275,7 +264,7 @@ class NasHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
return data
# Filter the list based on the attribute fields
output = ""
output = []
for line in data.splitlines():
s = line.split('\t')
@@ -301,22 +290,13 @@ class NasHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
matched = False
if matched == True:
output += line + '\r\n'
output.append(line)
if output == '':
# if nothing matches, at least return a newline; Pokemon BW at least expects this and will error without it
output = '\r\n'
return output
# if nothing matches, at least return a newline; Pokemon BW at least expects this and will error without it
return '\r\n'.join(output) + '\r\n'
def get_file_count(self, data):
file_count = 0
for line in data.splitlines():
if line:
file_count += 1
return file_count
return sum(1 for line in data.splitlines() if line)
if __name__ == "__main__":
nas = NasServer()

View File

@@ -1,14 +1,18 @@
import random
import logging
import random
import string
def generate_random_str(len, set = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"):
return ''.join(random.choice(set) for _ in range(len))
def generate_random_str_from_set(ln, chs):
return ''.join(random.choice(chs) for _ in range(ln))
def generate_random_number_str(len):
return ''.join(random.choice("1234567890") for _ in range(len))
def generate_random_str(ln, chs=""):
return generate_random_str_from_set(ln, chs or (string.ascii_letters + string.digits))
def generate_random_hex_str(len):
return ''.join(random.choice("1234567890abcdef") for _ in range(len))
def generate_random_number_str(ln):
return generate_random_str_from_set(ln, string.digits)
def generate_random_hex_str(ln):
return generate_random_str_from_set(ln, string.hexdigits.lower())
# Code: Tetris DS @ 020573F4
def calculate_crc8(input):
@@ -185,4 +189,4 @@ def pretty_print_hex(orig_data, cols = 16):
output += "%c" % data[i * cols + x]
output += "\n"
return output
return output

View File

@@ -96,10 +96,7 @@ class StorageHTTPServer(BaseHTTPServer.HTTPServer):
for t in tabledata:
cursor.execute("PRAGMA table_info(%s)" % t[0]) # yeah I know but parameters don't work in pragmas, and inserting table names like that should be safe
columns = cursor.fetchall()
columndata = []
for c in columns:
columndata.append(c[1])
self.tables[t[0]] = columndata
self.tables[t[0]] = [c[1] for c in columns]
self.db.commit()
@@ -127,7 +124,7 @@ class StorageHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
for c in columndata:
colname = c.firstChild.data.replace('.', '___') # fake the attributes that the actual sake databases have
if not colname in self.server.tables[table]:
if colname not in self.server.tables[table]:
raise IllegalColumnAccessException("Unknown column access '%s' in table '%s'" % (colname, table))
columns.append(colname)
@@ -176,9 +173,7 @@ class StorageHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# build SELECT statement, yes I know one shouldn't do this but I cross-checked the table name and all the columns above so it should be fine
statement = 'SELECT '
statement += columns[0]
for c in columns[1:]:
statement += ',' + c
statement += ",".join(columns)
statement += ' FROM ' + table
if shortaction == 'SearchForRecords':
@@ -186,9 +181,8 @@ class StorageHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
owneriddata = data.getElementsByTagName('ns1:ownerids')
if owneriddata and owneriddata[0] and owneriddata[0].firstChild:
oids = owneriddata[0].getElementsByTagName('ns1:int')
statement += ' WHERE ownerid = ' + str(int(oids[0].firstChild.data))
for oid in oids[1:]:
statement += ' OR ownerid = ' + str(int(oid.firstChild.data))
statement += ' WHERE '
statement += ' OR '.join('ownerid = '+str(int(oid.firstChild.data)) for oid in oids)
elif shortaction == 'GetMyRecords':
profileid = self.server.gamespydb.get_profileid_from_loginticket(loginticket)
@@ -198,11 +192,8 @@ class StorageHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
recordids = data.getElementsByTagName('ns1:recordids')[0].getElementsByTagName('ns1:int')
# limit to requested records
id = int(recordids[0].firstChild.data)
statement += ' WHERE recordid = ' + str(id)
for r in recordids[1:]:
id = int(r.firstChild.data)
statement += ' OR recordid = ' + str(id)
statement += ' WHERE '
statement += ' OR '.join('recordid = '+str(int(r.firstChild.data)) for r in recordids)
# if only a subset of the data is wanted
limit_offset_data = data.getElementsByTagName('ns1:offset')
@@ -259,11 +250,10 @@ class StorageHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
profileid = self.server.gamespydb.get_profileid_from_loginticket(loginticket)
columndata = []
values = data.getElementsByTagName('ns1:values')[0]
recordfields = values.getElementsByTagName('ns1:RecordField')
for rf in recordfields:
columndata.append( rf.getElementsByTagName('ns1:name')[0] )
columndata = [rf.getElementsByTagName('ns1:name')[0]
for rf in recordfields]
try:
columns = self.confirm_columns(columndata, table)
@@ -285,20 +275,16 @@ class StorageHTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
if shortaction == 'UpdateRecord':
statement = 'UPDATE ' + table + ' SET '
statement += columns[0] + ' = ?'
for c in columns[1:]:
statement += ', ' + c + ' = ?'
statement += ', '.join(c+' = ?' for c in columns)
statement += ' WHERE recordid = ? AND ownerid = ?'
rowdata.append( recordid )
rowdata.append( profileid )
elif shortaction == 'CreateRecord':
statement = 'INSERT INTO ' + table + ' ('
for c in columns:
statement += c + ', '
statement += 'ownerid) VALUES ('
for i in xrange(len(columns)):
statement += '?, '
statement += ', '.join(columns)
statement += ', ownerid) VALUES ('
statement += '?, '*len(columns)
statement += '?)'
rowdata.append( profileid )
else: