diff options
author | Boutade <thegoofist@protonmail.com> | 2019-09-25 15:04:53 -0500 |
---|---|---|
committer | Boutade <thegoofist@protonmail.com> | 2019-09-25 15:04:53 -0500 |
commit | 94f31a5e1ff502fa12fb6a27c1f9b8b0854e3ad4 (patch) | |
tree | ceaed5ae8ae564686859daf26fcf78587a0ec03c | |
parent | e1b239d0b075bd613a7ea6e321b762dd389e0e5e (diff) |
added HARDCOPY slot to CLIENT.
-rw-r--r-- | examples/shell-echo-bot.lisp | 18 | ||||
-rw-r--r-- | granolin.lisp | 25 |
2 files changed, 37 insertions, 6 deletions
diff --git a/examples/shell-echo-bot.lisp b/examples/shell-echo-bot.lisp index f7bca56..6cd9f1b 100644 --- a/examples/shell-echo-bot.lisp +++ b/examples/shell-echo-bot.lisp @@ -1,10 +1,22 @@ (defclass shell-echo-bot (granolin:client granolin::message-log) ()) -(defvar *bot* (make-instance 'shell-echo-bot - :homeserver "https://matrix.hrlo.world" - :output *standard-output*)) +(defvar *bot* + (make-instance 'shell-echo-bot + :hardcopy (merge-pathnames ".shell-echo-bot.conf" + (user-homedir-pathname)) + :homeserver "https://matrix.hrlo.world" + :output *standard-output*)) +;; a script to login if necessary, and then start the bot +(unless (access-token *bot*) + (princ "Log in to the server:") + (terpri) + (granolin:login *bot* + (and (princ "username: ") (read-line)) + (and (princ "password: ") (read-line)))) + +(start *bot*) diff --git a/granolin.lisp b/granolin.lisp index ede3a2e..5c9e144 100644 --- a/granolin.lisp +++ b/granolin.lisp @@ -22,6 +22,11 @@ :initarg :homeserver :initform (error "HOMESERVER is required.") :type string) + (hardcopy + :accessor hardcopy + :initform nil + :type pathname + :documentation "A file path where client state is saved.") (running-p :accessor running-p :initform t) @@ -39,12 +44,19 @@ :accessor next-batch :initform nil :type string - :documentation "Used on sync requests as the value of the SINCE parameter"))) + :documentation "Used on sync requests as the value of the SINCE parameter")) + (:documentation "An instance of CLIENT holds the necessary state for + interacting with a Matrix server. If HARDCOPY is supplied, the + INITIALIZE-INSTANCE :after auxilliary method will attempt to populate the + following slots from a file: HOMESERVER, TIMEOUT, ACCESS-TOKEN, NEXT-BATCH.")) (defun save-client-state (client &key (fname "granolin.conf")) "Save a PLIST of client state to disk. Saves HOMESERVER, TIMEOUT, ACCESS-TOKEN, and NEXT-BATCH values to the file." + (when (hardcopy client) + (setf fname hardcopy)) + (with-open-file (out fname :direction :output) (print (list :homeserver (homeserver client) :timeout (timeout client) @@ -54,6 +66,10 @@ (defun load-client-state (client &optional (fname "granolin.conf")) "Load client state from a PLIST stored in a file." + + (when (hardcopy client) + (setf fname hardcopy)) + (let ((conf (with-open-file (in fname) (read in)))) (setf (homeserver client) (getf conf :homeserver)) (setf (timeout client) (getf conf :timeout)) @@ -66,7 +82,9 @@ "Ensure that the homeserver url is well formed, and makes an attempt to format it if it isnt") (defmethod initialize-instance :after ((client client) &key) - (validate-homserver-url client)) + (validate-homserver-url client) + (when (and (hardcopy client) (probe-file (hardcopy client))) + (load-client-state client))) (defgeneric handle-event (client room event) (:documentation "Implemented on handlers that need to respond to events.") @@ -75,7 +93,8 @@ (defgeneric clean-up (client) (:documentation "To be run before the client crashes or is killed.") (:method ((client client)) - (setf (running-p client) nil))) + (setf (running-p client) nil) + (save-client-state client))) ;;; Dynamic variables bound during response handling. Each such variable should ;;; be bound to a value during any message handler call. |