-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathgnus-gmail-prepare.el
More file actions
143 lines (115 loc) · 4.3 KB
/
Copy pathgnus-gmail-prepare.el
File metadata and controls
143 lines (115 loc) · 4.3 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
;;; Gnus Setup For GMail imap: -*- lexical-binding: t; -*-
;; Read GMailusing gnus with 2-factor (Oauth2) authentication.
;; Uses auth-source-xoauth2:
;; https://github.com/ccrusius/auth-source-xoauth2
;; That module extends Emacs' auth-source with xoauth2 support.
;; This module sets things up for GMail.
;;Using a file-based creds store.
;;; specials:
(require 'cl-lib)
(cl-declaim (special emacspeak-z-keymap gnus-summary-mode-map
emacspeak-y-keymap smtpmail-auth-supported
gnus-auto-subscribed-groups smtpmail-smtp-service
smtpmail-smtp-server smtpmail-stream-type
s smtpmail-smtp-user
mm-file-name-rewrite-functions smtpmail-auth-supported))
;;; Requires:
(with-eval-after-load "gnus"
(require 'cl-lib)
(require 'auth-source-xoauth2)
(require 'smtpmail)
(defconst auth-source-xoauth2-creds
(expand-file-name "~/.xoauth2-creds.gpg")
"Where we store our tokens.
This file should be GPG encrypted --- Emacs will decrypt on load.")
(auth-source-xoauth2-enable)
(add-to-list 'smtpmail-auth-supported 'xoauth2)
;;; Tests:
;; (auth-source-xoauth2--search nil nil "gmail" "[email protected]""993")
;; (auth-source-search :host "smtp.gmail.com"
;; :user "[email protected]" :type 'xoauth2 :max 1 :port "465")
;;; Sending Mail:
(setq
send-mail-function 'smtpmail-send-it
;smtpmail-debug-info t
;smtpmail-debug-verb t
smtpmail-stream-type 'ssl
smtpmail-smtp-user "[email protected]"
smtpmail-smtp-server "smtp.gmail.com"
smtpmail-smtp-service 465)
;;; GMail Using xoauth2 and Gnus:
(cl-declaim (special gnus-select-method gnus-secondary-select-methods))
(setq
gnus-select-method
`(nnimap
"gmail"
(nnimap-address "imap.gmail.com")
(nnimap-server-port 993)
(nnimap-user "[email protected]")
(nnimap-authenticator xoauth2)
(nnimap-expunge always)
(nnmail-expiry-wait immediate)
(nnimap-streaming t)
(nnimap-stream ssl)))
(defun gm-user-to-nnimap (user)
"Return nnimap select method for sspecified user."
`(nnimap
,user
(nnimap-user ,(format "%[email protected]" user))
(nnimap-authenticator xoauth2)
(nnimap-address "imap.gmail.com")
(nnimap-server-port 993)
(nnmail-expiry-wait immediate)
(nnimap-streaming t)
(nnimap-stream ssl)))
(setq gnus-secondary-select-methods
(mapcar #'gm-user-to-nnimap
'( "tv.raman.tv" "emacspeak")))
;;; Additional gnus settings:
(setq gnus-auto-subscribed-groups nil)
(defun gmail-report-spam ()
"Report the current or marked mails as spam.
This moves them into the Spam folder."
(interactive)
(make-thread
#'(lambda ()
(gnus-summary-move-article nil "nnimap+imap.gmail.com:[Gmail]/Spam")
(emacspeak-icon 'task-done))))
(defun gmail-unspam ()
"Move incorrectly marked spam to inbox"
(interactive)
(make-thread
#'(lambda ()
(gnus-summary-move-article nil "nnimap+imap.gmail.com:[Gmail]/inbox")
(emacspeak-icon 'task-done))))
(define-key gnus-summary-mode-map "$" 'gmail-report-spam)
(defun tvr-unlock-xoauth ()
"Unlock xoauth creds if gpg-agent has timed out."
(interactive )
(cl-declare (special auth-source-xoauth2-creds))
(kill-buffer (find-file-noselect auth-source-xoauth2-creds))
(dtk-stop)
(emacspeak-icon 'task-done))
(when (keymapp emacspeak-z-keymap )
(define-key emacspeak-z-keymap "u" 'tvr-unlock-xoauth))
(setq mm-file-name-rewrite-functions
'(mm-file-name-trim-whitespace
mm-file-name-collapse-whitespace
mm-file-name-replace-whitespace))
;;; Utils:
(defun google-py-oauth2-cli (user app-secret)
"generate command-line for pasting into a shell.
Uses the go oauth tool found in the xoauth git repo."
(kill-new
(format
"oauth --user %s --client_id %s --client_secret %s --generate_oauth2_token"
user
(plist-get app-secret :client-id)
(plist-get app-secret :client-secret))))
;;; Usage:
;;(google-py-oauth2-cli "[email protected]" file-app-secrets)
;;(google-py-oauth2-cli "[email protected]" file-app-secrets)
)
;; local variables:
;; folded-file: t
;; end: