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