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 my-username)
82 (match (string-split line #\space)
84 (irc-display "PONG" socket)
85 (display "PONG'ed back ;)\n"))
90 (define (make-simple-irc-handler handle-line username)
92 (define (reset-buffer)
94 (define (should-read-char socket)
95 (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
96 (define (irc-handler socket)
97 (while (should-read-char socket)
98 (set! buffer (cons (read-char socket) buffer))
100 ((#\newline #\return (? char? line-chars) ...)
101 (%sync (%run (handle-line
103 (list->string (reverse line-chars))
110 (define* (queue-and-start-irc-agenda! agenda socket #:key
112 (inet-port default-irc-port)
113 (handler (make-simple-irc-handler
115 (apply handle-line args))
121 (enq! (agenda-queue agenda) (wrap (install-socket socket handler)))
122 (enq! (agenda-queue agenda) (wrap (handle-login socket username
123 #:channels channels)))
124 (start-agenda agenda))
126 (display "Cleaning up...\n")
135 `((server (single-char #\s) (required? #t) (value #t))
136 (port (single-char #\p)
140 (if (string->number s) #t #f))))
141 (username (single-char #\u) (required? #t) (value #t))
142 (channels (value #t))
146 (let* ((options (getopt-long args option-spec))
147 (hostname (option-ref options 'server #f))
148 (port (or (option-ref options 'port #f)
150 (username (option-ref options 'username #f))
151 (listen (option-ref options 'listen #f))
152 (channels (option-ref options 'channels ""))
153 (agenda (make-agenda)))
154 (display `((server ,hostname) (port ,port) (username ,username)
155 (listen ,listen) (channels-split ,(string-split channels #\space))))
158 (spawn-and-queue-repl-server! agenda))
159 (queue-and-start-irc-agenda!
161 (irc-socket-setup hostname port)
164 #:channels (string-split channels #\space))))