1 ;;; elmo-pop3.el -- POP3 Interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1999,2000 Kenichi OKADA <okada@opaopa.org>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Kenichi OKADA <okada@opaopa.org>
8 ;; Keywords: mail, net news
10 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
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)
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.
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.
44 (defun-maybe starttls-negotiate (a))
45 (defun-maybe sasl-find-mechanism (mechanisms))
46 (defun-maybe sasl-make-client (mechanism name service server))
47 (defun-maybe sasl-mechanism-name (client))
48 (defun-maybe sasl-next-step (client step))
49 (defun-maybe sasl-step-data (step))
50 (defun-maybe sasl-step-set-data (step data)))
56 (defvar elmo-pop3-use-uidl t
57 "*If non-nil, use UIDL.")
59 (defvar elmo-pop3-exists-exactly t)
62 (luna-define-class elmo-pop3-session (elmo-network-session) ()))
65 (defvar elmo-pop3-read-point nil)
66 (defvar elmo-pop3-number-uidl-hash nil) ; number -> uidl
67 (defvar elmo-pop3-uidl-number-hash nil) ; uidl -> number
68 (defvar elmo-pop3-size-hash nil) ; number -> size
69 (defvar elmo-pop3-uidl-done nil)
70 (defvar elmo-pop3-list-done nil)
72 (defvar elmo-pop3-local-variables '(elmo-pop3-read-point
73 elmo-pop3-uidl-number-hash
74 elmo-pop3-number-uidl-hash
79 (luna-define-method elmo-network-close-session ((session elmo-pop3-session))
80 (unless (memq (process-status
81 (elmo-network-session-process-internal session))
83 (elmo-pop3-send-command (elmo-network-session-process-internal session)
85 (or (elmo-pop3-read-response
86 (elmo-network-session-process-internal session) t)
87 (error "POP error: QUIT failed")))
88 (kill-buffer (process-buffer
89 (elmo-network-session-process-internal session)))
90 (delete-process (elmo-network-session-process-internal session)))
92 (defun elmo-pop3-get-session (spec &optional if-exists)
93 (elmo-network-get-session
96 (elmo-pop3-spec-hostname spec)
97 (elmo-pop3-spec-port spec)
98 (elmo-pop3-spec-username spec)
99 (elmo-pop3-spec-auth spec)
100 (elmo-pop3-spec-stream-type spec)
103 (defun elmo-pop3-send-command (process command &optional no-erase)
104 (with-current-buffer (process-buffer process)
107 (goto-char (point-min))
108 (setq elmo-pop3-read-point (point))
109 (process-send-string process command)
110 (process-send-string process "\r\n")))
112 (defun elmo-pop3-read-response (process &optional not-command)
113 (with-current-buffer (process-buffer process)
114 (let ((case-fold-search nil)
115 (response-string nil)
116 (response-continue t)
119 (while response-continue
120 (goto-char elmo-pop3-read-point)
121 (while (not (re-search-forward "\r?\n" nil t))
122 (accept-process-output process)
123 (goto-char elmo-pop3-read-point))
124 (setq match-end (point))
125 (setq response-string
126 (buffer-substring elmo-pop3-read-point (- match-end 2)))
127 (goto-char elmo-pop3-read-point)
128 (if (looking-at "\\+.*$")
130 (setq response-continue nil)
131 (setq elmo-pop3-read-point match-end)
134 (concat return-value "\n" response-string)
136 (if (looking-at "\\-.*$")
138 (setq response-continue nil)
139 (setq elmo-pop3-read-point match-end)
140 (setq return-value nil))
141 (setq elmo-pop3-read-point match-end)
143 (setq response-continue nil))
146 (concat return-value "\n" response-string)
148 (setq elmo-pop3-read-point match-end)))
151 (defun elmo-pop3-process-filter (process output)
153 (set-buffer (process-buffer process))
154 (goto-char (point-max))
157 (defun elmo-pop3-auth-user (session)
158 (let ((process (elmo-network-session-process-internal session)))
160 (elmo-pop3-send-command
162 (format "user %s" (elmo-network-session-user-internal session)))
163 (or (elmo-pop3-read-response process t)
164 (signal 'elmo-authenticate-error
165 '(elmo-pop-auth-user)))
166 (elmo-pop3-send-command process
170 (elmo-network-session-password-key session))))
171 (or (elmo-pop3-read-response process t)
172 (signal 'elmo-authenticate-error
173 '(elmo-pop-auth-user)))))
175 (defun elmo-pop3-auth-apop (session)
176 (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
177 (elmo-network-session-greeting-internal session))
178 ;; good, APOP ready server
181 (elmo-pop3-send-command
182 (elmo-network-session-process-internal session)
184 (elmo-network-session-user-internal session)
186 (concat (match-string
188 (elmo-network-session-greeting-internal session))
190 (elmo-network-session-password-key session))))))
191 (or (elmo-pop3-read-response
192 (elmo-network-session-process-internal session)
194 (signal 'elmo-authenticate-error
195 '(elmo-pop3-auth-apop))))
196 (signal 'elmo-open-error '(elmo-pop-auth-apop))))
198 (luna-define-method elmo-network-initialize-session-buffer :after
199 ((session elmo-pop3-session) buffer)
200 (with-current-buffer buffer
201 (mapcar 'make-variable-buffer-local elmo-pop3-local-variables)))
203 (luna-define-method elmo-network-initialize-session ((session
205 (let ((process (elmo-network-session-process-internal session))
207 (with-current-buffer (process-buffer process)
208 (set-process-filter process 'elmo-pop3-process-filter)
209 (setq elmo-pop3-read-point (point-min))
210 (or (elmo-network-session-set-greeting-internal
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))
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)))))))
225 (luna-define-method elmo-network-authenticate-session ((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 client name step response mechanism
234 sasl-read-passphrase)
235 (or (and (string= "USER" (car auth))
236 (elmo-pop3-auth-user session))
237 (and (string= "APOP" (car auth))
238 (elmo-pop3-auth-apop session))
240 (setq mechanism (sasl-find-mechanism auth))
242 (signal 'elmo-authenticate-error '(elmo-pop3-auth-no-mechanisms)))
246 (elmo-network-session-user-internal session)
248 (elmo-network-session-host-internal session)))
249 ;;; (if elmo-pop3-auth-user-realm
250 ;;; (sasl-client-set-property client 'realm elmo-pop3-auth-user-realm))
251 (setq name (sasl-mechanism-name mechanism))
252 (elmo-network-session-set-auth-internal session
253 (intern (downcase name)))
254 (setq sasl-read-passphrase
258 (elmo-network-session-password-key session)))))
259 (setq step (sasl-next-step client nil))
260 (elmo-pop3-send-command
263 (and (sasl-step-data step)
266 (elmo-base64-encode-string
267 (sasl-step-data step) 'no-line-break))))) ;)
270 (unless (setq response (elmo-pop3-read-response process t))
271 (signal 'elmo-authenticate-error
273 (concat "elmo-pop3-auth-"
275 (if (string-match "^\+OK" response)
276 (if (sasl-next-step client step)
277 (signal 'elmo-authenticate-error
279 (concat "elmo-pop3-auth-"
284 (elmo-base64-decode-string
285 (cadr (split-string response " "))))
286 (setq step (sasl-next-step client step))
287 (elmo-pop3-send-command
289 (if (sasl-step-data step)
290 (elmo-base64-encode-string (sasl-step-data step)
294 (luna-define-method elmo-network-setup-session ((session
296 (let ((process (elmo-network-session-process-internal session))
298 (with-current-buffer (process-buffer process)
299 (setq elmo-pop3-size-hash (make-vector 31 0))
300 ;; To get obarray of uidl and size
301 (elmo-pop3-send-command process "list")
302 (if (null (elmo-pop3-read-response process))
303 (error "POP List folder failed"))
304 (if (null (setq response
305 (elmo-pop3-read-contents
306 (current-buffer) process)))
307 (error "POP List folder failed"))
308 ;; POP server always returns a sequence of serial numbers.
309 (elmo-pop3-parse-list-response response)
311 (when elmo-pop3-use-uidl
312 (setq elmo-pop3-uidl-number-hash (make-vector 31 0))
313 (setq elmo-pop3-number-uidl-hash (make-vector 31 0))
315 (elmo-pop3-send-command process "uidl")
316 (unless (elmo-pop3-read-response process)
317 (error "UIDL failed"))
318 (unless (setq response (elmo-pop3-read-contents
319 (current-buffer) process))
320 (error "UIDL failed"))
321 (elmo-pop3-parse-uidl-response response)))))
323 (defun elmo-pop3-read-contents (buffer process)
326 (let ((case-fold-search nil)
328 (goto-char elmo-pop3-read-point)
329 (while (not (re-search-forward "^\\.\r\n" nil t))
330 (accept-process-output process)
331 (goto-char elmo-pop3-read-point))
332 (setq match-end (point))
334 (buffer-substring elmo-pop3-read-point
338 (defun elmo-pop3-list-folders (spec &optional hierarchy) nil)
339 (defun elmo-pop3-append-msg (spec string) nil nil)
340 (defun elmo-pop3-folder-creatable-p (spec) nil)
341 (defun elmo-pop3-create-folder (spec) nil)
343 (defun elmo-pop3-folder-exists-p (spec)
344 (if (and elmo-pop3-exists-exactly
345 (elmo-pop3-plugged-p spec))
347 (let (elmo-auto-change-plugged ; don't change plug status.
348 elmo-pop3-use-uidl ; No need to use uidl.
351 (setq session (elmo-pop3-get-session spec))
353 (elmo-network-close-session session)))))
356 (defun elmo-pop3-parse-uidl-response (string)
357 (let ((buffer (current-buffer))
360 (let (number uid list)
362 (goto-char (point-min))
363 (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([^ \n]+\\)$" nil t)
364 (setq number (elmo-match-buffer 1))
365 (setq uid (elmo-match-buffer 2))
366 (with-current-buffer buffer
367 (elmo-set-hash-val uid number elmo-pop3-uidl-number-hash)
368 (elmo-set-hash-val (concat "#" number) uid
369 elmo-pop3-number-uidl-hash))
370 (setq list (cons uid list)))
371 (with-current-buffer buffer (setq elmo-pop3-uidl-done t))
374 (defun elmo-pop3-parse-list-response (string)
375 (let ((buffer (current-buffer))
379 (goto-char (point-min))
380 (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
383 (string-to-int (setq number (elmo-match-buffer 1)))
385 (setq size (elmo-match-buffer 2))
386 (with-current-buffer buffer
387 (elmo-set-hash-val (concat "#" number)
389 elmo-pop3-size-hash)))
390 (with-current-buffer buffer (setq elmo-pop3-list-done t))
393 (defun elmo-pop3-list-location (spec)
394 (with-current-buffer (process-buffer
395 (elmo-network-session-process-internal
396 (elmo-pop3-get-session spec)))
398 (if elmo-pop3-uidl-done
402 (setq list (cons (symbol-name atom) list)))
403 elmo-pop3-uidl-number-hash)
405 (error "POP3: Error in UIDL")))))
407 (defun elmo-pop3-list-by-uidl-subr (spec &optional nonsort)
408 (let ((flist (elmo-list-folder-by-location
410 (elmo-pop3-list-location spec))))
412 (cons (elmo-max-of-list flist) (length flist))
415 (defun elmo-pop3-list-by-list (spec)
416 (with-current-buffer (process-buffer
417 (elmo-network-session-process-internal
418 (elmo-pop3-get-session spec)))
420 (if elmo-pop3-list-done
422 (mapatoms (lambda (atom)
423 (setq list (cons (string-to-int
424 (substring (symbol-name atom) 1))
428 (error "POP3: Error in list")))))
430 (defun elmo-pop3-list-folder (spec)
431 (let ((killed (and elmo-use-killed-list
432 (elmo-msgdb-killed-list-load
433 (elmo-msgdb-expand-path spec))))
435 (elmo-pop3-commit spec)
436 (setq numbers (if elmo-pop3-use-uidl
438 (elmo-pop3-list-by-uidl-subr spec))
439 (elmo-pop3-list-by-list spec)))
440 (elmo-living-messages numbers killed)))
442 (defun elmo-pop3-max-of-folder (spec)
443 (elmo-pop3-commit spec)
444 (if elmo-pop3-use-uidl
445 (elmo-pop3-list-by-uidl-subr spec 'nonsort)
447 (elmo-network-session-process-internal
448 (elmo-pop3-get-session spec)))
451 (with-current-buffer (process-buffer process)
452 (elmo-pop3-send-command process "STAT")
453 (setq response (elmo-pop3-read-response process))
454 ;; response: "^\+OK 2 7570$"
455 (if (not (string-match "^\+OK[ \t]*\\([0-9]*\\)" response))
456 (error "POP STAT command failed")
459 (substring response (match-beginning 1)(match-end 1 ))))
460 (cons total total))))))
462 (defvar elmo-pop3-header-fetch-chop-length 200)
464 (defsubst elmo-pop3-next-result-arrived-p ()
466 ((eq (following-char) ?+)
467 (if (re-search-forward "\n\\.\r?\n" nil t)
471 (if (search-forward "\n" nil t)
477 (defun elmo-pop3-retrieve-headers (buffer tobuffer process articles)
481 (let ((number (length articles))
484 (last-point (point-min)))
485 ;; Send HEAD commands.
487 (elmo-pop3-send-command process (format
488 "top %s 0" (car articles))
490 ;;; (accept-process-output process 1)
491 (setq articles (cdr articles))
492 (setq count (1+ count))
493 ;; Every 200 requests we have to read the stream in
494 ;; order to avoid deadlocks.
495 (when (or elmo-pop3-send-command-synchronously
496 (null articles) ;All requests have been sent.
497 (zerop (% count elmo-pop3-header-fetch-chop-length)))
498 (unless elmo-pop3-send-command-synchronously
499 (accept-process-output process 1))
503 (goto-char last-point)
505 (while (elmo-pop3-next-result-arrived-p)
506 (setq last-point (point))
507 (setq received (1+ received)))
509 (when (> number elmo-display-progress-threshold)
510 (if (or (zerop (% received 5)) (= received number))
511 (elmo-display-progress
512 'elmo-pop3-retrieve-headers "Getting headers..."
513 (/ (* received 100) number))))
514 (accept-process-output process 1)
515 ;;; (accept-process-output process)
517 ;; Remove all "\r"'s.
518 (goto-char (point-min))
519 (while (search-forward "\r\n" nil t)
520 (replace-match "\n"))
521 (copy-to-buffer tobuffer (point-min) (point-max)))))
523 (defalias 'elmo-pop3-msgdb-create 'elmo-pop3-msgdb-create-as-numlist)
525 (defun elmo-pop3-msgdb-create-as-numlist (spec numlist new-mark
526 already-mark seen-mark
527 important-mark seen-list
530 (let ((process (elmo-network-session-process-internal
531 (elmo-pop3-get-session spec)))
533 (if elmo-pop3-use-uidl
534 (setq loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
535 (elmo-msgdb-location-load
536 (elmo-msgdb-expand-path spec)))))
537 (elmo-pop3-msgdb-create-by-header process numlist
538 new-mark already-mark
542 (defun elmo-pop3-uidl-to-number (uidl)
543 (string-to-number (elmo-get-hash-val uidl
544 elmo-pop3-uidl-number-hash)))
546 (defun elmo-pop3-number-to-uidl (number)
547 (elmo-get-hash-val (format "#%d" number)
548 elmo-pop3-number-uidl-hash))
550 (defun elmo-pop3-number-to-size (number)
551 (elmo-get-hash-val (format "#%d" number)
552 elmo-pop3-size-hash))
554 (defun elmo-pop3-msgdb-create-by-header (process numlist
555 new-mark already-mark
559 (let ((tmp-buffer (get-buffer-create " *ELMO Overview TMP*")))
560 (with-current-buffer (process-buffer process)
561 (if loc-alist ; use uidl.
567 (elmo-pop3-uidl-to-number (cdr (assq number loc-alist))))
569 (elmo-pop3-retrieve-headers (process-buffer process)
570 tmp-buffer process numlist)
572 (elmo-pop3-msgdb-create-message
577 new-mark already-mark seen-mark seen-list loc-alist)
578 (kill-buffer tmp-buffer)))))
580 (defun elmo-pop3-msgdb-create-message (buffer
583 numlist new-mark already-mark
588 (let (beg overview number-alist mark-alist
589 entity i number message-id gmark seen size)
591 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
592 (goto-char (point-min))
594 (message "Creating msgdb...")
596 (setq beg (save-excursion (forward-line 1) (point)))
597 (elmo-pop3-next-result-arrived-p)
601 (narrow-to-region beg (point))
603 (elmo-msgdb-create-overview-from-buffer
605 (setq numlist (cdr numlist))
608 (elmo-msgdb-append-element
610 (with-current-buffer (process-buffer process)
611 (elmo-msgdb-overview-entity-set-size
614 (elmo-pop3-number-to-size
615 (elmo-msgdb-overview-entity-get-number entity))))
619 (elmo-pop3-number-to-uidl
620 (elmo-msgdb-overview-entity-get-number entity))
622 (elmo-msgdb-overview-entity-set-number entity number)))
624 (elmo-msgdb-number-add
626 (elmo-msgdb-overview-entity-get-number entity)
628 (setq message-id (car entity))
629 (setq seen (member message-id seen-list))
630 (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
631 (if (elmo-cache-exists-p
637 (if elmo-pop3-use-cache
641 (elmo-msgdb-mark-append
643 (elmo-msgdb-overview-entity-get-number entity)
645 (when (> num elmo-display-progress-threshold)
647 (if (or (zerop (% i 5)) (= i num))
648 (elmo-display-progress
649 'elmo-pop3-msgdb-create-message "Creating msgdb..."
650 (/ (* i 100) num)))))
651 (list overview number-alist mark-alist loc-alist))))
653 (defun elmo-pop3-read-body (process outbuf)
654 (with-current-buffer (process-buffer process)
655 (let ((start elmo-pop3-read-point)
658 (while (not (re-search-forward "^\\.\r?\n" nil t))
659 (accept-process-output process)
662 (with-current-buffer outbuf
664 (insert-buffer-substring (process-buffer process) start (- end 3))
665 (elmo-delete-cr-get-content-type)))))
667 (defun elmo-pop3-read-msg (spec number outbuf &optional msgdb)
668 (let* ((loc-alist (if elmo-pop3-use-uidl
670 (elmo-msgdb-get-location msgdb)
671 (elmo-msgdb-location-load
672 (elmo-msgdb-expand-path spec)))))
673 (process (elmo-network-session-process-internal
674 (elmo-pop3-get-session spec)))
676 (with-current-buffer (process-buffer process)
678 (setq number (elmo-pop3-uidl-to-number
679 (cdr (assq number loc-alist)))))
681 (elmo-pop3-send-command process
682 (format "retr %s" number))
683 (when (null (setq response (elmo-pop3-read-response
685 (error "Fetching message failed"))
686 (setq response (elmo-pop3-read-body process outbuf))
688 (goto-char (point-min))
689 (while (re-search-forward "^\\." nil t)
694 (defun elmo-pop3-delete-msg (process number loc-alist)
695 (with-current-buffer (process-buffer process)
696 (let (response errmsg msg)
698 (setq number (elmo-pop3-uidl-to-number
699 (cdr (assq number loc-alist)))))
702 (elmo-pop3-send-command process
703 (format "dele %s" number))
704 (when (null (setq response (elmo-pop3-read-response
706 (error "Deleting message failed")))
707 (error "Deleting message failed")))))
709 (defun elmo-pop3-delete-msgs (spec msgs &optional msgdb)
710 (let ((loc-alist (if elmo-pop3-use-uidl
712 (elmo-msgdb-get-location msgdb)
713 (elmo-msgdb-location-load
714 (elmo-msgdb-expand-path spec)))))
715 (process (elmo-network-session-process-internal
716 (elmo-pop3-get-session spec))))
717 (mapcar '(lambda (msg) (elmo-pop3-delete-msg
718 process msg loc-alist))
721 (defun elmo-pop3-search (spec condition &optional numlist)
722 (error "Searching in pop3 folder is not implemented yet"))
724 (defun elmo-pop3-use-cache-p (spec number)
727 (defun elmo-pop3-local-file-p (spec number)
730 (defun elmo-pop3-port-label (spec)
732 (if (elmo-pop3-spec-stream-type spec)
733 (concat "!" (symbol-name
734 (elmo-network-stream-type-symbol
735 (elmo-pop3-spec-stream-type spec)))))))
737 (defsubst elmo-pop3-portinfo (spec)
738 (list (elmo-pop3-spec-hostname spec)
739 (elmo-pop3-spec-port spec)))
741 (defun elmo-pop3-plugged-p (spec)
742 (apply 'elmo-plugged-p
743 (append (elmo-pop3-portinfo spec)
744 (list nil (quote (elmo-pop3-port-label spec))))))
746 (defun elmo-pop3-set-plugged (spec plugged add)
747 (apply 'elmo-set-plugged plugged
748 (append (elmo-pop3-portinfo spec)
749 (list nil nil (quote (elmo-pop3-port-label spec)) add))))
751 (defalias 'elmo-pop3-sync-number-alist
752 'elmo-generic-sync-number-alist)
753 (defalias 'elmo-pop3-list-folder-unread
754 'elmo-generic-list-folder-unread)
755 (defalias 'elmo-pop3-list-folder-important
756 'elmo-generic-list-folder-important)
757 (defalias 'elmo-pop3-folder-diff 'elmo-generic-folder-diff)
759 (defun elmo-pop3-commit (spec)
760 (if (elmo-pop3-plugged-p spec)
761 (let ((session (elmo-pop3-get-session spec 'if-exists)))
763 (elmo-network-close-session session)))))
767 (product-provide (provide 'elmo-pop3) (require 'elmo-version))
769 ;;; elmo-pop3.el ends here