From 797bc62a9b31ab7d063ced4d3285fde1c50fea05 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sat, 18 Oct 2025 07:16:59 +0300 Subject: Add com.dieggsy.f-string, replace simple formats with #f"strings" --- src/inline-bots.lisp | 35 ++++++++++++++++++++++------------- src/main.lisp | 19 ++++++++++++------- src/tg/method-macros.lisp | 9 ++++++--- src/tg/type-macros.lisp | 6 +++++- 4 files changed, 45 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/inline-bots.lisp b/src/inline-bots.lisp index 94ed478..6001cb2 100644 --- a/src/inline-bots.lisp +++ b/src/inline-bots.lisp @@ -1,7 +1,9 @@ ;; SPDX-License-Identifier: EUPL-1.2 ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/inline-bots + (:documentation "This package deals with removing unwanted inline bot usage") (:use :c2cl :ukkoclot/config :ukkoclot/tg) + (:import-from :com.dieggsy.f-string :enable-f-strings) (:import-from :log) (:import-from :ukkoclot/tg :send-message :try-delete-message) (:import-from :ukkoclot/state :bot-config :bot-db) @@ -9,10 +11,18 @@ (:export :blacklist-inline-bot :on-inline-bot :whitelist-inline-bot)) (in-package :ukkoclot/inline-bots) +(enable-f-strings) + (defun blacklist-inline-bot (bot inline-bot-id) + "Blacklist the given bot. + +No more messages about deleting its messages will be sent." (db:set-inline-bot-type (bot-db bot) inline-bot-id :blacklisted)) (defun whitelist-inline-bot (bot inline-bot-id) + "Whitelist the given bot. + +Its messages will no longer be deleted." (db:set-inline-bot-type (bot-db bot) inline-bot-id :whitelisted)) (defun on-inline-bot (bot msg via) @@ -26,17 +36,16 @@ (unless (eql ty :blacklisted) ;; Not explicitly blacklisted, notify dev group (let ((whitelist (make-inline-keyboard-button :text "Whitelist" - :callback-data (format nil "bwl:~A" (user-id via)))) + :callback-data #f"bwl:{(user-id via)}")) (blacklist (make-inline-keyboard-button :text "Blacklist" - :callback-data (format nil "bbl:~A" (user-id via))))) - (send-message bot - :chat-id (config-dev-group (bot-config bot)) - :text (format nil "Deleted a message sent via inline bot @~A ~A" - (user-username via) - (user-id via)) - :parse-mode html - :reply-markup (make-inline-keyboard-markup - :inline-keyboard - (make-array '(1 2) - :initial-contents - (list (list whitelist blacklist))))))))))) + :callback-data #f"bbl:{(user-id via)}"))) + (send-message + bot + :chat-id (config-dev-group (bot-config bot)) + :text #f"Deleted a message sent via inline bot @{(user-username via)} {(user-id via)}" + :parse-mode html + :reply-markup (make-inline-keyboard-markup + :inline-keyboard + (make-array '(1 2) + :initial-contents + (list (list whitelist blacklist))))))))))) diff --git a/src/main.lisp b/src/main.lisp index f9720c9..cd9e755 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -4,6 +4,7 @@ (:nicknames :ukkoclot) (:use :c2cl :ukkoclot/config :ukkoclot/inline-bots :ukkoclot/tg) (:import-from :anaphora :acond :awhen :it) + (:import-from :com.dieggsy.f-string :enable-f-strings) (:import-from :log) (:import-from :ukkoclot/db :with-db) (:import-from :ukkoclot/serializing :fixup-value) @@ -15,6 +16,8 @@ (:export :main)) (in-package :ukkoclot/main) +(enable-f-strings) + (defvar *in-prod* t) (defmacro reporty ((evt) &body body) @@ -240,7 +243,9 @@ ;; TODO: Replace this cond with a nicer dispatch. Something like string-case? (acond ((equal simple-cmd "chatid") - (reply-message bot msg (format nil "~A" (message-chat-id msg)) :parse-mode html)) + (reply-message bot msg + #f"{(message-chat-id msg)}" + :parse-mode html)) ((and (equal simple-cmd "msginfo") (message-reply-to-message msg)) @@ -248,15 +253,14 @@ ((equal simple-cmd "ping") (let* ((start-time (get-internal-real-time)) - (reply (reply-message bot msg "Pong! -Send time: ...")) + (reply (reply-message bot msg #f"Pong!{;~2%}Send time: ...")) (end-time (get-internal-real-time)) (time-elapsed (* (/ (- end-time start-time) internal-time-units-per-second) 1000))) (edit-message-text bot :chat-id (message-chat-id reply) :message-id (message-id reply) - :text (format nil "Pong!~2%Send time: ~Gms" time-elapsed)))) + :text #f"Pong!{;~2%}Send time: {time-elapsed;~G}ms"))) ((and (equal simple-cmd "shutdown") (message-from msg) @@ -264,11 +268,12 @@ Send time: ...")) (setf (bot-power-on bot) nil) (reply-message bot msg "Initialising shutdown..." :allow-sending-without-reply t))))) +(defun escape-xml-obj (obj) + (escape-xml #f"{obj}")) + (defun report-error (bot evt err) (log:error "While handling ~A: ~A" evt err) - (let ((msg (format nil "~A while handling ~&
~A
" - (escape-xml (format nil "~A" err)) - (escape-xml (format nil "~A" evt))))) + (let ((msg #f"{(escape-xml-obj err)} while handling{;~%}
{(escape-xml-obj evt)}
")) (send-message bot :chat-id (config-dev-group (bot-config bot)) :text msg diff --git a/src/tg/method-macros.lisp b/src/tg/method-macros.lisp index e614db9..817a3e3 100644 --- a/src/tg/method-macros.lisp +++ b/src/tg/method-macros.lisp @@ -1,7 +1,9 @@ ;; SPDX-License-Identifier: EUPL-1.2 ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/tg/method-macros + (:documentation "Macros for easy defining TG methods." (:use :c2cl :iterate) + (:import-from :com.dieggsy.f-string :enable-f-strings) (:import-from :serapeum :take) (:import-from :ukkoclot/state :bot) (:import-from :ukkoclot/strings :ends-with :lisp->camel-case) @@ -9,6 +11,8 @@ (:export :define-tg-method)) (in-package :ukkoclot/tg/method-macros) +(enable-f-strings) + (eval-when (:compile-toplevel :load-toplevel :execute) (defstruct (param (:constructor make-param%)) name type default skip-if-default) @@ -19,7 +23,7 @@ &optional (default +unique+) &key (skip-if-default (not (eq default +unique+)))) (let ((default (if (eq default +unique+) - `(error ,(format nil "No value given for ~A" name)) + `(error ,#f"No value given for {name}") default))) (make-param% :name name :type type @@ -67,8 +71,7 @@ ,return-type) ,name)))) -(defmacro define-tg-method ( - (name type &optional (method :POST)) +(defmacro define-tg-method ((name type &optional (method :POST)) &body param-specs) (let ((params (parse-param-specs param-specs))) `(progn diff --git a/src/tg/type-macros.lisp b/src/tg/type-macros.lisp index b9d649c..390781f 100644 --- a/src/tg/type-macros.lisp +++ b/src/tg/type-macros.lisp @@ -1,7 +1,9 @@ ;; SPDX-License-Identifier: EUPL-1.2 ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/tg/type-macros + (:documentation "Macros for easy defining TG types.") (:use :c2cl :iterate) + (:import-from :com.dieggsy.f-string :enable-f-strings) (:import-from :ukkoclot/serializing :parse-value) (:import-from :ukkoclot/hash-tables :gethash-lazy) (:import-from :ukkoclot/strings :lisp->snake-case) @@ -10,6 +12,8 @@ (:export :define-tg-type)) (in-package :ukkoclot/tg/type-macros) +(enable-f-strings) + (eval-when (:compile-toplevel :load-toplevel :execute) (defstruct (field (:constructor make-field%)) name type default skip-if-default) @@ -20,7 +24,7 @@ &optional (default +unique+) &key (skip-if-default (not (eq default +unique+)))) (let ((default (if (eq default +unique+) - `(error ,(format nil "No value given for ~A" name)) + `(error ,#f"No value given for {name}") default))) (make-field% :name name :type type -- cgit v1.2.3