demos: irc-bot: Refactor message handling using new handle-message.
[8sync.git] / demos / ircbot.scm
index 32b0d1fd557ad21bfecaccf6e3acf93af3a8b79c..350611138924e7a58254875e67afee5d1e21f768 100755 (executable)
@@ -21,6 +21,7 @@
 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
 
 (use-modules (8sync)
+             (8sync contrib irc)
              (8sync systems irc)
              (8sync repl)
              (oop goops)
 
 (define-class <my-irc-bot> (<irc-bot>))
 
-(define-method (handle-line (irc-bot <my-irc-bot>) message
-                            speaker channel line emote?)
+(define-method (handle-message (irc-bot <my-irc-bot>) message)
   (define my-name (irc-bot-username irc-bot))
   (define (looks-like-me? str)
     (or (equal? str my-name)
         (equal? str (string-concatenate (list my-name ":")))))
-  (match (string-split line #\space)
-    (((? looks-like-me? _) action action-args ...)
-     (match action
-       ;; The classic botsnack!
-       ("botsnack"
-        (<- (actor-id irc-bot) 'send-line channel
-            "Yippie! *does a dance!*"))
-       ;; Return greeting
-       ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!"
-            "hei" "hei." "hei!" "hi" "hi!")
-        (<- (actor-id irc-bot) 'send-line channel
-            (format #f "Oh hi ~a!" speaker)))
 
-       ;; --->  Add yours here <---
+  (match message
+    ((and ($ <irc:message>)
+          (= irc:message-line line)
+          (= irc:message-command 'PRIVMSG)
+          (= irc:message-speaker speaker)
+          (= irc:message-channel channel)
+          (= irc:message-message message)
+          (= irc:message-emote? emote?))
 
-       ;; Default
+     (match (string-split message #\space)
+       (((? looks-like-me? _) action action-args ...)
+        (match action
+          ;; The classic botsnack!
+          ("botsnack"
+           (<- (actor-id irc-bot) 'send-line channel
+               "Yippie! *does a dance!*"))
+          ;; Return greeting
+          ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!"
+               "hei" "hei." "hei!" "hi" "hi!")
+           (<- (actor-id irc-bot) 'send-line channel
+               (format #f "Oh hi ~a!" speaker)))
+
+          ;; --->  Add yours here <---
+
+          ;; Default
+          (_
+           (<- (actor-id irc-bot) 'send-line channel
+               "*stupid puppy look*"))))
+       ;; Otherwise... just spit the output to current-output-port or whatever
        (_
-        (<- (actor-id irc-bot) 'send-line channel
-            "*stupid puppy look*"))))
-    ;; Otherwise... just spit the output to current-output-port or whatever
-    (_
-     (if emote?
-         (format #t "~a emoted ~s in channel ~a\n"
-                 speaker line channel)
-         (format #t "~a said ~s in channel ~a\n"
-                 speaker line channel)))))
+        (if emote?
+            (format #t "~a emoted ~s in channel ~a\n"
+                    speaker message channel)
+            (format #t "~a said ~s in channel ~a\n"
+                    speaker message channel)))))
+    (_ #f)))
 
 
 (define (display-help scriptname)