summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/mat.py2
-rw-r--r--lib/misc.py74
-rw-r--r--test/clean.torrentbin0 -> 54609 bytes
-rw-r--r--test/dirty.torrentbin0 -> 54622 bytes
4 files changed, 43 insertions, 33 deletions
diff --git a/lib/mat.py b/lib/mat.py
index 97a71ea..fd13287 100644
--- a/lib/mat.py
+++ b/lib/mat.py
@@ -17,6 +17,7 @@ import images
17import audio 17import audio
18import office 18import office
19import archive 19import archive
20import misc
20 21
21__version__ = '0.1' 22__version__ = '0.1'
22__author__ = 'jvoisin' 23__author__ = 'jvoisin'
@@ -33,6 +34,7 @@ STRIPPERS = {
33 'audio/mpeg': audio.MpegAudioStripper, 34 'audio/mpeg': audio.MpegAudioStripper,
34 'image/jpeg': images.JpegStripper, 35 'image/jpeg': images.JpegStripper,
35 'image/png': images.PngStripper, 36 'image/png': images.PngStripper,
37 'application/x-bittorrent': misc.TorrentStripper,
36 'application/opendocument': office.OpenDocumentStripper, 38 'application/opendocument': office.OpenDocumentStripper,
37 'application/officeopenxml': office.OpenXmlStripper, 39 'application/officeopenxml': office.OpenXmlStripper,
38} 40}
diff --git a/lib/misc.py b/lib/misc.py
index 963800e..c92182a 100644
--- a/lib/misc.py
+++ b/lib/misc.py
@@ -2,36 +2,33 @@
2 Care about misc formats 2 Care about misc formats
3''' 3'''
4 4
5import hachoir_core
6import parser 5import parser
7 6
7import bencode
8
8 9
9class TorrentStripper(parser.GenericParser): 10class TorrentStripper(parser.GenericParser):
10 ''' 11 '''
11 A torrent file looks like: 12 Represent a torrent file with the help
12 -root 13 of the bencode lib from Petru Paler
13 -start
14 -announce
15 -announce-list
16 -comment
17 -created_by
18 -creation_date
19 -encoding
20 -info
21 -end
22 ''' 14 '''
23 def remove_all(self): 15 def __init__(self, filename, parser, mime, backup, add2archive):
24 for field in self.editor['root']: 16 super(TorrentStripper, self).__init__(filename, parser, mime,
25 if self._should_remove(field): 17 backup, add2archive)
26 #FIXME : hachoir does not support torrent metadata editing :< 18 self.fields = ['comment', 'creation date', 'created by']
27 del self.editor['/root/' + field.name]
28 hachoir_core.field.writeIntoFile(self.editor, self.output)
29 self.do_backup()
30 19
31 def is_clean(self): 20 def is_clean(self):
32 for field in self.editor['root']: 21 '''
33 if self._should_remove(field): 22 Check if the file is clean from harmful metadatas
34 return False 23 '''
24 with open(self.filename, 'r') as f:
25 decoded = bencode.bdecode(f.read())
26 for key in self.fields:
27 try:
28 if decoded[key] != '':
29 return False
30 except:
31 pass
35 return True 32 return True
36 33
37 def get_meta(self): 34 def get_meta(self):
@@ -39,16 +36,27 @@ class TorrentStripper(parser.GenericParser):
39 Return a dict with all the meta of the file 36 Return a dict with all the meta of the file
40 ''' 37 '''
41 metadata = {} 38 metadata = {}
42 for field in self.editor['root']: 39 with open(self.filename, 'r') as f:
43 if self._should_remove(field): 40 decoded = bencode.bdecode(f.read())
44 try: # FIXME 41 for key in self.fields:
45 metadata[field.name] = field.value 42 try:
46 except: 43 if decoded[key] != '':
47 metadata[field.name] = 'harmful content' 44 metadata[key] = decoded[key]
45 except:
46 pass
48 return metadata 47 return metadata
49 48
50 def _should_remove(self, field): 49 def remove_all(self):
51 if field.name in ('comment', 'created_by', 'creation_date', 'info'): 50 '''
52 return True 51 Remove all the files that are compromizing
53 else: 52 '''
54 return False 53 with open(self.filename, 'r') as f:
54 decoded = bencode.bdecode(f.read())
55 for key in self.fields:
56 try:
57 decoded[key] = ''
58 except:
59 pass
60 with open(self.output, 'w') as f: # encode the decoded torrent
61 f.write(bencode.bencode(decoded)) # and write it in self.output
62 self.do_backup()
diff --git a/test/clean.torrent b/test/clean.torrent
new file mode 100644
index 0000000..e6324f7
--- /dev/null
+++ b/test/clean.torrent
Binary files differ
diff --git a/test/dirty.torrent b/test/dirty.torrent
new file mode 100644
index 0000000..bd056a0
--- /dev/null
+++ b/test/dirty.torrent
Binary files differ