1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.transport.mailets;
21
22 import org.apache.mailet.base.FlowedMessageUtils;
23 import org.apache.mailet.base.GenericMailet;
24 import org.apache.mailet.Mail;
25 import org.apache.mailet.MailetException;
26
27 import javax.mail.MessagingException;
28
29 import java.io.IOException;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class UnwrapText extends GenericMailet {
56 public final static String PARAMETER_NAME_QUOTEWIDTH = "quotewidth";
57
58 private int quotewidth;
59
60
61
62
63
64
65 public String getMailetInfo() {
66 return "UnwrapText";
67 }
68
69 public void init() throws MailetException {
70 quotewidth = Integer.parseInt(getInitParameter(PARAMETER_NAME_QUOTEWIDTH, "-10"));
71 }
72
73 public void service(Mail mail) throws MailetException {
74 try {
75
76
77 if (FlowedMessageUtils.isFlowedTextMessage(mail.getMessage()))
78 FlowedMessageUtils.deflowMessage(mail.getMessage());
79
80 else {
81 Object o = mail.getMessage().getContent();
82 if (o instanceof String) {
83 String unwrapped = unwrap((String) o, quotewidth);
84 mail.getMessage().setContent(unwrapped, mail.getMessage().getContentType());
85 mail.getMessage().saveChanges();
86 }
87 }
88
89 } catch (MessagingException e) {
90 throw new MailetException("Could not unwrap message", e);
91
92 } catch (IOException e) {
93 throw new MailetException("Could not unwrap message", e);
94 }
95
96 }
97
98 public static String unwrap(String text) {
99 return unwrap(text, - 10);
100 }
101
102 public static String unwrap(String text, int qwidth) {
103 String[] lines = text.split("\r\n|\n", -1);
104
105
106 Pattern p1 = Pattern.compile("([> ]*)(.*[^ .?!][ ]*)$", 0);
107
108
109 Pattern p2 = Pattern.compile("^([> ]*)(([a-z\u00E0\u00E8\u00E9\u00EC\u00F2\u00F9][^ \t\r\n]*|[0-9][0-9,.]*)([ \t].*$|$))", 0);
110
111
112 int width = 0;
113 for (int i = 0; i < lines.length - 1; i++) {
114 String l = lines[i].trim();
115 if (l.length() > width) width = l.length();
116 }
117
118 if (width < 40) return text;
119 if (qwidth < 0) qwidth = width - qwidth;
120
121 StringBuffer result = new StringBuffer();
122 int prevWrapped = 0;
123 for (int i = 0; i < lines.length; i++) {
124 if (prevWrapped != 0) {
125 if (prevWrapped > 0 ) {
126 if (result.charAt(result.length() - 1) != ' ') result.append(" ");
127 }
128 else result.append("\r\n");
129 }
130 String l = lines[i];
131 Matcher m1 = p1.matcher(l);
132 Matcher m2 = i < lines.length - 1 ? p2.matcher(lines[i + 1]) : null;
133 boolean b;
134 int w;
135
136 if (m1.matches() && i < lines.length - 1 && m2.matches() && (
137
138 ((b = m1.group(1).trim().equals(m2.group(1).trim())) && l.length() + m2.group(3).length() + 1 > width)
139 ||
140
141 (!b && m2.group(1).trim().equals("") && (w = l.length() + m2.group(2).trim().length() + 1) > width && w <= qwidth)
142 )) {
143
144 if (b) {
145 if (prevWrapped > 0 && m1.groupCount() >= 2) result.append(m1.group(2));
146 else result.append(l);
147 prevWrapped = 1;
148
149 } else {
150 lines[i + 1] = l + (l.charAt(l.length() - 1) != ' ' ? " " : "") + m2.group(2).trim();
151
152 if (prevWrapped != 0) {
153 if (prevWrapped > 0) result.deleteCharAt(result.length() - 1);
154 else result.delete(result.length() - 2, result.length());
155 }
156 }
157
158 } else {
159 Matcher m3 = p2.matcher(l);
160 if (prevWrapped > 0 && m3.matches()) result.append(m3.group(2));
161 else result.append(lines[i]);
162 prevWrapped = -1;
163 }
164 }
165
166 return result.toString();
167 }
168
169 }