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 (8sync systems irc)
24 #:use-module (8sync repl)
25 #:use-module (8sync 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 rdelim)
31 #:use-module (ice-9 q)
32 #:use-module (ice-9 match)
33 #:export (;; The only things you definitely need if writing a bot
35 irc-format irc-display irc-send-message irc-send-formatted
37 ;; Useful things if you're making something more complicated
46 make-irc-line irc-line?
47 irc-line-prefix irc-line-command irc-line-params
55 make-handle-line make-basic-irc-handler
56 queue-and-start-irc-agenda!))
62 (define default-irc-port 6665)
64 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
65 (let* ((s (socket PF_INET SOCK_STREAM 0))
66 (flags (fcntl s F_GETFL))
67 (ip-address (inet-ntop AF_INET (car (hostent:addr-list (gethost hostname))))))
68 (fcntl s F_SETFL (logior O_NONBLOCK flags))
70 (inet-pton AF_INET ip-address)
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 (irc-loop socket handle-line username)
228 (define line (string-trim-right (read-line socket) #\return))
229 (handle-line socket line username)
231 ;; The port's been closed for some reason, so stop looping
232 ((port-closed? socket)
234 ;; We've reached the EOF object, which means we should close
235 ;; the port ourselves and stop looping
236 ((eof-object? (peek-char socket))
239 ;; Otherwise, let's read till the next line!
243 (define default-line-handler (make-handle-line))
245 (define* (queue-and-start-irc-agenda! agenda socket #:key
247 (inet-port default-irc-port)
248 (line-handler default-line-handler)
253 (enq! (agenda-queue agenda)
254 (wrap (irc-loop socket line-handler username)))
255 (enq! (agenda-queue agenda) (wrap (handle-login socket username
256 #:channels channels)))
257 (start-agenda agenda))
259 (display "Cleaning up...\n")
268 `((server (single-char #\s) (required? #t) (value #t))
269 (port (single-char #\p)
273 (if (string->number s) #t #f))))
274 (username (single-char #\u) (required? #t) (value #t))
275 (channels (value #t))
278 (define* (make-irc-bot-cli #:optional
279 (line-handler default-line-handler)
280 (print-and-continue-on-error #t))
282 (let* ((options (getopt-long args option-spec))
283 (hostname (option-ref options 'server #f))
284 (port (or (option-ref options 'port #f)
286 (username (option-ref options 'username #f))
287 (listen (option-ref options 'listen #f))
288 (channels (option-ref options 'channels ""))
289 (agenda (if print-and-continue-on-error
290 (make-agenda #:pre-unwind-handler print-error-and-continue)
292 (display `((server ,hostname) (port ,port) (username ,username)
293 (listen ,listen) (channels-split ,(string-split channels #\space))))
296 (spawn-and-queue-repl-server! agenda))
297 (queue-and-start-irc-agenda!
299 (irc-socket-setup hostname port)
302 #:channels (string-split channels #\space)
303 #:line-handler line-handler)))
306 (define main (make-irc-bot-cli))