Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 648298f095
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 162 lines (140 sloc) 5.821 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
;;; 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.

(defgroup fetchmail-ctl nil
  "Simple code for controlling Fetchmail."
  :group 'gnus)

(defvar fetchmail-process nil)

(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 name &rest extra-args)
  (interactive)
  (let ((procname (or name "*fetchmail*")))
    (unless (process-running-p procname)
      (message "Starting Fetchmail...")
      (let ((buf (get-buffer-create procname)))
        (setq fetchmail-process
              (apply #'start-process procname buf
                     "/opt/local/bin/fetchmail" "-d" "900" "-N" extra-args)))
      (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 "*fetchmail-spam*")
  (safely-kill-process "*fetchnews*"))

(defun kick-fetchmail ()
  (interactive)
  (safely-kill-process "*fetchmail*" 'SIGUSR1 "Kicking")
  (safely-kill-process "*fetchmail-lists*" 'SIGUSR1 "Kicking")
  (safely-kill-process "*fetchmail-spam*" '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 switch-to-fetchmail ()
  (interactive)
  (let ((fetchmail-buf
         (get-buffer-or-call-func
          "*fetchmail*"
          (function
           (lambda ()
             (start-fetchmail "*fetchmail*" "--idle")))))
        (fetchmail-lists-buf
         (get-buffer-or-call-func
          "*fetchmail-lists*"
          (function
           (lambda ()
             (let ((process-environment (copy-alist process-environment)))
               (setenv "FETCHMAILHOME" (expand-file-name "~/Messages/Newsdir"))
               (start-fetchmail "*fetchmail-lists*"
                                "-f" (expand-file-name
                                      "~/Messages/fetchmailrc.lists")))))))
        (fetchmail-spam-buf
         (get-buffer-or-call-func
          "*fetchmail-spam*"
          (function
           (lambda ()
             (let ((process-environment (copy-alist process-environment)))
               (setenv "FETCHMAILHOME" (expand-file-name "~/Messages/Maildir"))
               (start-fetchmail "*fetchmail-spam*"
                                "-f" (expand-file-name
                                      "~/Messages/fetchmailrc.spam")))))))
        (fetchnews-buf
         (get-buffer-or-call-func
          "*fetchnews*"
          (function
           (lambda ()
             (start-process "*fetchnews*"
                            (get-buffer-create "*fetchnews*")
                            (executable-find "fetchnews") "-vvv" "-n")))))
        (cur-buf (current-buffer)))
    (delete-other-windows)
    (flet ((switch-in-other-buffer
            (buf)
            (when buf
              (split-window-vertically)
              (balance-windows)
              (switch-to-buffer-other-window buf))))
      (switch-to-buffer cur-buf)
      (switch-in-other-buffer fetchmail-buf)
      (switch-in-other-buffer fetchmail-lists-buf)
      (switch-in-other-buffer fetchmail-spam-buf)
      (switch-in-other-buffer fetchnews-buf)
      (select-window (get-buffer-window cur-buf))
      (balance-windows))))

(add-hook 'gnus-after-exiting-gnus-hook 'shutdown-fetchmail)

(defun fetchnews-post ()
  (interactive)
  (async-shell-command "fetchnews -vv -n -P"))

(eval-after-load "gnus-group"
  '(progn
     (define-key gnus-group-mode-map [?v ?b] 'switch-to-fetchmail)
     (define-key gnus-group-mode-map [?v ?o] 'start-fetchmail)
     (define-key gnus-group-mode-map [?v ?d] 'shutdown-fetchmail)
     (define-key gnus-group-mode-map [?v ?k] 'kick-fetchmail)
     (define-key gnus-group-mode-map [?v ?p] 'fetchnews-post)))

(provide 'fetchmail-ctl)

;;; fetchmail-ctl.el ends here
Something went wrong with that request. Please try again.