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