You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8 KiB

#! /usr/bin/env slope
; prattle - small realtime chat for localhost
; author:
; utility bot
(define me "jarvis")
(define inp (string-format "/join %v\n" me))
(define l #f)
(define screen-width (car (term-size)))
(define screen-height (car (reverse (term-size))))
(define feed 1)
(define run #t)
(define log-file (path-abs (timestamp->date (timestamp) "~/chatbot-%Y%M%D.txt")))
;;; utils
(define get-fortune
(lambda ()
(define fh (file-open-read "/home/samhunter/projects/fortune.farberisms"))
(define fortunes (read-all-lines fh))
(close fh)
(define fh (file-open-read "/home/samhunter/projects/fortune.epigrams"))
(set! fortunes (list-join fortunes (read-all-lines fh)))
(close fh)
(append (ref fortunes (round (* (rand) (length fortunes)))) "\r\n")))
(define get-whoop
(lambda ()
(define out (string-make-buf))
(subprocess ["whoop" "-a"] out)
(define ret (list->string (read-all-lines out) "\r\n"))
(close out)
(append ret "\r\n")))
(define get-recent
(lambda ()
(define out (string-make-buf))
(subprocess ["recent++" "--since" "1d"] out)
(define ret (list->string (read-all-lines out) "\r\n"))
(close out)
(append ret "\r\n")))
(define writer
(lambda (sock)
(for () (run) ; endless loop
(if (and inp (> (length inp) 0))
((regex-match? "^/.*" inp)
(write (string-format "%v\n" inp) sock))
(write (string-format "%v: %v\n" me inp) sock)))
(set! inp #f))
(sleep 100)))))
(define reader
(lambda (sock)
(for () (run) ; endless loop
(if (open? sock)
(set! l (read-line sock))
(if l
(file-append-to log-file (append l "\n"))
((regex-match? "/recent" l)
(write (get-recent) sock))
((regex-match? "/whoop" l)
(write (get-whoop) sock))
((regex-match? "/fortune" l)
(write (get-fortune) sock))))))))))
(define get-inp
(lambda ()
(for () (run) ; endless loop
(sleep (* 15 60 1000))
(set! inp (timestamp->date (timestamp) "%Y.%M.%D %G:%I:%S"))))))
(define safe-exit
(lambda ()
(write (string-format "### %v left the chat\n" me) sock)
(set! run #f)
; don't pay attention to the stupid shit I'm doing to stderr and stdout here
(write (string-format "/leave %v\n" me) sock)
(close sock)
(close stdin)
(close stdout)
(close stderr)
(define sock (net-conn "localhost" "1168"))
(signal-catch-sigint safe-exit)
[ get-inp ]
[ reader sock ]
[ writer sock ]