From: Christopher Allan Webber Date: Fri, 6 Jan 2017 21:58:16 +0000 (-0600) Subject: irc: Convert handle-line to a message handler. X-Git-Tag: v0.4.0~12 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=8a163053ad2008ef8be013224a8fd15b96287bdf;p=8sync.git irc: Convert handle-line to a message handler. * 8sync/systems/irc.scm (): Add 'handle-line action handler, which points to handle-line generic. (dispatch-raw-line): Send message to our own actor to call handle-line. (handle-line): Convert to be an action handler. * demos/ircbot.scm: * doc/8sync-new-manual.org: Update implementers of handle-line generic method. --- diff --git a/8sync/systems/irc.scm b/8sync/systems/irc.scm index e6ad361..a3c5815 100755 --- a/8sync/systems/irc.scm +++ b/8sync/systems/irc.scm @@ -167,6 +167,7 @@ (*init* irc-bot-init) (*cleanup* irc-bot-cleanup) (main-loop irc-bot-main-loop) + (handle-line handle-line) (send-line irc-bot-send-line-action)))) (define (irc-bot-realname irc-bot) @@ -244,12 +245,13 @@ irc-bot-send-line." (receive (channel-name line-text emote?) (condense-privmsg-line line-params) (let ((username (irc-line-username line-prefix))) - (handle-line irc-bot username channel-name - line-text emote?)))) + (<- (actor-id irc-bot) 'handle-line + username channel-name + line-text emote?)))) (_ (handle-misc-input irc-bot raw-line))))) -(define-method (handle-line (irc-bot ) username channel-name - line-text emote?) +(define-method (handle-line (irc-bot ) message + username channel-name line-text emote?) (echo-message irc-bot username channel-name line-text emote?)) (define-method (handle-misc-input (irc-bot ) raw-line) diff --git a/demos/ircbot.scm b/demos/ircbot.scm index 6f32d61..22d2a1d 100755 --- a/demos/ircbot.scm +++ b/demos/ircbot.scm @@ -30,8 +30,8 @@ (define-class ()) -(define-method (handle-line (irc-bot ) speaker channel - line emote?) +(define-method (handle-line (irc-bot ) message + speaker channel line emote?) (define my-name (irc-bot-username irc-bot)) (define (looks-like-me? str) (or (equal? str my-name) diff --git a/doc/8sync-new-manual.org b/doc/8sync-new-manual.org index 3c81bc8..2545b16 100644 --- a/doc/8sync-new-manual.org +++ b/doc/8sync-new-manual.org @@ -103,8 +103,8 @@ Now we need to add our bot. Initially, it won't do much. #+BEGIN_SRC scheme (define-class ()) - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) (if emote? (format #t "~a emoted ~s in channel ~a\n" speaker line channel) @@ -117,6 +117,11 @@ This is an 8sync actor. (8sync uses GOOPS to define actors.) We extended the handle-line generic method, so this is the code that will be called whenever the IRC bot "hears" anything. +This method is itself an action handler, hence the second argument +for =message=, which we can ignore for now. +Pleasantly, the message's argument body is passed in as the rest of +the arguments. + For now the code is pretty basic: it just outputs whatever it "hears" from a user in a channel to the current output port. Pretty boring! @@ -174,8 +179,8 @@ Let's get it to echo whatever we say back to us. Change handle-line to this: #+BEGIN_SRC scheme - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) (<- (actor-id irc-bot) 'send-line channel (format #f "Bawwwwk! ~a says: ~a" speaker line))) #+END_SRC @@ -241,8 +246,8 @@ Indeed, we do have such a method, so we /could/ rewrite handle-line like so: #+BEGIN_SRC scheme - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) (irc-bot-send-line irc-bot channel (format #f "Bawwwwk! ~a says: ~a" speaker line))) #+END_SRC @@ -269,8 +274,8 @@ Whee, that looks like fun! To implement it, we're going to pull out Guile's pattern matcher. #+BEGIN_SRC scheme - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) (define my-name (irc-bot-username irc-bot)) (define (looks-like-me? str) (or (equal? str my-name) @@ -306,8 +311,8 @@ If you're getting the sense that we could make this a bit less wordy, you're right: #+BEGIN_SRC scheme - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) (define my-name (irc-bot-username irc-bot)) (define (looks-like-me? str) (or (equal? str my-name) @@ -929,8 +934,8 @@ Edit the respond section to see what channel it's really sending things to: #+BEGIN_SRC scheme - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) ;; [... snip ...] (define (respond respond-line) (<- (actor-id irc-bot) 'send-line (pk 'channel channel) @@ -959,8 +964,8 @@ to looks like our own username that we respond back to the sender. (We can remove the pk now that we know what's going on.) #+BEGIN_SRC scheme - (define-method (handle-line (irc-bot ) speaker channel - line emote?) + (define-method (handle-line (irc-bot ) message + speaker channel line emote?) ;; [... snip ...] (define (respond respond-line) (<- (actor-id irc-bot) 'send-line