a5e9ba4426fb6e9f1e5c5a0b0b8d558584a3f561
[8sync.git] / demos / irc.scm
1 #!/usr/bin/guile \
2 -e main -s
3 !#
4
5 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
6
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.
11 ;;
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.
16 ;;
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
20 ;; 02110-1301 USA
21
22 (use-modules (eightsync repl)
23              (eightsync agenda)
24              (ice-9 getopt-long)
25              (ice-9 format)
26              (ice-9 q)
27              (ice-9 match))
28
29 \f
30 ;;; Network stuff
31 ;;; =============
32
33 (define default-irc-port 6665)
34
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))))))
38     (connect s AF_INET
39              (inet-pton AF_INET ip-address)
40              inet-port)
41     s))
42
43 (define (install-socket socket handler)
44   (display "Installing socket...\n")   ; debugging :)
45   (make-port-request socket #:read handler))
46
47 (define irc-eol "\r\n")
48
49 (define (irc-line line)
50   (string-concatenate (list line irc-eol)))
51
52 (define-syntax-rule (irc-format dest format-string rest ...)
53   (let ((line (string-concatenate
54                (list (format #f format-string rest ...)
55                      irc-eol))))
56     (match dest
57       (#f line)
58       (#t (display line))
59       (else
60        (display line dest)))))
61
62 (define* (irc-display line #:optional dest)
63   (if dest
64       (display (irc-line line) dest)
65       (display (irc-line dest))))
66
67 (define* (handle-login socket username
68                        #:key
69                        (hostname "*")
70                        (servername "*")
71                        (realname username)
72                        (channels '()))
73   (irc-format socket "USER ~a ~a ~a :~a"
74               username hostname servername realname)
75   (irc-format socket "NICK ~a" username)
76   (for-each
77    (lambda (channel)
78      (irc-format socket "JOIN ~a" channel))
79    channels))
80
81 (define (startswith-colon? str)
82   (and (> (string-length str) 0)
83        (eq? (string-ref str 0)
84             #\:)))
85
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 '()))
92       (for-each
93        (lambda (param-item)
94          (cond
95           ((startswith-colon? param-item)
96            (if (not (eq? currently-building '()))
97                (set! param-list
98                      (cons
99                       (reverse currently-building)
100                       param-list)))
101            (set! currently-building (list param-item)))
102           (else
103            (set! currently-building (cons param-item currently-building)))))
104        pre-params)
105       ;; We're still building something, so tack that on there
106       (if (not (eq? currently-building '()))
107           (set! param-list
108                 (cons (reverse currently-building) param-list)))
109       ;; return the reverse of the param list
110       (reverse param-list)))
111
112   (match (string-split line #\space)
113     (((? startswith-colon? prefix)
114       command
115       pre-params ...)
116      (list prefix command
117            (parse-params2 pre-params)))
118     ((command pre-params ...)
119      (list #f command (parse-params2 pre-params)))))
120
121
122 (define (handle-line socket line my-username)
123   (match (string-split line #\space)
124     (("PING" rest ...)
125      (irc-display "PONG" socket)
126      (display "PONG'ed back ;)\n"))
127     (_
128      (display line)
129      (newline))))
130
131 (define (make-simple-irc-handler handle-line username)
132   (let ((buffer '()))
133     (define (reset-buffer)
134       (set! 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))
140         (match buffer
141           ((#\newline #\return (? char? line-chars) ...)
142            (%sync (%run (handle-line
143                          socket
144                          (list->string (reverse line-chars))
145                          username)))
146            ;; reset buffer
147            (set! buffer '()))
148           (_ #f))))
149     irc-handler))
150
151 (define* (queue-and-start-irc-agenda! agenda socket #:key
152                                       (username "syncbot")
153                                       (inet-port default-irc-port)
154                                       (handler (make-simple-irc-handler
155                                                 (lambda args
156                                                   (apply handle-line args))
157                                                 username))
158                                       (channels '()))
159   (dynamic-wind
160     (lambda () #f)
161     (lambda ()
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))
166     (lambda ()
167       (display "Cleaning up...\n")
168       (close socket))))
169
170
171 \f
172 ;;; CLI
173 ;;; ===
174
175 (define option-spec
176   `((server (single-char #\s) (required? #t) (value #t))
177     (port (single-char #\p)
178           (value #t)
179           (predicate
180            ,(lambda (s)
181               (if (string->number s) #t #f))))
182     (username (single-char #\u) (required? #t) (value #t))
183     (channels (value #t))
184     (listen)))
185
186 (define (main args)
187   (let* ((options (getopt-long args option-spec))
188          (hostname (option-ref options 'server #f))
189          (port (or (option-ref options 'port #f)
190                    default-irc-port))
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))))
197     (newline)
198     (if listen
199         (spawn-and-queue-repl-server! agenda))
200     (queue-and-start-irc-agenda!
201      agenda
202      (irc-socket-setup hostname port)
203      #:inet-port port
204      #:username username
205      #:channels (string-split channels #\space))))