From: <dm...@dm...> - 2002-05-05 15:34:21
|
This version: - supports xemacs (tested with 21.4 patch 6), - leaves point at the correct place after your own robot replies are inserted, - has a better 'add-hook' call Still no support for talking to your own robot. (eval-after-load "erc" '(add-hook 'erc-server-PRIVMSG-hook 'dme:erc-robot t)) (if emacs-xemacs (defun dme:replace-regexp-in-string (regexp rep string &optional fixedcase literal subexp start) (replace-in-string string regexp rep literal)) (defalias 'dme:replace-regexp-in-string 'replace-regexp-in-string)) (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 "^$") (dme: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) (dme: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) ("version" (lambda (args) (erc-version))) )) (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 (set-buffer (erc-get-buffer tgt proc)) (let ((inhibit-read-only t) p) (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)) (goto-char (point-max)) (erc-process-input-line (concat reply "\n")))) ; need to return nil to ensure that the string is passed ; to other hook functions nil) nil))) |