From: <dm...@dm...> - 2002-05-03 14:06:50
|
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. Is anyone interested in this ? I was thinking about adding support for evaluating lisp expressions using the unsafep package... (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))) (if track track "nothing."))))) ("echo" (lambda (args) args)) )) (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 (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) (goto-char (point-max)) (setq reply (concat nick ": " (if l (funcall (cadr l) args) (concat "unknown command: " cmd ": try \"cmds\"")))) (erc-log reply) ; how to insert the reply in my buffer as well ? (save-excursion (set-buffer (erc-get-buffer tgt proc)) (erc-process-input-line reply)) ; need to return nil to ensure that the string is passed ; to other hook functions nil) nil))) (add-hook 'erc-server-PRIVMSG-hook 'dme:erc-robot) |