* wl-expire.el (wl-summary-archive): Fixed;
[elisp/wanderlust.git] / elmo / elmo-pop3.el
1 ;;; elmo-pop3.el -- POP3 Interface for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1999,2000      Kenichi OKADA <okada@opaopa.org>
5
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;;      Kenichi OKADA <okada@opaopa.org>
8 ;; Keywords: mail, net news
9
10 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
11
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)
15 ;; any later version.
16 ;;
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.
21 ;;
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.
26 ;;
27
28 ;;; Commentary:
29 ;; 
30
31 ;;; Code:
32 ;; 
33
34 (require 'elmo-msgdb)
35 (require 'elmo-net)
36
37 (eval-when-compile
38   (require 'elmo-util))
39
40 (eval-and-compile
41   (autoload 'md5 "md5"))
42
43 (defcustom elmo-pop3-default-use-uidl t
44   "If non-nil, use UIDL on POP3."
45   :type 'boolean
46   :group 'elmo)
47
48 (defvar elmo-pop3-use-uidl-internal t
49   "(Internal switch for using UIDL on POP3).")
50
51 (defvar elmo-pop3-use-cache t
52   "Use cache in pop3 folder.")
53
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
57 set as non-nil.")
58
59 (defvar elmo-pop3-exists-exactly t)
60 (defvar sasl-mechanism-alist)
61
62 (defvar elmo-pop3-total-size nil)
63
64 ;; For debugging.
65 (defvar elmo-pop3-debug nil
66   "Non-nil forces POP3 folder as debug mode.
67 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
68
69 (defvar elmo-pop3-debug-inhibit-logging nil)
70
71 ;;; Debug
72 (defsubst elmo-pop3-debug (message &rest args)
73   (if elmo-pop3-debug
74       (with-current-buffer (get-buffer-create "*POP3 DEBUG*")
75         (goto-char (point-max))
76         (if elmo-pop3-debug-inhibit-logging
77             (insert "NO LOGGING\n")
78           (insert (apply 'format message args) "\n")))))
79
80 ;;; ELMO POP3 folder
81 (eval-and-compile
82   (luna-define-class elmo-pop3-folder (elmo-net-folder)
83                      (use-uidl location-alist))
84   (luna-define-internal-accessors 'elmo-pop3-folder))
85
86 (luna-define-method elmo-folder-initialize :around ((folder
87                                                      elmo-pop3-folder)
88                                                     name)
89   (let ((elmo-network-stream-type-alist
90          (if elmo-pop3-stream-type-alist
91              (append elmo-pop3-stream-type-alist
92                      elmo-network-stream-type-alist)
93            elmo-network-stream-type-alist)))
94     (setq name (luna-call-next-method))
95     ;; Setup slots for elmo-net-folder
96     (when (string-match "^\\([^:/!]*\\)\\(/[^/:@!]+\\)?\\(:[^/:@!]+\\)?" name)
97       (elmo-net-folder-set-user-internal folder
98                                          (if (match-beginning 1)
99                                              (elmo-match-string 1 name)))
100       (if (eq (length (elmo-net-folder-user-internal folder)) 0)
101           (elmo-net-folder-set-user-internal folder
102                                              elmo-pop3-default-user))
103       (elmo-net-folder-set-auth-internal
104        folder
105        (if (match-beginning 2)
106            (intern (elmo-match-substring 2 name 1))
107          elmo-pop3-default-authenticate-type))
108       (elmo-pop3-folder-set-use-uidl-internal
109        folder
110        (if (match-beginning 3)
111            (string= (elmo-match-substring 3 name 1) "uidl")
112          elmo-pop3-default-use-uidl)))
113     (unless (elmo-net-folder-server-internal folder)
114       (elmo-net-folder-set-server-internal folder 
115                                            elmo-pop3-default-server))
116     (unless (elmo-net-folder-port-internal folder)
117       (elmo-net-folder-set-port-internal folder
118                                          elmo-pop3-default-port))
119     (unless (elmo-net-folder-stream-type-internal folder)
120       (elmo-net-folder-set-stream-type-internal
121        folder
122        (elmo-get-network-stream-type
123         elmo-pop3-default-stream-type)))
124     folder))
125
126 ;;; POP3 session
127 (luna-define-class elmo-pop3-session (elmo-network-session) ())
128
129 ;; buffer-local
130 (defvar elmo-pop3-read-point nil)
131 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
132 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
133 (defvar elmo-pop3-size-hash nil) ; number -> size
134 (defvar elmo-pop3-uidl-done nil)
135 (defvar elmo-pop3-list-done nil)
136 (defvar elmo-pop3-lock nil)
137
138 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
139                                     elmo-pop3-uidl-number-hash
140                                     elmo-pop3-number-uidl-hash
141                                     elmo-pop3-uidl-done
142                                     elmo-pop3-size-hash
143                                     elmo-pop3-list-done
144                                     elmo-pop3-lock))
145
146 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
147   (when (elmo-network-session-process-internal session)
148     (when (memq (process-status
149                  (elmo-network-session-process-internal session))
150                 '(open run))
151       (let ((buffer (process-buffer 
152                      (elmo-network-session-process-internal session))))
153         (elmo-pop3-send-command (elmo-network-session-process-internal session)
154                                 "quit")
155         ;; process is dead.
156         (or (elmo-pop3-read-response
157              (elmo-network-session-process-internal session)
158              t)
159             (error "POP error: QUIT failed"))))
160     (kill-buffer (process-buffer
161                   (elmo-network-session-process-internal session)))
162     (delete-process (elmo-network-session-process-internal session))))
163
164 (defun elmo-pop3-get-session (folder &optional if-exists)
165   (let ((elmo-pop3-use-uidl-internal (if elmo-inhibit-number-mapping
166                                          nil
167                                        (elmo-pop3-folder-use-uidl-internal
168                                         folder))))
169     (elmo-network-get-session 'elmo-pop3-session "POP3" folder if-exists)))
170
171 (defun elmo-pop3-send-command (process command &optional no-erase)
172   (with-current-buffer (process-buffer process)
173     (unless no-erase
174       (erase-buffer))
175     (goto-char (point-min))
176     (setq elmo-pop3-read-point (point))
177     (elmo-pop3-debug "SEND: %s\n" command)
178     (process-send-string process command)
179     (process-send-string process "\r\n")))
180
181 (defun elmo-pop3-read-response (process &optional not-command)
182   ;; buffer is in case for process is dead.
183   (with-current-buffer (process-buffer process)
184     (let ((case-fold-search nil)
185           (response-string nil)
186           (response-continue t)
187           (return-value nil)
188           match-end)
189       (while response-continue
190         (goto-char elmo-pop3-read-point)
191         (while (not (re-search-forward "\r?\n" nil t))
192           (accept-process-output process)
193           (goto-char elmo-pop3-read-point))
194         (setq match-end (point))
195         (setq response-string
196               (buffer-substring elmo-pop3-read-point (- match-end 2)))
197         (goto-char elmo-pop3-read-point)
198         (if (looking-at "\\+.*$")
199             (progn
200               (setq response-continue nil)
201               (setq elmo-pop3-read-point match-end)
202               (setq return-value
203                     (if return-value
204                         (concat return-value "\n" response-string)
205                       response-string)))
206           (if (looking-at "\\-.*$")
207               (progn
208                 (setq response-continue nil)
209                 (setq elmo-pop3-read-point match-end)
210                 (setq return-value nil))
211             (setq elmo-pop3-read-point match-end)
212             (if not-command
213                 (setq response-continue nil))
214             (setq return-value
215                   (if return-value
216                       (concat return-value "\n" response-string)
217                     response-string)))
218           (setq elmo-pop3-read-point match-end)))
219       return-value)))
220
221 (defun elmo-pop3-process-filter (process output)
222   (save-excursion
223     (set-buffer (process-buffer process))
224     (goto-char (point-max))
225     (insert output)
226     (elmo-pop3-debug "RECEIVED: %s\n" output)
227     (if (and elmo-pop3-total-size
228              (> elmo-pop3-total-size 
229                 (min elmo-display-retrieval-progress-threshold 100)))
230         (elmo-display-progress
231          'elmo-display-retrieval-progress
232          (format "Retrieving (%d/%d bytes)..."
233                  (buffer-size)
234                  elmo-pop3-total-size)
235          (/ (buffer-size) (/ elmo-pop3-total-size 100))))))
236
237 (defun elmo-pop3-auth-user (session)
238   (let ((process (elmo-network-session-process-internal session)))
239     ;; try USER/PASS
240     (elmo-pop3-send-command
241      process
242      (format "user %s" (elmo-network-session-user-internal session)))
243     (or (elmo-pop3-read-response process t)
244         (signal 'elmo-authenticate-error
245                 '(elmo-pop-auth-user)))
246     (elmo-pop3-send-command  process
247                              (format
248                               "pass %s"
249                               (elmo-get-passwd
250                                (elmo-network-session-password-key session))))
251     (or (elmo-pop3-read-response process t)
252         (signal 'elmo-authenticate-error
253                 '(elmo-pop-auth-user)))))
254
255 (defun elmo-pop3-auth-apop (session)
256   (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
257                     (elmo-network-session-greeting-internal session))
258       ;; good, APOP ready server
259       (progn
260         (elmo-pop3-send-command
261          (elmo-network-session-process-internal session)
262          (format "apop %s %s"
263                  (elmo-network-session-user-internal session)
264                  (md5
265                   (concat (match-string
266                            1
267                            (elmo-network-session-greeting-internal session))
268                           (elmo-get-passwd
269                            (elmo-network-session-password-key session))))))
270         (or (elmo-pop3-read-response
271              (elmo-network-session-process-internal session)
272              t)
273             (signal 'elmo-authenticate-error
274                     '(elmo-pop3-auth-apop))))
275     (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
276     
277 (luna-define-method elmo-network-initialize-session-buffer :after
278   ((session elmo-pop3-session) buffer)
279   (with-current-buffer buffer
280     (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
281
282 (luna-define-method elmo-network-initialize-session ((session
283                                                       elmo-pop3-session))
284   (let ((process (elmo-network-session-process-internal session))
285         response mechanism)
286     (with-current-buffer (process-buffer process)
287       (set-process-filter process 'elmo-pop3-process-filter)
288       (setq elmo-pop3-read-point (point-min))
289       ;; Skip garbage output from process before greeting.
290       (while (and (memq (process-status process) '(open run))
291                   (goto-char (point-max))
292                   (forward-line -1)
293                   (not (looking-at "+OK")))
294         (accept-process-output process 1))
295       (setq elmo-pop3-read-point (point))
296       (or (elmo-network-session-set-greeting-internal
297            session
298            (elmo-pop3-read-response process t))
299           (signal 'elmo-open-error
300                   '(elmo-network-intialize-session)))
301       (when (eq (elmo-network-stream-type-symbol
302                  (elmo-network-session-stream-type-internal session))
303                 'starttls)
304         (elmo-pop3-send-command process "stls")
305         (if (string-match "^\+OK"
306                           (elmo-pop3-read-response process))
307             (starttls-negotiate process)
308           (signal 'elmo-open-error
309                   '(elmo-pop3-starttls-error)))))))
310
311 (luna-define-method elmo-network-authenticate-session ((session
312                                                         elmo-pop3-session))
313   (with-current-buffer (process-buffer 
314                         (elmo-network-session-process-internal session))
315     (let* ((process (elmo-network-session-process-internal session))
316            (elmo-pop3-debug-inhibit-logging t)
317            (auth (elmo-network-session-auth-internal session))
318            (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
319                          (if (listp auth) auth (list auth))))
320            sasl-mechanisms
321            client name step response mechanism
322            sasl-read-passphrase)
323       (or (and (string= "USER" (car auth))
324                (elmo-pop3-auth-user session))
325           (and (string= "APOP" (car auth))
326                (elmo-pop3-auth-apop session))
327           (progn
328             (require 'sasl)
329             (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
330             (setq mechanism (sasl-find-mechanism auth))
331             (unless mechanism
332               (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
333             (setq client
334                   (sasl-make-client
335                    mechanism
336                    (elmo-network-session-user-internal session)
337                    "pop"
338                    (elmo-network-session-server-internal session)))
339 ;;;         (if elmo-pop3-auth-user-realm
340 ;;;             (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
341             (setq name (sasl-mechanism-name mechanism))
342             (elmo-network-session-set-auth-internal session
343                                                     (intern (downcase name)))
344             (setq sasl-read-passphrase
345                   (function
346                    (lambda (prompt)
347                      (elmo-get-passwd
348                       (elmo-network-session-password-key session)))))
349             (setq step (sasl-next-step client nil))
350             (elmo-pop3-send-command
351              process
352              (concat "AUTH " name
353                      (and (sasl-step-data step)
354                           (concat
355                            " "
356                            (elmo-base64-encode-string
357                             (sasl-step-data step) 'no-line-break))))) ;)
358             (catch 'done
359               (while t
360                 (unless (setq response (elmo-pop3-read-response process t))
361                   ;; response is NO or BAD.
362                   (signal 'elmo-authenticate-error
363                           (list (intern
364                                  (concat "elmo-pop3-auth-"
365                                          (downcase name))))))
366                 (if (string-match "^\+OK" response)
367                     (if (sasl-next-step client step)
368                         ;; Bogus server?
369                         (signal 'elmo-authenticate-error
370                                 (list (intern
371                                        (concat "elmo-pop3-auth-"
372                                                (downcase name)))))
373                       ;; The authentication process is finished.
374                       (throw 'done nil)))
375                 (sasl-step-set-data
376                  step
377                  (elmo-base64-decode-string 
378                   (cadr (split-string response " "))))
379                 (setq step (sasl-next-step client step))
380                 (elmo-pop3-send-command
381                  process
382                  (if (sasl-step-data step)
383                      (elmo-base64-encode-string (sasl-step-data step)
384                                                 'no-line-break)
385                    "")))))))))
386
387 (luna-define-method elmo-network-setup-session ((session
388                                                  elmo-pop3-session))
389   (let ((process (elmo-network-session-process-internal session))
390         count response)
391     (with-current-buffer (process-buffer process)
392       (setq elmo-pop3-size-hash (elmo-make-hash 31))
393       ;; To get obarray of uidl and size
394       (elmo-pop3-send-command process "list")
395       (if (null (elmo-pop3-read-response process))
396           (error "POP LIST command failed"))
397       (if (null (setq response
398                       (elmo-pop3-read-contents
399                        (current-buffer) process)))
400           (error "POP LIST command failed"))
401       ;; POP server always returns a sequence of serial numbers.
402       (setq count (elmo-pop3-parse-list-response response))
403       ;; UIDL
404       (when elmo-pop3-use-uidl-internal
405         (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
406         (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
407         ;; UIDL
408         (elmo-pop3-send-command process "uidl")
409         (unless (elmo-pop3-read-response process)
410           (error "POP UIDL failed"))
411         (unless (setq response (elmo-pop3-read-contents
412                                 (current-buffer) process))
413           (error "POP UIDL failed"))
414         (elmo-pop3-parse-uidl-response response)))))
415
416 (defun elmo-pop3-read-contents (buffer process)
417   (with-current-buffer buffer
418     (let ((case-fold-search nil)
419           match-end)
420       (goto-char elmo-pop3-read-point)
421       (while (not (re-search-forward "^\\.\r\n" nil t))
422         (accept-process-output process)
423         (goto-char elmo-pop3-read-point))
424       (setq match-end (point))
425       (elmo-delete-cr
426        (buffer-substring elmo-pop3-read-point
427                          (- match-end 3))))))
428
429 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-pop3-folder))
430   (convert-standard-filename
431    (expand-file-name
432     (elmo-safe-filename (elmo-net-folder-user-internal folder))
433     (expand-file-name (elmo-net-folder-server-internal folder)
434                       (expand-file-name
435                        "pop"
436                        elmo-msgdb-dir)))))
437
438 (luna-define-method elmo-folder-exists-p ((folder elmo-pop3-folder))
439   (if (and elmo-pop3-exists-exactly
440            (elmo-folder-plugged-p folder))
441       (save-excursion
442         (let (elmo-auto-change-plugged  ; don't change plug status.
443               (elmo-inhibit-number-mapping t) ; No need to use uidl.
444               session)
445           (prog1
446               (setq session (elmo-pop3-get-session folder))
447             (if session
448                 (elmo-network-close-session session)))))
449     t))
450
451 (defun elmo-pop3-parse-uidl-response (string)
452   (let ((buffer (current-buffer))
453         number list size)
454     (with-temp-buffer
455       (let (number uid list)
456         (insert string)
457         (goto-char (point-min))
458         (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([^ \n]+\\)$" nil t)
459           (setq number  (elmo-match-buffer 1))
460           (setq uid (elmo-match-buffer 2))
461           (with-current-buffer buffer
462             (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
463             (elmo-set-hash-val (concat "#" number) uid
464                                elmo-pop3-number-uidl-hash))
465           (setq list (cons uid list)))
466         (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
467         (nreverse list)))))
468
469 (defun elmo-pop3-parse-list-response (string)
470   (let ((buffer (current-buffer))
471         (count 0)
472         alist)
473     (with-temp-buffer
474       (insert string)
475       (goto-char (point-min))
476       (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
477         (setq alist
478               (cons
479                (cons (elmo-match-buffer 1)
480                      (elmo-match-buffer 2))
481                alist))
482         (setq count (1+ count)))
483       (with-current-buffer buffer
484         (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
485         (while alist
486           (elmo-set-hash-val (concat "#" (car (car alist)))
487                              (cdr (car alist))
488                              elmo-pop3-size-hash)
489           (setq alist (cdr alist)))
490         (setq elmo-pop3-list-done t))
491       count)))
492
493 (defun elmo-pop3-list-location (folder)
494   (with-current-buffer (process-buffer
495                         (elmo-network-session-process-internal
496                          (elmo-pop3-get-session folder)))
497     (let (locations)
498       (if elmo-pop3-uidl-done
499           (progn
500             (mapatoms
501              (lambda (atom)
502                (setq locations (cons (symbol-name atom) locations)))
503              elmo-pop3-uidl-number-hash)
504             (sort locations
505                   (lambda (loc1 loc2)
506                     (< (elmo-pop3-uidl-to-number loc1)
507                        (elmo-pop3-uidl-to-number loc2)))))
508         (error "POP3: Error in UIDL")))))
509
510 (defun elmo-pop3-list-folder-by-location (folder locations)
511   (let* ((location-alist (elmo-pop3-folder-location-alist-internal folder))
512          (locations-in-db (mapcar 'cdr location-alist))
513          result new-locs new-alist deleted-locs i)
514     (setq new-locs
515           (elmo-delete-if (function
516                            (lambda (x) (member x locations-in-db)))
517                           locations))
518     (setq deleted-locs
519           (elmo-delete-if (function
520                            (lambda (x) (member x locations)))
521                           locations-in-db))
522     (setq i (or (elmo-max-of-list (mapcar 'car location-alist)) 0))
523     (mapcar
524      (function
525       (lambda (x)
526         (setq location-alist
527               (delq (rassoc x location-alist) location-alist))))
528      deleted-locs)
529     (while new-locs
530       (setq i (1+ i))
531       (setq new-alist (cons (cons i (car new-locs)) new-alist))
532       (setq new-locs (cdr new-locs)))
533     (setq result (nconc location-alist new-alist))
534     (setq result (sort result (lambda (x y) (< (car x)(car y)))))
535     (elmo-pop3-folder-set-location-alist-internal folder result)
536     (mapcar 'car result)))
537
538 (defun elmo-pop3-list-by-uidl-subr (folder &optional nonsort)
539   (let ((flist (elmo-pop3-list-folder-by-location
540                 folder
541                 (elmo-pop3-list-location folder))))
542     (if nonsort
543         (cons (elmo-max-of-list flist) (length flist))
544       (sort flist '<))))
545
546 (defun elmo-pop3-list-by-list (folder)
547   (with-current-buffer (process-buffer
548                         (elmo-network-session-process-internal
549                          (elmo-pop3-get-session folder)))
550     (let (list)
551       (if elmo-pop3-list-done
552           (progn
553             (mapatoms (lambda (atom)
554                         (setq list (cons (string-to-int
555                                           (substring (symbol-name atom) 1))
556                                          list)))
557                       elmo-pop3-size-hash)
558             (sort list '<))
559         (error "POP3: Error in list")))))
560
561 (defsubst elmo-pop3-folder-list-messages (folder)
562   (if (and (not elmo-inhibit-number-mapping)
563            (elmo-pop3-folder-use-uidl-internal folder))
564       (elmo-pop3-list-by-uidl-subr folder)
565     (elmo-pop3-list-by-list folder)))
566
567 (luna-define-method elmo-folder-list-messages-internal
568   ((folder elmo-pop3-folder) &optional nohide)
569   (elmo-pop3-folder-list-messages folder))
570
571 (luna-define-method elmo-folder-status ((folder elmo-pop3-folder))
572   (elmo-folder-open-internal folder)
573   (elmo-folder-check folder)
574   (if (elmo-pop3-folder-use-uidl-internal folder)
575       (prog1
576           (elmo-pop3-list-by-uidl-subr folder 'nonsort)
577         (elmo-folder-close-internal folder))
578     (let* ((process
579             (elmo-network-session-process-internal
580              (elmo-pop3-get-session folder)))
581            (total 0)
582            response)
583       (with-current-buffer (process-buffer process)
584         (elmo-pop3-send-command process "STAT")
585         (setq response (elmo-pop3-read-response process))
586         ;; response: "^\+OK 2 7570$"
587         (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
588             (error "POP STAT command failed")
589           (setq total
590                 (string-to-int
591                  (substring response (match-beginning 1)(match-end 1 ))))
592           (elmo-folder-close-internal folder)
593           (cons total total))))))
594
595 (defvar elmo-pop3-header-fetch-chop-length 200)
596
597 (defsubst elmo-pop3-next-result-arrived-p ()
598   (cond
599    ((eq (following-char) ?+)
600     (if (re-search-forward "\n\\.\r?\n" nil t)
601         t
602       nil))
603    ((looking-at "-")
604     (if (search-forward "\n" nil t)
605         t
606       nil))
607    (t
608     nil)))
609
610 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
611   (save-excursion
612     (set-buffer buffer)
613     (erase-buffer)
614     (let ((number (length articles))
615           (count 0)
616           (received 0)
617           (last-point (point-min)))
618       ;; Send HEAD commands.
619       (while articles
620         (elmo-pop3-send-command process (format
621                                          "top %s 0" (car articles))
622                                 'no-erase)
623 ;;;     (accept-process-output process 1)
624         (setq articles (cdr articles))
625         (setq count (1+ count))
626         ;; Every 200 requests we have to read the stream in
627         ;; order to avoid deadlocks.
628         (when (or elmo-pop3-send-command-synchronously
629                   (null articles)       ;All requests have been sent.
630                   (zerop (% count elmo-pop3-header-fetch-chop-length)))
631           (unless elmo-pop3-send-command-synchronously
632             (accept-process-output process 1))
633           (discard-input)
634           (while (progn
635                    (set-buffer buffer)
636                    (goto-char last-point)
637                    ;; Count replies.
638                    (while (elmo-pop3-next-result-arrived-p)
639                      (setq last-point (point))
640                      (setq received (1+ received)))
641                    (< received count))
642             (when (> number elmo-display-progress-threshold)
643               (if (or (zerop (% received 5)) (= received number))
644                   (elmo-display-progress
645                    'elmo-pop3-retrieve-headers "Getting headers..."
646                    (/ (* received 100) number))))
647             (accept-process-output process 1)
648 ;;;         (accept-process-output process)
649             (discard-input))))
650       ;; Remove all "\r"'s.
651       (goto-char (point-min))
652       (while (search-forward "\r\n" nil t)
653         (replace-match "\n"))
654       (copy-to-buffer tobuffer (point-min) (point-max)))))
655
656 (luna-define-method elmo-folder-msgdb-create ((folder elmo-pop3-folder)
657                                               numlist new-mark
658                                               already-mark seen-mark
659                                               important-mark seen-list)
660   (let ((process (elmo-network-session-process-internal
661                   (elmo-pop3-get-session folder))))
662     (with-current-buffer (process-buffer process)
663       (elmo-pop3-sort-msgdb-by-original-number
664        folder
665        (elmo-pop3-msgdb-create-by-header
666         process
667         numlist
668         new-mark already-mark
669         seen-mark seen-list
670         (if (elmo-pop3-folder-use-uidl-internal folder)
671             (elmo-pop3-folder-location-alist-internal folder)))))))
672
673 (defun elmo-pop3-sort-overview-by-original-number (overview loc-alist)
674   (if loc-alist
675       (sort overview
676             (lambda (ent1 ent2)
677               (< (elmo-pop3-uidl-to-number
678                   (cdr (assq (elmo-msgdb-overview-entity-get-number ent1)
679                              loc-alist)))
680                  (elmo-pop3-uidl-to-number
681                   (cdr (assq (elmo-msgdb-overview-entity-get-number ent2)
682                              loc-alist))))))
683     overview))
684
685 (defun elmo-pop3-sort-msgdb-by-original-number (folder msgdb)
686   (message "Sorting...")
687   (let ((overview (elmo-msgdb-get-overview msgdb)))
688     (setq overview (elmo-pop3-sort-overview-by-original-number
689                     overview
690                     (elmo-pop3-folder-location-alist-internal folder)))
691     (message "Sorting...done")
692     (list overview (nth 1 msgdb)(nth 2 msgdb)(nth 3 msgdb))))
693
694 (defun elmo-pop3-uidl-to-number (uidl)
695   (string-to-number (elmo-get-hash-val uidl
696                                        elmo-pop3-uidl-number-hash)))
697
698 (defun elmo-pop3-number-to-uidl (number)
699   (elmo-get-hash-val (format "#%d" number)
700                      elmo-pop3-number-uidl-hash))
701
702 (defun elmo-pop3-number-to-size (number)
703   (elmo-get-hash-val (format "#%d" number)
704                      elmo-pop3-size-hash))
705
706 (defun elmo-pop3-msgdb-create-by-header (process numlist
707                                                  new-mark already-mark
708                                                  seen-mark
709                                                  seen-list
710                                                  loc-alist)
711   (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
712     (with-current-buffer (process-buffer process)
713       (if loc-alist ; use uidl.
714           (setq numlist
715                 (delq
716                  nil
717                  (mapcar
718                   (lambda (number)
719                     (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
720                   numlist))))
721       (elmo-pop3-retrieve-headers (process-buffer process)
722                                   tmp-buffer process numlist)
723       (prog1
724           (elmo-pop3-msgdb-create-message
725            tmp-buffer
726            process
727            (length numlist)
728            numlist
729            new-mark already-mark seen-mark seen-list loc-alist)
730         (kill-buffer tmp-buffer)))))
731
732 (defun elmo-pop3-msgdb-create-message (buffer
733                                        process
734                                        num
735                                        numlist new-mark already-mark
736                                        seen-mark
737                                        seen-list
738                                        loc-alist)
739   (save-excursion
740     (let (beg overview number-alist mark-alist
741               entity i number message-id gmark seen size)
742       (set-buffer buffer)
743       (elmo-set-buffer-multibyte default-enable-multibyte-characters)
744       (goto-char (point-min))
745       (setq i 0)
746       (message "Creating msgdb...")
747       (while (not (eobp))
748         (setq beg (save-excursion (forward-line 1) (point)))
749         (elmo-pop3-next-result-arrived-p)
750         (save-excursion
751           (forward-line -1)
752           (save-restriction
753             (narrow-to-region beg (point))
754             (setq entity
755                   (elmo-msgdb-create-overview-from-buffer
756                    (car numlist)))
757             (setq numlist (cdr numlist))
758             (when entity
759               (setq overview
760                     (elmo-msgdb-append-element
761                      overview entity))
762               (with-current-buffer (process-buffer process)
763                 (elmo-msgdb-overview-entity-set-size
764                  entity
765                  (string-to-number
766                   (elmo-pop3-number-to-size
767                    (elmo-msgdb-overview-entity-get-number entity))))
768                 (if (setq number
769                           (car
770                            (rassoc
771                             (elmo-pop3-number-to-uidl
772                              (elmo-msgdb-overview-entity-get-number entity))
773                             loc-alist)))
774                     (elmo-msgdb-overview-entity-set-number entity number)))
775               (setq number-alist
776                     (elmo-msgdb-number-add
777                      number-alist
778                      (elmo-msgdb-overview-entity-get-number entity)
779                      (car entity)))
780               (setq message-id (car entity))
781               (setq seen (member message-id seen-list))
782               (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
783                                   (if (elmo-file-cache-status
784                                        (elmo-file-cache-get message-id))
785                                       (if seen
786                                           nil
787                                         already-mark)
788                                     (if seen
789                                         (if elmo-pop3-use-cache
790                                             seen-mark)
791                                       new-mark))))
792                   (setq mark-alist
793                         (elmo-msgdb-mark-append
794                          mark-alist
795                          (elmo-msgdb-overview-entity-get-number entity)
796                          gmark))))))
797         (when (> num elmo-display-progress-threshold)
798           (setq i (1+ i))
799           (if (or (zerop (% i 5)) (= i num))
800               (elmo-display-progress
801                'elmo-pop3-msgdb-create-message "Creating msgdb..."
802                (/ (* i 100) num)))))
803       (list overview number-alist mark-alist))))
804
805 (defun elmo-pop3-read-body (process outbuf)
806   (with-current-buffer (process-buffer process)
807     (let ((start elmo-pop3-read-point)
808           end)
809       (goto-char start)
810       (while (not (re-search-forward "^\\.\r?\n" nil t))
811         (accept-process-output process)
812         (goto-char start))
813       (setq end (point))
814       (with-current-buffer outbuf
815         (erase-buffer)
816         (insert-buffer-substring (process-buffer process) start (- end 3))))))
817
818 (luna-define-method elmo-folder-open-internal ((folder elmo-pop3-folder))
819   (if (and (not elmo-inhibit-number-mapping)
820            (elmo-pop3-folder-use-uidl-internal folder))
821       (elmo-pop3-folder-set-location-alist-internal
822        folder (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))))
823
824 (luna-define-method elmo-folder-commit :after ((folder elmo-pop3-folder))
825   (when (elmo-folder-persistent-p folder)
826     (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
827                               (elmo-pop3-folder-location-alist-internal
828                                folder))))
829
830 (luna-define-method elmo-folder-close-internal ((folder elmo-pop3-folder))
831   (elmo-pop3-folder-set-location-alist-internal folder nil)
832   ;; Just close connection
833   (elmo-folder-check folder))
834
835 (luna-define-method elmo-message-fetch-plugged ((folder elmo-pop3-folder)
836                                                 number strategy
837                                                 &optional section
838                                                 outbuf unseen)
839   (let* ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
840          (process (elmo-network-session-process-internal
841                    (elmo-pop3-get-session folder)))
842         size  response errmsg msg)
843     (with-current-buffer (process-buffer process)
844       (if loc-alist
845           (setq number (elmo-pop3-uidl-to-number
846                         (cdr (assq number loc-alist)))))
847       (setq size (string-to-number
848                   (elmo-pop3-number-to-size number)))
849       (when number
850         (elmo-pop3-send-command process
851                                 (format "retr %s" number))
852         (unless elmo-inhibit-display-retrieval-progress
853           (setq elmo-pop3-total-size size)
854           (elmo-display-progress
855            'elmo-display-retrieval-progress
856            (format "Retrieving (0/%d bytes)..." elmo-pop3-total-size)
857            0))
858         (unwind-protect
859             (progn
860               (when (null (setq response (elmo-pop3-read-response
861                                           process t)))
862                 (error "Fetching message failed"))
863               (setq response (elmo-pop3-read-body process outbuf)))
864           (setq elmo-pop3-total-size nil))
865         (unless elmo-inhibit-display-retrieval-progress
866           (elmo-display-progress
867            'elmo-display-retrieval-progress "" 100)  ; remove progress bar.
868           (message "Retrieving...done."))
869         (set-buffer outbuf)
870         (goto-char (point-min))
871         (while (re-search-forward "^\\." nil t)
872           (replace-match "")
873           (forward-line))
874         response))))
875
876 (defun elmo-pop3-delete-msg (process number loc-alist)
877   (with-current-buffer (process-buffer process)
878     (let (response errmsg msg)
879       (if loc-alist
880           (setq number (elmo-pop3-uidl-to-number
881                         (cdr (assq number loc-alist)))))
882       (if number
883           (progn
884             (elmo-pop3-send-command process
885                                     (format "dele %s" number))
886             (when (null (setq response (elmo-pop3-read-response
887                                         process t)))
888               (error "Deleting message failed")))
889         (error "Deleting message failed")))))
890
891 (luna-define-method elmo-folder-delete-messages ((folder elmo-pop3-folder)
892                                                       msgs)
893   (let ((loc-alist (elmo-pop3-folder-location-alist-internal folder))
894         (process (elmo-network-session-process-internal
895                   (elmo-pop3-get-session folder))))
896     (mapcar '(lambda (msg) (elmo-pop3-delete-msg
897                             process msg loc-alist))
898             msgs)))
899
900 (luna-define-method elmo-message-use-cache-p ((folder elmo-pop3-folder) number)
901   elmo-pop3-use-cache)
902
903 (luna-define-method elmo-folder-persistent-p ((folder elmo-pop3-folder))
904   (and (elmo-folder-persistent-internal folder)
905        (elmo-pop3-folder-use-uidl-internal folder)))
906
907 (luna-define-method elmo-folder-clear :around ((folder elmo-pop3-folder)
908                                                &optional keep-killed)
909   (unless keep-killed
910     (elmo-pop3-folder-set-location-alist-internal folder nil))
911   (luna-call-next-method))
912
913 (luna-define-method elmo-folder-check ((folder elmo-pop3-folder))
914   (if (elmo-folder-plugged-p folder)
915       (let ((session (elmo-pop3-get-session folder 'if-exists)))
916         (when session
917           (elmo-network-close-session session)))))
918
919 (require 'product)
920 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
921
922 ;;; elmo-pop3.el ends here