Add use of algorithm switch

DisPass has supported more than one algorithm for some time already,
but how to specify which algorithm to use was not quite clear. Now it
should use the correct algorithm and when specifying an unknown label
it will ask for the preferred algorithm.
This commit is contained in:
Tom Willemsen 2013-01-20 14:29:17 +01:00
parent 02ec82b151
commit fb3a9ab978

View file

@ -63,6 +63,11 @@
"Keymap for `dispass-labels-mode', uses "Keymap for `dispass-labels-mode', uses
`tabulated-list-mode-map' as its parent.") `tabulated-list-mode-map' as its parent.")
(defconst dispass-algorithms
'("dispass1" "dispass2")
"The list of algorithms supported by DisPass, this should be
extracted from DisPass at some point.")
(defun dispass-process-sentinel (proc status) (defun dispass-process-sentinel (proc status)
"Report PROC's status change to STATUS." "Report PROC's status change to STATUS."
(let ((status (substring status 0 -1)) (let ((status (substring status 0 -1))
@ -106,12 +111,12 @@ an eye out for LABEL."
(clipboard-kill-ring-save (point-min) (point-max)) (clipboard-kill-ring-save (point-min) (point-max))
(message "Password copied to clipboard."))))))) (message "Password copied to clipboard.")))))))
(defun dispass-start-process (label create length) (defun dispass-start-process (label create length &optional algo args)
"Start dispass process. When CREATE is non-nil send along the "Start dispass process. When CREATE is non-nil send along the
-c switch to make it ask for a password twice. When LENGTH is -c switch to make it ask for a password twice. When LENGTH is
an integer and greater than 0, send along the -l switch with an integer and greater than 0, send along the -l switch with
LENGTH." LENGTH."
(let ((args `("-o" ,label)) (let ((args `("-o" ,@args ,label))
proc) proc)
(when create (when create
(setq args (append '("-c") args))) (setq args (append '("-c") args)))
@ -119,6 +124,10 @@ an eye out for LABEL."
(when (and (integerp length) (> length 0)) (when (and (integerp length) (> length 0))
(setq args (append `("-l" ,(number-to-string length)) args))) (setq args (append `("-l" ,(number-to-string length)) args)))
(when (and algo (not (equal algo ""))
(member algo dispass-algorithms))
(setq args (append `("-a" ,algo) args)))
(setq proc (apply 'start-process "dispass" "*dispass*" (setq proc (apply 'start-process "dispass" "*dispass*"
dispass-executable args)) dispass-executable args))
(set-process-sentinel proc 'dispass-process-sentinel) (set-process-sentinel proc 'dispass-process-sentinel)
@ -128,9 +137,18 @@ an eye out for LABEL."
"Get the list of labels and their information." "Get the list of labels and their information."
(let ((result '())) (let ((result '()))
(with-temp-buffer (with-temp-buffer
(insert (shell-command-to-string (dispass-read-labels)
(concat dispass-labels-executable " -l --script"))) (while (re-search-forward
(goto-char (point-min)) "^\\(\\(?:\\sw\\|\\s_\\)+\\)"
nil t)
(add-to-list 'result (match-string 1)))
result)))
(defun dispass-get-labels-for-display ()
"Prepare the list of labels for info table."
(let ((result '()))
(with-temp-buffer
(dispass-read-labels)
(while (re-search-forward (while (re-search-forward
"^\\(\\(?:\\sw\\|\\s_\\)+\\) +\\([0-9]+\\) +\\(\\(?:\\sw\\|\\s_\\)+\\)" "^\\(\\(?:\\sw\\|\\s_\\)+\\) +\\([0-9]+\\) +\\(\\(?:\\sw\\|\\s_\\)+\\)"
nil t) nil t)
@ -150,23 +168,37 @@ an eye out for LABEL."
,algo]))))) ,algo])))))
result)) result))
;;;###autoload (defun dispass-read-labels ()
(defun dispass-create (label &optional length) "Load a list of all labels into a buffer."
"Create a new password for LABEL." (insert (shell-command-to-string
(interactive "MLabel: \nP") (concat dispass-labels-executable " -l --script")))
(let ((length (or length dispass-default-length))) (goto-char (point-min)))
(dispass-start-process label t length)))
;;;###autoload ;;;###autoload
(defun dispass (label &optional length) (defun dispass-create (label &optional length algo)
"Create a new password for LABEL."
(interactive (list
(read-from-minibuffer "Label: ")
current-prefix-arg
(completing-read "Algorithm: " dispass-algorithms)))
(let ((length (or length dispass-default-length)))
(dispass-start-process label t length algo)))
;;;###autoload
(defun dispass (label &optional length algo)
"Recreate a password previously used." "Recreate a password previously used."
(interactive (list (interactive (list
(completing-read (completing-read
"Label: " (mapcar (lambda (elm) (elt elm 0)) "Label: " (dispass-get-labels))
(dispass-get-labels)))
current-prefix-arg)) current-prefix-arg))
(if (called-interactively-p 'any)
(unless (member label (dispass-get-labels))
(setq algo (completing-read
"Algorithm: " dispass-algorithms))))
(let ((length (or length dispass-default-length))) (let ((length (or length dispass-default-length)))
(dispass-start-process label nil length))) (dispass-start-process
label nil length algo
(when (member label (dispass-get-labels)) '("-s")))))
;; Labels management ;; Labels management
;;;###autoload ;;;###autoload
@ -177,10 +209,12 @@ an eye out for LABEL."
(read-from-minibuffer (read-from-minibuffer
(format "Length (%d): " dispass-default-length) nil nil t nil (format "Length (%d): " dispass-default-length) nil nil t nil
(number-to-string dispass-default-length)) (number-to-string dispass-default-length))
(symbol-name (read-from-minibuffer (completing-read
"Algorithm (dispass1): " nil nil t nil "dispass1")))) "Algorithm (dispass1): "
dispass-algorithms nil nil nil nil "dispass1")))
(shell-command (shell-command
(format "%s --add %s:%d:%s" dispass-labels-executable label length algo))) (format "%s --add %s:%d:%s"
dispass-labels-executable label length algo)))
;;;###autoload ;;;###autoload
(defun dispass-remove-label (label) (defun dispass-remove-label (label)
@ -191,9 +225,7 @@ thrown."
(interactive (interactive
(list (or (dispass-label-at-point) (list (or (dispass-label-at-point)
(completing-read (completing-read
"Label: " (mapcar (lambda (elm) (elt elm 0)) "Label: " (dispass-get-labels)))))
(dispass-get-labels))))))
(shell-command (shell-command
(format "%s --remove %s" dispass-labels-executable label))) (format "%s --remove %s" dispass-labels-executable label)))
@ -207,7 +239,7 @@ thrown."
(setq tabulated-list-entries nil) (setq tabulated-list-entries nil)
(let ((tmp-list '())) (let ((tmp-list '()))
(setq tabulated-list-entries (dispass-get-labels)))) (setq tabulated-list-entries (dispass-get-labels-for-display))))
(define-derived-mode dispass-labels-mode tabulated-list-mode "DisPass" (define-derived-mode dispass-labels-mode tabulated-list-mode "DisPass"
"Major mode for listing dispass labels. "Major mode for listing dispass labels.