Ignore:
Timestamp:
04/02/19 15:22:06 (5 years ago)
Author:
bln4
Message:

Changes have been made to how mail is stored. Each user now has a location in their deaddrops folder (found inside their resources folder) that acts as an exchange location for each of their partnerships. Mail databases now belong in there.

Initial functionality to support the culling of already processed messages from the mail database has been implemented with more to follow.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/auth/Mail.java

    r1243 r1277  
    11package org.expeditee.auth;
    22
     3import java.io.File;
     4import java.io.IOException;
    35import java.nio.file.Path;
     6import java.nio.file.Paths;
    47import java.security.InvalidKeyException;
     8import java.security.KeyStoreException;
    59import java.security.NoSuchAlgorithmException;
    610import java.security.PrivateKey;
    711import java.security.PublicKey;
     12import java.security.cert.CertificateException;
     13import java.security.spec.InvalidKeySpecException;
    814import java.sql.Connection;
    915import java.sql.DriverManager;
    1016import java.sql.PreparedStatement;
    1117import java.sql.SQLException;
     18import java.sql.Statement;
    1219import java.util.ArrayList;
    1320import java.util.Arrays;
     
    2229import javax.crypto.NoSuchPaddingException;
    2330
     31import org.expeditee.gui.FrameIO;
    2432import org.ngikm.cryptography.CryptographyConstants;
    2533
     
    3947        }
    4048       
    41         public static void sendMail(MailEntry mail, PublicKey key, Path outbox) {
     49        public static void sendMail(MailEntry mail, String colleagueName) {
     50                // Ensure dead drop area is set up.
     51                Path databaseFileDirPath = Paths.get(FrameIO.DEAD_DROPS_PATH).resolve(colleagueName);
     52                Path databaseFilePath = databaseFileDirPath.resolve(colleagueName + ".db");
     53                File databaseFile = databaseFilePath.toFile();
     54                if (!databaseFile.exists()) {
     55                        databaseFileDirPath.toFile().mkdirs();
     56                        String sql =
     57                                        "CREATE TABLE EXPMAIL (" +
     58                                        "TIME           TEXT                                    NOT NULL, " +
     59                                        "SND            TEXT                                    NOT NULL, " +
     60                                        "REC            TEXT                                    NOT NULL, " +
     61                                        "MSG            TEXT                                    NOT NULL, " +
     62                                        "MSG2           TEXT                                    NOT NULL, " +
     63                                        "OPTS           ARRAY                                   NOT NULL, " +
     64                                        "OPTSVAL        ARRAY                                   NOT NULL)";
     65                        try {
     66                                Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath());
     67                                Statement createTable = c.createStatement();
     68                                createTable.executeUpdate(sql);
     69                                createTable.close();
     70                                c.close();
     71                        } catch (SQLException e) {
     72                                System.err.println("Error while creating database file.");
     73                                e.printStackTrace();
     74                        }
     75                }
     76               
     77                // Obtain public key
     78                PublicKey publicKey = null;
     79                try {
     80                        publicKey = Authenticator.getInstance().getPublicKey(colleagueName);
     81                } catch (InvalidKeySpecException | NoSuchAlgorithmException | KeyStoreException | CertificateException
     82                                | ClassNotFoundException | IOException | SQLException e) {
     83                        System.err.println("Error while sending message.  Unable to obtain public key for colleague " + 
     84                                colleagueName + ".  Exception message: " + e.getMessage());
     85                        return;
     86                }
     87               
     88                // Check we got public key
     89                if (publicKey == null) {
     90                        System.err.println("Error while sending message.  Unable to obtain public key for colleague.  Have you exchanged contact details?");
     91                        return;
     92                }
     93               
     94                // Send message
     95                sendMail(mail, publicKey, databaseFilePath);
     96        }
     97       
     98        private static void sendMail(MailEntry mail, PublicKey key, Path databaseFile) {
    4299                try {
    43100                        Cipher cipher = Cipher.getInstance(AsymmetricAlgorithm + AsymmetricAlgorithmParameters);
    44101                       
    45102                        // encrypt the necessary parts of the message
    46                         cipher.init(Cipher.ENCRYPT_MODE, key);
    47                         String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes()));
     103                        //cipher.init(Cipher.ENCRYPT_MODE, key);
     104                        //String time = Base64.getEncoder().encodeToString(cipher.doFinal(mail.timestamp.getBytes()));
    48105                        cipher.init(Cipher.ENCRYPT_MODE, key);
    49106                        String sender = Base64.getEncoder().encodeToString(cipher.doFinal(mail.sender.getBytes()));
     
    65122                       
    66123                        // write to mail database
    67                         Connection c = DriverManager.getConnection("jdbc:sqlite:" + outbox.resolve("expmail.db"));
     124                        Connection c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile);
    68125                        String sql = "INSERT INTO EXPMAIL (TIME,SND,REC,MSG,MSG2,OPTS,OPTSVAL) VALUES (?, ?, ?, ?, ?, ?, ?);";
    69126                        PreparedStatement statement = c.prepareStatement(sql);
    70                         statement.setString(1, time);
     127                        statement.setString(1, mail.timestamp);
    71128                        statement.setString(2, sender);
    72129                        statement.setString(3, rec);
     
    110167                        if (receiverDecrypted.compareToIgnoreCase(name) == 0) {
    111168                                c.init(Cipher.DECRYPT_MODE, key);
    112                                 String timestamp = new String(c.doFinal(Base64.getDecoder().decode(mail.timestamp)));
    113                                 c.init(Cipher.DECRYPT_MODE, key);
    114169                                String sender = new String(c.doFinal(Base64.getDecoder().decode(mail.sender)));
    115170                                c.init(Cipher.DECRYPT_MODE, key);
     
    128183                               
    129184                                //String arguments = new String(c.doFinal(Base64.getDecoder().decode(mail.args)));
    130                                 filtered.add(new MailEntry(timestamp, sender, receiverDecrypted, message, message2, options));
     185                                filtered.add(new MailEntry(mail.timestamp, sender, receiverDecrypted, message, message2, options));
    131186                        }
    132187                }
Note: See TracChangeset for help on using the changeset viewer.