/
fetchmail-ctl.el
169 lines (148 loc) · 6.32 KB
/
fetchmail-ctl.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; fetchmail-ctl --- Simple code for controlling Fetchmail
;; Copyright (C) 2011 John Wiegley
;; Author: John Wiegley <jwiegley@gmail.com>
;; Created: 29 Sep 2011
;; Version: 1.0
;; Keywords: gnus imap mail message fetchmail
;; X-URL: https://github.com/jwiegley/dot-emacs
;; 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 2, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Unlike fetchmail.el, which does a lot more, this module provides very
;; simple control of Fetchmail and Leafnode's fetchnews behind a 'v' keymap
;; in the Gnus *Group* buffer.
(require 'cl)
(defgroup fetchmail-ctl nil
"Simple code for controlling Fetchmail."
:group 'gnus)
(defun process-running-p (name)
(catch 'proc-running
(ignore
(dolist (proc (process-list))
(if (string= name (process-name proc))
(if (eq 'run (process-status proc))
(throw 'proc-running proc)
(throw 'proc-running nil)))))))
(defun start-fetchmail (&optional passwd name once &rest extra-args)
(interactive)
(let ((procname (or name "*fetchmail*")))
(unless (process-running-p procname)
(message "Starting Fetchmail...")
(let ((buf (get-buffer-create procname))
(args (copy-list extra-args)))
(unless once (nconc args '("-d" "900")))
(let ((fetchmail-process
(apply #'start-process procname buf
"fetchmail" "-n" "-N" args)))
(sleep-for 3 0)
(process-send-string fetchmail-process passwd)
(process-send-string fetchmail-process "\n")))
(message "Starting Fetchmail...done"))))
(defun safely-kill-process (name &optional signal verb)
(let ((proc (process-running-p name)))
(when proc
(message "%s process %s..." (or verb "Killing") name)
(signal-process proc (or signal 'SIGHUP))
(sit-for 0.5)
(if (eq 'run (process-status proc))
(display-buffer (process-buffer proc)))
(if (null signal)
(while (eq 'run (process-status proc))
(sit-for 1)))
(message "%s process %s...done" (or verb "Killing") name))))
(defun shutdown-fetchmail ()
(interactive)
(safely-kill-process "*fetchmail*")
(safely-kill-process "*fetchmail-lists*")
;; (safely-kill-process "*fetchnews*")
)
(defun kick-fetchmail ()
(interactive)
(safely-kill-process "*fetchmail*" 'SIGUSR1 "Kicking")
(safely-kill-process "*fetchmail-lists*" 'SIGUSR1 "Kicking"))
(defun get-buffer-or-call-func (name func)
(let ((buf (get-buffer name)))
(or buf
(progn
(save-window-excursion
(save-excursion
(funcall func)))
(get-buffer name)))))
(defun get-buffer-and-call-func (name func)
(let ((buf (get-buffer name)))
(if (and buf
(or (not (get-buffer-process buf))
(not (eq 'run (process-status (get-buffer-process buf))))))
(kill-buffer buf))
(get-buffer-or-call-func name func)))
(defun switch-to-fetchmail ()
(interactive)
(let ((fetchmail-buf
(get-buffer-or-call-func
"*fetchmail*"
(function
(lambda ()
(let* ((config-dir (expand-file-name "~/.config/fetchmail"))
(cache-dir (expand-file-name "~/.cache/fetchmail"))
(config-file (expand-file-name "config" config-dir))
(config-copy (make-temp-file "fm-config")))
(unless (file-directory-p cache-dir)
(make-directory cache-dir t))
(call-process "/bin/cp" nil nil nil "-pL" config-file config-copy)
(chmod config-copy #o600)
(start-fetchmail
(lookup-password "imap.fastmail.com" "johnw@newartisans.com" 993)
"*fetchmail*" nil "--idle"
"--pidfile" (expand-file-name "pid" cache-dir)
"-f" config-copy))))))
(fetchmail-lists-buf
(get-buffer-or-call-func
"*fetchmail-lists*"
(function
(lambda ()
(let* ((process-environment (copy-alist process-environment))
(config-dir (expand-file-name "~/.config/fetchmail"))
(cache-dir (expand-file-name "~/.cache/fetchmail"))
(config-file (expand-file-name "config-lists" config-dir))
(config-copy (make-temp-file "fm-config-lists")))
(unless (file-directory-p cache-dir)
(make-directory cache-dir t))
(setenv "FETCHMAILHOME" (expand-file-name "~/Messages/Newsdir"))
(call-process "/bin/cp" nil nil nil "-pL" config-file config-copy)
(chmod config-copy #o600)
(start-fetchmail
(lookup-password "imap.fastmail.com" "johnw@newartisans.com" 993)
"*fetchmail-lists*" nil
"--pidfile" (expand-file-name "lists.pid" cache-dir)
"-f" config-copy))))))
;; (fetchnews-buf
;; (get-buffer-or-call-func
;; "*fetchnews*"
;; (function
;; (lambda ()
;; (start-process "*fetchnews*"
;; (get-buffer-create "*fetchnews*")
;; (executable-find "fetchnews")
;; "-d" (expand-file-name "~/Messages/Newsdir")
;; "-F" (expand-file-name "~/Messages/leafnode/config")
;; "-vvv" "-n")))))
)
(display-buffer fetchmail-buf)))
(add-hook 'gnus-after-exiting-gnus-hook 'shutdown-fetchmail)
;; (defun fetchnews-post ()
;; (interactive)
;; (async-shell-command
;; (format "fetchnews -F %s -vv -n -P"
;; (expand-file-name "~/Messages/leafnode/config"))))
(provide 'fetchmail-ctl)
;;; fetchmail-ctl.el ends here