[go: up one dir, main page]

File: urlscan

package info (click to toggle)
urlscan 0.5.6-0.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 624 kB
  • ctags: 58
  • sloc: python: 448; makefile: 26
file content (93 lines) | stat: -rwxr-xr-x 3,165 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
#
# A simple urlview replacement that handles things like quoted-printable properly.
# aka "urlview minus teh suck"
#
#   Copyright (C) 2006-2007 Daniel Burrows
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation; either version 2 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; see the file COPYING.  If not, write to
#   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.


import os
import sys
import re
import optparse

import email.Parser

optparser = optparse.OptionParser(usage='%prog [options] [<] message')
optparser.add_option('-c', '--compact', action='store_true',
                     dest="compact", default=False,
                     help='Don\'t display the context of each URL.')
optparser.add_option('-b', '--background', action='store_true',
                     dest="background", default=False,
                     help='Run the Web browser as a background process.')

options, args = optparser.parse_args()

homedir = os.path.dirname(sys.argv[0])
moduledir = os.path.join(homedir, 'modules')
if os.path.isdir(moduledir):
    sys.path = [moduledir] + sys.path

from urlscan import urlchoose
from urlscan import urlscan

# Written as a generator so I can easily choose only
# one subpart in the future (e.g., for
# multipart/alternative).  Actually, I might even add
# a browser for the message structure?
def msgurls(msg, urlidx = 1):
    if msg.is_multipart():
        for part in msg.get_payload():
            for chunk in msgurls(part, urlidx):
                urlidx += 1
                yield chunk
    elif msg.get_content_type() == 'text/plain':
        for chunk in urlscan.extracturls(msg.get_payload(decode = True)):
            urlidx += 1
            yield chunk
    elif msg.get_content_type() == 'text/html':
        for chunk in urlscan.extracthtmlurls(msg.get_payload(decode = True)):
            urlidx += 1
            yield chunk

def main(msg):
    global options

    ui = urlchoose.URLChooser(msgurls(msg),
                              compact_mode = options.compact,
                              background = options.background)
    ui.main()

# TODO: accept messages on the command-line?
if len(args) > 1:
    sys.stderr.write('Too many messages.\n')
elif len(args) == 1:
    msg = email.Parser.Parser().parse(file(args[0], 'r'))
    main(msg)
else:
    msg = email.Parser.Parser().parse(sys.stdin)
    if not os.isatty(0):
        fd = os.open('/dev/tty', os.O_RDONLY)
        if fd < 0:
            sys.stderr.write('Unable to open an input tty.\n')
            sys.exit(-1)
        else:
            os.dup2(fd, 0)
            os.close(fd)
    main(msg)