(riece-log-display-message-function): Use
[elisp/riece.git] / lisp / riece-handle.el
index e83fbe5..9dc1cb7 100644 (file)
@@ -19,8 +19,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Code:
 
 (require 'riece-channel)
 (require 'riece-naming)
 (require 'riece-signal)
+(require 'riece-mode)
+(require 'riece-000)
+(require 'riece-200)
+(require 'riece-300)
+(require 'riece-400)
+(require 'riece-500)
+
+(defun riece-default-handle-numeric-reply
+  (client-prefix prefix number name string)
+  (riece-insert
+   (list riece-dialogue-buffer riece-others-buffer)
+   (concat client-prefix
+          (riece-concat-server-name
+           (mapconcat #'identity (riece-split-parameters string) " "))
+          "\n")))
 
 (defun riece-handle-nick-message (prefix string)
   (let* ((old (riece-prefix-nickname prefix))
     (riece-naming-assert-rename old new)
     (if (riece-identity-member old-identity riece-current-channels)
        (setq channels (cons new channels)))
-    (riece-insert-change (mapcar
-                         (lambda (channel)
-                           (riece-channel-buffer (riece-make-identity
-                                                  channel riece-server-name)))
-                         channels)
+    (riece-insert-change (delq nil (mapcar
+                                   (lambda (channel)
+                                     (riece-channel-buffer
+                                      (riece-make-identity
+                                       channel riece-server-name)))
+                                   channels))
                         (format "%s -> %s\n"
                                 (riece-format-identity old-identity t)
                                 (riece-format-identity new-identity t)))
                                 (riece-format-identity new-identity t)))
                          "\n"))))
 
-(defun riece-handle-privmsg-message (prefix string)
+(defun riece-handle-privmsg-message (prefix decoded)
   (let* ((user (riece-prefix-nickname prefix))
-        (parameters (riece-split-parameters string))
+        (parameters (riece-split-parameters (riece-decoded-string decoded)))
         (targets (split-string (car parameters) ","))
-        (message (nth 1 parameters)))
+        message)
+    (setq parameters (riece-split-parameters
+                     (riece-decoded-string-for-identity
+                      decoded
+                      (riece-make-identity (car targets) riece-server-name)))
+         message (nth 1 parameters))
     (riece-display-message
      (riece-make-message (riece-make-identity user
                                              riece-server-name)
                         (riece-make-identity (car targets)
                                              riece-server-name)
-                        message nil
+                        message
+                        nil
                         (riece-identity-equal-no-server
                          user riece-real-nickname)))))
 
-(defun riece-handle-notice-message (prefix string)
+(defun riece-handle-notice-message (prefix decoded)
   (let* ((user (if prefix
                   (riece-prefix-nickname prefix)))
-        (parameters (riece-split-parameters string))
+        (parameters (riece-split-parameters (riece-decoded-string decoded)))
         (targets (split-string (car parameters) ","))
-        (message (nth 1 parameters)))
+        message)
+    (setq parameters (riece-split-parameters
+                     (riece-decoded-string-for-identity
+                      decoded
+                      (riece-make-identity (car targets) riece-server-name)))
+         message (nth 1 parameters))
     (if user
        (riece-display-message
         (riece-make-message (riece-make-identity user
                             (riece-make-identity (car targets)
                                                  riece-server-name)
                             message 'notice
-                            (riece-identity-equal-no-server
-                             user riece-real-nickname)))
+                            (if riece-real-nickname
+                                (riece-identity-equal-no-server
+                                 user riece-real-nickname))))
       ;; message from server
       (riece-insert-notice
        (list riece-dialogue-buffer riece-others-buffer)
             (buffer (riece-channel-buffer channel-identity)))
        (riece-insert-change
         buffer
-        (format "%s (%s) has joined %s\n"
+        (format (riece-mcat "%s (%s) has joined %s\n")
                 (riece-format-identity user-identity t)
                 (riece-user-get-user-at-host user)
                 (riece-format-identity channel-identity t)))
           riece-dialogue-buffer)
         (concat
          (riece-concat-server-name
-          (format "%s (%s) has joined %s"
+          (format (riece-mcat "%s (%s) has joined %s")
                   (riece-format-identity user-identity t)
                   (riece-user-get-user-at-host user)
                   (riece-format-identity channel-identity t)))
          "\n")))
       (setq channels (cdr channels)))))
 
-(defun riece-handle-part-message (prefix string)
+(defun riece-handle-part-message (prefix decoded)
   (let* ((user (riece-prefix-nickname prefix))
-        (parameters (riece-split-parameters string))
+        (parameters (riece-split-parameters (riece-decoded-string decoded)))
         ;; RFC2812 3.2.2 doesn't recommend server to send part
         ;; messages which contain multiple targets.
         (channels (split-string (car parameters) ","))
-        (message (nth 1 parameters))
         (user-identity (riece-make-identity user riece-server-name)))
     (while channels
-      (riece-naming-assert-part user (car channels))
       (let* ((channel-identity (riece-make-identity (car channels)
                                                    riece-server-name))
-            (buffer (riece-channel-buffer channel-identity)))
+            (buffer (riece-channel-buffer channel-identity))
+            message)
+       (setq parameters (riece-split-parameters
+                         (riece-decoded-string-for-identity decoded
+                                                            channel-identity))
+             message (nth 1 parameters))
        (riece-insert-change
         buffer
         (concat
          (riece-concat-message
-          (format "%s has left %s"
+          (format (riece-mcat "%s has left %s")
                   (riece-format-identity user-identity t)
                   (riece-format-identity channel-identity t))
           message)
         (concat
          (riece-concat-server-name
           (riece-concat-message
-           (format "%s has left %s"
+           (format (riece-mcat "%s has left %s")
                    (riece-format-identity user-identity t)
                    (riece-format-identity channel-identity t))
            message))
          "\n")))
+      (riece-naming-assert-part user (car channels))
       (setq channels (cdr channels)))))
 
-(defun riece-handle-kick-message (prefix string)
+(defun riece-handle-kick-message (prefix decoded)
   (let* ((kicker (riece-prefix-nickname prefix))
-        (parameters (riece-split-parameters string))
+        (parameters (riece-split-parameters (riece-decoded-string decoded)))
         (channel (car parameters))
         (user (nth 1 parameters))
-        (message (nth 2 parameters))
+        message
         (kicker-identity (riece-make-identity kicker riece-server-name))
         (channel-identity (riece-make-identity channel riece-server-name))
         (user-identity (riece-make-identity user riece-server-name)))
+    (setq parameters (riece-split-parameters
+                     (riece-decoded-string-for-identity decoded
+                                                        channel-identity))
+         message (nth 2 parameters))
     (riece-naming-assert-part user channel)
     (let ((buffer (riece-channel-buffer channel-identity)))
       (riece-insert-change
        buffer
        (concat
        (riece-concat-message
-        (format "%s kicked %s out from %s"
+        (format (riece-mcat "%s kicked %s out from %s")
                 (riece-format-identity kicker-identity t)
                 (riece-format-identity user-identity t)
                 (riece-format-identity channel-identity t))
        (concat
        (riece-concat-server-name
         (riece-concat-message
-         (format "%s kicked %s out from %s\n"
+         (format (riece-mcat "%s kicked %s out from %s\n")
                 (riece-format-identity kicker-identity t)
                 (riece-format-identity user-identity t)
                 (riece-format-identity channel-identity t))
         (user-identity (riece-make-identity user riece-server-name)))
     ;; If you are talking with the user, quit it.
     (if (riece-identity-member user-identity riece-current-channels)
-       (riece-part-channel user))
+       (riece-part-channel user-identity))
     (setq pointer channels)
     (while pointer
       (riece-naming-assert-part user (car pointer))
       (setq pointer (cdr pointer)))
     (let ((buffers
-          (mapcar
-           (lambda (channel)
-             (riece-channel-buffer (riece-make-identity channel
-                                                        riece-server-name)))
-           channels)))
+          (delq nil (mapcar
+                     (lambda (channel)
+                       (riece-channel-buffer (riece-make-identity
+                                              channel riece-server-name)))
+                     channels))))
       (riece-insert-change
        buffers
        (concat
        (riece-concat-message
-        (format "%s has left IRC"
+        (format (riece-mcat "%s has left IRC")
                 (riece-format-identity user-identity t))
         message)
        "\n"))
        (concat
        (riece-concat-server-name
         (riece-concat-message
-         (format "%s has left IRC"
+         (format (riece-mcat "%s has left IRC")
                  (riece-format-identity user-identity t))
          message))
        "\n")))))
       (riece-naming-assert-part user (car pointer))
       (setq pointer (cdr pointer)))
     (let ((buffers
-          (mapcar
-           (lambda (channel)
-             (riece-channel-buffer (riece-make-identity channel
-                                                        riece-server-name)))
-           channels)))
+          (delq nil (mapcar
+                     (lambda (channel)
+                       (riece-channel-buffer (riece-make-identity
+                                              channel riece-server-name)))
+                     channels))))
       (riece-insert-change
        buffers
        (concat
        (riece-concat-message
-        (format "%s killed %s"
+        (format (riece-mcat "%s killed %s")
                 (riece-format-identity killer-identity t)
                 (riece-format-identity user-identity t))
         message)
        (concat
        (riece-concat-server-name
         (riece-concat-message
-         (format "%s killed %s"
+         (format (riece-mcat "%s killed %s")
                 (riece-format-identity killer-identity t)
                 (riece-format-identity user-identity t))
          message))
      (list riece-dialogue-buffer riece-others-buffer)
      (concat
       (riece-concat-server-name
-       (format "%s invites %s to %s"
+       (format (riece-mcat "%s invites %s to %s")
               (riece-format-identity (riece-make-identity
                                       user riece-server-name))
               (riece-format-identity (riece-make-identity
               (riece-format-identity channel-identity)))
       "\n"))))
 
-(defun riece-handle-topic-message (prefix string)
+(defun riece-handle-topic-message (prefix decoded)
   (let* ((user (riece-prefix-nickname prefix))
-        (parameters (riece-split-parameters string))
+        (parameters (riece-split-parameters (riece-decoded-string decoded)))
         (channel (car parameters))
-        (topic (nth 1 parameters))
+        topic
         (user-identity (riece-make-identity user riece-server-name))
         (channel-identity (riece-make-identity channel riece-server-name)))
+    (setq parameters (riece-split-parameters
+                     (riece-decoded-string-for-identity decoded
+                                                        channel-identity))
+         topic (nth 1 parameters))
     (riece-channel-set-topic (riece-get-channel channel) topic)
     (riece-emit-signal 'channel-topic-changed
                       channel-identity topic)
     (let ((buffer (riece-channel-buffer channel-identity)))
       (riece-insert-change
        buffer
-       (format "Topic by %s: %s\n"
+       (format (riece-mcat "Topic by %s: %s\n")
               (riece-format-identity user-identity t)
               topic))
       (riece-insert-change
         riece-dialogue-buffer)
        (concat
        (riece-concat-server-name
-        (format "Topic on %s by %s: %s"
+        (format (riece-mcat "Topic on %s by %s: %s")
                 (riece-format-identity channel-identity t)
                 (riece-format-identity user-identity t)
                 topic))
        "\n")))))
 
-(defun riece-parse-modes (string)
-  (let ((start 0)
-       result)
-    (while (and (string-match "[-+]\\([^ ]*\\) *" string start)
-               (= (match-beginning 0) start))
-      (let ((toggle (eq (aref string 0) ?+))
-           (modes (string-to-list (match-string 1 string))))
-       (setq start (match-end 0))
-       (while modes
-         (if (and (string-match "\\([^-+][^ ]*\\) *" string start)
-                  (= (match-beginning 0) start))
-             (setq start (match-end 0)
-                   result (cons (list (car modes) toggle
-                                      (match-string 1 string))
-                                result))
-           (setq result (cons (list (car modes) toggle)
-                              result)))
-         (setq modes (cdr modes)))))
-    (nreverse result)))
-
-(defun riece-handle-channel-modes (channel modes)
-  (while modes
-    (cond
-     ((eq (car (car modes)) ?o)
-      (riece-channel-toggle-operator channel
-                                    (nth 2 (car modes))
-                                    (nth 1 (car modes)))
-      (riece-emit-signal 'channel-operators-changed
-                        (riece-make-identity channel
-                                             riece-server-name)
-                        (riece-make-identity (nth 2 (car modes))
-                                             riece-server-name)
-                        (nth 1 (car modes))))
-     ((eq (car (car modes)) ?v)
-      (riece-channel-toggle-speaker channel
-                                   (nth 2 (car modes))
-                                   (nth 1 (car modes)))
-      (riece-emit-signal 'channel-speakers-changed
-                        (riece-make-identity channel
-                                             riece-server-name)
-                        (riece-make-identity (nth 2 (car modes))
-                                             riece-server-name)
-                        (nth 1 (car modes))))
-     ((eq (car (car modes)) ?b)
-      (riece-channel-toggle-banned channel
-                                  (nth 2 (car modes))
-                                  (nth 1 (car modes))))
-     ((eq (car (car modes)) ?e)
-      (riece-channel-toggle-uninvited channel
-                                     (nth 2 (car modes))
-                                     (nth 1 (car modes))))
-     ((eq (car (car modes)) ?I)
-      (riece-channel-toggle-invited channel
-                                   (nth 2 (car modes))
-                                   (nth 1 (car modes))))
-     (t
-      (apply #'riece-channel-toggle-mode channel (car modes))))
-    (setq modes (cdr modes)))
-  (riece-emit-signal 'channel-modes-changed
-                    (riece-make-identity channel
-                                         riece-server-name)))
-
 (defun riece-handle-mode-message (prefix string)
   (let* ((user (riece-prefix-nickname prefix))
         (user-identity (riece-make-identity user riece-server-name))
       (setq channel (match-string 1 string)
            string (substring string (match-end 0)))
       (if (string-match (concat "^" riece-channel-regexp "$") channel)
-         (riece-handle-channel-modes channel (riece-parse-modes string)))
+         (riece-naming-assert-channel-modes channel
+                                            (riece-parse-modes string)))
       (let* ((channel-identity (riece-make-identity channel riece-server-name))
             (buffer (riece-channel-buffer channel-identity)))
        (riece-insert-change
         buffer
-        (format "Mode by %s: %s\n"
+        (format (riece-mcat "Mode by %s: %s\n")
                 (riece-format-identity user-identity t)
                 string))
        (riece-insert-change
           riece-dialogue-buffer)
         (concat
          (riece-concat-server-name
-          (format "Mode on %s by %s: %s"
+          (format (riece-mcat "Mode on %s by %s: %s")
                   (riece-format-identity channel-identity t)
                   (riece-format-identity user-identity t)
                   string))