~emersion/python-emailthreads

ref: 0a56af7fd6de16105c27b7c149eeb0282e95e587 python-emailthreads/emailthreads/quotes.py -rw-r--r-- 5.3 KiB View raw
                                                                                
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import re
import sys
from email.message import EmailMessage

from util import *

def trim_empty_lines(block):
	start = 0
	for (i, l) in enumerate(block):
		if l != "":
			break
		start = i + 1
	block = block[start:]

	end = len(block)
	for (i, l) in enumerate(reversed(block)):
		if l != "":
			break
		end = len(block) - i - 1
	block = block[:end]

	return block

class Text:
	def __init__(self, region, lines=[]):
		self.region = region
		self.lines = lines_as_list(lines)

	def __repr__(self):
		return "[text " + "\n".join(self.lines) + "]"

	def __eq__(self, other):
		return self.region == other.region and self.lines == other.lines

class Quote:
	def __init__(self, region, lines, parent_region=None):
		self.region = region
		self.lines = lines_as_list(lines)
		self.parent_region = parent_region

	def __repr__(self):
		s = "["
		if self.parent_region is not None:
			s += "quote at " + str(self.parent_region[0]) + "-" + str(self.parent_region[1] - 1)
		else:
			s += "unknown quote"

		return s + " " + "\n".join(self.lines) + "]"

	def __eq__(self, other):
		return self.region == other.region and self.lines == other.lines and self.parent_region == other.parent_region

def parse_blocks(msg):
	text = get_text(msg)
	text_lines = text.splitlines()

	blocks = []

	block_start = 0
	block_lines = []
	was_quoted = False
	for (i, line) in enumerate(text_lines):
		line = line.strip()

		line_quoted = line.startswith(">")
		if line_quoted:
			line = line[1:].lstrip()

		if line_quoted != was_quoted and block_lines != []:
			reg = (block_start, i)
			if was_quoted:
				blocks.append(Quote(reg, block_lines))
			else:
				blocks.append(Text(reg, block_lines))
			block_start = i
			block_lines = []

		block_lines.append(line)
		was_quoted = line_quoted

	if block_lines != []:
		reg = (block_start, len(text_lines))
		if was_quoted:
			blocks.append(Quote(reg, block_lines))
		else:
			blocks.append(Text(reg, block_lines))

	return blocks

def match_block_at(ref_block, block, index):
	start = max(0, index - len(block))
	ref_block = list(ref_block[start:index])

	n_lines = 0
	for line in reversed(block):
		if ref_block == []:
			break

		ref_line = ref_block[-1]

		# TODO: match level of quotes
		line = line.lstrip("> ").strip()
		ref_line = ref_line.lstrip("> ").strip()

		if line == ref_line:
			ref_block = ref_block[:-1]
			n_lines += 1
		elif ref_line.endswith(line):
			ref_block[-1] = ref_line[:-len(line)].strip()
		else:
			break

	return n_lines

def find_block(ref_block, block, start=0):
	# TODO: optimize this
	regions = []
	for i in range(start, len(ref_block)):
		match_len = match_block_at(ref_block, block, i)
		# TODO: require to match at least a % of the block
		if match_len > 0:
			regions.append((i - match_len, i))

	return regions

def match_quotes(blocks, in_reply_to):
	in_reply_to_text = get_text(in_reply_to)
	in_reply_to_lines = [l.strip() for l in in_reply_to_text.splitlines()]

	last_quote_index = 0
	for block in blocks:
		block.lines = trim_empty_lines(block.lines)

		if isinstance(block, Quote):
			regions = find_block(in_reply_to_lines, block.lines, last_quote_index)
			regions = sorted(regions, key=lambda reg: reg[1] - reg[0], reverse=True)
			if regions == []:
				# Quote that isn't in the In-Reply-To message
				pass
			else:
				if len(regions) > 1:
					# TODO: ranking
					print("Warning: multiple matches at " + str(regions))
				parent_region = regions[0]
				block.parent_region = parent_region
				last_quote_index = parent_region[0]

	return blocks

def trim_noisy_text(blocks):
	# Trim snips
	# TODO: more of these
	snips = ["...", "…", "[...]", "[snip]", "snip"]
	for block in blocks:
		if isinstance(block, Text) and len(block.lines) == 1 and block.lines[0] in snips:
			blocks.remove(block)

	if len(blocks) >= 2:
		# Trim "On …, … wrote:" headers
		# TODO: support more variations of these
		# TODO: support text comments before this header
		first = blocks[0]
		second = blocks[1]
		if isinstance(first, Text) and first.lines != [] and isinstance(second, Quote):
			if len(first.lines) <= 2 and first.lines[0].startswith("On ") and first.lines[-1].rstrip(" :").endswith(" wrote"):
				blocks = blocks[1:]

	return blocks

def trim_quotes_footer(blocks):
	# TODO: make footers configurable
	# TODO: only trim for the last quotes
	for block in blocks:
		if isinstance(block, Quote):
			# Trim retarded mailing list footers
			try:
				i = block.lines.index("_______________________________________________")
				if i >= 0:
					block.lines = block.lines[:i]
				# TODO: cleanup empty quotes
			except ValueError:
				pass
		else:
			# Trim mailing list scrubbing attachments
			try:
				i = block.lines.index("-------------- next part --------------")
				if i >= 0:
					block.lines = block.lines[:i]
				# TODO: cleanup empty quotes
			except ValueError:
				pass

	return blocks

def quote_to_text(quote):
	lines = ["> " + l for l in quote.lines]
	return Text(quote.region, lines)

def merge_blocks(blocks):
	merged = []

	last_block = None
	for block in blocks:
		merge = False
		if isinstance(block, Text):
			merge = isinstance(last_block, Text)
		elif isinstance(block, Quote):
			if block.parent_region is None:
				# Unknown quote
				merge = isinstance(last_block, Text)
				block = quote_to_text(block)

		if merge:
			last_block.lines += block.lines
		elif block.lines != []:
			merged.append(block)
			last_block = block

	return merged