* elmo-vars.el (elmo-pop3-use-uidl): Moved from `elmo-pop3.el'.
[elisp/wanderlust.git] / elmo / elmo-pop3.el
1 ;;; elmo-pop3.el -- POP3 Interface for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1999,2000      Kenichi OKADA <okada@opaopa.org>
5
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;;      Kenichi OKADA <okada@opaopa.org>
8 ;; Keywords: mail, net news
9
10 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
11
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16 ;;
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21 ;;
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26 ;;
27
28 ;;; Commentary:
29 ;; 
30
31 ;;; Code:
32 ;; 
33
34 (require 'elmo-msgdb)
35 (require 'elmo-net)
36
37 (eval-when-compile
38   (require 'elmo-util))
39
40 (eval-and-compile
41   (autoload 'md5 "md5"))
42
43 (defvar elmo-pop3-exists-exactly t)
44 (defvar sasl-mechanism-alist)
45
46 (luna-define-class elmo-pop3-session (elmo-network-session))
47
48 ;; buffer-local
49 (defvar elmo-pop3-read-point nil)
50 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
51 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
52 (defvar elmo-pop3-size-hash nil) ; number -> size
53 (defvar elmo-pop3-uidl-done nil)
54 (defvar elmo-pop3-list-done nil)
55
56 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
57                                     elmo-pop3-uidl-number-hash
58                                     elmo-pop3-number-uidl-hash
59                                     elmo-pop3-uidl-done
60                                     elmo-pop3-size-hash
61                                     elmo-pop3-list-done))
62
63 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
64   (when (elmo-network-session-process-internal session)
65     (when (memq (process-status
66                  (elmo-network-session-process-internal session))
67                 '(open run))
68       (elmo-pop3-send-command (elmo-network-session-process-internal session)
69                               "quit")
70       (or (elmo-pop3-read-response
71            (elmo-network-session-process-internal session) t)
72           (error "POP error: QUIT failed")))
73     (kill-buffer (process-buffer
74                   (elmo-network-session-process-internal session)))
75     (delete-process (elmo-network-session-process-internal session))))
76
77 (defun elmo-pop3-get-session (spec &optional if-exists)
78   (elmo-network-get-session
79    'elmo-pop3-session
80    "POP3"
81    (elmo-pop3-spec-hostname spec)
82    (elmo-pop3-spec-port spec)
83    (elmo-pop3-spec-username spec)
84    (elmo-pop3-spec-auth spec)
85    (elmo-pop3-spec-stream-type spec)
86    if-exists))
87
88 (defun elmo-pop3-send-command (process command &optional no-erase)
89   (with-current-buffer (process-buffer process)
90     (unless no-erase
91       (erase-buffer))
92     (goto-char (point-min))
93     (setq elmo-pop3-read-point (point))
94     (process-send-string process command)
95     (process-send-string process "\r\n")))
96
97 (defun elmo-pop3-read-response (process &optional not-command)
98   (with-current-buffer (process-buffer process)
99     (let ((case-fold-search nil)
100           (response-string nil)
101           (response-continue t)
102           (return-value nil)
103           match-end)
104       (while response-continue
105         (goto-char elmo-pop3-read-point)
106         (while (not (re-search-forward "\r?\n" nil t))
107           (accept-process-output process)
108           (goto-char elmo-pop3-read-point))
109         (setq match-end (point))
110         (setq response-string
111               (buffer-substring elmo-pop3-read-point (- match-end 2)))
112         (goto-char elmo-pop3-read-point)
113         (if (looking-at "\\+.*$")
114             (progn
115               (setq response-continue nil)
116               (setq elmo-pop3-read-point match-end)
117               (setq return-value
118                     (if return-value
119                         (concat return-value "\n" response-string)
120                       response-string)))
121           (if (looking-at "\\-.*$")
122               (progn
123                 (setq response-continue nil)
124                 (setq elmo-pop3-read-point match-end)
125                 (setq return-value nil))
126             (setq elmo-pop3-read-point match-end)
127             (if not-command
128                 (setq response-continue nil))
129             (setq return-value
130                   (if return-value
131                       (concat return-value "\n" response-string)
132                     response-string)))
133           (setq elmo-pop3-read-point match-end)))
134       return-value)))
135
136 (defun elmo-pop3-process-filter (process output)
137   (save-excursion
138     (set-buffer (process-buffer process))
139     (goto-char (point-max))
140     (insert output)))
141
142 (defun elmo-pop3-auth-user (session)
143   (let ((process (elmo-network-session-process-internal session)))
144     ;; try USER/PASS
145     (elmo-pop3-send-command
146      process
147      (format "user %s" (elmo-network-session-user-internal session)))
148     (or (elmo-pop3-read-response process t)
149         (signal 'elmo-authenticate-error
150                 '(elmo-pop-auth-user)))
151     (elmo-pop3-send-command  process
152                              (format
153                               "pass %s"
154                               (elmo-get-passwd
155                                (elmo-network-session-password-key session))))
156     (or (elmo-pop3-read-response process t)
157         (signal 'elmo-authenticate-error
158                 '(elmo-pop-auth-user)))))
159
160 (defun elmo-pop3-auth-apop (session)
161   (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
162                     (elmo-network-session-greeting-internal session))
163       ;; good, APOP ready server
164       (progn
165         (elmo-pop3-send-command
166          (elmo-network-session-process-internal session)
167          (format "apop %s %s"
168                  (elmo-network-session-user-internal session)
169                  (md5
170                   (concat (match-string
171                            1
172                            (elmo-network-session-greeting-internal session))
173                           (elmo-get-passwd
174                            (elmo-network-session-password-key session))))))
175         (or (elmo-pop3-read-response
176              (elmo-network-session-process-internal session)
177              t)
178             (signal 'elmo-authenticate-error
179                     '(elmo-pop3-auth-apop))))
180     (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
181     
182 (luna-define-method elmo-network-initialize-session-buffer :after
183   ((session elmo-pop3-session) buffer)
184   (with-current-buffer buffer
185     (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
186
187 (luna-define-method elmo-network-initialize-session ((session
188                                                       elmo-pop3-session))
189   (let ((process (elmo-network-session-process-internal session))
190         response mechanism)
191     (with-current-buffer (process-buffer process)
192       (set-process-filter process 'elmo-pop3-process-filter)
193       (setq elmo-pop3-read-point (point-min))
194       ;; Skip garbage output from process before greeting.
195       (while (and (memq (process-status process) '(open run))
196                   (goto-char (point-max))
197                   (forward-line -1)
198                   (not (looking-at "+OK")))
199         (accept-process-output process 1))
200       (setq elmo-pop3-read-point (point))
201       (or (elmo-network-session-set-greeting-internal
202            session
203            (elmo-pop3-read-response process t))
204           (signal 'elmo-open-error
205                   '(elmo-network-intialize-session)))
206       (when (eq (elmo-network-stream-type-symbol
207                  (elmo-network-session-stream-type-internal session))
208                 'starttls)
209         (elmo-pop3-send-command process "stls")
210         (if (string-match "^\+OK"
211                           (elmo-pop3-read-response process))
212             (starttls-negotiate process)
213           (signal 'elmo-open-error
214                   '(elmo-pop3-starttls-error)))))))
215
216 (luna-define-method elmo-network-authenticate-session ((session
217                                                         elmo-pop3-session))
218   (with-current-buffer (process-buffer 
219                         (elmo-network-session-process-internal session))
220     (let* ((process (elmo-network-session-process-internal session))
221            (auth (elmo-network-session-auth-internal session))
222            (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
223                          (if (listp auth) auth (list auth))))
224            sasl-mechanisms
225            client name step response mechanism
226            sasl-read-passphrase)
227       (or (and (string= "USER" (car auth))
228                (elmo-pop3-auth-user session))
229           (and (string= "APOP" (car auth))
230                (elmo-pop3-auth-apop session))
231           (progn
232             (require 'sasl)
233             (setq sasl-mechanisms (mapcar 'car sasl-mechanism-alist))
234             (setq mechanism (sasl-find-mechanism auth))
235             (unless mechanism
236               (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
237             (setq client
238                   (sasl-make-client
239                    mechanism
240                    (elmo-network-session-user-internal session)
241                    "pop"
242                    (elmo-network-session-host-internal session)))
243 ;;;         (if elmo-pop3-auth-user-realm
244 ;;;             (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
245             (setq name (sasl-mechanism-name mechanism))
246             (elmo-network-session-set-auth-internal session
247                                                     (intern (downcase name)))
248             (setq sasl-read-passphrase
249                   (function
250                    (lambda (prompt)
251                      (elmo-get-passwd
252                       (elmo-network-session-password-key session)))))
253             (setq step (sasl-next-step client nil))
254             (elmo-pop3-send-command
255              process
256              (concat "AUTH " name
257                      (and (sasl-step-data step)
258                           (concat
259                            " "
260                            (elmo-base64-encode-string
261                             (sasl-step-data step) 'no-line-break))))) ;)
262             (catch 'done
263               (while t
264                 (unless (setq response (elmo-pop3-read-response process t))
265                   ;; response is NO or BAD.
266                   (signal 'elmo-authenticate-error
267                           (list (intern
268                                  (concat "elmo-pop3-auth-"
269                                          (downcase name))))))
270                 (if (string-match "^\+OK" response)
271                     (if (sasl-next-step client step)
272                         ;; Bogus server?
273                         (signal 'elmo-authenticate-error
274                                 (list (intern
275                                        (concat "elmo-pop3-auth-"
276                                                (downcase name)))))
277                       ;; The authentication process is finished.
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