summaryrefslogtreecommitdiff
blob: fa0b5c9fcca23b7169127598e85deeea5e8670e5 (plain)
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
94
95
96
--- lib/pyzor/client.py	Sun Sep  8 22:37:15 2002
+++ lib/pyzor/client.py	Wed Aug  3 10:58:03 2005
@@ -466,7 +470,7 @@
 
         (fp, offsets) = self.get_line_offsets(fp)
         
-        # did we get an empty file?
+	# did we get an empty (parsed output)file?
         if len(offsets) == 0:
             return
 
@@ -662,39 +666,66 @@
         self.multifile = None
         self.curfile   = None
 
+	# Check if we got a mail or not. Set type to binary if there is no 'From:' header and
+	# type text/plain with encoding 7bit. 7bit is passed trough anyway so nobody cares.
+	if (not msg.has_key("From") and self.type == 'text' and msg.subtype == 'plain' and msg.getencoding() == '7bit'):
+		self.type = 'binary';
+
         if self.type == 'text':
             encoding = msg.getencoding()
-            if encoding == '7bit':
-                self.curfile = msg.fp
-            else:
-                self.curfile = tempfile.TemporaryFile()
-                mimetools.decode(msg.fp, self.curfile, encoding)
-                self.curfile.seek(0)
-                
+            self.curfile = msg.fp
+            if encoding != '7bit':
+                # fix bad encoding name
+                if encoding == '8bits':
+                    encoding = '8bit'
+                try:
+                    newcurfile = tempfile.TemporaryFile()
+                    mimetools.decode(msg.fp, newcurfile, encoding)
+                    newcurfile.seek(0)
+                    self.curfile = newcurfile
+                except:
+                    # ignore encoding on errors, pass msg as is
+                    pass
+
         elif self.type == 'multipart':
             import multifile
             self.multifile = multifile.MultiFile(msg.fp, seekable=False)
             self.multifile.push(msg.getparam('boundary'))
-            self.multifile.next()
-            self.curfile = self.__class__(self.multifile)
-
+	    try:
+		self.multifile.next()
+		self.curfile = self.__class__(self.multifile)
+	    except:
+	    	#
+	    	# Catch multipart decoding errors
+	    	#
+		fp.seek(0)
+		self.curfile = fp
+		self.type = 'binary'
 
         if self.type == 'text' or self.type == 'multipart':
             assert self.curfile is not None
+        elif self.type == 'binary':
+	    try:
+	 	fp.seek(0)
+	    except:
+	    	pass
+	    self.curfile = fp
         else:
             assert self.curfile is None
 
         
     def readline(self):
         l = ''
-        if self.type in ('text', 'multipart'):
-            l = self.curfile.readline()
-
-        if self.type == 'multipart' and not l and self.multifile.next():
-            self.curfile = self.__class__(self.multifile)
-            # recursion.  Could get messy if
-            # we get a bunch of empty multifile parts
-            l = self.readline()
+	try:
+		if self.type in ('text', 'multipart', 'binary'):
+		    l = self.curfile.readline()
+		if self.type == 'multipart' and not l and self.multifile.next():
+		    self.curfile = self.__class__(self.multifile)
+		    # recursion.  Could get messy if
+		    # we get a bunch of empty multifile parts
+		    l = self.readline()
+	except (TypeError, multifile.Error):
+		pass
         return l