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