A WhatsApp (Web) transport for XMPP.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines

  1. (in-package :whatsxmpp)
  2. (defun octets-to-lowercase-hex (buf)
  3. "Formats BUF, a vector of octets, as a lowercase hex string and returns it."
  4. (declare (type (vector (unsigned-byte 8)) buf))
  5. (format nil "~(~{~2,'0X~}~)" (coerce buf 'list)))
  6. (defun sha1-octets (buf)
  7. "Returns the SHA1 of BUF, a vector of octets, in lowercase hex."
  8. (octets-to-lowercase-hex (ironclad:digest-sequence :sha1 buf)))
  9. (defun sha1-hex (str)
  10. "Returns the SHA1 of STR, a string, in lowercase hex."
  11. (sha1-octets (babel:string-to-octets str)))
  12. (defun child-elements (node)
  13. "Returns the child elements (excluding text nodes) of the CXML DOM node NODE."
  14. (remove-if-not #'dom:element-p (dom:child-nodes node)))
  15. (defun nil-empty (seq)
  16. "If SEQ (a sequence) is empty, returns NIL; otherwise, returns SEQ."
  17. (unless (eql (length seq) 0) seq))
  18. (defmacro with-promise-from-thread (() &body forms)
  19. "Return a promise that executes FORMS in a new thread, resolving the promise with the return value of (PROGN ,@FORMS) or rejecting it if an ERROR condition is thrown (with said condition)."
  20. (let ((resolve (gensym))
  21. (reject (gensym)))
  22. `(with-promise (,resolve ,reject)
  23. (bt:make-thread
  24. (lambda ()
  25. (handler-case
  26. (,resolve (progn ,@forms))
  27. (error (e) (,reject e))))))))