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=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 24179 invoked from network); 10 Jan 2023 20:10:23 -0000 Received: from lists.gnu.org (209.51.188.17) by inbox.vuxu.org with ESMTPUTF8; 10 Jan 2023 20:10:23 -0000 Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pFKwi-0002Fq-RK; Tue, 10 Jan 2023 15:10:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pFKwh-0002FS-8o for info-gnus-english@gnu.org; Tue, 10 Jan 2023 15:10:07 -0500 Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pFKwf-00005Z-5G for info-gnus-english@gnu.org; Tue, 10 Jan 2023 15:10:06 -0500 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1pFKwb-0004Of-P4 for info-gnus-english@gnu.org; Tue, 10 Jan 2023 21:10:01 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: info-gnus-english@gnu.org To: info-gnus-english@gnu.org From: Emanuel Berg Subject: Re: Keeping IMAP connection alive when using it in mail-sources Date: Tue, 10 Jan 2023 21:08:25 +0100 Message-ID: <87y1qab1ae.fsf@dataswamp.org> References: <87cz7oj3s2.fsf@ericabrahamsen.net> <878riaw8vt.fsf@tullinup.koldfront.dk> <874jsyw5uj.fsf@tullinup.koldfront.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:f4FFf+TGyIFivHehanaCrlZu+Zs= Mail-Copies-To: never Received-SPF: pass client-ip=116.202.254.214; envelope-from=gegu-info-gnus-english@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: info-gnus-english@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Announcements and discussions for GNUS, the GNU Emacs Usenet newsreader \(in English\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: info-gnus-english-bounces+ml=inbox.vuxu.org@gnu.org Sender: info-gnus-english-bounces+ml=inbox.vuxu.org@gnu.org Adam Sjøgren wrote: > Here's the modified version of mail-source-fetch-imap > I cobbled together - the handling of deleting the buffer if > there is no process is not so pretty: > > (defun mail-source-fetch-imap (source callback) > "Fetcher for imap sources." > (mail-source-bind (imap source) > (mail-source-run-script > prescript > `((?p . ,password) (?t . ,mail-source-crash-box) > (?s . ,server) (?P . ,port) (?u . ,user)) > prescript-delay) > (let ((from (format "%s:%s:%s" server user port)) > (found 0) > (imap-shell-program (or (list program) imap-shell-program))) > (let ((buf (or (and (or (get-buffer-process " *imap source*") > (and (get-buffer " *imap source*") > (kill-buffer " *imap source*"))) > (get-buffer " *imap source*")) > (let ((newbuf (generate-new-buffer " *imap source*"))) > (if (and (imap-open server port stream authentication newbuf) > (imap-authenticate > user (or (cdr (assoc from mail-source-password-cache)) > password) > newbuf)) > 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)))) Good work but consider writing longer functions? -- underground experts united https://dataswamp.org/~incal