demos: actors: Update main functions to support arbitrary arguments.
[8sync.git] / demos / ircbot.scm
1 #!/usr/bin/guile \
2 -e main -s
3 !#
4
5 ;;; 8sync --- Asynchronous programming for Guile
6 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
7 ;;;
8 ;;; This file is part of 8sync.
9 ;;;
10 ;;; 8sync is free software: you can redistribute it and/or modify it
11 ;;; under the terms of the GNU Lesser General Public License as
12 ;;; published by the Free Software Foundation, either version 3 of the
13 ;;; License, or (at your option) any later version.
14 ;;;
15 ;;; 8sync is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;;; GNU Lesser General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU Lesser General Public
21 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
22
23 (use-modules (8sync systems irc)
24              (8sync agenda)
25              (ice-9 match))
26
27 (define (handle-message socket my-name speaker
28                         channel message is-action)
29   (define (looks-like-me? str)
30     (or (equal? str my-name)
31         (equal? str (string-concatenate (list my-name ":")))))
32   (match (string-split message #\space)
33     (((? looks-like-me? _) action action-args ...)
34      (match action
35        ("botsnack"
36         (irc-format socket "PRIVMSG ~a :Yippie! *does a dance!*" channel))
37        ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!"
38             "hei" "hei." "hei!")
39         (irc-format socket "PRIVMSG ~a :Oh hi ~a!" channel speaker))
40        ;; Add yours here
41        (_
42         (irc-format socket "PRIVMSG ~a :*stupid puppy look*" channel))))
43     (_
44      (cond
45       (is-action
46        (format #t "~a emoted ~s in channel ~a\n"
47                speaker message channel))
48       (else
49        (format #t "~a said ~s in channel ~a\n"
50                speaker message channel))))))
51
52 (define main
53   (make-irc-bot-cli (make-handle-line
54                      #:handle-privmsg (wrap-apply handle-message))))
55