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 (defconst elmo-pop3-folder-name-syntax `(([user ".+"])
66 (?: [uidl "^[A-Za-z]+$"])
67 ,@elmo-net-folder-name-syntax))
69 (defvar sasl-mechanism-alist)
71 (defvar elmo-pop3-total-size nil)
74 (defvar elmo-pop3-debug nil
75 "Non-nil forces POP3 folder as debug mode.
76 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
79 (defsubst elmo-pop3-debug (message &rest args)
81 (let ((biff (string-match "BIFF-" (buffer-name)))
83 (with-current-buffer (get-buffer-create (concat "*POP3 DEBUG*"
85 (goto-char (point-max))
87 (insert (apply 'format message args) "\n")))))
91 (luna-define-class elmo-pop3-folder (elmo-net-folder)
92 (use-uidl location-alist))
93 (luna-define-internal-accessors 'elmo-pop3-folder))
95 (luna-define-method elmo-folder-initialize ((folder elmo-pop3-folder) name)
96 (let ((elmo-network-stream-type-alist
97 (if elmo-pop3-stream-type-alist
98 (append elmo-pop3-stream-type-alist
99 elmo-network-stream-type-alist)
100 elmo-network-stream-type-alist))
102 (setq tokens (car (elmo-parse-separated-tokens
104 elmo-pop3-folder-name-syntax)))
106 (elmo-net-folder-set-user-internal folder
107 (or (cdr (assq 'user tokens))
108 elmo-pop3-default-user))
110 (setq auth (cdr (assq 'auth tokens)))
111 (elmo-net-folder-set-auth-internal folder
113 (intern (downcase auth))
114 elmo-pop3-default-authenticate-type))
116 (setq uidl (cdr (assq 'uidl tokens)))
117 (elmo-pop3-folder-set-use-uidl-internal folder
119 (string= uidl "uidl")
120 elmo-pop3-default-use-uidl))
122 (elmo-net-folder-set-parameters
125 (list :server elmo-pop3-default-server
126 :port elmo-pop3-default-port
128 (elmo-get-network-stream-type 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 (cdr (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 (concat command "\r\n"))))
202 (defun elmo-pop3-read-response (process &optional not-command)
203 "Read response and return a cons cell of \(CODE . BODY\).
204 PROCESS is the process to read response from.
205 If optional NOT-COMMAND is non-nil, read only the first line.
206 CODE is one of the following:
207 'ok ... response is OK.
208 'err ... response is ERROR.
209 'login-delay ... user is not allowed to login until the login delay
211 'in-use ... authentication was successful but the mailbox is in use."
212 ;; buffer is in case for process is dead.
213 (with-current-buffer (process-buffer process)
214 (let ((case-fold-search nil)
215 (response-string nil)
216 (response-continue t)
220 (while response-continue
221 (goto-char elmo-pop3-read-point)
222 (while (not (re-search-forward "\r?\n" nil t))
223 (accept-process-output process 1)
224 (goto-char elmo-pop3-read-point))
225 (setq match-end (point))
226 (setq response-string
227 (buffer-substring elmo-pop3-read-point (- match-end 2)))
228 (goto-char elmo-pop3-read-point)
229 (if (looking-at "\\+.*$")
231 (setq response-continue nil)
232 (setq elmo-pop3-read-point match-end)
235 (concat return-value "\n" response-string)
237 (if (looking-at "\\-.*$")
239 (when (looking-at "[^ ]+ \\[\\([^]]+\\)\\]")
243 (buffer-substring (match-beginning 1)
246 response-continue nil
247 elmo-pop3-read-point match-end
248 return-value (cons (or return-value 'err) nil)))
249 (setq elmo-pop3-read-point match-end)
251 (setq response-continue nil))
254 (concat return-value "\n" response-string)
256 (setq elmo-pop3-read-point match-end)))
259 (cons 'ok return-value)))))
261 (defun elmo-pop3-process-filter (process output)
262 (when (buffer-live-p (process-buffer process))
263 (with-current-buffer (process-buffer process)
264 (goto-char (point-max))
266 (elmo-pop3-debug "RECEIVED: %s\n" output)
267 (if (and elmo-pop3-total-size
268 (> elmo-pop3-total-size
269 (min elmo-display-retrieval-progress-threshold 100)))
270 (elmo-display-progress
271 'elmo-display-retrieval-progress
272 (format "Retrieving (%d/%d bytes)..."
274 elmo-pop3-total-size)
275 (/ (buffer-size) (/ elmo-pop3-total-size 100)))))))
277 (defun elmo-pop3-auth-user (session)
278 (let ((process (elmo-network-session-process-internal session))
281 (elmo-pop3-send-command
283 (format "user %s" (elmo-network-session-user-internal session))
285 (setq response (elmo-pop3-read-response process t))
286 (unless (eq (car response) 'ok)
287 (signal 'elmo-open-error '(elmo-pop-auth-user)))
288 (elmo-pop3-send-command process
292 (elmo-network-session-password-key session)))
294 (setq response (elmo-pop3-read-response process t))
298 (error "Maildrop is currently in use"))
300 (error "Not allowed to login until the login delay period has expired"))
302 (signal 'elmo-authenticate-error '(elmo-pop-auth-user))))
305 (defun elmo-pop3-auth-apop (session)
306 (unless (string-match "^\+OK .*\\(<[^\>]+>\\)"
307 (elmo-network-session-greeting-internal session))
308 (signal 'elmo-open-error '(elmo-pop3-auth-apop)))
309 ;; good, APOP ready server
310 (elmo-pop3-send-command
311 (elmo-network-session-process-internal session)
313 (elmo-network-session-user-internal session)
315 (concat (match-string
317 (elmo-network-session-greeting-internal session))
319 (elmo-network-session-password-key session)))))
321 (let ((response (elmo-pop3-read-response
322 (elmo-network-session-process-internal session)
327 (error "Maildrop is currently in use"))
329 (error "Not allowed to login until the login delay period has expired"))
331 (signal 'elmo-authenticate-error '(elmo-pop-auth-apop))))
334 (luna-define-method elmo-network-initialize-session-buffer :after
335 ((session elmo-pop3-session) buffer)
336 (with-current-buffer buffer
337 (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
339 (luna-define-method elmo-network-initialize-session ((session
341 (let ((process (elmo-network-session-process-internal session))
343 (with-current-buffer (process-buffer process)
344 (set-process-filter process 'elmo-pop3-process-filter)
345 (setq elmo-pop3-read-point (point-min))
346 ;; Skip garbage output from process before greeting.
347 (while (and (memq (process-status process) '(open run))
348 (goto-char (point-max))
350 (not (looking-at "+OK")))
351 (accept-process-output process 1))
352 (setq elmo-pop3-read-point (point))
353 (or (elmo-network-session-set-greeting-internal
355 (cdr (elmo-pop3-read-response process t))) ; if ok, cdr is non-nil.
356 (signal 'elmo-open-error
357 '(elmo-network-intialize-session)))
358 (when (eq (elmo-network-stream-type-symbol
359 (elmo-network-session-stream-type-internal session))
361 (elmo-pop3-send-command process "stls")
362 (if (eq 'ok (car (elmo-pop3-read-response process)))
363 (starttls-negotiate process)
364 (signal 'elmo-open-error '(elmo-pop3-starttls-error)))))))
366 (luna-define-method elmo-network-authenticate-session ((session
368 (with-current-buffer (process-buffer
369 (elmo-network-session-process-internal session))
370 (let* ((process (elmo-network-session-process-internal session))
371 (auth (elmo-network-session-auth-internal session))
372 (auth (mapcar (lambda (mechanism) (upcase (symbol-name mechanism)))
373 (if (listp auth) auth (list auth)))))
374 (or (and (string= "USER" (car auth))
375 (elmo-pop3-auth-user session))
376 (and (string= "APOP" (car auth))
377 (elmo-pop3-auth-apop session))
378 (let (sasl-mechanisms
379 client name step response mechanism
380 sasl-read-passphrase)
382 (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
383 (setq mechanism (sasl-find-mechanism auth))
385 (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
389 (elmo-network-session-user-internal session)
391 (elmo-network-session-server-internal session)))
392 ;;; (if elmo-pop3-auth-user-realm
393 ;;; (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
394 (setq name (sasl-mechanism-name mechanism))
395 (elmo-network-session-set-auth-internal session
396 (intern (downcase name)))
397 (setq sasl-read-passphrase
400 (elmo-network-session-password-key session))))
401 (setq step (sasl-next-step client nil))
402 (elmo-pop3-send-command
405 (and (sasl-step-data step)
408 (elmo-base64-encode-string
409 (sasl-step-data step) 'no-line-break))))
413 (setq response (elmo-pop3-read-response process t))
417 (error "Maildrop is currently in use"))
419 (error "Not allowed to login \
420 until the login delay period has expired"))
422 (signal 'elmo-authenticate-error
423 (list (intern (concat "elmo-pop3-auth-"
424 (downcase name)))))))
425 (if (sasl-next-step client step)
427 (signal 'elmo-authenticate-error
429 (concat "elmo-pop3-auth-"
431 ;; The authentication process is finished.
435 (elmo-base64-decode-string
436 (cadr (split-string response " "))))
437 (setq step (sasl-next-step client step))
438 (elmo-pop3-send-command
440 (if (sasl-step-data step)
441 (elmo-base64-encode-string (sasl-step-data step)
443 "") nil 'no-log))))))))
445 (luna-define-method elmo-network-setup-session ((session
447 (let ((process (elmo-network-session-process-internal session))
449 (with-current-buffer (process-buffer process)
450 (setq elmo-pop3-size-hash (elmo-make-hash 31))
451 ;; To get obarray of uidl and size
452 (elmo-pop3-send-command process "list")
453 (if (null (cdr (elmo-pop3-read-response process)))
454 (error "POP LIST command failed"))
455 (if (null (setq response
456 (elmo-pop3-read-contents process)))
457 (error "POP LIST command failed"))
458 ;; POP server always returns a sequence of serial numbers.
459 (setq count (elmo-pop3-parse-list-response response))
461 (when elmo-pop3-use-uidl-internal
462 (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
463 (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
465 (elmo-pop3-send-command process "uidl")
466 (unless (cdr (elmo-pop3-read-response process))
467 (error "POP UIDL failed"))
468 (unless (setq response (elmo-pop3-read-contents process))
469 (error "POP UIDL failed"))
470 (elmo-pop3-parse-uidl-response response)))))
472 (defun elmo-pop3-read-contents (process)
473 (with-current-buffer (process-buffer process)
474 (let ((case-fold-search nil)
476 (goto-char elmo-pop3-read-point)
477 (while (not (re-search-forward "^\\.\r\n" nil t))
478 (accept-process-output process 1)
479 (goto-char elmo-pop3-read-point))
480 (setq match-end (point))
482 (buffer-substring elmo-pop3-read-point
485 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-pop3-folder))
486 (convert-standard-filename
488 (elmo-safe-filename (elmo-net-folder-user-internal folder))
489 (expand-file-name (elmo-net-folder-server-internal folder)
492 elmo-msgdb-directory)))))
494 (luna-define-method elmo-folder-exists-p ((folder elmo-pop3-folder))
495 (if (and elmo-pop3-exists-exactly
496 (elmo-folder-plugged-p folder))
498 (let (elmo-auto-change-plugged ; don't change plug status.
499 (elmo-inhibit-number-mapping t) ; No need to use uidl.
502 (setq session (elmo-pop3-get-session folder))
504 (elmo-network-close-session session)))))
505 (or (file-directory-p (elmo-folder-msgdb-path folder))
507 (when (elmo-folder-plugged-p folder)
508 (let ((elmo-pop3-exists-exactly t))
509 (elmo-folder-exists-p folder))))))
511 (defun elmo-pop3-parse-uidl-response (string)
512 (let ((buffer (current-buffer))
515 (let (number uid list)
517 (goto-char (point-min))
518 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([^ \n]+\\)$" nil t)
519 (setq number (elmo-match-buffer 1))
520 (setq uid (elmo-match-buffer 2))
521 (with-current-buffer buffer
522 (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
523 (elmo-set-hash-val (concat "#" number) uid
524 elmo-pop3-number-uidl-hash))
525 (setq list (cons uid list)))
526 (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
529 (defun elmo-pop3-parse-list-response (string)
530 (let ((buffer (current-buffer))
535 (goto-char (point-min))
536 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([0-9]+\\)$" nil t)
539 (cons (elmo-match-buffer 1)
540 (elmo-match-buffer 2))
542 (setq count (1+ count)))
543 (with-current-buffer buffer
544 (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
546 (elmo-set-hash-val (concat "#" (car (car alist)))
549 (setq alist (cdr alist)))
550 (setq elmo-pop3-list-done t))
553 (defun elmo-pop3-list-location (folder)
554 (with-current-buffer (process-buffer
555 (elmo-network-session-process-internal
556 (elmo-pop3-get-session folder)))
558 (if elmo-pop3-uidl-done
562 (setq locations (cons (symbol-name atom) locations)))
563 elmo-pop3-uidl-number-hash)
566 (< (elmo-pop3-uidl-to-number loc1)
567 (elmo-pop3-uidl-to-number loc2)))))
568 (error "POP3: Error in UIDL")))))
570 (defun elmo-pop3-list-folder-by-location (folder locations)
571 (let* ((location-alist (elmo-pop3-folder-location-alist-internal folder))
572 (locations-in-db (mapcar 'cdr location-alist))
573 result new-locs new-alist deleted-locs i)
575 (elmo-delete-if (function
576 (lambda (x) (member x locations-in-db)))
579 (elmo-delete-if (function
580 (lambda (x) (member x locations)))
582 (setq i (or (elmo-max-of-list (mapcar 'car location-alist)) 0))
587 (delq (rassoc x location-alist) location-alist))))
591 (setq new-alist (cons (cons i (car new-locs)) new-alist))
592 (setq new-locs (cdr new-locs)))
593 (setq result (nconc location-alist new-alist))
594 (setq result (sort result (lambda (x y) (< (car x)(car y)))))
595 (elmo-pop3-folder-set-location-alist-internal folder result)
596 (mapcar 'car result)))
598 (defun elmo-pop3-list-by-uidl-subr (folder &optional nonsort)
599 (let ((flist (elmo-pop3-list-folder-by-location
601 (elmo-pop3-list-location folder))))
603 (cons (elmo-max-of-list flist) (length flist))
606 (defun elmo-pop3-list-by-list (folder)
607 (with-current-buffer (process-buffer
608 (elmo-network-session-process-internal
609 (elmo-pop3-get-session folder)))
611 (if elmo-pop3-list-done
613 (mapatoms (lambda (atom)
614 (setq list (cons (string-to-int
615 (substring (symbol-name atom) 1))
619 (error "POP3: Error in list")))))
621 (defsubst elmo-pop3-folder-list-messages (folder)
622 (if (and (not elmo-inhibit-number-mapping)
623 (elmo-pop3-folder-use-uidl-internal folder))
624 (elmo-pop3-list-by-uidl-subr folder)
625 (elmo-pop3-list-by-list folder)))
627 (luna-define-method elmo-folder-list-messages-plugged
628 ((folder elmo-pop3-folder) &optional nohide)
629 (elmo-pop3-folder-list-messages folder))
631 (luna-define-method elmo-folder-status ((folder elmo-pop3-folder))
632 (elmo-folder-open-internal folder)
633 (elmo-folder-check folder)
634 (if (elmo-pop3-folder-use-uidl-internal folder)
636 (elmo-pop3-list-by-uidl-subr folder 'nonsort)
637 (elmo-folder-close-internal folder))
639 (elmo-network-session-process-internal
640 (elmo-pop3-get-session folder)))
643 (with-current-buffer (process-buffer process)
644 (elmo-pop3-send-command process "STAT")
645 (setq response (cdr (elmo-pop3-read-response process)))
646 ;; response: "^\+OK 2 7570$"
647 (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
648 (error "POP STAT command failed")
651 (substring response (match-beginning 1)(match-end 1 ))))
652 (elmo-folder-close-internal folder)
653 (cons total total))))))
655 (defvar elmo-pop3-header-fetch-chop-length 200)
657 (defsubst elmo-pop3-next-result-arrived-p ()
659 ((eq (following-char) ?+)
660 (if (re-search-forward "\n\\.\r?\n" nil t)
664 (if (search-forward "\n" nil t)
670 (defun elmo-pop3-retrieve-headers (process tobuffer articles)
672 (set-buffer (process-buffer process))
674 (let ((number (length articles))
677 (last-point (point-min)))
678 ;; Send HEAD commands.
680 (elmo-pop3-send-command process (format
681 "top %s 0" (car articles))
683 ;;; (accept-process-output process 1)
684 (setq articles (cdr articles))
685 (setq count (1+ count))
686 ;; Every 200 requests we have to read the stream in
687 ;; order to avoid deadlocks.
688 (when (or elmo-pop3-send-command-synchronously
689 (null articles) ;All requests have been sent.
690 (zerop (% count elmo-pop3-header-fetch-chop-length)))
691 (unless elmo-pop3-send-command-synchronously
692 (accept-process-output process 1))
695 (goto-char last-point)
697 (while (elmo-pop3-next-result-arrived-p)
698 (setq last-point (point))
699 (setq received (1+ received)))
701 (when (> number elmo-display-progress-threshold)
702 (if (or (zerop (% received 5)) (= received number))
703 (elmo-display-progress
704 'elmo-pop3-retrieve-headers "Getting headers..."
705 (/ (* received 100) number))))
706 (accept-process-output process 1)
707 ;;; (accept-process-output process)
709 ;; Replace all CRLF with LF.
710 (elmo-delete-cr-buffer)
711 (copy-to-buffer tobuffer (point-min) (point-max)))))
713 (luna-define-method elmo-folder-msgdb-create ((folder elmo-pop3-folder)
715 (let ((process (elmo-network-session-process-internal
716 (elmo-pop3-get-session folder))))
717 (with-current-buffer (process-buffer process)
718 (elmo-pop3-sort-msgdb-by-original-number
720 (elmo-pop3-msgdb-create-by-header
725 (if (elmo-pop3-folder-use-uidl-internal folder)
726 (elmo-pop3-folder-location-alist-internal folder)))))))
728 (defun elmo-pop3-sort-msgdb-by-original-number (folder msgdb)
729 (let ((location-alist (elmo-pop3-folder-location-alist-internal folder)))
731 (elmo-msgdb-sort-entities
733 (lambda (ent1 ent2 loc-alist)
734 (< (elmo-pop3-uidl-to-number
735 (cdr (assq (elmo-message-entity-number ent1)
737 (elmo-pop3-uidl-to-number
738 (cdr (assq (elmo-message-entity-number ent2)
743 (defun elmo-pop3-uidl-to-number (uidl)
744 (string-to-number (elmo-get-hash-val uidl
745 elmo-pop3-uidl-number-hash)))
747 (defun elmo-pop3-number-to-uidl (number)
748 (elmo-get-hash-val (format "#%d" number)
749 elmo-pop3-number-uidl-hash))
751 (defun elmo-pop3-number-to-size (number)
752 (elmo-get-hash-val (format "#%d" number)
753 elmo-pop3-size-hash))
755 (defun elmo-pop3-msgdb-create-by-header (folder process numlist
758 (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
759 (with-current-buffer (process-buffer process)
760 (if loc-alist ; use uidl.
766 (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
768 (elmo-pop3-retrieve-headers process tmp-buffer numlist)
770 (elmo-pop3-msgdb-create-message
776 flag-table loc-alist)
777 (kill-buffer tmp-buffer)))))
779 (defun elmo-pop3-msgdb-create-message (folder
787 (let ((new-msgdb (elmo-make-msgdb))
788 beg entity i number message-id flags)
790 (set-buffer-multibyte default-enable-multibyte-characters)
791 (goto-char (point-min))
793 (message "Creating msgdb...")
795 (setq beg (save-excursion (forward-line 1) (point)))
796 (elmo-pop3-next-result-arrived-p)
800 (narrow-to-region beg (point))
802 (elmo-msgdb-create-message-entity-from-buffer
803 (elmo-msgdb-message-entity-handler new-msgdb)
805 (setq numlist (cdr numlist))
807 (with-current-buffer (process-buffer process)
808 (elmo-message-entity-set-field
812 (elmo-pop3-number-to-size
813 (elmo-message-entity-number entity))))
817 (elmo-pop3-number-to-uidl
818 (elmo-message-entity-number entity))
820 (elmo-message-entity-set-number entity number)))
821 (setq message-id (elmo-message-entity-field entity 'message-id)
822 flags (elmo-flag-table-get flag-table message-id))
823 (elmo-global-flags-set flags folder number message-id)
824 (elmo-msgdb-append-entity new-msgdb entity flags))))
825 (when (> num elmo-display-progress-threshold)
827 (if (or (zerop (% i 5)) (= i num))
828 (elmo-display-progress
829 'elmo-pop3-msgdb-create-message "Creating msgdb..."
830 (/ (* i 100) num)))))
833 (defun elmo-pop3-read-body (process outbuf)
834 (with-current-buffer (process-buffer process)
835 (let ((start elmo-pop3-read-point)
838 (while (not (re-search-forward "^\\.\r?\n" nil t))
839 (accept-process-output process 1)
842 (with-current-buffer outbuf
844 (insert-buffer-substring (process-buffer process) start (- end 3)))
847 (luna-define-method elmo-folder-open-internal ((folder elmo-pop3-folder))
848 (if (and (not elmo-inhibit-number-mapping)
849 (elmo-pop3-folder-use-uidl-internal folder))
850 (elmo-pop3-folder-set-location-alist-internal
851 folder (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))))
853 (luna-define-method elmo-folder-commit :after ((folder elmo-pop3-folder))
854 (when (elmo-folder-persistent-p folder)
855 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
856 (elmo-pop3-folder-location-alist-internal
859 (luna-define-method elmo-folder-close-internal ((folder elmo-pop3-folder))
860 (elmo-pop3-folder-set-location-alist-internal folder nil)
861 ;; Just close connection
862 (elmo-folder-check folder))
864 (luna-define-method elmo-message-fetch-plugged ((folder elmo-pop3-folder)
868 (let* ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
869 (process (elmo-network-session-process-internal
870 (elmo-pop3-get-session folder)))
871 size response errmsg msg)
872 (with-current-buffer (process-buffer process)
874 (setq number (elmo-pop3-uidl-to-number
875 (cdr (assq number loc-alist)))))
876 (setq size (string-to-number
877 (elmo-pop3-number-to-size number)))
879 (elmo-pop3-send-command process
880 (format "retr %s" number))
881 (unless elmo-inhibit-display-retrieval-progress
882 (setq elmo-pop3-total-size size)
883 (elmo-display-progress
884 'elmo-display-retrieval-progress
885 (format "Retrieving (0/%d bytes)..." elmo-pop3-total-size)
889 (when (null (setq response (cdr (elmo-pop3-read-response
891 (error "Fetching message failed"))
892 (setq response (elmo-pop3-read-body process outbuf)))
893 (setq elmo-pop3-total-size nil))
894 (unless elmo-inhibit-display-retrieval-progress
895 (elmo-display-progress
896 'elmo-display-retrieval-progress
897 "Retrieving..." 100) ; remove progress bar.
898 (message "Retrieving...done"))
900 (goto-char (point-min))
901 (while (re-search-forward "^\\." nil t)
904 (elmo-delete-cr-buffer)
907 (defun elmo-pop3-delete-msg (process number loc-alist)
908 (with-current-buffer (process-buffer process)
909 (let (response errmsg msg)
911 (setq number (elmo-pop3-uidl-to-number
912 (cdr (assq number loc-alist)))))
915 (elmo-pop3-send-command process
916 (format "dele %s" number))
917 (when (null (setq response (cdr (elmo-pop3-read-response
919 (error "Deleting message failed")))
920 (error "Deleting message failed")))))
922 (luna-define-method elmo-folder-delete-messages-plugged ((folder
925 (let ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
926 (process (elmo-network-session-process-internal
927 (elmo-pop3-get-session folder))))
928 (mapcar '(lambda (msg) (elmo-pop3-delete-msg process msg loc-alist))
931 (luna-define-method elmo-message-use-cache-p ((folder elmo-pop3-folder) number)
934 (luna-define-method elmo-folder-persistent-p ((folder elmo-pop3-folder))
935 (and (elmo-folder-persistent-internal folder)
936 (elmo-pop3-folder-use-uidl-internal folder)))
938 (luna-define-method elmo-folder-clear :around ((folder elmo-pop3-folder)
939 &optional keep-killed)
941 (elmo-pop3-folder-set-location-alist-internal folder nil))
942 (luna-call-next-method))
944 (luna-define-method elmo-folder-check ((folder elmo-pop3-folder))
945 (if (elmo-folder-plugged-p folder)
946 (let ((session (elmo-pop3-get-session folder 'if-exists)))
948 (elmo-network-close-session session)))))
951 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
953 ;;; elmo-pop3.el ends here