Can now start to connect to things via the irc client
[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                        #:optional
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)
82   (display line)
83   (newline))
84
85 (define (make-simple-irc-handler handle-line)
86   (let ((buffer '()))
87     (define (reset-buffer)
88       (set! 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))
94         (match buffer
95           ((#\newline #\return (? char? line-chars) ...)
96            (%sync (%run (handle-line
97                          socket
98                          (list->string (reverse line-chars)))))
99            ;; reset buffer
100            (set! buffer '()))
101           (_ #f))))
102     irc-handler))
103
104 (define* (queue-and-start-irc-agenda! agenda socket #:key
105                                       (username "syncbot")
106                                       (inet-port default-irc-port)
107                                       (handler (make-simple-irc-handler handle-line))
108                                       (channels '()))
109   (dynamic-wind
110     (lambda () #f)
111     (lambda ()
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))
116     (lambda ()
117       (display "Cleaning up...\n")
118       (close socket))))
119
120
121 \f
122 ;;; CLI
123 ;;; ===
124
125 (define option-spec
126   `((server (single-char #\s) (required? #t) (value #t))
127     (port (single-char #\p)
128           (value #t)
129           (predicate
130            ,(lambda (s)
131               (if (string->number s) #t #f))))
132     (username (single-char #\u) (required? #t) (value #t))
133     (listen)))
134
135 (define (main args)
136   (let* ((options (getopt-long args option-spec))
137          (hostname (option-ref options 'server #f))
138          (port (or (option-ref options 'port #f)
139                    default-irc-port))
140          (username (option-ref options 'username #f))
141          (listen (option-ref options 'listen #f)))
142     (display `((server ,hostname) (port ,port) (username ,username)
143                (listen ,listen)))
144     (newline)
145     (queue-and-start-irc-agenda!
146      (make-agenda)
147      (irc-socket-setup hostname port)
148      #:inet-port port
149      #:username username
150      #:handler (make-simple-irc-handler handle-line))))