Skip to content

Commit

Permalink
add setmood and disconnect to client
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Marklund <[email protected]>
  • Loading branch information
trollkarlen committed Sep 22, 2014
1 parent 5476357 commit 8400d15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions hangups/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def header_client(self):
def email(self):
return self._email

@property
def is_connected(self):
return self._is_connected

@asyncio.coroutine
def listen(self):
"""Listen for messages on the channel.
Expand Down
31 changes: 29 additions & 2 deletions hangups/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def __init__(self, cookies):
# Public methods
##########################################################################

def disconnect(self):
"""Disconnect from the server and stop loop."""
if self._channel and self._channel.is_connected:
asyncio.async(self.setpresence(False)).add_done_callback(
lambda res: asyncio.async(self.setactiveclient(False)).add_done_callback(
lambda res: asyncio.get_event_loop().stop()))

@asyncio.coroutine
def connect(self):
"""Connect to the server and receive events."""
Expand Down Expand Up @@ -115,8 +122,8 @@ def _on_pre_connect(self, initial_data):
"""
logger.debug("_on_pre_connect")
asyncio.async(self.setactiveclient(True)).add_done_callback(
lambda res: asyncio.async(self.setpresence(True)).add_done_callback(
asyncio.async(self.setpresence(True)).add_done_callback(
lambda res: asyncio.async(self.setactiveclient(True)).add_done_callback(
lambda res: self.on_connect.fire(initial_data)))

@asyncio.coroutine
Expand Down Expand Up @@ -404,6 +411,26 @@ def setpresence(self, online):
raise exceptions.NetworkError('Unexpected status: {}'
.format(res_status))

@asyncio.coroutine
def setmood(self, mood=None):
"""Set the presence of the client and also the mood
mood is a utf-8 smiley like 0x1f603
"""
res = yield from self._request('presence/setpresence', [
self._get_request_header(),
None,
None,
None,
None,
[mood]
])
res = json.loads(res.body.decode())
res_status = res['response_header']['status']
if res_status != 'OK':
raise exceptions.NetworkError('Unexpected status: {}'
.format(res_status))

@asyncio.coroutine
def setactiveclient(self, online):
"""Set the active client.
Expand Down

0 comments on commit 8400d15

Please sign in to comment.