2015-12-31 19:43:10 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
2013-08-01 23:45:51 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2013-08-01 23:45:51 +00:00
|
|
|
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
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
from PIL import Image
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
from yowsup.layers.protocol_media.mediauploader import MediaUploader
|
|
|
|
from yowsup.layers.protocol_media.mediadownloader import MediaDownloader
|
|
|
|
|
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
|
2016-01-01 15:31:17 +00:00
|
|
|
import deferred
|
2016-01-02 14:00:47 +00:00
|
|
|
from deferred import call
|
2015-08-30 18:51:44 +00:00
|
|
|
from yowsupwrapper import YowsupApp
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
|
|
|
|
class MsgIDs:
|
|
|
|
def __init__(self, xmppId, waId):
|
|
|
|
self.xmppId = xmppId
|
|
|
|
self.waId = waId
|
|
|
|
self.cnt = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
class Session(YowsupApp):
|
2016-04-09 22:21:58 +00:00
|
|
|
broadcast_prefix = '\U0001F4E2 '
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-12-02 21:06:56 +00:00
|
|
|
def __init__(self, backend, user, legacyName, extra):
|
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__)
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Created: %s" % legacyName)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
self.backend = backend
|
|
|
|
self.user = user
|
2013-06-28 20:46:56 +00:00
|
|
|
self.legacyName = legacyName
|
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
self.status = protocol_pb2.STATUS_NONE
|
2015-12-31 19:43:10 +00:00
|
|
|
self.statusMessage = ''
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
self.groups = {}
|
2015-10-26 16:47:17 +00:00
|
|
|
self.gotGroupList = False
|
2015-12-02 17:32:55 +00:00
|
|
|
# Functions to exectute when logged in via yowsup
|
|
|
|
self.loginQueue = []
|
2015-10-26 16:47:17 +00:00
|
|
|
self.joinRoomQueue = []
|
2013-06-28 20:46:56 +00:00
|
|
|
self.presenceRequested = []
|
|
|
|
self.offlineQueue = []
|
2015-09-15 19:29:26 +00:00
|
|
|
self.msgIDs = { }
|
2013-06-28 20:46:56 +00:00
|
|
|
self.groupOfflineQueue = { }
|
2015-12-02 17:32:55 +00:00
|
|
|
self.loggedIn = False
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs = []
|
2013-06-28 20:46:56 +00:00
|
|
|
|
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-11-09 22:09:12 +00:00
|
|
|
self.lastMsgId = None
|
2015-09-15 19:29:26 +00:00
|
|
|
self.synced = False
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-12-01 21:21:38 +00:00
|
|
|
self.buddies = BuddyList(self.legacyName, self.backend, self.user, self)
|
2013-06-28 20:46:56 +00:00
|
|
|
self.bot = Bot(self)
|
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
self.imgMsgId = None
|
2015-12-31 19:43:10 +00:00
|
|
|
self.imgPath = ""
|
2015-11-23 15:06:14 +00:00
|
|
|
self.imgBuddy = None
|
2015-12-31 19:43:10 +00:00
|
|
|
self.imgType = ""
|
2015-11-09 22:09:12 +00:00
|
|
|
|
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def __del__(self): # handleLogoutRequest
|
|
|
|
self.logout()
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def logout(self):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("%s logged out" % self.user)
|
2015-08-30 18:51:44 +00:00
|
|
|
super(Session, self).logout()
|
2015-12-02 17:32:55 +00:00
|
|
|
self.loggedIn = False
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
def login(self, password):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("%s attempting login" % self.user)
|
2015-06-21 19:24:36 +00:00
|
|
|
self.password = password
|
2015-09-15 19:29:26 +00:00
|
|
|
self.shouldBeConncted = True
|
2015-08-30 18:51:44 +00:00
|
|
|
super(Session, self).login(self.legacyName, self.password)
|
2013-06-28 20:46:56 +00:00
|
|
|
|
2015-09-21 18:30:11 +00:00
|
|
|
def _shortenGroupId(self, gid):
|
2015-10-26 16:47:17 +00:00
|
|
|
# FIXME: might have problems if number begins with 0
|
|
|
|
return gid
|
2015-12-31 19:43:10 +00:00
|
|
|
# return '-'.join(hex(int(s))[2:] for s in gid.split('-'))
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2015-09-21 18:30:11 +00:00
|
|
|
def _lengthenGroupId(self, gid):
|
2015-11-09 22:09:12 +00:00
|
|
|
return gid
|
2015-10-26 16:47:17 +00:00
|
|
|
# FIXME: might have problems if number begins with 0
|
2015-12-31 19:43:10 +00:00
|
|
|
# return '-'.join(str(int(s, 16)) for s in gid.split('-'))
|
2015-09-21 18:30:11 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
def updateRoomList(self):
|
|
|
|
rooms = []
|
2015-10-26 16:47:17 +00:00
|
|
|
text = []
|
2013-06-28 20:46:56 +00:00
|
|
|
for room, group in self.groups.iteritems():
|
2015-09-21 18:30:11 +00:00
|
|
|
rooms.append([self._shortenGroupId(room), group.subject])
|
2015-12-31 19:43:10 +00:00
|
|
|
text.append(self._shortenGroupId(room) + '@' + self.backend.spectrum_jid + ' :' + group.subject)
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Got rooms: %s" % rooms)
|
2013-06-28 20:46:56 +00:00
|
|
|
self.backend.handleRoomList(rooms)
|
2016-04-09 22:21:58 +00:00
|
|
|
message = "Note, you are a participant of the following groups:\n" + \
|
|
|
|
"\n".join(text) + "\nIf you do not join them you will lose messages"
|
2015-11-10 13:00:38 +00:00
|
|
|
#self.bot.send(message)
|
2013-06-28 20:46:56 +00:00
|
|
|
|
2015-09-06 12:06:16 +00:00
|
|
|
def _updateGroups(self, response, request):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('Received groups list %s' % response)
|
2015-09-06 12:06:16 +00:00
|
|
|
groups = response.getGroups()
|
|
|
|
for group in groups:
|
|
|
|
room = group.getId()
|
2015-12-31 19:43:10 +00:00
|
|
|
owner = group.getOwner().split('@')[0]
|
|
|
|
subjectOwner = group.getSubjectOwner().split('@')[0]
|
2015-09-15 19:29:26 +00:00
|
|
|
subject = utils.softToUni(group.getSubject())
|
2015-09-06 12:06:16 +00:00
|
|
|
|
|
|
|
if room in self.groups:
|
|
|
|
oroom = self.groups[room]
|
|
|
|
oroom.owner = owner
|
|
|
|
oroom.subjectOwner = subjectOwner
|
|
|
|
oroom.subject = subject
|
|
|
|
else:
|
2016-01-01 09:42:11 +00:00
|
|
|
self.groups[room] = Group(room, owner, subject, subjectOwner, self.backend, self.user)
|
2015-12-31 19:43:10 +00:00
|
|
|
# self.joinRoom(self._shortenGroupId(room), self.user.split("@")[0])
|
2016-01-01 09:42:11 +00:00
|
|
|
self.groups[room].addParticipants(group.getParticipants().keys(),
|
|
|
|
self.buddies, self.legacyName)
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2015-10-26 16:47:17 +00:00
|
|
|
#self._addParticipantsToRoom(room, group.getParticipants())
|
2015-09-15 19:29:26 +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],
|
2015-12-31 19:43:10 +00:00
|
|
|
msg[0], "", msg[2])
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Send queued group message to: %s %s %s" %
|
|
|
|
(msg[0],msg[1], msg[2]))
|
2015-10-26 16:47:17 +00:00
|
|
|
self.gotGroupList = True
|
|
|
|
for room, nick in self.joinRoomQueue:
|
|
|
|
self.joinRoom(room, nick)
|
|
|
|
self.joinRoomQueue = []
|
2015-09-15 19:29:26 +00:00
|
|
|
self.updateRoomList()
|
|
|
|
|
|
|
|
def joinRoom(self, room, nick):
|
2015-10-26 16:47:17 +00:00
|
|
|
if not self.gotGroupList:
|
|
|
|
self.joinRoomQueue.append((room, nick))
|
|
|
|
return
|
2015-09-21 18:30:11 +00:00
|
|
|
room = self._lengthenGroupId(room)
|
2015-09-15 19:29:26 +00:00
|
|
|
if room in self.groups:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Joining room: %s room=%s, nick=%s" %
|
|
|
|
(self.legacyName, room, nick))
|
2015-09-06 12:06:16 +00:00
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
group = self.groups[room]
|
2016-01-01 09:42:11 +00:00
|
|
|
group.joined = True
|
2015-09-15 19:29:26 +00:00
|
|
|
group.nick = nick
|
2016-01-01 09:42:11 +00:00
|
|
|
group.participants[self.legacyName] = nick
|
2015-09-15 19:29:26 +00:00
|
|
|
try:
|
2016-01-01 09:42:11 +00:00
|
|
|
ownerNick = group.participants[group.subjectOwner]
|
2015-09-15 19:29:26 +00:00
|
|
|
except KeyError:
|
|
|
|
ownerNick = group.subjectOwner
|
|
|
|
|
2016-01-01 09:42:11 +00:00
|
|
|
group.sendParticipantsToSpectrum(self.legacyName)
|
2015-09-22 14:14:25 +00:00
|
|
|
self.backend.handleSubject(self.user, self._shortenGroupId(room),
|
2015-11-10 12:24:40 +00:00
|
|
|
group.subject, ownerNick)
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Room subject: room=%s, subject=%s" %
|
|
|
|
(room, group.subject))
|
2015-09-22 14:14:25 +00:00
|
|
|
self.backend.handleRoomNicknameChanged(
|
|
|
|
self.user, self._shortenGroupId(room), group.subject
|
|
|
|
)
|
2015-09-15 19:29:26 +00:00
|
|
|
else:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.warn("Room doesn't exist: %s" % room)
|
2015-09-06 12:06:16 +00:00
|
|
|
|
2015-11-23 15:06:14 +00:00
|
|
|
def leaveRoom(self, room):
|
|
|
|
if room in self.groups:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Leaving room: %s room=%s" % (self.legacyName, room))
|
2015-11-23 15:06:14 +00:00
|
|
|
group = self.groups[room]
|
|
|
|
group.joined = False
|
|
|
|
else:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.warn("Room doesn't exist: %s. Unable to leave." % room)
|
2015-12-31 19:43:10 +00:00
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def _lastSeen(self, number, seconds):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Last seen %s at %s seconds" % (number, seconds))
|
2015-09-03 18:04:29 +00:00
|
|
|
if seconds < 60:
|
|
|
|
self.onPresenceAvailable(number)
|
|
|
|
else:
|
|
|
|
self.onPresenceUnavailable(number)
|
2016-02-19 16:31:34 +00:00
|
|
|
def sendReadReceipts(self, buddy):
|
|
|
|
for _id, _from, participant in self.recvMsgIDs:
|
2016-02-19 17:12:47 +00:00
|
|
|
if _from.split('@')[0] == buddy:
|
2016-02-19 16:31:34 +00:00
|
|
|
self.sendReceipt(_id, _from, 'read', participant)
|
|
|
|
self.recvMsgIDs.remove((_id, _from, participant))
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Send read receipt to %s (ID: %s)" % (_from, _id))
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onAuthSuccess(self, status, kind, creation,
|
|
|
|
expiration, props, nonce, t):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Auth success: %s" % self.user)
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
self.backend.handleConnected(self.user)
|
2015-12-31 19:43:10 +00:00
|
|
|
self.backend.handleBuddyChanged(self.user, "bot", self.bot.name,
|
2016-04-09 22:21:58 +00:00
|
|
|
["Admin"], protocol_pb2.STATUS_ONLINE)
|
2016-01-08 13:30:47 +00:00
|
|
|
# Initialisation?
|
|
|
|
self.requestPrivacyList()
|
|
|
|
self.requestClientConfig()
|
|
|
|
self.requestServerProperties()
|
|
|
|
# ?
|
|
|
|
|
|
|
|
self.logger.debug('Requesting groups list')
|
|
|
|
self.requestGroupsList(self._updateGroups)
|
|
|
|
# self.requestBroadcastList()
|
|
|
|
|
|
|
|
# This should handle, sync, statuses, and presence
|
|
|
|
self.sendPresence(True)
|
|
|
|
for func in self.loginQueue:
|
|
|
|
func()
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
if self.initialized == False:
|
|
|
|
self.sendOfflineMessages()
|
2015-12-31 19:43:10 +00:00
|
|
|
#self.bot.call("welcome")
|
2015-09-15 19:29:26 +00:00
|
|
|
self.initialized = True
|
2015-08-30 18:51:44 +00:00
|
|
|
|
2015-12-02 17:32:55 +00:00
|
|
|
self.loggedIn = True
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onAuthFailed(self, reason):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Auth failed: %s (%s)" % (self.user, reason))
|
2015-08-30 18:51:44 +00:00
|
|
|
self.backend.handleDisconnected(self.user, 0, reason)
|
|
|
|
self.password = None
|
2015-12-02 17:32:55 +00:00
|
|
|
self.loggedIn = False
|
2015-09-21 18:30:11 +00:00
|
|
|
|
2015-08-30 18:51:44 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onDisconnect(self):
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug('Disconnected')
|
|
|
|
self.backend.handleDisconnected(self.user, 0, 'Disconnected for unknown reasons')
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onReceipt(self, _id, _from, timestamp, type, participant, offline, items):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("received receipt, sending ack: %s" %
|
|
|
|
[ _id, _from, timestamp, type, participant, offline, items ]
|
2015-08-30 18:51:44 +00:00
|
|
|
)
|
2015-09-21 18:30:11 +00:00
|
|
|
try:
|
2016-01-04 08:14:02 +00:00
|
|
|
number = _from.split('@')[0]
|
|
|
|
self.backend.handleMessageAck(self.user, number, self.msgIDs[_id].xmppId)
|
|
|
|
self.msgIDs[_id].cnt = self.msgIDs[_id].cnt + 1
|
|
|
|
if self.msgIDs[_id].cnt == 2:
|
|
|
|
del self.msgIDs[_id]
|
2015-09-21 18:30:11 +00:00
|
|
|
except KeyError:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.error("Message %s not found. Unable to send ack" % _id)
|
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):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('received ack: %s' % [ _id, _class, _from, timestamp ])
|
2015-08-30 18:51:44 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
2015-09-15 19:29:26 +00:00
|
|
|
def onTextMessage(self, _id, _from, to, notify, timestamp, participant,
|
|
|
|
offline, retry, body):
|
2016-04-09 22:21:58 +00:00
|
|
|
|
|
|
|
self.logger.debug('received TextMessage: %s' %
|
|
|
|
[_id, _from, to, notify, timestamp,
|
|
|
|
participant, offline, retry, body]
|
2015-09-05 20:05:34 +00:00
|
|
|
)
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = _from.split('@')[0]
|
2015-09-05 20:05:34 +00:00
|
|
|
messageContent = utils.softToUni(body)
|
2015-09-22 14:14:25 +00:00
|
|
|
self.sendReceipt(_id, _from, None, participant)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs.append((_id, _from, participant))
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Message received from %s to %s: %s (at ts=%s)" %
|
|
|
|
(buddy, self.legacyName, messageContent, timestamp))
|
2016-01-06 16:31:42 +00:00
|
|
|
if participant is not None: # Group message or broadcast
|
2015-12-31 19:43:10 +00:00
|
|
|
partname = participant.split('@')[0]
|
2016-01-06 16:31:42 +00:00
|
|
|
if _from.split('@')[1] == 'broadcast': # Broadcast message
|
|
|
|
message = self.broadcast_prefix + messageContent
|
|
|
|
self.sendMessageToXMPP(partname, message, timestamp)
|
|
|
|
else: # Group message
|
|
|
|
if notify is None:
|
|
|
|
notify = ""
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, messageContent,
|
|
|
|
timestamp, notify)
|
2015-09-06 12:06:16 +00:00
|
|
|
else:
|
|
|
|
self.sendMessageToXMPP(buddy, messageContent, timestamp)
|
2015-09-05 20:05:34 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onImage(self, image):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('Received image message: %s' % image)
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = image._from.split('@')[0]
|
2015-11-09 22:09:12 +00:00
|
|
|
participant = image.participant
|
2015-11-05 15:57:16 +00:00
|
|
|
if image.caption is None:
|
2016-01-07 19:12:16 +00:00
|
|
|
image.caption = ''
|
2015-11-09 22:09:12 +00:00
|
|
|
if participant is not None: # Group message
|
2016-01-06 16:31:42 +00:00
|
|
|
partname = participant.split('@')[0]
|
|
|
|
if image._from.split('@')[1] == 'broadcast': # Broadcast message
|
|
|
|
self.sendMessageToXMPP(partname, self.broadcast_prefix, image.timestamp)
|
2016-01-07 19:12:16 +00:00
|
|
|
self.sendMessageToXMPP(partname, image.url, image.timestamp)
|
|
|
|
self.sendMessageToXMPP(partname, image.caption, image.timestamp)
|
2016-01-06 16:31:42 +00:00
|
|
|
else: # Group message
|
2016-01-07 19:12:16 +00:00
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, image.url, image.timestamp)
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, image.caption, image.timestamp)
|
2016-01-06 16:31:42 +00:00
|
|
|
else:
|
2016-01-07 19:12:16 +00:00
|
|
|
self.sendMessageToXMPP(buddy, image.url, image.timestamp)
|
|
|
|
self.sendMessageToXMPP(buddy, image.caption, image.timestamp)
|
2015-09-15 19:29:26 +00:00
|
|
|
self.sendReceipt(image._id, image._from, None, image.participant)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs.append((image._id, image._from, 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):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('Received audio message: %s' % audio)
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = audio._from.split('@')[0]
|
2015-11-09 22:09:12 +00:00
|
|
|
participant = audio.participant
|
2015-09-05 20:05:34 +00:00
|
|
|
message = audio.url
|
2015-11-09 22:09:12 +00:00
|
|
|
if participant is not None: # Group message
|
2016-01-06 16:31:42 +00:00
|
|
|
partname = participant.split('@')[0]
|
|
|
|
if audio._from.split('@')[1] == 'broadcast': # Broadcast message
|
|
|
|
self.sendMessageToXMPP(partname, self.broadcast_prefix, audio.timestamp)
|
|
|
|
self.sendMessageToXMPP(partname, message, audio.timestamp)
|
|
|
|
else: # Group message
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, message, audio.timestamp)
|
|
|
|
else:
|
2015-11-09 22:09:12 +00:00
|
|
|
self.sendMessageToXMPP(buddy, message, audio.timestamp)
|
2015-09-15 19:29:26 +00:00
|
|
|
self.sendReceipt(audio._id, audio._from, None, audio.participant)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs.append((audio._id, audio._from, audio.participant))
|
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onVideo(self, video):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('Received video message: %s' % video)
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = video._from.split('@')[0]
|
2015-11-09 22:09:12 +00:00
|
|
|
participant = video.participant
|
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
message = video.url
|
2015-11-09 22:09:12 +00:00
|
|
|
if participant is not None: # Group message
|
2016-01-06 16:31:42 +00:00
|
|
|
partname = participant.split('@')[0]
|
|
|
|
if video._from.split('@')[1] == 'broadcast': # Broadcast message
|
|
|
|
self.sendMessageToXMPP(partname, self.broadcast_prefix, video.timestamp)
|
|
|
|
self.sendMessageToXMPP(partname, message, video.timestamp)
|
|
|
|
else: # Group message
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, message, video.timestamp)
|
|
|
|
else:
|
2015-11-09 22:09:12 +00:00
|
|
|
self.sendMessageToXMPP(buddy, message, video.timestamp)
|
2015-09-15 19:29:26 +00:00
|
|
|
self.sendReceipt(video._id, video._from, None, video.participant)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs.append((video._id, video._from, video.participant))
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
|
|
|
|
def onLocation(self, location):
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = location._from.split('@')[0]
|
2015-09-15 19:29:26 +00:00
|
|
|
latitude = location.getLatitude()
|
|
|
|
longitude = location.getLongitude()
|
2016-01-06 16:31:42 +00:00
|
|
|
url = location.getLocationURL()
|
2015-11-09 22:09:12 +00:00
|
|
|
participant = location.participant
|
2016-01-06 16:31:42 +00:00
|
|
|
latlong = 'geo:' + latitude + ',' + longitude
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Location received from %s: %s, %s" %
|
|
|
|
(buddy, latitude, longitude))
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-01-06 16:31:42 +00:00
|
|
|
if participant is not None: # Group message
|
|
|
|
partname = participant.split('@')[0]
|
|
|
|
if location._from.split('@')[1] == 'broadcast': # Broadcast message
|
|
|
|
self.sendMessageToXMPP(partname, self.broadcast_prefix, location.timestamp)
|
|
|
|
if url is not None:
|
|
|
|
self.sendMessageToXMPP(partname, url, location.timestamp)
|
|
|
|
self.sendMessageToXMPP(partname, latlong, location.timestamp)
|
|
|
|
else: # Group message
|
|
|
|
if url is not None:
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, url, location.timestamp)
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, latlong, location.timestamp)
|
|
|
|
else:
|
|
|
|
if url is not None:
|
|
|
|
self.sendMessageToXMPP(buddy, url, location.timestamp)
|
|
|
|
self.sendMessageToXMPP(buddy, latlong, location.timestamp)
|
|
|
|
self.sendReceipt(location._id, location._from, None, location.participant)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs.append((loaction._id, location._from, location.participant))
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
|
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):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('received VCard: %s' %
|
|
|
|
[ _id, _from, name, card_data, to, notify, timestamp, participant ]
|
2015-09-05 20:05:34 +00:00
|
|
|
)
|
2016-01-06 16:31:42 +00:00
|
|
|
message = "Received VCard (not implemented yet)"
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = _from.split("@")[0]
|
2015-11-09 22:09:12 +00:00
|
|
|
if participant is not None: # Group message
|
2016-01-06 16:31:42 +00:00
|
|
|
partname = participant.split('@')[0]
|
|
|
|
if _from.split('@')[1] == 'broadcast': # Broadcast message
|
|
|
|
message = self.broadcast_prefix + message
|
|
|
|
self.sendMessageToXMPP(partname, message, timestamp)
|
|
|
|
else: # Group message
|
|
|
|
self.sendGroupMessageToXMPP(buddy, partname, message, timestamp)
|
|
|
|
else:
|
|
|
|
self.sendMessageToXMPP(buddy, message, timestamp)
|
2015-09-15 19:29:26 +00:00
|
|
|
# self.sendMessageToXMPP(buddy, card_data)
|
2015-11-09 22:09:12 +00:00
|
|
|
#self.transferFile(buddy, str(name), card_data)
|
2015-09-05 20:05:34 +00:00
|
|
|
self.sendReceipt(_id, _from, None, participant)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.recvMsgIDs.append((_id, _from, participant))
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2015-09-05 20:05:34 +00:00
|
|
|
def transferFile(self, buddy, name, data):
|
|
|
|
# Not working
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('transfering file: %s' % name)
|
2015-09-05 20:05:34 +00:00
|
|
|
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):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Started typing: %s" % buddy)
|
2015-12-31 19:43:10 +00:00
|
|
|
if buddy != 'bot':
|
2015-09-15 19:29:26 +00:00
|
|
|
self.sendPresence(True)
|
|
|
|
self.backend.handleBuddyTyping(self.user, buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
if self.timer != None:
|
|
|
|
self.timer.cancel()
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onContactPaused(self, buddy):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Paused typing: %s" % buddy)
|
2015-12-31 19:43:10 +00:00
|
|
|
if buddy != 'bot':
|
2015-09-15 19:29:26 +00:00
|
|
|
self.backend.handleBuddyTyped(self.user, buddy)
|
|
|
|
self.timer = Timer(3, self.backend.handleBuddyStoppedTyping,
|
|
|
|
(self.user, buddy)).start()
|
|
|
|
|
2015-10-26 16:47:17 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onAddedToGroup(self, group):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Added to group: %s" % group)
|
2015-10-26 16:47:17 +00:00
|
|
|
room = group.getGroupId()
|
|
|
|
owner = group.getCreatorJid(full = False)
|
|
|
|
subjectOwner = group.getSubjectOwnerJid(full = False)
|
|
|
|
subject = utils.softToUni(group.getSubject())
|
|
|
|
|
2016-01-01 09:42:11 +00:00
|
|
|
self.groups[room] = Group(room, owner, subject, subjectOwner, self.backend, self.user)
|
|
|
|
self.groups[room].addParticipants(group.getParticipants, self.buddies, self.legacyName)
|
2015-12-31 19:43:10 +00:00
|
|
|
self.bot.send("You have been added to group: %s@%s (%s)"
|
2015-10-26 16:47:17 +00:00
|
|
|
% (self._shortenGroupId(room), subject, self.backend.spectrum_jid))
|
|
|
|
|
2015-11-29 14:32:33 +00:00
|
|
|
# Called by superclass
|
2015-10-26 16:47:17 +00:00
|
|
|
def onParticipantsAddedToGroup(self, group):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Participants added to group: %s" % group)
|
2015-12-31 19:43:10 +00:00
|
|
|
room = group.getGroupId().split('@')[0]
|
2016-01-01 09:42:11 +00:00
|
|
|
self.groups[room].addParticipants(group.getParticipants(), self.buddies, self.legacyName)
|
|
|
|
self.groups[room].sendParticipantsToSpectrum(self.legacyName)
|
2015-10-26 16:47:17 +00:00
|
|
|
|
2016-01-07 10:58:03 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onSubjectChanged(self, room, subject, subjectOwner, timestamp):
|
|
|
|
self.logger.debug(
|
2016-04-09 22:21:58 +00:00
|
|
|
"onSubjectChange(rrom=%s, subject=%s, subjectOwner=%s, timestamp=%s)" %
|
|
|
|
(room, subject, subjectOwner, timestamp)
|
|
|
|
)
|
2016-01-07 10:58:03 +00:00
|
|
|
try:
|
|
|
|
group = self.groups[room]
|
|
|
|
except KeyError:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.error("Subject of non-existant group (%s) changed" % group)
|
2016-01-07 10:58:03 +00:00
|
|
|
else:
|
|
|
|
group.subject = subject
|
|
|
|
group.subjectOwner = subjectOwner
|
|
|
|
if not group.joined:
|
|
|
|
# We have not joined group so we should not send subject
|
|
|
|
return
|
|
|
|
self.backend.handleSubject(self.user, room, subject, subjectOwner)
|
|
|
|
self.backend.handleRoomNicknameChanged(self.user, room, subject)
|
|
|
|
|
2015-11-29 14:32:33 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onParticipantsRemovedFromGroup(self, room, participants):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Participants removed from group: %s, %s" %
|
|
|
|
(room, participants))
|
2016-01-01 09:42:11 +00:00
|
|
|
self.groups[room].removeParticipants(participants)
|
2015-11-29 14:32:33 +00:00
|
|
|
|
2015-12-14 18:27:10 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onContactStatusChanged(self, number, status):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("%s changed their status to %s" % (number, status))
|
2015-12-14 18:27:10 +00:00
|
|
|
try:
|
|
|
|
buddy = self.buddies[number]
|
|
|
|
buddy.statusMsg = status
|
|
|
|
self.buddies.updateSpectrum(buddy)
|
|
|
|
except KeyError:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("%s not in buddy list" % number)
|
2015-12-14 18:27:10 +00:00
|
|
|
|
2016-01-07 09:47:33 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onContactPictureChanged(self, number):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("%s changed their profile picture" % number)
|
2016-01-07 09:47:33 +00:00
|
|
|
self.buddies.requestVCard(number)
|
|
|
|
|
2016-01-07 10:27:40 +00:00
|
|
|
# Called by superclass
|
|
|
|
def onContactAdded(self, number, nick):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Adding new contact %s (%s)" % (nick, number))
|
2016-01-07 10:27:40 +00:00
|
|
|
self.updateBuddy(number, nick, [])
|
|
|
|
|
|
|
|
# Called by superclass
|
|
|
|
def onContactRemoved(self, number):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Removing contact %s" % number)
|
2016-01-07 10:27:40 +00:00
|
|
|
self.removeBuddy(number)
|
|
|
|
|
|
|
|
def onContactUpdated(self, oldnumber, newnumber):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Contact has changed number from %s to %s" %
|
|
|
|
(oldnumber, newnumber))
|
2016-01-07 10:27:40 +00:00
|
|
|
if newnumber in self.buddies:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.warn("Contact %s exists, just updating" % newnumber)
|
2016-01-07 10:27:40 +00:00
|
|
|
self.buddies.refresh(newnumber)
|
|
|
|
try:
|
|
|
|
buddy = self.buddies[oldnumber]
|
|
|
|
except KeyError:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.warn("Old contact (%s) not found. Adding new contact (%s)" %
|
|
|
|
(oldnumber, newnumber))
|
2016-01-07 10:27:40 +00:00
|
|
|
nick = ""
|
|
|
|
else:
|
|
|
|
self.removeBuddy(buddy.number)
|
|
|
|
nick = buddy.nick
|
|
|
|
self.updateBuddy(newnumber, nick, [])
|
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def onPresenceReceived(self, _type, name, jid, lastseen):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Presence received: %s %s %s %s" % (_type, name, jid, lastseen))
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = jid.split("@")[0]
|
2016-01-02 20:40:11 +00:00
|
|
|
try:
|
|
|
|
buddy = self.buddies[buddy]
|
2015-11-10 16:04:20 +00:00
|
|
|
except KeyError:
|
2016-01-02 20:40:11 +00:00
|
|
|
# Sometimes whatsapp send our own presence
|
|
|
|
if buddy != self.legacyName:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.error("Buddy not found: %s" % buddy)
|
2015-11-10 16:04:20 +00:00
|
|
|
return
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
if (lastseen == buddy.lastseen) and (_type == buddy.presence):
|
2015-11-10 16:04:20 +00:00
|
|
|
return
|
2015-12-01 19:33:02 +00:00
|
|
|
|
2016-01-07 09:47:33 +00:00
|
|
|
if ((lastseen != "deny") and (lastseen != None) and (lastseen != "none")):
|
2015-11-10 16:04:20 +00:00
|
|
|
buddy.lastseen = int(lastseen)
|
|
|
|
if (_type == None):
|
|
|
|
buddy.lastseen = time.time()
|
2015-09-03 18:04:29 +00:00
|
|
|
|
2015-11-10 16:04:20 +00:00
|
|
|
buddy.presence = _type
|
2015-09-03 18:04:29 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
if _type == "unavailable":
|
2015-12-14 18:14:20 +00:00
|
|
|
self.onPresenceUnavailable(buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
else:
|
2015-12-14 18:14:20 +00:00
|
|
|
self.onPresenceAvailable(buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
|
|
|
|
2015-12-01 19:33:02 +00:00
|
|
|
|
2015-12-14 18:14:20 +00:00
|
|
|
def onPresenceAvailable(self, buddy):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Is available: %s" % buddy)
|
2015-12-14 18:14:20 +00:00
|
|
|
self.buddies.updateSpectrum(buddy)
|
2015-11-10 16:04:20 +00:00
|
|
|
|
2015-12-14 18:14:20 +00:00
|
|
|
def onPresenceUnavailable(self, buddy):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Is unavailable: %s" % buddy)
|
2015-12-14 18:14:20 +00:00
|
|
|
self.buddies.updateSpectrum(buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
# spectrum RequestMethods
|
2013-05-31 18:53:21 +00:00
|
|
|
def sendTypingStarted(self, buddy):
|
2015-12-31 19:43:10 +00:00
|
|
|
if buddy != "bot":
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Started typing: %s to %s" % (self.legacyName, buddy))
|
2015-09-03 18:04:29 +00:00
|
|
|
self.sendTyping(buddy, True)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.sendReadReceipts(buddy)
|
2015-09-03 18:04:29 +00:00
|
|
|
# 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):
|
2015-12-31 19:43:10 +00:00
|
|
|
if buddy != "bot":
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Stopped typing: %s to %s" % (self.legacyName, buddy))
|
2015-09-03 18:04:29 +00:00
|
|
|
self.sendTyping(buddy, False)
|
2016-02-19 16:31:34 +00:00
|
|
|
self.sendReadReceipts(buddy)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2016-01-09 15:12:59 +00:00
|
|
|
def sendImage(self, message, ID, to):
|
|
|
|
if (".jpg" in message.lower()):
|
|
|
|
imgType = "jpg"
|
|
|
|
if (".webp" in message.lower()):
|
|
|
|
imgType = "webp"
|
|
|
|
|
|
|
|
success = deferred.Deferred()
|
|
|
|
error = deferred.Deferred()
|
|
|
|
self.downloadMedia(message, success.run, error.run)
|
|
|
|
|
|
|
|
# Success
|
|
|
|
path = success.arg(0)
|
2016-04-09 22:21:58 +00:00
|
|
|
call(self.logger.info, "Success: Image downloaded to %s" % path)
|
2016-01-09 15:12:59 +00:00
|
|
|
pathWithExt = path.then(lambda p: p + "." + imgType)
|
|
|
|
call(os.rename, path, pathWithExt)
|
|
|
|
pathJpg = path.then(lambda p: p + ".jpg")
|
|
|
|
if imgType != "jpg":
|
|
|
|
im = call(Image.open, pathWithExt)
|
|
|
|
call(im.save, pathJpg)
|
|
|
|
call(os.remove, pathWithExt)
|
2016-04-09 22:21:58 +00:00
|
|
|
call(self.logger.info, "Sending image to %s" % to)
|
2016-01-09 15:12:59 +00:00
|
|
|
waId = deferred.Deferred()
|
|
|
|
call(super(Session, self).sendImage, to, pathJpg, onSuccess = waId.run)
|
|
|
|
call(self.setWaId, ID, waId)
|
|
|
|
waId.when(call, os.remove, pathJpg)
|
|
|
|
waId.when(self.logger.info, "Image sent")
|
|
|
|
|
|
|
|
# Error
|
|
|
|
error.when(self.logger.info, "Download Error. Sending message as is.")
|
|
|
|
waId = error.when(self.sendTextMessage, to, message)
|
|
|
|
call(self.setWaId, ID, waId)
|
|
|
|
|
|
|
|
def setWaId(self, XmppId, waId):
|
|
|
|
self.msgIDs[waId] = MsgIDs(XmppId, waId)
|
|
|
|
|
2016-01-06 14:57:17 +00:00
|
|
|
def sendMessageToWA(self, sender, message, ID, xhtml=""):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Message sent from %s to %s: %s (xhtml=%s)" %
|
|
|
|
(self.legacyName, sender, message, xhtml))
|
2015-09-15 19:29:26 +00:00
|
|
|
|
2016-02-19 16:31:34 +00:00
|
|
|
self.sendReadReceipts(sender)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
if sender == "bot":
|
2015-09-06 12:06:16 +00:00
|
|
|
self.bot.parse(message)
|
2015-12-31 19:43:10 +00:00
|
|
|
elif "-" in sender: # group msg
|
|
|
|
if "/" in sender: # directed at single user
|
|
|
|
room, nick = sender.split("/")
|
2016-01-01 09:42:11 +00:00
|
|
|
group = self.groups[room]
|
|
|
|
number = None
|
|
|
|
for othernumber, othernick in group.participants.iteritems():
|
|
|
|
if othernick == nick:
|
|
|
|
number = othernumber
|
|
|
|
break
|
|
|
|
if number is not None:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Private message sent from %s to %s" % (self.legacyName, number))
|
2016-01-01 09:42:11 +00:00
|
|
|
waId = self.sendTextMessage(number + '@s.whatsapp.net', message)
|
|
|
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
|
|
|
else:
|
|
|
|
self.logger.error("Attempted to send private message to non-existent user")
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("%s to %s in %s" % (self.legacyName, nick, room))
|
2015-09-06 12:06:16 +00:00
|
|
|
else:
|
|
|
|
room = sender
|
2015-12-31 19:43:10 +00:00
|
|
|
if message[0] == '\\' and message[:1] != '\\\\':
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Executing command %s in %s" % (message, room))
|
2015-11-27 20:01:16 +00:00
|
|
|
self.executeCommand(message, room)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
group = self.groups[self._lengthenGroupId(room)]
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Group Message from %s to %s Groups: %s" %
|
|
|
|
(group.nick , group , self.groups))
|
2015-11-27 20:01:16 +00:00
|
|
|
self.backend.handleMessage(
|
2016-04-09 22:21:58 +00:00
|
|
|
self.user, room, message, group.nick, xhtml=xhtml
|
2015-11-27 20:01:16 +00:00
|
|
|
)
|
|
|
|
except KeyError:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.error('Group not found: %s' % room)
|
2016-01-06 14:57:17 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
if (".jpg" in message.lower()) or (".webp" in message.lower()):
|
2016-01-09 15:12:59 +00:00
|
|
|
self.sendImage(message, ID, room + '@g.us')
|
|
|
|
elif "geo:" in message.lower():
|
|
|
|
self._sendLocation(room + "@g.us", message, ID)
|
|
|
|
else:
|
|
|
|
self.sendTextMessage(room + '@g.us', message)
|
2015-09-06 12:06:16 +00:00
|
|
|
else: # private msg
|
|
|
|
buddy = sender
|
2015-12-31 19:43:10 +00:00
|
|
|
# if message == "\\lastseen":
|
|
|
|
# self.call("presence_request", buddy = (buddy + "@s.whatsapp.net",))
|
2015-08-30 20:18:03 +00:00
|
|
|
# else:
|
2015-12-31 19:43:10 +00:00
|
|
|
if message.split(" ").pop(0) == "\\lastseen":
|
2015-11-09 22:09:12 +00:00
|
|
|
self.presenceRequested.append(buddy)
|
2015-12-31 19:43:10 +00:00
|
|
|
#self.call("presence_request", (buddy + "@s.whatsapp.net",))
|
2015-11-09 22:09:12 +00:00
|
|
|
self._requestLastSeen(buddy)
|
2015-12-31 19:43:10 +00:00
|
|
|
elif message.split(" ").pop(0) == "\\gpp":
|
|
|
|
self.logger.info("Get Profile Picture! ")
|
|
|
|
self.sendMessageToXMPP(buddy, "Fetching Profile Picture")
|
|
|
|
#self.call("contact_getProfilePicture", (buddy + "@s.whatsapp.net",))
|
2015-11-09 22:09:12 +00:00
|
|
|
self.requestVCard(buddy)
|
|
|
|
else:
|
2016-01-09 15:12:59 +00:00
|
|
|
if (".jpg" in message.lower()) or (".webp" in message.lower()):
|
|
|
|
self.sendImage(message, ID, buddy + "@s.whatsapp.net")
|
2015-12-31 19:43:10 +00:00
|
|
|
elif "geo:" in message.lower():
|
|
|
|
self._sendLocation(buddy + "@s.whatsapp.net", message, ID)
|
2015-11-09 22:09:12 +00:00
|
|
|
else:
|
2015-12-31 19:43:10 +00:00
|
|
|
waId = self.sendTextMessage(sender + '@s.whatsapp.net', message)
|
2015-11-09 22:09:12 +00:00
|
|
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("WA Message send to %s with ID %s" % (buddy, waId))
|
2015-12-31 19:43:10 +00:00
|
|
|
#self.sendTextMessage(sender + '@s.whatsapp.net', message)
|
2015-11-27 20:01:16 +00:00
|
|
|
|
|
|
|
def executeCommand(self, command, room):
|
2015-12-31 19:43:10 +00:00
|
|
|
if command == '\\leave':
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Leaving room %s" % room)
|
2015-11-27 20:01:16 +00:00
|
|
|
# Leave group on whatsapp side
|
|
|
|
self.leaveGroup(room)
|
|
|
|
# Delete Room on spectrum side
|
|
|
|
group = self.groups[room]
|
2016-01-06 14:33:28 +00:00
|
|
|
group.leaveRoom()
|
2015-11-27 20:01:16 +00:00
|
|
|
del self.groups[room]
|
2015-11-09 22:09:12 +00:00
|
|
|
|
|
|
|
def _requestLastSeen(self, buddy):
|
|
|
|
|
|
|
|
def onSuccess(buddy, lastseen):
|
2015-11-10 16:04:20 +00:00
|
|
|
timestamp = time.localtime(time.localtime()-lastseen)
|
2015-12-31 19:43:10 +00:00
|
|
|
timestring = time.strftime("%a, %d %b %Y %H:%M:%S", timestamp)
|
2016-04-09 22:21:58 +00:00
|
|
|
self.sendMessageToXMPP(buddy, "%s (%s) %s" % (timestring, utils.ago(lastseen), lastseen))
|
2015-11-09 22:09:12 +00:00
|
|
|
def onError(errorIqEntity, originalIqEntity):
|
2015-12-31 19:43:10 +00:00
|
|
|
self.sendMessageToXMPP(errorIqEntity.getFrom(), "LastSeen Error")
|
2015-11-09 22:09:12 +00:00
|
|
|
|
|
|
|
self.requestLastSeen(buddy, onSuccess, onError)
|
|
|
|
|
|
|
|
def _sendLocation(self, buddy, message, ID):
|
2015-12-31 19:43:10 +00:00
|
|
|
#with open('/opt/transwhat/map.jpg', 'rb') as imageFile:
|
2015-11-09 22:09:12 +00:00
|
|
|
# raw = base64.b64encode(imageFile.read())
|
2015-12-31 19:43:10 +00:00
|
|
|
latitude,longitude = message.split(':')[1].split(',')
|
2015-11-09 22:09:12 +00:00
|
|
|
waId = self.sendLocation(buddy, float(latitude), float(longitude))
|
|
|
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("WA Location Message send to %s with ID %s" % (buddy, waId))
|
2015-11-09 22:09:12 +00:00
|
|
|
|
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
def sendMessageToXMPP(self, buddy, messageContent, timestamp = "", nickname = ""):
|
2015-06-21 14:11:43 +00:00
|
|
|
if timestamp:
|
2015-12-31 19:43:10 +00:00
|
|
|
timestamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime(timestamp))
|
2015-06-21 14:11:43 +00:00
|
|
|
|
|
|
|
if self.initialized == False:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Message queued from %s to %s: %s" %
|
|
|
|
(buddy, self.legacyName, messageContent))
|
2015-06-21 14:11:43 +00:00
|
|
|
self.offlineQueue.append((buddy, messageContent, timestamp))
|
|
|
|
else:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Message sent from %s to %s: %s" % (
|
|
|
|
buddy, self.legacyName, messageContent))
|
2015-12-31 19:43:10 +00:00
|
|
|
self.backend.handleMessage(self.user, buddy, messageContent, "",
|
|
|
|
"", timestamp)
|
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
def sendGroupMessageToXMPP(self, room, number, messageContent, timestamp = "", defaultname = ""):
|
2013-06-28 20:46:56 +00:00
|
|
|
if timestamp:
|
2015-12-31 19:43:10 +00:00
|
|
|
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:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Group message queued from %s to %s: %s" %
|
|
|
|
(number, 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] = [ ]
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
self.groupOfflineQueue[room].append(
|
2016-01-01 09:42:11 +00:00
|
|
|
(number, messageContent, timestamp)
|
2015-09-15 19:29:26 +00:00
|
|
|
)
|
2013-05-31 18:53:21 +00:00
|
|
|
else:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("Group message sent from %s to %s: %s" %
|
|
|
|
(number, room, messageContent))
|
2015-11-23 15:06:14 +00:00
|
|
|
try:
|
|
|
|
group = self.groups[room]
|
2016-01-01 09:42:11 +00:00
|
|
|
# Update nickname
|
|
|
|
try:
|
2016-01-05 13:46:32 +00:00
|
|
|
if defaultname != "" and group.participants[number] == number:
|
2016-01-01 09:42:11 +00:00
|
|
|
group.changeNick(number, defaultname)
|
|
|
|
if self.buddies[number].nick != "":
|
|
|
|
group.changeNick(number, self.buddies[number].nick)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
nick = group.participants[number]
|
2015-11-23 15:06:14 +00:00
|
|
|
if group.joined:
|
2016-01-01 09:42:11 +00:00
|
|
|
self.backend.handleMessage(self.user, room, messageContent,
|
2015-12-31 19:43:10 +00:00
|
|
|
nick, "", timestamp)
|
2015-11-23 15:06:14 +00:00
|
|
|
else:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.bot.send("You have received a message in group: %s@%s"
|
2015-11-23 15:06:14 +00:00
|
|
|
% (room, self.backend.spectrum_jid))
|
2015-12-31 19:43:10 +00:00
|
|
|
self.bot.send("Join the group in order to reply")
|
|
|
|
self.bot.send("%s: %s" % (nick, messageContent))
|
2015-11-23 15:06:14 +00:00
|
|
|
except KeyError:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.warn("Group is not in group list")
|
2015-11-23 15:06:14 +00:00
|
|
|
self.backend.handleMessage(self.user, self._shortenGroupId(room),
|
2016-01-01 09:42:11 +00:00
|
|
|
messageContent, number, "", timestamp)
|
2015-11-23 15:06:14 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
def changeStatus(self, status):
|
|
|
|
if status != self.status:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Status changed: %s" % status)
|
2013-06-28 20:46:56 +00:00
|
|
|
self.status = status
|
|
|
|
|
2015-09-15 19:29:26 +00:00
|
|
|
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
|
2016-04-09 22:21:58 +00:00
|
|
|
self.setStatus(statusMessage)
|
2016-04-10 00:47:53 +00:00
|
|
|
self.logger.info("Status message changed: %s" % statusMessage)
|
2013-06-28 20:46:56 +00:00
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
#if self.initialized == False:
|
|
|
|
# self.sendOfflineMessages()
|
2015-12-31 19:43:10 +00:00
|
|
|
# self.bot.call("welcome")
|
2015-11-09 22:09:12 +00:00
|
|
|
# self.initialized = True
|
2013-06-28 20:46:56 +00:00
|
|
|
|
|
|
|
def sendOfflineMessages(self):
|
|
|
|
# Flush Queues
|
|
|
|
while self.offlineQueue:
|
|
|
|
msg = self.offlineQueue.pop(0)
|
2015-12-31 19:43:10 +00:00
|
|
|
self.backend.handleMessage(self.user, msg[0], msg[1], "", "", msg[2])
|
2013-06-28 20:46:56 +00:00
|
|
|
|
2015-12-01 19:33:02 +00:00
|
|
|
# Called when user logs in to initialize the roster
|
2015-12-01 21:21:38 +00:00
|
|
|
def loadBuddies(self, buddies):
|
|
|
|
self.buddies.load(buddies)
|
2015-12-01 19:33:02 +00:00
|
|
|
|
2013-06-28 20:46:56 +00:00
|
|
|
# also for adding a new buddy
|
2015-09-21 18:30:11 +00:00
|
|
|
def updateBuddy(self, buddy, nick, groups, image_hash = None):
|
2015-12-31 19:43:10 +00:00
|
|
|
if buddy != "bot":
|
2015-09-05 22:14:12 +00:00
|
|
|
self.buddies.update(buddy, nick, groups, image_hash)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def removeBuddy(self, buddy):
|
2015-12-31 19:43:10 +00:00
|
|
|
if buddy != "bot":
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Buddy removed: %s" % buddy)
|
2013-06-28 20:46:56 +00:00
|
|
|
self.buddies.remove(buddy)
|
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
def requestVCard(self, buddy, ID=None):
|
2016-01-02 15:08:30 +00:00
|
|
|
self.buddies.requestVCard(buddy, ID)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
def createThumb(self, size=100, raw=False):
|
|
|
|
img = Image.open(self.imgPath)
|
|
|
|
width, height = img.size
|
2015-12-31 19:43:10 +00:00
|
|
|
img_thumbnail = self.imgPath + '_thumbnail'
|
2015-11-09 22:09:12 +00:00
|
|
|
|
|
|
|
if width > height:
|
|
|
|
nheight = float(height) / width * size
|
|
|
|
nwidth = size
|
|
|
|
else:
|
|
|
|
nwidth = float(width) / height * size
|
|
|
|
nheight = size
|
|
|
|
|
|
|
|
img.thumbnail((nwidth, nheight), Image.ANTIALIAS)
|
2015-12-31 19:43:10 +00:00
|
|
|
img.save(img_thumbnail, 'JPEG')
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
with open(img_thumbnail, 'rb') as imageFile:
|
2015-11-09 22:09:12 +00:00
|
|
|
raw = base64.b64encode(imageFile.read())
|
|
|
|
|
|
|
|
return raw
|
|
|
|
|
2015-11-29 15:06:50 +00:00
|
|
|
# Not used
|
|
|
|
def onLocationReceived(self, messageId, jid, name, preview, latitude, longitude, receiptRequested, isBroadcast):
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = jid.split("@")[0]
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Location received from %s: %s, %s" % (buddy, latitude, longitude))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
url = "http://maps.google.de?%s" % urllib.urlencode({ "q": "%s %s" % (latitude, longitude) })
|
2015-11-29 15:06:50 +00:00
|
|
|
self.sendMessageToXMPP(buddy, utils.shorten(url))
|
2015-12-31 19:43:10 +00:00
|
|
|
if receiptRequested: self.call("message_ack", (jid, messageId))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def onGroupSubjectReceived(self, messageId, gjid, jid, subject, timestamp, receiptRequested):
|
2015-12-31 19:43:10 +00:00
|
|
|
room = gjid.split("@")[0]
|
|
|
|
buddy = jid.split("@")[0]
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
self.backend.handleSubject(self.user, room, subject, buddy)
|
2015-12-31 19:43:10 +00:00
|
|
|
if receiptRequested: self.call("subject_ack", (gjid, messageId))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
# Yowsup Notifications
|
|
|
|
def onGroupParticipantRemoved(self, gjid, jid, author, timestamp, messageId, receiptRequested):
|
2015-12-31 19:43:10 +00:00
|
|
|
room = gjid.split("@")[0]
|
|
|
|
buddy = jid.split("@")[0]
|
2015-11-29 15:06:50 +00:00
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Removed %s from room %s" % (buddy, room))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
self.backend.handleParticipantChanged(self.user, buddy, room, protocol_pb2.PARTICIPANT_FLAG_NONE, protocol_pb2.STATUS_NONE) # TODO
|
2015-12-31 19:43:10 +00:00
|
|
|
if receiptRequested: self.call("notification_ack", (gjid, messageId))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
def onContactProfilePictureUpdated(self, jid, timestamp, messageId, pictureId, receiptRequested):
|
|
|
|
# TODO
|
2015-12-31 19:43:10 +00:00
|
|
|
if receiptRequested: self.call("notification_ack", (jid, messageId))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
def onGroupPictureUpdated(self, jid, author, timestamp, messageId, pictureId, receiptRequested):
|
|
|
|
# TODO
|
2015-12-31 19:43:10 +00:00
|
|
|
if receiptRequested: self.call("notification_ack", (jid, messageId))
|
2015-11-29 15:06:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|