diff options
Diffstat (limited to 'src/state.lisp')
| -rw-r--r-- | src/state.lisp | 99 |
1 files changed, 82 insertions, 17 deletions
diff --git a/src/state.lisp b/src/state.lisp index 6348ee3..ef4050d 100644 --- a/src/state.lisp +++ b/src/state.lisp | |||
| @@ -2,30 +2,95 @@ | |||
| 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> | 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> |
| 3 | (defpackage :ukkoclot/src/state | 3 | (defpackage :ukkoclot/src/state |
| 4 | (:documentation "Holds the global state") | 4 | (:documentation "Holds the global state") |
| 5 | (:use :c2cl) | 5 | (:nicknames :state) |
| 6 | (:use :c2cl :ukkoclot/src/rw-lock) | ||
| 6 | (:import-from :com.dieggsy.f-string :enable-f-strings) | 7 | (:import-from :com.dieggsy.f-string :enable-f-strings) |
| 7 | (:import-from :conf :bot-token) | 8 | (:import-from :conf :*config* :bot-token) |
| 9 | (:import-from :sqlite :sqlite-handle) | ||
| 8 | (:export | 10 | (:export |
| 9 | #:bot | 11 | #:*state* |
| 10 | #:make-bot | 12 | #:state |
| 11 | #:bot-p | 13 | #:make-state |
| 12 | #:copy-bot | 14 | #:state-p |
| 13 | #:bot-db | 15 | #:db |
| 14 | #:bot-base-uri | 16 | #:base-uri |
| 15 | #:bot-power-on | 17 | #:power-on |
| 16 | #:bot-username% | 18 | #:set-power-on |
| 17 | #:bot-id%)) | 19 | #:username% |
| 20 | #:set-username% | ||
| 21 | #:id% | ||
| 22 | #:set-id%)) | ||
| 18 | (in-package :ukkoclot/src/state) | 23 | (in-package :ukkoclot/src/state) |
| 19 | 24 | ||
| 20 | (enable-f-strings) | 25 | (enable-f-strings) |
| 21 | 26 | ||
| 22 | (defstruct (bot (:constructor make-bot%)) | 27 | (defstruct (state (:constructor make-state%)) |
| 23 | (db (error "No value given for DB") :read-only t) | 28 | (lock (make-rw-lock :name "state's lock") :type rw-lock :read-only t) |
| 24 | (base-uri (error "No value given for base-uri") :read-only t) | 29 | (db (error "No value given for DB") :type sqlite-handle :read-only t) |
| 30 | (base-uri (error "No value given for base-uri") :type string :read-only t) | ||
| 25 | (power-on t :type boolean) | 31 | (power-on t :type boolean) |
| 26 | (username% nil :type (or string null)) | 32 | (username% nil :type (or string null)) |
| 27 | (id% nil :type (or integer null))) | 33 | (id% nil :type (or integer null))) |
| 28 | 34 | ||
| 29 | (defun make-bot (db) | 35 | (defun make-state (db &optional (config *config*)) |
| 30 | (let ((base-uri #f"https://api.telegram.org/bot{(bot-token)}/")) | 36 | (check-type db sqlite-handle) |
| 31 | (make-bot% :db db :base-uri base-uri))) | 37 | (let ((base-uri #f"https://api.telegram.org/bot{(bot-token config)}/")) |
| 38 | (make-state% :db db :base-uri base-uri))) | ||
| 39 | |||
| 40 | (defvar *state* nil | ||
| 41 | "Bot's general state. You should initialise this with a value before doing anything fun.") | ||
| 42 | |||
| 43 | (defun db (&optional (state *state*)) | ||
| 44 | "Get the database handle of the bot." | ||
| 45 | (with-slots (lock db) state | ||
| 46 | (with-read-lock (lock) | ||
| 47 | db))) | ||
| 48 | |||
| 49 | (defun base-uri (&optional (state *state*)) | ||
| 50 | "Get the base URI of the bot." | ||
| 51 | (with-slots (lock base-uri) state | ||
| 52 | (with-read-lock (lock) | ||
| 53 | base-uri))) | ||
| 54 | |||
| 55 | (defun power-on (&optional (state *state*)) | ||
| 56 | "Get whether the bot is running" | ||
| 57 | (with-slots (lock power-on) state | ||
| 58 | (with-read-lock (lock) | ||
| 59 | power-on))) | ||
| 60 | |||
| 61 | (defun set-power-on (new-value &optional (state *state*)) | ||
| 62 | "Set the value of the power-on" | ||
| 63 | (with-slots (lock power-on) state | ||
| 64 | (with-write-lock (lock) | ||
| 65 | (setf power-on new-value)))) | ||
| 66 | |||
| 67 | (defsetf power-on (&optional (state '*state*)) (new-value) | ||
| 68 | `(set-power-on ,new-value ,state)) | ||
| 69 | |||
| 70 | (defun username% (&optional (state *state*)) | ||
| 71 | "Get the cached bot's username, you should probably use `ukkoclot/src/tg:bot-username' instead." | ||
| 72 | (with-slots (lock username%) state | ||
| 73 | (with-read-lock (lock) | ||
| 74 | username%))) | ||
| 75 | |||
| 76 | (defun set-username% (new-value &optional (state *state*)) | ||
| 77 | (with-slots (lock username%) state | ||
| 78 | (with-write-lock (lock) | ||
| 79 | (setf username% new-value)))) | ||
| 80 | |||
| 81 | (defsetf username% (&optional (state '*state*)) (new-value) | ||
| 82 | `(set-username% ,new-value ,state)) | ||
| 83 | |||
| 84 | (defun id% (&optional (state *state*)) | ||
| 85 | "Get the cached bot's ID, you should probably use `ukkoclot/src/tg:bot-id' instead." | ||
| 86 | (with-slots (lock id%) state | ||
| 87 | (with-read-lock (lock) | ||
| 88 | id%))) | ||
| 89 | |||
| 90 | (defun set-id% (new-value &optional (state *state*)) | ||
| 91 | (with-slots (lock id%) state | ||
| 92 | (with-write-lock (lock) | ||
| 93 | (setf id% new-value)))) | ||
| 94 | |||
| 95 | (defsetf id% (&optional (state '*state*)) (new-value) | ||
| 96 | `(set-id% ,new-value ,state)) | ||