[geary] Validate contact emails before adding



commit 0f6e76edb3cdd6a85e3d247af1d1ebef1af9dd70
Author: Charles Lindsay <chaz yorba org>
Date:   Fri Feb 21 15:00:57 2014 -0800

    Validate contact emails before adding
    
    Also updates the DB to filter out existing invalid contacts.
    
    Closes: bgo #713932

 sql/CMakeLists.txt                                |    1 +
 sql/version-019.sql                               |    5 +++
 src/engine/imap-db/imap-db-database.vala          |   29 +++++++++++++++++++++
 src/engine/imap-db/imap-db-message-addresses.vala |    3 ++
 4 files changed, 38 insertions(+), 0 deletions(-)
---
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt
index c206d2c..84ad3e4 100644
--- a/sql/CMakeLists.txt
+++ b/sql/CMakeLists.txt
@@ -18,3 +18,4 @@ install(FILES version-015.sql DESTINATION ${SQL_DEST})
 install(FILES version-016.sql DESTINATION ${SQL_DEST})
 install(FILES version-017.sql DESTINATION ${SQL_DEST})
 install(FILES version-018.sql DESTINATION ${SQL_DEST})
+install(FILES version-019.sql DESTINATION ${SQL_DEST})
diff --git a/sql/version-019.sql b/sql/version-019.sql
new file mode 100644
index 0000000..6ea8732
--- /dev/null
+++ b/sql/version-019.sql
@@ -0,0 +1,5 @@
+--
+-- Dummy database upgrade to validate contact email addresses.  See
+-- src/engine/imap-db/imap-db-database.vala in post_upgrade() for the code
+-- that runs the upgrade.
+--
diff --git a/src/engine/imap-db/imap-db-database.vala b/src/engine/imap-db/imap-db-database.vala
index f9ce635..a594356 100644
--- a/src/engine/imap-db/imap-db-database.vala
+++ b/src/engine/imap-db/imap-db-database.vala
@@ -97,6 +97,10 @@ private class Geary.ImapDB.Database : Geary.Db.VersionedDatabase {
             case 18:
                 post_upgrade_populate_internal_date_time_t();
             break;
+            
+            case 19:
+                post_upgrade_validate_contacts();
+            break;
         }
     }
     
@@ -370,6 +374,31 @@ private class Geary.ImapDB.Database : Geary.Db.VersionedDatabase {
         }
     }
     
+    // Version 19.
+    private void post_upgrade_validate_contacts() {
+        try {
+            exec_transaction(Db.TransactionType.RW, (cx) => {
+                Db.Result result = cx.query("SELECT id, email FROM ContactTable");
+                while (!result.finished) {
+                    string email = result.string_at(1);
+                    if (!RFC822.MailboxAddress.is_valid_address(email)) {
+                        int64 id = result.rowid_at(0);
+                        
+                        Db.Statement stmt = cx.prepare("DELETE FROM ContactTable WHERE id = ?");
+                        stmt.bind_rowid(0, id);
+                        stmt.exec();
+                    }
+                    
+                    result.next();
+                }
+                
+                return Db.TransactionOutcome.COMMIT;
+            });
+        } catch (Error err) {
+            debug("Error populating autocompletion table during upgrade to database schema 5");
+        }
+    }
+    
     private void on_prepare_database_connection(Db.Connection cx) throws Error {
         cx.set_busy_timeout_msec(Db.Connection.RECOMMENDED_BUSY_TIMEOUT_MSEC);
         cx.set_foreign_keys(true);
diff --git a/src/engine/imap-db/imap-db-message-addresses.vala 
b/src/engine/imap-db/imap-db-message-addresses.vala
index f25f547..d46ed35 100644
--- a/src/engine/imap-db/imap-db-message-addresses.vala
+++ b/src/engine/imap-db/imap-db-message-addresses.vala
@@ -126,6 +126,9 @@ private class Geary.ImapDB.MessageAddresses : BaseObject {
     
     private void add_contact(Gee.Map<string, Contact> contacts_map, RFC822.MailboxAddress address,
         int importance) {
+        if (!address.is_valid())
+            return;
+        
         Contact contact = new Contact.from_rfc822_address(address, importance);
         Contact? old_contact = contacts_map[contact.normalized_email];
         if (old_contact == null || old_contact.highest_importance < contact.highest_importance)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]