2013-08-01 23:45:51 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2013, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
|
|
|
__status__ = "Prototype"
|
|
|
|
|
|
|
|
"""
|
|
|
|
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/>.
|
|
|
|
"""
|
|
|
|
|
2013-06-28 20:45:38 +00:00
|
|
|
import utils
|
|
|
|
import logging
|
|
|
|
import urllib
|
|
|
|
import time
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
from Spectrum2 import protocol_pb2
|
|
|
|
|
2013-06-28 20:45:38 +00:00
|
|
|
from buddy import BuddyList
|
2013-05-31 18:53:21 +00:00
|
|
|
from threading import Timer
|
|
|
|
from group import Group
|
2013-06-28 20:45:38 +00:00
|
|
|
from bot import Bot
|
|
|
|
from constants import *
|
2015-08-30 18:51:44 +00:00
|
|
|
from yowsupwrapper import YowsupApp
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
class Session(YowsupApp):
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def __init__(self, backend, user, legacyName, extra, db):
|
2015-08-30 19:03:11 +00:00
|
|
|
super(Session, self).__init__()
|
2013-06-28 20:46:56 +00:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
self.logger.info("Created: %s", legacyName)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
self.db = db
|
|
|
|
self.backend = backend
|
|
|
|
self.user = user
|
2013-06-28 20:46:56 +00:00
|
|
|
self.legacyName = legacyName
|
2015-08-30 20:18:03 +00:00
|
|
|
self.buddies = BuddyList(self.legacyName, self.db)
|
|
|
|
self.bot = Bot(self)
|
2013-06-28 20:46:56 +00:00
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
self.status = protocol_pb2.STATUS_NONE
|
|
|
|
self.statusMessage = ''
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
self.groups = {}
|
|
|
|
self.presenceRequested = []
|
|
|
|
self.offlineQueue = []
|
|
|
|
self.groupOfflineQueue = { }
|
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
self.timer = None
|
2013-06-28 20:46:56 +00:00
|
|
|
self.password = None
|
|
|
|
self.initialized = False
|
2015-06-21 19:24:36 +00:00
|
|
|
self.loggedin = False
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
self.bot = Bot(self)
|
|
|
|
|
|
|
|
def __del__(self): # handleLogoutRequest
|
|
|
|
self.logout()
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-02-27 17:28:36 +00:00
|
|
|
def call(self, method, **kwargs):
|
|
|
|
self.logger.debug("%s(%s)", method,
|
|
|
|
", ".join(str(k) + ': ' + str(v) for k, v in kwargs.items()))
|
2015-08-30 18:51:44 +00:00
|
|
|
##self.stack.broadcastEvent(YowLayerEvent(method, **kwargs))
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def logout(self):
|
2015-06-21 19:24:36 +00:00
|
|
|
self.loggedin = False
|
2015-08-30 18:51:44 +00:00
|
|
|
super(Session, self).logout()
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
def login(self, password):
|
2015-06-21 19:24:36 +00:00
|
|
|
self.loggedin = True
|
|
|
|
self.password = password
|
2015-08-30 18:51:44 +00:00
|
|
|
super(Session, self).login(self.legacyName, self.password)
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
def updateRoomList(self):
|
|
|
|
rooms = []
|
|
|
|
for room, group in self.groups.iteritems():
|
|
|
|
rooms.append([room, group.subject])
|
2015-09-06 12:06:16 +00:00
|
|
|
self.logger.debug("Got rooms: %s", rooms)
|
2013-06-28 20:46:56 +00:00
|
|
|
self.backend.handleRoomList(rooms)
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
def updateRoster(self):
|
|
|
|
self.logger.debug("Update roster")
|
|
|
|
|
|
|
|
old = self.buddies.keys()
|
|
|
|
self.buddies.load()
|
|
|
|
new = self.buddies.keys()
|
|
|
|
|
|
|
|
add = set(new) - set(old)
|
|
|
|
remove = set(old) - set(new)
|
|
|
|
|
|
|
|
self.logger.debug("Roster remove: %s", str(list(remove)))
|
|
|
|
self.logger.debug("Roster add: %s", str(list(add)))
|
|
|
|
|
|
|
|
for number in remove:
|
|
|
|
self.backend.handleBuddyChanged(self.user, number, "", [], protocol_pb2.STATUS_NONE)
|
|
|
|
self.backend.handleBuddyRemoved(self.user, number)
|
2015-09-03 18:04:29 +00:00
|
|
|
self.unsubscribePresence(number)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
for number in add:
|
|
|
|
buddy = self.buddies[number]
|
2015-09-03 18:04:29 +00:00
|
|
|
self.subscribePresence(number)
|
2015-09-05 22:14:12 +00:00
|
|
|
self.backend.handleBuddyChanged(self.user, number, buddy.nick, buddy.groups, protocol_pb2.STATUS_NONE, iconHash = buddy.image_hash if buddy.image_hash is not None else "")
|
2015-09-06 12:06:16 +00:00
|
|
|
#self.requestLastSeen(number, self._lastSeen)
|
|
|
|
self.logger.debug('Requesting groups list')
|
|
|
|
self.requestGroupsList(self._updateGroups)
|
|
|
|
|
|
|
|
def _updateGroups(self, response, request):
|
|
|
|
self.logger.debug('Received groups list %s', response)
|
|
|
|
# This XMPP client is not receiving this for some reason.
|
|
|
|
groups = response.getGroups()
|
|
|
|
for group in groups:
|
|
|
|
room = group.getId()
|
|
|
|
owner = group.getOwner()
|
|
|
|
subjectOwner = group.getSubjectOwner()
|
|
|
|
subject = group.getSubject()
|
|
|
|
|
|
|
|
if room in self.groups:
|
|
|
|
oroom = self.groups[room]
|
|
|
|
oroom.owner = owner
|
|
|
|
oroom.subjectOwner = subjectOwner
|
|
|
|
oroom.subject = subject
|
|
|
|
else:
|
|
|
|
self.groups[room] = Group(room, owner, subject, subjectOwner)
|
|
|
|
# A crude implemtation of groups that act like buddies
|
|
|
|
|
|
|
|
self.backend.handleBuddyChanged(self.user, room, subject, [], protocol_pb2.STATUS_NONE)
|
|
|
|
# This XMPP client is not receiving this for some reason.
|
|
|
|
# self.updateRoomList()
|
|
|
|
# for group in groups:
|
|
|
|
# room = group.getId()
|
|
|
|
# subjectOwner = group.getSubjectOwner()
|
|
|
|
# subject = group.getSubject()
|
|
|
|
# self.backend.handleSubject(self.user, room, subject, subjectOwner)
|
|
|
|
# for participant in group.getParticipants():
|
|
|
|
# buddy = participant.split('@')[0]
|
|
|
|
# self.logger.debug("Added %s to room %s", buddy, room)
|
|
|
|
# self.backend.handleParticipantChanged(self.user, buddy, room,
|
|
|
|
# protocol_pb2.PARTICIPANT_FLAG_NONE, protocol_pb2.STATUS_ONLINE)
|
|
|
|
|
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
def _lastSeen(self, number, seconds):
|
|
|
|
self.logger.debug("Last seen %s at %s seconds" % (number, str(seconds)))
|
|
|
|
if seconds < 60:
|
|
|
|
self.onPresenceAvailable(number)
|
|
|
|
else:
|
|
|
|
self.onPresenceUnavailable(number)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onAuthSuccess(self, status, kind, creation,
|
|
|
|
expiration, props, nonce, t):
|
|
|
|
self.logger.info("Auth success: %s", self.user)
|
|
|
|
|
|
|
|
self.backend.handleConnected(self.user)
|
|
|
|
self.backend.handleBuddyChanged(self.user, "bot", self.bot.name, ["Admin"], protocol_pb2.STATUS_ONLINE)
|
|
|
|
self.initialized = True
|
2015-09-03 18:04:29 +00:00
|
|
|
self.sendPresence(True)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
self.updateRoster()
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onAuthFailed(self, reason):
|
|
|
|
self.logger.info("Auth failed: %s (%s)", self.user, reason)
|
|
|
|
self.backend.handleDisconnected(self.user, 0, reason)
|
|
|
|
self.password = None
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onDisconnect(self):
|
2015-09-02 15:04:49 +00:00
|
|
|
self.logger.debug('Disconnected')
|
2015-08-30 18:51:44 +00:00
|
|
|
self.backend.handleDisconnected(self.user, 0, 'Disconnected for unknown reasons')
|
|
|
|
self.loggedin = False
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onReceipt(self, _id, _from, timestamp, type, participant, offline, items):
|
|
|
|
self.logger.debug("received receipt, sending ack: " +
|
|
|
|
' '.join(map(str, [_id, _from, timestamp,
|
|
|
|
type, participant, offline, items]))
|
|
|
|
)
|
2015-09-03 18:04:29 +00:00
|
|
|
buddy = self.buddies[_from.split('@')[0]]
|
|
|
|
self.backend.handleBuddyChanged(self.user, buddy.number.number,
|
|
|
|
buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
2015-09-03 18:04:29 +00:00
|
|
|
def onAck(self, _id, _class, _from, timestamp):
|
2015-08-30 18:51:44 +00:00
|
|
|
self.logger.debug('received ack ' +
|
|
|
|
' '.join(map(str, [_id, _class, _from,timestamp,]))
|
|
|
|
)
|
|
|
|
|
|
|
|
# Called by superclass
|
2015-09-05 20:05:34 +00:00
|
|
|
def onTextMessage(self, _id, _from, to, notify, timestamp, participant, offline, retry, body):
|
|
|
|
self.logger.debug('received TextMessage' +
|
|
|
|
' '.join(map(str, [
|
|
|
|
_id, _from, to, notify, timestamp,
|
|
|
|
participant, offline, retry, body
|
|
|
|
]))
|
|
|
|
)
|
|
|
|
buddy = _from.split('@')[0]
|
|
|
|
messageContent = utils.softToUni(body)
|
|
|
|
self.sendReceipt(_id, _from, None, participant)
|
|
|
|
self.logger.info("Message received from %s to %s: %s (at ts=%s)",
|
|
|
|
buddy, self.legacyName, messageContent, timestamp)
|
2015-09-06 12:06:16 +00:00
|
|
|
if participant is not None:
|
|
|
|
partname = participant.split('@')[0]
|
|
|
|
message = partname + ': ' + messageContent
|
|
|
|
self.sendMessageToXMPP(buddy, message, timestamp)
|
|
|
|
else:
|
|
|
|
self.sendMessageToXMPP(buddy, messageContent, timestamp)
|
2015-09-05 20:05:34 +00:00
|
|
|
# isBroadcast always returns false, I'm not sure how to get a broadcast
|
|
|
|
# message.
|
|
|
|
#if messageEntity.isBroadcast():
|
|
|
|
# self.logger.info("Broadcast received from %s to %s: %s (at ts=%s)",\
|
|
|
|
# buddy, self.legacyName, messageContent, timestamp)
|
|
|
|
# messageContent = "[Broadcast] " + messageContent
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onImage(self, image):
|
|
|
|
self.logger.debug('Received image message %s', str(image))
|
|
|
|
buddy = image._from.split('@')[0]
|
|
|
|
message = image.url + ' ' + image.caption
|
|
|
|
self.sendMessageToXMPP(buddy, message, image.timestamp)
|
|
|
|
self.sendReceipt(image._id, image._from, None, image.participant)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onAudio(self, audio):
|
|
|
|
self.logger.debug('Received audio message %s', str(audio))
|
|
|
|
buddy = audio._from.split('@')[0]
|
|
|
|
message = audio.url
|
|
|
|
self.sendMessageToXMPP(buddy, message, audio.timestamp)
|
|
|
|
self.sendReceipt(audio._id, audio._from, None, audio.participant)
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onVideo(self, video):
|
|
|
|
self.logger.debug('Received video message %s', str(video))
|
|
|
|
buddy = video._from.split('@')[0]
|
|
|
|
message = video.url
|
|
|
|
self.sendMessageToXMPP(buddy, message, video.timestamp)
|
|
|
|
self.sendReceipt(video._id, video._from, None, video.participant)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
|
|
|
self.logger.debug('received VCard' +
|
|
|
|
' '.join(map(str, [
|
|
|
|
_id, _from, name, card_data, to, notify, timestamp, participant
|
|
|
|
]))
|
|
|
|
)
|
|
|
|
buddy = _from.split("@")[0]
|
|
|
|
self.sendMessageToXMPP(buddy, "Received VCard (not implemented yet)")
|
|
|
|
self.sendMessageToXMPP(buddy, card_data)
|
|
|
|
self.transferFile(buddy, str(name), card_data)
|
|
|
|
self.sendReceipt(_id, _from, None, participant)
|
|
|
|
|
|
|
|
def transferFile(self, buddy, name, data):
|
|
|
|
# Not working
|
|
|
|
self.logger.debug('transfering file %s', name)
|
|
|
|
self.backend.handleFTStart(self.user, buddy, name, len(data))
|
|
|
|
self.backend.handleFTData(0, data)
|
|
|
|
self.backend.handleFTFinish(self.user, buddy, name, len(data), 0)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onContactTyping(self, buddy):
|
|
|
|
self.logger.info("Started typing: %s", buddy)
|
|
|
|
self.sendPresence(True)
|
|
|
|
self.backend.handleBuddyTyping(self.user, buddy)
|
|
|
|
|
|
|
|
if self.timer != None:
|
|
|
|
self.timer.cancel()
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onContactPaused(self, buddy):
|
|
|
|
self.logger.info("Paused typing: %s", buddy)
|
|
|
|
self.backend.handleBuddyTyped(self.user, buddy)
|
|
|
|
self.timer = Timer(3, self.backend.handleBuddyStoppedTyping, (self.user, buddy)).start()
|
|
|
|
def onPresenceReceived(self, _type, name, jid, lastseen):
|
|
|
|
self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen)
|
|
|
|
buddy = jid.split("@")[0]
|
|
|
|
# seems to be causing an error
|
|
|
|
# self.logger.info("Lastseen: %s %s", buddy, utils.ago(lastseen))
|
|
|
|
|
|
|
|
if buddy in self.presenceRequested:
|
|
|
|
timestamp = time.localtime(time.time() - lastseen)
|
|
|
|
timestring = time.strftime("%a, %d %b %Y %H:%M:%S", timestamp)
|
|
|
|
self.sendMessageToXMPP(buddy, "%s (%s)" % (timestring, utils.ago(lastseen)))
|
|
|
|
self.presenceRequested.remove(buddy)
|
|
|
|
|
|
|
|
if lastseen < 60:
|
|
|
|
self.onPresenceAvailable(buddy)
|
|
|
|
else:
|
|
|
|
self.onPresenceUnavailable(buddy)
|
|
|
|
|
|
|
|
def onPresenceAvailable(self, buddy):
|
|
|
|
try:
|
|
|
|
buddy = self.buddies[buddy]
|
|
|
|
self.logger.info("Is available: %s", buddy)
|
|
|
|
self.backend.handleBuddyChanged(self.user, buddy.number.number, buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE)
|
|
|
|
except KeyError:
|
|
|
|
self.logger.error("Buddy not found: %s", buddy)
|
|
|
|
|
|
|
|
def onPresenceUnavailable(self, buddy):
|
|
|
|
try:
|
|
|
|
buddy = self.buddies[buddy]
|
|
|
|
self.logger.info("Is unavailable: %s", buddy)
|
|
|
|
self.backend.handleBuddyChanged(self.user, buddy.number.number, buddy.nick, buddy.groups, protocol_pb2.STATUS_XA)
|
|
|
|
except KeyError:
|
|
|
|
self.logger.error("Buddy not found: %s", buddy)
|
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
# spectrum RequestMethods
|
2013-05-31 18:53:21 +00:00
|
|
|
def sendTypingStarted(self, buddy):
|
2013-06-28 20:46:56 +00:00
|
|
|
if buddy != "bot":
|
|
|
|
self.logger.info("Started typing: %s to %s", self.legacyName, buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
self.sendTyping(buddy, True)
|
|
|
|
# If he is typing he is present
|
|
|
|
# I really don't know where else to put this.
|
|
|
|
# Ideally, this should be sent if the user is looking at his client
|
|
|
|
self.sendPresence(True)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def sendTypingStopped(self, buddy):
|
2013-06-28 20:46:56 +00:00
|
|
|
if buddy != "bot":
|
|
|
|
self.logger.info("Stopped typing: %s to %s", self.legacyName, buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
self.sendTyping(buddy, False)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def sendMessageToWA(self, sender, message):
|
|
|
|
self.logger.info("Message sent from %s to %s: %s", self.legacyName, sender, message)
|
2013-05-31 18:53:21 +00:00
|
|
|
message = message.encode("utf-8")
|
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
if sender == "bot":
|
|
|
|
self.bot.parse(message)
|
|
|
|
elif "-" in sender: # group msg
|
|
|
|
if "/" in sender:
|
|
|
|
room, buddy = sender.split("/")
|
|
|
|
self.sendTextMessage(buddy + '@s.whatsapp.net', message)
|
|
|
|
else:
|
|
|
|
room = sender
|
2015-08-30 20:18:03 +00:00
|
|
|
# group = self.groups[room]
|
2015-09-06 12:06:16 +00:00
|
|
|
|
2015-08-30 20:18:03 +00:00
|
|
|
# self.backend.handleMessage(self.user, room, message, group.nick)
|
2015-09-06 12:06:16 +00:00
|
|
|
self.sendTextMessage(room + '@g.us', message)
|
|
|
|
|
|
|
|
else: # private msg
|
|
|
|
buddy = sender
|
2015-08-30 20:18:03 +00:00
|
|
|
# if message == "\\lastseen":
|
|
|
|
# self.call("presence_request", buddy = (buddy + "@s.whatsapp.net",))
|
|
|
|
# else:
|
2015-09-06 12:06:16 +00:00
|
|
|
self.sendTextMessage(sender + '@s.whatsapp.net', message)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
def sendMessageToXMPP(self, buddy, messageContent, timestamp = "", nickname = ""):
|
2015-06-21 14:11:43 +00:00
|
|
|
if timestamp:
|
|
|
|
timestamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime(timestamp))
|
|
|
|
|
|
|
|
if self.initialized == False:
|
|
|
|
self.logger.debug("Message queued from %s to %s: %s",
|
|
|
|
buddy, self.legacyName, messageContent)
|
|
|
|
self.offlineQueue.append((buddy, messageContent, timestamp))
|
|
|
|
else:
|
|
|
|
self.logger.debug("Message sent from %s to %s: %s", buddy,
|
|
|
|
self.legacyName, messageContent)
|
|
|
|
self.backend.handleMessage(self.user, buddy, messageContent, "",
|
|
|
|
"", timestamp)
|
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def sendGroupMessageToXMPP(self, room, buddy, messageContent, timestamp = ""):
|
|
|
|
if timestamp:
|
|
|
|
timestamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime(timestamp))
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
if self.initialized == False:
|
|
|
|
self.logger.debug("Group message queued from %s to %s: %s", buddy, room, messageContent)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
if room not in self.groupOfflineQueue:
|
|
|
|
self.groupOfflineQueue[room] = [ ]
|
|
|
|
|
|
|
|
self.groupOfflineQueue[room].append((buddy, messageContent, timestamp))
|
2013-05-31 18:53:21 +00:00
|
|
|
else:
|
2013-06-28 20:46:56 +00:00
|
|
|
self.logger.debug("Group message sent from %s to %s: %s", buddy, room, messageContent)
|
|
|
|
self.backend.handleMessage(self.user, room, messageContent, buddy, "", timestamp)
|
|
|
|
|
|
|
|
def changeStatus(self, status):
|
|
|
|
if status != self.status:
|
|
|
|
self.logger.info("Status changed: %s", status)
|
|
|
|
self.status = status
|
|
|
|
|
|
|
|
if status == protocol_pb2.STATUS_ONLINE or status == protocol_pb2.STATUS_FFC:
|
2015-09-02 15:04:49 +00:00
|
|
|
self.sendPresence(True)
|
2013-06-28 20:46:56 +00:00
|
|
|
else:
|
2015-09-02 15:04:49 +00:00
|
|
|
self.sendPresence(False)
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
def changeStatusMessage(self, statusMessage):
|
2013-08-06 19:21:37 +00:00
|
|
|
if (statusMessage != self.statusMessage) or (self.initialized == False):
|
2013-06-28 20:46:56 +00:00
|
|
|
self.statusMessage = statusMessage
|
2015-09-02 15:04:49 +00:00
|
|
|
self.setStatus(statusMessage.encode('utf-8'))
|
2013-06-28 20:46:56 +00:00
|
|
|
self.logger.info("Status message changed: %s", statusMessage)
|
|
|
|
|
|
|
|
if self.initialized == False:
|
|
|
|
self.sendOfflineMessages()
|
|
|
|
self.bot.call("welcome")
|
|
|
|
self.initialized = True
|
|
|
|
|
|
|
|
def sendOfflineMessages(self):
|
|
|
|
# Flush Queues
|
|
|
|
while self.offlineQueue:
|
|
|
|
msg = self.offlineQueue.pop(0)
|
|
|
|
self.backend.handleMessage(self.user, msg[0], msg[1], "", "", msg[2])
|
|
|
|
|
|
|
|
# also for adding a new buddy
|
2015-09-05 22:14:12 +00:00
|
|
|
def updateBuddy(self, buddy, nick, groups, image_hash =""):
|
2013-06-28 20:46:56 +00:00
|
|
|
if buddy != "bot":
|
2015-09-05 22:14:12 +00:00
|
|
|
self.buddies.update(buddy, nick, groups, image_hash)
|
2013-06-28 20:46:56 +00:00
|
|
|
self.updateRoster()
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def removeBuddy(self, buddy):
|
2013-06-28 20:46:56 +00:00
|
|
|
if buddy != "bot":
|
|
|
|
self.logger.info("Buddy removed: %s", buddy)
|
|
|
|
self.buddies.remove(buddy)
|
|
|
|
self.updateRoster()
|
|
|
|
|
|
|
|
def joinRoom(self, room, nick):
|
|
|
|
if room in self.groups:
|
|
|
|
group = self.groups[room]
|
|
|
|
|
|
|
|
self.logger.info("Joining room: %s room=%s, nick=%s", self.legacyName, room, nick)
|
|
|
|
|
|
|
|
group.nick = nick
|
|
|
|
|
|
|
|
self.call("group_getParticipants", (room + "@g.us",))
|
|
|
|
self.backend.handleSubject(self.user, room, group.subject, group.subjectOwner)
|
|
|
|
else:
|
|
|
|
self.logger.warn("Room doesn't exist: %s", room)
|
2015-09-05 22:14:12 +00:00
|
|
|
|
|
|
|
def requestVCard(self, buddy, ID):
|
|
|
|
|
|
|
|
def onSuccess(response, request):
|
|
|
|
self.logger.debug('Sending VCard (%s) with image id %s',
|
|
|
|
ID, response.pictureId)
|
|
|
|
image_hash = utils.sha1hash(response.pictureData)
|
|
|
|
self.logger.debug('Image hash is %s', image_hash)
|
|
|
|
self.backend.handleVCard(self.user, ID, buddy, "", "", response.pictureData)
|
|
|
|
obuddy = self.buddies[buddy]
|
|
|
|
self.updateBuddy(buddy, obuddy.nick, obuddy.groups, image_hash)
|
|
|
|
|
|
|
|
self.logger.debug('Requesting profile picture of %s', buddy)
|
|
|
|
self.requestProfilePicture(buddy, onSuccess = onSuccess)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-09-05 20:22:30 +00:00
|
|
|
# Not used
|
2013-05-31 18:53:21 +00:00
|
|
|
def onLocationReceived(self, messageId, jid, name, preview, latitude, longitude, receiptRequested, isBroadcast):
|
|
|
|
buddy = jid.split("@")[0]
|
2013-06-28 20:46:56 +00:00
|
|
|
self.logger.info("Location received from %s: %s, %s", buddy, latitude, longitude)
|
|
|
|
|
|
|
|
url = "http://maps.google.de?%s" % urllib.urlencode({ "q": "%s %s" % (latitude, longitude) })
|
|
|
|
self.sendMessageToXMPP(buddy, utils.shorten(url))
|
2013-05-31 18:53:21 +00:00
|
|
|
if receiptRequested: self.call("message_ack", (jid, messageId))
|
|
|
|
|
|
|
|
|
|
|
|
def onGroupGotParticipants(self, gjid, jids):
|
|
|
|
room = gjid.split("@")[0]
|
2013-06-28 20:46:56 +00:00
|
|
|
group = self.groups[room]
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
for jid in jids:
|
|
|
|
buddy = jid.split("@")[0]
|
2013-06-28 20:46:56 +00:00
|
|
|
self.logger.info("Added %s to room %s", buddy, room)
|
|
|
|
|
|
|
|
if buddy == group.owner:
|
|
|
|
flags = protocol_pb2.PARTICIPANT_FLAG_MODERATOR
|
|
|
|
else:
|
|
|
|
flags = protocol_pb2.PARTICIPANT_FLAG_NONE
|
|
|
|
|
|
|
|
self.backend.handleParticipantChanged(self.user, buddy, room, flags, protocol_pb2.STATUS_ONLINE) # TODO check status
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
if room in self.groupOfflineQueue:
|
|
|
|
while self.groupOfflineQueue[room]:
|
|
|
|
msg = self.groupOfflineQueue[room].pop(0)
|
|
|
|
self.backend.handleMessage(self.user, room, msg[1], msg[0], "", msg[2])
|
|
|
|
self.logger.debug("Send queued group message to: %s %s %s", msg[0],msg[1], msg[2])
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def onGroupMessageReceived(self, messageId, gjid, jid, messageContent, timestamp, receiptRequested, pushName):
|
|
|
|
buddy = jid.split("@")[0]
|
|
|
|
room = gjid.split("@")[0]
|
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
self.logger.info("Group message received in %s from %s: %s", room, buddy, messageContent)
|
|
|
|
|
|
|
|
self.sendGroupMessageToXMPP(room, buddy, utils.softToUni(messageContent), timestamp)
|
2013-05-31 18:53:21 +00:00
|
|
|
if receiptRequested: self.call("message_ack", (gjid, messageId))
|
|
|
|
|
|
|
|
def onGroupSubjectReceived(self, messageId, gjid, jid, subject, timestamp, receiptRequested):
|
|
|
|
room = gjid.split("@")[0]
|
|
|
|
buddy = jid.split("@")[0]
|
|
|
|
|
|
|
|
self.backend.handleSubject(self.user, room, subject, buddy)
|
|
|
|
if receiptRequested: self.call("subject_ack", (gjid, messageId))
|
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
# Yowsup Notifications
|
|
|
|
def onGroupParticipantRemoved(self, gjid, jid, author, timestamp, messageId, receiptRequested):
|
|
|
|
room = gjid.split("@")[0]
|
|
|
|
buddy = jid.split("@")[0]
|
|
|
|
|
|
|
|
self.logger.info("Removed %s from room %s", buddy, room)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
self.backend.handleParticipantChanged(self.user, buddy, room, protocol_pb2.PARTICIPANT_FLAG_NONE, protocol_pb2.STATUS_NONE) # TODO
|
2013-06-28 20:46:56 +00:00
|
|
|
if receiptRequested: self.call("notification_ack", (gjid, messageId))
|
|
|
|
|
|
|
|
def onContactProfilePictureUpdated(self, jid, timestamp, messageId, pictureId, receiptRequested):
|
|
|
|
# TODO
|
|
|
|
if receiptRequested: self.call("notification_ack", (jid, messageId))
|
|
|
|
|
|
|
|
def onGroupPictureUpdated(self, jid, author, timestamp, messageId, pictureId, receiptRequested):
|
|
|
|
# TODO
|
|
|
|
if receiptRequested: self.call("notification_ack", (jid, messageId))
|