1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 01:42:16 -05:00
Option to strip signature from quoted text in replies.
Strips signature from quoted text in replies (line of /^-- $/ or html
equivalent, and everything below). Per account.
This commit is contained in:
ashley willis 2011-11-14 13:58:01 -08:00 committed by Andrew Chen
parent 1012ad56dd
commit 37f480c969
12 changed files with 178 additions and 4 deletions

View File

@ -6,6 +6,7 @@ LOCAL_STATIC_JAVA_LIBRARIES += libdom
LOCAL_STATIC_JAVA_LIBRARIES += libio
LOCAL_STATIC_JAVA_LIBRARIES += libjutf
LOCAL_STATIC_JAVA_LIBRARIES += libjzlib
LOCAL_STATIC_JAVA_LIBRARIES += libhtmlcleaner
LOCAL_MODULE_TAGS := eng
@ -24,6 +25,7 @@ LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += libdom:libs/apache-mime4j-dom-0.7-SNAPSH
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += libio:libs/commons-io-2.0.1.jar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += libjutf:libs/jutf7-1.0.1-SNAPSHOT.jar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += libjzlib:libs/jzlib-1.0.7.jar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += libhtmlcleaner:libs/htmlcleaner-2.2.jar
include $(BUILD_MULTI_PREBUILT)

35
HTMLCLEANER_LICENSE Normal file
View File

@ -0,0 +1,35 @@
HtmlCleaner is distributed under BSD License. It gives the freedom for
anyone to use, explore, modify, and distribute HtmlCleaner, but without any
warranty.
Copyright (c) 2006-2011, HtmlCleaner team.
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* The name of HtmlCleaner may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -77,6 +77,18 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/libs/htmlcleaner-2.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MODULE_DIR$/libs/htmlcleaner-2.2-all.zip!/HtmlCleaner/src/main/java" />
<root url="jar://$MODULE_DIR$/libs/htmlcleaner-2.2-all.zip!/HtmlCleaner/src/test/java" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library>
<CLASSES>

Binary file not shown.

BIN
libs/htmlcleaner-2.2.jar Normal file

Binary file not shown.

View File

@ -5,6 +5,7 @@
<string name="app_authors">Google, The K-9 Dog Walkers.</string>
<string name="app_copyright_fmt">Copyright 2008-<xliff:g>%s</xliff:g> The K-9 Dog Walkers. Portions Copyright 2006-<xliff:g>%s</xliff:g> the Android Open Source Project.</string>
<string name="app_license">Licensed under the Apache License, Version 2.0.</string>
<string name="app_htmlcleaner_license">&lt;p>HtmlCleaner is distributed under BSD License. It gives the freedom for anyone to use, explore, modify, and distribute HtmlCleaner, but without any warranty.&lt;/p>&lt;p>Copyright (c) 2006-2011, HtmlCleaner team.&lt;br>All rights reserved.&lt;/p>&lt;p>Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:&lt;/p>&lt;p>&lt;ul>&lt;li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.&lt;/li>&lt;li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.&lt;/li>&lt;li>The name of HtmlCleaner may not be used to endorse or promote products derived from this software without specific prior written permission.&lt;/li>&lt;/ul>&lt;/p>&lt;p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;/p></string>
<string name="app_authors_fmt">Authors: <xliff:g id="app_authors">%s</xliff:g></string>
<string name="app_revision_fmt">Revision Information: <xliff:g id="app_revision_url">%s</xliff:g></string>
<string name="app_libraries">We\'re using the following third-party libraries: <xliff:g id="app_libraries_list">%s</xliff:g></string>
@ -565,6 +566,9 @@ Welcome to K-9 Mail setup. K-9 is an open source mail client for Android origin
<string name="account_settings_reply_after_quote_label">Reply after quoted text</string>
<string name="account_settings_reply_after_quote_summary">When replying to messages, the original message will appear above your reply.</string>
<string name="account_settings_strip_signature_label">Strip signature from quoted reply</string>
<string name="account_settings_strip_signature_summary">When replying to messages, the signature of the quoted text will be stripped</string>
<string name="account_settings_message_format_label">Message Format</string>
<string name="account_settings_message_format_text">Plain Text (images and formatting will be removed)</string>
<string name="account_settings_message_format_html">HTML (images and formatting are preserved)</string>

View File

@ -259,6 +259,13 @@
android:defaultValue="false"
android:summary="@string/account_settings_reply_after_quote_summary" />
<CheckBoxPreference
android:persistent="false"
android:key="strip_signature"
android:title="@string/account_settings_strip_signature_label"
android:defaultValue="true"
android:summary="@string/account_settings_strip_signature_summary" />
<EditTextPreference
android:persistent="false"
android:key="account_quote_prefix"

View File

@ -65,6 +65,7 @@ public class Account implements BaseAccount {
public static final String DEFAULT_QUOTE_PREFIX = ">";
public static final boolean DEFAULT_QUOTED_TEXT_SHOWN = true;
public static final boolean DEFAULT_REPLY_AFTER_QUOTE = false;
public static final boolean DEFAULT_STRIP_SIGNATURE = true;
public static final String ACCOUNT_DESCRIPTION_KEY = "description";
public static final String STORE_URI_KEY = "storeUri";
@ -142,6 +143,7 @@ public class Account implements BaseAccount {
private String mQuotePrefix;
private boolean mDefaultQuotedTextShown;
private boolean mReplyAfterQuote;
private boolean mStripSignature;
private boolean mSyncRemoteDeletions;
private String mCryptoApp;
private boolean mCryptoAutoSignature;
@ -231,6 +233,7 @@ public class Account implements BaseAccount {
mQuotePrefix = DEFAULT_QUOTE_PREFIX;
mDefaultQuotedTextShown = DEFAULT_QUOTED_TEXT_SHOWN;
mReplyAfterQuote = DEFAULT_REPLY_AFTER_QUOTE;
mStripSignature = DEFAULT_STRIP_SIGNATURE;
mSyncRemoteDeletions = true;
mCryptoApp = Apg.NAME;
mCryptoAutoSignature = false;
@ -307,6 +310,7 @@ public class Account implements BaseAccount {
mQuotePrefix = prefs.getString(mUuid + ".quotePrefix", DEFAULT_QUOTE_PREFIX);
mDefaultQuotedTextShown = prefs.getBoolean(mUuid + ".defaultQuotedTextShown", DEFAULT_QUOTED_TEXT_SHOWN);
mReplyAfterQuote = prefs.getBoolean(mUuid + ".replyAfterQuote", DEFAULT_REPLY_AFTER_QUOTE);
mStripSignature = prefs.getBoolean(mUuid + ".stripSignature", DEFAULT_STRIP_SIGNATURE);
for (String type : networkTypes) {
Boolean useCompression = prefs.getBoolean(mUuid + ".useCompression." + type,
true);
@ -465,6 +469,7 @@ public class Account implements BaseAccount {
editor.remove(mUuid + ".quotePrefix");
editor.remove(mUuid + ".showPicturesEnum");
editor.remove(mUuid + ".replyAfterQuote");
editor.remove(mUuid + ".stripSignature");
editor.remove(mUuid + ".cryptoApp");
editor.remove(mUuid + ".cryptoAutoSignature");
editor.remove(mUuid + ".enabled");
@ -618,6 +623,7 @@ public class Account implements BaseAccount {
editor.putString(mUuid + ".quotePrefix", mQuotePrefix);
editor.putBoolean(mUuid + ".defaultQuotedTextShown", mDefaultQuotedTextShown);
editor.putBoolean(mUuid + ".replyAfterQuote", mReplyAfterQuote);
editor.putBoolean(mUuid + ".stripSignature", mStripSignature);
editor.putString(mUuid + ".cryptoApp", mCryptoApp);
editor.putBoolean(mUuid + ".cryptoAutoSignature", mCryptoAutoSignature);
editor.putBoolean(mUuid + ".enabled", mEnabled);
@ -1409,6 +1415,14 @@ public class Account implements BaseAccount {
mReplyAfterQuote = replyAfterQuote;
}
public synchronized boolean isStripSignature() {
return mStripSignature;
}
public synchronized void setStripSignature(boolean stripSignature) {
mStripSignature = stripSignature;
}
public boolean getEnableMoveButtons() {
return mEnableMoveButtons;
}

View File

@ -1109,6 +1109,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
new String[] {"JZlib", "http://www.jcraft.com/jzlib/"},
new String[] {"Commons IO", "http://commons.apache.org/io/"},
new String[] {"Mime4j", "http://james.apache.org/mime4j/"},
new String[] {"HtmlCleaner", "http://htmlcleaner.sourceforge.net/"},
};
private void onAbout() {
@ -1153,7 +1154,9 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
"<div>TypePad \u7d75\u6587\u5b57\u30a2\u30a4\u30b3\u30f3\u753b\u50cf " +
"(<a href=\"http://typepad.jp/\">Six Apart Ltd</a>) / " +
"<a href=\"http://creativecommons.org/licenses/by/2.1/jp/\">CC BY 2.1</a></div>"))
.append("</p>");
.append("</p><hr/><p>")
.append(getString(R.string.app_htmlcleaner_license));
wv.loadDataWithBaseURL("file:///android_res/drawable/", html.toString(), "text/html", "utf-8", null);
new AlertDialog.Builder(this)

View File

@ -47,6 +47,11 @@ import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
import com.fsck.k9.Account;
import com.fsck.k9.Account.QuoteStyle;
import com.fsck.k9.Account.MessageFormat;
@ -842,7 +847,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
mMessageFormat = (MessageFormat) savedInstanceState
.getSerializable(STATE_KEY_MESSAGE_FORMAT);
mReadReceipt = savedInstanceState
.getBoolean(STATE_KEY_READ_RECEIPT);
.getBoolean(STATE_KEY_READ_RECEIPT);
mCcWrapper.setVisibility(savedInstanceState.getBoolean(STATE_KEY_CC_SHOWN) ? View.VISIBLE
: View.GONE);
mBccWrapper.setVisibility(savedInstanceState
@ -1515,10 +1520,10 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
private void onReadReceipt() {
CharSequence txt;
if (mReadReceipt == false) {
txt=getString(R.string.read_receipt_enabled);
txt = getString(R.string.read_receipt_enabled);
mReadReceipt = true;
} else {
txt=getString(R.string.read_receipt_disabled);
txt = getString(R.string.read_receipt_disabled);
mReadReceipt = false;
}
Context context = getApplicationContext();
@ -2357,6 +2362,12 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
}
}
// Regexes to check for signature.
private static final Pattern DASH_SIGNATURE_PLAIN = Pattern.compile("\r\n-- \r\n.*", Pattern.DOTALL);
private static final Pattern DASH_SIGNATURE_HTML = Pattern.compile("(<br( /)?>|\r?\n)-- <br( /)?>", Pattern.CASE_INSENSITIVE);
private static final Pattern BLOCKQUOTE_START = Pattern.compile("<blockquote", Pattern.CASE_INSENSITIVE);
private static final Pattern BLOCKQUOTE_END = Pattern.compile("</blockquote>", Pattern.CASE_INSENSITIVE);
/**
* Build and populate the UI with the quoted message.
* @throws MessagingException
@ -2370,6 +2381,79 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
? mSourceMessageBody
: getBodyTextFromMessage(mSourceMessage, mMessageFormat);
if (mMessageFormat == MessageFormat.HTML) {
// Strip signature.
// closing tags such as </div>, </span>, </table>, </pre> will be cut off.
if (mAccount.isStripSignature() && (ACTION_REPLY_ALL.equals(getIntent().getAction()) ||
ACTION_REPLY.equals(getIntent().getAction()))) {
Matcher dashSignatureHtml = DASH_SIGNATURE_HTML.matcher(content);
if (dashSignatureHtml.find()) {
Matcher blockquoteStart = BLOCKQUOTE_START.matcher(content);
Matcher blockquoteEnd = BLOCKQUOTE_END.matcher(content);
List<Integer> start = new ArrayList<Integer>();
List<Integer> end = new ArrayList<Integer>();
while(blockquoteStart.find()) {
start.add(blockquoteStart.start());
}
while(blockquoteEnd.find()) {
end.add(blockquoteEnd.start());
}
if (start.size() != end.size()) {
Log.d(K9.LOG_TAG, "There are " + start.size() + " <blockquote> tags, but " +
end.size() + " </blockquote> tags. Refusing to strip.");
} else if (start.size() > 0) {
// Ignore quoted signatures in blockquotes.
dashSignatureHtml.region(0, start.get(0));
if (dashSignatureHtml.find()) {
// before first <blockquote>.
content = content.substring(0, dashSignatureHtml.start());
} else {
for (int i = 0; i < start.size() - 1; i++) {
// within blockquotes.
if (end.get(i) < start.get(i+1)) {
dashSignatureHtml.region(end.get(i), start.get(i+1));
if (dashSignatureHtml.find()) {
content = content.substring(0, dashSignatureHtml.start());
break;
}
}
}
if (end.get(end.size() - 1) < content.length()) {
// after last </blockquote>.
dashSignatureHtml.region(end.get(end.size() - 1), content.length());
if (dashSignatureHtml.find()) {
content = content.substring(0, dashSignatureHtml.start());
}
}
}
} else {
// No blockquotes found.
content = content.substring(0, dashSignatureHtml.start());
}
}
// Fix the stripping off of closing tags if a signature was stripped,
// as well as clean up the HTML of the quoted message.
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties properties = cleaner.getProperties();
// see http://htmlcleaner.sourceforge.net/parameters.php for descriptions
properties.setNamespacesAware(false);
properties.setAdvancedXmlEscape(false);
properties.setOmitXmlDeclaration(true);
properties.setOmitDoctypeDeclaration(false);
properties.setTranslateSpecialEntities(false);
properties.setRecognizeUnicodeChars(false);
TagNode node = cleaner.clean(content);
SimpleHtmlSerializer htmlSerialized = new SimpleHtmlSerializer(properties);
try {
content = htmlSerialized.getAsString(node, "UTF8");
} catch (java.io.IOException ioe) {
// Can't imagine this happening.
Log.e(K9.LOG_TAG, "Problem cleaning quoted message.", ioe);
}
}
// Add the HTML reply header to the top of the content.
mQuotedHtmlContent = quoteOriginalHtmlMessage(mSourceMessage, content, mQuoteStyle);
// Load the message with the reply header.
@ -2378,6 +2462,12 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
getBodyTextFromMessage(mSourceMessage, MessageFormat.TEXT), mQuoteStyle));
} else if (mMessageFormat == MessageFormat.TEXT) {
if (mAccount.isStripSignature() && (ACTION_REPLY_ALL.equals(getIntent().getAction()) ||
ACTION_REPLY.equals(getIntent().getAction()))) {
if (DASH_SIGNATURE_PLAIN.matcher(content).find()) {
content = DASH_SIGNATURE_PLAIN.matcher(content).replaceFirst("\r\n");
}
}
mQuotedText.setText(quoteOriginalTextMessage(mSourceMessage, content, mQuoteStyle));
}

View File

@ -93,6 +93,7 @@ public class AccountSettings extends K9PreferenceActivity {
private static final String PREFERENCE_QUOTE_STYLE = "quote_style";
private static final String PREFERENCE_DEFAULT_QUOTED_TEXT_SHOWN = "default_quoted_text_shown";
private static final String PREFERENCE_REPLY_AFTER_QUOTE = "reply_after_quote";
private static final String PREFERENCE_STRIP_SIGNATURE = "strip_signature";
private static final String PREFERENCE_SYNC_REMOTE_DELETIONS = "account_sync_remote_deletetions";
private static final String PREFERENCE_CRYPTO_APP = "crypto_app";
private static final String PREFERENCE_CRYPTO_AUTO_SIGNATURE = "crypto_auto_signature";
@ -152,6 +153,7 @@ public class AccountSettings extends K9PreferenceActivity {
private EditTextPreference mAccountQuotePrefix;
private CheckBoxPreference mAccountDefaultQuotedTextShown;
private CheckBoxPreference mReplyAfterQuote;
private CheckBoxPreference mStripSignature;
private CheckBoxPreference mSyncRemoteDeletions;
private CheckBoxPreference mSaveAllHeaders;
private CheckBoxPreference mPushPollOnConnect;
@ -241,6 +243,9 @@ public class AccountSettings extends K9PreferenceActivity {
mReplyAfterQuote = (CheckBoxPreference) findPreference(PREFERENCE_REPLY_AFTER_QUOTE);
mReplyAfterQuote.setChecked(mAccount.isReplyAfterQuote());
mStripSignature = (CheckBoxPreference) findPreference(PREFERENCE_STRIP_SIGNATURE);
mStripSignature.setChecked(mAccount.isStripSignature());
mComposingScreen = (PreferenceScreen) findPreference(PREFERENCE_SCREEN_COMPOSING);
Preference.OnPreferenceChangeListener quoteStyleListener = new Preference.OnPreferenceChangeListener() {
@ -726,6 +731,7 @@ public class AccountSettings extends K9PreferenceActivity {
mAccount.setQuotePrefix(mAccountQuotePrefix.getText());
mAccount.setDefaultQuotedTextShown(mAccountDefaultQuotedTextShown.isChecked());
mAccount.setReplyAfterQuote(mReplyAfterQuote.isChecked());
mAccount.setStripSignature(mStripSignature.isChecked());
mAccount.setCryptoApp(mCryptoApp.getValue());
mAccount.setCryptoAutoSignature(mCryptoAutoSignature.isChecked());
mAccount.setLocalStorageProviderId(mLocalStorageProvider.getValue());

View File

@ -65,6 +65,7 @@ public class AccountSettings {
s.put("quoteStyle",
new EnumSetting(Account.QuoteStyle.class, Account.DEFAULT_QUOTE_STYLE));
s.put("replyAfterQuote", new BooleanSetting(Account.DEFAULT_REPLY_AFTER_QUOTE));
//s.put("stripSignature", new BooleanSetting(Account.DEFAULT_STRIP_SIGNATURE)); // added to version 2
s.put("ring", new BooleanSetting(true));
s.put("ringtone", new RingtoneSetting("content://settings/system/notification_sound"));
s.put("saveAllHeaders", new BooleanSetting(true));