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.
This commit is contained in:
eta 2021-04-07 01:14:42 +01:00
parent 0c0f2feb70
commit 07e2702ec5
1 changed files with 22 additions and 9 deletions

View File

@ -148,8 +148,9 @@ Commands:
(unless ticks (unless ticks
(setf ticks 0)) (setf ticks 0))
(when (> (incf ticks) #.(/ (* 60 60) 5)) (when (> (incf ticks) #.(/ (* 60 60) 5))
(format *debug-io* "~&periodic presence available for ~A~%" jid) ;; Used to send a presence timer here; currently
(whatscl::send-presence conn :available)))) ;; not used.
(setf ticks 0))))
append (unless conn append (unless conn
(list jid)))) (list jid))))
(num-users (length users-to-reconnect))) (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) (update-session-data jid sessdata)
(admin-msg comp jid status) (admin-msg comp jid status)
(admin-presence 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)))) (format *debug-io* "~&ws-connected: ~A (as ~A)~%" jid wa-jid))))
(defun wa-handle-disconnect (comp conn jid kind) (defun wa-handle-disconnect (comp conn jid kind)
(with-wa-handler-context (comp conn jid) (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 (let ((reason
(case kind (case kind
(:replaced "Connection replaced by other WhatsApp Web session") (: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.)") (admin-msg comp jid "(Disabling automatic reconnections.)")
(remhash jid (component-whatsapps comp)))) (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) (defun wa-handle-error-status-code (comp conn jid err)
(with-wa-handler-context (comp conn jid) (with-wa-handler-context (comp conn jid)
(format *debug-io* "~&error-status-code for ~A: ~A~%" jid err) (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 :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 :contacts conn (lambda (contacts) (wa-handle-contacts comp conn jid contacts)))
(on :chats conn (lambda (chats) (wa-handle-chats comp conn jid chats))) (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 :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) (on :message-ack conn (lambda (&key id ack from to participant &allow-other-keys)
(wa-handle-message-ack comp conn jid (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" :stanza-type "presence"
:id id :to from :from to :id id :to from :from to
:e e)))) :e e))))
((and uid (string-equal type "available")) ;; Only map chat states to WA presence, not actual XMPP presence.
(let ((conn (gethash stripped (component-whatsapps comp)))) ;; Presence wakes up the device; this is bad.
(when conn ;; ((and uid (string-equal type "available"))
(format *debug-io* "~&sending available presence for ~A~%" stripped) ;; (let ((conn (gethash stripped (component-whatsapps comp))))
(whatscl::send-presence conn :available)))) ;; (when conn
;; (format *debug-io* "~&sending available presence for ~A~%" stripped)
;; (whatscl::send-presence conn :available))))
(t nil)))))) (t nil))))))
(defun whatsxmpp-presence-probe-handler (comp &key from to id &allow-other-keys) (defun whatsxmpp-presence-probe-handler (comp &key from to id &allow-other-keys)