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"))
44 (defcustom elmo-pop3-default-user (or (getenv "USER")
47 "*Default username for POP3."
51 (defcustom elmo-pop3-default-server "localhost"
52 "*Default POP3 server."
56 (defcustom elmo-pop3-default-authenticate-type 'user
57 "*Default Authentication type for POP3."
61 (defcustom elmo-pop3-default-port 110
66 (defcustom elmo-pop3-default-stream-type nil
67 "*Default stream type for POP3.
68 Any symbol value of `elmo-network-stream-type-alist' or
69 `elmo-pop3-stream-type-alist'."
73 (defcustom elmo-pop3-default-use-uidl t
74 "If non-nil, use UIDL on POP3."
78 (defvar elmo-pop3-stream-type-alist nil
79 "*Stream bindings for POP3.
80 This is taken precedence over `elmo-network-stream-type-alist'.")
82 (defvar elmo-pop3-use-uidl-internal t
83 "(Internal switch for using UIDL on POP3).")
85 (defvar elmo-pop3-use-cache t
86 "Use cache in pop3 folder.")
88 (defvar elmo-pop3-send-command-synchronously nil
89 "If non-nil, commands are send synchronously.
90 If server doesn't accept asynchronous commands, this variable should be
93 (defvar elmo-pop3-exists-exactly t)
94 (defvar sasl-mechanism-alist)
96 (defvar elmo-pop3-total-size nil)
99 (defvar elmo-pop3-debug nil
100 "Non-nil forces POP3 folder as debug mode.
101 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
103 (defvar elmo-pop3-debug-inhibit-logging nil)
106 (defsubst elmo-pop3-debug (message &rest args)
108 (with-current-buffer (get-buffer-create "*POP3 DEBUG*")
109 (goto-char (point-max))
110 (if elmo-pop3-debug-inhibit-logging
111 (insert "NO LOGGING\n")
112 (insert (apply 'format message args) "\n")))))
116 (luna-define-class elmo-pop3-folder (elmo-net-folder)
117 (use-uidl location-alist))
118 (luna-define-internal-accessors 'elmo-pop3-folder))
120 (luna-define-method elmo-folder-initialize :around ((folder
123 (let ((elmo-network-stream-type-alist
124 (if elmo-pop3-stream-type-alist
125 (append elmo-pop3-stream-type-alist
126 elmo-network-stream-type-alist)
127 elmo-network-stream-type-alist)))
128 (setq name (luna-call-next-method))
129 ;; Setup slots for elmo-net-folder
130 (when (string-match "^\\([^:/!]*\\)\\(/[^/:@!]+\\)?\\(:[^/:@!]+\\)?" name)
131 (elmo-net-folder-set-user-internal folder
132 (if (match-beginning 1)
133 (elmo-match-string 1 name)))
134 (if (eq (length (elmo-net-folder-user-internal folder)) 0)
135 (elmo-net-folder-set-user-internal folder
136 elmo-pop3-default-user))
137 (elmo-net-folder-set-auth-internal
139 (if (match-beginning 2)
140 (intern (elmo-match-substring 2 name 1))
141 elmo-pop3-default-authenticate-type))
142 (elmo-pop3-folder-set-use-uidl-internal
144 (if (match-beginning 3)
145 (string= (elmo-match-substring 3 name 1) "uidl")
146 elmo-pop3-default-use-uidl)))
147 (unless (elmo-net-folder-server-internal folder)
148 (elmo-net-folder-set-server-internal folder
149 elmo-pop3-default-server))
150 (unless (elmo-net-folder-port-internal folder)
151 (elmo-net-folder-set-port-internal folder
152 elmo-pop3-default-port))
153 (unless (elmo-net-folder-stream-type-internal folder)
154 (elmo-net-folder-set-stream-type-internal
156 elmo-pop3-default-stream-type))
160 (luna-define-class elmo-pop3-session (elmo-network-session) ())
163 (defvar elmo-pop3-read-point nil)
164 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
165 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
166 (defvar elmo-pop3-size-hash nil) ; number -> size
167 (defvar elmo-pop3-uidl-done nil)
168 (defvar elmo-pop3-list-done nil)
169 (defvar elmo-pop3-lock nil)
171 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
172 elmo-pop3-uidl-number-hash
173 elmo-pop3-number-uidl-hash
179 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
180 (when (elmo-network-session-process-internal session)
181 (when (memq (process-status
182 (elmo-network-session-process-internal session))
184 (let ((buffer (process-buffer
185 (elmo-network-session-process-internal session))))
186 (elmo-pop3-send-command (elmo-network-session-process-internal session)
189 (or (elmo-pop3-read-response
190 (elmo-network-session-process-internal session)
192 (error "POP error: QUIT failed"))))
193 (kill-buffer (process-buffer
194 (elmo-network-session-process-internal session)))
195 (delete-process (elmo-network-session-process-internal session))))
197 (defun elmo-pop3-get-session (folder &optional if-exists)
198 (let ((elmo-pop3-use-uidl-internal (if elmo-inhibit-number-mapping
200 (elmo-pop3-folder-use-uidl-internal
202 (elmo-network-get-session 'elmo-pop3-session "POP3" folder if-exists)))
204 (defun elmo-pop3-send-command (process command &optional no-erase)
205 (with-current-buffer (process-buffer process)
208 (goto-char (point-min))
209 (setq elmo-pop3-read-point (point))
210 (elmo-pop3-debug "SEND: %s\n" command)
211 (process-send-string process command)
212 (process-send-string process "\r\n")))
214 (defun elmo-pop3-read-response (process &optional not-command)
215 ;; buffer is in case for process is dead.
216 (with-current-buffer (process-buffer process)
217 (let ((case-fold-search nil)
218 (response-string nil)
219 (response-continue t)
222 (while response-continue
223 (goto-char elmo-pop3-read-point)
224 (while (not (re-search-forward "\r?\n" nil t))
225 (accept-process-output process)
226 (goto-char elmo-pop3-read-point))
227 (setq match-end (point))
228 (setq response-string
229 (buffer-substring elmo-pop3-read-point (- match-end 2)))
230 (goto-char elmo-pop3-read-point)
231 (if (looking-at "\\+.*$")
233 (setq response-continue nil)
234 (setq elmo-pop3-read-point match-end)
237 (concat return-value "\n" response-string)
239 (if (looking-at "\\-.*$")
241 (setq response-continue nil)
242 (setq elmo-pop3-read-point match-end)
243 (setq return-value nil))
244 (setq elmo-pop3-read-point match-end)
246 (setq response-continue nil))
249 (concat return-value "\n" response-string)
251 (setq elmo-pop3-read-point match-end)))
254 (defun elmo-pop3-process-filter (process output)
256 (set-buffer (process-buffer process))
257 (goto-char (point-max))
259 (elmo-pop3-debug "RECEIVED: %s\n" output)
260 (if (and elmo-pop3-total-size
261 (> elmo-pop3-total-size
262 (min elmo-display-retrieval-progress-threshold 100)))
263 (elmo-display-progress
264 'elmo-display-retrieval-progress
265 (format "Retrieving (%d/%d bytes)..."
267 elmo-pop3-total-size)
268 (/ (buffer-size) (/ elmo-pop3-total-size 100))))))
270 (defun elmo-pop3-auth-user (session)
271 (let ((process (elmo-network-session-process-internal session)))
273 (elmo-pop3-send-command
275 (format "user %s" (elmo-network-session-user-internal session)))
276 (or (elmo-pop3-read-response process t)
277 (signal 'elmo-authenticate-error
278 '(elmo-pop-auth-user)))
279 (elmo-pop3-send-command process
283 (elmo-network-session-password-key session))))
284 (or (elmo-pop3-read-response process t)
285 (signal 'elmo-authenticate-error
286 '(elmo-pop-auth-user)))))
288 (defun elmo-pop3-auth-apop (session)
289 (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
290 (elmo-network-session-greeting-internal session))
291 ;; good, APOP ready server
293 (elmo-pop3-send-command
294 (elmo-network-session-process-internal session)
296 (elmo-network-session-user-internal session)
298 (concat (match-string
300 (elmo-network-session-greeting-internal session))
302 (elmo-network-session-password-key session))))))
303 (or (elmo-pop3-read-response
304 (elmo-network-session-process-internal session)
306 (signal 'elmo-authenticate-error
307 '(elmo-pop3-auth-apop))))
308 (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
310 (luna-define-method elmo-network-initialize-session-buffer :after
311 ((session elmo-pop3-session) buffer)
312 (with-current-buffer buffer
313 (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
315 (luna-define-method elmo-network-initialize-session ((session
317 (let ((process (elmo-network-session-process-internal session))
319 (with-current-buffer (process-buffer process)
320 (set-process-filter process 'elmo-pop3-process-filter)
321 (setq elmo-pop3-read-point (point-min))
322 ;; Skip garbage output from process before greeting.
323 (while (and (memq (process-status process) '(open run))
324 (goto-char (point-max))
326 (not (looking-at "+OK")))
327 (accept-process-output process 1))
328 (setq elmo-pop3-read-point (point))
329 (or (elmo-network-session-set-greeting-internal
331 (elmo-pop3-read-response process t))
332 (signal 'elmo-open-error
333 '(elmo-network-intialize-session)))
334 (when (eq (elmo-network-stream-type-symbol
335 (elmo-network-session-stream-type-internal session))
337 (elmo-pop3-send-command process "stls")
338 (if (string-match "^\+OK"
339 (elmo-pop3-read-response process))
340 (starttls-negotiate process)
341 (signal 'elmo-open-error
342 '(elmo-pop3-starttls-error)))))))
344 (luna-define-method elmo-network-authenticate-session ((session
346 (with-current-buffer (process-buffer
347 (elmo-network-session-process-internal session))
348 (let* ((process (elmo-network-session-process-internal session))
349 (elmo-pop3-debug-inhibit-logging t)
350 (auth (elmo-network-session-auth-internal session))
351 (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
352 (if (listp auth) auth (list auth))))
354 client name step response mechanism
355 sasl-read-passphrase)
356 (or (and (string= "USER" (car auth))
357 (elmo-pop3-auth-user session))
358 (and (string= "APOP" (car auth))
359 (elmo-pop3-auth-apop session))
362 (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
363 (setq mechanism (sasl-find-mechanism auth))
365 (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
369 (elmo-network-session-user-internal session)
371 (elmo-network-session-server-internal session)))
372 ;;; (if elmo-pop3-auth-user-realm
373 ;;; (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
374 (setq name (sasl-mechanism-name mechanism))
375 (elmo-network-session-set-auth-internal session
376 (intern (downcase name)))
377 (setq sasl-read-passphrase
381 (elmo-network-session-password-key session)))))
382 (setq step (sasl-next-step client nil))
383 (elmo-pop3-send-command
386 (and (sasl-step-data step)
389 (elmo-base64-encode-string
390 (sasl-step-data step) 'no-line-break))))) ;)
393 (unless (setq response (elmo-pop3-read-response process t))
394 ;; response is NO or BAD.
395 (signal 'elmo-authenticate-error
397 (concat "elmo-pop3-auth-"
399 (if (string-match "^\+OK" response)
400 (if (sasl-next-step client step)
402 (signal 'elmo-authenticate-error
404 (concat "elmo-pop3-auth-"
406 ;; The authentication process is finished.
410 (elmo-base64-decode-string
411 (cadr (split-string response " "))))
412 (setq step (sasl-next-step client step))
413 (elmo-pop3-send-command
415 (if (sasl-step-data step)
416 (elmo-base64-encode-string (sasl-step-data step)
420 (luna-define-method elmo-network-setup-session ((session
422 (let ((process (elmo-network-session-process-internal session))
424 (with-current-buffer (process-buffer process)
425 (setq elmo-pop3-size-hash (elmo-make-hash 31))
426 ;; To get obarray of uidl and size
427 (elmo-pop3-send-command process "list")
428 (if (null (elmo-pop3-read-response process))
429 (error "POP LIST command failed"))
430 (if (null (setq response
431 (elmo-pop3-read-contents
432 (current-buffer) process)))
433 (error "POP LIST command failed"))
434 ;; POP server always returns a sequence of serial numbers.
435 (setq count (elmo-pop3-parse-list-response response))
437 (when elmo-pop3-use-uidl-internal
438 (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
439 (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
441 (elmo-pop3-send-command process "uidl")
442 (unless (elmo-pop3-read-response process)
443 (error "POP UIDL failed"))
444 (unless (setq response (elmo-pop3-read-contents
445 (current-buffer) process))
446 (error "POP UIDL failed"))
447 (elmo-pop3-parse-uidl-response response)))))
449 (defun elmo-pop3-read-contents (buffer process)
450 (with-current-buffer buffer
451 (let ((case-fold-search nil)
453 (goto-char elmo-pop3-read-point)
454 (while (not (re-search-forward "^\\.\r\n" nil t))
455 (accept-process-output process)
456 (goto-char elmo-pop3-read-point))
457 (setq match-end (point))
459 (buffer-substring elmo-pop3-read-point
462 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-pop3-folder))
463 (convert-standard-filename
465 (elmo-safe-filename (elmo-net-folder-user-internal folder))
466 (expand-file-name (elmo-net-folder-server-internal folder)
471 (luna-define-method elmo-folder-exists-p ((folder elmo-pop3-folder))
472 (if (and elmo-pop3-exists-exactly
473 (elmo-folder-plugged-p folder))
475 (let (elmo-auto-change-plugged ; don't change plug status.
476 (elmo-inhibit-number-mapping t) ; No need to use uidl.
479 (setq session (elmo-pop3-get-session folder))
481 (elmo-network-close-session session)))))
484 (defun elmo-pop3-parse-uidl-response (string)
485 (let ((buffer (current-buffer))
488 (let (number uid list)
490 (goto-char (point-min))
491 (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([^ \n]+\\)$" nil t)
492 (setq number (elmo-match-buffer 1))
493 (setq uid (elmo-match-buffer 2))
494 (with-current-buffer buffer
495 (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
496 (elmo-set-hash-val (concat "#" number) uid
497 elmo-pop3-number-uidl-hash))
498 (setq list (cons uid list)))
499 (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
502 (defun elmo-pop3-parse-list-response (string)
503 (let ((buffer (current-buffer))
508 (goto-char (point-min))
509 (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
512 (cons (elmo-match-buffer 1)
513 (elmo-match-buffer 2))
515 (setq count (1+ count)))
516 (with-current-buffer buffer
517 (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
519 (elmo-set-hash-val (concat "#" (car (car alist)))
522 (setq alist (cdr alist)))
523 (setq elmo-pop3-list-done t))
526 (defun elmo-pop3-list-location (folder)
527 (with-current-buffer (process-buffer
528 (elmo-network-session-process-internal
529 (elmo-pop3-get-session folder)))
531 (if elmo-pop3-uidl-done
535 (setq list (cons (symbol-name atom) list)))
536 elmo-pop3-uidl-number-hash)
538 (error "POP3: Error in UIDL")))))
540 (defun elmo-pop3-list-folder-by-location (folder locations)
541 (let* ((location-alist (elmo-pop3-folder-location-alist-internal folder))
542 (locations-in-db (mapcar 'cdr location-alist))
543 result new-locs new-alist deleted-locs i)
545 (elmo-delete-if (function
546 (lambda (x) (member x locations-in-db)))
549 (elmo-delete-if (function
550 (lambda (x) (member x locations)))
552 (setq i (or (elmo-max-of-list (mapcar 'car location-alist)) 0))
557 (delq (rassoc x location-alist) location-alist))))
561 (setq new-alist (cons (cons i (car new-locs)) new-alist))
562 (setq new-locs (cdr new-locs)))
563 (setq result (nconc location-alist new-alist))
564 (setq result (sort result (lambda (x y) (< (car x)(car y)))))
565 (elmo-pop3-folder-set-location-alist-internal folder result)
566 (mapcar 'car result)))
568 (defun elmo-pop3-list-by-uidl-subr (folder &optional nonsort)
569 (let ((flist (elmo-pop3-list-folder-by-location
571 (elmo-pop3-list-location folder))))
573 (cons (elmo-max-of-list flist) (length flist))
576 (defun elmo-pop3-list-by-list (folder)
577 (with-current-buffer (process-buffer
578 (elmo-network-session-process-internal
579 (elmo-pop3-get-session folder)))
581 (if elmo-pop3-list-done
583 (mapatoms (lambda (atom)
584 (setq list (cons (string-to-int
585 (substring (symbol-name atom) 1))
589 (error "POP3: Error in list")))))
591 (defsubst elmo-pop3-folder-list-messages (folder)
592 (if (and (not elmo-inhibit-number-mapping)
593 (elmo-pop3-folder-use-uidl-internal folder))
594 (elmo-pop3-list-by-uidl-subr folder)
595 (elmo-pop3-list-by-list folder)))
597 (luna-define-method elmo-folder-list-messages-internal
598 ((folder elmo-pop3-folder) &optional nohide)
599 (elmo-pop3-folder-list-messages folder))
601 (luna-define-method elmo-folder-status ((folder elmo-pop3-folder))
602 (elmo-folder-check folder)
603 (if (elmo-pop3-folder-use-uidl-internal folder)
605 (elmo-pop3-list-by-uidl-subr folder 'nonsort)
606 (elmo-folder-close folder))
608 (elmo-network-session-process-internal
609 (elmo-pop3-get-session folder)))
612 (with-current-buffer (process-buffer process)
613 (elmo-pop3-send-command process "STAT")
614 (setq response (elmo-pop3-read-response process))
615 ;; response: "^\+OK 2 7570$"
616 (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
617 (error "POP STAT command failed")
620 (substring response (match-beginning 1)(match-end 1 ))))
621 (elmo-folder-close folder)
622 (cons total total))))))
624 (defvar elmo-pop3-header-fetch-chop-length 200)
626 (defsubst elmo-pop3-next-result-arrived-p ()
628 ((eq (following-char) ?+)
629 (if (re-search-forward "\n\\.\r?\n" nil t)
633 (if (search-forward "\n" nil t)
639 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
643 (let ((number (length articles))
646 (last-point (point-min)))
647 ;; Send HEAD commands.
649 (elmo-pop3-send-command process (format
650 "top %s 0" (car articles))
652 ;;; (accept-process-output process 1)
653 (setq articles (cdr articles))
654 (setq count (1+ count))
655 ;; Every 200 requests we have to read the stream in
656 ;; order to avoid deadlocks.
657 (when (or elmo-pop3-send-command-synchronously
658 (null articles) ;All requests have been sent.
659 (zerop (% count elmo-pop3-header-fetch-chop-length)))
660 (unless elmo-pop3-send-command-synchronously
661 (accept-process-output process 1))
665 (goto-char last-point)
667 (while (elmo-pop3-next-result-arrived-p)
668 (setq last-point (point))
669 (setq received (1+ received)))
671 (when (> number elmo-display-progress-threshold)
672 (if (or (zerop (% received 5)) (= received number))
673 (elmo-display-progress
674 'elmo-pop3-retrieve-headers "Getting headers..."
675 (/ (* received 100) number))))
676 (accept-process-output process 1)
677 ;;; (accept-process-output process)
679 ;; Remove all "\r"'s.
680 (goto-char (point-min))
681 (while (search-forward "\r\n" nil t)
682 (replace-match "\n"))
683 (copy-to-buffer tobuffer (point-min) (point-max)))))
685 (luna-define-method elmo-folder-msgdb-create ((folder elmo-pop3-folder)
687 already-mark seen-mark
688 important-mark seen-list)
689 (let ((process (elmo-network-session-process-internal
690 (elmo-pop3-get-session folder))))
691 (with-current-buffer (process-buffer process)
692 (elmo-pop3-sort-msgdb-by-original-number
694 (elmo-pop3-msgdb-create-by-header
697 new-mark already-mark
699 (if (elmo-pop3-folder-use-uidl-internal folder)
700 (elmo-pop3-folder-location-alist-internal folder)))))))
702 (defun elmo-pop3-sort-overview-by-original-number (overview loc-alist)
706 (< (elmo-pop3-uidl-to-number
707 (cdr (assq (elmo-msgdb-overview-entity-get-number ent1)
709 (elmo-pop3-uidl-to-number
710 (cdr (assq (elmo-msgdb-overview-entity-get-number ent2)
714 (defun elmo-pop3-sort-msgdb-by-original-number (folder msgdb)
715 (message "Sorting...")
716 (let ((overview (elmo-msgdb-get-overview msgdb)))
718 (setq overview (elmo-pop3-sort-overview-by-original-number
720 (elmo-pop3-folder-location-alist-internal folder)))
721 (message "Sorting...done")
722 (list overview (nth 1 msgdb)(nth 2 msgdb)(nth 3 msgdb))))
724 (defun elmo-pop3-uidl-to-number (uidl)
725 (string-to-number (elmo-get-hash-val uidl
726 elmo-pop3-uidl-number-hash)))
728 (defun elmo-pop3-number-to-uidl (number)
729 (elmo-get-hash-val (format "#%d" number)
730 elmo-pop3-number-uidl-hash))
732 (defun elmo-pop3-number-to-size (number)
733 (elmo-get-hash-val (format "#%d" number)
734 elmo-pop3-size-hash))
736 (defun elmo-pop3-msgdb-create-by-header (process numlist
737 new-mark already-mark
741 (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
742 (with-current-buffer (process-buffer process)
743 (if loc-alist ; use uidl.
749 (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
751 (elmo-pop3-retrieve-headers (process-buffer process)
752 tmp-buffer process numlist)
754 (elmo-pop3-msgdb-create-message
759 new-mark already-mark seen-mark seen-list loc-alist)
760 (kill-buffer tmp-buffer)))))
762 (defun elmo-pop3-msgdb-create-message (buffer
765 numlist new-mark already-mark
770 (let (beg overview number-alist mark-alist
771 entity i number message-id gmark seen size)
773 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
774 (goto-char (point-min))
776 (message "Creating msgdb...")
778 (setq beg (save-excursion (forward-line 1) (point)))
779 (elmo-pop3-next-result-arrived-p)
783 (narrow-to-region beg (point))
785 (elmo-msgdb-create-overview-from-buffer
787 (setq numlist (cdr numlist))
790 (elmo-msgdb-append-element
792 (with-current-buffer (process-buffer process)
793 (elmo-msgdb-overview-entity-set-size
796 (elmo-pop3-number-to-size
797 (elmo-msgdb-overview-entity-get-number entity))))
801 (elmo-pop3-number-to-uidl
802 (elmo-msgdb-overview-entity-get-number entity))
804 (elmo-msgdb-overview-entity-set-number entity number)))
806 (elmo-msgdb-number-add
808 (elmo-msgdb-overview-entity-get-number entity)
810 (setq message-id (car entity))
811 (setq seen (member message-id seen-list))
812 (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
813 (if (elmo-file-cache-status
814 (elmo-file-cache-get message-id))
819 (if elmo-pop3-use-cache
823 (elmo-msgdb-mark-append
825 (elmo-msgdb-overview-entity-get-number entity)
827 (when (> num elmo-display-progress-threshold)
829 (if (or (zerop (% i 5)) (= i num))
830 (elmo-display-progress
831 'elmo-pop3-msgdb-create-message "Creating msgdb..."
832 (/ (* i 100) num)))))
833 (list overview number-alist mark-alist))))
835 (defun elmo-pop3-read-body (process outbuf)
836 (with-current-buffer (process-buffer process)
837 (let ((start elmo-pop3-read-point)
840 (while (not (re-search-forward "^\\.\r?\n" nil t))
841 (accept-process-output process)
844 (with-current-buffer outbuf
846 (insert-buffer-substring (process-buffer process) start (- end 3))))))
848 (luna-define-method elmo-folder-open-internal ((folder elmo-pop3-folder))
849 (if (and (not elmo-inhibit-number-mapping)
850 (elmo-pop3-folder-use-uidl-internal folder))
851 (elmo-pop3-folder-set-location-alist-internal
852 folder (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))))
854 (luna-define-method elmo-folder-commit :after ((folder elmo-pop3-folder))
855 (when (elmo-folder-persistent-p folder)
856 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
857 (elmo-pop3-folder-location-alist-internal
860 (luna-define-method elmo-folder-close-internal ((folder elmo-pop3-folder))
861 (elmo-folder-check folder))
863 (luna-define-method elmo-message-fetch-plugged ((folder elmo-pop3-folder)
867 (let* ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
868 (process (elmo-network-session-process-internal
869 (elmo-pop3-get-session folder)))
870 size response errmsg msg)
871 (with-current-buffer (process-buffer process)
873 (setq number (elmo-pop3-uidl-to-number
874 (cdr (assq number loc-alist)))))
875 (setq size (string-to-number
876 (elmo-pop3-number-to-size number)))
878 (elmo-pop3-send-command process
879 (format "retr %s" number))
880 (unless elmo-inhibit-display-retrieval-progress
881 (setq elmo-pop3-total-size size)
882 (elmo-display-progress
883 'elmo-pop3-display-retrieval-progress
884 (format "Retrieving (0/%d bytes)..." elmo-pop3-total-size)
888 (when (null (setq response (elmo-pop3-read-response
890 (error "Fetching message failed"))
891 (setq response (elmo-pop3-read-body process outbuf)))
892 (setq elmo-pop3-total-size nil))
893 (unless elmo-inhibit-display-retrieval-progress
894 (elmo-display-progress
895 'elmo-display-retrieval-progress "" 100) ; remove progress bar.
896 (message "Retrieving...done."))
898 (goto-char (point-min))
899 (while (re-search-forward "^\\." nil t)
904 (defun elmo-pop3-delete-msg (process number loc-alist)
905 (with-current-buffer (process-buffer process)
906 (let (response errmsg msg)
908 (setq number (elmo-pop3-uidl-to-number
909 (cdr (assq number loc-alist)))))
912 (elmo-pop3-send-command process
913 (format "dele %s" number))
914 (when (null (setq response (elmo-pop3-read-response
916 (error "Deleting message failed")))
917 (error "Deleting message failed")))))
919 (luna-define-method elmo-folder-delete-messages ((folder elmo-pop3-folder)
921 (let ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
922 (process (elmo-network-session-process-internal
923 (elmo-pop3-get-session folder))))
924 (mapcar '(lambda (msg) (elmo-pop3-delete-msg
925 process msg loc-alist))
928 (luna-define-method elmo-message-use-cache-p ((folder elmo-pop3-folder) number)
931 (luna-define-method elmo-folder-persistent-p ((folder elmo-pop3-folder))
932 (and (elmo-folder-persistent-internal folder)
933 (elmo-pop3-folder-use-uidl-internal folder)))
935 (luna-define-method elmo-folder-check ((folder elmo-pop3-folder))
936 (if (elmo-folder-plugged-p folder)
937 (let ((session (elmo-pop3-get-session folder 'if-exists)))
939 (elmo-pop3-folder-set-location-alist-internal folder nil)
940 (elmo-network-close-session session)))))
943 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
945 ;;; elmo-pop3.el ends here