1 ;;; elmo-pop3.el -- POP3 Interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1999,2000 Kenichi OKADA <okada@opaopa.org>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Kenichi OKADA <okada@opaopa.org>
8 ;; Keywords: mail, net news
10 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
41 (autoload 'md5 "md5"))
43 (defcustom elmo-pop3-default-use-uidl t
44 "If non-nil, use UIDL on POP3."
48 (defvar elmo-pop3-use-uidl-internal t
49 "(Internal switch for using UIDL on POP3).")
51 (defvar elmo-pop3-use-cache t
52 "Use cache in pop3 folder.")
54 (defvar elmo-pop3-send-command-synchronously nil
55 "If non-nil, commands are send synchronously.
56 If server doesn't accept asynchronous commands, this variable should be
59 (defvar elmo-pop3-exists-exactly t)
60 (defvar sasl-mechanism-alist)
62 (defvar elmo-pop3-total-size nil)
65 (defvar elmo-pop3-debug nil
66 "Non-nil forces POP3 folder as debug mode.
67 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
69 (defvar elmo-pop3-debug-inhibit-logging nil)
72 (defsubst elmo-pop3-debug (message &rest args)
74 (with-current-buffer (get-buffer-create "*POP3 DEBUG*")
75 (goto-char (point-max))
76 (if elmo-pop3-debug-inhibit-logging
77 (insert "NO LOGGING\n")
78 (insert (apply 'format message args) "\n")))))
82 (luna-define-class elmo-pop3-folder (elmo-net-folder)
83 (use-uidl location-alist))
84 (luna-define-internal-accessors 'elmo-pop3-folder))
86 (luna-define-method elmo-folder-initialize :around ((folder
89 (let ((elmo-network-stream-type-alist
90 (if elmo-pop3-stream-type-alist
91 (append elmo-pop3-stream-type-alist
92 elmo-network-stream-type-alist)
93 elmo-network-stream-type-alist)))
94 (setq name (luna-call-next-method))
95 ;; Setup slots for elmo-net-folder
96 (when (string-match "^\\([^:/!]*\\)\\(/[^/:@!]+\\)?\\(:[^/:@!]+\\)?" name)
97 (elmo-net-folder-set-user-internal folder
98 (if (match-beginning 1)
99 (elmo-match-string 1 name)))
100 (if (eq (length (elmo-net-folder-user-internal folder)) 0)
101 (elmo-net-folder-set-user-internal folder
102 elmo-pop3-default-user))
103 (elmo-net-folder-set-auth-internal
105 (if (match-beginning 2)
106 (intern (elmo-match-substring 2 name 1))
107 elmo-pop3-default-authenticate-type))
108 (elmo-pop3-folder-set-use-uidl-internal
110 (if (match-beginning 3)
111 (string= (elmo-match-substring 3 name 1) "uidl")
112 elmo-pop3-default-use-uidl)))
113 (unless (elmo-net-folder-server-internal folder)
114 (elmo-net-folder-set-server-internal folder
115 elmo-pop3-default-server))
116 (unless (elmo-net-folder-port-internal folder)
117 (elmo-net-folder-set-port-internal folder
118 elmo-pop3-default-port))
119 (unless (elmo-net-folder-stream-type-internal folder)
120 (elmo-net-folder-set-stream-type-internal
122 (elmo-get-network-stream-type
123 elmo-pop3-default-stream-type)))
127 (luna-define-class elmo-pop3-session (elmo-network-session) ())
130 (defvar elmo-pop3-read-point nil)
131 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
132 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
133 (defvar elmo-pop3-size-hash nil) ; number -> size
134 (defvar elmo-pop3-uidl-done nil)
135 (defvar elmo-pop3-list-done nil)
136 (defvar elmo-pop3-lock nil)
138 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
139 elmo-pop3-uidl-number-hash
140 elmo-pop3-number-uidl-hash
146 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
147 (when (elmo-network-session-process-internal session)
148 (when (memq (process-status
149 (elmo-network-session-process-internal session))
151 (let ((buffer (process-buffer
152 (elmo-network-session-process-internal session))))
153 (elmo-pop3-send-command (elmo-network-session-process-internal session)
156 (or (elmo-pop3-read-response
157 (elmo-network-session-process-internal session)
159 (error "POP error: QUIT failed"))))
160 (kill-buffer (process-buffer
161 (elmo-network-session-process-internal session)))
162 (delete-process (elmo-network-session-process-internal session))))
164 (defun elmo-pop3-get-session (folder &optional if-exists)
165 (let ((elmo-pop3-use-uidl-internal (if elmo-inhibit-number-mapping
167 (elmo-pop3-folder-use-uidl-internal
169 (elmo-network-get-session 'elmo-pop3-session "POP3" folder if-exists)))
171 (defun elmo-pop3-send-command (process command &optional no-erase)
172 (with-current-buffer (process-buffer process)
175 (goto-char (point-min))
176 (setq elmo-pop3-read-point (point))
177 (elmo-pop3-debug "SEND: %s\n" command)
178 (process-send-string process command)
179 (process-send-string process "\r\n")))
181 (defun elmo-pop3-read-response (process &optional not-command)
182 ;; buffer is in case for process is dead.
183 (with-current-buffer (process-buffer process)
184 (let ((case-fold-search nil)
185 (response-string nil)
186 (response-continue t)
189 (while response-continue
190 (goto-char elmo-pop3-read-point)
191 (while (not (re-search-forward "\r?\n" nil t))
192 (accept-process-output process)
193 (goto-char elmo-pop3-read-point))
194 (setq match-end (point))
195 (setq response-string
196 (buffer-substring elmo-pop3-read-point (- match-end 2)))
197 (goto-char elmo-pop3-read-point)
198 (if (looking-at "\\+.*$")
200 (setq response-continue nil)
201 (setq elmo-pop3-read-point match-end)
204 (concat return-value "\n" response-string)
206 (if (looking-at "\\-.*$")
208 (setq response-continue nil)
209 (setq elmo-pop3-read-point match-end)
210 (setq return-value nil))
211 (setq elmo-pop3-read-point match-end)
213 (setq response-continue nil))
216 (concat return-value "\n" response-string)
218 (setq elmo-pop3-read-point match-end)))
221 (defun elmo-pop3-process-filter (process output)
223 (set-buffer (process-buffer process))
224 (goto-char (point-max))
226 (elmo-pop3-debug "RECEIVED: %s\n" output)
227 (if (and elmo-pop3-total-size
228 (> elmo-pop3-total-size
229 (min elmo-display-retrieval-progress-threshold 100)))
230 (elmo-display-progress
231 'elmo-display-retrieval-progress
232 (format "Retrieving (%d/%d bytes)..."
234 elmo-pop3-total-size)
235 (/ (buffer-size) (/ elmo-pop3-total-size 100))))))
237 (defun elmo-pop3-auth-user (session)
238 (let ((process (elmo-network-session-process-internal session)))
240 (elmo-pop3-send-command
242 (format "user %s" (elmo-network-session-user-internal session)))
243 (or (elmo-pop3-read-response process t)
244 (signal 'elmo-authenticate-error
245 '(elmo-pop-auth-user)))
246 (elmo-pop3-send-command process
250 (elmo-network-session-password-key session))))
251 (or (elmo-pop3-read-response process t)
252 (signal 'elmo-authenticate-error
253 '(elmo-pop-auth-user)))))
255 (defun elmo-pop3-auth-apop (session)
256 (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
257 (elmo-network-session-greeting-internal session))
258 ;; good, APOP ready server
260 (elmo-pop3-send-command
261 (elmo-network-session-process-internal session)
263 (elmo-network-session-user-internal session)
265 (concat (match-string
267 (elmo-network-session-greeting-internal session))
269 (elmo-network-session-password-key session))))))
270 (or (elmo-pop3-read-response
271 (elmo-network-session-process-internal session)
273 (signal 'elmo-authenticate-error
274 '(elmo-pop3-auth-apop))))
275 (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
277 (luna-define-method elmo-network-initialize-session-buffer :after
278 ((session elmo-pop3-session) buffer)
279 (with-current-buffer buffer
280 (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
282 (luna-define-method elmo-network-initialize-session ((session
284 (let ((process (elmo-network-session-process-internal session))
286 (with-current-buffer (process-buffer process)
287 (set-process-filter process 'elmo-pop3-process-filter)
288 (setq elmo-pop3-read-point (point-min))
289 ;; Skip garbage output from process before greeting.
290 (while (and (memq (process-status process) '(open run))
291 (goto-char (point-max))
293 (not (looking-at "+OK")))
294 (accept-process-output process 1))
295 (setq elmo-pop3-read-point (point))
296 (or (elmo-network-session-set-greeting-internal
298 (elmo-pop3-read-response process t))
299 (signal 'elmo-open-error
300 '(elmo-network-intialize-session)))
301 (when (eq (elmo-network-stream-type-symbol
302 (elmo-network-session-stream-type-internal session))
304 (elmo-pop3-send-command process "stls")
305 (if (string-match "^\+OK"
306 (elmo-pop3-read-response process))
307 (starttls-negotiate process)
308 (signal 'elmo-open-error
309 '(elmo-pop3-starttls-error)))))))
311 (luna-define-method elmo-network-authenticate-session ((session
313 (with-current-buffer (process-buffer
314 (elmo-network-session-process-internal session))
315 (let* ((process (elmo-network-session-process-internal session))
316 (elmo-pop3-debug-inhibit-logging t)
317 (auth (elmo-network-session-auth-internal session))
318 (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
319 (if (listp auth) auth (list auth))))
321 client name step response mechanism
322 sasl-read-passphrase)
323 (or (and (string= "USER" (car auth))
324 (elmo-pop3-auth-user session))
325 (and (string= "APOP" (car auth))
326 (elmo-pop3-auth-apop session))
329 (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
330 (setq mechanism (sasl-find-mechanism auth))
332 (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
336 (elmo-network-session-user-internal session)
338 (elmo-network-session-server-internal session)))
339 ;;; (if elmo-pop3-auth-user-realm
340 ;;; (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
341 (setq name (sasl-mechanism-name mechanism))
342 (elmo-network-session-set-auth-internal session
343 (intern (downcase name)))
344 (setq sasl-read-passphrase
348 (elmo-network-session-password-key session)))))
349 (setq step (sasl-next-step client nil))
350 (elmo-pop3-send-command
353 (and (sasl-step-data step)
356 (elmo-base64-encode-string
357 (sasl-step-data step) 'no-line-break))))) ;)
360 (unless (setq response (elmo-pop3-read-response process t))
361 ;; response is NO or BAD.
362 (signal 'elmo-authenticate-error
364 (concat "elmo-pop3-auth-"
366 (if (string-match "^\+OK" response)
367 (if (sasl-next-step client step)
369 (signal 'elmo-authenticate-error
371 (concat "elmo-pop3-auth-"
373 ;; The authentication process is finished.
377 (elmo-base64-decode-string
378 (cadr (split-string response " "))))
379 (setq step (sasl-next-step client step))
380 (elmo-pop3-send-command
382 (if (sasl-step-data step)
383 (elmo-base64-encode-string (sasl-step-data step)
387 (luna-define-method elmo-network-setup-session ((session
389 (let ((process (elmo-network-session-process-internal session))
391 (with-current-buffer (process-buffer process)
392 (setq elmo-pop3-size-hash (elmo-make-hash 31))
393 ;; To get obarray of uidl and size
394 (elmo-pop3-send-command process "list")
395 (if (null (elmo-pop3-read-response process))
396 (error "POP LIST command failed"))
397 (if (null (setq response
398 (elmo-pop3-read-contents
399 (current-buffer) process)))
400 (error "POP LIST command failed"))
401 ;; POP server always returns a sequence of serial numbers.
402 (setq count (elmo-pop3-parse-list-response response))
404 (when elmo-pop3-use-uidl-internal
405 (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
406 (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
408 (elmo-pop3-send-command process "uidl")
409 (unless (elmo-pop3-read-response process)
410 (error "POP UIDL failed"))
411 (unless (setq response (elmo-pop3-read-contents
412 (current-buffer) process))
413 (error "POP UIDL failed"))
414 (elmo-pop3-parse-uidl-response response)))))
416 (defun elmo-pop3-read-contents (buffer process)
417 (with-current-buffer buffer
418 (let ((case-fold-search nil)
420 (goto-char elmo-pop3-read-point)
421 (while (not (re-search-forward "^\\.\r\n" nil t))
422 (accept-process-output process)
423 (goto-char elmo-pop3-read-point))
424 (setq match-end (point))
426 (buffer-substring elmo-pop3-read-point
429 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-pop3-folder))
430 (convert-standard-filename
432 (elmo-safe-filename (elmo-net-folder-user-internal folder))
433 (expand-file-name (elmo-net-folder-server-internal folder)
438 (luna-define-method elmo-folder-exists-p ((folder elmo-pop3-folder))
439 (if (and elmo-pop3-exists-exactly
440 (elmo-folder-plugged-p folder))
442 (let (elmo-auto-change-plugged ; don't change plug status.
443 (elmo-inhibit-number-mapping t) ; No need to use uidl.
446 (setq session (elmo-pop3-get-session folder))
448 (elmo-network-close-session session)))))
451 (defun elmo-pop3-parse-uidl-response (string)
452 (let ((buffer (current-buffer))
455 (let (number uid list)
457 (goto-char (point-min))
458 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([^ \n]+\\)$" nil t)
459 (setq number (elmo-match-buffer 1))
460 (setq uid (elmo-match-buffer 2))
461 (with-current-buffer buffer
462 (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
463 (elmo-set-hash-val (concat "#" number) uid
464 elmo-pop3-number-uidl-hash))
465 (setq list (cons uid list)))
466 (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
469 (defun elmo-pop3-parse-list-response (string)
470 (let ((buffer (current-buffer))
475 (goto-char (point-min))
476 (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
479 (cons (elmo-match-buffer 1)
480 (elmo-match-buffer 2))
482 (setq count (1+ count)))
483 (with-current-buffer buffer
484 (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
486 (elmo-set-hash-val (concat "#" (car (car alist)))
489 (setq alist (cdr alist)))
490 (setq elmo-pop3-list-done t))
493 (defun elmo-pop3-list-location (folder)
494 (with-current-buffer (process-buffer
495 (elmo-network-session-process-internal
496 (elmo-pop3-get-session folder)))
498 (if elmo-pop3-uidl-done
502 (setq locations (cons (symbol-name atom) locations)))
503 elmo-pop3-uidl-number-hash)
506 (< (elmo-pop3-uidl-to-number loc1)
507 (elmo-pop3-uidl-to-number loc2)))))
508 (error "POP3: Error in UIDL")))))
510 (defun elmo-pop3-list-folder-by-location (folder locations)
511 (let* ((location-alist (elmo-pop3-folder-location-alist-internal folder))
512 (locations-in-db (mapcar 'cdr location-alist))
513 result new-locs new-alist deleted-locs i)
515 (elmo-delete-if (function
516 (lambda (x) (member x locations-in-db)))
519 (elmo-delete-if (function
520 (lambda (x) (member x locations)))
522 (setq i (or (elmo-max-of-list (mapcar 'car location-alist)) 0))
527 (delq (rassoc x location-alist) location-alist))))
531 (setq new-alist (cons (cons i (car new-locs)) new-alist))
532 (setq new-locs (cdr new-locs)))
533 (setq result (nconc location-alist new-alist))
534 (setq result (sort result (lambda (x y) (< (car x)(car y)))))
535 (elmo-pop3-folder-set-location-alist-internal folder result)
536 (mapcar 'car result)))
538 (defun elmo-pop3-list-by-uidl-subr (folder &optional nonsort)
539 (let ((flist (elmo-pop3-list-folder-by-location
541 (elmo-pop3-list-location folder))))
543 (cons (elmo-max-of-list flist) (length flist))
546 (defun elmo-pop3-list-by-list (folder)
547 (with-current-buffer (process-buffer
548 (elmo-network-session-process-internal
549 (elmo-pop3-get-session folder)))
551 (if elmo-pop3-list-done
553 (mapatoms (lambda (atom)
554 (setq list (cons (string-to-int
555 (substring (symbol-name atom) 1))
559 (error "POP3: Error in list")))))
561 (defsubst elmo-pop3-folder-list-messages (folder)
562 (if (and (not elmo-inhibit-number-mapping)
563 (elmo-pop3-folder-use-uidl-internal folder))
564 (elmo-pop3-list-by-uidl-subr folder)
565 (elmo-pop3-list-by-list folder)))
567 (luna-define-method elmo-folder-list-messages-internal
568 ((folder elmo-pop3-folder) &optional nohide)
569 (elmo-pop3-folder-list-messages folder))
571 (luna-define-method elmo-folder-status ((folder elmo-pop3-folder))
572 (elmo-folder-open-internal folder)
573 (elmo-folder-check folder)
574 (if (elmo-pop3-folder-use-uidl-internal folder)
576 (elmo-pop3-list-by-uidl-subr folder 'nonsort)
577 (elmo-folder-close-internal folder))
579 (elmo-network-session-process-internal
580 (elmo-pop3-get-session folder)))
583 (with-current-buffer (process-buffer process)
584 (elmo-pop3-send-command process "STAT")
585 (setq response (elmo-pop3-read-response process))
586 ;; response: "^\+OK 2 7570$"
587 (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
588 (error "POP STAT command failed")
591 (substring response (match-beginning 1)(match-end 1 ))))
592 (elmo-folder-close-internal folder)
593 (cons total total))))))
595 (defvar elmo-pop3-header-fetch-chop-length 200)
597 (defsubst elmo-pop3-next-result-arrived-p ()
599 ((eq (following-char) ?+)
600 (if (re-search-forward "\n\\.\r?\n" nil t)
604 (if (search-forward "\n" nil t)
610 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
614 (let ((number (length articles))
617 (last-point (point-min)))
618 ;; Send HEAD commands.
620 (elmo-pop3-send-command process (format
621 "top %s 0" (car articles))
623 ;;; (accept-process-output process 1)
624 (setq articles (cdr articles))
625 (setq count (1+ count))
626 ;; Every 200 requests we have to read the stream in
627 ;; order to avoid deadlocks.
628 (when (or elmo-pop3-send-command-synchronously
629 (null articles) ;All requests have been sent.
630 (zerop (% count elmo-pop3-header-fetch-chop-length)))
631 (unless elmo-pop3-send-command-synchronously
632 (accept-process-output process 1))
636 (goto-char last-point)
638 (while (elmo-pop3-next-result-arrived-p)
639 (setq last-point (point))
640 (setq received (1+ received)))
642 (when (> number elmo-display-progress-threshold)
643 (if (or (zerop (% received 5)) (= received number))
644 (elmo-display-progress
645 'elmo-pop3-retrieve-headers "Getting headers..."
646 (/ (* received 100) number))))
647 (accept-process-output process 1)
648 ;;; (accept-process-output process)
650 ;; Remove all "\r"'s.
651 (goto-char (point-min))
652 (while (search-forward "\r\n" nil t)
653 (replace-match "\n"))
654 (copy-to-buffer tobuffer (point-min) (point-max)))))
656 (luna-define-method elmo-folder-msgdb-create ((folder elmo-pop3-folder)
658 already-mark seen-mark
659 important-mark seen-list)
660 (let ((process (elmo-network-session-process-internal
661 (elmo-pop3-get-session folder))))
662 (with-current-buffer (process-buffer process)
663 (elmo-pop3-sort-msgdb-by-original-number
665 (elmo-pop3-msgdb-create-by-header
668 new-mark already-mark
670 (if (elmo-pop3-folder-use-uidl-internal folder)
671 (elmo-pop3-folder-location-alist-internal folder)))))))
673 (defun elmo-pop3-sort-overview-by-original-number (overview loc-alist)
677 (< (elmo-pop3-uidl-to-number
678 (cdr (assq (elmo-msgdb-overview-entity-get-number ent1)
680 (elmo-pop3-uidl-to-number
681 (cdr (assq (elmo-msgdb-overview-entity-get-number ent2)
685 (defun elmo-pop3-sort-msgdb-by-original-number (folder msgdb)
686 (message "Sorting...")
687 (let ((overview (elmo-msgdb-get-overview msgdb)))
688 (setq overview (elmo-pop3-sort-overview-by-original-number
690 (elmo-pop3-folder-location-alist-internal folder)))
691 (message "Sorting...done")
692 (list overview (nth 1 msgdb)(nth 2 msgdb)(nth 3 msgdb))))
694 (defun elmo-pop3-uidl-to-number (uidl)
695 (string-to-number (elmo-get-hash-val uidl
696 elmo-pop3-uidl-number-hash)))
698 (defun elmo-pop3-number-to-uidl (number)
699 (elmo-get-hash-val (format "#%d" number)
700 elmo-pop3-number-uidl-hash))
702 (defun elmo-pop3-number-to-size (number)
703 (elmo-get-hash-val (format "#%d" number)
704 elmo-pop3-size-hash))
706 (defun elmo-pop3-msgdb-create-by-header (process numlist
707 new-mark already-mark
711 (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
712 (with-current-buffer (process-buffer process)
713 (if loc-alist ; use uidl.
719 (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
721 (elmo-pop3-retrieve-headers (process-buffer process)
722 tmp-buffer process numlist)
724 (elmo-pop3-msgdb-create-message
729 new-mark already-mark seen-mark seen-list loc-alist)
730 (kill-buffer tmp-buffer)))))
732 (defun elmo-pop3-msgdb-create-message (buffer
735 numlist new-mark already-mark
740 (let (beg overview number-alist mark-alist
741 entity i number message-id gmark seen size)
743 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
744 (goto-char (point-min))
746 (message "Creating msgdb...")
748 (setq beg (save-excursion (forward-line 1) (point)))
749 (elmo-pop3-next-result-arrived-p)
753 (narrow-to-region beg (point))
755 (elmo-msgdb-create-overview-from-buffer
757 (setq numlist (cdr numlist))
760 (elmo-msgdb-append-element
762 (with-current-buffer (process-buffer process)
763 (elmo-msgdb-overview-entity-set-size
766 (elmo-pop3-number-to-size
767 (elmo-msgdb-overview-entity-get-number entity))))
771 (elmo-pop3-number-to-uidl
772 (elmo-msgdb-overview-entity-get-number entity))
774 (elmo-msgdb-overview-entity-set-number entity number)))
776 (elmo-msgdb-number-add
778 (elmo-msgdb-overview-entity-get-number entity)
780 (setq message-id (car entity))
781 (setq seen (member message-id seen-list))
782 (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
783 (if (elmo-file-cache-status
784 (elmo-file-cache-get message-id))
789 (if elmo-pop3-use-cache
793 (elmo-msgdb-mark-append
795 (elmo-msgdb-overview-entity-get-number entity)
797 (when (> num elmo-display-progress-threshold)
799 (if (or (zerop (% i 5)) (= i num))
800 (elmo-display-progress
801 'elmo-pop3-msgdb-create-message "Creating msgdb..."
802 (/ (* i 100) num)))))
803 (list overview number-alist mark-alist))))
805 (defun elmo-pop3-read-body (process outbuf)
806 (with-current-buffer (process-buffer process)
807 (let ((start elmo-pop3-read-point)
810 (while (not (re-search-forward "^\\.\r?\n" nil t))
811 (accept-process-output process)
814 (with-current-buffer outbuf
816 (insert-buffer-substring (process-buffer process) start (- end 3))))))
818 (luna-define-method elmo-folder-open-internal ((folder elmo-pop3-folder))
819 (if (and (not elmo-inhibit-number-mapping)
820 (elmo-pop3-folder-use-uidl-internal folder))
821 (elmo-pop3-folder-set-location-alist-internal
822 folder (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))))
824 (luna-define-method elmo-folder-commit :after ((folder elmo-pop3-folder))
825 (when (elmo-folder-persistent-p folder)
826 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
827 (elmo-pop3-folder-location-alist-internal
830 (luna-define-method elmo-folder-close-internal ((folder elmo-pop3-folder))
831 (elmo-pop3-folder-set-location-alist-internal folder nil)
832 ;; Just close connection
833 (elmo-folder-check folder))
835 (luna-define-method elmo-message-fetch-plugged ((folder elmo-pop3-folder)
839 (let* ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
840 (process (elmo-network-session-process-internal
841 (elmo-pop3-get-session folder)))
842 size response errmsg msg)
843 (with-current-buffer (process-buffer process)
845 (setq number (elmo-pop3-uidl-to-number
846 (cdr (assq number loc-alist)))))
847 (setq size (string-to-number
848 (elmo-pop3-number-to-size number)))
850 (elmo-pop3-send-command process
851 (format "retr %s" number))
852 (unless elmo-inhibit-display-retrieval-progress
853 (setq elmo-pop3-total-size size)
854 (elmo-display-progress
855 'elmo-display-retrieval-progress
856 (format "Retrieving (0/%d bytes)..." elmo-pop3-total-size)
860 (when (null (setq response (elmo-pop3-read-response
862 (error "Fetching message failed"))
863 (setq response (elmo-pop3-read-body process outbuf)))
864 (setq elmo-pop3-total-size nil))
865 (unless elmo-inhibit-display-retrieval-progress
866 (elmo-display-progress
867 'elmo-display-retrieval-progress "" 100) ; remove progress bar.
868 (message "Retrieving...done."))
870 (goto-char (point-min))
871 (while (re-search-forward "^\\." nil t)
876 (defun elmo-pop3-delete-msg (process number loc-alist)
877 (with-current-buffer (process-buffer process)
878 (let (response errmsg msg)
880 (setq number (elmo-pop3-uidl-to-number
881 (cdr (assq number loc-alist)))))
884 (elmo-pop3-send-command process
885 (format "dele %s" number))
886 (when (null (setq response (elmo-pop3-read-response
888 (error "Deleting message failed")))
889 (error "Deleting message failed")))))
891 (luna-define-method elmo-folder-delete-messages ((folder elmo-pop3-folder)
893 (let ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
894 (process (elmo-network-session-process-internal
895 (elmo-pop3-get-session folder))))
896 (mapcar '(lambda (msg) (elmo-pop3-delete-msg
897 process msg loc-alist))
900 (luna-define-method elmo-message-use-cache-p ((folder elmo-pop3-folder) number)
903 (luna-define-method elmo-folder-persistent-p ((folder elmo-pop3-folder))
904 (and (elmo-folder-persistent-internal folder)
905 (elmo-pop3-folder-use-uidl-internal folder)))
907 (luna-define-method elmo-folder-clear :around ((folder elmo-pop3-folder)
908 &optional keep-killed)
910 (elmo-pop3-folder-set-location-alist-internal folder nil))
911 (luna-call-next-method))
913 (luna-define-method elmo-folder-check ((folder elmo-pop3-folder))
914 (if (elmo-folder-plugged-p folder)
915 (let ((session (elmo-pop3-get-session folder 'if-exists)))
917 (elmo-network-close-session session)))))
920 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
922 ;;; elmo-pop3.el ends here