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 (handle-line socket line)
85 (define (make-simple-irc-handler handle-line)
87 (define (reset-buffer)
89 (define (should-read-char socket)
90 (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
91 (define (irc-handler socket)
92 (while (should-read-char socket)
93 (set! buffer (cons (read-char socket) buffer))
95 ((#\newline #\return (? char? line-chars) ...)
96 (%sync (%run (handle-line
98 (list->string (reverse line-chars)))))
104 (define* (queue-and-start-irc-agenda! agenda socket #:key
106 (inet-port default-irc-port)
107 (handler (make-simple-irc-handler handle-line))
112 (enq! (agenda-queue agenda) (wrap (install-socket socket handler)))
113 (enq! (agenda-queue agenda) (wrap (handle-login socket username
114 #:channels channels)))
115 (start-agenda agenda))
117 (display "Cleaning up...\n")
126 `((server (single-char #\s) (required? #t) (value #t))
127 (port (single-char #\p)
131 (if (string->number s) #t #f))))
132 (username (single-char #\u) (required? #t) (value #t))
133 (channels (value #t))
137 (let* ((options (getopt-long args option-spec))
138 (hostname (option-ref options 'server #f))
139 (port (or (option-ref options 'port #f)
141 (username (option-ref options 'username #f))
142 (listen (option-ref options 'listen #f))
143 (channels (option-ref options 'channels ""))
144 (agenda (make-agenda)))
145 (display `((server ,hostname) (port ,port) (username ,username)
146 (listen ,listen) (channels-split ,(string-split channels #\space))))
149 (spawn-and-queue-repl-server! agenda))
150 (queue-and-start-irc-agenda!
152 (irc-socket-setup hostname port)
155 #:handler (make-simple-irc-handler handle-line)
156 #:channels (string-split channels #\space))))