2017-02-11 19:23:30 +00:00
|
|
|
# use unicode encoding for all literals by default (for python2.x)
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-02-11 19:06:12 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015-2017, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
|
|
|
|
|
|
|
"""
|
|
|
|
This file is part of transWhat
|
|
|
|
|
|
|
|
transWhat is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
transwhat is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2015-10-26 16:47:17 +00:00
|
|
|
import logging
|
2017-02-20 12:56:34 +00:00
|
|
|
import os
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-08-30 18:08:41 +00:00
|
|
|
from yowsup import env
|
2016-08-10 13:14:46 +00:00
|
|
|
from yowsup.env import S40YowsupEnv
|
2017-10-29 15:28:48 +00:00
|
|
|
from yowsup.stacks import YowStack, YowStackBuilder
|
2015-08-30 18:51:44 +00:00
|
|
|
from yowsup.common import YowConstants
|
|
|
|
from yowsup.layers import YowLayerEvent, YowParallelLayer
|
2015-08-30 20:18:03 +00:00
|
|
|
from yowsup.layers.auth import AuthError
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
# Layers
|
2016-04-19 19:10:05 +00:00
|
|
|
from yowsup.layers.axolotl import AxolotlSendLayer, AxolotlControlLayer, AxolotlReceivelayer
|
2015-08-30 18:08:41 +00:00
|
|
|
from yowsup.layers.auth import YowCryptLayer, YowAuthenticationProtocolLayer
|
|
|
|
from yowsup.layers.coder import YowCoderLayer
|
|
|
|
from yowsup.layers.logger import YowLoggerLayer
|
|
|
|
from yowsup.layers.network import YowNetworkLayer
|
|
|
|
from yowsup.layers.protocol_messages import YowMessagesProtocolLayer
|
|
|
|
from yowsup.layers.stanzaregulator import YowStanzaRegulator
|
|
|
|
from yowsup.layers.protocol_media import YowMediaProtocolLayer
|
|
|
|
from yowsup.layers.protocol_acks import YowAckProtocolLayer
|
|
|
|
from yowsup.layers.protocol_receipts import YowReceiptProtocolLayer
|
|
|
|
from yowsup.layers.protocol_groups import YowGroupsProtocolLayer
|
|
|
|
from yowsup.layers.protocol_presence import YowPresenceProtocolLayer
|
|
|
|
from yowsup.layers.protocol_ib import YowIbProtocolLayer
|
|
|
|
from yowsup.layers.protocol_notifications import YowNotificationsProtocolLayer
|
|
|
|
from yowsup.layers.protocol_iq import YowIqProtocolLayer
|
|
|
|
from yowsup.layers.protocol_contacts import YowContactsIqProtocolLayer
|
|
|
|
from yowsup.layers.protocol_chatstate import YowChatstateProtocolLayer
|
|
|
|
from yowsup.layers.protocol_privacy import YowPrivacyProtocolLayer
|
|
|
|
from yowsup.layers.protocol_profiles import YowProfilesProtocolLayer
|
|
|
|
from yowsup.layers.protocol_calls import YowCallsProtocolLayer
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
# ProtocolEntities
|
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_acks.protocolentities import *
|
2015-08-30 18:51:44 +00:00
|
|
|
from yowsup.layers.protocol_chatstate.protocolentities import *
|
2015-09-15 19:29:26 +00:00
|
|
|
from yowsup.layers.protocol_contacts.protocolentities import *
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_groups.protocolentities import *
|
|
|
|
from yowsup.layers.protocol_media.protocolentities import *
|
2015-12-14 18:27:10 +00:00
|
|
|
from yowsup.layers.protocol_notifications.protocolentities import *
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_messages.protocolentities import *
|
|
|
|
from yowsup.layers.protocol_presence.protocolentities import *
|
2015-09-05 20:22:30 +00:00
|
|
|
from yowsup.layers.protocol_profiles.protocolentities import *
|
2016-01-08 13:30:47 +00:00
|
|
|
from yowsup.layers.protocol_privacy.protocolentities import *
|
2015-09-06 12:06:16 +00:00
|
|
|
from yowsup.layers.protocol_receipts.protocolentities import *
|
2016-01-08 13:30:47 +00:00
|
|
|
from yowsup.layers.protocol_iq.protocolentities import *
|
2015-11-09 22:09:12 +00:00
|
|
|
from yowsup.layers.protocol_media.mediauploader import MediaUploader
|
2016-01-09 15:12:59 +00:00
|
|
|
from yowsup.layers.protocol_media.mediadownloader import MediaDownloader
|
2015-09-06 12:06:16 +00:00
|
|
|
|
2016-01-07 15:57:41 +00:00
|
|
|
|
|
|
|
# Registration
|
|
|
|
|
2016-01-07 17:31:05 +00:00
|
|
|
from yowsup.registration import WACodeRequest
|
|
|
|
from yowsup.registration import WARegRequest
|
2016-01-07 15:57:41 +00:00
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
from functools import partial
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-08-30 19:03:11 +00:00
|
|
|
class YowsupApp(object):
|
2015-08-30 18:08:41 +00:00
|
|
|
def __init__(self):
|
2016-04-19 19:10:05 +00:00
|
|
|
env.CURRENT_ENV = env.AndroidYowsupEnv()
|
2015-08-30 18:08:41 +00:00
|
|
|
|
2015-08-30 20:18:03 +00:00
|
|
|
layers = (YowsupAppLayer,
|
2015-08-30 18:08:41 +00:00
|
|
|
YowParallelLayer((YowAuthenticationProtocolLayer,
|
|
|
|
YowMessagesProtocolLayer,
|
|
|
|
YowReceiptProtocolLayer,
|
|
|
|
YowAckProtocolLayer,
|
|
|
|
YowMediaProtocolLayer,
|
|
|
|
YowIbProtocolLayer,
|
|
|
|
YowIqProtocolLayer,
|
|
|
|
YowNotificationsProtocolLayer,
|
|
|
|
YowContactsIqProtocolLayer,
|
2015-09-03 18:04:29 +00:00
|
|
|
YowChatstateProtocolLayer,
|
2015-08-30 18:08:41 +00:00
|
|
|
YowCallsProtocolLayer,
|
|
|
|
YowPrivacyProtocolLayer,
|
|
|
|
YowProfilesProtocolLayer,
|
|
|
|
YowGroupsProtocolLayer,
|
|
|
|
YowPresenceProtocolLayer)),
|
2016-04-19 19:10:05 +00:00
|
|
|
AxolotlControlLayer,
|
|
|
|
YowParallelLayer((AxolotlSendLayer, AxolotlReceivelayer)),
|
2015-08-30 18:08:41 +00:00
|
|
|
YowCoderLayer,
|
|
|
|
YowCryptLayer,
|
|
|
|
YowStanzaRegulator,
|
|
|
|
YowNetworkLayer
|
|
|
|
)
|
2016-08-10 13:14:46 +00:00
|
|
|
|
2015-10-26 16:47:17 +00:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2016-08-10 13:14:46 +00:00
|
|
|
stackBuilder = YowStackBuilder()
|
|
|
|
|
|
|
|
self.stack = stackBuilder \
|
|
|
|
.pushDefaultLayers(True) \
|
|
|
|
.push(YowsupAppLayer) \
|
|
|
|
.build()
|
2015-08-30 18:08:41 +00:00
|
|
|
self.stack.broadcastEvent(
|
|
|
|
YowLayerEvent(YowsupAppLayer.EVENT_START, caller = self)
|
|
|
|
)
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
def login(self, username, password):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""Login to yowsup
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
Should result in onAuthSuccess or onAuthFailure to be called.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- username: (str) username in the form of 1239482382 (country code
|
|
|
|
and cellphone number)
|
|
|
|
|
|
|
|
- password: (str) base64 encoded password
|
|
|
|
"""
|
|
|
|
self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS,
|
|
|
|
(username, password))
|
|
|
|
# self.stack.setProp(YowIqProtocolLayer.PROP_PING_INTERVAL, 5)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.stack.broadcastEvent(
|
|
|
|
YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
|
|
|
|
except TypeError as e: # Occurs when password is not correctly formated
|
2015-12-31 19:43:10 +00:00
|
|
|
self.onAuthFailure('password not base64 encoded')
|
2015-09-02 15:04:49 +00:00
|
|
|
# try:
|
|
|
|
# self.stack.loop(timeout=0.5, discrete=0.5)
|
|
|
|
# except AuthError as e: # For some reason Yowsup throws an exception
|
2015-12-31 19:43:10 +00:00
|
|
|
# self.onAuthFailure("%s" % e)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
def logout(self):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:51:44 +00:00
|
|
|
Logout from whatsapp
|
|
|
|
"""
|
|
|
|
self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT))
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2017-02-11 18:52:17 +00:00
|
|
|
def sendReceipt(self, _id, _from, read, participant):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:51:44 +00:00
|
|
|
Send a receipt (delivered: double-tick, read: blue-ticks)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id: id of message received
|
2015-09-05 20:05:34 +00:00
|
|
|
- _from: jid of person who sent the message
|
|
|
|
- read: ('read' or None) None is just delivered, 'read' is read
|
2015-08-30 18:51:44 +00:00
|
|
|
- participant
|
|
|
|
"""
|
2017-02-11 18:52:17 +00:00
|
|
|
self.logger.debug(u'Sending receipt to whatsapp: %s', [_id, _from, read, participant])
|
|
|
|
receipt = OutgoingReceiptProtocolEntity(_id, _from, read, participant)
|
2015-09-02 15:04:49 +00:00
|
|
|
self.sendEntity(receipt)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2016-01-09 15:12:59 +00:00
|
|
|
def downloadMedia(self, url, onSuccess = None, onFailure = None):
|
|
|
|
downloader = MediaDownloader(onSuccess, onFailure)
|
|
|
|
downloader.download(url)
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
def sendTextMessage(self, to, message):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:51:44 +00:00
|
|
|
Sends a text message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- to: (xxxxxxxxxx@s.whatsapp.net) who to send the message to
|
|
|
|
- message: (str) the body of the message
|
|
|
|
"""
|
2016-04-12 12:53:15 +00:00
|
|
|
messageEntity = TextMessageProtocolEntity(message.encode('utf-8'), to = to)
|
2015-08-30 20:18:03 +00:00
|
|
|
self.sendEntity(messageEntity)
|
2015-11-09 22:09:12 +00:00
|
|
|
return messageEntity.getId()
|
|
|
|
|
|
|
|
def sendLocation(self, to, latitude, longitude):
|
2015-12-31 19:43:10 +00:00
|
|
|
messageEntity = LocationMediaMessageProtocolEntity(latitude,longitude, None, None, "raw", to = to)
|
2015-11-09 22:09:12 +00:00
|
|
|
self.sendEntity(messageEntity)
|
2016-04-06 20:02:40 +00:00
|
|
|
|
|
|
|
return messageEntity.getId()
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-01-09 15:12:59 +00:00
|
|
|
def sendImage(self, jid, path, caption = None, onSuccess = None, onFailure = None):
|
|
|
|
entity = RequestUploadIqProtocolEntity(RequestUploadIqProtocolEntity.MEDIA_TYPE_IMAGE, filePath=path)
|
|
|
|
successFn = lambda successEntity, originalEntity: self.onRequestUploadResult(jid, path, successEntity, originalEntity, caption, onSuccess, onFailure)
|
2016-04-06 20:02:40 +00:00
|
|
|
errorFn = lambda errorEntity, originalEntity: self.onRequestUploadError(jid, path, errorEntity, originalEntity)
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-04-06 20:02:40 +00:00
|
|
|
self.sendIq(entity, successFn, errorFn)
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-01-09 15:12:59 +00:00
|
|
|
def onRequestUploadResult(self, jid, filePath, resultRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity, caption = None, onSuccess=None, onFailure=None):
|
2016-04-06 20:02:40 +00:00
|
|
|
if requestUploadIqProtocolEntity.mediaType == RequestUploadIqProtocolEntity.MEDIA_TYPE_AUDIO:
|
|
|
|
doSendFn = self.doSendAudio
|
|
|
|
else:
|
|
|
|
doSendFn = self.doSendImage
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-04-06 20:02:40 +00:00
|
|
|
if resultRequestUploadIqProtocolEntity.isDuplicate():
|
|
|
|
doSendFn(filePath, resultRequestUploadIqProtocolEntity.getUrl(), jid,
|
2016-10-08 01:42:28 +00:00
|
|
|
resultRequestUploadIqProtocolEntity.getIp(), caption, onSuccess, onFailure)
|
2016-04-06 20:02:40 +00:00
|
|
|
else:
|
2016-10-08 01:42:28 +00:00
|
|
|
successFn = lambda filePath, jid, url: doSendFn(filePath, url.encode('ascii','ignore'), jid, resultRequestUploadIqProtocolEntity.getIp(), caption, onSuccess, onFailure)
|
2016-01-09 15:12:59 +00:00
|
|
|
ownNumber = self.stack.getLayerInterface(YowAuthenticationProtocolLayer).getUsername(full=False)
|
2016-10-08 01:42:28 +00:00
|
|
|
mediaUploader = MediaUploader(jid, ownNumber, filePath,
|
|
|
|
resultRequestUploadIqProtocolEntity.getUrl(),
|
|
|
|
resultRequestUploadIqProtocolEntity.getResumeOffset(),
|
|
|
|
successFn, self.onUploadError, self.onUploadProgress, async=False)
|
|
|
|
mediaUploader.start()
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-10-08 01:42:28 +00:00
|
|
|
def onRequestUploadError(self, jid, path, errorRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):
|
|
|
|
self.logger.error("Request upload for file %s for %s failed" % (path, jid))
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-10-08 01:42:28 +00:00
|
|
|
def onUploadError(self, filePath, jid, url):
|
2017-02-11 19:08:18 +00:00
|
|
|
self.logger.error("Upload file %s to %s for %s failed!" % (filePath, url, jid))
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-10-08 01:42:28 +00:00
|
|
|
def onUploadProgress(self, filePath, jid, url, progress):
|
2017-02-11 19:08:18 +00:00
|
|
|
self.logger.info("%s => %s, %d%% \r" % (os.path.basename(filePath), jid, progress))
|
2015-11-09 22:09:12 +00:00
|
|
|
pass
|
2016-04-06 20:02:40 +00:00
|
|
|
|
2017-02-11 19:08:18 +00:00
|
|
|
def doSendImage(self, filePath, url, to, ip = None, caption = None, onSuccess = None, onFailure = None):
|
|
|
|
entity = ImageDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, ip, to, caption = caption)
|
|
|
|
self.sendEntity(entity)
|
|
|
|
#self.msgIDs[entity.getId()] = MsgIDs(self.imgMsgId, entity.getId())
|
|
|
|
if onSuccess is not None:
|
|
|
|
onSuccess(entity.getId())
|
|
|
|
return entity.getId()
|
|
|
|
|
|
|
|
def doSendAudio(self, filePath, url, to, ip = None, caption = None, onSuccess = None, onFailure = None):
|
|
|
|
entity = AudioDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, ip, to)
|
|
|
|
self.sendEntity(entity)
|
|
|
|
#self.msgIDs[entity.getId()] = MsgIDs(self.imgMsgId, entity.getId())
|
|
|
|
if onSuccess is not None:
|
|
|
|
onSuccess(entity.getId())
|
|
|
|
return entity.getId()
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-09-02 15:04:49 +00:00
|
|
|
def sendPresence(self, available):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-02 15:04:49 +00:00
|
|
|
Send presence to whatsapp
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- available: (boolean) True if available false otherwise
|
|
|
|
"""
|
|
|
|
if available:
|
|
|
|
self.sendEntity(AvailablePresenceProtocolEntity())
|
|
|
|
else:
|
|
|
|
self.sendEntity(UnavailablePresenceProtocolEntity())
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
def subscribePresence(self, phone_number):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Subscribe to presence updates from phone_number
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- phone_number: (str) The cellphone number of the person to
|
|
|
|
subscribe to
|
|
|
|
"""
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Subscribing to Presence updates from %s" % phone_number)
|
2015-12-31 19:43:10 +00:00
|
|
|
jid = phone_number + '@s.whatsapp.net'
|
2015-09-03 18:04:29 +00:00
|
|
|
entity = SubscribePresenceProtocolEntity(jid)
|
|
|
|
self.sendEntity(entity)
|
|
|
|
|
|
|
|
def unsubscribePresence(self, phone_number):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Unsubscribe to presence updates from phone_number
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- phone_number: (str) The cellphone number of the person to
|
|
|
|
unsubscribe from
|
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
jid = phone_number + '@s.whatsapp.net'
|
2015-09-03 18:04:29 +00:00
|
|
|
entity = UnsubscribePresenceProtocolEntity(jid)
|
|
|
|
self.sendEntity(entity)
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-11-27 20:01:16 +00:00
|
|
|
def leaveGroup(self, group):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-11-27 20:01:16 +00:00
|
|
|
Permanently leave a WhatsApp group
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- group: (str) the group id (e.g. 27831788123-144024456)
|
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
entity = LeaveGroupsIqProtocolEntity([group + '@g.us'])
|
2015-11-27 20:01:16 +00:00
|
|
|
self.sendEntity(entity)
|
|
|
|
|
2015-09-02 15:04:49 +00:00
|
|
|
def setStatus(self, statusText):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-02 15:04:49 +00:00
|
|
|
Send status to whatsapp
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- statusTest: (str) Your whatsapp status
|
|
|
|
"""
|
2015-09-05 20:22:30 +00:00
|
|
|
iq = SetStatusIqProtocolEntity(statusText)
|
|
|
|
self.sendIq(iq)
|
2015-12-25 14:38:09 +00:00
|
|
|
|
|
|
|
def setProfilePicture(self, previewPicture, fullPicture = None):
|
|
|
|
"""
|
|
|
|
Requests profile picture of whatsapp user
|
|
|
|
Args:
|
|
|
|
- previewPicture: (bytes) The preview picture
|
|
|
|
- fullPicture: (bytes) The full profile picture
|
|
|
|
"""
|
|
|
|
if fullPicture == None:
|
|
|
|
fullPicture = previewPicture
|
|
|
|
ownJid = self.stack.getLayerInterface(YowAuthenticationProtocolLayer).getUsername(full = True)
|
|
|
|
iq = SetPictureIqProtocolEntity(ownJid, previewPicture, fullPicture)
|
|
|
|
self.sendIq(iq)
|
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def sendTyping(self, phoneNumber, typing):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Notify buddy using phoneNumber that you are typing to him
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- phoneNumber: (str) cellphone number of the buddy you are typing to.
|
|
|
|
- typing: (bool) True if you are typing, False if you are not
|
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
jid = phoneNumber + '@s.whatsapp.net'
|
2015-09-03 18:04:29 +00:00
|
|
|
if typing:
|
|
|
|
state = OutgoingChatstateProtocolEntity(
|
|
|
|
ChatstateProtocolEntity.STATE_TYPING, jid
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
state = OutgoingChatstateProtocolEntity(
|
|
|
|
ChatstateProtocolEntity.STATE_PAUSED, jid
|
|
|
|
)
|
|
|
|
self.sendEntity(state)
|
2016-01-06 15:26:59 +00:00
|
|
|
|
|
|
|
def sendSync(self, contacts, delta = False, interactive = True, success = None, failure = None):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-15 19:29:26 +00:00
|
|
|
You need to sync new contacts before you interact with
|
|
|
|
them, failure to do so could result in a temporary ban.
|
2016-01-06 15:26:59 +00:00
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
Args:
|
|
|
|
- contacts: ([str]) a list of phone numbers of the
|
|
|
|
contacts you wish to sync
|
|
|
|
- delta: (bool; default: False) If true only send new
|
|
|
|
contacts to sync, if false you should send your full
|
|
|
|
contact list.
|
|
|
|
- interactive: (bool; default: True) Set to false if you are
|
|
|
|
sure this is the first time registering
|
2016-01-06 15:26:59 +00:00
|
|
|
- success: (func) - Callback; Takes three arguments: existing numbers,
|
|
|
|
non-existing numbers, invalid numbers.
|
2015-09-15 19:29:26 +00:00
|
|
|
"""
|
|
|
|
mode = GetSyncIqProtocolEntity.MODE_DELTA if delta else GetSyncIqProtocolEntity.MODE_FULL
|
|
|
|
context = GetSyncIqProtocolEntity.CONTEXT_INTERACTIVE if interactive else GetSyncIqProtocolEntity.CONTEXT_REGISTRATION
|
2016-01-06 15:38:52 +00:00
|
|
|
# International contacts must be preceded by a plus. Other numbers are
|
|
|
|
# considered local.
|
|
|
|
contacts = ['+' + c for c in contacts]
|
2015-09-15 19:29:26 +00:00
|
|
|
iq = GetSyncIqProtocolEntity(contacts, mode, context)
|
2016-01-06 15:26:59 +00:00
|
|
|
def onSuccess(response, request):
|
2016-01-06 15:38:52 +00:00
|
|
|
# Remove leading plus
|
2016-01-07 19:12:59 +00:00
|
|
|
if success is not None:
|
|
|
|
existing = [s[1:] for s in response.inNumbers.keys()]
|
|
|
|
nonexisting = [s[1:] for s in response.outNumbers.keys()]
|
|
|
|
invalid = [s[1:] for s in response.invalidNumbers]
|
|
|
|
success(existing, nonexisting, invalid)
|
2016-01-06 15:26:59 +00:00
|
|
|
|
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = failure)
|
|
|
|
|
2016-01-08 13:30:47 +00:00
|
|
|
def requestClientConfig(self, success = None, failure = None):
|
|
|
|
"""I'm not sure what this does, but it might be required on first login."""
|
|
|
|
iq = PushIqProtocolEntity()
|
|
|
|
self.sendIq(iq, onSuccess = success, onError = failure)
|
|
|
|
|
|
|
|
|
|
|
|
def requestPrivacyList(self, success = None, failure = None):
|
|
|
|
"""I'm not sure what this does, but it might be required on first login."""
|
|
|
|
iq = PrivacyListIqProtocolEntity()
|
|
|
|
self.sendIq(iq, onSuccess = success, onError = failure)
|
|
|
|
|
|
|
|
def requestServerProperties(self, success = None, failure = None):
|
|
|
|
"""I'm not sure what this does, but it might be required on first login."""
|
|
|
|
iq = PropsIqProtocolEntity()
|
|
|
|
self.sendIq(iq, onSuccess = success, onError = failure)
|
2016-01-06 15:26:59 +00:00
|
|
|
|
2015-12-14 18:14:20 +00:00
|
|
|
def requestStatuses(self, contacts, success = None, failure = None):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-12-14 18:14:20 +00:00
|
|
|
Request the statuses of a number of users.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- contacts: ([str]) the phone numbers of users whose statuses you
|
|
|
|
wish to request
|
|
|
|
- success: (func) called when request is successful
|
|
|
|
- failure: (func) called when request has failed
|
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
iq = GetStatusesIqProtocolEntity([c + '@s.whatsapp.net' for c in contacts])
|
2015-12-14 18:14:20 +00:00
|
|
|
def onSuccess(response, request):
|
2016-01-07 19:12:59 +00:00
|
|
|
if success is not None:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Received Statuses %s" % response)
|
2016-01-07 19:12:59 +00:00
|
|
|
s = {}
|
|
|
|
for k, v in response.statuses.iteritems():
|
|
|
|
s[k.split('@')[0]] = v
|
|
|
|
success(s)
|
2015-12-14 18:14:20 +00:00
|
|
|
|
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = failure)
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def requestLastSeen(self, phoneNumber, success = None, failure = None):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Requests when user was last seen.
|
|
|
|
Args:
|
|
|
|
- phone_number: (str) the phone number of the user
|
|
|
|
- success: (func) called when request is successfully processed.
|
|
|
|
The first argument is the number, second argument is the seconds
|
|
|
|
since last seen.
|
|
|
|
- failure: (func) called when request has failed
|
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
iq = LastseenIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
|
2015-09-06 12:06:16 +00:00
|
|
|
self.sendIq(iq, onSuccess = partial(self._lastSeenSuccess, success),
|
|
|
|
onError = failure)
|
2015-09-05 20:22:30 +00:00
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
def _lastSeenSuccess(self, success, response, request):
|
2015-12-31 19:43:10 +00:00
|
|
|
success(response._from.split('@')[0], response.seconds)
|
2015-09-02 15:04:49 +00:00
|
|
|
|
2015-09-05 22:14:12 +00:00
|
|
|
def requestProfilePicture(self, phoneNumber, onSuccess = None, onFailure = None):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-05 22:14:12 +00:00
|
|
|
Requests profile picture of whatsapp user
|
|
|
|
Args:
|
|
|
|
- phoneNumber: (str) the phone number of the user
|
2015-09-15 19:29:26 +00:00
|
|
|
- onSuccess: (func) called when request is successfully processed.
|
|
|
|
- onFailure: (func) called when request has failed
|
2015-09-05 22:14:12 +00:00
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
iq = GetPictureIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
|
2015-09-05 22:14:12 +00:00
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
2015-09-06 12:06:16 +00:00
|
|
|
|
|
|
|
def requestGroupsList(self, onSuccess = None, onFailure = None):
|
|
|
|
iq = ListGroupsIqProtocolEntity()
|
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
2015-09-05 22:14:12 +00:00
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
def requestGroupInfo(self, group, onSuccess = None, onFailure = None):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-15 19:29:26 +00:00
|
|
|
Request info on a specific group (includes participants, subject, owner etc.)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- group: (str) the group id in the form of xxxxxxxxx-xxxxxxxx
|
|
|
|
- onSuccess: (func) called when request is successfully processed.
|
|
|
|
- onFailure: (func) called when request is has failed
|
|
|
|
"""
|
2015-12-31 19:43:10 +00:00
|
|
|
iq = InfoGroupsIqProtocolEntity(group + '@g.us')
|
2015-09-15 19:29:26 +00:00
|
|
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
|
|
|
|
2016-01-07 15:57:41 +00:00
|
|
|
def requestSMSCode(self, countryCode, phoneNumber):
|
|
|
|
"""
|
|
|
|
Request an sms regitration code. WARNING: this function is blocking
|
|
|
|
|
|
|
|
Args:
|
|
|
|
countryCode: The country code of the phone you wish to register
|
|
|
|
phoneNumber: phoneNumber of the phone you wish to register without
|
|
|
|
the country code.
|
|
|
|
"""
|
|
|
|
request = WACodeRequest(countryCode, phoneNumber)
|
|
|
|
return request.send()
|
|
|
|
|
2016-01-07 17:31:05 +00:00
|
|
|
def requestPassword(self, countryCode, phoneNumber, smsCode):
|
|
|
|
"""
|
|
|
|
Request a password. WARNING: this function is blocking
|
|
|
|
|
|
|
|
Args:
|
|
|
|
countryCode: The country code of the phone you wish to register
|
|
|
|
phoneNumber: phoneNumber of the phone you wish to register without
|
|
|
|
the country code.
|
|
|
|
smsCode: The sms code that you asked for previously
|
|
|
|
"""
|
|
|
|
smsCode = smsCode.replace('-', '')
|
|
|
|
request = WARegRequest(countryCode, phoneNumber, smsCode)
|
|
|
|
return request.send()
|
|
|
|
|
|
|
|
|
2016-01-07 15:57:41 +00:00
|
|
|
|
2015-08-30 18:08:41 +00:00
|
|
|
def onAuthSuccess(self, status, kind, creation, expiration, props, nonce, t):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:08:41 +00:00
|
|
|
Called when login is successful.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- status
|
|
|
|
- kind
|
|
|
|
- creation
|
|
|
|
- expiration
|
|
|
|
- props
|
|
|
|
- nonce
|
|
|
|
- t
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onAuthFailure(self, reason):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:08:41 +00:00
|
|
|
Called when login is a failure
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- reason: (str) Reason for the login failure
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onReceipt(self, _id, _from, timestamp, type, participant, offline, items):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:08:41 +00:00
|
|
|
Called when a receipt is received (double tick or blue tick)
|
|
|
|
|
|
|
|
Args
|
|
|
|
- _id
|
|
|
|
- _from
|
|
|
|
- timestamp
|
|
|
|
- type: Is 'read' for blue ticks and None for double-ticks
|
|
|
|
- participant: (dxxxxxxxxxx@s.whatsapp.net) delivered to or
|
|
|
|
read by this participant in group
|
|
|
|
- offline: (True, False or None)
|
|
|
|
- items
|
|
|
|
"""
|
2015-09-02 15:04:49 +00:00
|
|
|
pass
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
def onAck(self, _id,_class, _from, timestamp):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:08:41 +00:00
|
|
|
Called when Ack is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id
|
|
|
|
- _class: ('message', 'receipt' or something else?)
|
|
|
|
- _from
|
|
|
|
- timestamp
|
|
|
|
"""
|
|
|
|
pass
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def onPresenceReceived(self, _type, name, _from, last):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Called when presence (e.g. available, unavailable) is received
|
|
|
|
from whatsapp
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _type: (str) 'available' or 'unavailable'
|
|
|
|
- _name
|
|
|
|
- _from
|
|
|
|
- _last
|
|
|
|
"""
|
|
|
|
pass
|
2015-08-30 18:08:41 +00:00
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
def onDisconnect(self):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-08-30 18:51:44 +00:00
|
|
|
Called when disconnected from whatsapp
|
|
|
|
"""
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def onContactTyping(self, number):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Called when contact starts to type
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- number: (str) cellphone number of contact
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onContactPaused(self, number):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-03 18:04:29 +00:00
|
|
|
Called when contact stops typing
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- number: (str) cellphone number of contact
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2016-04-12 12:53:15 +00:00
|
|
|
def onTextMessage(self, _id, _from, to, notify, timestamp, participant, offline, retry, body):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-05 20:05:34 +00:00
|
|
|
Called when text message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id:
|
|
|
|
- _from: (str) jid of of sender
|
|
|
|
- to:
|
|
|
|
- notify: (str) human readable name of _from (e.g. John Smith)
|
|
|
|
- timestamp:
|
2015-09-06 12:06:16 +00:00
|
|
|
- participant: (str) jid of user who sent the message in a groupchat
|
2015-09-05 20:05:34 +00:00
|
|
|
- offline:
|
|
|
|
- retry:
|
|
|
|
- body: The content of the message
|
|
|
|
"""
|
|
|
|
pass
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
def onImage(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-05 20:05:34 +00:00
|
|
|
Called when image message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: ImageDownloadableMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onAudio(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-05 20:05:34 +00:00
|
|
|
Called when audio message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: AudioDownloadableMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
|
|
|
|
def onVideo(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-05 20:05:34 +00:00
|
|
|
Called when video message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: VideoDownloadableMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
2015-09-15 19:29:26 +00:00
|
|
|
|
|
|
|
def onLocation(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-15 19:29:26 +00:00
|
|
|
Called when location message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- entity: LocationMediaMessageProtocolEntity
|
|
|
|
"""
|
|
|
|
pass
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2015-09-05 20:05:34 +00:00
|
|
|
Called when VCard message is received
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- _id: (str) id of entity
|
|
|
|
- _from:
|
|
|
|
- name:
|
|
|
|
- card_data:
|
|
|
|
- to:
|
|
|
|
- notify:
|
|
|
|
- timestamp:
|
|
|
|
- participant:
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2015-10-26 16:47:17 +00:00
|
|
|
def onAddedToGroup(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""Called when the user has been added to a new group"""
|
2015-10-26 16:47:17 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def onParticipantsAddedToGroup(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""Called when participants have been added to a group"""
|
2015-10-26 16:47:17 +00:00
|
|
|
pass
|
|
|
|
|
2015-11-29 14:32:33 +00:00
|
|
|
def onParticipantsRemovedFromGroup(self, group, participants):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""Called when participants have been removed from a group
|
2016-01-07 10:58:03 +00:00
|
|
|
|
2015-11-29 14:32:33 +00:00
|
|
|
Args:
|
|
|
|
- group: (str) id of the group (e.g. 27831788123-144024456)
|
|
|
|
- participants: (list) jids of participants that are removed
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2016-01-07 10:58:03 +00:00
|
|
|
def onSubjectChanged(self, group, subject, subjectOwner, timestamp):
|
|
|
|
"""Called when someone changes the grousp subject
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- group: (str) id of the group (e.g. 27831788123-144024456)
|
|
|
|
- subject: (str) the new subject
|
|
|
|
- subjectOwner: (str) the number of the person who changed the subject
|
|
|
|
- timestamp: (str) time the subject was changed
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2015-12-14 18:27:10 +00:00
|
|
|
def onContactStatusChanged(self, number, status):
|
|
|
|
"""Called when a contacts changes their status
|
|
|
|
|
2016-01-07 09:47:33 +00:00
|
|
|
Args:
|
2016-01-07 10:58:03 +00:00
|
|
|
number: (str) the number of the contact who changed their status
|
|
|
|
status: (str) the new status
|
2015-12-14 18:27:10 +00:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2016-01-07 09:47:33 +00:00
|
|
|
def onContactPictureChanged(self, number):
|
|
|
|
"""Called when a contact changes their profile picture
|
|
|
|
Args
|
|
|
|
number: (str) the number of the contact who changed their picture
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2016-01-07 10:27:40 +00:00
|
|
|
def onContactRemoved(self, number):
|
|
|
|
"""Called when a contact has been removed
|
|
|
|
|
|
|
|
Args:
|
|
|
|
number: (str) the number of the contact who has been removed
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onContactAdded(self, number, nick):
|
|
|
|
"""Called when a contact has been added
|
|
|
|
|
|
|
|
Args:
|
|
|
|
number: (str) contacts number
|
|
|
|
nick: (str) contacts nickname
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onContactUpdated(self, oldNumber, newNumber):
|
|
|
|
"""Called when a contact has changed their number
|
|
|
|
|
|
|
|
Args:
|
|
|
|
oldNumber: (str) the number the contact previously used
|
|
|
|
newNumber: (str) the new number of the contact
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2015-08-30 20:18:03 +00:00
|
|
|
def sendEntity(self, entity):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""Sends an entity down the stack (as if YowsupAppLayer called toLower)"""
|
2015-08-30 20:18:03 +00:00
|
|
|
self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT,
|
|
|
|
entity = entity
|
|
|
|
))
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-09-05 20:22:30 +00:00
|
|
|
def sendIq(self, iq, onSuccess = None, onError = None):
|
|
|
|
self.stack.broadcastEvent(
|
|
|
|
YowLayerEvent(
|
|
|
|
YowsupAppLayer.SEND_IQ,
|
|
|
|
iq = iq,
|
|
|
|
success = onSuccess,
|
|
|
|
failure = onError,
|
|
|
|
)
|
|
|
|
)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-08-30 18:08:41 +00:00
|
|
|
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
|
|
|
|
|
|
|
|
class YowsupAppLayer(YowInterfaceLayer):
|
2015-12-31 19:43:10 +00:00
|
|
|
EVENT_START = 'transwhat.event.YowsupAppLayer.start'
|
|
|
|
TO_LOWER_EVENT = 'transwhat.event.YowsupAppLayer.toLower'
|
|
|
|
SEND_IQ = 'transwhat.event.YowsupAppLayer.sendIq'
|
2015-08-30 18:08:41 +00:00
|
|
|
|
|
|
|
def onEvent(self, layerEvent):
|
|
|
|
# We cannot pass instance varaibles in through init, so we use an event
|
|
|
|
# instead
|
|
|
|
# Return False if you want the event to propogate down the stack
|
|
|
|
# return True otherwise
|
|
|
|
if layerEvent.getName() == YowsupAppLayer.EVENT_START:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.caller = layerEvent.getArg('caller')
|
2015-10-26 16:47:17 +00:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2015-08-30 18:51:44 +00:00
|
|
|
return True
|
|
|
|
elif layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
|
|
|
|
self.caller.onDisconnect()
|
|
|
|
return True
|
2015-08-30 20:18:03 +00:00
|
|
|
elif layerEvent.getName() == YowsupAppLayer.TO_LOWER_EVENT:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.toLower(layerEvent.getArg('entity'))
|
2015-08-30 20:18:03 +00:00
|
|
|
return True
|
2015-09-03 18:04:29 +00:00
|
|
|
elif layerEvent.getName() == YowsupAppLayer.SEND_IQ:
|
2015-12-31 19:43:10 +00:00
|
|
|
iq = layerEvent.getArg('iq')
|
|
|
|
success = layerEvent.getArg('success')
|
|
|
|
failure = layerEvent.getArg('failure')
|
2015-09-03 18:04:29 +00:00
|
|
|
self._sendIq(iq, success, failure)
|
|
|
|
return True
|
|
|
|
return False
|
2015-08-30 18:08:41 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('success')
|
2015-08-30 18:08:41 +00:00
|
|
|
def onAuthSuccess(self, entity):
|
|
|
|
# entity is SuccessProtocolEntity
|
2015-08-30 18:51:44 +00:00
|
|
|
status = entity.status
|
|
|
|
kind = entity.kind
|
|
|
|
creation = entity.creation
|
|
|
|
expiration = entity.expiration
|
|
|
|
props = entity.props
|
|
|
|
nonce = entity.nonce
|
2015-12-31 19:43:10 +00:00
|
|
|
t = entity.t # I don't know what this is
|
2015-08-30 18:08:41 +00:00
|
|
|
self.caller.onAuthSuccess(status, kind, creation, expiration, props, nonce, t)
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('failure')
|
2015-08-30 18:08:41 +00:00
|
|
|
def onAuthFailure(self, entity):
|
|
|
|
# entity is FailureProtocolEntity
|
|
|
|
reason = entity.reason
|
|
|
|
self.caller.onAuthFailure(reason)
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('receipt')
|
2015-08-30 18:08:41 +00:00
|
|
|
def onReceipt(self, entity):
|
|
|
|
"""Sends ack automatically"""
|
|
|
|
# entity is IncomingReceiptProtocolEntity
|
2016-04-05 18:32:29 +00:00
|
|
|
#ack = OutgoingAckProtocolEntity(entity.getId(),
|
|
|
|
# 'receipt', entity.getType(), entity.getFrom())
|
|
|
|
self.toLower(entity.ack())
|
2015-08-30 18:08:41 +00:00
|
|
|
_id = entity._id
|
|
|
|
_from = entity._from
|
|
|
|
timestamp = entity.timestamp
|
2015-08-30 18:51:44 +00:00
|
|
|
type = entity.type
|
|
|
|
participant = entity.participant
|
2015-08-30 18:08:41 +00:00
|
|
|
offline = entity.offline
|
|
|
|
items = entity.items
|
|
|
|
self.caller.onReceipt(_id, _from, timestamp, type, participant, offline, items)
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('ack')
|
2015-08-30 19:03:11 +00:00
|
|
|
def onAck(self, entity):
|
2015-08-30 18:08:41 +00:00
|
|
|
# entity is IncomingAckProtocolEntity
|
|
|
|
self.caller.onAck(
|
|
|
|
entity._id,
|
|
|
|
entity._class,
|
|
|
|
entity._from,
|
|
|
|
entity.timestamp
|
|
|
|
)
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('notification')
|
2015-08-30 18:08:41 +00:00
|
|
|
def onNotification(self, entity):
|
|
|
|
"""
|
|
|
|
Sends ack automatically
|
|
|
|
"""
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Received notification (%s): %s" % (type(entity), entity))
|
2015-09-03 18:04:29 +00:00
|
|
|
self.toLower(entity.ack())
|
2015-10-26 16:47:17 +00:00
|
|
|
if isinstance(entity, CreateGroupsNotificationProtocolEntity):
|
|
|
|
self.caller.onAddedToGroup(entity)
|
|
|
|
elif isinstance(entity, AddGroupsNotificationProtocolEntity):
|
|
|
|
self.caller.onParticipantsAddedToGroup(entity)
|
2015-11-29 14:32:33 +00:00
|
|
|
elif isinstance(entity, RemoveGroupsNotificationProtocolEntity):
|
|
|
|
self.caller.onParticipantsRemovedFromGroup(
|
2015-12-31 19:43:10 +00:00
|
|
|
entity.getGroupId().split('@')[0],
|
2015-11-29 14:32:33 +00:00
|
|
|
entity.getParticipants().keys()
|
|
|
|
)
|
2016-01-07 10:58:03 +00:00
|
|
|
elif isinstance(entity, SubjectGroupsNotificationProtocolEntity):
|
|
|
|
self.caller.onSubjectChanged(
|
|
|
|
entity.getGroupId().split('@')[0],
|
|
|
|
entity.getSubject(),
|
|
|
|
entity.getSubjectOwner(full=False),
|
|
|
|
entity.getSubjectTimestamp()
|
|
|
|
)
|
2015-12-14 18:27:10 +00:00
|
|
|
elif isinstance(entity, StatusNotificationProtocolEntity):
|
|
|
|
self.caller.onContactStatusChanged(
|
|
|
|
entity._from.split('@')[0],
|
|
|
|
entity.status
|
|
|
|
)
|
2016-01-07 21:04:20 +00:00
|
|
|
elif isinstance(entity, SetPictureNotificationProtocolEntity):
|
2016-01-07 09:47:33 +00:00
|
|
|
self.caller.onContactPictureChanged(entity.setJid.split('@')[0])
|
2016-01-07 21:04:20 +00:00
|
|
|
elif isinstance(entity, DeletePictureNotificationProtocolEntity):
|
|
|
|
self.caller.onContactPictureChanged(entity.deleteJid.split('@')[0])
|
2016-01-07 10:27:40 +00:00
|
|
|
elif isinstance(entity, RemoveContactNotificationProtocolEntity):
|
|
|
|
self.caller.onContactRemoved(entity.contactJid.split('@')[0])
|
|
|
|
elif isinstance(entity, AddContactNotificationProtocolEntity):
|
|
|
|
self.caller.onContactAdded(
|
|
|
|
entity.contactJid.split('@')[0],
|
|
|
|
entity.notify
|
|
|
|
)
|
|
|
|
elif isinstance(entity, UpdateContactNotificationProtocolEntity):
|
|
|
|
self.caller.onContactUpdated(
|
|
|
|
entity._from.split('@')[0],
|
|
|
|
entity.contactJid.split('@')[0],
|
|
|
|
)
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('message')
|
2015-08-30 18:08:41 +00:00
|
|
|
def onMessageReceived(self, entity):
|
2016-04-27 21:08:47 +00:00
|
|
|
self.logger.debug("Received Message: %s" % unicode(entity))
|
2015-09-05 20:05:34 +00:00
|
|
|
if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT:
|
|
|
|
self.caller.onTextMessage(
|
|
|
|
entity._id,
|
|
|
|
entity._from,
|
|
|
|
entity.to,
|
|
|
|
entity.notify,
|
|
|
|
entity.timestamp,
|
|
|
|
entity.participant,
|
|
|
|
entity.offline,
|
|
|
|
entity.retry,
|
|
|
|
entity.body
|
|
|
|
)
|
|
|
|
elif entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_MEDIA:
|
|
|
|
if isinstance(entity, ImageDownloadableMediaMessageProtocolEntity):
|
|
|
|
# There is just way too many fields to pass them into the
|
|
|
|
# function
|
|
|
|
self.caller.onImage(entity)
|
|
|
|
elif isinstance(entity, AudioDownloadableMediaMessageProtocolEntity):
|
|
|
|
self.caller.onAudio(entity)
|
|
|
|
elif isinstance(entity, VideoDownloadableMediaMessageProtocolEntity):
|
|
|
|
self.caller.onVideo(entity)
|
|
|
|
elif isinstance(entity, VCardMediaMessageProtocolEntity):
|
|
|
|
self.caller.onVCard(
|
|
|
|
entity._id,
|
|
|
|
entity._from,
|
|
|
|
entity.name,
|
|
|
|
entity.card_data,
|
|
|
|
entity.to,
|
|
|
|
entity.notify,
|
|
|
|
entity.timestamp,
|
|
|
|
entity.participant
|
|
|
|
)
|
2015-09-15 19:29:26 +00:00
|
|
|
elif isinstance(entity, LocationMediaMessageProtocolEntity):
|
|
|
|
self.caller.onLocation(entity)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('presence')
|
2015-09-03 18:04:29 +00:00
|
|
|
def onPresenceReceived(self, presence):
|
|
|
|
_type = presence.getType()
|
|
|
|
name = presence.getName()
|
|
|
|
_from = presence.getFrom()
|
|
|
|
last = presence.getLast()
|
|
|
|
self.caller.onPresenceReceived(_type, name, _from, last)
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
@ProtocolEntityCallback('chatstate')
|
2015-09-03 18:04:29 +00:00
|
|
|
def onChatstate(self, chatstate):
|
|
|
|
number = chatstate._from.split('@')[0]
|
|
|
|
if chatstate.getState() == ChatstateProtocolEntity.STATE_TYPING:
|
|
|
|
self.caller.onContactTyping(number)
|
|
|
|
else:
|
|
|
|
self.caller.onContactPaused(number)
|