Added autoload to main start procedure.
[lurk.git] / murk.el
diff --git a/murk.el b/murk.el
index 2b21449..373b7c1 100644 (file)
--- a/murk.el
+++ b/murk.el
   "If non-nil, use buffer header to display current host and channel."
   :type '(boolean))
 
   "If non-nil, use buffer header to display current host and channel."
   :type '(boolean))
 
+(defcustom murk-autoreply-table nil
+  "Table of autoreply messages.
+
+Each autoreply is a list of two elements: (matcher reply)
+
+Here matcher is a list:
+
+(network src cmd params ...)
+
+and reply is another list:
+
+ (cmd params ...)
+
+Each entry in the matcher list is a regular expression tested against the
+corresponding values in the incomming message.  Entries can be nil,
+in which case they match anything."
+  :type '(list (list) (list)))
+
 
 ;;; Faces
 ;;
 
 ;;; Faces
 ;;
@@ -583,7 +601,9 @@ The head of this list is always the current context.")
                    (murk-get-context server))))
     (murk-display-string
      context
                    (murk-get-context server))))
     (murk-display-string
      context
-     (concat (murk-context->string context) " *")
+     (propertize
+      (concat (murk-context->string context) " *")
+      'face (murk-get-context-facelist context))
      from " " action-text)))
 
 (defun murk-display-notice (context &rest notices)
      from " " action-text)))
 
 (defun murk-display-notice (context &rest notices)
@@ -723,6 +743,7 @@ The head of this list is always the current context.")
   (if murk-debug
       (murk-display-string nil nil string))
   (let* ((msg (murk-string->msg string)))
   (if murk-debug
       (murk-display-string nil nil string))
   (let* ((msg (murk-string->msg string)))
+    (murk-process-autoreplies server msg)
     (pcase (murk-msg-cmd msg)
       ("PING"
        (murk-send-msg server
     (pcase (murk-msg-cmd msg)
       ("PING"
        (murk-send-msg server
@@ -899,6 +920,38 @@ The head of this list is always the current context.")
        (murk-display-notice (murk-get-context server)
                             (murk-msg->string msg))))))
 
        (murk-display-notice (murk-get-context server)
                             (murk-msg->string msg))))))
 
+
+;;; User-defined responses
+;;
+
+(defun murk--lists-equal (l1 l2)
+    (if (and l1 l2)
+        (if (or (not (and (car l1) (car l2)))
+                (string-match (car l1) (car l2)))
+            (murk--lists-equal (cdr l1) (cdr l2))
+          nil)
+      t))
+
+(defun murk-process-autoreply (server msg autoreply)
+  (let ((matcher (car autoreply))
+        (reply (cadr autoreply)))
+    (let ((target-server (car matcher)))
+      (when (and (or (not target-server)
+                     (and (equal server target-server)))
+                 (murk--lists-equal (cdr matcher)
+                                    (append (list (murk-msg-src msg)
+                                                  (murk-msg-cmd msg))
+                                            (murk-msg-params msg))))
+        (murk-send-msg server
+         (murk-msg nil nil (car reply) (cdr reply)))))))
+
+(defun murk-process-autoreplies (server msg)
+  (mapc
+   (lambda (autoreply)
+     (murk-process-autoreply server msg autoreply))
+   murk-autoreply-table))
+
+
 ;;; Commands
 ;;
 
 ;;; Commands
 ;;
 
@@ -991,7 +1044,7 @@ The head of this list is always the current context.")
 (defun murk-command-quit (params)
   (let ((ctx (murk-current-context)))
     (if (not ctx)
 (defun murk-command-quit (params)
   (let ((ctx (murk-current-context)))
     (if (not ctx)
-        (murk-display-error "No current context")
+        (murk-display-error "No current server")
       (let ((quit-msg (if params (string-join params " ") murk-default-quit-msg)))
         (murk-send-msg
          (murk-context-server ctx)
       (let ((quit-msg (if params (string-join params " ") murk-default-quit-msg)))
         (murk-send-msg
          (murk-context-server ctx)
@@ -1082,7 +1135,7 @@ The head of this list is always the current context.")
             (let ((server (murk-context-server ctx))
                   (nick (car params)))
               (murk-send-msg server
             (let ((server (murk-context-server ctx))
                   (nick (car params)))
               (murk-send-msg server
-                             (lurk-msg nil nil "PRIVMSG"
+                             (murk-msg nil nil "PRIVMSG"
                                        (list nick "\01VERSION\01")))
               (murk-display-notice ctx "CTCP version request sent to "
                                    nick " on " server))
                                        (list nick "\01VERSION\01")))
               (murk-display-notice ctx "CTCP version request sent to "
                                    nick " on " server))
@@ -1263,6 +1316,7 @@ The head of this list is always the current context.")
 ;;; Main start procedure
 ;;
 
 ;;; Main start procedure
 ;;
 
+;;;###autoload
 (defun murk ()
   "Start murk or just switch to the murk buffer if one already exists."
   (interactive)
 (defun murk ()
   "Start murk or just switch to the murk buffer if one already exists."
   (interactive)