2023-05-29 00:21:03 -05:00
|
|
|
#! /bin/sh
|
|
|
|
#|
|
|
|
|
exec csi -s "$0" "$@"
|
|
|
|
|#
|
|
|
|
|
2023-01-08 18:51:36 -06:00
|
|
|
;;
|
2023-02-13 10:35:15 -06:00
|
|
|
;; Copyright 2023, Jaidyn Levesque <jadedctrl@posteo.at>
|
2023-01-08 18:51:36 -06:00
|
|
|
;;
|
|
|
|
;; This program is free software: you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License as
|
|
|
|
;; published by the Free Software Foundation, either version 3 of
|
|
|
|
;; the License, or (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
;;
|
|
|
|
|
|
|
|
(import scheme
|
2023-05-29 11:00:52 -05:00
|
|
|
(chicken file) (chicken file posix) (chicken io) (chicken pathname)
|
2023-05-29 00:21:03 -05:00
|
|
|
(chicken process-context) (chicken process-context posix)
|
|
|
|
(chicken string)
|
|
|
|
srfi-1 srfi-18 srfi-69
|
|
|
|
(prefix chatdir chatdir:) (prefix chatdir-inotify chatdir:)
|
|
|
|
ircc
|
|
|
|
getopt-long)
|
2023-02-13 10:35:15 -06:00
|
|
|
|
|
|
|
|
2023-05-29 00:21:03 -05:00
|
|
|
;; Join an IRC channel
|
|
|
|
(define (make-join-channel-callback connection)
|
|
|
|
(let ([root-dir (hash-table-ref connection 'directory)])
|
|
|
|
(lambda (channel)
|
|
|
|
(irc:write-cmd connection "JOIN" channel))))
|
2023-01-10 22:59:57 -06:00
|
|
|
|
|
|
|
|
2023-05-29 00:21:03 -05:00
|
|
|
;; Leave an IRC channel
|
|
|
|
(define (make-leave-channel-callback connection)
|
|
|
|
(let ([root-dir (hash-table-ref connection 'directory)])
|
|
|
|
(lambda (channel)
|
|
|
|
(irc:write-cmd connection "PART" channel))))
|
2023-02-13 10:35:15 -06:00
|
|
|
|
|
|
|
|
2023-02-13 11:15:38 -06:00
|
|
|
;; Send message to an IRC channel
|
2023-05-29 00:21:03 -05:00
|
|
|
(define (make-send-message-callback connection)
|
|
|
|
(let ([root-dir (hash-table-ref connection 'directory)])
|
|
|
|
(lambda (channel message)
|
|
|
|
(irc:write-cmd connection "PRIVMSG" channel message)
|
|
|
|
(chatdir:channel-message-add! root-dir channel message
|
|
|
|
(hash-table-ref connection 'nick)))))
|
2023-02-13 11:15:38 -06:00
|
|
|
|
|
|
|
|
2023-01-10 22:59:57 -06:00
|
|
|
;; Hook function for irc:loop; handles all IRC commands
|
2023-05-29 00:21:03 -05:00
|
|
|
(define (make-irc-command-callback conn)
|
|
|
|
(let ([root-dir (hash-table-ref conn 'directory)])
|
|
|
|
(lambda (conn cmd params #!optional sender)
|
|
|
|
(cond
|
|
|
|
[(and (string=? cmd "PRIVMSG")
|
|
|
|
(string? sender)
|
|
|
|
(irc:hostmask? sender))
|
|
|
|
(let ([target (if (irc:user-is-self? conn (car params))
|
|
|
|
(irc:hostmask-nick sender)
|
|
|
|
(car params))])
|
|
|
|
(chatdir:channel-message-add! root-dir target
|
|
|
|
(last params) (irc:hostmask-nick sender)))]
|
|
|
|
|
|
|
|
[(or (string=? cmd "NOTICE")
|
|
|
|
(and (string=? cmd "PRIVMSG")
|
|
|
|
(or (string-null? sender) (not (irc:hostmask? sender)))))
|
2023-05-29 00:41:32 -05:00
|
|
|
(chatdir:channel-message-add! root-dir ".server" (last params))]
|
2023-05-29 00:21:03 -05:00
|
|
|
|
|
|
|
[(and (string=? cmd "JOIN") (irc:user-is-self? conn sender))
|
|
|
|
(chatdir:channel-add! root-dir (last params))]
|
|
|
|
|
|
|
|
[(string=? cmd "JOIN")
|
2023-05-29 00:41:32 -05:00
|
|
|
(let ([channel (car params)]
|
|
|
|
[nick (irc:hostmask-nick sender)])
|
|
|
|
(chatdir:channel-user-add! root-dir channel nick)
|
|
|
|
(chatdir:channel-user-toggle-states! root-dir channel nick
|
|
|
|
"online" "offline"))]
|
2023-05-29 00:21:03 -05:00
|
|
|
|
2023-05-29 00:41:32 -05:00
|
|
|
[(string=? cmd "PART")
|
|
|
|
(chatdir:channel-user-toggle-states!
|
|
|
|
root-dir (car params) (irc:hostmask-nick sender)
|
|
|
|
"offline" "online")]))))
|
2023-05-29 00:21:03 -05:00
|
|
|
;; [(string=? cmd "NICK")
|
|
|
|
;; (chatd-json-write conn
|
2023-02-08 10:31:56 -06:00
|
|
|
;; (compose-event-alist conn "user-info" #:user (last params)))])
|
2023-01-08 18:51:36 -06:00
|
|
|
|
|
|
|
|
2023-01-10 22:59:57 -06:00
|
|
|
;; Hook function for irc:loop; handles all IRC errors and replies
|
2023-05-29 00:21:03 -05:00
|
|
|
(define (make-irc-reply-callback conn)
|
|
|
|
(let ([root-dir (hash-table-ref conn 'directory)])
|
|
|
|
(lambda (conn reply params #!optional sender)
|
2023-05-29 11:00:52 -05:00
|
|
|
(let ([channel (second params)])
|
|
|
|
(cond
|
|
|
|
;; If topic set, output to a channel's .topic file
|
|
|
|
[(and (eq? reply RPL_TOPIC)
|
|
|
|
(irc:channel? channel))
|
|
|
|
(chatdir:channel-metadata-set! root-dir channel
|
|
|
|
"topic" (last params))]
|
|
|
|
|
|
|
|
[(and (eq? reply RPL_TOPICWHOTIME)
|
|
|
|
(irc:channel? (second params)))
|
|
|
|
(chatdir:channel-metadata-set! root-dir channel
|
|
|
|
"topic" #f
|
|
|
|
`((user.chat.sender . ,(third params))
|
|
|
|
(user.chat.date . ,(last params))))]
|
|
|
|
|
|
|
|
;; We've got to add users, when they join the room!
|
|
|
|
[(or (and (irc:capability? conn 'userhost-in-names)
|
|
|
|
(eq? reply RPL_ENDOFNAMES))
|
|
|
|
(eq? reply RPL_ENDOFWHO))
|
|
|
|
(map (lambda (nick)
|
|
|
|
(let ([hostmask (irc:user-get conn nick 'hostmask)])
|
|
|
|
(chatdir:channel-user-add! root-dir channel nick)
|
|
|
|
(chatdir:channel-user-toggle-states! root-dir channel nick
|
|
|
|
"online" "offline")))
|
|
|
|
(irc:channel-users conn (second params)))]
|
|
|
|
[(string? (last params))
|
|
|
|
(chatdir:channel-message-add! root-dir ".server" (last params))])))))
|
2023-01-08 18:51:36 -06:00
|
|
|
|
|
|
|
|
|
|
|
(define *help-msg*
|
|
|
|
(string-append
|
2023-05-29 11:17:11 -05:00
|
|
|
"usage: irc-chatd [-hd] [-n nick] [-u user] [-p password] hostname\n\n"
|
2023-01-08 18:51:36 -06:00
|
|
|
"`chatd` is a standard format for chat client-daemons; the goal being that a\n"
|
|
|
|
"chat client should be able to work with any chat protocol (IRC, XMPP, etc)\n"
|
|
|
|
"just by reading and writing to files served by a `chatd` daemon, without\n"
|
|
|
|
"having to worry about the protocol in use.\n\n"
|
|
|
|
"irc-chatd is a `chatd`-compliant IRC client-daemon, that outputs all messages\n"
|
|
|
|
"from the server in parseable format to an output file, and receives input\n"
|
|
|
|
"from a FIFO File.\n".))
|
|
|
|
|
|
|
|
|
|
|
|
(define *opts*
|
|
|
|
'((help
|
|
|
|
"Print a usage message"
|
|
|
|
(single-char #\h))
|
|
|
|
(nickname
|
|
|
|
"Your preferred nickname. Default is your system username."
|
|
|
|
(single-char #\n)
|
|
|
|
(value (required NICK)))
|
|
|
|
(username
|
|
|
|
"Username of the connection. Default is your system username."
|
|
|
|
(single-char #\u)
|
|
|
|
(value (required USERNAME)))
|
|
|
|
(password
|
|
|
|
"The password optionally used in connection."
|
|
|
|
(single-char #\p)
|
|
|
|
(value (required PASSWORD)))
|
|
|
|
(name
|
|
|
|
"Set the realname of your connection."
|
2023-01-15 21:31:01 -06:00
|
|
|
(value (required NAME)))
|
2023-02-08 10:31:56 -06:00
|
|
|
(directory
|
|
|
|
"Root directory for channels and messages. Defaults to CWD."
|
2023-01-15 21:31:01 -06:00
|
|
|
(single-char #\o)
|
2023-05-29 11:17:11 -05:00
|
|
|
(value (required PATH)))
|
|
|
|
(debug
|
|
|
|
(single-char #\d)
|
|
|
|
"Print all messages received from the IRC server.")))
|
2023-01-08 18:51:36 -06:00
|
|
|
|
|
|
|
|
|
|
|
;; Prints cli usage to stderr.
|
|
|
|
(define (help)
|
|
|
|
(write-string *help-msg* #f (open-output-file* fileno/stderr))
|
2023-01-15 21:31:01 -06:00
|
|
|
(write-string (usage *opts*) #f (open-output-file* fileno/stderr))
|
|
|
|
(exit 1))
|
2023-01-08 18:51:36 -06:00
|
|
|
|
|
|
|
|
2023-02-13 10:35:15 -06:00
|
|
|
(define (wait-for-registration connection)
|
|
|
|
(if (not (hash-table-exists? connection 'registered))
|
|
|
|
(begin
|
|
|
|
(thread-sleep! .1)
|
|
|
|
(wait-for-registration connection))
|
|
|
|
#t))
|
|
|
|
|
|
|
|
|
2023-01-08 18:51:36 -06:00
|
|
|
;; The `main` procedure that should be called to run feedsnake-unix for use as script.
|
|
|
|
(define (main)
|
|
|
|
(let* ([args (getopt-long (command-line-arguments) *opts*)]
|
|
|
|
[free-args (alist-ref '@ args)])
|
|
|
|
(if (or (null? free-args) (alist-ref 'help args))
|
2023-01-15 21:31:01 -06:00
|
|
|
(help))
|
|
|
|
|
|
|
|
(let*
|
|
|
|
([username (or (alist-ref 'username args)
|
|
|
|
(current-effective-user-name))]
|
|
|
|
[password (alist-ref 'password args)]
|
|
|
|
[nickname (or (alist-ref 'nickname args)
|
|
|
|
(current-effective-user-name))]
|
|
|
|
[fullname (alist-ref 'name args)]
|
|
|
|
[server (last free-args)]
|
|
|
|
[hostname (first (string-split server ":"))]
|
2023-05-29 00:21:03 -05:00
|
|
|
[port (string->number (or (last (string-split server ":"))
|
|
|
|
"6697"))]
|
|
|
|
[directory (normalize-pathname
|
|
|
|
(string-append (or (alist-ref 'directory args) "./")
|
|
|
|
"/"))]
|
2023-01-15 21:31:01 -06:00
|
|
|
[connection (if server
|
2023-05-29 00:21:03 -05:00
|
|
|
(irc:connect hostname port username nickname password fullname)
|
2023-01-15 21:31:01 -06:00
|
|
|
#f)])
|
|
|
|
|
|
|
|
(unless connection
|
|
|
|
(help))
|
|
|
|
|
2023-02-13 10:35:15 -06:00
|
|
|
(hash-table-set! connection 'directory
|
|
|
|
(normalize-pathname (string-append directory "/")))
|
2023-05-29 00:21:03 -05:00
|
|
|
(create-directory (string-append directory "/.server"))
|
2023-02-13 10:35:15 -06:00
|
|
|
|
|
|
|
;; Kick off the input loop, which monitors channels' .in/ dirs
|
|
|
|
(thread-start!
|
|
|
|
(make-thread
|
|
|
|
(lambda ()
|
2023-05-29 00:21:03 -05:00
|
|
|
(let ([callbacks
|
|
|
|
`((join-channel . ,(make-join-channel-callback connection))
|
|
|
|
(leave-channel . ,(make-leave-channel-callback connection))
|
|
|
|
(send-message . ,(make-send-message-callback connection)))])
|
|
|
|
(thread-sleep! 10)
|
|
|
|
(chatdir:input-loop-init directory callbacks)
|
|
|
|
(chatdir:input-loop directory callbacks)))
|
2023-02-13 10:35:15 -06:00
|
|
|
"Chat input"))
|
2023-01-15 21:31:01 -06:00
|
|
|
|
2023-05-29 00:21:03 -05:00
|
|
|
(print (hash-table-ref connection 'directory))
|
|
|
|
;; Kick off the main loop!
|
2023-01-15 21:31:01 -06:00
|
|
|
(irc:loop connection
|
2023-05-29 00:21:03 -05:00
|
|
|
(make-irc-command-callback connection)
|
2023-05-29 11:17:11 -05:00
|
|
|
(make-irc-reply-callback connection)
|
|
|
|
(alist-ref 'debug args)))))
|
2023-01-08 18:51:36 -06:00
|
|
|
|
|
|
|
|
|
|
|
(main)
|