This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathweechat-secrets.el
97 lines (78 loc) · 3.47 KB
/
weechat-secrets.el
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
96
97
;;; weechat --- Support secrets.el -*- lexical-binding: t -*-
;; Copyright (C) 2013 Rüdiger Sonderfeld
;; Author: Moritz Ulrich <[email protected]>
;; Rüdiger Sonderfeld <[email protected]>
;; Aristid Breitkreuz <[email protected]>
;; Keywords: irc chat network weechat
;; URL: https://github.com/the-kenny/weechat.el
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; secrets.el support for weechat.el
;; Load this file and then set `weechat-password-callback' to
;; `weechat-secrets-get-password'. You can use
;; `weechat-secrets-create' to create new entries and
;; `weechat-secrets-delete' to delete them.
;;; Code:
(require 'weechat)
(require 'secrets)
(defgroup weechat-secrets nil
"Secrets.el support for WeeChat."
:link '(url-link "https://github.com/the-kenny/weechat.el")
:prefix "weechat-secrets"
:group 'weechat)
(defcustom weechat-secrets-collection "weechat.el"
"Collection name."
:type 'string
:group 'weechat-secrets)
(defun weechat-secrets--to-item (host port)
"Convert HOST and PORT to an item name."
(format "%s:%s" host port))
(defun weechat-secrets-create (host port &optional password)
"Associate HOST and PORT with PASSWORD.
A collection named after `weechat-secrets-collection' is created if required."
(interactive
(list
(read-string
(format "Host for password (default '%s'): " weechat-host-default)
nil nil weechat-host-default)
(read-number "Port for password: " weechat-port-default)))
(unless secrets-enabled
(error "Secrets.el-API not available."))
(unless (member weechat-secrets-collection (secrets-list-collections))
(secrets-create-collection weechat-secrets-collection))
(unless password
(setq password (read-passwd "Password: " 'confirm)))
(secrets-create-item weechat-secrets-collection
(weechat-secrets--to-item host port) password
:host host :port (number-to-string port))
(clear-string password))
(defun weechat-secrets-delete (host port &optional allow-empty-collection)
"Delete HOST and PORT entry.
Unless ALLOW-EMPTY-COLLECTION is non-nil then the collection is removed
if it is empty."
(interactive
(list
(read-string
(format "Host for password (default '%s'): " weechat-host-default)
nil nil weechat-host-default)
(read-number "Port for password: " weechat-port-default)))
(let ((item (weechat-secrets--to-item host port)))
(secrets-delete-item weechat-secrets-collection item)
(unless (and allow-empty-collection
(null (secrets-list-items weechat-secrets-collection)))
(secrets-delete-collection weechat-secrets-collection))))
(defun weechat-secrets-get-password (host port)
"Get password for HOST and PORT."
(secrets-get-secret weechat-secrets-collection
(weechat-secrets--to-item host port)))
(provide 'weechat-secrets)
;;; weechat-secrets.el ends here