actors: Implicit from-actor argument <-foo methods, and add rest of <-foo*.
[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)
24              (8sync systems irc)
25              (8sync repl)
26              (oop goops)
27              (srfi srfi-37)
28              (ice-9 format)
29              (ice-9 match))
30
31 (define-class <my-irc-bot> (<irc-bot>))
32
33 (define-method (handle-line (irc-bot <my-irc-bot>) speaker channel
34                             line emote?)
35   (define my-name (irc-bot-username irc-bot))
36   (define (looks-like-me? str)
37     (or (equal? str my-name)
38         (equal? str (string-concatenate (list my-name ":")))))
39   (match (string-split line #\space)
40     (((? looks-like-me? _) action action-args ...)
41      (match action
42        ;; The classic botsnack!
43        ("botsnack"
44         (<- (actor-id irc-bot) 'send-line channel
45             "Yippie! *does a dance!*"))
46        ;; Return greeting
47        ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!"
48             "hei" "hei." "hei!" "hi" "hi!")
49         (<- (actor-id irc-bot) 'send-line channel
50             (format #f "Oh hi ~a!" speaker)))
51
52        ;; --->  Add yours here <---
53
54        ;; Default
55        (_
56         (<- (actor-id irc-bot) 'send-line channel
57             "*stupid puppy look*"))))
58     ;; Otherwise... just spit the output to current-output-port or whatever
59     (_
60      (if emote?
61          (format #t "~a emoted ~s in channel ~a\n"
62                  speaker line channel)
63          (format #t "~a said ~s in channel ~a\n"
64                  speaker line channel)))))
65
66
67 (define (display-help scriptname)
68   (format #t "Usage: ~a [OPTION] username" scriptname)
69   (display "
70   -h, --help                  display this text
71       --server=SERVER-NAME    connect to SERVER-NAME
72                                 defaults to \"irc.freenode.net\"
73       --channels=CHANNEL1,CHANNEL2
74                               join comma-separated list of channels on connect
75                                 defaults to \"##botchat\"")
76   (newline))
77
78 (define (parse-args scriptname args)
79   (args-fold (cdr args)
80              (list (option '(#\h "help") #f #f
81                            (lambda _
82                              (display-help scriptname)
83                              (exit 0)))
84                    (option '("server") #t #f
85                            (lambda (opt name arg result)
86                              `(#:server ,arg ,@result)))
87                    (option '("channels") #t #f
88                            (lambda (opt name arg result)
89                              `(#:channels ,(string-split arg #\,)
90                                ,@result)))
91                    (option '("repl") #f #t
92                            (lambda (opt name arg result)
93                              `(#:repl ,(or arg #t) ,@result))))
94              (lambda (opt name arg result)
95                (format #t "Unrecognized option `~a'\n" name)
96                (exit 1))
97              (lambda (option result)
98                `(#:username ,option ,@result))
99              '()))
100
101 (define* (run-bot #:key (username "examplebot")
102                   (server "irc.freenode.net")
103                   (channels '("##botchat"))
104                   (repl #f))
105   (define hive (make-hive))
106   (define irc-bot
107     (hive-create-actor* hive <my-irc-bot> "irc-bot"
108                         #:username username
109                         #:server server
110                         #:channels channels))
111   (define repl-manager
112     (cond
113      ((string? repl)
114       (hive-create-actor* hive <repl-manager> "repl"
115                           #:path repl))
116      (repl
117       (hive-create-actor* hive <repl-manager> "repl"))))
118
119   (define initial-messages
120     (if repl
121         (list (bootstrap-message hive irc-bot 'init)
122               (bootstrap-message hive repl-manager 'init))
123         (list (bootstrap-message hive irc-bot 'init))))
124
125   ;; TODO: load REPL
126   (run-hive hive initial-messages))
127
128 (define (main args)
129   (define parsed-args (parse-args "ircbot.scm" args))
130   (apply (lambda* (#:key username #:allow-other-keys)
131            (when (not username)
132              (display "Error: username not specified!")
133              (newline) (newline)
134              (display-help "ircbot.scm")
135              (exit 1)))
136          parsed-args)
137   (apply run-bot parsed-args))