1
0
Fork 0

Allow for disconnecting from WhatsApp with command

This commit introduces a new ad-hoc command, `logout`, which disconnects
from WhatsApp without unlinking our authentication credentials (i.e.
what is generally understood to be meant when saying "log out").
This commit is contained in:
Alex Palaistras 2023-12-03 16:52:18 +00:00
parent 3ca6948639
commit 3713cfe7ad
2 changed files with 33 additions and 21 deletions

View File

@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Optional
from slidge.command import Command, CommandAccess, Form, FormField
from slidge.util import is_valid_phone_number
from slixmpp import JID
from slixmpp.exceptions import XMPPError
from .generated import whatsapp
@ -10,6 +11,37 @@ if TYPE_CHECKING:
from .session import Session
class Logout(Command):
NAME = "Disconnect from WhatsApp"
HELP = (
"Disconnects active WhatsApp session without removing any linked device credentials. "
"To re-connect, use the 're-login' command."
)
NODE = "wa_logout"
CHAT_COMMAND = "logout"
ACCESS = CommandAccess.USER_LOGGED
async def run(
self,
session: Optional["Session"], # type:ignore
ifrom: JID,
*args,
) -> Form:
assert session is not None
try:
msg = session.shutdown()
except Exception as e:
session.send_gateway_status(f"Logout failed: {e}", show="dnd")
raise XMPPError(
"internal-server-error",
etype="wait",
text=f"Could not logout WhatsApp session: {e}",
)
session.send_gateway_message(msg or "Logged out successfully")
session.send_gateway_status(msg or "Logged out", show="away")
return msg
class PairPhone(Command):
NAME = "Complete registration via phone number"
HELP = (
@ -88,24 +120,3 @@ class ChangePresence(Command):
else:
raise ValueError("Not a valid presence kind.", p)
return f"Presence succesfully set to {p}"
class SubscribeToPresences(Command):
NAME = "Subscribe to contacts' presences"
HELP = (
"This command is here for tests about "
"https://todo.sr.ht/~nicoco/slidge-whatsapp/7 ."
)
NODE = "wa_subscribe"
CHAT_COMMAND = "subscribe"
ACCESS = CommandAccess.USER_LOGGED
async def run(
self,
session: Optional["Session"], # type:ignore
ifrom: JID,
*args,
) -> str:
assert session is not None
session.whatsapp.GetContacts(False)
return "Looks like no exception was raised. Success, I guess?"

View File

@ -93,6 +93,7 @@ class Session(BaseSession[str, Recipient]):
will thus allow previously authenticated sessions to re-authenticate without needing to pair.
"""
self.whatsapp.Disconnect()
self.logged = False
@ignore_contact_is_user
async def handle_event(self, event, ptr):