* elmo-localdir.el (elmo-folder-expand-msgdb-path): Expand
[elisp/wanderlust.git] / elmo / elmo-pop3.el
index 6dfb0d7..d594e93 100644 (file)
@@ -66,15 +66,15 @@ set as non-nil.")
   "Non-nil forces POP3 folder as debug mode.
 Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
 
-(defvar elmo-pop3-debug-inhibit-logging nil)
-
 ;;; Debug
 (defsubst elmo-pop3-debug (message &rest args)
   (if elmo-pop3-debug
-      (with-current-buffer (get-buffer-create "*POP3 DEBUG*")
-       (goto-char (point-max))
-       (if elmo-pop3-debug-inhibit-logging
-           (insert "NO LOGGING\n")
+      (let ((biff (string-match "BIFF-" (buffer-name)))
+           pos)
+       (with-current-buffer (get-buffer-create (concat "*POP3 DEBUG*"
+                                                       (if biff "BIFF")))
+         (goto-char (point-max))
+         (setq pos (point))
          (insert (apply 'format message args) "\n")))))
 
 ;;; ELMO POP3 folder
@@ -149,33 +149,46 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
     (when (memq (process-status
                 (elmo-network-session-process-internal session))
                '(open run))
-      (let ((buffer (process-buffer
-                    (elmo-network-session-process-internal session))))
-       (elmo-pop3-send-command (elmo-network-session-process-internal session)
-                               "quit")
-       ;; process is dead.
-       (or (elmo-pop3-read-response
-            (elmo-network-session-process-internal session)
-            t)
-           (error "POP error: QUIT failed"))))
+      (elmo-pop3-send-command (elmo-network-session-process-internal session)
+                             "quit")
+      ;; process is dead.
+      (or (elmo-pop3-read-response
+          (elmo-network-session-process-internal session)
+          t)
+         (error "POP error: QUIT failed")))
     (kill-buffer (process-buffer
                  (elmo-network-session-process-internal session)))
     (delete-process (elmo-network-session-process-internal session))))
 
 (defun elmo-pop3-get-session (folder &optional if-exists)
+  "Get POP3 session for FOLDER.
+If IF-EXISTS is non-nil, don't get new session.
+If IF-EXISTS is `any-exists', get BIFF session or normal session if exists."
   (let ((elmo-pop3-use-uidl-internal (if elmo-inhibit-number-mapping
                                         nil
                                       (elmo-pop3-folder-use-uidl-internal
                                        folder))))
-    (elmo-network-get-session 'elmo-pop3-session "POP3" folder if-exists)))
-
-(defun elmo-pop3-send-command (process command &optional no-erase)
+    (if (eq if-exists 'any-exists)
+       (or (elmo-network-get-session 'elmo-pop3-session
+                                     "POP3"
+                                     folder if-exists)
+           (elmo-network-get-session 'elmo-pop3-session
+                                     "BIFF-POP3"
+                                     folder if-exists))
+      (elmo-network-get-session 'elmo-pop3-session
+                               (concat
+                                (if (elmo-folder-biff-internal folder)
+                                    "BIFF-")
+                                "POP3")
+                               folder if-exists))))
+
+(defun elmo-pop3-send-command (process command &optional no-erase no-log)
   (with-current-buffer (process-buffer process)
     (unless no-erase
       (erase-buffer))
     (goto-char (point-min))
     (setq elmo-pop3-read-point (point))
-    (elmo-pop3-debug "SEND: %s\n" command)
+    (elmo-pop3-debug "SEND: %s\n" (if no-log "<NO LOGGING>" command))
     (process-send-string process command)
     (process-send-string process "\r\n")))
 
@@ -190,7 +203,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
       (while response-continue
        (goto-char elmo-pop3-read-point)
        (while (not (re-search-forward "\r?\n" nil t))
-         (accept-process-output process)
+         (accept-process-output process 1)
          (goto-char elmo-pop3-read-point))
        (setq match-end (point))
        (setq response-string
@@ -220,38 +233,44 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
       return-value)))
 
 (defun elmo-pop3-process-filter (process output)
-  (save-excursion
-    (set-buffer (process-buffer process))
-    (goto-char (point-max))
-    (insert output)
-    (elmo-pop3-debug "RECEIVED: %s\n" output)
-    (if (and elmo-pop3-total-size
-            (> elmo-pop3-total-size
-               (min elmo-display-retrieval-progress-threshold 100)))
-       (elmo-display-progress
-        'elmo-display-retrieval-progress
-        (format "Retrieving (%d/%d bytes)..."
-                (buffer-size)
-                elmo-pop3-total-size)
-        (/ (buffer-size) (/ elmo-pop3-total-size 100))))))
+  (when (buffer-live-p (process-buffer process))
+    (with-current-buffer (process-buffer process)
+      (goto-char (point-max))
+      (insert output)
+      (elmo-pop3-debug "RECEIVED: %s\n" output)
+      (if (and elmo-pop3-total-size
+              (> elmo-pop3-total-size
+                 (min elmo-display-retrieval-progress-threshold 100)))
+         (elmo-display-progress
+          'elmo-display-retrieval-progress
+          (format "Retrieving (%d/%d bytes)..."
+                  (buffer-size)
+                  elmo-pop3-total-size)
+          (/ (buffer-size) (/ elmo-pop3-total-size 100)))))))
 
 (defun elmo-pop3-auth-user (session)
   (let ((process (elmo-network-session-process-internal session)))
     ;; try USER/PASS
     (elmo-pop3-send-command
      process
-     (format "user %s" (elmo-network-session-user-internal session)))
+     (format "user %s" (elmo-network-session-user-internal session))
+     nil 'no-log)
     (or (elmo-pop3-read-response process t)
-       (signal 'elmo-authenticate-error
-               '(elmo-pop-auth-user)))
+       (progn
+         (delete-process process)
+         (signal 'elmo-authenticate-error
+                 '(elmo-pop-auth-user))))
     (elmo-pop3-send-command  process
                             (format
                              "pass %s"
                              (elmo-get-passwd
-                              (elmo-network-session-password-key session))))
+                              (elmo-network-session-password-key session)))
+                            nil 'no-log)
     (or (elmo-pop3-read-response process t)
-       (signal 'elmo-authenticate-error
-               '(elmo-pop-auth-user)))))
+       (progn
+         (delete-process process)
+         (signal 'elmo-authenticate-error
+                 '(elmo-pop-auth-user))))))
 
 (defun elmo-pop3-auth-apop (session)
   (if (string-match "^\+OK .*\\(<[^\>]+>\\)"
@@ -267,12 +286,15 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
                           1
                           (elmo-network-session-greeting-internal session))
                          (elmo-get-passwd
-                          (elmo-network-session-password-key session))))))
+                          (elmo-network-session-password-key session)))))
+        nil 'no-log)
        (or (elmo-pop3-read-response
             (elmo-network-session-process-internal session)
             t)
-           (signal 'elmo-authenticate-error
-                   '(elmo-pop3-auth-apop))))
+           (progn
+             (delete-process (elmo-network-session-process-internal session))
+             (signal 'elmo-authenticate-error
+                     '(elmo-pop3-auth-apop)))))
     (signal 'elmo-open-error '(elmo-pop3-auth-apop))))
 
 (luna-define-method elmo-network-initialize-session-buffer :after
@@ -314,7 +336,6 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
   (with-current-buffer (process-buffer
                        (elmo-network-session-process-internal session))
     (let* ((process (elmo-network-session-process-internal session))
-          (elmo-pop3-debug-inhibit-logging t)
           (auth (elmo-network-session-auth-internal session))
           (auth (mapcar '(lambda (mechanism) (upcase (symbol-name mechanism)))
                         (if (listp auth) auth (list auth))))
@@ -355,7 +376,8 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
                          (concat
                           " "
                           (elmo-base64-encode-string
-                           (sasl-step-data step) 'no-line-break))))) ;)
+                           (sasl-step-data step) 'no-line-break))))
+            nil 'no-log)
            (catch 'done
              (while t
                (unless (setq response (elmo-pop3-read-response process t))
@@ -383,7 +405,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
                 (if (sasl-step-data step)
                     (elmo-base64-encode-string (sasl-step-data step)
                                                'no-line-break)
-                  "")))))))))
+                  "") nil 'no-log))))))))
 
 (luna-define-method elmo-network-setup-session ((session
                                                 elmo-pop3-session))
@@ -420,7 +442,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
          match-end)
       (goto-char elmo-pop3-read-point)
       (while (not (re-search-forward "^\\.\r\n" nil t))
-       (accept-process-output process)
+       (accept-process-output process 1)
        (goto-char elmo-pop3-read-point))
       (setq match-end (point))
       (elmo-delete-cr
@@ -434,7 +456,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
     (expand-file-name (elmo-net-folder-server-internal folder)
                      (expand-file-name
                       "pop"
-                      elmo-msgdb-dir)))))
+                      elmo-msgdb-directory)))))
 
 (luna-define-method elmo-folder-exists-p ((folder elmo-pop3-folder))
   (if (and elmo-pop3-exists-exactly
@@ -447,7 +469,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
              (setq session (elmo-pop3-get-session folder))
            (if session
                (elmo-network-close-session session)))))
-    t))
+    (file-directory-p (elmo-folder-msgdb-path folder))))
 
 (defun elmo-pop3-parse-uidl-response (string)
   (let ((buffer (current-buffer))
@@ -474,7 +496,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
     (with-temp-buffer
       (insert string)
       (goto-char (point-min))
-      (while (re-search-forward "^\\([0-9]+\\)[\t ]\\([0-9]+\\)$" nil t)
+      (while (re-search-forward "^\\([0-9]+\\)[\t ]+\\([0-9]+\\)$" nil t)
        (setq alist
              (cons
               (cons (elmo-match-buffer 1)
@@ -565,7 +587,7 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
       (elmo-pop3-list-by-uidl-subr folder)
     (elmo-pop3-list-by-list folder)))
 
-(luna-define-method elmo-folder-list-messages-internal
+(luna-define-method elmo-folder-list-messages-plugged
   ((folder elmo-pop3-folder) &optional nohide)
   (elmo-pop3-folder-list-messages folder))
 
@@ -809,12 +831,13 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
          end)
       (goto-char start)
       (while (not (re-search-forward "^\\.\r?\n" nil t))
-       (accept-process-output process)
+       (accept-process-output process 1)
        (goto-char start))
       (setq end (point))
       (with-current-buffer outbuf
        (erase-buffer)
-       (insert-buffer-substring (process-buffer process) start (- end 3))))))
+       (insert-buffer-substring (process-buffer process) start (- end 3)))
+      t)))
 
 (luna-define-method elmo-folder-open-internal ((folder elmo-pop3-folder))
   (if (and (not elmo-inhibit-number-mapping)
@@ -861,12 +884,13 @@ Debug information is inserted in the buffer \"*POP3 DEBUG*\"")
              (when (null (setq response (elmo-pop3-read-response
                                          process t)))
                (error "Fetching message failed"))
-             (setq response (elmo-pop3-read-body process outbuf)))
+             (setq response  (elmo-pop3-read-body process outbuf)))
          (setq elmo-pop3-total-size nil))
        (unless elmo-inhibit-display-retrieval-progress
          (elmo-display-progress
-          'elmo-display-retrieval-progress "" 100)  ; remove progress bar.
-         (message "Retrieving...done."))
+          'elmo-display-retrieval-progress
+          "Retrieving..." 100)  ; remove progress bar.
+         (message "Retrieving...done"))
        (set-buffer outbuf)
        (goto-char (point-min))
        (while (re-search-forward "^\\." nil t)