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