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