1 #! /usr/bin/env guile \
5 ;;; 8sync --- Asynchronous programming for Guile
6 ;;; Copyright © 2015, 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
8 ;;; This file is part of 8sync.
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.
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.
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/>.
32 (define-class <my-irc-bot> (<irc-bot>))
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 ":")))))
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?))
49 (match (string-split message #\space)
50 (((? looks-like-me? _) action action-args ...)
52 ;; The classic botsnack!
54 (<- (actor-id irc-bot) 'send-line channel
55 "Yippie! *does a dance!*"))
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)))
62 ;; ---> Add yours here <---
66 (<- (actor-id irc-bot) 'send-line channel
67 "*stupid puppy look*"))))
68 ;; Otherwise... just spit the output to current-output-port or whatever
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)))))
78 (define (display-help scriptname)
79 (format #t "Usage: ~a [OPTION] username" scriptname)
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\"")
89 (define (parse-args scriptname args)
91 (list (option '(#\h "help") #f #f
93 (display-help scriptname)
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 #\,)
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)
108 (lambda (option result)
109 `(#:username ,option ,@result))
112 (define* (run-bot #:key (username "examplebot")
113 (server "irc.freenode.net")
114 (channels '("##botchat"))
116 (define hive (make-hive))
118 (bootstrap-actor* hive <my-irc-bot> "irc-bot"
121 #:channels channels))
125 (bootstrap-actor* hive <repl-manager> "repl"
128 (bootstrap-actor* hive <repl-manager> "repl"))))
134 (define parsed-args (parse-args "ircbot.scm" args))
135 (apply (lambda* (#:key username #:allow-other-keys)
137 (display "Error: username not specified!")
139 (display-help "ircbot.scm")
142 (apply run-bot parsed-args))