view src/main/kotlin/name/blackcap/passman/ImportSubcommand.kt @ 18:8f3ddebb4295

Was using wrong db object to decrypt, fixed.
author David Barts <n5jrn@me.com>
date Tue, 04 Apr 2023 20:38:52 -0700
parents 7a74ae668665
children ea65ab890f66
line wrap: on
line source

package name.blackcap.passman

import com.opencsv.CSVParserBuilder
import com.opencsv.CSVReaderBuilder
import com.opencsv.exceptions.CsvException
import org.apache.commons.cli.ParseException
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStreamReader
import java.text.SimpleDateFormat
import java.util.*

class ImportSubcommand(): Subcommand() {
    private companion object {
        const val NFIELDS = 7
    }
    private lateinit var csvDateFormat: SimpleDateFormat
    private lateinit var db: Database
    private val options = ImportExportArguments()
    private lateinit var csvFile: String
    private var line = 0

    override fun run(args: Array<String>) {
        parseArguments(args)
        db = Database.open()
        try {
            doImport()
        } catch (e: IOException) {
            die(e.message ?: "I/O error")
        } catch (e: CsvException) {
            val message = e.message ?: "CSV error"
            die("line $line, $message")
        }
    }

    private fun parseArguments(args: Array<String>) {
        val params = parseInto("import", args, options)
        when (params.size) {
            0 -> die("expecting CSV file name", 2)
            1 -> csvFile = params[0]
            else -> die("unexpected trailing arguments", 2)
        }
        csvDateFormat = SimpleDateFormat(options.format).apply {
            timeZone = TimeZone.getTimeZone(options.zone)
            isLenient = false
        }
    }

    private fun doImport() {
        val csvParser = CSVParserBuilder()
            .withEscapeChar(options.escape)
            .withQuoteChar(options.quote)
            .withSeparator(options.separator)
            .withIgnoreLeadingWhiteSpace(options.ignore)
            .build()

        val csvReader = CSVReaderBuilder(InputStreamReader(FileInputStream(csvFile), options.charset))
            .withCSVParser(csvParser)
            .build()

        csvReader.use {
            if (options.skip) {
                line++
                it.skip(1)
            }

            it.iterator().forEach { fields ->
                line++
                val importedEntry = fromCsv(fields)
                val thisEntry = Entry.fromDatabase(db, importedEntry.name)
                try {
                    if (okToChange(thisEntry, importedEntry)) {
                        if (thisEntry == null) {
                            importedEntry.insert(db)
                        } else {
                            importedEntry.update(db)
                        }
                    }
                } finally {
                    thisEntry?.password?.clear()
                }
            }
        }
    }

    private fun fromCsv(fields: Array<String>): Entry {
        if (fields.size != NFIELDS) {
            die("line $line, expected $NFIELDS fields but got ${fields.size}")
        }
        return Entry(
            name = fields[0],
            username = fields[1],
            password = fields[2].toCharArray(),
            notes = if (saysNull(fields[3])) null else fields[3],
            created = parseCsvTime(fields[4]) ?: Date(),
            modified = parseCsvTime(fields[5]),
            accessed = parseCsvTime(fields[6])
        )
    }

    private fun parseCsvTime(unparsed: String): Date? {
        if (saysNull(unparsed)) {
            return null
        }
        try {
            return csvDateFormat.parse(unparsed)
        } catch (e: ParseException) {
            die("${see(unparsed)} - invalid date/time string")
            throw e /* kotlin is too stupid to realize this never happens */
        }
    }

    private fun okToChange(thisEntry: Entry?, otherEntry: Entry): Boolean =
        thisEntry == null || options.force || askUserIfOkToOverwrite(thisEntry, otherEntry)

    private fun saysNull(string: String) = string.lowercase() == "null"

}