From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 27980 invoked from network); 18 Dec 2023 17:02:54 -0000 Received: from mx1.math.uh.edu (129.7.128.32) by inbox.vuxu.org with ESMTPUTF8; 18 Dec 2023 17:02:54 -0000 Received: from lists1.math.uh.edu ([129.7.128.208]) by mx1.math.uh.edu with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rEz9s-003XLs-1U for ml@inbox.vuxu.org; Sun, 17 Dec 2023 15:58:50 -0600 Received: from lists1.math.uh.edu ([127.0.0.1] helo=lists.math.uh.edu) by lists1.math.uh.edu with smtp (Exim 4.96.2) (envelope-from ) id 1rEz9s-002RvO-09 for ml@inbox.vuxu.org; Sun, 17 Dec 2023 15:58:48 -0600 Received: from mx2.math.uh.edu ([129.7.128.33]) by lists1.math.uh.edu with esmtp (Exim 4.96.2) (envelope-from ) id 1rEz9k-002RvF-0Z for ding@lists.math.uh.edu; Sun, 17 Dec 2023 15:58:45 -0600 Received: from quimby.gnus.org ([95.216.78.240]) by mx2.math.uh.edu with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rEz9Y-006LFG-0d for ding@lists.math.uh.edu; Sun, 17 Dec 2023 15:58:35 -0600 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnus.org; s=20200322; h=Content-Type:Mime-Version:Message-ID:Sender:Date:Subject:From: To:Reply-To:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=mVy/PEvAHgjlPKOLJGshmfwJkNXXsKScuQi081pzNuY=; b=Cx/96HWWSeTSMwmQF96bekBWc1 qcUq/KWx9jFzIsq0RCOhMXN6G6aVKMTAqtJS/3vxV/1pCEND0MOArsJJqa74zkpOir17X+3dyVZwi IOkpBYKbtCgGc1HbFVRW77YHZOe7tWX+wSe1CCBJuvBvC7zU1NZduaUzVaSZ3o55V7Xw=; Received: from ciao.gmane.io ([116.202.254.214]) by quimby.gnus.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rEz9Q-0005vb-CD for ding@gnus.org; Sun, 17 Dec 2023 22:58:23 +0100 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1rEz9N-0005OG-Pf for ding@gnus.org; Sun, 17 Dec 2023 22:58:17 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: ding@gnus.org From: =?utf-8?Q?Adam_Sj=C3=B8gren?= Subject: [PATCH] Keep IMAP connection open when used in mail-sources Date: Sun, 17 Dec 2023 22:58:11 +0100 Organization: koldfront - analysis & revolution, Copenhagen, Denmark Message-ID: <87il4wbifg.fsf@tullinup.koldfront.dk> Mime-Version: 1.0 Content-Type: text/plain User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:5yFEZvffmaq2a8kTxCE3Nh3IULU= OpenPGP: id=476630590A231909B0A0961A49D0746121BDE416; url=https://asjo.koldfront.dk/gpg.asc Mail-Follow-Up-To: never X-Now-Playing: Painted Eyelids, Live at the Washington Olympia (Beck) X-Face: )qY&CseJ?.:=8F#^~GcSA?F=9eu'{KAFfL1C3/A&:nE?PW\i65"ba0NS)97,Q(^@xk}n4Ou rPuR#V8I(J_@~H($[ym:`K_+]*kjvW>xH5jbgLBVFGXY:(#4P>zVBklLbdL&XxL\M)%T}3S/IS9lMJ ^St'=VZBR Precedence: bulk * lisp/gnus/mail-source.el (mail-source-fetch-imap): don't close process buffer and only open if it isn't already. Name it per user, server, port. --- Hi, If you are using IMAP as POP, ie you have imap entries in your mail-sources, this patch is for you. Back in January I got annoyed by Gnus closing down the IMAP-connection after every 'g', and opening it on the next (I may be using 'g' a little too much), so I made this change to keep the process buffer open and around after fetching mail - and I have been using it ever since. I only have one IMAP entry in my mail-sources, so while this change has been working well for almost a year, the testing has admittedly been one-sided. I hope you find it useful! Best regards, Adam lisp/gnus/mail-source.el | 137 ++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 65 deletions(-) diff --git a/lisp/gnus/mail-source.el b/lisp/gnus/mail-source.el index f870c0b8274..d3e58943f9c 100644 --- a/lisp/gnus/mail-source.el +++ b/lisp/gnus/mail-source.el @@ -1063,71 +1063,78 @@ mail-source-fetch-imap prescript-delay) (let ((from (format "%s:%s:%s" server user port)) (found 0) - (buf (generate-new-buffer " *imap source*")) - (imap-shell-program (or (list program) imap-shell-program))) - (if (and (imap-open server port stream authentication buf) - (imap-authenticate - user (or (cdr (assoc from mail-source-password-cache)) - password) - buf)) - (let ((mailbox-list (if (listp mailbox) mailbox (list mailbox)))) - (dolist (mailbox mailbox-list) - (when (imap-mailbox-select mailbox nil buf) - (let ((coding-system-for-write - mail-source-imap-file-coding-system) - (mail-source-string (format "imap:%s:%s" server mailbox)) - str remove) - (message "Fetching from %s..." mailbox) - (with-temp-file mail-source-crash-box - ;; Avoid converting 8-bit chars from inserted strings to - ;; multibyte. - (mm-disable-multibyte) - ;; remember password - (with-current-buffer buf - (when (and imap-password - (not (member (cons from imap-password) - mail-source-password-cache))) - (push (cons from imap-password) mail-source-password-cache))) - ;; if predicate is nil, use all uids - (dolist (uid (imap-search (or predicate "1:*") buf)) - (when (setq str - (if (imap-capability 'IMAP4rev1 buf) - (caddar (imap-fetch uid "BODY.PEEK[]" - 'BODYDETAIL nil buf)) - (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))) - (push uid remove) - (insert "From imap " (current-time-string) "\n") - (save-excursion - (insert str "\n\n")) - (while (let ((case-fold-search nil)) - (re-search-forward "^From " nil t)) - (replace-match ">From ")) - (goto-char (point-max)))) - (nnheader-ms-strip-cr)) - (cl-incf found (mail-source-callback callback server)) - (mail-source-delete-crash-box) - (when (and remove fetchflag) - (setq remove (nreverse remove)) - (imap-message-flags-add - (imap-range-to-message-set (gnus-compress-sequence remove)) - fetchflag nil buf)) - (if dontexpunge - (imap-mailbox-unselect buf) - (imap-mailbox-close nil buf))))) - (imap-close buf)) - (imap-close buf) - ;; We nix out the password in case the error - ;; was because of a wrong password being given. - (setq mail-source-password-cache - (delq (assoc from mail-source-password-cache) - mail-source-password-cache)) - (error "IMAP error: %s" (imap-error-text buf))) - (kill-buffer buf) - (mail-source-run-script - postscript - `((?p . ,password) (?t . ,mail-source-crash-box) - (?s . ,server) (?P . ,port) (?u . ,user))) - found))) + (buffer-name (format " *imap source %s@%s:%s" user server (or port "default"))) + (imap-shell-program (or (list program) imap-shell-program))) + (let ((buf (or (and (or (get-buffer-process buffer-name) + (and (get-buffer buffer-name) + (kill-buffer buffer-name))) + (get-buffer buffer-name)) + (let ((newbuf (generate-new-buffer buffer-name))) + (if (and (imap-open server port stream authentication newbuf) + (imap-authenticate + user (or (cdr (assoc from mail-source-password-cache)) + password) + newbuf)) + (progn + (set-process-query-on-exit-flag (get-buffer-process newbuf) nil) + newbuf) + (progn + (imap-close newbuf) + ;; We nix out the password in case the error + ;; was because of a wrong password being given. + (setq mail-source-password-cache + (delq (assoc from mail-source-password-cache) + mail-source-password-cache)) + (error "IMAP error: %s" (imap-error-text newbuf)))))))) + (let ((mailbox-list (if (listp mailbox) mailbox (list mailbox)))) + (dolist (mailbox mailbox-list) + (when (imap-mailbox-select mailbox nil buf) + (let ((coding-system-for-write + mail-source-imap-file-coding-system) + (mail-source-string (format "imap:%s:%s" server mailbox)) + str remove) + (message "Fetching from %s..." mailbox) + (with-temp-file mail-source-crash-box + ;; Avoid converting 8-bit chars from inserted strings to + ;; multibyte. + (mm-disable-multibyte) + ;; remember password + (with-current-buffer buf + (when (and imap-password + (not (member (cons from imap-password) + mail-source-password-cache))) + (push (cons from imap-password) mail-source-password-cache))) + ;; if predicate is nil, use all uids + (dolist (uid (imap-search (or predicate "1:*") buf)) + (when (setq str + (if (imap-capability 'IMAP4rev1 buf) + (caddar (imap-fetch uid "BODY.PEEK[]" + 'BODYDETAIL nil buf)) + (imap-fetch uid "RFC822.PEEK" 'RFC822 nil buf))) + (push uid remove) + (insert "From imap " (current-time-string) "\n") + (save-excursion + (insert str "\n\n")) + (while (let ((case-fold-search nil)) + (re-search-forward "^From " nil t)) + (replace-match ">From ")) + (goto-char (point-max)))) + (nnheader-ms-strip-cr)) + (cl-incf found (mail-source-callback callback server)) + (mail-source-delete-crash-box) + (when (and remove fetchflag) + (setq remove (nreverse remove)) + (imap-message-flags-add + (imap-range-to-message-set (gnus-compress-sequence remove)) + fetchflag nil buf)) + (if dontexpunge + (imap-mailbox-unselect buf) + (imap-mailbox-close nil buf)))))) + (mail-source-run-script + postscript + `((?p . ,password) (?t . ,mail-source-crash-box) + (?s . ,server) (?P . ,port) (?u . ,user))) + found)))) (provide 'mail-source) -- 2.40.1