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)
33 (define default-irc-port 6665)
35 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
36 (let ((s (socket PF_INET SOCK_STREAM 0))
37 (ip-address (inet-ntoa (car (hostent:addr-list (gethost hostname))))))
39 (inet-pton AF_INET ip-address)
43 (define (install-socket socket handler)
44 (display "Installing socket...\n") ; debugging :)
45 (make-port-request socket #:read handler))
47 (define irc-eol "\r\n")
49 (define (irc-line line)
50 (string-concatenate (list line irc-eol)))
52 (define-syntax-rule (irc-format dest format-string rest ...)
53 (let ((line (string-concatenate
54 (list (format #f format-string rest ...)
60 (display line dest)))))
62 (define* (irc-display line #:optional dest)
64 (display (irc-line line) dest)
65 (display (irc-line dest))))
67 (define* (handle-login socket username
73 (irc-format socket "USER ~a ~a ~a :~a"
74 username hostname servername realname)
75 (irc-format socket "NICK ~a" username)
78 (irc-format socket "JOIN ~a" channel))
81 (define (startswith-colon? str)
82 (and (> (string-length str) 0)
83 (eq? (string-ref str 0)
86 (define (parse-line line)
87 (define (parse-params pre-params)
88 ;; This is stupid and imperative but I can't wrap my brain around
89 ;; the right way to do it in a functional way :\
90 (let ((param-list '())
91 (currently-building '()))
95 ((startswith-colon? param-item)
96 (if (not (eq? currently-building '()))
99 (reverse currently-building)
101 (set! currently-building (list param-item)))
103 (set! currently-building (cons param-item currently-building)))))
105 ;; We're still building something, so tack that on there
106 (if (not (eq? currently-building '()))
108 (cons (reverse currently-building) param-list)))
109 ;; return the reverse of the param list
110 (reverse param-list)))
112 (match (string-split line #\space)
113 (((? startswith-colon? prefix)
117 (parse-params2 pre-params)))
118 ((command pre-params ...)
119 (list #f command (parse-params2 pre-params)))))
122 (define (handle-line socket line my-username)
123 (match (string-split line #\space)
125 (irc-display "PONG" socket)
126 (display "PONG'ed back ;)\n"))
131 (define (make-simple-irc-handler handle-line username)
133 (define (reset-buffer)
135 (define (should-read-char socket)
136 (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
137 (define (irc-handler socket)
138 (while (should-read-char socket)
139 (set! buffer (cons (read-char socket) buffer))
141 ((#\newline #\return (? char? line-chars) ...)
142 (%sync (%run (handle-line
144 (list->string (reverse line-chars))
151 (define* (queue-and-start-irc-agenda! agenda socket #:key
153 (inet-port default-irc-port)
154 (handler (make-simple-irc-handler
156 (apply handle-line args))
162 (enq! (agenda-queue agenda) (wrap (install-socket socket handler)))
163 (enq! (agenda-queue agenda) (wrap (handle-login socket username
164 #:channels channels)))
165 (start-agenda agenda))
167 (display "Cleaning up...\n")
176 `((server (single-char #\s) (required? #t) (value #t))
177 (port (single-char #\p)
181 (if (string->number s) #t #f))))
182 (username (single-char #\u) (required? #t) (value #t))
183 (channels (value #t))
187 (let* ((options (getopt-long args option-spec))
188 (hostname (option-ref options 'server #f))
189 (port (or (option-ref options 'port #f)
191 (username (option-ref options 'username #f))
192 (listen (option-ref options 'listen #f))
193 (channels (option-ref options 'channels ""))
194 (agenda (make-agenda)))
195 (display `((server ,hostname) (port ,port) (username ,username)
196 (listen ,listen) (channels-split ,(string-split channels #\space))))
199 (spawn-and-queue-repl-server! agenda))
200 (queue-and-start-irc-agenda!
202 (irc-socket-setup hostname port)
205 #:channels (string-split channels #\space))))