From: plugd Date: Sun, 3 Sep 2023 12:52:33 +0000 (+0200) Subject: Improved logging + allowed bot script reload. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=d9a3b8bc5dd3d8223ef070c982f6c80cd307f8e0;p=botbot.git Improved logging + allowed bot script reload. --- diff --git a/botbot.scm b/botbot.scm index eb9c078..456d3a7 100644 --- a/botbot.scm +++ b/botbot.scm @@ -33,16 +33,19 @@ (define bot-channel #f) (define bot-proc-file #f) (define usetls #t) +(define allow-reload #f) (define bot-proc #f) +(define verbosity-level 0) + (define ping-period 60) ;seconds (tcp-read-timeout #f) ;disable read timeout (define (launch-bot) ;; (let-values (((in-port out-port) (tcp-connect host port))) - (set! bot-proc (eval (with-input-from-file bot-proc-file read))) + (load-bot) (let-values (((in-port out-port) (if usetls (ssl-connect* hostname: irc-host port: (or irc-port 6697)) @@ -71,15 +74,33 @@ (write-msg `(#f #f "PING" (,irc-host)) out-port) ; send ping (loop))))) +(define (load-bot) + (set! bot-proc (eval (with-input-from-file bot-proc-file read))) + (print "Loaded bot procedure file.")) + (define (bot-loop in-port out-port) (let ((privmsg (lambda (to . args) - (write-msg (list #f #f "PRIVMSG" (cons to args)) out-port)))) + (let ((msg (list #f #f "PRIVMSG" (cons to args)))) + (write-msg msg out-port) + (when (>= verbosity-level 1) + (display "Responded with msg: ") + (write msg) + (newline)))))) (let loop ((msg (read-msg in-port))) (match (cons (msg-source msg) (cons (msg-command msg) (msg-args msg))) ((_ "PING" token) (write-msg `(#f #f "PONG" (,token)) out-port)) + ((source "PRIVMSG" target "bbreload") + (when (and allow-reload (string=? target bot-nick)) + (print "Reveived reload command from " source) + (load-bot) + (privmsg source "Reloaded bot script."))) ((source "PRIVMSG" target args ...) (when (string=? target bot-nick) + (when (>= verbosity-level 1) + (display "Received msg: ") + (write msg) + (newline)) (bot-proc source args privmsg))) (_ ;; Do nothing @@ -88,15 +109,17 @@ (define (read-msg in-port) (let ((msg (string->msg (read-line in-port)))) - (display "Received message: ") - (write msg) - (newline) + (when (>= verbosity-level 2) + (display "Received message: ") + (write msg) + (newline)) msg)) (define (write-msg msg out-port) (with-output-to-port out-port (lambda () (write-string (conc (msg->string msg) "\r\n")))) - (print "Sent message: " msg)) + (when (>= verbosity-level 2) + (print "Sent message: " msg))) (define msg-regex (irregex '(: @@ -148,6 +171,7 @@ (print "Usage:\n" progname " [-h/--help]\n" progname " [-p/--port PORT] [--notls] [-c/--channnel CHANNEL]\n" + indent-str " [-v/--verbose] [-a/--allow-reload]" indent-str " proc-file host nick"))) (define (main) @@ -175,6 +199,14 @@ (equal? this-arg "--channel")) (set! bot-channel (car rest-args)) (loop (cdr rest-args))) + ((or (equal? this-arg "-v") + (equal? this-arg "--verbose")) + (set! verbosity-level (+ 1 verbosity-level)) + (loop rest-args)) + ((or (equal? this-arg "-a") + (equal? this-arg "--allow-reload")) + (set! allow-reload #t) + (loop rest-args)) (else (print "Unknown argument '" this-arg "'") (print-usage progname)))