root/python/utility/mutt_save_attachment.py

Revision 4:1190bee97fec, 2.2 kB (checked in by pbx, 1 year ago)

[svn] initial import

  • Property exe set to *
Line 
1 #!/usr/bin/env python
2 """
3 Attachment-saver for mutt
4
5 Saves attached files in world-readable form and displays a URL for clicking.
6 Once you've used your web browser to download or view the file, you can delete
7 it or make it private.
8
9 My .mailcap file looks something like this:
10
11     text/html; links -dump %s; copiousoutput;
12     application/*; mutt_save_attachment.py %s
13     image/*; mutt_save_attachment.py %s
14
15 This script technically creates a race condition -- if somebody knows
16 the MUTT_TEMP_WWW_URL where files are being saved, and knows filenames
17 to probe for, and probes between the time you invoke this script and
18 the time you choose 'delete' or 'private', they can see files. If that
19 is a concern, add webserver-based auth to MUTT_TEMP_WWW_DIR. (Via
20 `htpasswd` when using Apache, for example.)
21 """
22
23 __author__ = "Paul Bissex <pb@e-scribe.com>"
24 __license__ = "MIT"
25
26 import sys
27 import shutil
28 import os
29 from cgi import escape
30
31 def rename(filepath):
32     """Prompt user to rename the file"""
33     newpath = raw_input("New path: ")
34     if newpath:
35         os.rename(filepath, newpath)
36     else:
37         print "Rename cancelled"
38
39 def delete(filepath):
40     """Remove the file"""
41     os.remove(filepath)
42
43 def private(filepath):
44     """Change file's permissions to owner-read-write only"""
45     os.chmod(filepath, 0600)
46
47 def showit(filepath, www_dir, www_url):
48     """Make file at filepath publicly readable in www_dir via www_url"""
49     name = os.path.basename(filepath)
50     dest = os.path.join(www_dir, name)
51     # Note: does not check if file exists at dest!
52     shutil.copyfile(filepath, dest)
53     os.chmod(dest, 0644)
54     print os.path.join(www_url, escape(name))
55     action = raw_input("(d)elete, make (p)rivate, (r)ename, return to continue: ")
56     cmd_map = { 'd': delete, 'r': rename, 'p': private }
57
58     if action:
59         cmd = action.lower()[0]
60         if cmd in cmd_map:
61             cmd_map[cmd](filepath)
62
63
64 if __name__ == "__main__":
65     try:
66         www_dir = os.environ['MUTT_TEMP_WWW_DIR']
67         www_url = os.environ['MUTT_TEMP_WWW_URL']
68     except KeyError:
69         print "You must set $MUTT_TEMP_WWW_DIR and $MUTT_TEMP_WWW_URL."
70     filepath = sys.argv[1]
71     showit(filepath, www_dir, www_url)
72    
73
Note: See TracBrowser for help on using the browser.