diff options
| author | 2025-10-13 02:43:23 +0300 | |
|---|---|---|
| committer | 2025-10-13 02:43:23 +0300 | |
| commit | 5db1d491cecd9b610e9693d10b5015822429d261 (patch) | |
| tree | e1980c2b68d78779fb5244d7673111fc80cd7391 | |
| parent | Remove some commented-out code (diff) | |
| download | ukkoclot-5db1d491cecd9b610e9693d10b5015822429d261.tar.gz ukkoclot-5db1d491cecd9b610e9693d10b5015822429d261.tar.xz ukkoclot-5db1d491cecd9b610e9693d10b5015822429d261.zip | |
Added a SWANK server
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | config.default.lisp | 3 | ||||
| -rw-r--r-- | src/config.lisp | 5 | ||||
| -rw-r--r-- | src/main.lisp | 21 |
4 files changed, 31 insertions, 4 deletions
| @@ -2,6 +2,12 @@ | |||
| 2 | 2 | ||
| 3 | A shitty small telegram bot written in common lisp. | 3 | A shitty small telegram bot written in common lisp. |
| 4 | 4 | ||
| 5 | When running in a debuggy environment, consider | ||
| 6 | ``` common-lisp | ||
| 7 | (setf ukkoclot::*in-prod* nil) | ||
| 8 | (log:config :debug) | ||
| 9 | ``` | ||
| 10 | |||
| 5 | # Licensing | 11 | # Licensing |
| 6 | 12 | ||
| 7 | European Union Public Licence, version 1.2. | 13 | European Union Public Licence, version 1.2. |
diff --git a/config.default.lisp b/config.default.lisp index 39b80c8..ac519ae 100644 --- a/config.default.lisp +++ b/config.default.lisp | |||
| @@ -3,4 +3,5 @@ | |||
| 3 | :bot-token "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi" | 3 | :bot-token "123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi" |
| 4 | :db-path "./data.db" | 4 | :db-path "./data.db" |
| 5 | :dev-group -1001234567890 | 5 | :dev-group -1001234567890 |
| 6 | :owner 12345678) | 6 | :owner 12345678 |
| 7 | :swank-port 4006) | ||
diff --git a/src/config.lisp b/src/config.lisp index 17c5c73..1bac762 100644 --- a/src/config.lisp +++ b/src/config.lisp | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | (:export | 5 | (:export |
| 6 | :config-load :config-merge | 6 | :config-load :config-merge |
| 7 | :config-p | 7 | :config-p |
| 8 | :config-bot-name :config-bot-token :config-db-path :config-dev-group :config-owner)) | 8 | :config-bot-name :config-bot-token :config-db-path :config-dev-group :config-owner :config-swank-port)) |
| 9 | (in-package :ukkoclot/config) | 9 | (in-package :ukkoclot/config) |
| 10 | 10 | ||
| 11 | (defmacro defconfig (&rest slots-and-types) | 11 | (defmacro defconfig (&rest slots-and-types) |
| @@ -18,7 +18,8 @@ | |||
| 18 | :bot-token string | 18 | :bot-token string |
| 19 | :db-path string | 19 | :db-path string |
| 20 | :dev-group integer | 20 | :dev-group integer |
| 21 | :owner integer) | 21 | :owner integer |
| 22 | :swank-port (integer 1 65536)) | ||
| 22 | 23 | ||
| 23 | (defun config-load (filename) | 24 | (defun config-load (filename) |
| 24 | (apply #'make-config (with-open-file (f filename) (read f)))) | 25 | (apply #'make-config (with-open-file (f filename) (read f)))) |
diff --git a/src/main.lisp b/src/main.lisp index 862526b..a113ab0 100644 --- a/src/main.lisp +++ b/src/main.lisp | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | (:use :c2cl :ukkoclot/bot :ukkoclot/config :ukkoclot/inline-bots :ukkoclot/tg) | 5 | (:use :c2cl :ukkoclot/bot :ukkoclot/config :ukkoclot/inline-bots :ukkoclot/tg) |
| 6 | (:import-from :anaphora :acond :awhen :it) | 6 | (:import-from :anaphora :acond :awhen :it) |
| 7 | (:import-from :log) | 7 | (:import-from :log) |
| 8 | (:import-from :swank) | ||
| 8 | (:import-from :ukkoclot/bot :make-bot :bot-power-on :fixup-value) | 9 | (:import-from :ukkoclot/bot :make-bot :bot-power-on :fixup-value) |
| 9 | (:import-from :ukkoclot/db :with-db) | 10 | (:import-from :ukkoclot/db :with-db) |
| 10 | (:import-from :ukkoclot/strings :escape-xml :is-tg-whitespace-str :starts-with :starts-with-ignore-case) | 11 | (:import-from :ukkoclot/strings :escape-xml :is-tg-whitespace-str :starts-with :starts-with-ignore-case) |
| @@ -21,16 +22,34 @@ | |||
| 21 | (error (err) (report-error bot ,evt err))) | 22 | (error (err) (report-error bot ,evt err))) |
| 22 | (progn ,@body))) | 23 | (progn ,@body))) |
| 23 | 24 | ||
| 25 | (defun start-swank (port) | ||
| 26 | (log:info "Starting a SWANK server on port ~A..." port) | ||
| 27 | (swank:create-server :port port :dont-close t) | ||
| 28 | (log:info "SWANK started. You can connect to it by forwarding ports via SSH: `ssh -L~A:127.0.0.1:~A username@server.com'" port port) | ||
| 29 | (log:info "And then afterwards M-x slime-connect giving localhost and ~A" port)) | ||
| 30 | |||
| 31 | (defun stop-swank (port) | ||
| 32 | (log:info "Stopping the SWANK server on port ~A..." port) | ||
| 33 | (swank:stop-server port) | ||
| 34 | (log:info "Done")) | ||
| 35 | |||
| 24 | (defun main () | 36 | (defun main () |
| 25 | (unwind-protect | 37 | (unwind-protect |
| 26 | (let ((config (config-load #P"config.default.lisp"))) | 38 | (let ((config (config-load #P"config.default.lisp"))) |
| 27 | (config-merge config #P"config.lisp") | 39 | (config-merge config #P"config.lisp") |
| 28 | (log:info "Starting up ~A" (config-bot-name config)) | 40 | (log:info "Starting up ~A" (config-bot-name config)) |
| 41 | (main-with-config config)) | ||
| 42 | (log:info "Quitting!"))) | ||
| 43 | |||
| 44 | (defun main-with-config (config) | ||
| 45 | (unwind-protect | ||
| 46 | (progn | ||
| 47 | (start-swank (config-swank-port config)) | ||
| 29 | (with-db (db (config-db-path config)) | 48 | (with-db (db (config-db-path config)) |
| 30 | (let ((bot (make-bot config db))) | 49 | (let ((bot (make-bot config db))) |
| 31 | ;; TODO: Catch fatal errors & report them | 50 | ;; TODO: Catch fatal errors & report them |
| 32 | (wrapped-main bot config)))) | 51 | (wrapped-main bot config)))) |
| 33 | (log:info "We're done!"))) | 52 | (stop-swank (config-swank-port config)))) |
| 34 | 53 | ||
| 35 | (defun wrapped-main (bot config) | 54 | (defun wrapped-main (bot config) |
| 36 | (when *in-prod* | 55 | (when *in-prod* |