* elmo-imap4.el (elmo-network-authenticate-session): Fix.
[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                   ;; response is NO or BAD.
269                   (signal 'elmo-authenticate-error
270                           (list (intern
271                                  (concat "elmo-pop3-auth-"
272                                          (downcase name))))))
273                 (if (string-match "^\+OK" response)
274                     (if (sasl-next-step client step)
275                         ;; Bogus server?
276                         (signal 'elmo-authenticate-error
277                                 (list (intern
278                                        (concat "elmo-pop3-auth-"
279                                                (downcase name)))))
280                       ;; The authentication process is finished.
281                       (throw 'done nil)))
282                 (sasl-step-set-data
283                  step
284                  (elmo-base64-decode-string 
285                   (cadr (split-string response " "))))
286                 (setq step (sasl-next-step client step))
287                 (elmo-pop3-send-command
288                  process
289                  (if (sasl-step-data step)
290                      (elmo-base64-encode-string (sasl-step-data step)
291                                                 'no-line-break)
292                    "")))))))))
293
294 (luna-define-method elmo-network-setup-session ((session
295                                                  elmo-pop3-session))
296   (let ((process (elmo-network-session-process-internal session))
297         count response)
298     (with-current-buffer (process-buffer process)
299       (setq elmo-pop3-size-hash (elmo-make-hash 31))
300       ;; To get obarray of uidl and size
301       (elmo-pop3-send-command process "list")
302       (if (null (elmo-pop3-read-response process))
303           (error "POP LIST command failed"))
304       (if (null (setq response
305                       (elmo-pop3-read-contents
306                        (current-buffer) process)))
307           (error "POP LIST command failed"))
308       ;; POP server always returns a sequence of serial numbers.
309       (setq count (elmo-pop3-parse-list-response response))
310       ;; UIDL
311       (when elmo-pop3-use-uidl
312         (setq elmo-pop3-uidl-number-hash (elmo-make-hash (* count 2)))
313         (setq elmo-pop3-number-uidl-hash (elmo-make-hash (* count 2)))
314         ;; UIDL
315         (elmo-pop3-send-command process "uidl")
316         (unless (elmo-pop3-read-response process)
317           (error "POP UIDL failed"))
318         (unless (setq response (elmo-pop3-read-contents
319                                 (current-buffer) process))
320           (error "POP UIDL failed"))
321         (elmo-pop3-parse-uidl-response response)))))
322
323 (defun elmo-pop3-read-contents (buffer process)
324   (save-excursion
325     (set-buffer buffer)
326     (let ((case-fold-search nil)
327           match-end)
328       (goto-char elmo-pop3-read-point)
329       (while (not (re-search-forward "^\\.\r\n" nil t))
330         (accept-process-output process)
331         (goto-char elmo-pop3-read-point))
332       (setq match-end (point))
333       (elmo-delete-cr
334        (buffer-substring elmo-pop3-read-point
335                          (- match-end 3))))))
336
337 ;; dummy functions
338 (defun elmo-pop3-list-folders (spec &optional hierarchy) nil)
339 (defun elmo-pop3-append-msg (spec string) nil nil)
340 (defun elmo-pop3-folder-creatable-p (spec) nil)
341 (defun elmo-pop3-create-folder (spec) nil)
342
343 (defun elmo-pop3-folder-exists-p (spec)
344   (if (and elmo-pop3-exists-exactly
345            (elmo-pop3-plugged-p spec))
346       (save-excursion
347         (let (elmo-auto-change-plugged ; don't change plug status.
348               elmo-pop3-use-uidl       ; No need to use uidl.
349               session)
350           (prog1
351               (setq session (elmo-pop3-get-session spec))
352             (if session
353                 (elmo-network-close-session session)))))
354     t))
355
356 (defun elmo-pop3-parse-uidl-response (string)
357   (let ((buffer (current-buffer))
358         number list size)
359     (with-temp-buffer
360       (let (number uid list)
361         (insert string)
362         (goto-char (point-min))
363         (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([^ \n]+\\)$" nil t)
364           (setq number  (elmo-match-buffer 1))
365           (setq uid (elmo-match-buffer 2))
366           (with-current-buffer buffer
367             (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
368             (elmo-set-hash-val (concat "#" number) uid
369                                elmo-pop3-number-uidl-hash))
370           (setq list (cons uid list)))
371         (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
372         (nreverse list)))))
373
374 (defun elmo-pop3-parse-list-response (string)
375   (let ((buffer (current-buffer))
376         (count 0)
377         alist)
378     (with-temp-buffer
379       (insert string)
380       (goto-char (point-min))
381       (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
382         (setq alist
383               (cons
384                (cons (elmo-match-buffer 1)
385                      (elmo-match-buffer 2))
386                alist))
387         (setq count (1+ count)))
388       (with-current-buffer buffer
389         (setq elmo-pop3-size-hash (elmo-make-hash (* (length alist) 2)))
390         (while alist
391           (elmo-set-hash-val (concat "#" (car (car alist)))
392                              (cdr (car alist))
393                              elmo-pop3-size-hash)
394           (setq alist (cdr alist)))
395         (setq elmo-pop3-list-done t))
396       count)))
397
398 (defun elmo-pop3-list-location (spec)
399   (with-current-buffer (process-buffer
400                         (elmo-network-session-process-internal
401                          (elmo-pop3-get-session spec)))
402     (let (list)
403       (if elmo-pop3-uidl-done
404           (progn
405             (mapatoms
406              (lambda (atom)
407                (setq list (cons (symbol-name atom) list)))
408              elmo-pop3-uidl-number-hash)
409             (nreverse list))
410         (error "POP3: Error in UIDL")))))
411
412 (defun elmo-pop3-list-by-uidl-subr (spec &optional nonsort)
413   (let ((flist (elmo-list-folder-by-location
414                 spec
415                 (elmo-pop3-list-location spec))))
416     (if nonsort
417         (cons (elmo-max-of-list flist) (length flist))
418       (sort flist '<))))
419
420 (defun elmo-pop3-list-by-list (spec)
421   (with-current-buffer (process-buffer
422                         (elmo-network-session-process-internal
423                          (elmo-pop3-get-session spec)))
424     (let (list)
425       (if elmo-pop3-list-done
426           (progn
427             (mapatoms (lambda (atom)
428                         (setq list (cons (string-to-int
429                                           (substring (symbol-name atom) 1))
430                                          list)))
431                       elmo-pop3-size-hash)
432             (sort list '<))
433         (error "POP3: Error in list")))))
434
435 (defun elmo-pop3-list-folder (spec &optional nohide)
436   (let ((killed (and elmo-use-killed-list
437                      (elmo-msgdb-killed-list-load
438                       (elmo-msgdb-expand-path spec))))
439         numbers)
440     (elmo-pop3-commit spec)
441     (setq numbers (if elmo-pop3-use-uidl
442                       (progn
443                         (elmo-pop3-list-by-uidl-subr spec))
444                     (elmo-pop3-list-by-list spec)))
445     (elmo-living-messages numbers killed)))
446
447 (defun elmo-pop3-max-of-folder (spec)
448   (elmo-pop3-commit spec)
449   (if elmo-pop3-use-uidl
450       (elmo-pop3-list-by-uidl-subr spec 'nonsort)
451     (let* ((process
452             (elmo-network-session-process-internal
453              (elmo-pop3-get-session spec)))
454            (total 0)
455            response)
456       (with-current-buffer (process-buffer process)
457         (elmo-pop3-send-command process "STAT")
458         (setq response (elmo-pop3-read-response process))
459         ;; response: "^\+OK 2 7570$"
460         (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
461             (error "POP STAT command failed")
462           (setq total
463                 (string-to-int
464                  (substring response (match-beginning 1)(match-end 1 ))))
465           (cons total total))))))
466
467 (defvar elmo-pop3-header-fetch-chop-length 200)
468
469 (defsubst elmo-pop3-next-result-arrived-p ()
470   (cond
471    ((eq (following-char) ?+)
472     (if (re-search-forward "\n\\.\r?\n" nil t)
473         t
474       nil))
475    ((looking-at "-")
476     (if (search-forward "\n" nil t)
477         t
478       nil))
479    (t
480     nil)))
481      
482 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
483   (save-excursion
484     (set-buffer buffer)
485     (erase-buffer)
486     (let ((number (length articles))
487           (count 0)
488           (received 0)
489           (last-point (point-min)))
490       ;; Send HEAD commands.
491       (while articles
492         (elmo-pop3-send-command process (format
493                                          "top %s 0" (car articles))
494                                 'no-erase)
495 ;;;     (accept-process-output process 1)
496         (setq articles (cdr articles))
497         (setq count (1+ count))
498         ;; Every 200 requests we have to read the stream in
499         ;; order to avoid deadlocks.
500         (when (or elmo-pop3-send-command-synchronously
501                   (null articles)       ;All requests have been sent.
502                   (zerop (% count elmo-pop3-header-fetch-chop-length)))
503           (unless elmo-pop3-send-command-synchronously
504             (accept-process-output process 1))
505           (discard-input)
506           (while (progn
507                    (set-buffer buffer)
508                    (goto-char last-point)
509                    ;; Count replies.
510                    (while (elmo-pop3-next-result-arrived-p)
511                      (setq last-point (point))
512                      (setq received (1+ received)))
513                    (< received count))
514             (when (> number elmo-display-progress-threshold)
515               (if (or (zerop (% received 5)) (= received number))
516                   (elmo-display-progress
517                    'elmo-pop3-retrieve-headers "Getting headers..."
518                    (/ (* received 100) number))))
519             (accept-process-output process 1)
520 ;;;         (accept-process-output process)
521             (discard-input))))
522       ;; Remove all "\r"'s.
523       (goto-char (point-min))
524       (while (search-forward "\r\n" nil t)
525         (replace-match "\n"))
526       (copy-to-buffer tobuffer (point-min) (point-max)))))
527
528 (defalias 'elmo-pop3-msgdb-create 'elmo-pop3-msgdb-create-as-numlist)
529
530 (defun elmo-pop3-sort-overview-by-original-number (overview loc-alist)
531   (if loc-alist
532       (sort overview
533             (lambda (ent1 ent2)
534               (< (elmo-pop3-uidl-to-number
535                   (cdr (assq (elmo-msgdb-overview-entity-get-number ent1)
536                              loc-alist)))
537                  (elmo-pop3-uidl-to-number
538                   (cdr (assq (elmo-msgdb-overview-entity-get-number ent2)
539                              loc-alist))))))
540     overview))
541
542 (defun elmo-pop3-sort-msgdb-by-original-number (msgdb)
543   (message "Sorting...")
544   (let ((overview (elmo-msgdb-get-overview msgdb)))
545     (setq overview (elmo-pop3-sort-overview-by-original-number
546                     overview
547                     (elmo-msgdb-get-location msgdb)))
548     (message "Sorting...done")
549     (list overview (nth 1 msgdb)(nth 2 msgdb)(nth 3 msgdb)(nth 4 msgdb))))
550
551 (defun elmo-pop3-msgdb-create-as-numlist (spec numlist new-mark
552                                                already-mark seen-mark
553                                                important-mark seen-list
554                                                &optional msgdb)
555   (when numlist
556     (let ((process (elmo-network-session-process-internal
557                     (elmo-pop3-get-session spec)))
558           loc-alist)
559       (if elmo-pop3-use-uidl
560           (setq loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
561                             (elmo-msgdb-location-load
562                              (elmo-msgdb-expand-path spec)))))
563       (with-current-buffer (process-buffer process)
564         (elmo-pop3-sort-msgdb-by-original-number
565          (elmo-pop3-msgdb-create-by-header process numlist
566                                            new-mark already-mark
567                                            seen-mark seen-list
568                                            loc-alist))))))
569
570 (defun elmo-pop3-uidl-to-number (uidl)
571   (string-to-number (elmo-get-hash-val uidl
572                                        elmo-pop3-uidl-number-hash)))
573
574 (defun elmo-pop3-number-to-uidl (number)
575   (elmo-get-hash-val (format "#%d" number)
576                      elmo-pop3-number-uidl-hash))
577
578 (defun elmo-pop3-number-to-size (number)
579   (elmo-get-hash-val (format "#%d" number)
580                      elmo-pop3-size-hash))
581
582 (defun elmo-pop3-msgdb-create-by-header (process numlist
583                                                  new-mark already-mark
584                                                  seen-mark
585                                                  seen-list
586                                                  loc-alist)
587   (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
588     (with-current-buffer (process-buffer process)
589       (if loc-alist ; use uidl.
590           (setq numlist
591                 (delq
592                  nil
593                  (mapcar
594                   (lambda (number)
595                     (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
596                   numlist))))
597       (elmo-pop3-retrieve-headers (process-buffer process)
598                                   tmp-buffer process numlist)
599       (prog1
600           (elmo-pop3-msgdb-create-message
601            tmp-buffer
602            process
603            (length numlist)
604            numlist
605            new-mark already-mark seen-mark seen-list loc-alist)
606         (kill-buffer tmp-buffer)))))
607
608 (defun elmo-pop3-msgdb-create-message (buffer
609                                        process
610                                        num
611                                        numlist new-mark already-mark
612                                        seen-mark
613                                        seen-list
614                                        loc-alist)
615   (save-excursion
616     (let (beg overview number-alist mark-alist
617               entity i number message-id gmark seen size)
618       (set-buffer buffer)
619       (elmo-set-buffer-multibyte default-enable-multibyte-characters)
620       (goto-char (point-min))
621       (setq i 0)
622       (message "Creating msgdb...")
623       (while (not (eobp))
624         (setq beg (save-excursion (forward-line 1) (point)))
625         (elmo-pop3-next-result-arrived-p)
626         (save-excursion
627           (forward-line -1)
628           (save-restriction
629             (narrow-to-region beg (point))
630             (setq entity
631                   (elmo-msgdb-create-overview-from-buffer
632                    (car numlist)))
633             (setq numlist (cdr numlist))
634             (when entity
635               (setq overview
636                     (elmo-msgdb-append-element
637                      overview entity))
638               (with-current-buffer (process-buffer process)
639                 (elmo-msgdb-overview-entity-set-size
640                  entity
641                  (string-to-number
642                   (elmo-pop3-number-to-size
643                    (elmo-msgdb-overview-entity-get-number entity))))
644                 (if (setq number
645                           (car
646                            (rassoc
647                             (elmo-pop3-number-to-uidl
648                              (elmo-msgdb-overview-entity-get-number entity))
649                             loc-alist)))
650                     (elmo-msgdb-overview-entity-set-number entity number)))
651               (setq number-alist
652                     (elmo-msgdb-number-add
653                      number-alist
654                      (elmo-msgdb-overview-entity-get-number entity)
655                      (car entity)))
656               (setq message-id (car entity))
657               (setq seen (member message-id seen-list))
658               (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
659                                   (if (elmo-cache-exists-p
660                                        message-id) ; XXX
661                                       (if seen
662                                           nil
663                                         already-mark)
664                                     (if seen
665                                         (if elmo-pop3-use-cache
666                                             seen-mark)
667                                       new-mark))))
668                   (setq mark-alist
669                         (elmo-msgdb-mark-append
670                          mark-alist
671                          (elmo-msgdb-overview-entity-get-number entity)
672                          gmark))))))
673         (when (> num elmo-display-progress-threshold)
674           (setq i (1+ i))
675           (if (or (zerop (% i 5)) (= i num))
676               (elmo-display-progress
677                'elmo-pop3-msgdb-create-message "Creating msgdb..."
678                (/ (* i 100) num)))))
679       (list overview number-alist mark-alist loc-alist))))
680
681 (defun elmo-pop3-read-body (process outbuf)
682   (with-current-buffer (process-buffer process)
683     (let ((start elmo-pop3-read-point)
684           end)
685       (goto-char start)
686       (while (not (re-search-forward "^\\.\r?\n" nil t))
687         (accept-process-output process)
688         (goto-char start))
689       (setq end (point))
690       (with-current-buffer outbuf
691         (erase-buffer)
692         (insert-buffer-substring (process-buffer process) start (- end 3))
693         (elmo-delete-cr-get-content-type)))))
694
695 (defun elmo-pop3-read-msg (spec number outbuf &optional msgdb unread)
696   (let* ((loc-alist (if elmo-pop3-use-uidl
697                         (if msgdb
698                             (elmo-msgdb-get-location msgdb)
699                           (elmo-msgdb-location-load
700                            (elmo-msgdb-expand-path spec)))))
701          (process (elmo-network-session-process-internal
702                    (elmo-pop3-get-session spec)))
703          response errmsg msg)
704     (with-current-buffer (process-buffer process)
705       (if loc-alist
706           (setq number (elmo-pop3-uidl-to-number
707                         (cdr (assq number loc-alist)))))
708       (when number
709         (elmo-pop3-send-command process
710                                 (format "retr %s" number))
711         (when (null (setq response (elmo-pop3-read-response
712                                     process t)))
713           (error "Fetching message failed"))
714         (setq response (elmo-pop3-read-body process outbuf))
715         (set-buffer outbuf)
716         (goto-char (point-min))
717         (while (re-search-forward "^\\." nil t)
718           (replace-match "")
719           (forward-line))
720         response))))
721
722 (defun elmo-pop3-delete-msg (process number loc-alist)
723   (with-current-buffer (process-buffer process)
724     (let (response errmsg msg)
725       (if loc-alist
726           (setq number (elmo-pop3-uidl-to-number
727                         (cdr (assq number loc-alist)))))
728       (if number
729           (progn
730             (elmo-pop3-send-command process
731                                     (format "dele %s" number))
732             (when (null (setq response (elmo-pop3-read-response
733                                         process t)))
734               (error "Deleting message failed")))
735         (error "Deleting message failed")))))
736
737 (defun elmo-pop3-delete-msgs (spec msgs &optional msgdb)
738   (let ((loc-alist (if elmo-pop3-use-uidl
739                        (if msgdb
740                            (elmo-msgdb-get-location msgdb)
741                          (elmo-msgdb-location-load
742                           (elmo-msgdb-expand-path spec)))))
743         (process (elmo-network-session-process-internal
744                   (elmo-pop3-get-session spec))))
745     (mapcar '(lambda (msg) (elmo-pop3-delete-msg
746                             process msg loc-alist))
747             msgs)))
748
749 (defun elmo-pop3-search (spec condition &optional numlist)
750   (error "Searching in pop3 folder is not implemented yet"))
751
752 (defun elmo-pop3-use-cache-p (spec number)
753   elmo-pop3-use-cache)
754
755 (defun elmo-pop3-local-file-p (spec number)
756   nil)
757
758 (defun elmo-pop3-port-label (spec)
759   (concat "pop3"
760           (if (elmo-pop3-spec-stream-type spec)
761               (concat "!" (symbol-name
762                            (elmo-network-stream-type-symbol
763                             (elmo-pop3-spec-stream-type spec)))))))
764
765 (defsubst elmo-pop3-portinfo (spec)
766   (list (elmo-pop3-spec-hostname spec)
767         (elmo-pop3-spec-port spec)))
768
769 (defun elmo-pop3-plugged-p (spec)
770   (apply 'elmo-plugged-p
771          (append (elmo-pop3-portinfo spec)
772                  (list nil (quote (elmo-pop3-port-label spec))))))
773
774 (defun elmo-pop3-set-plugged (spec plugged add)
775   (apply 'elmo-set-plugged plugged
776          (append (elmo-pop3-portinfo spec)
777                  (list nil nil (quote (elmo-pop3-port-label spec)) add))))
778
779 (defalias 'elmo-pop3-sync-number-alist
780   'elmo-generic-sync-number-alist)
781 (defalias 'elmo-pop3-list-folder-unread
782   'elmo-generic-list-folder-unread)
783 (defalias 'elmo-pop3-list-folder-important
784   'elmo-generic-list-folder-important)
785 (defalias 'elmo-pop3-folder-diff 'elmo-generic-folder-diff)
786
787 (defun elmo-pop3-commit (spec)
788   (if (elmo-pop3-plugged-p spec)
789       (let ((session (elmo-pop3-get-session spec 'if-exists)))
790         (and session
791              (elmo-network-close-session session)))))
792        
793
794 (require 'product)
795 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
796
797 ;;; elmo-pop3.el ends here