command line thing starts to do things
[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 q)
26              (ice-9 match))
27
28 \f
29 ;;; Network stuff
30 ;;; =============
31
32 (define default-irc-port 6665)
33
34 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
35   (let ((s (socket PF_INET SOCK_STREAM 0))
36         (ip-address (inet-ntoa (car (hostent:addr-list (gethost hostname))))))
37     (connect s AF_INET
38              (inet-pton AF_INET ip-address)
39              inet-port)
40     s))
41
42 (define (install-socket socket handler)
43   (display "Installing socket...\n")   ; debugging :)
44   (make-port-request socket #:read handler))
45
46 (define (handle-line socket line)
47   (display line)
48   (newline))
49
50 (define (make-simple-irc-handler handle-line)
51   (let ((buffer '()))
52     (define (reset-buffer)
53       (set! buffer '()))
54     (define (should-read-char socket)
55       (and (char-ready? socket) (not (eof-object? (peek-char socket)))))
56     (define (irc-handler socket)
57       (while (should-read-char socket)
58         (set! buffer (cons (read-char socket) buffer))
59         (match buffer
60           ((#\newline #\return (? char? line-chars) ...)
61            (%sync (%run (handle-line
62                          socket
63                          (list->string (reverse line-chars)))))
64            ;; reset buffer
65            (set! buffer '()))
66           (_ #f))))
67     irc-handler))
68
69 (define* (queue-and-start-irc-agenda! agenda socket #:key
70                                       (inet-port default-irc-port)
71                                       (handler (make-simple-irc-handler handle-line)))
72   (dynamic-wind
73     (lambda () #f)
74     (lambda ()
75       (enq! (agenda-queue agenda) (wrap (install-socket socket handler)))
76       (start-agenda agenda))
77     (lambda ()
78       (display "Cleaning up...\n")
79       (close socket))))
80
81
82 \f
83 ;;; CLI
84 ;;; ===
85
86 (define option-spec
87   `((server (single-char #\s) (required? #t) (value #t))
88     (port (single-char #\p)
89           (value #t)
90           (predicate
91            ,(lambda (s)
92               (if (string->number s) #t #f))))
93     (username (single-char #\u) (required? #t) (value #t))
94     (listen)))
95
96 (define (main args)
97   (let* ((options (getopt-long args option-spec))
98          (hostname (option-ref options 'server #f))
99          (port (or (option-ref options 'port #f)
100                    default-irc-port))
101          (username (option-ref options 'username #f))
102          (listen (option-ref options 'listen #f)))
103     (display `((server ,hostname) (port ,port) (username ,username)
104                (listen ,listen)))
105     (newline)
106     (queue-and-start-irc-agenda!
107      (make-agenda)
108      (irc-socket-setup hostname port)
109      #:inet-port port
110      #:handler (make-simple-irc-handler handle-line))))