* riece-addon.el (riece-insinuate-addon): Add optional 2nd
[riece] / lisp / riece-server.el
index 7a918ff..d49e4d4 100644 (file)
@@ -37,7 +37,7 @@
       (:nickname riece-nickname)
       (:username riece-username)
       (:password)
-      (:function #'open-network-stream)
+      (:function riece-default-open-connection-function)
       (:coding riece-default-coding-system))
     "Mapping from keywords to default values.
 All keywords that can be used must be listed here."))
@@ -103,21 +103,24 @@ the `riece-server-keyword-map' variable."
        (error "Server closed"))))
 
 (put 'riece-with-server-buffer 'lisp-indent-function 1)
+(put 'riece-with-server-buffer 'edebug-form-spec '(form body))
 
 (defun riece-process-send-string (process string)
   (with-current-buffer (process-buffer process)
     (process-send-string process (riece-encode-coding-string string))))
 
-(defun riece-send-string (string)
-  (let* ((server-name
-         (or riece-overriding-server-name
+(defun riece-current-server-name ()
+  (or riece-overriding-server-name
                                        ;already in the server buffer
-             (if (local-variable-p 'riece-server-name (current-buffer))
-                 riece-server-name
-               (if riece-current-channel
-                   (riece-identity-server riece-current-channel)
-                 (if (riece-server-opened "")
-                     "")))))
+      (if (local-variable-p 'riece-server-name (current-buffer))
+         riece-server-name
+       (if riece-current-channel
+           (riece-identity-server riece-current-channel)
+         (if (riece-server-opened "")
+             "")))))
+
+(defun riece-send-string (string)
+  (let* ((server-name (riece-current-server-name))
         (process (riece-server-process server-name)))
     (unless process
       (error "%s" (substitute-command-keys
@@ -150,24 +153,42 @@ the `riece-server-keyword-map' variable."
        (message "Logging in to %s..." server-name))
       (if riece-reconnect-with-password        ;password incorrect or not set.
          (unwind-protect
-             (setq password (riece-read-passwd "Password: "))
+             (setq password
+                   (condition-case nil
+                       (let (inhibit-quit)
+                         (if (equal server-name "")
+                             (riece-read-passwd "Password: ")
+                           (riece-read-passwd (format "Password for %s: "
+                                                      server-name))))
+                     (quit
+                      (if (equal server-name "")
+                          (message "Password: Quit")
+                        (message (format "Password for %s: Quit"
+                                         server-name)))
+                      'quit)))
            (setq riece-reconnect-with-password nil)))
-      (if password
-         (riece-process-send-string process
-                                    (format "PASS %s\r\n" password)))
-      (riece-process-send-string process
-                                (format "USER %s * * :%s\r\n"
-                                        (user-real-login-name)
-                                        (or username
-                                            "No information given")))
-      (riece-process-send-string process (format "NICK %s\r\n" nickname))
-      (with-current-buffer (process-buffer process)
-       (setq riece-last-nickname riece-real-nickname
-             riece-nick-accepted 'sent
-             riece-coding-system coding))
-      (setq riece-server-process-alist
-           (cons (cons server-name process)
-                 riece-server-process-alist)))))
+      (if (eq password 'quit)
+         (progn
+           (riece-close-server-process process)
+           ;; If no server process is available, exit.
+           (unless riece-server-process-alist
+             (riece-exit)))
+       (if password
+           (riece-process-send-string process
+                                      (format "PASS %s\r\n" password)))
+       (riece-process-send-string process
+                                  (format "USER %s * * :%s\r\n"
+                                          (user-real-login-name)
+                                          (or username
+                                              "No information given")))
+       (riece-process-send-string process (format "NICK %s\r\n" nickname))
+       (with-current-buffer (process-buffer process)
+         (setq riece-last-nickname riece-real-nickname
+               riece-nick-accepted 'sent
+               riece-coding-system coding))
+       (setq riece-server-process-alist
+             (cons (cons server-name process)
+                   riece-server-process-alist))))))
 
 (defun riece-reset-process-buffer (process)
   (save-excursion
@@ -202,25 +223,33 @@ the `riece-server-keyword-map' variable."
        (delq (rassq process riece-server-process-alist)
              riece-server-process-alist)))
 
+(defun riece-server-process-opened (process)
+  (not (null (memq (process-status process) '(open run)))))
+
 (defun riece-server-opened (&optional server-name)
-  (let ((alist riece-server-process-alist))
-    (catch 'found
-      (while alist
-       (if (memq (process-status (cdr (car alist))) '(open run))
-           (throw 'found t))
-       (setq alist (cdr alist))))))
+  (if server-name
+      (let ((process (riece-server-process server-name)))
+       (and process
+            (riece-server-process-opened process)))
+    (let ((alist riece-server-process-alist))
+      (catch 'found
+       (while alist
+         (if (riece-server-process-opened (cdr (car alist)))
+             (throw 'found t))
+         (setq alist (cdr alist)))))))
 
 (eval-when-compile
   (autoload 'riece-exit "riece"))
 (defun riece-quit-server-process (process &optional message)
-  (run-at-time riece-quit-timeout nil
-              (lambda (process)
-                (when (rassq process riece-server-process-alist)
-                  (riece-close-server-process process)
-                  ;; If no server process is available, exit.
-                  (unless riece-server-process-alist
-                    (riece-exit))))
-              process)
+  (if riece-quit-timeout
+      (riece-run-at-time riece-quit-timeout nil
+                        (lambda (process)
+                          (when (rassq process riece-server-process-alist)
+                            (riece-close-server-process process)
+                            ;; If no server process is available, exit.
+                            (unless riece-server-process-alist
+                              (riece-exit))))
+                        process))
   (riece-process-send-string process
                             (if message
                                 (format "QUIT :%s\r\n" message)