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