Browse Source

component: help make client code make more DRY when handling messages

- We now pass subclassed components the stripped resource as a keyword, so they
  don't have to bother doing it themselves.
- If a STANZA-ERROR is signaled during message processing, it gets sent back as
  a response to the message. Yay less boilerplate!
master
eta 2 years ago
parent
commit
57f43231a2
  1. 39
      component.lisp

39
component.lisp

@ -295,24 +295,31 @@ @@ -295,24 +295,31 @@
(id (dom:get-attribute stanza "id"))
(children (child-elements stanza))
(body (get-node-named children "body"))
(stripped (strip-resource from))
(marker (get-node-with-xmlns children +chat-markers-ns+))
(chat-state (get-node-with-xmlns children +chat-states-ns+)))
(cond
(body
(let* ((child-nodes (dom:child-nodes body))
(text (if (> (length child-nodes) 0)
(dom:node-value (elt child-nodes 0))
"")))
(emit :text-message comp :from from :to to :body text :id id :stanza stanza)))
(marker
(let ((marker-type (dom:tag-name marker))
(msgid (dom:get-attribute marker "id")))
(emit :message-marker comp :from from :to to :type marker-type :marker-id msgid :id id :stanza stanza)))
(chat-state
(let ((state-type (dom:tag-name chat-state)))
(emit :chat-state comp :from from :to to :type state-type :id id :stanza stanza)))
(t
(emit :message comp :from from :to to :id id :stanza stanza)))))
(handler-case
(cond
(body
(let* ((child-nodes (dom:child-nodes body))
(text (if (> (length child-nodes) 0)
(dom:node-value (elt child-nodes 0))
"")))
(emit :text-message comp :from from :to to :body text :id id :stanza stanza :stripped stripped)))
(marker
(let ((marker-type (dom:tag-name marker))
(msgid (dom:get-attribute marker "id")))
(emit :message-marker comp :from from :to to :type marker-type :marker-id msgid :id id :stanza stanza :stripped stripped)))
(chat-state
(let ((state-type (dom:tag-name chat-state)))
(emit :chat-state comp :from from :to to :type state-type :id id :stanza stanza :stripped stripped)))
(t
(emit :message comp :from from :to to :id id :stanza stanza :stripped stripped)))
(stanza-error (e)
(send-stanza-error comp
:id id :to to :from from
:stanza-type "message"
:e e)))))
(defun handle-connection-complete (comp)
(format *debug-io* "Connection complete! \\o/")

Loading…
Cancel
Save