(ns cipher-analytical-machine.cli.cli (:require [cipher-analytical-machine.cli.file :as file] [cipher-analytical-machine.ciphers.caesar :as caesar] [cipher-analytical-machine.analyzers.caesar :as caesar-analyzers] [cipher-analytical-machine.symbols.factories :as sf] [cipher-analytical-machine.symbols.frequences :as symbol-frequences] [clojure.string :as cs] [clojure.tools.cli :refer [parse-opts]]) (:gen-class)) (def cipher-options #{"Caesar"}) (def language-options #{"en", "uk"}) (def cli-options [["-m" "--message MESSAGE" "The message will be encrypted or decrypted."] ["-M" "--message-file MESSAGE-FILE" "The file contains the message that will be encrypted or decrypted."] ["-k" "--key KEY" "The key will be used to encrypt or decrypt."] ["-c" "--cipher CIPHER" "The cipher will be used to encrypt or decrypt a message." :default "Caesar"] ["-e" "--encrypt" "Encrypt the message."] ["-d" "--decrypt" "Decrypt the message."] ["-s" "--symbols" "The string will be used as a set of symbols for a cipher." :default (sf/default-symbol-factory "en")] ["-l" "--language CODE" "The string will be used to set a default symbols for a cipher." :validate [#(contains? language-options %) "Must be a code from the list: en, uk!!!"]] ["-C" "--cracking" "Cracking the encrypted message."] ["-O" "--output-file OUTPUT-FILE" "Save the program output to a file."] ["-h" "--help"]]) (defn usage [options-summary] (->> ["This is cipher-analytical-machine. It can help you to learn how ciphers works." "" "Usage: cipher-analytical-machine [options]" "" "Options:" options-summary ""] (cs/join \newline))) (defn error-msg [errors] (str "The following errors occurred while parsing your command:\n\n" (cs/join \newline errors))) (defn map-has-keys? "Return true if all keys in the map." [map keys] (every? #(contains? map %) keys)) (defn validate-args "Validate command line arguments. Either return a map indicating the program should exit (with an error message, and optional ok status), or a map indicating the action the program should take and the options provided." [args] (let [{:keys [options arguments errors summary]} (parse-opts args cli-options)] (cond (:help options) {:exit-message (usage summary) :ok? true} errors {:exit-message (error-msg errors)} (map-has-keys? options [:encrypt, :decrypt]) {:exit-message (error-msg ["You can't use enctypt and decrypt mode at the same time!!!"])} (map-has-keys? options [:message, :message-file]) {:exit-message (error-msg ["You can't use message and message-file options at the same time!!!"])} (and (contains? options :message-file) (file/is-file-not-exists? (:message-file options))) {:exit-message (error-msg ["Please, check the path to the message file!!!"])} (and (contains? options :output-file) (file/is-file-exists? (:output-file options))) {:exit-message (error-msg ["Please, choose another file!!! Overwriting was prevented!!!"])} (and (map-has-keys? options [:cipher, :key]) (or (contains? options :message) (contains? options :message-file)) (or (contains? options :encrypt) (contains? options :decrypt))) (cond (contains? options :encrypt) {:options options :arguments arguments :action-type :encrypt} (contains? options :decrypt) {:options options :arguments arguments :action-type :decrypt}) (and (map-has-keys? options [:cipher, :cracking]) (or (contains? options :message) (contains? options :message-file))) {:options options :arguments arguments :action-type :cracking} :else {:exit-message (usage summary)}))) (defn exit [exit-message status] (println exit-message) (System/exit (if status 0 1))) (defn get-or-load-option "Return the option value or load the value from a file." [options arguments option] (let [file-option (keyword (str (name option) "-file"))] (cond (contains? options option) (option options) (contains? options file-option) (file/read-file (file-option options))))) (defn set-option "Set a option in the option map." [options option value] (-> options (assoc option value))) (defn set-symbols "Set defaults symbols for a language." [options] (set-option options :symbols (sf/default-symbol-factory (:language options)))) (defn load-and-set-option "Load the option and set it." [options option] (->> (get-or-load-option options nil option) (set-option options option))) (defn load-all-options "Load all options." [options] (-> options (set-symbols) (load-and-set-option :message))) (defn save-output "Save the output to a file" [output options] (let [file-path (:output-file options)] (file/write-file file-path output))) (defn show-and-save-output "Print and save the output to a file if needed" [output options] (when (contains? options :output-file) (save-output output options)) (println output)) (defn cracking-actions [options arguments action-type] (let [message (:message options) symbols (:symbols options) frequencies symbol-frequences/english-letter-frequences] (cond (= action-type :cracking) (caesar-analyzers/get-plaintext message symbols frequencies) ))) (defn crypt-actions [options arguments action-type] (let [message (:message options) key (Integer/parseInt (:key options)) symbols (:symbols options)] (cond (= action-type :encrypt) (caesar/encrypt-message message key symbols) (= action-type :decrypt) (caesar/decrypt-message message key symbols)))) (defn actions [options arguments action-type] (let [options (load-all-options options)] (-> (cond (contains? #{:encrypt :decrypt} action-type) (crypt-actions options arguments action-type) (= action-type :cracking) (cracking-actions options arguments action-type)) (show-and-save-output options))))