From 07e2702ec5299239e50b221449ffc01e839b92a6 Mon Sep 17 00:00:00 2001 From: eta Date: Wed, 7 Apr 2021 01:14:42 +0100 Subject: [PATCH] Restrict available presence sending to chat states & messages Sending lots of available presence is a battery drain, and also might even get the WA app killed on iOS. (It also screws with your Last Seen, which is probably not what you want.) I somewhat forget why the code wanted to send so many presences in the first place, but, anyway, to mitigate the issues above, let's disable that for now. --- stuff.lisp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/stuff.lisp b/stuff.lisp index 80da03a..9a2c448 100644 --- a/stuff.lisp +++ b/stuff.lisp @@ -148,8 +148,9 @@ Commands: (unless ticks (setf ticks 0)) (when (> (incf ticks) #.(/ (* 60 60) 5)) - (format *debug-io* "~&periodic presence available for ~A~%" jid) - (whatscl::send-presence conn :available)))) + ;; Used to send a presence timer here; currently + ;; not used. + (setf ticks 0)))) append (unless conn (list jid)))) (num-users (length users-to-reconnect))) @@ -243,12 +244,16 @@ WhatsXMPP represents users as u440123456789 and groups as g1234-5678." (update-session-data jid sessdata) (admin-msg comp jid status) (admin-presence comp jid status) - (whatscl::send-presence conn :available) + ;; Don't send available presence here, because it causes WA to wake + ;; up. This is allegedly bad on iOS, for example, and drains the + ;; battery on all devices nevertheless. + ;; + ;; (whatscl::send-presence conn :available) (format *debug-io* "~&ws-connected: ~A (as ~A)~%" jid wa-jid)))) (defun wa-handle-disconnect (comp conn jid kind) (with-wa-handler-context (comp conn jid) - (format *debug-io* "~&disconnect for ~A: ~A" jid kind) + (format *debug-io* "~&disconnect for ~A: ~A~%" jid kind) (let ((reason (case kind (:replaced "Connection replaced by other WhatsApp Web session") @@ -258,6 +263,11 @@ WhatsXMPP represents users as u440123456789 and groups as g1234-5678." (admin-msg comp jid "(Disabling automatic reconnections.)") (remhash jid (component-whatsapps comp)))) +(defun wa-handle-stream-asleep (comp conn jid) + (with-wa-handler-context (comp conn jid) + (format *debug-io* "~&stream asleep for ~A~%" jid) + (admin-presence comp jid "Phone sleeping" "away"))) + (defun wa-handle-error-status-code (comp conn jid err) (with-wa-handler-context (comp conn jid) (format *debug-io* "~&error-status-code for ~A: ~A~%" jid err) @@ -848,6 +858,7 @@ Returns three values: avatar data (as two values), and a generalized boolean spe (on :message conn (lambda (msg dt) (wa-handle-message comp conn jid msg dt))) (on :contacts conn (lambda (contacts) (wa-handle-contacts comp conn jid contacts))) (on :chats conn (lambda (chats) (wa-handle-chats comp conn jid chats))) +;; (on :stream-asleep conn (lambda () (wa-handle-stream-asleep comp conn jid))) (on :contact conn (lambda (contact) (wa-handle-contact comp conn jid contact))) (on :message-ack conn (lambda (&key id ack from to participant &allow-other-keys) (wa-handle-message-ack comp conn jid @@ -1230,11 +1241,13 @@ Returns three values: avatar data (as two values), and a generalized boolean spe :stanza-type "presence" :id id :to from :from to :e e)))) - ((and uid (string-equal type "available")) - (let ((conn (gethash stripped (component-whatsapps comp)))) - (when conn - (format *debug-io* "~&sending available presence for ~A~%" stripped) - (whatscl::send-presence conn :available)))) + ;; Only map chat states to WA presence, not actual XMPP presence. + ;; Presence wakes up the device; this is bad. +;; ((and uid (string-equal type "available")) +;; (let ((conn (gethash stripped (component-whatsapps comp)))) +;; (when conn +;; (format *debug-io* "~&sending available presence for ~A~%" stripped) +;; (whatscl::send-presence conn :available)))) (t nil)))))) (defun whatsxmpp-presence-probe-handler (comp &key from to id &allow-other-keys)