diff --git a/rbw.el b/rbw.el new file mode 100644 index 0000000..0547457 --- /dev/null +++ b/rbw.el @@ -0,0 +1,81 @@ +(defun rbw-get-password () + (interactive) + (kill-new (rbw/list-all))) + +(defun rbw-add () + (interactive) + (let* ((name (read-string "password name: ")) + (username (read-string "username: ")) + (entry (concat name " " username)) + (exists? (car (rbw/get entry)))) + (message exists?) + (if exists? (message "entry already exists") + (let* ((length (read-number "length: "))) + (rbw/generate length name username))))) + +;; TODO +;; (defun rbw-remove () +;; (interactive) + +;; ) + +(defun rbw-get-user () + (interactive) + (let* ((output (rbw/list-all "--field user")) + (output (string-trim-left output "Username: "))) + (kill-new output))) + +(defun rbw-generate () + (interactive) + (let* ((length (read-number "password length: "))) + (kill-new (rbw/generate length)))) + +(defun rbw/sync () (shell-command "rbw sync")) + +(defun rbw/list-all (&optional options entries selected) + (let* ((entries (if entries entries (rbw/list))) + (entry (completing-read "bitwarden entries: " entries)) + (selected (concat selected " " entry)) + (selected (string-trim selected)) + (result (rbw/get selected options)) + (output (car (last result))) + (result (car result))) + (if result output + (rbw/list-all options (rbw/extract-multiple output selected) selected)))) + +(defun rbw/generate (length &optional name user) + (let* ((length (number-to-string length)) + (command (concat "rbw generate " length " " name " " user))) + (shell-command-to-string command))) + +(defun rbw/unlocked () + (if (string-empty-p (shell-command-to-string "rbw unlocked")) t nil)) + +(defun rbw/list () + (let* ((output (shell-command-to-string "rbw list")) + (output (string-trim output)) + (entries (split-string output "\n"))) + entries)) + +(defun rbw/get (entry &optional options) + (let* ((command (concat "rbw get " options " " entry " ; echo $?")) + (output (shell-command-to-string command)) + (output (string-trim output)) + (output (split-string output "\n")) + (result (car (last output))) + (result (string-equal result "0")) + (output (car output))) + (list result output))) + +(defun rbw/extract-multiple (output selected) + (string-match "\\([^:]*\\)$" output) + (let* ((trimmed (string-trim (match-string 1 output))) + (splitted (split-string trimmed ", ")) + (splitted (cl-remove-if-not (lambda (x) (string-match "@" x)) splitted)) + (ending (concat "@" selected)) + (result-list '())) + (dolist (element splitted) + (push (car (split-string element ending)) result-list)) + (reverse result-list))) + +(provide 'rbw)