1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright © 2015, 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of 8sync.
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU Lesser General Public License for more details.
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (8sync systems irc)
20 #:use-module (8sync repl)
21 #:use-module (8sync actors)
22 #:use-module (srfi srfi-9)
23 #:use-module (ice-9 getopt-long)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 receive)
26 #:use-module (ice-9 rdelim)
27 #:use-module (ice-9 q)
28 #:use-module (ice-9 match)
29 #:use-module (oop goops)
31 irc-bot-username irc-bot-server irc-bot-channels irc-bot-port
35 handle-line handle-misc-input
36 handle-user-join handle-user-quit
44 (define default-irc-port 6665)
46 (define* (irc-socket-setup hostname #:optional (inet-port default-irc-port))
47 (let* ((s (socket PF_INET SOCK_STREAM 0))
48 (flags (fcntl s F_GETFL))
49 (ip-address (inet-ntop AF_INET (car (hostent:addr-list (gethost hostname))))))
50 (fcntl s F_SETFL (logior O_NONBLOCK flags))
52 (inet-pton AF_INET ip-address)
56 (define irc-eol "\r\n")
58 (define (startswith-colon? str)
59 (and (> (string-length str) 0)
60 (eq? (string-ref str 0)
63 ;; TODO: This needs a cleanup. Maybe even just using a regex is fine.
64 (define (parse-line line)
65 (define (parse-params pre-params)
66 ;; This is stupid and imperative but I can't wrap my brain around
67 ;; the right way to do it in a functional way :\
68 (let ((param-list '())
69 (currently-building '()))
73 ((startswith-colon? param-item)
74 (if (not (eq? currently-building '()))
77 (reverse currently-building)
79 (set! currently-building (list param-item)))
81 (set! currently-building (cons param-item currently-building)))))
83 ;; We're still building something, so tack that on there
84 (if (not (eq? currently-building '()))
86 (cons (reverse currently-building) param-list)))
87 ;; return the reverse of the param list
88 (reverse param-list)))
90 (match (string-split line #\space)
91 (((? startswith-colon? prefix)
94 (values prefix command
95 (parse-params pre-params)))
96 ((command pre-params ...)
98 (parse-params pre-params)))))
100 (define (strip-colon-if-necessary string)
101 (if (and (> (string-length string) 0)
102 (string-ref string 0))
103 (substring/copy string 1)
106 ;; @@: Not sure if this works in all cases, like what about in a non-privmsg one?
107 (define (irc-line-username irc-line-prefix)
108 (let* ((prefix-name (strip-colon-if-necessary irc-line-prefix))
109 (exclaim-index (string-index prefix-name #\!)))
111 (substring/copy prefix-name 0 exclaim-index)
114 (define (condense-privmsg-line line)
115 "Condense message line and do multiple value return of
116 (channel message emote?)"
117 (define (strip-last-char string)
118 (substring/copy string 0 (- (string-length string) 1)))
119 (let* ((channel-name (caar line))
120 (rest-params (apply append (cdr line))))
122 (((or "\x01ACTION" ":\x01ACTION") middle-words ... (= strip-last-char last-word))
125 (append middle-words (list last-word))
128 (((= strip-colon-if-necessary first-word) rest-message ...)
130 (string-join (cons first-word rest-message) " ")
134 (define (echo-message irc-bot speaker channel-name
136 "Simply echoes the message to the current-output-port."
138 (format #t "~a emoted ~s in channel ~a\n"
139 speaker line-text channel-name)
140 (format #t "~a said ~s in channel ~a\n"
141 speaker line-text channel-name)))
147 (define-class <irc-bot> (<actor>)
148 (username #:init-keyword #:username
149 #:getter irc-bot-username)
150 (realname #:init-keyword #:realname
152 (server #:init-keyword #:server
153 #:getter irc-bot-server)
154 (channels #:init-keyword #:channels
155 #:getter irc-bot-channels)
156 (port #:init-keyword #:port
157 #:init-value default-irc-port
158 #:getter irc-bot-port)
159 (socket #:accessor irc-bot-socket)
160 (actions #:allocation #:each-subclass
161 #:init-thunk (build-actions
162 (main-loop irc-bot-main-loop)
163 (handle-line handle-line)
164 (send-line irc-bot-send-line-action))))
166 (define (irc-bot-realname irc-bot)
167 (or (slot-ref irc-bot 'realname)
168 (irc-bot-username irc-bot)))
170 (define-method (actor-init! (irc-bot <irc-bot>))
171 "Initialize the IRC bot"
173 (irc-socket-setup (irc-bot-server irc-bot)
174 (irc-bot-port irc-bot)))
176 (set! (irc-bot-socket irc-bot) socket)
177 (format socket "USER ~a ~a ~a :~a~a"
178 (irc-bot-username irc-bot)
179 "*" "*" ; hostname and servername
180 (irc-bot-realname irc-bot) irc-eol)
181 (format socket "NICK ~a~a" (irc-bot-username irc-bot) irc-eol)
185 (format socket "JOIN ~a~a" channel irc-eol))
186 (irc-bot-channels irc-bot))
188 (<- (actor-id irc-bot) 'main-loop))
190 (define-method (actor-cleanup! (irc-bot <irc-bot>))
191 (close (irc-bot-socket irc-bot)))
193 (define (irc-bot-main-loop irc-bot message)
194 (define socket (irc-bot-socket irc-bot))
195 (define line (string-trim-right (read-line socket) #\return))
196 (with-actor-nonblocking-ports
198 (dispatch-raw-line irc-bot line)
200 ;; The port's been closed for some reason, so stop looping
201 ((port-closed? socket)
203 ;; We've reached the EOF object, which means we should close
204 ;; the port ourselves and stop looping
205 ((eof-object? (peek-char socket))
209 (<- (actor-id irc-bot) 'main-loop))))))
211 (define* (irc-bot-send-line-action irc-bot message
212 channel line #:key emote?)
213 "Action handler for sending lines. Real behavior happens in
215 (irc-bot-send-line irc-bot channel line #:emote? emote?))
217 (define* (irc-bot-send-line irc-bot channel line #:key emote?)
218 ;; TODO: emote? handling
219 (format (irc-bot-socket irc-bot) "PRIVMSG ~a :~a~a"
220 channel line irc-eol))
223 ;;; Likely-to-be-overridden generic methods
225 (define-method (dispatch-raw-line (irc-bot <irc-bot>) raw-line)
226 "Dispatch a raw line of input"
227 (receive (line-prefix line-command line-params)
228 (parse-line raw-line)
231 (display (string-append "PONG" irc-eol)
232 (irc-bot-socket irc-bot)))
234 (receive (channel-name line-text emote?)
235 (condense-privmsg-line line-params)
236 (let ((username (irc-line-username line-prefix)))
237 (<- (actor-id irc-bot) 'handle-line
238 username channel-name
240 (_ (handle-misc-input irc-bot raw-line)))))
242 (define-method (handle-line (irc-bot <irc-bot>) message
243 username channel-name line-text emote?)
244 (echo-message irc-bot username channel-name line-text emote?))
246 (define-method (handle-misc-input (irc-bot <irc-bot>) raw-line)
250 (define-method (handle-user-join (irc-bot <irc-bot>) user channel)
253 (define-method (handle-user-quit (irc-bot <irc-bot>) user channel)