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