summaryrefslogtreecommitdiff
path: root/.emacs.d/modules/thanos-pass.el
blob: 505093b66359e541633c3b22897ab13b66ab6b4b (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;;; thanos-pass.el --- Pass configuration            -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Thanos Apollo

;; Author: Thanos Apollo <[email protected]>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;; ╭━━━━┳╮╱╱╱╱╱╱╱╱╱╱╱╱╱╱╭━━━╮╱╱╱╱╱╭╮╭╮╱╱╱╱╱╱╱╱╭━━━╮
;; ┃╭╮╭╮┃┃╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃╭━╮┃╱╱╱╱╱┃┃┃┃╱╱╱╱╱╱╱╱┃╭━━╯
;; ╰╯┃┃╰┫╰━┳━━┳━╮╭━━┳━━╮┃┃╱┃┣━━┳━━┫┃┃┃╭━━╮╱╱╱╱┃╰━━┳╮╭┳━━┳━━┳━━╮
;; ╱╱┃┃╱┃╭╮┃╭╮┃╭╮┫╭╮┃━━┫┃╰━╯┃╭╮┃╭╮┃┃┃┃┃╭╮┃╭━━╮┃╭━━┫╰╯┃╭╮┃╭━┫━━┫
;; ╱╱┃┃╱┃┃┃┃╭╮┃┃┃┃╰╯┣━━┃┃╭━╮┃╰╯┃╰╯┃╰┫╰┫╰╯┃╰━━╯┃╰━━┫┃┃┃╭╮┃╰━╋━━┃
;; ╱╱╰╯╱╰╯╰┻╯╰┻╯╰┻━━┻━━╯╰╯╱╰┫╭━┻━━┻━┻━┻━━╯╱╱╱╱╰━━━┻┻┻┻╯╰┻━━┻━━╯
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃┃
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╰╯
;;; Code:

(require 'password-store)

(setf password-store-password-length (+ 20 (random 20)))

(defun thanos/pass-launcher ()
  "Launch Emacs as a front-end for pass."
  (interactive)
  (cl-flet ((pass-autotype (entry)
	      (let ((user (password-store-get-field entry "user"))
		    (pass (password-store-get entry)))
		(start-process-shell-command
		 "xdotool" nil
		 (if user
		     (format "sleep 0.3 && xdotool getactivewindow type \"%s\" && xdotool getactivewindow key Tab && xdotool getactivewindow type \"%s\"" user pass)
		   (format "sleep 0.3 && xdotool getactivewindow type 'thanosapollo' && xdotool getactivewindow key Tab && xdotool getactivewindow type \"%s\"" pass))))))
    (let ((ivy-height 100))
      (unwind-protect
	  (with-selected-frame
	      (make-frame '((name . "thanos/pass-launcher")
			    (minibuffer . only)
			    (fullscreen . 0)
			    (undecorated . t)
			    (internal-border-width . 10)
			    (width . 80)
			    (height . 11)))
	    (let* ((choice (completing-read "Choose an action: " '("AUTO" "COPY PASS" "COPY USERNAME" "EDIT" "GENERATE")))
		   (action (pcase choice
			     ("AUTO" #'pass-autotype)
			     ("COPY PASS" #'password-store-copy)
			     ("COPY USERNAME" #'(lambda (entry) (password-store-copy-field entry "user")))
			     ("EDIT" #'password-store-edit)
			     ("GENERATE" #'password-store-generate))))
	      (funcall action (completing-read "Search: " (password-store-list)))
	      (delete-frame)))))))

(defun smtp-get-pass ()
  "Get password for smtp."
  (interactive)
  (password-store-copy-field "fastmail.com/[email protected]" "smtp"))

(define-prefix-command 'thanos/pass)
(global-set-key (kbd "C-c p") 'thanos/pass)
(define-key thanos/pass (kbd "i") 'password-store-insert)
(define-key thanos/pass (kbd "e") 'password-store-edit)
(define-key thanos/pass (kbd "g") 'password-store-generate)
(define-key thanos/pass (kbd "s") 'smtp-get-pass)

(defun thanos/app-launcher ()
  "Launch Emacs as an Application Launcher."
  (interactive)
  (let ((ivy-height 100))
    (unwind-protect
	(with-selected-frame
	    (make-frame '((name . "thanos/emacs-launcher")
			  (minibuffer . only)
			  (fullscreen . 0)
			  (undecorated . t)
			  (internal-border-width . 10)
			  (width . 80)
			  (height . 11)))
	  (counsel-linux-app)
	  (delete-frame)))))

(provide 'thanos-pass)
;;; thanos-pass.el ends here