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 (defcustom elmo-pop3-exists-exactly nil
60 "If non-nil, POP3 folder existence is checked everytime before the session."
64 (defvar sasl-mechanism-alist)
66 (defvar elmo-pop3-total-size nil)
69 (defvar elmo-pop3-debug nil
70 "Non-nil forces POP3 folder as debug mode.
71 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
74 (defsubst elmo-pop3-debug (message &rest args)
76 (let ((biff (string-match "BIFF-" (buffer-name)))
78 (with-current-buffer (get-buffer-create (concat "*POP3 DEBUG*"
80 (goto-char (point-max))
82 (insert (apply 'format message args) "\n")))))
86 (luna-define-class elmo-pop3-folder (elmo-net-folder)
87 (use-uidl location-alist))
88 (luna-define-internal-accessors 'elmo-pop3-folder))
90 (luna-define-method elmo-folder-initialize :around ((folder
93 (let ((elmo-network-stream-type-alist
94 (if elmo-pop3-stream-type-alist
95 (append elmo-pop3-stream-type-alist
96 elmo-network-stream-type-alist)
97 elmo-network-stream-type-alist))
99 (setq name (luna-call-next-method))
101 (setq parse (elmo-parse-token name "/:"))
102 (elmo-net-folder-set-user-internal folder
103 (if (eq (length (car parse)) 0)
104 elmo-pop3-default-user
107 (setq parse (elmo-parse-prefixed-element ?/ (cdr parse) ":"))
108 (elmo-net-folder-set-auth-internal folder
109 (if (eq (length (car parse)) 0)
110 elmo-pop3-default-authenticate-type
111 (intern (downcase (car parse)))))
113 (setq parse (elmo-parse-prefixed-element ?: (cdr parse)))
114 (elmo-pop3-folder-set-use-uidl-internal folder
115 (if (eq (length (car parse)) 0)
116 elmo-pop3-default-use-uidl
117 (string= (car parse) "uidl")))
118 (unless (elmo-net-folder-server-internal folder)
119 (elmo-net-folder-set-server-internal folder
120 elmo-pop3-default-server))
121 (unless (elmo-net-folder-port-internal folder)
122 (elmo-net-folder-set-port-internal folder
123 elmo-pop3-default-port))
124 (unless (elmo-net-folder-stream-type-internal folder)
125 (elmo-net-folder-set-stream-type-internal
127 (elmo-get-network-stream-type
128 elmo-pop3-default-stream-type)))
132 (luna-define-class elmo-pop3-session (elmo-network-session) ())
135 (defvar elmo-pop3-read-point nil)
136 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
137 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
138 (defvar elmo-pop3-size-hash nil) ; number -> size
139 (defvar elmo-pop3-uidl-done nil)
140 (defvar elmo-pop3-list-done nil)
141 (defvar elmo-pop3-lock nil)
143 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
144 elmo-pop3-uidl-number-hash
145 elmo-pop3-number-uidl-hash
151 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
152 (when (elmo-network-session-process-internal session)
153 (when (memq (process-status
154 (elmo-network-session-process-internal session))
156 (elmo-pop3-send-command (elmo-network-session-process-internal session)
159 (or (elmo-pop3-read-response
160 (elmo-network-session-process-internal session)
162 (error "POP error: QUIT failed")))
163 (kill-buffer (process-buffer
164 (elmo-network-session-process-internal session)))
165 (delete-process (elmo-network-session-process-internal session))))
167 (defun elmo-pop3-get-session (folder &optional if-exists)
168 "Get POP3 session for FOLDER.
169 If IF-EXISTS is non-nil, don't get new session.
170 If IF-EXISTS is `any-exists', get BIFF session or normal session if exists."
171 (let ((elmo-pop3-use-uidl-internal (if elmo-inhibit-number-mapping
173 (elmo-pop3-folder-use-uidl-internal
176 (if (eq if-exists 'any-exists)
177 (or (elmo-network-get-session 'elmo-pop3-session
180 (elmo-network-get-session 'elmo-pop3-session
183 (elmo-network-get-session 'elmo-pop3-session
185 (if (elmo-folder-biff-internal folder)
189 ;; For saving existency.
190 (unless (file-exists-p (elmo-folder-msgdb-path folder))
191 (elmo-make-directory (elmo-folder-msgdb-path folder))))))
193 (defun elmo-pop3-send-command (process command &optional no-erase no-log)
194 (with-current-buffer (process-buffer process)
197 (goto-char (point-min))
198 (setq elmo-pop3-read-point (point))
199 (elmo-pop3-debug "SEND: %s\n" (if no-log "<NO LOGGING>" command))
200 (process-send-string process command)
201 (process-send-string process "\r\n")))
203 (defun elmo-pop3-read-response (process &optional not-command)
204 ;; buffer is in case for process is dead.
205 (with-current-buffer (process-buffer process)
206 (let ((case-fold-search nil)
207 (response-string nil)
208 (response-continue t)
211 (while response-continue
212 (goto-char elmo-pop3-read-point)
213 (while (not (re-search-forward "\r?\n" nil t))
214 (accept-process-output process 1)
215 (goto-char elmo-pop3-read-point))
216 (setq match-end (point))
217 (setq response-string
218 (buffer-substring elmo-pop3-read-point (- match-end 2)))
219 (goto-char elmo-pop3-read-point)
220 (if (looking-at "\\+.*$")
222 (setq response-continue nil)
223 (setq elmo-pop3-read-point match-end)
226 (concat return-value "\n" response-string)
228 (if (looking-at "\\-.*$")
230 (setq response-continue nil)
231 (setq elmo-pop3-read-point match-end)
232 (setq return-value nil))
233 (setq elmo-pop3-read-point match-end)
235 (setq response-continue nil))
238 (concat return-value "\n" response-string)
240 (setq elmo-pop3-read-point match-end)))
243 (defun elmo-pop3-process-filter (process output)
244 (when (buffer-live-p (process-buffer process))
245 (with-current-buffer (process-buffer process)
246 (goto-char (point-max))
248 (elmo-pop3-debug "RECEIVED: %s\n" output)
249 (if (and elmo-pop3-total-size
250 (> elmo-pop3-total-size
251 (min elmo-display-retrieval-progress-threshold 100)))
252 (elmo-display-progress
253 'elmo-display-retrieval-progress
254 (format "Retrieving (%d/%d bytes)..."
256 elmo-pop3-total-size)
257 (/ (buffer-size) (/ elmo-pop3-total-size 100)))))))
259 (defun elmo-pop3-auth-user (session)
260 (let ((process (elmo-network-session-process-internal session)))
262 (elmo-pop3-send-command
264 (format "user %s" (elmo-network-session-user-internal session))
266 (or (elmo-pop3-read-response process t)
268 (delete-process process)
269 (signal 'elmo-authenticate-error
270 '(elmo-pop-auth-user))))
271 (elmo-pop3-send-command process
275 (elmo-network-session-password-key session)))
277 (or (elmo-pop3-read-response process t)
279 (delete-process process)
280 (signal 'elmo-authenticate-error
281 '(elmo-pop-auth-user))))))
283 (defun elmo-pop3-auth-apop (session)
284 (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
285 (elmo-network-session-greeting-internal session))
286 ;; good, APOP ready server
288 (elmo-pop3-send-command
289 (elmo-network-session-process-internal session)
291 (elmo-network-session-user-internal session)
293 (concat (match-string
295 (elmo-network-session-greeting-internal session))
297 (elmo-network-session-password-key session)))))
299 (or (elmo-pop3-read-response
300 (elmo-network-session-process-internal session)
303 (delete-process (elmo-network-session-process-internal session))
304 (signal 'elmo-authenticate-error
305 '(elmo-pop3-auth-apop)))))
306 (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
308 (luna-define-method elmo-network-initialize-session-buffer :after
309 ((session elmo-pop3-session) buffer)
310 (with-current-buffer buffer
311 (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
313 (luna-define-method elmo-network-initialize-session ((session
315 (let ((process (elmo-network-session-process-internal session))
317 (with-current-buffer (process-buffer process)
318 (set-process-filter process 'elmo-pop3-process-filter)
319 (setq elmo-pop3-read-point (point-min))
320 ;; Skip garbage output from process before greeting.
321 (while (and (memq (process-status process) '(open run))
322 (goto-char (point-max))
324 (not (looking-at "+OK")))
325 (accept-process-output process 1))
326 (setq elmo-pop3-read-point (point))
327 (or (elmo-network-session-set-greeting-internal
329 (elmo-pop3-read-response process t))
330 (signal 'elmo-open-error
331 '(elmo-network-intialize-session)))
332 (when (eq (elmo-network-stream-type-symbol
333 (elmo-network-session-stream-type-internal session))
335 (elmo-pop3-send-command process "stls")
336 (if (string-match "^\+OK"
337 (elmo-pop3-read-response process))
338 (starttls-negotiate process)
339 (signal 'elmo-open-error
340 '(elmo-pop3-starttls-error)))))))
342 (luna-define-method elmo-network-authenticate-session ((session
344 (with-current-buffer (process-buffer
345 (elmo-network-session-process-internal session))
346 (let* ((process (elmo-network-session-process-internal session))
347 (auth (elmo-network-session-auth-internal session))
348 (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
349 (if (listp auth) auth (list auth))))
351 client name step response mechanism
352 sasl-read-passphrase)
353 (or (and (string= "USER" (car auth))
354 (elmo-pop3-auth-user session))
355 (and (string= "APOP" (car auth))
356 (elmo-pop3-auth-apop session))
359 (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
360 (setq mechanism (sasl-find-mechanism auth))
362 (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
366 (elmo-network-session-user-internal session)
368 (elmo-network-session-server-internal session)))
369 ;;; (if elmo-pop3-auth-user-realm
370 ;;; (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
371 (setq name (sasl-mechanism-name mechanism))
372 (elmo-network-session-set-auth-internal session
373 (intern (downcase name)))
374 (setq sasl-read-passphrase
378 (elmo-network-session-password-key session)))))
379 (setq step (sasl-next-step client nil))
380 (elmo-pop3-send-command
383 (and (sasl-step-data step)
386 (elmo-base64-encode-string
387 (sasl-step-data step) 'no-line-break))))
391 (unless (setq response (elmo-pop3-read-response process t))
392 ;; response is NO or BAD.
393 (signal 'elmo-authenticate-error
395 (concat "elmo-pop3-auth-"
397 (if (string-match "^\+OK" response)
398 (if (sasl-next-step client step)
400 (signal 'elmo-authenticate-error
402 (concat "elmo-pop3-auth-"
404 ;; The authentication process is finished.
408 (elmo-base64-decode-string
409 (cadr (split-string response " "))))
410 (setq step (sasl-next-step client step))
411 (elmo-pop3-send-command
413 (if (sasl-step-data step)
414 (elmo-base64-encode-string (sasl-step-data step)
416 "") nil 'no-log))))))))
418 (luna-define-method elmo-network-setup-session ((session
420 (let ((process (elmo-network-session-process-internal session))
422 (with-current-buffer (process-buffer process)
423 (setq elmo-pop3-size-hash (elmo-make-hash 31))
424 ;; To get obarray of uidl and size
425 (elmo-pop3-send-command process "list")
426 (if (null (elmo-pop3-read-response process))
427 (error "POP LIST command failed"))
428 (if (null (setq response
429 (elmo-pop3-read-contents
430 (current-buffer) process)))
431 (error "POP LIST command failed"))
432 ;; POP server always returns a sequence of serial numbers.
433 (setq count (elmo-pop3-parse-list-response response))
435 (when elmo-pop3-use-uidl-internal
436 (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
437 (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
439 (elmo-pop3-send-command process "uidl")
440 (unless (elmo-pop3-read-response process)
441 (error "POP UIDL failed"))
442 (unless (setq response (elmo-pop3-read-contents
443 (current-buffer) process))
444 (error "POP UIDL failed"))
445 (elmo-pop3-parse-uidl-response response)))))
447 (defun elmo-pop3-read-contents (buffer process)
448 (with-current-buffer buffer
449 (let ((case-fold-search nil)
451 (goto-char elmo-pop3-read-point)
452 (while (not (re-search-forward "^\\.\r\n" nil t))
453 (accept-process-output process 1)
454 (goto-char elmo-pop3-read-point))
455 (setq match-end (point))
457 (buffer-substring elmo-pop3-read-point
460 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-pop3-folder))
461 (convert-standard-filename
463 (elmo-safe-filename (elmo-net-folder-user-internal folder))
464 (expand-file-name (elmo-net-folder-server-internal folder)
467 elmo-msgdb-directory)))))
469 (luna-define-method elmo-folder-exists-p ((folder elmo-pop3-folder))
470 (if (and elmo-pop3-exists-exactly
471 (elmo-folder-plugged-p folder))
473 (let (elmo-auto-change-plugged ; don't change plug status.
474 (elmo-inhibit-number-mapping t) ; No need to use uidl.
477 (setq session (elmo-pop3-get-session folder))
479 (elmo-network-close-session session)))))
480 (or (file-directory-p (elmo-folder-msgdb-path folder))
482 (when (elmo-folder-plugged-p folder)
483 (let ((elmo-pop3-exists-exactly t))
484 (elmo-folder-exists-p folder))))))
486 (defun elmo-pop3-parse-uidl-response (string)
487 (let ((buffer (current-buffer))
490 (let (number uid list)
492 (goto-char (point-min))
493 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([^ \n]+\\)$" nil t)
494 (setq number (elmo-match-buffer 1))
495 (setq uid (elmo-match-buffer 2))
496 (with-current-buffer buffer
497 (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
498 (elmo-set-hash-val (concat "#" number) uid
499 elmo-pop3-number-uidl-hash))
500 (setq list (cons uid list)))
501 (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
504 (defun elmo-pop3-parse-list-response (string)
505 (let ((buffer (current-buffer))
510 (goto-char (point-min))
511 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([0-9]+\\)$" nil t)
514 (cons (elmo-match-buffer 1)
515 (elmo-match-buffer 2))
517 (setq count (1+ count)))
518 (with-current-buffer buffer
519 (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
521 (elmo-set-hash-val (concat "#" (car (car alist)))
524 (setq alist (cdr alist)))
525 (setq elmo-pop3-list-done t))
528 (defun elmo-pop3-list-location (folder)
529 (with-current-buffer (process-buffer
530 (elmo-network-session-process-internal
531 (elmo-pop3-get-session folder)))
533 (if elmo-pop3-uidl-done
537 (setq locations (cons (symbol-name atom) locations)))
538 elmo-pop3-uidl-number-hash)
541 (< (elmo-pop3-uidl-to-number loc1)
542 (elmo-pop3-uidl-to-number loc2)))))
543 (error "POP3: Error in UIDL")))))
545 (defun elmo-pop3-list-folder-by-location (folder locations)
546 (let* ((location-alist (elmo-pop3-folder-location-alist-internal folder))
547 (locations-in-db (mapcar 'cdr location-alist))
548 result new-locs new-alist deleted-locs i)
550 (elmo-delete-if (function
551 (lambda (x) (member x locations-in-db)))
554 (elmo-delete-if (function
555 (lambda (x) (member x locations)))
557 (setq i (or (elmo-max-of-list (mapcar 'car location-alist)) 0))
562 (delq (rassoc x location-alist) location-alist))))
566 (setq new-alist (cons (cons i (car new-locs)) new-alist))
567 (setq new-locs (cdr new-locs)))
568 (setq result (nconc location-alist new-alist))
569 (setq result (sort result (lambda (x y) (< (car x)(car y)))))
570 (elmo-pop3-folder-set-location-alist-internal folder result)
571 (mapcar 'car result)))
573 (defun elmo-pop3-list-by-uidl-subr (folder &optional nonsort)
574 (let ((flist (elmo-pop3-list-folder-by-location
576 (elmo-pop3-list-location folder))))
578 (cons (elmo-max-of-list flist) (length flist))
581 (defun elmo-pop3-list-by-list (folder)
582 (with-current-buffer (process-buffer
583 (elmo-network-session-process-internal
584 (elmo-pop3-get-session folder)))
586 (if elmo-pop3-list-done
588 (mapatoms (lambda (atom)
589 (setq list (cons (string-to-int
590 (substring (symbol-name atom) 1))
594 (error "POP3: Error in list")))))
596 (defsubst elmo-pop3-folder-list-messages (folder)
597 (if (and (not elmo-inhibit-number-mapping)
598 (elmo-pop3-folder-use-uidl-internal folder))
599 (elmo-pop3-list-by-uidl-subr folder)
600 (elmo-pop3-list-by-list folder)))
602 (luna-define-method elmo-folder-list-messages-plugged
603 ((folder elmo-pop3-folder) &optional nohide)
604 (elmo-pop3-folder-list-messages folder))
606 (luna-define-method elmo-folder-status ((folder elmo-pop3-folder))
607 (elmo-folder-open-internal folder)
608 (elmo-folder-check folder)
609 (if (elmo-pop3-folder-use-uidl-internal folder)
611 (elmo-pop3-list-by-uidl-subr folder 'nonsort)
612 (elmo-folder-close-internal folder))
614 (elmo-network-session-process-internal
615 (elmo-pop3-get-session folder)))
618 (with-current-buffer (process-buffer process)
619 (elmo-pop3-send-command process "STAT")
620 (setq response (elmo-pop3-read-response process))
621 ;; response: "^\+OK 2 7570$"
622 (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
623 (error "POP STAT command failed")
626 (substring response (match-beginning 1)(match-end 1 ))))
627 (elmo-folder-close-internal folder)
628 (cons total total))))))
630 (defvar elmo-pop3-header-fetch-chop-length 200)
632 (defsubst elmo-pop3-next-result-arrived-p ()
634 ((eq (following-char) ?+)
635 (if (re-search-forward "\n\\.\r?\n" nil t)
639 (if (search-forward "\n" nil t)
645 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
649 (let ((number (length articles))
652 (last-point (point-min)))
653 ;; Send HEAD commands.
655 (elmo-pop3-send-command process (format
656 "top %s 0" (car articles))
658 ;;; (accept-process-output process 1)
659 (setq articles (cdr articles))
660 (setq count (1+ count))
661 ;; Every 200 requests we have to read the stream in
662 ;; order to avoid deadlocks.
663 (when (or elmo-pop3-send-command-synchronously
664 (null articles) ;All requests have been sent.
665 (zerop (% count elmo-pop3-header-fetch-chop-length)))
666 (unless elmo-pop3-send-command-synchronously
667 (accept-process-output process 1))
671 (goto-char last-point)
673 (while (elmo-pop3-next-result-arrived-p)
674 (setq last-point (point))
675 (setq received (1+ received)))
677 (when (> number elmo-display-progress-threshold)
678 (if (or (zerop (% received 5)) (= received number))
679 (elmo-display-progress
680 'elmo-pop3-retrieve-headers "Getting headers..."
681 (/ (* received 100) number))))
682 (accept-process-output process 1)
683 ;;; (accept-process-output process)
685 ;; Remove all "\r"'s.
686 (goto-char (point-min))
687 (while (search-forward "\r\n" nil t)
688 (replace-match "\n"))
689 (copy-to-buffer tobuffer (point-min) (point-max)))))
691 (luna-define-method elmo-folder-msgdb-create ((folder elmo-pop3-folder)
693 (let ((process (elmo-network-session-process-internal
694 (elmo-pop3-get-session folder))))
695 (with-current-buffer (process-buffer process)
696 (elmo-pop3-sort-msgdb-by-original-number
698 (elmo-pop3-msgdb-create-by-header
702 (if (elmo-pop3-folder-use-uidl-internal folder)
703 (elmo-pop3-folder-location-alist-internal folder)))))))
705 (defun elmo-pop3-sort-overview-by-original-number (overview loc-alist)
709 (< (elmo-pop3-uidl-to-number
710 (cdr (assq (elmo-msgdb-overview-entity-get-number ent1)
712 (elmo-pop3-uidl-to-number
713 (cdr (assq (elmo-msgdb-overview-entity-get-number ent2)
717 (defun elmo-pop3-sort-msgdb-by-original-number (folder msgdb)
718 (message "Sorting...")
719 (let ((overview (elmo-msgdb-get-overview msgdb)))
720 (elmo-msgdb-set-overview
722 (elmo-pop3-sort-overview-by-original-number
724 (elmo-pop3-folder-location-alist-internal folder)))
725 (message "Sorting...done")
728 (defun elmo-pop3-uidl-to-number (uidl)
729 (string-to-number (elmo-get-hash-val uidl
730 elmo-pop3-uidl-number-hash)))
732 (defun elmo-pop3-number-to-uidl (number)
733 (elmo-get-hash-val (format "#%d" number)
734 elmo-pop3-number-uidl-hash))
736 (defun elmo-pop3-number-to-size (number)
737 (elmo-get-hash-val (format "#%d" number)
738 elmo-pop3-size-hash))
740 (defun elmo-pop3-msgdb-create-by-header (process numlist
743 (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
744 (with-current-buffer (process-buffer process)
745 (if loc-alist ; use uidl.
751 (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
753 (elmo-pop3-retrieve-headers (process-buffer process)
754 tmp-buffer process numlist)
756 (elmo-pop3-msgdb-create-message
761 flag-table loc-alist)
762 (kill-buffer tmp-buffer)))))
764 (defun elmo-pop3-msgdb-create-message (buffer
771 (let ((new-msgdb (elmo-make-msgdb))
772 beg entity i number message-id gmark)
774 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
775 (goto-char (point-min))
777 (message "Creating msgdb...")
779 (setq beg (save-excursion (forward-line 1) (point)))
780 (elmo-pop3-next-result-arrived-p)
784 (narrow-to-region beg (point))
786 (elmo-msgdb-create-overview-from-buffer
788 (setq numlist (cdr numlist))
790 (with-current-buffer (process-buffer process)
791 (elmo-msgdb-overview-entity-set-size
794 (elmo-pop3-number-to-size
795 (elmo-msgdb-overview-entity-get-number entity))))
799 (elmo-pop3-number-to-uidl
800 (elmo-msgdb-overview-entity-get-number entity))
802 (elmo-msgdb-overview-entity-set-number entity number)))
803 (setq message-id (elmo-message-entity-field entity 'message-id))
804 (setq gmark (or (elmo-msgdb-global-mark-get message-id)
806 (elmo-flag-table-get flag-table message-id)
807 (elmo-file-cache-status
808 (elmo-file-cache-get message-id))
810 (elmo-msgdb-append-entity new-msgdb entity gmark))))
811 (when (> num elmo-display-progress-threshold)
813 (if (or (zerop (% i 5)) (= i num))
814 (elmo-display-progress
815 'elmo-pop3-msgdb-create-message "Creating msgdb..."
816 (/ (* i 100) num)))))
819 (defun elmo-pop3-read-body (process outbuf)
820 (with-current-buffer (process-buffer process)
821 (let ((start elmo-pop3-read-point)
824 (while (not (re-search-forward "^\\.\r?\n" nil t))
825 (accept-process-output process 1)
828 (with-current-buffer outbuf
830 (insert-buffer-substring (process-buffer process) start (- end 3)))
833 (luna-define-method elmo-folder-open-internal ((folder elmo-pop3-folder))
834 (if (and (not elmo-inhibit-number-mapping)
835 (elmo-pop3-folder-use-uidl-internal folder))
836 (elmo-pop3-folder-set-location-alist-internal
837 folder (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))))
839 (luna-define-method elmo-folder-commit :after ((folder elmo-pop3-folder))
840 (when (elmo-folder-persistent-p folder)
841 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
842 (elmo-pop3-folder-location-alist-internal
845 (luna-define-method elmo-folder-close-internal ((folder elmo-pop3-folder))
846 (elmo-pop3-folder-set-location-alist-internal folder nil)
847 ;; Just close connection
848 (elmo-folder-check folder))
850 (luna-define-method elmo-message-fetch-plugged ((folder elmo-pop3-folder)
854 (let* ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
855 (process (elmo-network-session-process-internal
856 (elmo-pop3-get-session folder)))
857 size response errmsg msg)
858 (with-current-buffer (process-buffer process)
860 (setq number (elmo-pop3-uidl-to-number
861 (cdr (assq number loc-alist)))))
862 (setq size (string-to-number
863 (elmo-pop3-number-to-size number)))
865 (elmo-pop3-send-command process
866 (format "retr %s" number))
867 (unless elmo-inhibit-display-retrieval-progress
868 (setq elmo-pop3-total-size size)
869 (elmo-display-progress
870 'elmo-display-retrieval-progress
871 (format "Retrieving (0/%d bytes)..." elmo-pop3-total-size)
875 (when (null (setq response (elmo-pop3-read-response
877 (error "Fetching message failed"))
878 (setq response (elmo-pop3-read-body process outbuf)))
879 (setq elmo-pop3-total-size nil))
880 (unless elmo-inhibit-display-retrieval-progress
881 (elmo-display-progress
882 'elmo-display-retrieval-progress
883 "Retrieving..." 100) ; remove progress bar.
884 (message "Retrieving...done"))
886 (goto-char (point-min))
887 (while (re-search-forward "^\\." nil t)
892 (defun elmo-pop3-delete-msg (process number loc-alist)
893 (with-current-buffer (process-buffer process)
894 (let (response errmsg msg)
896 (setq number (elmo-pop3-uidl-to-number
897 (cdr (assq number loc-alist)))))
900 (elmo-pop3-send-command process
901 (format "dele %s" number))
902 (when (null (setq response (elmo-pop3-read-response
904 (error "Deleting message failed")))
905 (error "Deleting message failed")))))
907 (luna-define-method elmo-folder-delete-messages-plugged ((folder
910 (let ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
911 (process (elmo-network-session-process-internal
912 (elmo-pop3-get-session folder))))
913 (mapcar '(lambda (msg) (elmo-pop3-delete-msg process msg loc-alist))
916 (luna-define-method elmo-message-use-cache-p ((folder elmo-pop3-folder) number)
919 (luna-define-method elmo-folder-persistent-p ((folder elmo-pop3-folder))
920 (and (elmo-folder-persistent-internal folder)
921 (elmo-pop3-folder-use-uidl-internal folder)))
923 (luna-define-method elmo-folder-clear :around ((folder elmo-pop3-folder)
924 &optional keep-killed)
926 (elmo-pop3-folder-set-location-alist-internal folder nil))
927 (luna-call-next-method))
929 (luna-define-method elmo-folder-check ((folder elmo-pop3-folder))
930 (if (elmo-folder-plugged-p folder)
931 (let ((session (elmo-pop3-get-session folder 'if-exists)))
933 (elmo-network-close-session session)))))
936 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
938 ;;; elmo-pop3.el ends here