5 ;;; 8sync --- Asynchronous programming for Guile
6 ;;; Copyright (C) 2015 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/>.
28 (define (handle-line irc-bot speaker channel line emote?)
29 (define my-name (irc-bot-username irc-bot))
30 (define (looks-like-me? str)
31 (or (equal? str my-name)
32 (equal? str (string-concatenate (list my-name ":")))))
33 (match (string-split line #\space)
34 (((? looks-like-me? _) action action-args ...)
36 ;; The classic botsnack!
38 (<- irc-bot (actor-id irc-bot) 'send-line channel
39 "Yippie! *does a dance!*"))
41 ((or "hello" "hello!" "hello." "greetings" "greetings." "greetings!"
42 "hei" "hei." "hei!" "hi" "hi!")
43 (<- irc-bot (actor-id irc-bot) 'send-line channel
44 (format #f "Oh hi ~a!" speaker)))
46 ;; ---> Add yours here <---
50 (<- irc-bot (actor-id irc-bot) 'send-line channel
51 "*stupid puppy look*"))))
52 ;; Otherwise... just spit the output to current-output-port or whatever
55 (format #t "~a emoted ~s in channel ~a\n"
57 (format #t "~a said ~s in channel ~a\n"
58 speaker line channel)))))
61 (define (display-help scriptname)
62 (format #t "Usage: ~a [OPTION] username" scriptname)
64 -h, --help display this text
65 --server=SERVER-NAME connect to SERVER-NAME
66 defaults to \"irc.freenode.net\"
67 --channels=CHANNEL1,CHANNEL2
68 join comma-separated list of channels on connect
69 defaults to \"##botchat\"")
72 (define (parse-args scriptname args)
74 (list (option '(#\h "help") #f #f
76 (display-help scriptname)
78 (option '("server") #t #f
79 (lambda (opt name arg result)
80 `(#:server ,arg ,@result)))
81 (option '("channels") #t #f
82 (lambda (opt name arg result)
83 `(#:channels ,(string-split arg #\,)
85 (lambda (opt name arg result)
86 (format #t "Unrecognized option `~a'\n" name)
88 (lambda (option result)
89 `(#:username ,option ,@result))
92 (define* (run-bot #:key (username "examplebot")
93 (server "irc.freenode.net")
94 (channels '("##botchat"))
96 (define hive (make-hive))
98 (hive-create-actor* hive <irc-bot> "irc-bot"
99 #:line-handler handle-line
100 ;; TODO: move these to argument parsing
103 #:channels channels))
105 (ez-run-hive hive (list (bootstrap-message hive irc-bot 'init))))
108 (define parsed-args (parse-args "ircbot.scm" (pk 'args args)))
109 (apply (lambda* (#:key username #:allow-other-keys)
111 (display "Error: username not specified!")
113 (display-help "ircbot.scm")
116 (apply run-bot parsed-args))