5 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
7 ;; This library is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU Lesser General Public
9 ;; License as published by the Free Software Foundation; either
10 ;; version 3 of the License, or (at your option) any later version.
12 ;; This library is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; Lesser General Public License for more details.
17 ;; You should have received a copy of the GNU Lesser General Public
18 ;; License along with this library; if not, write to the Free Software
19 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (eightsync systems irc)
23 #:use-module (eightsync repl)
24 #:use-module (eightsync agenda)
25 #:use-module (srfi srfi-9)
26 #:use-module (ice-9 getopt-long)
27 #:use-module (ice-9 format)
28 #:use-module (ice-9 receive)
29 #:use-module (ice-9 q)
30 #:use-module (ice-9 match)
31 #:export (;; The only things you definitely need if writing a bot
33 irc-format irc-display irc-send-message irc-send-formatted
35 ;; Useful things if you're making something more complicated
44 make-irc-line irc-line?
45 irc-line-prefix irc-line-command irc-line-params
53 make-handle-line make-basic-irc-handler
54 queue-and-start-irc-agenda!))
60 (define default-irc-port 6665)
62 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
63 (let ((s (socket PF_INET SOCK_STREAM 0))
64 (ip-address (inet-ntoa (car (hostent:addr-list (gethost hostname))))))
66 (inet-pton AF_INET ip-address)
70 (define (install-socket socket handler)
71 (display "Installing socket...\n") ; debugging :)
72 (make-port-request socket #:read handler))
74 (define irc-eol "\r\n")
76 (define (irc-line line)
77 (string-concatenate (list line irc-eol)))
79 (define-syntax-rule (irc-format dest format-string rest ...)
80 (let ((line (string-concatenate
81 (list (format #f format-string rest ...)
87 (display line dest)))))
89 (define* (irc-display line #:optional dest)
91 (display (irc-line line) dest)
92 (display (irc-line dest))))
94 (define (irc-send-message socket channel message)
95 (irc-format socket "PRIVMSG ~a :~a" channel message))
97 (define-syntax-rule (irc-send-formatted socket channel format-string
99 (irc-format socket "PRIVMSG ~a :~a" channel
100 (format #f format-string args ...)))
102 (define* (handle-login socket username
108 (irc-format socket "USER ~a ~a ~a :~a"
109 username hostname servername realname)
110 (irc-format socket "NICK ~a" username)
113 (irc-format socket "JOIN ~a" channel))
116 (define (startswith-colon? str)
117 (and (> (string-length str) 0)
118 (eq? (string-ref str 0)
121 (define-record-type <irc-line>
122 (make-irc-line prefix command params)
124 (prefix irc-line-prefix)
125 (command irc-line-command)
126 (params irc-line-params))
129 (define (parse-line line)
130 (define (parse-params pre-params)
131 ;; This is stupid and imperative but I can't wrap my brain around
132 ;; the right way to do it in a functional way :\
133 (let ((param-list '())
134 (currently-building '()))
138 ((startswith-colon? param-item)
139 (if (not (eq? currently-building '()))
142 (reverse currently-building)
144 (set! currently-building (list param-item)))
146 (set! currently-building (cons param-item currently-building)))))
148 ;; We're still building something, so tack that on there
149 (if (not (eq? currently-building '()))
151 (cons (reverse currently-building) param-list)))
152 ;; return the reverse of the param list
153 (reverse param-list)))
155 (match (string-split line #\space)
156 (((? startswith-colon? prefix)
159 (make-irc-line prefix command
160 (parse-params pre-params)))
161 ((command pre-params ...)
162 (make-irc-line #f command
163 (parse-params pre-params)))))
165 (define (strip-colon-if-necessary string)
166 (if (and (> (string-length string) 0)
167 (string-ref string 0))
168 (substring/copy string 1)
171 ;; @@: Not sure if this works in all cases, like what about in a non-privmsg one?
172 (define (irc-line-username irc-line)
173 (let* ((prefix-name (strip-colon-if-necessary (irc-line-prefix irc-line)))
174 (exclaim-index (string-index prefix-name #\!)))
176 (substring/copy prefix-name 0 exclaim-index)
179 (define (condense-privmsg-line line)
180 "Condense message line and do multiple value return of
181 (channel message is-action)"
182 (define (strip-last-char string)
183 (substring/copy string 0 (- (string-length string) 1)))
184 (let* ((channel-name (caar line))
185 (rest-params (apply append (cdr line))))
187 (((or "\x01ACTION" ":\x01ACTION") middle-words ... (= strip-last-char last-word))
190 (append middle-words (list last-word))
193 (((= strip-colon-if-necessary first-word) rest-message ...)
195 (string-join (cons first-word rest-message) " ")
198 (define (echo-back-message socket my-name speaker
199 channel-name message is-action)
201 (format #t "~a emoted ~s in channel ~a\n"
202 speaker message channel-name)
203 (format #t "~a said ~s in channel ~a\n"
204 speaker message channel-name)))
206 (define default-handle-privmsg echo-back-message)
208 (define* (make-handle-line #:key
209 (handle-privmsg default-handle-privmsg))
210 (define (handle-line socket line my-username)
211 (let ((parsed-line (parse-line line)))
212 (match (irc-line-command parsed-line)
214 (irc-display "PONG" socket))
216 (receive (channel-name message is-action)
217 (condense-privmsg-line (irc-line-params parsed-line))
218 (let ((username (irc-line-username parsed-line)))
219 (handle-privmsg socket my-username username
220 channel-name message is-action))))
226 (define (make-basic-irc-handler handle-line username)
228 (define (reset-buffer)
230 (define (should-read-char socket)
231 (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
232 (define (irc-handler socket)
233 (while (should-read-char socket)
234 (set! buffer (cons (read-char socket) buffer))
236 ((#\newline #\return (? char? line-chars) ...)
237 (let ((ready-line (list->string (reverse line-chars))))
241 (%8sync (%run (handle-line
248 (define default-line-handler (make-handle-line))
250 (define* (queue-and-start-irc-agenda! agenda socket #:key
252 (inet-port default-irc-port)
253 (line-handler default-line-handler)
258 (enq! (agenda-queue agenda)
259 (wrap (install-socket
261 (make-basic-irc-handler
264 (enq! (agenda-queue agenda) (wrap (handle-login socket username
265 #:channels channels)))
266 (start-agenda agenda))
268 (display "Cleaning up...\n")
277 `((server (single-char #\s) (required? #t) (value #t))
278 (port (single-char #\p)
282 (if (string->number s) #t #f))))
283 (username (single-char #\u) (required? #t) (value #t))
284 (channels (value #t))
287 (define* (make-irc-bot-cli #:optional
288 (line-handler default-line-handler)
289 (print-and-continue-on-error #t))
291 (let* ((options (getopt-long args option-spec))
292 (hostname (option-ref options 'server #f))
293 (port (or (option-ref options 'port #f)
295 (username (option-ref options 'username #f))
296 (listen (option-ref options 'listen #f))
297 (channels (option-ref options 'channels ""))
298 (agenda (if print-and-continue-on-error
299 (make-agenda #:pre-unwind-handler print-error-and-continue)
301 (display `((server ,hostname) (port ,port) (username ,username)
302 (listen ,listen) (channels-split ,(string-split channels #\space))))
305 (spawn-and-queue-repl-server! agenda))
306 (queue-and-start-irc-agenda!
308 (irc-socket-setup hostname port)
311 #:channels (string-split channels #\space)
312 #:line-handler line-handler)))
315 (define main (make-irc-bot-cli))