cbd3848378f8d8adb3748e0abe66b18e50921507
[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 (handle-line socket line my-username)
82   (match (string-split line #\space)
83     (("PING" rest ...)
84      (irc-display "PONG" socket)
85      (display "PONG'ed back ;)\n"))
86     (_
87      (display line)
88      (newline))))
89
90 (define (make-simple-irc-handler handle-line username)
91   (let ((buffer '()))
92     (define (reset-buffer)
93       (set! 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))
99         (match buffer
100           ((#\newline #\return (? char? line-chars) ...)
101            (%sync (%run (handle-line
102                          socket
103                          (list->string (reverse line-chars))
104                          username)))
105            ;; reset buffer
106            (set! buffer '()))
107           (_ #f))))
108     irc-handler))
109
110 (define* (queue-and-start-irc-agenda! agenda socket #:key
111                                       (username "syncbot")
112                                       (inet-port default-irc-port)
113                                       (handler (make-simple-irc-handler
114                                                 (lambda args
115                                                   (apply handle-line args))
116                                                 username))
117                                       (channels '()))
118   (dynamic-wind
119     (lambda () #f)
120     (lambda ()
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))
125     (lambda ()
126       (display "Cleaning up...\n")
127       (close socket))))
128
129
130 \f
131 ;;; CLI
132 ;;; ===
133
134 (define option-spec
135   `((server (single-char #\s) (required? #t) (value #t))
136     (port (single-char #\p)
137           (value #t)
138           (predicate
139            ,(lambda (s)
140               (if (string->number s) #t #f))))
141     (username (single-char #\u) (required? #t) (value #t))
142     (channels (value #t))
143     (listen)))
144
145 (define (main args)
146   (let* ((options (getopt-long args option-spec))
147          (hostname (option-ref options 'server #f))
148          (port (or (option-ref options 'port #f)
149                    default-irc-port))
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))))
156     (newline)
157     (if listen
158         (spawn-and-queue-repl-server! agenda))
159     (queue-and-start-irc-agenda!
160      agenda
161      (irc-socket-setup hostname port)
162      #:inet-port port
163      #:username username
164      #:channels (string-split channels #\space))))