[cl-irc-devel] [PATCH 3/6] Factorize mode change

Julien Danjou julien at danjou.info
Sun Jan 13 14:04:20 UTC 2013


The code used into mode-message and rpl_channelmodeis-message is exactly the
same, so let's factorize this a bit to less code.

Signed-off-by: Julien Danjou <julien at danjou.info>
---
 event.lisp   | 34 ++++++----------------------------
 package.lisp |  1 +
 utility.lisp | 15 +++++++++++++++
 3 files changed, 22 insertions(+), 28 deletions(-)

diff --git a/event.lisp b/event.lisp
index 5e6e417..57ccf51 100644
--- a/event.lisp
+++ b/event.lisp
@@ -279,20 +279,9 @@ objects in sync."))
     (destructuring-bind
         (target channel &rest mode-arguments)
         arguments
-    (let* ((channel (find-channel connection channel))
-           (mode-changes
-            (when channel
-              (parse-mode-arguments connection channel mode-arguments
-                                    :server-p (user connection)))))
-      (dolist (change mode-changes)
-        (destructuring-bind
-            (op mode-name value)
-            change
-          (unless (has-mode-p channel mode-name)
-            (add-mode channel mode-name
-                      (make-mode connection channel mode-name)))
-          (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
-                   channel mode-name value)))))))
+    (let ((channel (find-channel connection channel)))
+      (when channel
+        (apply-mode-changes connection channel mode-arguments (user connection)))))))
 
 (defmethod default-hook ((message irc-mode-message))
   (destructuring-bind
@@ -300,20 +289,9 @@ objects in sync."))
       (arguments message)
     (let* ((connection (connection message))
            (target (or (find-channel connection target)
-                       (find-user connection target)))
-           (mode-changes
-            (when target
-              (parse-mode-arguments connection target arguments
-                                     :server-p (user connection)))))
-      (dolist (change mode-changes)
-        (destructuring-bind
-            (op mode-name value)
-            change
-          (unless (has-mode-p target mode-name)
-            (add-mode target mode-name
-                      (make-mode connection target mode-name)))
-          (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
-                   target mode-name value))))))
+                       (find-user connection target))))
+      (when target
+        (apply-mode-changes connection target arguments (user connection))))))
 
 (defmethod default-hook ((message irc-nick-message))
   (with-slots
diff --git a/package.lisp b/package.lisp
index c09537a..bd5a480 100644
--- a/package.lisp
+++ b/package.lisp
@@ -32,6 +32,7 @@
              :get-mode
              :set-mode
              :unset-mode
+             :apply-mode-changes
              :parse-mode-arguments
              :parse-raw-message
              :normalize-nickname
diff --git a/utility.lisp b/utility.lisp
index 12c327c..9894ed0 100644
--- a/utility.lisp
+++ b/utility.lisp
@@ -493,6 +493,21 @@ It returns a list of mode-description records."
               (split-sequence:split-sequence #\: x))
           (split-sequence:split-sequence #\, argument)))
 
+(defun apply-mode-changes (connection target mode-arguments server-p)
+  (dolist (change (parse-mode-arguments connection target mode-arguments
+                                        :server-p server-p))
+    (apply-mode-change connection target change)))
+
+(defun apply-mode-change (connection target change)
+  (destructuring-bind
+      (op mode-name value)
+      change
+    (unless (has-mode-p target mode-name)
+      (add-mode target mode-name
+                (make-mode connection target mode-name)))
+    (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
+             target mode-name value)))
+
 (defun parse-mode-arguments (connection target arguments &key server-p)
   "Create a list of mode changes with their arguments for `target'
    from `mode-string' and `arguments'.
-- 
1.8.1





More information about the cl-irc-devel mailing list