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