File: UrlHandler.py

package info (click to toggle)
mbot 0.3-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 212 kB
  • ctags: 119
  • sloc: python: 595; makefile: 102; php: 98; sql: 59
file content (71 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

# mbot - a mail handling robot
#
# Author:  Dimitri Fontaine <dim@tapoueh.org>
#
# This code is licensed under the GPL.
# Get yourself a version here : http://www.gnu.org/copyleft/gpl.html

# $Id: UrlHandler.py,v 1.12 2003/12/22 11:04:47 dim Exp $

import MailHandler
import string, urllib, mimetools, urlparse

from MailHandler import MailHandler

class UrlHandler(MailHandler):
    "Handle getting url given in mail"
    
    def read_conf(self, ConfObj):
        ''' Getting config options for this handler '''
        self.log.notice("[UrlHandler]: read_conf")
        MailHandler.read_conf(self, ConfObj,
	                      ['mailsize', 'attsize'])

    def handle(self, body):
        """ The body may contain one url per line """
        result    = []
        glob_size = 0

        self.log.notice("[UrlHandler]")
        for line in body.split():
            if glob_size < self.mailsize:
                if line != '' and line is not None:
                    (p, server, path, params, q, f) = urlparse.urlparse(line)
                    url   = urlparse.urlunparse((p, server,
                                                 urllib.quote(path),
                                                 urllib.quote(params),
                                                 urllib.quote(q, '/='),
                                                 urllib.quote(f)))
                    self.log.debug("[UrlHandler]: url='%s'" % url)

                    # Now we get the source and read infos
                    src  = urllib.urlopen(url)
                    size = int(src.info().getheader("Content-Length"))
                    self.log.debug("[UrlHandler]: size='%d'" % size)
                        
                    if size < self.attsize:
                        result.append((src.info().gettype(), src.read()))
                        glob_size += size
                                
                        self.log.debug(
                            "[UrlHandler]: glob_size='%d'" % glob_size)
                        self.log.notice(
                            "[UrlHandler]: Retreiving url '%s'" % url)
                    else:
                        type = "text/plain"
                        data = "Attachment size exceed %s (%d)" % (self.attsize,
                                                                   size) + \
                               " for '%s'" % url
                        self.log.notice("[UrlHandler]: %s" % data)
                        result.append((type, data))
            else:
                type = "text/plain"
                data = "Mail size exceed %s (%d)" % (self.mailsize,
                                                     glob_size)
                self.log.notice("[UrlHandler]: %s" % data)
                result.append((type, data))

        return result