From: plugd Date: Thu, 23 May 2024 09:32:38 +0000 (+0200) Subject: Added autoreply X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=2d5b239d6b1b43beec02e7b0b2612772e2d1e316;p=lurk.git Added autoreply --- diff --git a/murk.el b/murk.el index 09bda81..0766b7d 100644 --- a/murk.el +++ b/murk.el @@ -66,6 +66,24 @@ "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 ;; @@ -725,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))) + (murk-process-autoreplies server msg) (pcase (murk-msg-cmd msg) ("PING" (murk-send-msg server @@ -901,6 +920,38 @@ The head of this list is always the current context.") (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 (lurk-msg-src msg) + (lurk-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 ;; @@ -993,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) - (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)