whatsxmpp/utils.lisp
eta 28a6303371 Add function for uploading (but not sending) media, clean up threads
- The new PUT-WHATSAPP-MEDIA-FILE can encrypt and upload a blob of file data to
  WhatsApp, and returns everything we'd need to make a protobuf message out of
  that.
- However, important functionality is still absent: whatscl needs to gain some
  support for sending protobuf messages for file uploads, and we'd also need to
  integrate with some image/video libraries in order to do related processing
  tasks.
  - For example, uploading an image requires producing a JPEG thumbnail for it,
    as well as figuring out its size.
  - Uploading audio would require conversion to Opus.
  - Maybe we could attempt to integrate with ffmpeg or something? This is likely
    to get tricky.
2020-08-06 17:18:09 +01:00

30 lines
1.2 KiB
Common Lisp

(in-package :whatsxmpp)
(defun octets-to-lowercase-hex (buf)
"Formats BUF, a vector of octets, as a lowercase hex string and returns it."
(declare (type (vector (unsigned-byte 8)) buf))
(format nil "~(~{~2,'0X~}~)" (coerce buf 'list)))
(defun sha1-octets (buf)
"Returns the SHA1 of BUF, a vector of octets, in lowercase hex."
(octets-to-lowercase-hex (ironclad:digest-sequence :sha1 buf)))
(defun sha1-hex (str)
"Returns the SHA1 of STR, a string, in lowercase hex."
(sha1-octets (babel:string-to-octets str)))
(defun child-elements (node)
"Returns the child elements (excluding text nodes) of the CXML DOM node NODE."
(remove-if-not #'dom:element-p (dom:child-nodes node)))
(defmacro with-promise-from-thread (() &body forms)
"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)."
(let ((resolve (gensym))
(reject (gensym)))
`(with-promise (,resolve ,reject)
(bt:make-thread
(lambda ()
(handler-case
(,resolve (progn ,@forms))
(error (e) (,reject e))))))))