From b102b8c5b89e0996095f4bc390384dbd8ae71cee Mon Sep 17 00:00:00 2001 From: Jonathan Harris <jonathan@marginal.org.uk> Date: Sun, 18 Mar 2018 11:48:04 +0000 Subject: [PATCH] Sanitise ship loadout file names Fixes #303 --- companion.py | 12 ++++++++++++ edshipyard.py | 2 +- loadout.py | 2 +- monitor.py | 4 ++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/companion.py b/companion.py index 13a574bb..71eefc2a 100644 --- a/companion.py +++ b/companion.py @@ -373,3 +373,15 @@ def ship(data): # subset of "ship" that's not noisy return filter_ship(data['ship']) + + +# Ship name suitable for writing to a file +def ship_file_name(ship_name, ship_type): + name = unicode(ship_name or ship_map.get(ship_type.lower(), ship_type)).strip() + if name.endswith('.'): + name = name[:-1] + if name.lower() in ['con', 'prn', 'aux', 'nul', + 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', + 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9']: + name = name + '_' + return name.translate({ ord(x): u'_' for x in ['\0', '<', '>', ':', '"', '/', '\\', '|', '?', '*'] }) diff --git a/edshipyard.py b/edshipyard.py index 31a1b5b9..30f5dc73 100644 --- a/edshipyard.py +++ b/edshipyard.py @@ -148,7 +148,7 @@ def export(data, filename=None): return # Look for last ship of this type - ship = companion.ship_map.get(data['ship']['name'].lower(), data['ship']['name']) # Use in-game name + ship = companion.ship_file_name(data['ship'].get('shipName'), data['ship']['name']) regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt') oldfiles = sorted([x for x in os.listdir(config.get('outdir')) if regexp.match(x)]) if oldfiles: diff --git a/loadout.py b/loadout.py index 872f6f1f..d429e8dc 100644 --- a/loadout.py +++ b/loadout.py @@ -19,7 +19,7 @@ def export(data, filename=None): return # Look for last ship of this type - ship = companion.ship_map.get(data['ship']['name'].lower(), data['ship']['name']) # Use in-game name + ship = companion.ship_file_name(data['ship'].get('shipName'), data['ship']['name']) regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt') oldfiles = sorted([x for x in os.listdir(config.get('outdir')) if regexp.match(x)]) if oldfiles: diff --git a/monitor.py b/monitor.py index 21510dd7..ced0624d 100644 --- a/monitor.py +++ b/monitor.py @@ -13,7 +13,7 @@ if __debug__: from traceback import print_exc from config import config -from companion import ship_map +from companion import ship_file_name if platform=='darwin': @@ -735,7 +735,7 @@ class EDLogs(FileSystemEventHandler): h.write(string) return - ship = self.state['ShipName'] or ship_map.get(self.state['ShipType'], self.state['ShipType']) + ship = ship_file_name(self.state['ShipName'], self.state['ShipType']) regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt') oldfiles = sorted([x for x in listdir(config.get('outdir')) if regexp.match(x)]) if oldfiles: