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.
43 (autoload 'md5
"md5"))
45 (defcustom elmo-pop3-default-use-uidl t
46 "If non-nil, use UIDL on POP3."
50 (defvar elmo-pop3-use-uidl-internal t
51 "(Internal switch for using UIDL on POP3).")
53 (defvar elmo-pop3-use-cache t
54 "Use cache in pop3 folder.")
56 (defvar elmo-pop3-send-command-synchronously nil
57 "If non-nil, commands are send synchronously.
58 If server doesn't accept asynchronous commands, this variable should be
61 (defcustom elmo-pop3-exists-exactly nil
62 "If non-nil, POP3 folder existence is checked everytime before the session."
66 (defconst elmo-pop3-folder-name-syntax
`(([user
".+"])
68 (?
: [uidl
"^[A-Za-z]+$"])
69 ,@elmo-net-folder-name-syntax
))
71 (defvar sasl-mechanism-alist
)
73 (defvar elmo-pop3-retrieve-progress-reporter nil
)
76 (defvar elmo-pop3-debug nil
77 "Non-nil forces POP3 folder as debug mode.
78 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
81 (defsubst elmo-pop3-debug
(message &rest args
)
83 (let ((biff (string-match "BIFF-" (buffer-name)))
85 (with-current-buffer (get-buffer-create (concat "*POP3 DEBUG*"
87 (goto-char (point-max))
89 (insert (apply 'format message args
) "\n")))))
93 (luna-define-class elmo-pop3-folder
(elmo-net-folder elmo-location-map
)
95 (luna-define-internal-accessors 'elmo-pop3-folder
))
97 (defsubst elmo-pop3-folder-use-uidl
(folder)
98 (if elmo-inhibit-number-mapping
100 (elmo-pop3-folder-use-uidl-internal folder
)))
102 (luna-define-method elmo-folder-initialize
((folder elmo-pop3-folder
) name
)
103 (let ((elmo-network-stream-type-alist
104 (if elmo-pop3-stream-type-alist
105 (append elmo-pop3-stream-type-alist
106 elmo-network-stream-type-alist
)
107 elmo-network-stream-type-alist
))
109 (setq tokens
(car (elmo-parse-separated-tokens
111 elmo-pop3-folder-name-syntax
)))
113 (elmo-net-folder-set-user-internal folder
114 (or (cdr (assq 'user tokens
))
115 elmo-pop3-default-user
))
117 (setq auth
(cdr (assq 'auth tokens
)))
118 (elmo-net-folder-set-auth-internal folder
120 (intern (downcase auth
))
121 elmo-pop3-default-authenticate-type
))
123 (setq uidl
(cdr (assq 'uidl tokens
)))
124 (elmo-pop3-folder-set-use-uidl-internal folder
126 (string= uidl
"uidl")
127 elmo-pop3-default-use-uidl
))
129 (elmo-net-folder-set-parameters
132 (list :server elmo-pop3-default-server
133 :port elmo-pop3-default-port
135 (elmo-get-network-stream-type elmo-pop3-default-stream-type
)))
139 (luna-define-class elmo-pop3-session
(elmo-network-session) ())
142 (defvar elmo-pop3-read-point nil
)
143 (defvar elmo-pop3-number-uidl-hash nil
) ; number -> uidl
144 (defvar elmo-pop3-uidl-number-hash nil
) ; uidl -> number
145 (defvar elmo-pop3-size-hash nil
) ; number -> size
146 (defvar elmo-pop3-uidl-done nil
)
147 (defvar elmo-pop3-list-done nil
)
148 (defvar elmo-pop3-lock nil
)
150 (defvar elmo-pop3-local-variables
'(elmo-pop3-read-point
151 elmo-pop3-uidl-number-hash
152 elmo-pop3-number-uidl-hash
158 (luna-define-method elmo-network-close-session
((session elmo-pop3-session
))
159 (when (elmo-network-session-process-internal session
)
160 (when (memq (process-status
161 (elmo-network-session-process-internal session
))
163 (elmo-pop3-send-command (elmo-network-session-process-internal session
)
166 (or (cdr (elmo-pop3-read-response
167 (elmo-network-session-process-internal session
)
169 (error "POP error: QUIT failed")))
170 (kill-buffer (process-buffer
171 (elmo-network-session-process-internal session
)))
172 (delete-process (elmo-network-session-process-internal session
))))
174 (defun elmo-pop3-get-session (folder &optional if-exists
)
175 "Get POP3 session for FOLDER.
176 If IF-EXISTS is non-nil, don't get new session.
177 If IF-EXISTS is `any-exists', get BIFF session or normal session if exists."
178 (let ((elmo-pop3-use-uidl-internal (elmo-pop3-folder-use-uidl folder
)))
180 (if (eq if-exists
'any-exists
)
181 (or (elmo-network-get-session 'elmo-pop3-session
184 (elmo-network-get-session 'elmo-pop3-session
187 (elmo-network-get-session 'elmo-pop3-session
189 (if (elmo-folder-biff-internal folder
)
193 ;; For saving existency.
194 (unless (file-exists-p (elmo-folder-msgdb-path folder
))
195 (elmo-make-directory (elmo-folder-msgdb-path folder
))))))
197 (defun elmo-pop3-send-command (process command
&optional no-erase no-log
)
198 (with-current-buffer (process-buffer process
)
201 (goto-char (point-min))
202 (setq elmo-pop3-read-point
(point))
203 (elmo-pop3-debug "SEND: %s\n" (if no-log
"<NO LOGGING>" command
))
204 (process-send-string process
(concat command
"\r\n"))))
206 (defun elmo-pop3-read-response (process &optional not-command
)
207 "Read response and return a cons cell of \(CODE . BODY\).
208 PROCESS is the process to read response from.
209 If optional NOT-COMMAND is non-nil, read only the first line.
210 CODE is one of the following:
211 'ok ... response is OK.
212 'err ... response is ERROR.
213 'login-delay ... user is not allowed to login until the login delay
215 'in-use ... authentication was successful but the mailbox is in use."
216 ;; buffer is in case for process is dead.
217 (with-current-buffer (process-buffer process
)
218 (let ((case-fold-search nil
)
219 (response-string nil
)
220 (response-continue t
)
224 (while response-continue
225 (goto-char elmo-pop3-read-point
)
226 (while (not (re-search-forward "\r?\n" nil t
))
227 (accept-process-output process
1)
228 (goto-char elmo-pop3-read-point
))
229 (setq match-end
(point))
230 (setq response-string
231 (buffer-substring elmo-pop3-read-point
(- match-end
2)))
232 (goto-char elmo-pop3-read-point
)
233 (if (looking-at "\\+.*$")
235 (setq response-continue nil
)
236 (setq elmo-pop3-read-point match-end
)
239 (concat return-value
"\n" response-string
)
241 (if (looking-at "\\-.*$")
243 (when (looking-at "[^ ]+ \\[\\([^]]+\\)\\]")
247 (buffer-substring (match-beginning 1)
250 response-continue nil
251 elmo-pop3-read-point match-end
252 return-value
(cons (or return-value
'err
) nil
)))
253 (setq elmo-pop3-read-point match-end
)
255 (setq response-continue nil
))
258 (concat return-value
"\n" response-string
)
260 (setq elmo-pop3-read-point match-end
)))
263 (cons 'ok return-value
)))))
265 (defun elmo-pop3-process-filter (process output
)
266 (when (buffer-live-p (process-buffer process
))
267 (with-current-buffer (process-buffer process
)
268 (goto-char (point-max))
270 (elmo-pop3-debug "RECEIVED: %s\n" output
)
271 (when elmo-pop3-retrieve-progress-reporter
272 (elmo-progress-notify 'elmo-retrieve-message
:set
(buffer-size))))))
274 (defun elmo-pop3-auth-user (session)
275 (let ((process (elmo-network-session-process-internal session
))
278 (elmo-pop3-send-command
280 (format "user %s" (elmo-network-session-user-internal session
))
282 (setq response
(elmo-pop3-read-response process t
))
283 (unless (eq (car response
) 'ok
)
284 (signal 'elmo-open-error
'(elmo-pop-auth-user)))
285 (elmo-pop3-send-command process
289 (elmo-network-session-password-key session
)))
291 (setq response
(elmo-pop3-read-response process t
))
295 (error "Maildrop is currently in use"))
297 (error "Not allowed to login until the login delay period has expired"))
299 (signal 'elmo-authenticate-error
'(elmo-pop-auth-user))))
302 (defun elmo-pop3-auth-apop (session)
303 (unless (string-match "^\+OK .*\\(<[=!-;?-~]+@[=!-;?-~]+>\\)"
304 (elmo-network-session-greeting-internal session
))
305 (signal 'elmo-open-error
'(elmo-pop3-auth-apop)))
306 ;; good, APOP ready server
307 (elmo-pop3-send-command
308 (elmo-network-session-process-internal session
)
310 (elmo-network-session-user-internal session
)
312 (concat (match-string
314 (elmo-network-session-greeting-internal session
))
316 (elmo-network-session-password-key session
)))))
318 (let ((response (elmo-pop3-read-response
319 (elmo-network-session-process-internal session
)
324 (error "Maildrop is currently in use"))
326 (error "Not allowed to login until the login delay period has expired"))
328 (signal 'elmo-authenticate-error
'(elmo-pop-auth-apop))))
331 (luna-define-method elmo-network-initialize-session-buffer
:after
332 ((session elmo-pop3-session
) buffer
)
333 (with-current-buffer buffer
334 (mapcar 'make-variable-buffer-local elmo-pop3-local-variables
)))
336 (luna-define-method elmo-network-initialize-session
((session
338 (let ((process (elmo-network-session-process-internal session
))
340 (with-current-buffer (process-buffer process
)
341 (set-process-filter process
'elmo-pop3-process-filter
)
342 (setq elmo-pop3-read-point
(point-min))
343 ;; Skip garbage output from process before greeting.
344 (while (and (memq (process-status process
) '(open run
))
345 (goto-char (point-max))
347 (not (looking-at "+OK")))
348 (accept-process-output process
1))
349 (setq elmo-pop3-read-point
(point))
350 (or (elmo-network-session-set-greeting-internal
352 (cdr (elmo-pop3-read-response process t
))) ; if ok, cdr is non-nil.
353 (signal 'elmo-open-error
354 '(elmo-network-intialize-session)))
355 (when (eq (elmo-network-stream-type-symbol
356 (elmo-network-session-stream-type-internal session
))
358 (elmo-pop3-send-command process
"stls")
359 (if (eq 'ok
(car (elmo-pop3-read-response process
)))
360 (starttls-negotiate process
)
361 (signal 'elmo-open-error
'(elmo-pop3-starttls-error)))))))
363 (luna-define-method elmo-network-authenticate-session
((session
365 (with-current-buffer (process-buffer
366 (elmo-network-session-process-internal session
))
367 (let* ((process (elmo-network-session-process-internal session
))
368 (auth (elmo-network-session-auth-internal session
))
369 (auth (mapcar (lambda (mechanism) (upcase (symbol-name mechanism
)))
370 (if (listp auth
) auth
(list auth
)))))
371 (or (and (string= "USER" (car auth
))
372 (elmo-pop3-auth-user session
))
373 (and (string= "APOP" (car auth
))
374 (elmo-pop3-auth-apop session
))
375 (let (sasl-mechanisms
376 client name step response mechanism
377 sasl-read-passphrase
)
379 (setq sasl-mechanisms
(mapcar 'car sasl-mechanism-alist
))
380 (setq mechanism
(sasl-find-mechanism auth
))
382 (signal 'elmo-authenticate-error
'(elmo-pop3-auth-no-mechanisms)))
386 (elmo-network-session-user-internal session
)
388 (elmo-network-session-server-internal session
)))
389 ;;; (if elmo-pop3-auth-user-realm
390 ;;; (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
391 (setq name
(sasl-mechanism-name mechanism
))
392 (elmo-network-session-set-auth-internal session
393 (intern (downcase name
)))
394 (setq sasl-read-passphrase
397 (elmo-network-session-password-key session
))))
398 (setq step
(sasl-next-step client nil
))
399 (elmo-pop3-send-command
402 (and (sasl-step-data step
)
405 (elmo-base64-encode-string
406 (sasl-step-data step
) 'no-line-break
))))
410 (setq response
(elmo-pop3-read-response process t
))
414 (error "Maildrop is currently in use"))
416 (error "Not allowed to login \
417 until the login delay period has expired"))
419 (signal 'elmo-authenticate-error
420 (list (intern (concat "elmo-pop3-auth-"
421 (downcase name
)))))))
422 (if (sasl-next-step client step
)
424 (signal 'elmo-authenticate-error
426 (concat "elmo-pop3-auth-"
428 ;; The authentication process is finished.
432 (elmo-base64-decode-string
433 (cadr (split-string response
" "))))
434 (setq step
(sasl-next-step client step
))
435 (elmo-pop3-send-command
437 (if (sasl-step-data step
)
438 (elmo-base64-encode-string (sasl-step-data step
)
440 "") nil
'no-log
))))))))
442 (luna-define-method elmo-network-setup-session
((session
444 (let ((process (elmo-network-session-process-internal session
))
446 (with-current-buffer (process-buffer process
)
447 (setq elmo-pop3-size-hash
(elmo-make-hash 31))
448 ;; To get obarray of uidl and size
449 (elmo-pop3-send-command process
"list")
450 (if (null (cdr (elmo-pop3-read-response process
)))
451 (error "POP LIST command failed"))
452 (if (null (setq response
453 (elmo-pop3-read-contents process
)))
454 (error "POP LIST command failed"))
455 ;; POP server always returns a sequence of serial numbers.
456 (setq count
(elmo-pop3-parse-list-response response
))
458 (when elmo-pop3-use-uidl-internal
459 (setq elmo-pop3-uidl-number-hash
(elmo-make-hash (* count
2)))
460 (setq elmo-pop3-number-uidl-hash
(elmo-make-hash (* count
2)))
462 (elmo-pop3-send-command process
"uidl")
463 (unless (cdr (elmo-pop3-read-response process
))
464 (error "POP UIDL failed"))
465 (unless (setq response
(elmo-pop3-read-contents process
))
466 (error "POP UIDL failed"))
467 (elmo-pop3-parse-uidl-response response
)))))
469 (defun elmo-pop3-read-contents (process)
470 (with-current-buffer (process-buffer process
)
471 (let ((case-fold-search nil
)
472 (point elmo-pop3-read-point
))
473 (while (and (goto-char (- point
2))
474 (not (search-forward "\r\n.\r\n" nil t
)))
475 (setq point
(max (- (point-max) 2) ; Care of \r\n.\r[EOF] case
476 elmo-pop3-read-point
))
477 (accept-process-output process
1))
479 (buffer-substring elmo-pop3-read-point
482 (luna-define-method elmo-folder-expand-msgdb-path
((folder elmo-pop3-folder
))
483 (convert-standard-filename
485 (elmo-safe-filename (elmo-net-folder-user-internal folder
))
486 (expand-file-name (elmo-net-folder-server-internal folder
)
489 elmo-msgdb-directory
)))))
491 (luna-define-method elmo-folder-exists-p
((folder elmo-pop3-folder
))
492 (if (and elmo-pop3-exists-exactly
493 (elmo-folder-plugged-p folder
))
495 (let (elmo-auto-change-plugged ; don't change plug status.
496 (elmo-inhibit-number-mapping t
) ; No need to use uidl.
499 (setq session
(elmo-pop3-get-session folder
))
501 (elmo-network-close-session session
)))))
502 (or (file-directory-p (elmo-folder-msgdb-path folder
))
504 (when (elmo-folder-plugged-p folder
)
505 (let ((elmo-pop3-exists-exactly t
))
506 (elmo-folder-exists-p folder
))))))
508 (defun elmo-pop3-parse-uidl-response (string)
509 (let ((buffer (current-buffer))
512 (let (number uid list
)
514 (goto-char (point-min))
515 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([^ \n]+\\)$" nil t
)
516 (setq number
(elmo-match-buffer 1))
517 (setq uid
(elmo-match-buffer 2))
518 (with-current-buffer buffer
519 (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash
)
520 (elmo-set-hash-val (concat "#" number
) uid
521 elmo-pop3-number-uidl-hash
))
522 (setq list
(cons uid list
)))
523 (with-current-buffer buffer
(setq elmo-pop3-uidl-done t
))
526 (defun elmo-pop3-parse-list-response (string)
527 (let ((buffer (current-buffer))
532 (goto-char (point-min))
533 (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([0-9]+\\)$" nil t
)
536 (cons (elmo-match-buffer 1)
537 (elmo-match-buffer 2))
539 (setq count
(1+ count
)))
540 (with-current-buffer buffer
541 (setq elmo-pop3-size-hash
(elmo-make-hash (* (length alist
) 2)))
543 (elmo-set-hash-val (concat "#" (car (car alist
)))
546 (setq alist
(cdr alist
)))
547 (setq elmo-pop3-list-done t
))
550 (defun elmo-pop3-list-location (folder)
551 (with-current-buffer (process-buffer
552 (elmo-network-session-process-internal
553 (elmo-pop3-get-session folder
)))
555 (if elmo-pop3-uidl-done
559 (setq locations
(cons (symbol-name atom
) locations
)))
560 elmo-pop3-uidl-number-hash
)
563 (< (elmo-pop3-uidl-to-number loc1
)
564 (elmo-pop3-uidl-to-number loc2
)))))
565 (error "POP3: Error in UIDL")))))
567 (defun elmo-pop3-list-folder-by-location (folder locations
)
568 (mapcar #'car
(elmo-location-map-update folder locations
)))
570 (defun elmo-pop3-list-by-uidl-subr (folder &optional nonsort
)
571 (let ((flist (elmo-pop3-list-folder-by-location
573 (elmo-pop3-list-location folder
))))
575 (cons (elmo-max-of-list flist
) (length flist
))
578 (defun elmo-pop3-list-by-list (folder)
579 (with-current-buffer (process-buffer
580 (elmo-network-session-process-internal
581 (elmo-pop3-get-session folder
)))
583 (if elmo-pop3-list-done
585 (mapatoms (lambda (atom)
586 (setq list
(cons (string-to-number
587 (substring (symbol-name atom
) 1))
591 (error "POP3: Error in list")))))
593 (defsubst elmo-pop3-folder-list-messages
(folder)
594 (if (elmo-pop3-folder-use-uidl folder
)
595 (elmo-pop3-list-by-uidl-subr folder
)
596 (elmo-pop3-list-by-list folder
)))
598 (luna-define-method elmo-folder-list-messages-plugged
599 ((folder elmo-pop3-folder
) &optional nohide
)
600 (elmo-pop3-folder-list-messages folder
))
602 (luna-define-method elmo-folder-status
((folder elmo-pop3-folder
))
603 (elmo-folder-open-internal folder
)
604 (elmo-folder-check folder
)
605 (if (elmo-pop3-folder-use-uidl folder
)
607 (elmo-pop3-list-by-uidl-subr folder
'nonsort
)
608 (elmo-folder-close-internal folder
))
610 (elmo-network-session-process-internal
611 (elmo-pop3-get-session folder
)))
614 (with-current-buffer (process-buffer process
)
615 (elmo-pop3-send-command process
"STAT")
616 (setq response
(cdr (elmo-pop3-read-response process
)))
617 ;; response: "^\+OK 2 7570$"
618 (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response
))
619 (error "POP STAT command failed")
622 (substring response
(match-beginning 1)(match-end 1 ))))
623 (elmo-folder-close-internal folder
)
624 (cons total total
))))))
626 (defvar elmo-pop3-header-fetch-chop-length
200)
628 (defsubst elmo-pop3-next-result-arrived-p
()
630 ((eq (following-char) ?
+)
631 (if (re-search-forward "\n\\.\r?\n" nil t
)
635 (if (search-forward "\n" nil t
)
641 (defun elmo-pop3-retrieve-headers (process tobuffer articles
)
643 (set-buffer (process-buffer process
))
647 (last-point (point-min)))
648 (elmo-with-progress-display (elmo-retrieve-header (length articles
))
650 ;; Send HEAD commands.
652 (elmo-pop3-send-command process
653 (format "top %s 0" (car articles
))
655 ;;; (accept-process-output process 1)
656 (setq articles
(cdr articles
))
657 (setq count
(1+ count
))
658 ;; Every 200 requests we have to read the stream in
659 ;; order to avoid deadlocks.
660 (when (or elmo-pop3-send-command-synchronously
661 (null articles
) ;All requests have been sent.
662 (zerop (% count elmo-pop3-header-fetch-chop-length
)))
663 (unless elmo-pop3-send-command-synchronously
664 (accept-process-output process
1))
667 (goto-char last-point
)
669 (while (elmo-pop3-next-result-arrived-p)
670 (setq last-point
(point))
671 (setq received
(1+ received
)))
673 (elmo-progress-notify 'elmo-retrieve-header
:set received
)
674 (accept-process-output process
1)
675 ;;; (accept-process-output process)
677 ;; Replace all CRLF with LF.
678 (elmo-delete-cr-buffer)
679 (copy-to-buffer tobuffer
(point-min) (point-max)))))
681 (luna-define-method elmo-folder-msgdb-create
((folder elmo-pop3-folder
)
683 (let ((process (elmo-network-session-process-internal
684 (elmo-pop3-get-session folder
))))
685 (with-current-buffer (process-buffer process
)
686 (elmo-pop3-msgdb-create-by-header
692 (defun elmo-pop3-uidl-to-number (uidl)
693 (string-to-number (elmo-get-hash-val uidl
694 elmo-pop3-uidl-number-hash
)))
696 (defun elmo-pop3-number-to-uidl (number)
697 (elmo-get-hash-val (format "#%d" number
)
698 elmo-pop3-number-uidl-hash
))
700 (defun elmo-pop3-number-to-size (number)
702 (elmo-get-hash-val (format "#%d" number
) elmo-pop3-size-hash
)))
704 (defun elmo-pop3-msgdb-create-by-header (folder process numlist
706 (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
708 (with-current-buffer (process-buffer process
)
709 (when (elmo-pop3-folder-use-uidl folder
)
715 (elmo-pop3-uidl-to-number
716 (elmo-map-message-location folder number
)))
718 (elmo-pop3-retrieve-headers process tmp-buffer numlist
)
719 (elmo-pop3-msgdb-create-message
726 (kill-buffer tmp-buffer
))))
728 (defun elmo-pop3-msgdb-create-message (folder
735 (let ((new-msgdb (elmo-make-msgdb))
736 beg entity number message-id flags
)
738 (set-buffer-multibyte default-enable-multibyte-characters
)
739 (goto-char (point-min))
740 (elmo-with-progress-display (elmo-folder-msgdb-create num
)
743 (setq beg
(save-excursion (forward-line 1) (point)))
744 (elmo-pop3-next-result-arrived-p)
748 (narrow-to-region beg
(point))
750 (elmo-msgdb-create-message-entity-from-buffer
751 (elmo-msgdb-message-entity-handler new-msgdb
)
753 (setq numlist
(cdr numlist
))
755 (with-current-buffer (process-buffer process
)
756 (elmo-message-entity-set-field
759 (elmo-pop3-number-to-size
760 (elmo-message-entity-number entity
)))
762 (elmo-map-message-number
764 (elmo-pop3-number-to-uidl
765 (elmo-message-entity-number entity
))))
766 (elmo-message-entity-set-number entity number
)))
767 (setq message-id
(elmo-message-entity-field entity
'message-id
)
768 flags
(elmo-flag-table-get flag-table message-id
))
769 (elmo-global-flags-set flags folder number message-id
)
770 (elmo-msgdb-append-entity new-msgdb entity flags
))))
771 (elmo-progress-notify 'elmo-folder-msgdb-create
)))
774 (defun elmo-pop3-read-body (process outbuf
)
775 (with-current-buffer (process-buffer process
)
776 (let ((start elmo-pop3-read-point
)
779 (while (not (re-search-forward "^\\.\r?\n" nil t
))
780 (accept-process-output process
1)
783 (with-current-buffer outbuf
785 (insert-buffer-substring (process-buffer process
) start
(- end
3)))
788 (luna-define-method elmo-folder-open-internal
((folder elmo-pop3-folder
))
789 (when (elmo-pop3-folder-use-uidl folder
)
790 (elmo-location-map-load folder
(elmo-folder-msgdb-path folder
))))
792 (luna-define-method elmo-folder-commit
:after
((folder elmo-pop3-folder
))
793 (when (and (not elmo-inhibit-number-mapping
)
794 (elmo-folder-persistent-p folder
))
795 (elmo-location-map-save folder
(elmo-folder-msgdb-path folder
))))
797 (luna-define-method elmo-folder-close-internal
((folder elmo-pop3-folder
))
798 (elmo-location-map-teardown folder
)
799 ;; Just close connection
800 (elmo-folder-check folder
))
802 (luna-define-method elmo-message-fetch-plugged
((folder elmo-pop3-folder
)
806 (let ((process (elmo-network-session-process-internal
807 (elmo-pop3-get-session folder
)))
808 size response errmsg msg
)
809 (with-current-buffer (process-buffer process
)
810 (when (elmo-pop3-folder-use-uidl folder
)
811 (setq number
(elmo-pop3-uidl-to-number
812 (elmo-map-message-location folder number
))))
813 (setq size
(elmo-pop3-number-to-size number
))
815 (elmo-with-progress-display
816 (elmo-retrieve-message size elmo-pop3-retrieve-progress-reporter
)
818 (elmo-pop3-send-command process
(format "retr %s" number
))
819 (when (null (setq response
(cdr (elmo-pop3-read-response
821 (error "Fetching message failed"))
822 (setq response
(elmo-pop3-read-body process outbuf
)))
824 (goto-char (point-min))
825 (while (re-search-forward "^\\." nil t
)
828 (elmo-delete-cr-buffer)
831 (defun elmo-pop3-delete-msg (process number
)
833 (error "Deleting message failed"))
834 (elmo-pop3-send-command process
(format "dele %s" number
))
835 (when (null (cdr (elmo-pop3-read-response process t
)))
836 (error "Deleting message failed")))
838 (luna-define-method elmo-folder-delete-messages-plugged
((folder
841 (let ((process (elmo-network-session-process-internal
842 (elmo-pop3-get-session folder
))))
843 (with-current-buffer (process-buffer process
)
844 (dolist (number (if (elmo-pop3-folder-use-uidl folder
)
847 (elmo-pop3-uidl-to-number
848 (elmo-map-message-location folder number
)))
851 (elmo-pop3-delete-msg process number
))
854 (luna-define-method elmo-message-use-cache-p
((folder elmo-pop3-folder
) number
)
857 (luna-define-method elmo-folder-persistent-p
((folder elmo-pop3-folder
))
858 (and (elmo-folder-persistent-internal folder
)
859 (elmo-pop3-folder-use-uidl-internal folder
)))
861 (luna-define-method elmo-folder-clear
:around
((folder elmo-pop3-folder
)
862 &optional keep-killed
)
864 (elmo-location-map-setup folder
))
865 (luna-call-next-method))
867 (luna-define-method elmo-folder-check
((folder elmo-pop3-folder
))
868 (if (elmo-folder-plugged-p folder
)
869 (let ((session (elmo-pop3-get-session folder
'if-exists
)))
871 (elmo-network-close-session session
)))))
874 (product-provide (provide 'elmo-pop3
) (require 'elmo-version
))
876 ;;; elmo-pop3.el ends here