From: <dm...@dm...> - 2002-05-03 19:22:56
|
* dm...@dm... [2002-05-03 15:04:48] > Here's a first pass implementation of robot support for erc. At the > moment the main problem is that the reply from the robot is not > inserted into the local buffer. Looking through erc-send-current-line > made me a bit dizzy, so I'll think about that later. This version correctly echoes the robot output at the local end and also has support for a face to colour it. Getting the output in the correct order means that the order of hook functions in erc-server-PRIVMSG-hook needs to be correct: (setq erc-server-PRIVMSG-hook '(erc-auto-query erc-server-PRIVMSG-or-NOTICE dme:erc-robot)) Here it is: (defun dme:erc-doctor (args) (let* ((dbuf "*doctor*") (docbuf (get-buffer dbuf)) outpoint res) (if (not docbuf) (progn (doctor) (setq docbuf (get-buffer dbuf)) (bury-buffer docbuf))) (save-excursion (set-buffer docbuf) (goto-char (point-max)) (insert args) (goto-char (point-max)) (setq outpoint (point)) (doctor-ret-or-read 1) (doctor-ret-or-read 1) (goto-char outpoint) (re-search-forward "^.") (setq outpoint (- (point) 1)) (re-search-forward "^$") (replace-regexp-in-string "\n" " "(buffer-substring outpoint (point))) ))) (setq dme:erc-robot-commands '( ("cmds" (lambda (args) (concat "commands available: " (mapconcat (lambda (e) (car e)) dme:erc-robot-commands " ")))) ("hello" (lambda (args) "hello to you too !")) ("zippy" (lambda (args) (replace-regexp-in-string "\n" " " (yow)))) ("music" (lambda (args) (concat "now playing: " (let ((track (dme:now-playing))) 2 (if track track "nothing."))))) ("echo" (lambda (args) args)) ("doctor" dme:erc-doctor) )) (defface dme:erc-robot-face '((t (:foreground "yellow"))) "ERC face used for your robot's output." :group 'erc-faces) (defun dme:erc-robot (proc parsed) "Implements a simple robot for erc. Messages to the robot are of the form: \"nick: !command args\", where: nick - the nickname of the user who is the target of the command, command - the specific command, args - arguments to the command (optional)." (let* ((sspec (aref parsed 1)) (nick (nth 0 (erc-parse-user sspec))) (tgt (aref parsed 2)) (msg (aref parsed 3)) (me (erc-current-nick))) (if (and dme:erc-robot-commands (string-match (concat "^" (regexp-quote me) ": !\\([^ ]+\\) ?\\(.*\\)") msg)) ; this is a robot command to me. (let* ((cmd (substring msg (match-beginning 1) (match-end 1))) (args (substring msg (match-beginning 2))) (l (assoc cmd dme:erc-robot-commands)) reply) (setq reply (concat nick ": " (if l (funcall (cadr l) args) (concat "unknown command: " cmd ": try \"cmds\"")))) (erc-log reply) (save-excursion (let (p) (set-buffer (erc-get-buffer tgt proc)) (goto-char (point-max)) (setq p (re-search-backward (erc-prompt))) (insert (erc-format-timestamp) "<" (erc-current-nick) "> ") (when (string-match "[\n]+$" reply) (setq reply (substring reply 0 (match-beginning 0)))) (erc-put-text-property 0 (length reply) 'face 'dme:erc-robot-face reply) (insert reply "\n") (save-excursion (save-match-data (save-restriction (narrow-to-region p (point)) (run-hook-with-args 'erc-send-modify-hook) (run-hook-with-args 'erc-send-post-hook)))) (set-marker (process-mark erc-process) (point)) (set-marker erc-insert-marker (point)) (erc-process-input-line (concat reply "\n")))) ; need to return nil to ensure that the string is passed ; to other hook functions nil) nil))) |