1.1 --- a/scripts/getfiles.py Sun Jan 12 18:39:15 2014 +0100
1.2 +++ b/scripts/getfiles.py Sun Jan 12 19:39:47 2014 +0100
1.3 @@ -28,6 +28,45 @@
1.4 else:
1.5 return content
1.6
1.7 +def save_part(gpg, directory, part):
1.8 +
1.9 + # The retrieved content may be encrypted.
1.10 +
1.11 + try:
1.12 + part = decrypt(gpg, part)
1.13 + except MoinMessageError:
1.14 + print >>sys.stderr, "Part could not be decrypted."
1.15 + return
1.16 +
1.17 + # The original sender may now be verified.
1.18 +
1.19 + if is_signed(part):
1.20 + fingerprint, identity, content = gpg.verifyMessage(part)
1.21 + print >>sys.stderr, "Content signed by %s." % identity
1.22 + else:
1.23 + content = part
1.24 +
1.25 + if content.is_multipart():
1.26 + files = content.get_payload()
1.27 + else:
1.28 + files = [content]
1.29 +
1.30 + # Treat each part of the verified, possibly encrypted message as a file.
1.31 +
1.32 + for n, file in enumerate(files):
1.33 + if file.is_multipart():
1.34 + save_part(gpg, directory, file)
1.35 + else:
1.36 + data = file.get_payload(decode=True)
1.37 +
1.38 + realname = file.get("Content-Disposition", str(n))
1.39 + filename = join(directory, realname)
1.40 +
1.41 + dirpath = join(target_dir, directory)
1.42 + if not exists(dirpath):
1.43 + makedirs(dirpath)
1.44 + writefile(join(target_dir, filename), data)
1.45 +
1.46 if __name__ == "__main__":
1.47 try:
1.48 service = sys.argv[1]
1.49 @@ -87,41 +126,13 @@
1.50 print part
1.51 else:
1.52 print part["Request-Type"]
1.53 - continue
1.54 -
1.55 - # Use the "outer" filename to determine a directory for the retrieved
1.56 - # file, even though the eventual filename in the directory may be
1.57 - # different.
1.58 -
1.59 - timestamp = strftime("%Y-%m-%d_%H:%M:%S")
1.60 - directory = part.get("Content-Disposition", timestamp)
1.61 -
1.62 - # The retrieved content may be encrypted.
1.63 -
1.64 - part = decrypt(gpg, part)
1.65 -
1.66 - # The original sender may now be verified.
1.67 + else:
1.68 + # Use the "outer" filename to determine a directory for the retrieved
1.69 + # file, even though the eventual filename in the directory may be
1.70 + # different.
1.71
1.72 - if is_signed(part):
1.73 - fingerprint, identity, content = gpg.verifyMessage(part)
1.74 - print >>sys.stderr, "Content signed by %s." % identity
1.75 -
1.76 - if content.is_multipart():
1.77 - files = content.get_payload()
1.78 - else:
1.79 - files = [content]
1.80 -
1.81 - # Treat each part of the verified, possibly encrypted message as a file.
1.82 -
1.83 - for file in files:
1.84 - data = file.get_payload(decode=True)
1.85 -
1.86 - realname = file["Content-Disposition"]
1.87 - filename = join(directory, realname)
1.88 -
1.89 - dirpath = join(target_dir, directory)
1.90 - if not exists(dirpath):
1.91 - makedirs(dirpath)
1.92 - writefile(join(target_dir, filename), data)
1.93 + timestamp = strftime("%Y-%m-%d_%H:%M:%S")
1.94 + directory = part.get("Content-Disposition", timestamp)
1.95 + save_part(gpg, directory, part)
1.96
1.97 # vim: tabstop=4 expandtab shiftwidth=4