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 (use-modules (eightsync repl)
34 (define default-irc-port 6665)
36 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
37 (let ((s (socket PF_INET SOCK_STREAM 0))
38 (ip-address (inet-ntoa (car (hostent:addr-list (gethost hostname))))))
40 (inet-pton AF_INET ip-address)
44 (define (install-socket socket handler)
45 (display "Installing socket...\n") ; debugging :)
46 (make-port-request socket #:read handler))
48 (define irc-eol "\r\n")
50 (define (irc-line line)
51 (string-concatenate (list line irc-eol)))
53 (define-syntax-rule (irc-format dest format-string rest ...)
54 (let ((line (string-concatenate
55 (list (format #f format-string rest ...)
61 (display line dest)))))
63 (define* (irc-display line #:optional dest)
65 (display (irc-line line) dest)
66 (display (irc-line dest))))
68 (define* (handle-login socket username
74 (irc-format socket "USER ~a ~a ~a :~a"
75 username hostname servername realname)
76 (irc-format socket "NICK ~a" username)
79 (irc-format socket "JOIN ~a" channel))
82 (define (startswith-colon? str)
83 (and (> (string-length str) 0)
84 (eq? (string-ref str 0)
87 (define-record-type <irc-line>
88 (make-irc-line prefix command params)
90 (prefix irc-line-prefix)
91 (command irc-line-command)
92 (params irc-line-params))
95 (define (parse-line line)
96 (define (parse-params pre-params)
97 ;; This is stupid and imperative but I can't wrap my brain around
98 ;; the right way to do it in a functional way :\
99 (let ((param-list '())
100 (currently-building '()))
104 ((startswith-colon? param-item)
105 (if (not (eq? currently-building '()))
108 (reverse currently-building)
110 (set! currently-building (list param-item)))
112 (set! currently-building (cons param-item currently-building)))))
114 ;; We're still building something, so tack that on there
115 (if (not (eq? currently-building '()))
117 (cons (reverse currently-building) param-list)))
118 ;; return the reverse of the param list
119 (reverse param-list)))
121 (match (string-split line #\space)
122 (((? startswith-colon? prefix)
125 (make-irc-line prefix command
126 (parse-params pre-params)))
127 ((command pre-params ...)
128 (make-irc-line #f command
129 (parse-params pre-params)))))
132 (define (handle-line socket line my-username)
133 (let ((parsed-line (parse-line line)))
134 (match (irc-line-command parsed-line)
136 (irc-display "PONG" socket))
138 (display "hey we got a PRIVMSG up in here!\n")
139 (display parsed-line)
147 (define (make-simple-irc-handler handle-line username)
149 (define (reset-buffer)
151 (define (should-read-char socket)
152 (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
153 (define (irc-handler socket)
154 (while (should-read-char socket)
155 (set! buffer (cons (read-char socket) buffer))
157 ((#\newline #\return (? char? line-chars) ...)
158 (%sync (%run (handle-line
160 (list->string (reverse line-chars))
167 (define* (queue-and-start-irc-agenda! agenda socket #:key
169 (inet-port default-irc-port)
170 (handler (make-simple-irc-handler
172 (apply handle-line args))
178 (enq! (agenda-queue agenda) (wrap (install-socket socket handler)))
179 (enq! (agenda-queue agenda) (wrap (handle-login socket username
180 #:channels channels)))
181 (start-agenda agenda))
183 (display "Cleaning up...\n")
192 `((server (single-char #\s) (required? #t) (value #t))
193 (port (single-char #\p)
197 (if (string->number s) #t #f))))
198 (username (single-char #\u) (required? #t) (value #t))
199 (channels (value #t))
203 (let* ((options (getopt-long args option-spec))
204 (hostname (option-ref options 'server #f))
205 (port (or (option-ref options 'port #f)
207 (username (option-ref options 'username #f))
208 (listen (option-ref options 'listen #f))
209 (channels (option-ref options 'channels ""))
210 (agenda (make-agenda)))
211 (display `((server ,hostname) (port ,port) (username ,username)
212 (listen ,listen) (channels-split ,(string-split channels #\space))))
215 (spawn-and-queue-repl-server! agenda))
216 (queue-and-start-irc-agenda!
218 (irc-socket-setup hostname port)
221 #:channels (string-split channels #\space))))