(elmo-pop3-process-filter): Show retrieve progress.
[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 (defvar elmo-pop3-exists-exactly t)
44 (defvar sasl-mechanism-alist)
45
46 (luna-define-class elmo-pop3-session (elmo-network-session))
47
48 ;; buffer-local
49 (defvar elmo-pop3-read-point nil)
50 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
51 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
52 (defvar elmo-pop3-size-hash nil) ; number -> size
53 (defvar elmo-pop3-uidl-done nil)
54 (defvar elmo-pop3-list-done nil)
55
56 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
57                                     elmo-pop3-uidl-number-hash
58                                     elmo-pop3-number-uidl-hash
59                                     elmo-pop3-uidl-done
60                                     elmo-pop3-size-hash
61                                     elmo-pop3-list-done))
62
63 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
64   (when (elmo-network-session-process-internal session)
65     (when (memq (process-status
66                  (elmo-network-session-process-internal session))
67                 '(open run))
68       (elmo-pop3-send-command (elmo-network-session-process-internal session)
69                               "quit")
70       (or (elmo-pop3-read-response
71            (elmo-network-session-process-internal session) t)
72           (error "POP error: QUIT failed")))
73     (kill-buffer (process-buffer
74                   (elmo-network-session-process-internal session)))
75     (delete-process (elmo-network-session-process-internal session))))
76
77 (defun elmo-pop3-get-session (spec &optional if-exists)
78   (elmo-network-get-session
79    'elmo-pop3-session
80    "POP3"
81    (elmo-pop3-spec-hostname spec)
82    (elmo-pop3-spec-port spec)
83    (elmo-pop3-spec-username spec)
84    (elmo-pop3-spec-auth spec)
85    (elmo-pop3-spec-stream-type spec)
86    if-exists))
87
88 (defun elmo-pop3-send-command (process command &optional no-erase)
89   (with-current-buffer (process-buffer process)
90     (unless no-erase
91       (erase-buffer))
92     (goto-char (point-min))
93     (setq elmo-pop3-read-point (point))
94     (process-send-string process command)
95     (process-send-string process "\r\n")))
96
97 (defun elmo-pop3-read-response (process &optional not-command)
98   (with-current-buffer (process-buffer process)
99     (let ((case-fold-search nil)
100           (response-string nil)
101           (response-continue t)
102           (return-value nil)
103           match-end)
104       (while response-continue
105         (goto-char elmo-pop3-read-point)
106         (while (not (re-search-forward "\r?\n" nil t))
107           (accept-process-output process)
108           (goto-char elmo-pop3-read-point))
109         (setq match-end (point))
110         (setq response-string
111               (buffer-substring elmo-pop3-read-point (- match-end 2)))
112         (goto-char elmo-pop3-read-point)
113         (if (looking-at "\\+.*$")
114             (progn
115               (setq response-continue nil)
116               (setq elmo-pop3-read-point match-end)
117               (setq return-value
118                     (if return-value
119                         (concat return-value "\n" response-string)
120                       response-string)))
121           (if (looking-at "\\-.*$")
122               (progn
123                 (setq response-continue nil)
124                 (setq elmo-pop3-read-point match-end)
125                 (setq return-value nil))
126             (setq elmo-pop3-read-point match-end)
127             (if not-command
128                 (setq response-continue nil))
129             (setq return-value
130                   (if return-value
131                       (concat return-value "\n" response-string)
132                     response-string)))
133           (setq elmo-pop3-read-point match-end)))
134       return-value)))
135
136 (defun elmo-pop3-process-filter (process output)
137   (save-excursion
138     (set-buffer (process-buffer process))
139     (goto-char (point-max))
140     (insert output)
141     (message "Retrieving...(%d bytes)." (buffer-size))))
142
143 (defun elmo-pop3-auth-user (session)
144   (let ((process (elmo-network-session-process-internal session)))
145     ;; try USER/PASS
146     (elmo-pop3-send-command
147      process
148      (format "user %s" (elmo-network-session-user-internal session)))
149     (or (elmo-pop3-read-response process t)
150         (signal 'elmo-authenticate-error
151                 '(elmo-pop-auth-user)))
152     (elmo-pop3-send-command  process
153                              (format
154                               "pass %s"
155                               (elmo-get-passwd
156                                (elmo-network-session-password-key session))))
157     (or (elmo-pop3-read-response process t)
158         (signal 'elmo-authenticate-error
159                 '(elmo-pop-auth-user)))))
160
161 (defun elmo-pop3-auth-apop (session)
162   (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
163                     (elmo-network-session-greeting-internal session))
164       ;; good, APOP ready server
165       (progn
166         (elmo-pop3-send-command
167          (elmo-network-session-process-internal session)
168          (format "apop %s %s"
169                  (elmo-network-session-user-internal session)
170                  (md5
171                   (concat (match-string
172                            1
173                            (elmo-network-session-greeting-internal session))
174                           (elmo-get-passwd
175                            (elmo-network-session-password-key session))))))
176         (or (elmo-pop3-read-response
177              (elmo-network-session-process-internal session)
178              t)
179             (signal 'elmo-authenticate-error
180                     '(elmo-pop3-auth-apop))))
181     (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
182     
183 (luna-define-method elmo-network-initialize-session-buffer :after
184   ((session elmo-pop3-session) buffer)
185   (with-current-buffer buffer
186     (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
187
188 (luna-define-method elmo-network-initialize-session ((session
189                                                       elmo-pop3-session))
190   (let ((process (elmo-network-session-process-internal session))
191         response mechanism)
192     (with-current-buffer (process-buffer process)
193       (set-process-filter process 'elmo-pop3-process-filter)
194       (setq elmo-pop3-read-point (point-min))
195       ;; Skip garbage output from process before greeting.
196       (while (and (memq (process-status process) '(open run))
197                   (goto-char (point-max))
198                   (forward-line -1)
199                   (not (looking-at "+OK")))
200         (accept-process-output process 1))
201       (setq elmo-pop3-read-point (point))
202       (or (elmo-network-session-set-greeting-internal
203            session
204            (elmo-pop3-read-response process t))
205           (signal 'elmo-open-error
206                   '(elmo-network-intialize-session)))
207       (when (eq (elmo-network-stream-type-symbol
208                  (elmo-network-session-stream-type-internal session))
209                 'starttls)
210         (elmo-pop3-send-command process "stls")
211         (if (string-match "^\+OK"
212                           (elmo-pop3-read-response process))
213             (starttls-negotiate process)
214           (signal 'elmo-open-error
215                   '(elmo-pop3-starttls-error)))))))
216
217 (luna-define-method elmo-network-authenticate-session ((session
218                                                         elmo-pop3-session))
219   (with-current-buffer (process-buffer 
220                         (elmo-network-session-process-internal session))
221     (let* ((process (elmo-network-session-process-internal session))
222            (auth (elmo-network-session-auth-internal session))
223            (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
224                          (if (listp auth) auth (list auth))))
225            sasl-mechanisms
226            client name step response mechanism
227            sasl-read-passphrase)
228       (or (and (string= "USER" (car auth))
229                (elmo-pop3-auth-user session))
230           (and (string= "APOP" (car auth))
231                (elmo-pop3-auth-apop session))
232           (progn
233             (require 'sasl)
234             (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
235             (setq mechanism (sasl-find-mechanism auth))
236             (unless mechanism
237               (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
238             (setq client
239                   (sasl-make-client
240                    mechanism
241                    (elmo-network-session-user-internal session)
242                    "pop"
243                    (elmo-network-session-host-internal session)))
244 ;;;         (if elmo-pop3-auth-user-realm
245 ;;;             (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
246             (setq name (sasl-mechanism-name mechanism))
247             (elmo-network-session-set-auth-internal session
248                                                     (intern (downcase name)))
249             (setq sasl-read-passphrase
250                   (function
251                    (lambda (prompt)
252                      (elmo-get-passwd
253                       (elmo-network-session-password-key session)))))
254             (setq step (sasl-next-step client nil))
255             (elmo-pop3-send-command
256              process
257              (concat "AUTH " name
258                      (and (sasl-step-data step)
259                           (concat
260                            " "
261                            (elmo-base64-encode-string
262                             (sasl-step-data step) 'no-line-break))))) ;)
263             (catch 'done
264               (while t
265                 (unless (setq response (elmo-pop3-read-response process t))
266                   ;; response is NO or BAD.
267                   (signal 'elmo-authenticate-error
268                           (list (intern
269                                  (concat "elmo-pop3-auth-"
270                                          (downcase name))))))
271                 (if (string-match "^\+OK" response)
272                     (if (sasl-next-step client step)
273                         ;; Bogus server?
274                         (signal 'elmo-authenticate-error
275                                 (list (intern
276                                        (concat "elmo-pop3-auth-"
277                                                (downcase name)))))
278                       ;; The authentication process is finished.
279                       (throw 'done nil)))
280                 (sasl-step-set-data
281                  step
282                  (elmo-base64-decode-string 
283                   (cadr (split-string response " "))))
284                 (setq step (sasl-next-step client step))
285                 (elmo-pop3-send-command
286                  process
287                  (if (sasl-step-data step)
288                      (elmo-base64-encode-string (sasl-step-data step)
289                                                 'no-line-break)
290                    "")))))))))
291
292 (luna-define-method elmo-network-setup-session ((session
293                                                  elmo-pop3-session))
294   (let ((process (elmo-network-session-process-internal session))
295         count response)
296     (with-current-buffer (process-buffer process)
297       (setq elmo-pop3-size-hash (elmo-make-hash 31))
298       ;; To get obarray of uidl and size
299       (elmo-pop3-send-command process "list")
300       (if (null (elmo-pop3-read-response process))
301           (error "POP LIST command failed"))
302       (if (null (setq response
303                       (elmo-pop3-read-contents
304                        (current-buffer) process)))
305           (error "POP LIST command failed"))
306       ;; POP server always returns a sequence of serial numbers.
307       (setq count (elmo-pop3-parse-list-response response))
308       ;; UIDL
309       (when elmo-pop3-use-uidl
310         (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
311         (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
312         ;; UIDL
313         (elmo-pop3-send-command process "uidl")
314         (unless (elmo-pop3-read-response process)
315           (error "POP UIDL failed"))
316         (unless (setq response (elmo-pop3-read-contents
317                                 (current-buffer) process))
318           (error "POP UIDL failed"))
319         (elmo-pop3-parse-uidl-response response)))))
320
321 (defun elmo-pop3-read-contents (buffer process)
322   (save-excursion
323     (set-buffer buffer)
324     (let ((case-fold-search nil)
325           match-end)
326       (goto-char elmo-pop3-read-point)
327       (while (not (re-search-forward "^\\.\r\n" nil t))
328         (accept-process-output process)
329         (goto-char elmo-pop3-read-point))
330       (setq match-end (point))
331       (elmo-delete-cr
332        (buffer-substring elmo-pop3-read-point
333                          (- match-end 3))))))
334
335 ;; dummy functions
336 (defun elmo-pop3-list-folders (spec &optional hierarchy) nil)
337 (defun elmo-pop3-append-msg (spec string) nil nil)
338 (defun elmo-pop3-folder-creatable-p (spec) nil)
339 (defun elmo-pop3-create-folder (spec) nil)
340
341 (defun elmo-pop3-folder-exists-p (spec)
342   (if (and elmo-pop3-exists-exactly
343            (elmo-pop3-plugged-p spec))
344       (save-excursion
345         (let (elmo-auto-change-plugged ; don't change plug status.
346               elmo-pop3-use-uidl       ; No need to use uidl.
347               session)
348           (prog1
349               (setq session (elmo-pop3-get-session spec))
350             (if session
351                 (elmo-network-close-session session)))))
352     t))
353
354 (defun elmo-pop3-parse-uidl-response (string)
355   (let ((buffer (current-buffer))
356         number list size)
357     (with-temp-buffer
358       (let (number uid list)
359         (insert string)
360         (goto-char (point-min))
361         (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([^ \n]+\\)$" nil t)
362           (setq number  (elmo-match-buffer 1))
363           (setq uid (elmo-match-buffer 2))
364           (with-current-buffer buffer
365             (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
366             (elmo-set-hash-val (concat "#" number) uid
367                                elmo-pop3-number-uidl-hash))
368           (setq list (cons uid list)))
369         (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
370         (nreverse list)))))
371
372 (defun elmo-pop3-parse-list-response (string)
373   (let ((buffer (current-buffer))
374         (count 0)
375         alist)
376     (with-temp-buffer
377       (insert string)
378       (goto-char (point-min))
379       (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
380         (setq alist
381               (cons
382                (cons (elmo-match-buffer 1)
383                      (elmo-match-buffer 2))
384                alist))
385         (setq count (1+ count)))
386       (with-current-buffer buffer
387         (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
388         (while alist
389           (elmo-set-hash-val (concat "#" (car (car alist)))
390                              (cdr (car alist))
391                              elmo-pop3-size-hash)
392           (setq alist (cdr alist)))
393         (setq elmo-pop3-list-done t))
394       count)))
395
396 (defun elmo-pop3-list-location (spec)
397   (with-current-buffer (process-buffer
398                         (elmo-network-session-process-internal
399                          (elmo-pop3-get-session spec)))
400     (let (list)
401       (if elmo-pop3-uidl-done
402           (progn
403             (mapatoms
404              (lambda (atom)
405                (setq list (cons (symbol-name atom) list)))
406              elmo-pop3-uidl-number-hash)
407             (nreverse list))
408         (error "POP3: Error in UIDL")))))
409
410 (defun elmo-pop3-list-by-uidl-subr (spec &optional nonsort)
411   (let ((flist (elmo-list-folder-by-location
412                 spec
413                 (elmo-pop3-list-location spec))))
414     (if nonsort
415         (cons (elmo-max-of-list flist) (length flist))
416       (sort flist '<))))
417
418 (defun elmo-pop3-list-by-list (spec)
419   (with-current-buffer (process-buffer
420                         (elmo-network-session-process-internal
421                          (elmo-pop3-get-session spec)))
422     (let (list)
423       (if elmo-pop3-list-done
424           (progn
425             (mapatoms (lambda (atom)
426                         (setq list (cons (string-to-int
427                                           (substring (symbol-name atom) 1))
428                                          list)))
429                       elmo-pop3-size-hash)
430             (sort list '<))
431         (error "POP3: Error in list")))))
432
433 (defun elmo-pop3-list-folder (spec &optional nohide)
434   (let ((killed (and elmo-use-killed-list
435                      (elmo-msgdb-killed-list-load
436                       (elmo-msgdb-expand-path spec))))
437         numbers)
438     (elmo-pop3-commit spec)
439     (setq numbers (if elmo-pop3-use-uidl
440                       (progn
441                         (elmo-pop3-list-by-uidl-subr spec))
442                     (elmo-pop3-list-by-list spec)))
443     (elmo-living-messages numbers killed)))
444
445 (defun elmo-pop3-max-of-folder (spec)
446   (elmo-pop3-commit spec)
447   (if elmo-pop3-use-uidl
448       (elmo-pop3-list-by-uidl-subr spec 'nonsort)
449     (let* ((process
450             (elmo-network-session-process-internal
451              (elmo-pop3-get-session spec)))
452            (total 0)
453            response)
454       (with-current-buffer (process-buffer process)
455         (elmo-pop3-send-command process "STAT")
456         (setq response (elmo-pop3-read-response process))
457         ;; response: "^\+OK 2 7570$"
458         (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
459             (error "POP STAT command failed")
460           (setq total
461                 (string-to-int
462                  (substring response (match-beginning 1)(match-end 1 ))))
463           (cons total total))))))
464
465 (defvar elmo-pop3-header-fetch-chop-length 200)
466
467 (defsubst elmo-pop3-next-result-arrived-p ()
468   (cond
469    ((eq (following-char) ?+)
470     (if (re-search-forward "\n\\.\r?\n" nil t)
471         t
472       nil))
473    ((looking-at "-")
474     (if (search-forward "\n" nil t)
475         t
476       nil))
477    (t
478     nil)))
479      
480 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
481   (save-excursion
482     (set-buffer buffer)
483     (erase-buffer)
484     (let ((number (length articles))
485           (count 0)
486           (received 0)
487           (last-point (point-min)))
488       ;; Send HEAD commands.
489       (while articles
490         (elmo-pop3-send-command process (format
491                                          "top %s 0" (car articles))
492                                 'no-erase)
493 ;;;     (accept-process-output process 1)
494         (setq articles (cdr articles))
495         (setq count (1+ count))
496         ;; Every 200 requests we have to read the stream in
497         ;; order to avoid deadlocks.
498         (when (or elmo-pop3-send-command-synchronously
499                   (null articles)       ;All requests have been sent.
500                   (zerop (% count elmo-pop3-header-fetch-chop-length)))
501           (unless elmo-pop3-send-command-synchronously
502             (accept-process-output process 1))
503           (discard-input)
504           (while (progn
505                    (set-buffer buffer)
506                    (goto-char last-point)
507                    ;; Count replies.
508                    (while (elmo-pop3-next-result-arrived-p)
509                      (setq last-point (point))
510                      (setq received (1+ received)))
511                    (< received count))
512             (when (> number elmo-display-progress-threshold)
513               (if (or (zerop (% received 5)) (= received number))
514                   (elmo-display-progress
515                    'elmo-pop3-retrieve-headers "Getting headers..."
516                    (/ (* received 100) number))))
517             (accept-process-output process 1)
518 ;;;         (accept-process-output process)
519             (discard-input))))
520       ;; Remove all "\r"'s.
521       (goto-char (point-min))
522       (while (search-forward "\r\n" nil t)
523         (replace-match "\n"))
524       (copy-to-buffer tobuffer (point-min) (point-max)))))
525
526 (defalias 'elmo-pop3-msgdb-create 'elmo-pop3-msgdb-create-as-numlist)
527
528 (defun elmo-pop3-sort-overview-by-original-number (overview loc-alist)
529   (if loc-alist
530       (sort overview
531             (lambda (ent1 ent2)
532               (< (elmo-pop3-uidl-to-number
533                   (cdr (assq (elmo-msgdb-overview-entity-get-number ent1)
534                              loc-alist)))
535                  (elmo-pop3-uidl-to-number
536                   (cdr (assq (elmo-msgdb-overview-entity-get-number ent2)
537                              loc-alist))))))
538     overview))
539
540 (defun elmo-pop3-sort-msgdb-by-original-number (msgdb)
541   (message "Sorting...")
542   (let ((overview (elmo-msgdb-get-overview msgdb)))
543     (setq overview (elmo-pop3-sort-overview-by-original-number
544                     overview
545                     (elmo-msgdb-get-location msgdb)))
546     (message "Sorting...done")
547     (list overview (nth 1 msgdb)(nth 2 msgdb)(nth 3 msgdb)(nth 4 msgdb))))
548
549 (defun elmo-pop3-msgdb-create-as-numlist (spec numlist new-mark
550                                                already-mark seen-mark
551                                                important-mark seen-list
552                                                &optional msgdb)
553   (when numlist
554     (let ((process (elmo-network-session-process-internal
555                     (elmo-pop3-get-session spec)))
556           loc-alist)
557       (if elmo-pop3-use-uidl
558           (setq loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
559                             (elmo-msgdb-location-load
560                              (elmo-msgdb-expand-path spec)))))
561       (with-current-buffer (process-buffer process)
562         (elmo-pop3-sort-msgdb-by-original-number
563          (elmo-pop3-msgdb-create-by-header process numlist
564                                            new-mark already-mark
565                                            seen-mark seen-list
566                                            loc-alist))))))
567
568 (defun elmo-pop3-uidl-to-number (uidl)
569   (string-to-number (elmo-get-hash-val uidl
570                                        elmo-pop3-uidl-number-hash)))
571
572 (defun elmo-pop3-number-to-uidl (number)
573   (elmo-get-hash-val (format "#%d" number)
574                      elmo-pop3-number-uidl-hash))
575
576 (defun elmo-pop3-number-to-size (number)
577   (elmo-get-hash-val (format "#%d" number)
578                      elmo-pop3-size-hash))
579
580 (defun elmo-pop3-msgdb-create-by-header (process numlist
581                                                  new-mark already-mark
582                                                  seen-mark
583                                                  seen-list
584                                                  loc-alist)
585   (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
586     (with-current-buffer (process-buffer process)
587       (if loc-alist ; use uidl.
588           (setq numlist
589                 (delq
590                  nil
591                  (mapcar
592                   (lambda (number)
593                     (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
594                   numlist))))
595       (elmo-pop3-retrieve-headers (process-buffer process)
596                                   tmp-buffer process numlist)
597       (prog1
598           (elmo-pop3-msgdb-create-message
599            tmp-buffer
600            process
601            (length numlist)
602            numlist
603            new-mark already-mark seen-mark seen-list loc-alist)
604         (kill-buffer tmp-buffer)))))
605
606 (defun elmo-pop3-msgdb-create-message (buffer
607                                        process
608                                        num
609                                        numlist new-mark already-mark
610                                        seen-mark
611                                        seen-list
612                                        loc-alist)
613   (save-excursion
614     (let (beg overview number-alist mark-alist
615               entity i number message-id gmark seen size)
616       (set-buffer buffer)
617       (elmo-set-buffer-multibyte default-enable-multibyte-characters)
618       (goto-char (point-min))
619       (setq i 0)
620       (message "Creating msgdb...")
621       (while (not (eobp))
622         (setq beg (save-excursion (forward-line 1) (point)))
623         (elmo-pop3-next-result-arrived-p)
624         (save-excursion
625           (forward-line -1)
626           (save-restriction
627             (narrow-to-region beg (point))
628             (setq entity
629                   (elmo-msgdb-create-overview-from-buffer
630                    (car numlist)))
631             (setq numlist (cdr numlist))
632             (when entity
633               (setq overview
634                     (elmo-msgdb-append-element
635                      overview entity))
636               (with-current-buffer (process-buffer process)
637                 (elmo-msgdb-overview-entity-set-size
638                  entity
639                  (string-to-number
640                   (elmo-pop3-number-to-size
641                    (elmo-msgdb-overview-entity-get-number entity))))
642                 (if (setq number
643                           (car
644                            (rassoc
645                             (elmo-pop3-number-to-uidl
646                              (elmo-msgdb-overview-entity-get-number entity))
647                             loc-alist)))
648                     (elmo-msgdb-overview-entity-set-number entity number)))
649               (setq number-alist
650                     (elmo-msgdb-number-add
651                      number-alist
652                      (elmo-msgdb-overview-entity-get-number entity)
653                      (car entity)))
654               (setq message-id (car entity))
655               (setq seen (member message-id seen-list))
656               (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
657                                   (if (elmo-cache-exists-p
658                                        message-id) ; XXX
659                                       (if seen
660                                           nil
661                                         already-mark)
662                                     (if seen
663                                         (if elmo-pop3-use-cache
664                                             seen-mark)
665                                       new-mark))))
666                   (setq mark-alist
667                         (elmo-msgdb-mark-append
668                          mark-alist
669                          (elmo-msgdb-overview-entity-get-number entity)
670                          gmark))))))
671         (when (> num elmo-display-progress-threshold)
672           (setq i (1+ i))
673           (if (or (zerop (% i 5)) (= i num))
674               (elmo-display-progress
675                'elmo-pop3-msgdb-create-message "Creating msgdb..."
676                (/ (* i 100) num)))))
677       (list overview number-alist mark-alist loc-alist))))
678
679 (defun elmo-pop3-read-body (process outbuf)
680   (with-current-buffer (process-buffer process)
681     (let ((start elmo-pop3-read-point)
682           end)
683       (goto-char start)
684       (while (not (re-search-forward "^\\.\r?\n" nil t))
685         (accept-process-output process)
686         (goto-char start))
687       (setq end (point))
688       (with-current-buffer outbuf
689         (erase-buffer)
690         (insert-buffer-substring (process-buffer process) start (- end 3))
691         (elmo-delete-cr-get-content-type)))))
692
693 (defun elmo-pop3-read-msg (spec number outbuf &optional msgdb unread)
694   (let* ((loc-alist (if elmo-pop3-use-uidl
695                         (if msgdb
696                             (elmo-msgdb-get-location msgdb)
697                           (elmo-msgdb-location-load
698                            (elmo-msgdb-expand-path spec)))))
699          (process (elmo-network-session-process-internal
700                    (elmo-pop3-get-session spec)))
701          response errmsg msg)
702     (with-current-buffer (process-buffer process)
703       (if loc-alist
704           (setq number (elmo-pop3-uidl-to-number
705                         (cdr (assq number loc-alist)))))
706       (when number
707         (elmo-pop3-send-command process
708                                 (format "retr %s" number))
709         (when (null (setq response (elmo-pop3-read-response
710                                     process t)))
711           (error "Fetching message failed"))
712         (setq response (elmo-pop3-read-body process outbuf))
713         (set-buffer outbuf)
714         (goto-char (point-min))
715         (while (re-search-forward "^\\." nil t)
716           (replace-match "")
717           (forward-line))
718         response))))
719
720 (defun elmo-pop3-delete-msg (process number loc-alist)
721   (with-current-buffer (process-buffer process)
722     (let (response errmsg msg)
723       (if loc-alist
724           (setq number (elmo-pop3-uidl-to-number
725                         (cdr (assq number loc-alist)))))
726       (if number
727           (progn
728             (elmo-pop3-send-command process
729                                     (format "dele %s" number))
730             (when (null (setq response (elmo-pop3-read-response
731                                         process t)))
732               (error "Deleting message failed")))
733         (error "Deleting message failed")))))
734
735 (defun elmo-pop3-delete-msgs (spec msgs &optional msgdb)
736   (let ((loc-alist (if elmo-pop3-use-uidl
737                        (if msgdb
738                            (elmo-msgdb-get-location msgdb)
739                          (elmo-msgdb-location-load
740                           (elmo-msgdb-expand-path spec)))))
741         (process (elmo-network-session-process-internal
742                   (elmo-pop3-get-session spec))))
743     (mapcar '(lambda (msg) (elmo-pop3-delete-msg
744                             process msg loc-alist))
745             msgs)))
746
747 (defun elmo-pop3-search (spec condition &optional numlist)
748   (error "Searching in pop3 folder is not implemented yet"))
749
750 (defun elmo-pop3-use-cache-p (spec number)
751   elmo-pop3-use-cache)
752
753 (defun elmo-pop3-local-file-p (spec number)
754   nil)
755
756 (defun elmo-pop3-port-label (spec)
757   (concat "pop3"
758           (if (elmo-pop3-spec-stream-type spec)
759               (concat "!" (symbol-name
760                            (elmo-network-stream-type-symbol
761                             (elmo-pop3-spec-stream-type spec)))))))
762
763 (defsubst elmo-pop3-portinfo (spec)
764   (list (elmo-pop3-spec-hostname spec)
765         (elmo-pop3-spec-port spec)))
766
767 (defun elmo-pop3-plugged-p (spec)
768   (apply 'elmo-plugged-p
769          (append (elmo-pop3-portinfo spec)
770                  (list nil (quote (elmo-pop3-port-label spec))))))
771
772 (defun elmo-pop3-set-plugged (spec plugged add)
773   (apply 'elmo-set-plugged plugged
774          (append (elmo-pop3-portinfo spec)
775                  (list nil nil (quote (elmo-pop3-port-label spec)) add))))
776
777 (defalias 'elmo-pop3-sync-number-alist
778   'elmo-generic-sync-number-alist)
779 (defalias 'elmo-pop3-list-folder-unread
780   'elmo-generic-list-folder-unread)
781 (defalias 'elmo-pop3-list-folder-important
782   'elmo-generic-list-folder-important)
783 (defalias 'elmo-pop3-folder-diff 'elmo-generic-folder-diff)
784
785 (defun elmo-pop3-commit (spec)
786   (if (elmo-pop3-plugged-p spec)
787       (let ((session (elmo-pop3-get-session spec 'if-exists)))
788         (and session
789              (elmo-network-close-session session)))))
790        
791
792 (require 'product)
793 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
794
795 ;;; elmo-pop3.el ends here