From 53b2725935447b3a57e0c3381155f5a0fbf395f8 Mon Sep 17 00:00:00 2001 From: Boutade Date: Wed, 25 Sep 2019 10:25:22 -0500 Subject: added basic logging example --- examples/shell-echo-bot.lisp | 10 ++++++++++ granolin.asd | 3 ++- granolin.lisp | 27 ++++++++++++++++++++------- package.lisp | 7 ------- utility-apps.lisp | 17 +++++++++++++++++ 5 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 examples/shell-echo-bot.lisp create mode 100644 utility-apps.lisp diff --git a/examples/shell-echo-bot.lisp b/examples/shell-echo-bot.lisp new file mode 100644 index 0000000..f7bca56 --- /dev/null +++ b/examples/shell-echo-bot.lisp @@ -0,0 +1,10 @@ + +(defclass shell-echo-bot (granolin:client granolin::message-log) ()) + +(defvar *bot* (make-instance 'shell-echo-bot + :homeserver "https://matrix.hrlo.world" + :output *standard-output*)) + + + + diff --git a/granolin.asd b/granolin.asd index bdeac36..5e24104 100644 --- a/granolin.asd +++ b/granolin.asd @@ -8,4 +8,5 @@ :serial t :depends-on (#:drakma #:jonathan) :components ((:file "package") - (:file "granolin"))) + (:file "granolin") + (:file "utility-apps"))) diff --git a/granolin.lisp b/granolin.lisp index 74afa7a..f486e43 100644 --- a/granolin.lisp +++ b/granolin.lisp @@ -22,6 +22,9 @@ :initarg :homeserver :initform (error "HOMESERVER is required.") :type string) + (running-p + :accessor running-p + :initform t) (timeout :accessor timeout :initarg :timeout @@ -172,15 +175,15 @@ `(multiple-value-bind (*response-body* *response-status* *response-headers*) - (drakma:http-request (make-matrix-url ,client ,path) + (drakma:http-request (make-matrix-path ,client ,path) :additional-headers (add-auth-header ,client ,headers) :method ,method - :body (jonathan:to-json ,body) + :content (jonathan:to-json ,body) :content-type "application/json") (if (= 200 *response-status*) (let ((*response-object* (,wrap - :data (jonathan:parse *response-body*)))) + :data (jonathan:parse (flexi-streams:octets-to-string *response-body*))))) ,on-ok) ,otherwise))) @@ -199,14 +202,14 @@ run." `(multiple-value-bind (*response-body* *response-status* *response-headers*) - (drakma:http-request (make-matrix-url ,client ,path) + (drakma:http-request (make-matrix-path ,client ,path) :additional-headers (add-auth-header ,client ,headers) :parameters ,params :method :get) (if (= 200 *response-status*) (let ((*response-object* (,wrap - :data (jonathan:parse *response-body*)))) + :data (jonathan:parse (flexi-streams:octets-to-string *response-body*))))) ,on-ok) ,otherwise))) @@ -249,13 +252,13 @@ " (let (params) (push (cons "full_state" full-state) params) - (push (cons "timeout" (timeout client)) params) + (push (cons "timeout" (format nil "~a" (timeout client))) params) (when (next-batch client) (push (cons "since" (next-batch client)) params)) (fetch (client +sync-path+ :params params :wrap make-sync-response) (handle-sync-response client) - (error "Matrix returned ~a from ~a~" + (error "Matrix returned ~a from ~a" *response-status* +sync-path+)))) (defun handle-sync-response (client) @@ -285,3 +288,13 @@ (handle-invitation-event client room-id invite-event))))) +;;; bot loop + +(defun start (client) + (setf (running-p client) t) + (loop :while (running-p client) + :do (sync client))) + +(defun stop (client) + (setf (running-p client) nil)) + diff --git a/package.lisp b/package.lisp index 7872d84..634fc57 100644 --- a/package.lisp +++ b/package.lisp @@ -26,11 +26,4 @@ #:login #:sync - - - - - - - )) diff --git a/utility-apps.lisp b/utility-apps.lisp new file mode 100644 index 0000000..4f4b2db --- /dev/null +++ b/utility-apps.lisp @@ -0,0 +1,17 @@ +(in-package :granolin) + +(defclass message-log () + ((output + :accessor output + :initarg :output + :initform (error "Message Log requires an output stream") + :type stream + :documentation "An output stream to which messages are logged." + ))) + +(defmethod handle-timeline-event :after ((log message-log) room (event timeline-event)) + (format (output log) "~a in ~a: ~a~%" + (sender event) + room + (msg-body event))) + -- cgit v1.2.3