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/>.
23 (define-module (eightsync systems irc)
24 #:use-module (eightsync repl)
25 #:use-module (eightsync agenda)
26 #:use-module (srfi srfi-9)
27 #:use-module (ice-9 getopt-long)
28 #:use-module (ice-9 format)
29 #:use-module (ice-9 receive)
30 #:use-module (ice-9 q)
31 #:use-module (ice-9 match)
32 #:export (;; The only things you definitely need if writing a bot
34 irc-format irc-display irc-send-message irc-send-formatted
36 ;; Useful things if you're making something more complicated
45 make-irc-line irc-line?
46 irc-line-prefix irc-line-command irc-line-params
54 make-handle-line make-basic-irc-handler
55 queue-and-start-irc-agenda!))
61 (define default-irc-port 6665)
63 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
64 (let ((s (socket PF_INET SOCK_STREAM 0))
65 (ip-address (inet-ntoa (car (hostent:addr-list (gethost hostname))))))
67 (inet-pton AF_INET ip-address)
71 (define (install-socket socket handler)
72 (display "Installing socket...\n") ; debugging :)
73 (make-port-request socket #:read handler))
75 (define irc-eol "\r\n")
77 (define (irc-line line)
78 (string-concatenate (list line irc-eol)))
80 (define-syntax-rule (irc-format dest format-string rest ...)
81 (let ((line (string-concatenate
82 (list (format #f format-string rest ...)
88 (display line dest)))))
90 (define* (irc-display line #:optional dest)
92 (display (irc-line line) dest)
93 (display (irc-line dest))))
95 (define (irc-send-message socket channel message)
96 (irc-format socket "PRIVMSG ~a :~a" channel message))
98 (define-syntax-rule (irc-send-formatted socket channel format-string
100 (irc-format socket "PRIVMSG ~a :~a" channel
101 (format #f format-string args ...)))
103 (define* (handle-login socket username
109 (irc-format socket "USER ~a ~a ~a :~a"
110 username hostname servername realname)
111 (irc-format socket "NICK ~a" username)
114 (irc-format socket "JOIN ~a" channel))
117 (define (startswith-colon? str)
118 (and (> (string-length str) 0)
119 (eq? (string-ref str 0)
122 (define-record-type <irc-line>
123 (make-irc-line prefix command params)
125 (prefix irc-line-prefix)
126 (command irc-line-command)
127 (params irc-line-params))
130 (define (parse-line line)
131 (define (parse-params pre-params)
132 ;; This is stupid and imperative but I can't wrap my brain around
133 ;; the right way to do it in a functional way :\
134 (let ((param-list '())
135 (currently-building '()))
139 ((startswith-colon? param-item)
140 (if (not (eq? currently-building '()))
143 (reverse currently-building)
145 (set! currently-building (list param-item)))
147 (set! currently-building (cons param-item currently-building)))))
149 ;; We're still building something, so tack that on there
150 (if (not (eq? currently-building '()))
152 (cons (reverse currently-building) param-list)))
153 ;; return the reverse of the param list
154 (reverse param-list)))
156 (match (string-split line #\space)
157 (((? startswith-colon? prefix)
160 (make-irc-line prefix command
161 (parse-params pre-params)))
162 ((command pre-params ...)
163 (make-irc-line #f command
164 (parse-params pre-params)))))
166 (define (strip-colon-if-necessary string)
167 (if (and (> (string-length string) 0)
168 (string-ref string 0))
169 (substring/copy string 1)
172 ;; @@: Not sure if this works in all cases, like what about in a non-privmsg one?
173 (define (irc-line-username irc-line)
174 (let* ((prefix-name (strip-colon-if-necessary (irc-line-prefix irc-line)))
175 (exclaim-index (string-index prefix-name #\!)))
177 (substring/copy prefix-name 0 exclaim-index)
180 (define (condense-privmsg-line line)
181 "Condense message line and do multiple value return of
182 (channel message is-action)"
183 (define (strip-last-char string)
184 (substring/copy string 0 (- (string-length string) 1)))
185 (let* ((channel-name (caar line))
186 (rest-params (apply append (cdr line))))
188 (((or "\x01ACTION" ":\x01ACTION") middle-words ... (= strip-last-char last-word))
191 (append middle-words (list last-word))
194 (((= strip-colon-if-necessary first-word) rest-message ...)
196 (string-join (cons first-word rest-message) " ")
199 (define (echo-back-message socket my-name speaker
200 channel-name message is-action)
202 (format #t "~a emoted ~s in channel ~a\n"
203 speaker message channel-name)
204 (format #t "~a said ~s in channel ~a\n"
205 speaker message channel-name)))
207 (define default-handle-privmsg echo-back-message)
209 (define* (make-handle-line #:key
210 (handle-privmsg default-handle-privmsg))
211 (define (handle-line socket line my-username)
212 (let ((parsed-line (parse-line line)))
213 (match (irc-line-command parsed-line)
215 (irc-display "PONG" socket))
217 (receive (channel-name message is-action)
218 (condense-privmsg-line (irc-line-params parsed-line))
219 (let ((username (irc-line-username parsed-line)))
220 (handle-privmsg socket my-username username
221 channel-name message is-action))))
227 (define (make-basic-irc-handler handle-line username)
229 (define (reset-buffer)
231 (define (should-read-char socket)
232 (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
233 (define (irc-handler socket)
234 (while (should-read-char socket)
235 (set! buffer (cons (read-char socket) buffer))
237 ((#\newline #\return (? char? line-chars) ...)
238 (let ((ready-line (list->string (reverse line-chars))))
242 (%8sync (%run (handle-line
249 (define default-line-handler (make-handle-line))
251 (define* (queue-and-start-irc-agenda! agenda socket #:key
253 (inet-port default-irc-port)
254 (line-handler default-line-handler)
259 (enq! (agenda-queue agenda)
260 (wrap (install-socket
262 (make-basic-irc-handler
265 (enq! (agenda-queue agenda) (wrap (handle-login socket username
266 #:channels channels)))
267 (start-agenda agenda))
269 (display "Cleaning up...\n")
278 `((server (single-char #\s) (required? #t) (value #t))
279 (port (single-char #\p)
283 (if (string->number s) #t #f))))
284 (username (single-char #\u) (required? #t) (value #t))
285 (channels (value #t))
288 (define* (make-irc-bot-cli #:optional
289 (line-handler default-line-handler)
290 (print-and-continue-on-error #t))
292 (let* ((options (getopt-long args option-spec))
293 (hostname (option-ref options 'server #f))
294 (port (or (option-ref options 'port #f)
296 (username (option-ref options 'username #f))
297 (listen (option-ref options 'listen #f))
298 (channels (option-ref options 'channels ""))
299 (agenda (if print-and-continue-on-error
300 (make-agenda #:pre-unwind-handler print-error-and-continue)
302 (display `((server ,hostname) (port ,port) (username ,username)
303 (listen ,listen) (channels-split ,(string-split channels #\space))))
306 (spawn-and-queue-repl-server! agenda))
307 (queue-and-start-irc-agenda!
309 (irc-socket-setup hostname port)
312 #:channels (string-split channels #\space)
313 #:line-handler line-handler)))
316 (define main (make-irc-bot-cli))