Merge pull request #20 from moyamo/yowsup-2
Message Receipts, improved groupchats, http upload for images.
This commit is contained in:
commit
b458ef8103
|
@ -4,7 +4,6 @@ import struct
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import google.protobuf
|
import google.protobuf
|
||||||
|
|
||||||
def WRAP(MESSAGE, TYPE):
|
def WRAP(MESSAGE, TYPE):
|
||||||
|
@ -20,7 +19,6 @@ class SpectrumBackend:
|
||||||
@param host: Host where Spectrum2 NetworkPluginServer runs.
|
@param host: Host where Spectrum2 NetworkPluginServer runs.
|
||||||
@param port: Port.
|
@param port: Port.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.m_pingReceived = False
|
self.m_pingReceived = False
|
||||||
self.m_data = ""
|
self.m_data = ""
|
||||||
|
@ -39,6 +37,17 @@ class SpectrumBackend:
|
||||||
message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE)
|
message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE)
|
||||||
self.send(message)
|
self.send(message)
|
||||||
|
|
||||||
|
def handleMessageAck(self, user, legacyName, ID):
|
||||||
|
m = protocol_pb2.ConversationMessage()
|
||||||
|
m.userName = user
|
||||||
|
m.buddyName = legacyName
|
||||||
|
m.message = ""
|
||||||
|
m.id = ID
|
||||||
|
|
||||||
|
message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE_ACK)
|
||||||
|
self.send(message)
|
||||||
|
|
||||||
|
|
||||||
def handleAttention(self, user, buddyName, msg):
|
def handleAttention(self, user, buddyName, msg):
|
||||||
m = protocol_pb2.ConversationMessage()
|
m = protocol_pb2.ConversationMessage()
|
||||||
m.userName = user
|
m.userName = user
|
||||||
|
@ -206,7 +215,7 @@ class SpectrumBackend:
|
||||||
|
|
||||||
def handleFTData(self, ftID, data):
|
def handleFTData(self, ftID, data):
|
||||||
d = protocol_pb2.FileTransferData()
|
d = protocol_pb2.FileTransferData()
|
||||||
d.ftID = ftID
|
d.ftid = ftID
|
||||||
d.data = data
|
d.data = data
|
||||||
|
|
||||||
message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_FT_DATA);
|
message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_FT_DATA);
|
||||||
|
@ -242,10 +251,20 @@ class SpectrumBackend:
|
||||||
|
|
||||||
def handleConvMessagePayload(self, data):
|
def handleConvMessagePayload(self, data):
|
||||||
payload = protocol_pb2.ConversationMessage()
|
payload = protocol_pb2.ConversationMessage()
|
||||||
|
self.logger.error("handleConvMessagePayload")
|
||||||
if (payload.ParseFromString(data) == False):
|
if (payload.ParseFromString(data) == False):
|
||||||
#TODO: ERROR
|
#TODO: ERROR
|
||||||
return
|
return
|
||||||
self.handleMessageSendRequest(payload.userName, payload.buddyName, payload.message, payload.xhtml)
|
self.handleMessageSendRequest(payload.userName, payload.buddyName, payload.message, payload.xhtml, payload.id)
|
||||||
|
|
||||||
|
def handleConvMessageAckPayload(self, data):
|
||||||
|
payload = protocol_pb2.ConversationMessage()
|
||||||
|
if (payload.ParseFromString(data) == False):
|
||||||
|
#TODO: ERROR
|
||||||
|
return
|
||||||
|
self.handleMessageAckRequest(payload.userName, payload.buddyName, payload.id)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def handleAttentionPayload(self, data):
|
def handleAttentionPayload(self, data):
|
||||||
payload = protocol_pb2.ConversationMessage()
|
payload = protocol_pb2.ConversationMessage()
|
||||||
|
@ -345,8 +364,10 @@ class SpectrumBackend:
|
||||||
if (len(self.m_data) >= 4):
|
if (len(self.m_data) >= 4):
|
||||||
expected_size = struct.unpack('!I', self.m_data[0:4])[0]
|
expected_size = struct.unpack('!I', self.m_data[0:4])[0]
|
||||||
if (len(self.m_data) - 4 < expected_size):
|
if (len(self.m_data) - 4 < expected_size):
|
||||||
|
self.logger.debug("Data packet incomplete")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
self.logger.debug("Data packet incomplete")
|
||||||
return
|
return
|
||||||
|
|
||||||
packet = self.m_data[4:4+expected_size]
|
packet = self.m_data[4:4+expected_size]
|
||||||
|
@ -355,12 +376,12 @@ class SpectrumBackend:
|
||||||
parseFromString = wrapper.ParseFromString(packet)
|
parseFromString = wrapper.ParseFromString(packet)
|
||||||
except:
|
except:
|
||||||
self.m_data = self.m_data[expected_size+4:]
|
self.m_data = self.m_data[expected_size+4:]
|
||||||
self.logger.error("Parse from String exception")
|
self.logger.error("Parse from String exception. Skipping packet.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if parseFromString == False:
|
if parseFromString == False:
|
||||||
self.m_data = self.m_data[expected_size+4:]
|
self.m_data = self.m_data[expected_size+4:]
|
||||||
self.logger.error("Parse from String failed")
|
self.logger.error("Parse from String failed. Skipping packet.")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.m_data = self.m_data[4+expected_size:]
|
self.m_data = self.m_data[4+expected_size:]
|
||||||
|
@ -403,6 +424,8 @@ class SpectrumBackend:
|
||||||
self.handleFTContinuePayload(wrapper.payload)
|
self.handleFTContinuePayload(wrapper.payload)
|
||||||
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_EXIT:
|
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_EXIT:
|
||||||
self.handleExitRequest()
|
self.handleExitRequest()
|
||||||
|
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE_ACK:
|
||||||
|
self.handleConvMessageAckPayload(wrapper.payload)
|
||||||
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_RAW_XML:
|
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_RAW_XML:
|
||||||
self.handleRawXmlRequest(wrapper.payload)
|
self.handleRawXmlRequest(wrapper.payload)
|
||||||
|
|
||||||
|
@ -475,17 +498,30 @@ class SpectrumBackend:
|
||||||
|
|
||||||
raise NotImplementedError, "Implement me"
|
raise NotImplementedError, "Implement me"
|
||||||
|
|
||||||
def handleMessageSendRequest(self, user, legacyName, message, xhtml = ""):
|
def handleMessageSendRequest(self, user, legacyName, message, xhtml = "", ID = 0):
|
||||||
"""
|
"""
|
||||||
Called when XMPP user sends message to legacy network.
|
Called when XMPP user sends message to legacy network.
|
||||||
@param user: XMPP JID of user for which this event occurs.
|
@param user: XMPP JID of user for which this event occurs.
|
||||||
@param legacyName: Legacy network name of buddy or room.
|
@param legacyName: Legacy network name of buddy or room.
|
||||||
@param message: Plain text message.
|
@param message: Plain text message.
|
||||||
@param xhtml: XHTML message.
|
@param xhtml: XHTML message.
|
||||||
|
@param ID: message ID
|
||||||
"""
|
"""
|
||||||
|
|
||||||
raise NotImplementedError, "Implement me"
|
raise NotImplementedError, "Implement me"
|
||||||
|
|
||||||
|
def handleMessageAckRequest(self, user, legacyName, ID = 0):
|
||||||
|
"""
|
||||||
|
Called when XMPP user sends message to legacy network.
|
||||||
|
@param user: XMPP JID of user for which this event occurs.
|
||||||
|
@param legacyName: Legacy network name of buddy or room.
|
||||||
|
@param ID: message ID
|
||||||
|
"""
|
||||||
|
|
||||||
|
# raise NotImplementedError, "Implement me"
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def handleVCardRequest(self, user, legacyName, ID):
|
def handleVCardRequest(self, user, legacyName, ID):
|
||||||
""" Called when XMPP user requests VCard of buddy.
|
""" Called when XMPP user requests VCard of buddy.
|
||||||
@param user: XMPP JID of user for which this event occurs.
|
@param user: XMPP JID of user for which this event occurs.
|
||||||
|
|
File diff suppressed because one or more lines are too long
92
bot.py
92
bot.py
|
@ -31,20 +31,26 @@ import os
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
from constants import *
|
from constants import *
|
||||||
|
#from googleclient import GoogleClient
|
||||||
|
|
||||||
from Yowsup.Contacts.contacts import WAContactsSyncRequest
|
#from Yowsup.Contacts.contacts import WAContactsSyncRequest
|
||||||
|
|
||||||
class Bot():
|
class Bot():
|
||||||
def __init__(self, session, name = "Bot"):
|
def __init__(self, session, name = "Bot"):
|
||||||
self.session = session
|
self.session = session
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
#self.google = GoogleClient()
|
||||||
|
|
||||||
self.commands = {
|
self.commands = {
|
||||||
|
"import": self._import,
|
||||||
"help": self._help,
|
"help": self._help,
|
||||||
"prune": self._prune,
|
"prune": self._prune,
|
||||||
"welcome": self._welcome,
|
"welcome": self._welcome,
|
||||||
"fortune": self._fortune,
|
"fortune": self._fortune,
|
||||||
"sync": self._sync
|
"sync": self._sync,
|
||||||
|
"groups": self._groups,
|
||||||
|
"getgroups": self._getgroups
|
||||||
}
|
}
|
||||||
|
|
||||||
def parse(self, message):
|
def parse(self, message):
|
||||||
|
@ -66,7 +72,7 @@ class Bot():
|
||||||
spec = inspect.getargspec(func)
|
spec = inspect.getargspec(func)
|
||||||
maxs = len(spec.args) - 1
|
maxs = len(spec.args) - 1
|
||||||
reqs = maxs - len(spec.defaults or [])
|
reqs = maxs - len(spec.defaults or [])
|
||||||
if reqs > len(args) > maxs:
|
if (reqs > len(args)) or (len(args) > maxs):
|
||||||
raise TypeError()
|
raise TypeError()
|
||||||
|
|
||||||
thread = threading.Thread(target=func, args=tuple(args))
|
thread = threading.Thread(target=func, args=tuple(args))
|
||||||
|
@ -75,6 +81,41 @@ class Bot():
|
||||||
def send(self, message):
|
def send(self, message):
|
||||||
self.session.backend.handleMessage(self.session.user, self.name, message)
|
self.session.backend.handleMessage(self.session.user, self.name, message)
|
||||||
|
|
||||||
|
def __do_import(self, token):
|
||||||
|
# Google
|
||||||
|
google = self.google.getContacts(token)
|
||||||
|
self.send("%d buddies imported from google" % len(google))
|
||||||
|
|
||||||
|
result = { }
|
||||||
|
for number, name in google.iteritems():
|
||||||
|
number = re.sub("[^0-9]", "", number)
|
||||||
|
number = number if number[0] == "0" else "+" + number
|
||||||
|
|
||||||
|
result[number] = { 'nick': name, 'state': 0 }
|
||||||
|
|
||||||
|
# WhatsApp
|
||||||
|
user = self.session.legacyName
|
||||||
|
password = self.session.password
|
||||||
|
sync = WAContactsSyncRequest(user, password, result.keys())
|
||||||
|
whatsapp = sync.send()['c']
|
||||||
|
|
||||||
|
for w in whatsapp:
|
||||||
|
result[w['p']]['state'] = w['w']
|
||||||
|
result[w['p']]['number'] = w['n']
|
||||||
|
|
||||||
|
self.send("%d buddies are using whatsapp" % len(filter(lambda w: w['w'], whatsapp)))
|
||||||
|
|
||||||
|
for r in result.values():
|
||||||
|
if r['nick']:
|
||||||
|
self.session.buddies.add(
|
||||||
|
number = r['number'],
|
||||||
|
nick = r['nick'],
|
||||||
|
groups = [u'Google'],
|
||||||
|
state = r['state']
|
||||||
|
)
|
||||||
|
|
||||||
|
self.send("%d buddies imported" % len(whatsapp))
|
||||||
|
|
||||||
def __get_token(self, filename, timeout = 30):
|
def __get_token(self, filename, timeout = 30):
|
||||||
file = open(filename, 'r')
|
file = open(filename, 'r')
|
||||||
file.seek(-1, 2) # look at the end
|
file.seek(-1, 2) # look at the end
|
||||||
|
@ -96,6 +137,25 @@ class Bot():
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
# commands
|
# commands
|
||||||
|
def _import(self, token = None):
|
||||||
|
if not token:
|
||||||
|
token_url = self.google.getTokenUrl("http://whatsapp.0l.de/auth.py")
|
||||||
|
auth_url = "http://whatsapp.0l.de/auth.py?number=%s&auth_url=%s" % (self.session.legacyName, urllib.quote(token_url))
|
||||||
|
short_url = utils.shorten(auth_url)
|
||||||
|
self.send("please visit this url to auth: %s" % short_url)
|
||||||
|
|
||||||
|
self.send("waiting for authorization...")
|
||||||
|
token = self.__get_token(TOKEN_FILE)
|
||||||
|
if token:
|
||||||
|
self.send("got token: %s" % token)
|
||||||
|
self.__do_import(token)
|
||||||
|
self.session.updateRoster()
|
||||||
|
else:
|
||||||
|
self.send("timeout! please use \"\\import [token]\"")
|
||||||
|
else:
|
||||||
|
self.__do_import(token)
|
||||||
|
self.session.updateRoster()
|
||||||
|
|
||||||
def _sync(self):
|
def _sync(self):
|
||||||
user = self.session.legacyName
|
user = self.session.legacyName
|
||||||
password = self.session.password
|
password = self.session.password
|
||||||
|
@ -112,14 +172,23 @@ class Bot():
|
||||||
self.send("""following bot commands are available:
|
self.send("""following bot commands are available:
|
||||||
\\help show this message
|
\\help show this message
|
||||||
\\prune clear your buddylist
|
\\prune clear your buddylist
|
||||||
|
\\import [token] import buddies from Google
|
||||||
\\sync sync your imported contacts with WhatsApp
|
\\sync sync your imported contacts with WhatsApp
|
||||||
\\fortune [database] give me a quote
|
\\fortune [database] give me a quote
|
||||||
|
\\groups print all attended groups
|
||||||
|
\\getgroups get current groups from WA
|
||||||
|
|
||||||
following user commands are available:
|
following user commands are available:
|
||||||
\\lastseen request last online timestamp from buddy""")
|
\\lastseen request last online timestamp from buddy
|
||||||
|
|
||||||
|
following group commands are available
|
||||||
|
\\leave permanently leave group chat""")
|
||||||
|
|
||||||
def _fortune(self, database = '', prefix=''):
|
def _fortune(self, database = '', prefix=''):
|
||||||
if os.path.exists("/usr/share/games/fortunes/%s" % database):
|
if os.path.exists("/usr/share/fortune/%s" % database):
|
||||||
|
fortune = os.popen('/usr/bin/fortune %s' % database).read()
|
||||||
|
self.send(prefix + fortune[:-1])
|
||||||
|
elif os.path.exists("/usr/share/games/fortunes/%s" % database):
|
||||||
fortune = os.popen('/usr/games/fortune %s' % database).read()
|
fortune = os.popen('/usr/games/fortune %s' % database).read()
|
||||||
self.send(prefix + fortune[:-1])
|
self.send(prefix + fortune[:-1])
|
||||||
else:
|
else:
|
||||||
|
@ -134,3 +203,16 @@ following user commands are available:
|
||||||
self.session.buddies.prune()
|
self.session.buddies.prune()
|
||||||
self.session.updateRoster()
|
self.session.updateRoster()
|
||||||
self.send("buddy list cleared")
|
self.send("buddy list cleared")
|
||||||
|
def _groups(self):
|
||||||
|
for group in self.session.groups:
|
||||||
|
buddy = self.session.groups[group].owner
|
||||||
|
try:
|
||||||
|
nick = self.session.buddies[buddy].nick
|
||||||
|
except KeyError:
|
||||||
|
nick = buddy
|
||||||
|
|
||||||
|
self.send(self.session.groups[group].id + "@" + self.session.backend.spectrum_jid + " " + self.session.groups[group].subject + " Owner: " + nick )
|
||||||
|
def _getgroups(self):
|
||||||
|
#self.session.call("group_getGroups", ("participating",))
|
||||||
|
self.session.requestGroupsList(self.session._updateGroups)
|
||||||
|
|
||||||
|
|
28
buddy.py
28
buddy.py
|
@ -23,9 +23,10 @@ __status__ = "Prototype"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from Spectrum2 import protocol_pb2
|
from Spectrum2 import protocol_pb2
|
||||||
from Yowsup.Contacts.contacts import WAContactsSyncRequest
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
class Number():
|
class Number():
|
||||||
|
|
||||||
|
@ -56,16 +57,21 @@ class Buddy():
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.number = number
|
self.number = number
|
||||||
self.groups = groups
|
self.groups = groups
|
||||||
self.image_hash = image_hash
|
self.image_hash = image_hash if image_hash is not None else ""
|
||||||
|
self.statusMsg = ""
|
||||||
|
self.lastseen = 0
|
||||||
|
self.presence = 0
|
||||||
|
|
||||||
|
|
||||||
def update(self, nick, groups, image_hash):
|
def update(self, nick, groups, image_hash):
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
self.groups = groups
|
self.groups = groups
|
||||||
|
if image_hash is not None:
|
||||||
self.image_hash = image_hash
|
self.image_hash = image_hash
|
||||||
|
|
||||||
groups = u",".join(groups).encode("latin-1")
|
groups = u",".join(groups).encode("latin-1")
|
||||||
cur = self.db.cursor()
|
cur = self.db.cursor()
|
||||||
cur.execute("UPDATE buddies SET nick = %s, groups = %s, image_hash = %s WHERE owner_id = %s AND buddy_id = %s", (self.nick, groups, image_hash, self.owner.id, self.number.id))
|
cur.execute("UPDATE buddies SET nick = %s, groups = %s, image_hash = %s WHERE owner_id = %s AND buddy_id = %s", (self.nick, groups, self.image_hash, self.owner.id, self.number.id))
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
@ -91,9 +97,12 @@ class BuddyList(dict):
|
||||||
def __init__(self, owner, db):
|
def __init__(self, owner, db):
|
||||||
self.db = db
|
self.db = db
|
||||||
self.owner = Number(owner, 1, db)
|
self.owner = Number(owner, 1, db)
|
||||||
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
self.clear()
|
self.clear()
|
||||||
|
self.lock.acquire()
|
||||||
|
|
||||||
cur = self.db.cursor()
|
cur = self.db.cursor()
|
||||||
cur.execute("""SELECT
|
cur.execute("""SELECT
|
||||||
|
@ -114,13 +123,17 @@ class BuddyList(dict):
|
||||||
for i in range(cur.rowcount):
|
for i in range(cur.rowcount):
|
||||||
id, number, nick, groups, state, image_hash = cur.fetchone()
|
id, number, nick, groups, state, image_hash = cur.fetchone()
|
||||||
self[number] = Buddy(self.owner, Number(number, state, self.db), nick.decode('latin1'), groups.split(","), image_hash, id, self.db)
|
self[number] = Buddy(self.owner, Number(number, state, self.db), nick.decode('latin1'), groups.split(","), image_hash, id, self.db)
|
||||||
|
self.lock.release()
|
||||||
|
|
||||||
|
|
||||||
def update(self, number, nick, groups, image_hash):
|
def update(self, number, nick, groups, image_hash):
|
||||||
|
self.lock.acquire()
|
||||||
if number in self:
|
if number in self:
|
||||||
buddy = self[number]
|
buddy = self[number]
|
||||||
buddy.update(nick, groups, image_hash)
|
buddy.update(nick, groups, image_hash)
|
||||||
else:
|
else:
|
||||||
buddy = self.add(number, nick, groups, 1, image_hash)
|
buddy = self.add(number, nick, groups, 1, image_hash)
|
||||||
|
self.lock.release()
|
||||||
|
|
||||||
return buddy
|
return buddy
|
||||||
|
|
||||||
|
@ -130,17 +143,24 @@ class BuddyList(dict):
|
||||||
def remove(self, number):
|
def remove(self, number):
|
||||||
try:
|
try:
|
||||||
buddy = self[number]
|
buddy = self[number]
|
||||||
|
self.lock.acquire()
|
||||||
buddy.delete()
|
buddy.delete()
|
||||||
|
self.lock.release()
|
||||||
return buddy
|
return buddy
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def prune(self):
|
def prune(self):
|
||||||
|
self.lock.acquire()
|
||||||
|
|
||||||
cur = self.db.cursor()
|
cur = self.db.cursor()
|
||||||
cur.execute("DELETE FROM buddies WHERE owner_id = %s", self.owner.id)
|
cur.execute("DELETE FROM buddies WHERE owner_id = %s", self.owner.id)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
|
self.lock.release()
|
||||||
|
|
||||||
|
|
||||||
def sync(self, user, password):
|
def sync(self, user, password):
|
||||||
|
self.lock.acquire()
|
||||||
cur = self.db.cursor()
|
cur = self.db.cursor()
|
||||||
cur.execute("""SELECT
|
cur.execute("""SELECT
|
||||||
n.number AS number,
|
n.number AS number,
|
||||||
|
@ -165,5 +185,5 @@ class BuddyList(dict):
|
||||||
cur.execute("UPDATE numbers SET state = %s WHERE number = %s", (number['w'], number['n']))
|
cur.execute("UPDATE numbers SET state = %s WHERE number = %s", (number['w'], number['n']))
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
using += number['w']
|
using += number['w']
|
||||||
|
self.lock.release()
|
||||||
return using
|
return using
|
||||||
|
|
3
group.py
3
group.py
|
@ -29,6 +29,7 @@ class Group():
|
||||||
self.subject = subject
|
self.subject = subject
|
||||||
self.subjectOwner = subjectOwner
|
self.subjectOwner = subjectOwner
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
|
self.joined = False
|
||||||
|
|
||||||
self.nick = "me"
|
self.nick = "me"
|
||||||
self.participants = { }
|
self.participants = []
|
||||||
|
|
666
session.py
666
session.py
|
@ -27,6 +27,15 @@ import logging
|
||||||
import urllib
|
import urllib
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
import MySQLdb
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
from yowsup.common.tools import TimeTools
|
||||||
|
from yowsup.layers.protocol_media.mediauploader import MediaUploader
|
||||||
|
from yowsup.layers.protocol_media.mediadownloader import MediaDownloader
|
||||||
|
|
||||||
from Spectrum2 import protocol_pb2
|
from Spectrum2 import protocol_pb2
|
||||||
|
|
||||||
from buddy import BuddyList
|
from buddy import BuddyList
|
||||||
|
@ -36,6 +45,16 @@ from bot import Bot
|
||||||
from constants import *
|
from constants import *
|
||||||
from yowsupwrapper import YowsupApp
|
from yowsupwrapper import YowsupApp
|
||||||
|
|
||||||
|
|
||||||
|
class MsgIDs:
|
||||||
|
def __init__(self, xmppId, waId):
|
||||||
|
self.xmppId = xmppId
|
||||||
|
self.waId = waId
|
||||||
|
self.cnt = 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Session(YowsupApp):
|
class Session(YowsupApp):
|
||||||
|
|
||||||
def __init__(self, backend, user, legacyName, extra, db):
|
def __init__(self, backend, user, legacyName, extra, db):
|
||||||
|
@ -43,51 +62,75 @@ class Session(YowsupApp):
|
||||||
self.logger = logging.getLogger(self.__class__.__name__)
|
self.logger = logging.getLogger(self.__class__.__name__)
|
||||||
self.logger.info("Created: %s", legacyName)
|
self.logger.info("Created: %s", legacyName)
|
||||||
|
|
||||||
self.db = db
|
#self.db = db
|
||||||
|
self.db = MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_TABLE)
|
||||||
|
|
||||||
self.backend = backend
|
self.backend = backend
|
||||||
self.user = user
|
self.user = user
|
||||||
self.legacyName = legacyName
|
self.legacyName = legacyName
|
||||||
self.buddies = BuddyList(self.legacyName, self.db)
|
|
||||||
self.bot = Bot(self)
|
|
||||||
|
|
||||||
self.status = protocol_pb2.STATUS_NONE
|
self.status = protocol_pb2.STATUS_NONE
|
||||||
self.statusMessage = ''
|
self.statusMessage = ''
|
||||||
|
|
||||||
self.groups = {}
|
self.groups = {}
|
||||||
|
self.gotGroupList = False
|
||||||
|
self.joinRoomQueue = []
|
||||||
self.presenceRequested = []
|
self.presenceRequested = []
|
||||||
self.offlineQueue = []
|
self.offlineQueue = []
|
||||||
|
self.msgIDs = { }
|
||||||
self.groupOfflineQueue = { }
|
self.groupOfflineQueue = { }
|
||||||
|
self.shouldBeConnected = False
|
||||||
|
|
||||||
self.timer = None
|
self.timer = None
|
||||||
self.password = None
|
self.password = None
|
||||||
self.initialized = False
|
self.initialized = False
|
||||||
self.loggedin = False
|
self.lastMsgId = None
|
||||||
|
self.synced = False
|
||||||
|
|
||||||
|
self.buddies = BuddyList(self.legacyName, self.db)
|
||||||
self.bot = Bot(self)
|
self.bot = Bot(self)
|
||||||
|
|
||||||
|
self.imgMsgId = None
|
||||||
|
self.imgPath = ""
|
||||||
|
self.imgBuddy = None
|
||||||
|
self.imgType = ""
|
||||||
|
|
||||||
|
|
||||||
def __del__(self): # handleLogoutRequest
|
def __del__(self): # handleLogoutRequest
|
||||||
self.logout()
|
self.logout()
|
||||||
|
|
||||||
def call(self, method, **kwargs):
|
|
||||||
self.logger.debug("%s(%s)", method,
|
|
||||||
", ".join(str(k) + ': ' + str(v) for k, v in kwargs.items()))
|
|
||||||
##self.stack.broadcastEvent(YowLayerEvent(method, **kwargs))
|
|
||||||
|
|
||||||
def logout(self):
|
def logout(self):
|
||||||
self.loggedin = False
|
self.logger.info("%s logged out", self.user)
|
||||||
super(Session, self).logout()
|
super(Session, self).logout()
|
||||||
|
|
||||||
def login(self, password):
|
def login(self, password):
|
||||||
self.loggedin = True
|
self.logger.info("%s attempting login", self.user)
|
||||||
self.password = password
|
self.password = password
|
||||||
|
self.shouldBeConncted = True
|
||||||
super(Session, self).login(self.legacyName, self.password)
|
super(Session, self).login(self.legacyName, self.password)
|
||||||
|
|
||||||
|
def _shortenGroupId(self, gid):
|
||||||
|
# FIXME: might have problems if number begins with 0
|
||||||
|
return gid
|
||||||
|
# return '-'.join(hex(int(s))[2:] for s in gid.split('-'))
|
||||||
|
|
||||||
|
def _lengthenGroupId(self, gid):
|
||||||
|
return gid
|
||||||
|
# FIXME: might have problems if number begins with 0
|
||||||
|
# return '-'.join(str(int(s, 16)) for s in gid.split('-'))
|
||||||
|
|
||||||
def updateRoomList(self):
|
def updateRoomList(self):
|
||||||
rooms = []
|
rooms = []
|
||||||
|
text = []
|
||||||
for room, group in self.groups.iteritems():
|
for room, group in self.groups.iteritems():
|
||||||
rooms.append([room, group.subject])
|
rooms.append([self._shortenGroupId(room), group.subject])
|
||||||
|
text.append(self._shortenGroupId(room) + '@' + self.backend.spectrum_jid + ' :' + group.subject)
|
||||||
|
|
||||||
self.logger.debug("Got rooms: %s", rooms)
|
self.logger.debug("Got rooms: %s", rooms)
|
||||||
self.backend.handleRoomList(rooms)
|
self.backend.handleRoomList(rooms)
|
||||||
|
message = "Note, you are a participant of the following groups:\n" +\
|
||||||
|
'\n'.join(text) + '\nIf you do not join them you will lose messages'
|
||||||
|
#self.bot.send(message)
|
||||||
|
|
||||||
def updateRoster(self):
|
def updateRoster(self):
|
||||||
self.logger.debug("Update roster")
|
self.logger.debug("Update roster")
|
||||||
|
@ -95,6 +138,11 @@ class Session(YowsupApp):
|
||||||
old = self.buddies.keys()
|
old = self.buddies.keys()
|
||||||
self.buddies.load()
|
self.buddies.load()
|
||||||
new = self.buddies.keys()
|
new = self.buddies.keys()
|
||||||
|
contacts = new
|
||||||
|
|
||||||
|
if self.synced == False:
|
||||||
|
self.sendSync(contacts, delta = False, interactive = True)
|
||||||
|
self.synced = True
|
||||||
|
|
||||||
add = set(new) - set(old)
|
add = set(new) - set(old)
|
||||||
remove = set(old) - set(new)
|
remove = set(old) - set(new)
|
||||||
|
@ -103,27 +151,28 @@ class Session(YowsupApp):
|
||||||
self.logger.debug("Roster add: %s", str(list(add)))
|
self.logger.debug("Roster add: %s", str(list(add)))
|
||||||
|
|
||||||
for number in remove:
|
for number in remove:
|
||||||
self.backend.handleBuddyChanged(self.user, number, "", [], protocol_pb2.STATUS_NONE)
|
self.backend.handleBuddyChanged(self.user, number, "", [],
|
||||||
|
protocol_pb2.STATUS_NONE)
|
||||||
self.backend.handleBuddyRemoved(self.user, number)
|
self.backend.handleBuddyRemoved(self.user, number)
|
||||||
self.unsubscribePresence(number)
|
self.unsubscribePresence(number)
|
||||||
|
|
||||||
for number in add:
|
for number in add:
|
||||||
buddy = self.buddies[number]
|
buddy = self.buddies[number]
|
||||||
self.subscribePresence(number)
|
self.subscribePresence(number)
|
||||||
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 "")
|
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 "")
|
||||||
|
|
||||||
#self.requestLastSeen(number, self._lastSeen)
|
#self.requestLastSeen(number, self._lastSeen)
|
||||||
self.logger.debug('Requesting groups list')
|
|
||||||
self.requestGroupsList(self._updateGroups)
|
|
||||||
|
|
||||||
def _updateGroups(self, response, request):
|
def _updateGroups(self, response, request):
|
||||||
self.logger.debug('Received groups list %s', response)
|
self.logger.debug('Received groups list %s', response)
|
||||||
# This XMPP client is not receiving this for some reason.
|
|
||||||
groups = response.getGroups()
|
groups = response.getGroups()
|
||||||
for group in groups:
|
for group in groups:
|
||||||
room = group.getId()
|
room = group.getId()
|
||||||
owner = group.getOwner()
|
owner = group.getOwner().split('@')[0]
|
||||||
subjectOwner = group.getSubjectOwner()
|
subjectOwner = group.getSubjectOwner().split('@')[0]
|
||||||
subject = group.getSubject()
|
subject = utils.softToUni(group.getSubject())
|
||||||
|
|
||||||
if room in self.groups:
|
if room in self.groups:
|
||||||
oroom = self.groups[room]
|
oroom = self.groups[room]
|
||||||
|
@ -132,23 +181,82 @@ class Session(YowsupApp):
|
||||||
oroom.subject = subject
|
oroom.subject = subject
|
||||||
else:
|
else:
|
||||||
self.groups[room] = Group(room, owner, subject, subjectOwner)
|
self.groups[room] = Group(room, owner, subject, subjectOwner)
|
||||||
# A crude implemtation of groups that act like buddies
|
# self.joinRoom(self._shortenGroupId(room), self.user.split("@")[0])
|
||||||
|
self.groups[room].participants = group.getParticipants().keys()
|
||||||
|
|
||||||
self.backend.handleBuddyChanged(self.user, room, subject, [], protocol_pb2.STATUS_NONE)
|
#self._addParticipantsToRoom(room, group.getParticipants())
|
||||||
# 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)
|
|
||||||
|
|
||||||
|
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])
|
||||||
|
self.gotGroupList = True
|
||||||
|
for room, nick in self.joinRoomQueue:
|
||||||
|
self.joinRoom(room, nick)
|
||||||
|
self.joinRoomQueue = []
|
||||||
|
self.updateRoomList()
|
||||||
|
|
||||||
|
def joinRoom(self, room, nick):
|
||||||
|
if not self.gotGroupList:
|
||||||
|
self.joinRoomQueue.append((room, nick))
|
||||||
|
return
|
||||||
|
room = self._lengthenGroupId(room)
|
||||||
|
if room in self.groups:
|
||||||
|
self.logger.info("Joining room: %s room=%s, nick=%s",
|
||||||
|
self.legacyName, room, nick)
|
||||||
|
|
||||||
|
group = self.groups[room]
|
||||||
|
group.nick = nick
|
||||||
|
try:
|
||||||
|
ownerNick = self.buddies[group.subjectOwner].nick
|
||||||
|
except KeyError:
|
||||||
|
ownerNick = group.subjectOwner
|
||||||
|
|
||||||
|
self._refreshParticipants(room)
|
||||||
|
self.backend.handleSubject(self.user, self._shortenGroupId(room),
|
||||||
|
group.subject, ownerNick)
|
||||||
|
self.logger.debug("Room subject: room=%s, subject=%s",
|
||||||
|
room, group.subject)
|
||||||
|
self.backend.handleRoomNicknameChanged(
|
||||||
|
self.user, self._shortenGroupId(room), group.subject
|
||||||
|
)
|
||||||
|
group.joined = True
|
||||||
|
else:
|
||||||
|
self.logger.warn("Room doesn't exist: %s", room)
|
||||||
|
|
||||||
|
def leaveRoom(self, room):
|
||||||
|
if room in self.groups:
|
||||||
|
self.logger.info("Leaving room: %s room=%s", self.legacyName, room)
|
||||||
|
group = self.groups[room]
|
||||||
|
group.joined = False
|
||||||
|
else:
|
||||||
|
self.logger.warn("Room doesn't exist: %s. Unable to leave.", room)
|
||||||
|
|
||||||
|
def _refreshParticipants(self, room):
|
||||||
|
group = self.groups[room]
|
||||||
|
for jid in group.participants:
|
||||||
|
buddy = jid.split("@")[0]
|
||||||
|
self.logger.info("Added %s to room %s", buddy, room)
|
||||||
|
try:
|
||||||
|
nick = self.buddies[buddy].nick
|
||||||
|
except KeyError:
|
||||||
|
nick = buddy
|
||||||
|
if nick == "":
|
||||||
|
nick = buddy
|
||||||
|
|
||||||
|
if buddy == group.owner:
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_MODERATOR
|
||||||
|
else:
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_NONE
|
||||||
|
if buddy == self.legacyName:
|
||||||
|
nick = group.nick
|
||||||
|
flags = flags | protocol_pb2.PARTICIPANT_FLAG_ME
|
||||||
|
self.backend.handleParticipantChanged(
|
||||||
|
self.user, nick, self._shortenGroupId(room), flags,
|
||||||
|
protocol_pb2.STATUS_ONLINE, buddy)
|
||||||
|
|
||||||
def _lastSeen(self, number, seconds):
|
def _lastSeen(self, number, seconds):
|
||||||
self.logger.debug("Last seen %s at %s seconds" % (number, str(seconds)))
|
self.logger.debug("Last seen %s at %s seconds" % (number, str(seconds)))
|
||||||
|
@ -163,23 +271,29 @@ class Session(YowsupApp):
|
||||||
self.logger.info("Auth success: %s", self.user)
|
self.logger.info("Auth success: %s", self.user)
|
||||||
|
|
||||||
self.backend.handleConnected(self.user)
|
self.backend.handleConnected(self.user)
|
||||||
self.backend.handleBuddyChanged(self.user, "bot", self.bot.name, ["Admin"], protocol_pb2.STATUS_ONLINE)
|
self.backend.handleBuddyChanged(self.user, "bot", self.bot.name,
|
||||||
|
["Admin"], protocol_pb2.STATUS_ONLINE)
|
||||||
|
if self.initialized == False:
|
||||||
|
self.sendOfflineMessages()
|
||||||
|
#self.bot.call("welcome")
|
||||||
self.initialized = True
|
self.initialized = True
|
||||||
self.sendPresence(True)
|
self.sendPresence(True)
|
||||||
|
|
||||||
self.updateRoster()
|
self.updateRoster()
|
||||||
|
|
||||||
|
self.logger.debug('Requesting groups list')
|
||||||
|
self.requestGroupsList(self._updateGroups)
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onAuthFailed(self, reason):
|
def onAuthFailed(self, reason):
|
||||||
self.logger.info("Auth failed: %s (%s)", self.user, reason)
|
self.logger.info("Auth failed: %s (%s)", self.user, reason)
|
||||||
self.backend.handleDisconnected(self.user, 0, reason)
|
self.backend.handleDisconnected(self.user, 0, reason)
|
||||||
self.password = None
|
self.password = None
|
||||||
|
self.shouldBeConnected = False
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onDisconnect(self):
|
def onDisconnect(self):
|
||||||
self.logger.debug('Disconnected')
|
self.logger.debug('Disconnected')
|
||||||
self.backend.handleDisconnected(self.user, 0, 'Disconnected for unknown reasons')
|
self.backend.handleDisconnected(self.user, 0, 'Disconnected for unknown reasons')
|
||||||
self.loggedin = False
|
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onReceipt(self, _id, _from, timestamp, type, participant, offline, items):
|
def onReceipt(self, _id, _from, timestamp, type, participant, offline, items):
|
||||||
|
@ -187,9 +301,17 @@ class Session(YowsupApp):
|
||||||
' '.join(map(str, [_id, _from, timestamp,
|
' '.join(map(str, [_id, _from, timestamp,
|
||||||
type, participant, offline, items]))
|
type, participant, offline, items]))
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
buddy = self.buddies[_from.split('@')[0]]
|
buddy = self.buddies[_from.split('@')[0]]
|
||||||
self.backend.handleBuddyChanged(self.user, buddy.number.number,
|
#self.backend.handleBuddyChanged(self.user, buddy.number.number,
|
||||||
buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE)
|
# buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE)
|
||||||
|
self.backend.handleMessageAck(self.user, buddy.number.number, self.msgIDs[_id].xmppId)
|
||||||
|
self.msgIDs[_id].cnt = self.msgIDs[_id].cnt +1
|
||||||
|
if self.msgIDs[_id].cnt == 2:
|
||||||
|
del self.msgIDs[_id]
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onAck(self, _id, _class, _from, timestamp):
|
def onAck(self, _id, _class, _from, timestamp):
|
||||||
|
@ -198,7 +320,8 @@ class Session(YowsupApp):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onTextMessage(self, _id, _from, to, notify, timestamp, participant, offline, retry, body):
|
def onTextMessage(self, _id, _from, to, notify, timestamp, participant,
|
||||||
|
offline, retry, body):
|
||||||
self.logger.debug('received TextMessage' +
|
self.logger.debug('received TextMessage' +
|
||||||
' '.join(map(str, [
|
' '.join(map(str, [
|
||||||
_id, _from, to, notify, timestamp,
|
_id, _from, to, notify, timestamp,
|
||||||
|
@ -210,10 +333,21 @@ class Session(YowsupApp):
|
||||||
self.sendReceipt(_id, _from, None, participant)
|
self.sendReceipt(_id, _from, None, participant)
|
||||||
self.logger.info("Message received from %s to %s: %s (at ts=%s)",
|
self.logger.info("Message received from %s to %s: %s (at ts=%s)",
|
||||||
buddy, self.legacyName, messageContent, timestamp)
|
buddy, self.legacyName, messageContent, timestamp)
|
||||||
if participant is not None:
|
if participant is not None: # Group message
|
||||||
partname = participant.split('@')[0]
|
partname = participant.split('@')[0]
|
||||||
message = partname + ': ' + messageContent
|
try:
|
||||||
self.sendMessageToXMPP(buddy, message, timestamp)
|
part = self.buddies[partname]
|
||||||
|
if part.nick == "":
|
||||||
|
part.nick = notify
|
||||||
|
self.backend.handleParticipantChanged(
|
||||||
|
self.user, partname, self._shortenGroupId(buddy),
|
||||||
|
protocol_pb2.PARTICIPANT_FLAG_NONE,
|
||||||
|
protocol_pb2.STATUS_ONLINE, "", part.nick
|
||||||
|
) # TODO
|
||||||
|
except KeyError:
|
||||||
|
self.updateBuddy(partname, notify, [])
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, messageContent,
|
||||||
|
timestamp)
|
||||||
else:
|
else:
|
||||||
self.sendMessageToXMPP(buddy, messageContent, timestamp)
|
self.sendMessageToXMPP(buddy, messageContent, timestamp)
|
||||||
# isBroadcast always returns false, I'm not sure how to get a broadcast
|
# isBroadcast always returns false, I'm not sure how to get a broadcast
|
||||||
|
@ -227,7 +361,15 @@ class Session(YowsupApp):
|
||||||
def onImage(self, image):
|
def onImage(self, image):
|
||||||
self.logger.debug('Received image message %s', str(image))
|
self.logger.debug('Received image message %s', str(image))
|
||||||
buddy = image._from.split('@')[0]
|
buddy = image._from.split('@')[0]
|
||||||
|
participant = image.participant
|
||||||
|
if image.caption is None:
|
||||||
|
image.caption = ''
|
||||||
message = image.url + ' ' + image.caption
|
message = image.url + ' ' + image.caption
|
||||||
|
if participant is not None: # Group message
|
||||||
|
partname = participant.split('@')[0]
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, message, image.timestamp)
|
||||||
|
else:
|
||||||
|
|
||||||
self.sendMessageToXMPP(buddy, message, image.timestamp)
|
self.sendMessageToXMPP(buddy, message, image.timestamp)
|
||||||
self.sendReceipt(image._id, image._from, None, image.participant)
|
self.sendReceipt(image._id, image._from, None, image.participant)
|
||||||
|
|
||||||
|
@ -235,7 +377,13 @@ class Session(YowsupApp):
|
||||||
def onAudio(self, audio):
|
def onAudio(self, audio):
|
||||||
self.logger.debug('Received audio message %s', str(audio))
|
self.logger.debug('Received audio message %s', str(audio))
|
||||||
buddy = audio._from.split('@')[0]
|
buddy = audio._from.split('@')[0]
|
||||||
|
participant = audio.participant
|
||||||
message = audio.url
|
message = audio.url
|
||||||
|
if participant is not None: # Group message
|
||||||
|
partname = participant.split('@')[0]
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, message, audio.timestamp)
|
||||||
|
else:
|
||||||
|
|
||||||
self.sendMessageToXMPP(buddy, message, audio.timestamp)
|
self.sendMessageToXMPP(buddy, message, audio.timestamp)
|
||||||
self.sendReceipt(audio._id, audio._from, None, audio.participant)
|
self.sendReceipt(audio._id, audio._from, None, audio.participant)
|
||||||
|
|
||||||
|
@ -243,10 +391,40 @@ class Session(YowsupApp):
|
||||||
def onVideo(self, video):
|
def onVideo(self, video):
|
||||||
self.logger.debug('Received video message %s', str(video))
|
self.logger.debug('Received video message %s', str(video))
|
||||||
buddy = video._from.split('@')[0]
|
buddy = video._from.split('@')[0]
|
||||||
|
participant = video.participant
|
||||||
|
|
||||||
message = video.url
|
message = video.url
|
||||||
|
if participant is not None: # Group message
|
||||||
|
partname = participant.split('@')[0]
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, message, video.timestamp)
|
||||||
|
else:
|
||||||
|
|
||||||
self.sendMessageToXMPP(buddy, message, video.timestamp)
|
self.sendMessageToXMPP(buddy, message, video.timestamp)
|
||||||
self.sendReceipt(video._id, video._from, None, video.participant)
|
self.sendReceipt(video._id, video._from, None, video.participant)
|
||||||
|
|
||||||
|
def onLocation(self, location):
|
||||||
|
buddy = location._from.split('@')[0]
|
||||||
|
latitude = location.getLatitude()
|
||||||
|
longitude = location.getLongitude()
|
||||||
|
url = location.getLocationUrl()
|
||||||
|
participant = location.participant
|
||||||
|
|
||||||
|
self.logger.debug("Location received from %s: %s, %s",
|
||||||
|
buddy, latitude, longitude)
|
||||||
|
|
||||||
|
if participant is not None: # Group message
|
||||||
|
partname = participant.split('@')[0]
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, url, location.timestamp)
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, 'geo:' + latitude + ',' + longitude,
|
||||||
|
location.timestamp)
|
||||||
|
else:
|
||||||
|
|
||||||
|
self.sendMessageToXMPP(buddy, url, location.timestamp)
|
||||||
|
self.sendMessageToXMPP(buddy, 'geo:' + latitude + ',' + longitude,
|
||||||
|
location.timestamp)
|
||||||
|
self.sendReceipt(location._id, location._from, None, location.participant, location.timestamp)
|
||||||
|
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
||||||
self.logger.debug('received VCard' +
|
self.logger.debug('received VCard' +
|
||||||
|
@ -255,9 +433,14 @@ class Session(YowsupApp):
|
||||||
]))
|
]))
|
||||||
)
|
)
|
||||||
buddy = _from.split("@")[0]
|
buddy = _from.split("@")[0]
|
||||||
|
if participant is not None: # Group message
|
||||||
|
partname = participant.split('@')[0]
|
||||||
|
self.sendGroupMessageToXMPP(buddy, partname, "Received VCard (not implemented yet)", timestamp)
|
||||||
|
else:
|
||||||
|
|
||||||
self.sendMessageToXMPP(buddy, "Received VCard (not implemented yet)")
|
self.sendMessageToXMPP(buddy, "Received VCard (not implemented yet)")
|
||||||
self.sendMessageToXMPP(buddy, card_data)
|
# self.sendMessageToXMPP(buddy, card_data)
|
||||||
self.transferFile(buddy, str(name), card_data)
|
#self.transferFile(buddy, str(name), card_data)
|
||||||
self.sendReceipt(_id, _from, None, participant)
|
self.sendReceipt(_id, _from, None, participant)
|
||||||
|
|
||||||
def transferFile(self, buddy, name, data):
|
def transferFile(self, buddy, name, data):
|
||||||
|
@ -270,6 +453,7 @@ class Session(YowsupApp):
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onContactTyping(self, buddy):
|
def onContactTyping(self, buddy):
|
||||||
self.logger.info("Started typing: %s", buddy)
|
self.logger.info("Started typing: %s", buddy)
|
||||||
|
if buddy != 'bot':
|
||||||
self.sendPresence(True)
|
self.sendPresence(True)
|
||||||
self.backend.handleBuddyTyping(self.user, buddy)
|
self.backend.handleBuddyTyping(self.user, buddy)
|
||||||
|
|
||||||
|
@ -279,40 +463,93 @@ class Session(YowsupApp):
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onContactPaused(self, buddy):
|
def onContactPaused(self, buddy):
|
||||||
self.logger.info("Paused typing: %s", buddy)
|
self.logger.info("Paused typing: %s", buddy)
|
||||||
|
if buddy != 'bot':
|
||||||
self.backend.handleBuddyTyped(self.user, buddy)
|
self.backend.handleBuddyTyped(self.user, buddy)
|
||||||
self.timer = Timer(3, self.backend.handleBuddyStoppedTyping, (self.user, buddy)).start()
|
self.timer = Timer(3, self.backend.handleBuddyStoppedTyping,
|
||||||
|
(self.user, buddy)).start()
|
||||||
|
|
||||||
|
# Called by superclass
|
||||||
|
def onAddedToGroup(self, group):
|
||||||
|
self.logger.debug("Added to group: %s", group)
|
||||||
|
room = group.getGroupId()
|
||||||
|
owner = group.getCreatorJid(full = False)
|
||||||
|
subjectOwner = group.getSubjectOwnerJid(full = False)
|
||||||
|
subject = utils.softToUni(group.getSubject())
|
||||||
|
|
||||||
|
self.groups[room] = Group(room, owner, subject, subjectOwner)
|
||||||
|
self.groups[room].participants = group.getParticipants().keys()
|
||||||
|
# self.joinRoom(self._shortenGroupId(room), self.user.split("@")[0])
|
||||||
|
|
||||||
|
#self._addParticipantsToRoom(room, group.getParticipants())
|
||||||
|
self.bot.send("You have been added to group: %s@%s (%s)"
|
||||||
|
% (self._shortenGroupId(room), subject, self.backend.spectrum_jid))
|
||||||
|
|
||||||
|
# Called by superclass
|
||||||
|
def onParticipantsAddedToGroup(self, group):
|
||||||
|
self.logger.debug("Participants added to group: %s", group)
|
||||||
|
room = group.getGroupId().split('@')[0]
|
||||||
|
self.groups[room].participants.extend(group.getParticipants())
|
||||||
|
self._refreshParticipants(room)
|
||||||
|
|
||||||
|
# Called by superclass
|
||||||
|
def onParticipantsRemovedFromGroup(self, room, participants):
|
||||||
|
self.logger.debug("Participants removed from group: %s, %s",
|
||||||
|
room, participants)
|
||||||
|
group = self.groups[room]
|
||||||
|
for jid in participants:
|
||||||
|
group.participants.remove(jid)
|
||||||
|
buddy = jid.split("@")[0]
|
||||||
|
try:
|
||||||
|
nick = self.buddies[buddy].nick
|
||||||
|
except KeyError:
|
||||||
|
nick = buddy
|
||||||
|
if nick == "":
|
||||||
|
nick = buddy
|
||||||
|
if buddy == self.legacyName:
|
||||||
|
nick = group.nick
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_NONE
|
||||||
|
self.backend.handleParticipantChanged(
|
||||||
|
self.user, nick, self._shortenGroupId(room), flags,
|
||||||
|
protocol_pb2.STATUS_NONE, buddy)
|
||||||
|
|
||||||
def onPresenceReceived(self, _type, name, jid, lastseen):
|
def onPresenceReceived(self, _type, name, jid, lastseen):
|
||||||
self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen)
|
self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen)
|
||||||
buddy = jid.split("@")[0]
|
buddy = jid.split("@")[0]
|
||||||
# seems to be causing an error
|
try:
|
||||||
# self.logger.info("Lastseen: %s %s", buddy, utils.ago(lastseen))
|
buddy = self.buddies[buddy]
|
||||||
|
except KeyError:
|
||||||
|
self.logger.error("Buddy not found: %s", buddy)
|
||||||
|
return
|
||||||
|
|
||||||
if buddy in self.presenceRequested:
|
if (lastseen == str(buddy.lastseen)) and (_type == buddy.presence):
|
||||||
timestamp = time.localtime(time.time() - lastseen)
|
return
|
||||||
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:
|
if ((lastseen != "deny") and (lastseen != None) and (lastseen != "none")):
|
||||||
self.onPresenceAvailable(buddy)
|
buddy.lastseen = int(lastseen)
|
||||||
|
if (_type == None):
|
||||||
|
buddy.lastseen = time.time()
|
||||||
|
|
||||||
|
buddy.presence = _type
|
||||||
|
|
||||||
|
timestamp = time.localtime(buddy.lastseen)
|
||||||
|
statusmsg = buddy.statusMsg + time.strftime("\n Last seen: %a, %d %b %Y %H:%M:%S", timestamp)
|
||||||
|
|
||||||
|
if _type == "unavailable":
|
||||||
|
self.onPresenceUnavailable(buddy, statusmsg)
|
||||||
else:
|
else:
|
||||||
self.onPresenceUnavailable(buddy)
|
self.onPresenceAvailable(buddy, statusmsg)
|
||||||
|
|
||||||
def onPresenceAvailable(self, buddy):
|
|
||||||
try:
|
|
||||||
buddy = self.buddies[buddy]
|
def onPresenceAvailable(self, buddy, statusmsg):
|
||||||
self.logger.info("Is available: %s", buddy)
|
self.logger.info("Is available: %s", buddy)
|
||||||
self.backend.handleBuddyChanged(self.user, buddy.number.number, buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE)
|
self.backend.handleBuddyChanged(self.user, buddy.number.number,
|
||||||
except KeyError:
|
buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE, statusmsg, buddy.image_hash)
|
||||||
self.logger.error("Buddy not found: %s", buddy)
|
|
||||||
|
|
||||||
def onPresenceUnavailable(self, buddy):
|
def onPresenceUnavailable(self, buddy, statusmsg):
|
||||||
try:
|
|
||||||
buddy = self.buddies[buddy]
|
|
||||||
self.logger.info("Is unavailable: %s", buddy)
|
self.logger.info("Is unavailable: %s", buddy)
|
||||||
self.backend.handleBuddyChanged(self.user, buddy.number.number, buddy.nick, buddy.groups, protocol_pb2.STATUS_XA)
|
self.backend.handleBuddyChanged(self.user, buddy.number.number,
|
||||||
except KeyError:
|
buddy.nick, buddy.groups, protocol_pb2.STATUS_AWAY, statusmsg, buddy.image_hash)
|
||||||
self.logger.error("Buddy not found: %s", buddy)
|
|
||||||
|
|
||||||
# spectrum RequestMethods
|
# spectrum RequestMethods
|
||||||
def sendTypingStarted(self, buddy):
|
def sendTypingStarted(self, buddy):
|
||||||
|
@ -329,29 +566,145 @@ class Session(YowsupApp):
|
||||||
self.logger.info("Stopped typing: %s to %s", self.legacyName, buddy)
|
self.logger.info("Stopped typing: %s to %s", self.legacyName, buddy)
|
||||||
self.sendTyping(buddy, False)
|
self.sendTyping(buddy, False)
|
||||||
|
|
||||||
def sendMessageToWA(self, sender, message):
|
def sendMessageToWA(self, sender, message, ID):
|
||||||
self.logger.info("Message sent from %s to %s: %s", self.legacyName, sender, message)
|
self.logger.info("Message sent from %s to %s: %s",
|
||||||
|
self.legacyName, sender, message)
|
||||||
|
|
||||||
message = message.encode("utf-8")
|
message = message.encode("utf-8")
|
||||||
|
# FIXME: Fragile, should pass this in to onDlerror
|
||||||
|
self.dlerror_message = message
|
||||||
|
self.dlerror_sender = sender
|
||||||
|
self.dlerror_ID = ID
|
||||||
|
# End Fragile
|
||||||
|
|
||||||
if sender == "bot":
|
if sender == "bot":
|
||||||
self.bot.parse(message)
|
self.bot.parse(message)
|
||||||
elif "-" in sender: # group msg
|
elif "-" in sender: # group msg
|
||||||
if "/" in sender:
|
if "/" in sender: # directed at single user
|
||||||
room, buddy = sender.split("/")
|
room, nick = sender.split("/")
|
||||||
self.sendTextMessage(buddy + '@s.whatsapp.net', message)
|
for buddy, buddy3 in self.buddies.iteritems():
|
||||||
|
self.logger.info("Group buddy=%s nick=%s", buddy,
|
||||||
|
buddy3.nick)
|
||||||
|
if buddy3.nick == nick:
|
||||||
|
nick = buddy
|
||||||
|
waId = self.sendTextMessage(nick + '@s.whatsapp.net', message)
|
||||||
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
||||||
else:
|
else:
|
||||||
room = sender
|
room = sender
|
||||||
# group = self.groups[room]
|
if message[0] == '\\' and message[:1] != '\\\\':
|
||||||
|
self.logger.debug("Executing command %s in %s", message, room)
|
||||||
|
self.executeCommand(message, room)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
group = self.groups[self._lengthenGroupId(room)]
|
||||||
|
self.logger.debug("Group Message from %s to %s Groups: %s",
|
||||||
|
group.nick , group , self.groups)
|
||||||
|
self.backend.handleMessage(
|
||||||
|
self.user, room, message.decode('utf-8'), group.nick
|
||||||
|
)
|
||||||
|
except KeyError:
|
||||||
|
self.logger.error('Group not found: %s', room)
|
||||||
|
|
||||||
# self.backend.handleMessage(self.user, room, message, group.nick)
|
if (".jpg" in message.lower()) or (".webp" in message.lower()):
|
||||||
self.sendTextMessage(room + '@g.us', message)
|
if (".jpg" in message.lower()):
|
||||||
|
self.imgType = "jpg"
|
||||||
|
if (".webp" in message.lower()):
|
||||||
|
self.imgType = "webp"
|
||||||
|
self.imgMsgId = ID
|
||||||
|
self.imgBuddy = room + "@g.us"
|
||||||
|
|
||||||
|
|
||||||
|
downloader = MediaDownloader(self.onDlsuccess, self.onDlerror)
|
||||||
|
downloader.download(message)
|
||||||
|
#self.imgMsgId = ID
|
||||||
|
#self.imgBuddy = room + "@g.us"
|
||||||
|
elif "geo:" in message.lower():
|
||||||
|
self._sendLocation(room + "@g.us", message, ID)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
self.sendTextMessage(self._lengthenGroupId(room) + '@g.us', message)
|
||||||
else: # private msg
|
else: # private msg
|
||||||
buddy = sender
|
buddy = sender
|
||||||
# if message == "\\lastseen":
|
# if message == "\\lastseen":
|
||||||
# self.call("presence_request", buddy = (buddy + "@s.whatsapp.net",))
|
# self.call("presence_request", buddy = (buddy + "@s.whatsapp.net",))
|
||||||
# else:
|
# else:
|
||||||
self.sendTextMessage(sender + '@s.whatsapp.net', message)
|
if message.split(" ").pop(0) == "\\lastseen":
|
||||||
|
self.presenceRequested.append(buddy)
|
||||||
|
#self.call("presence_request", (buddy + "@s.whatsapp.net",))
|
||||||
|
self._requestLastSeen(buddy)
|
||||||
|
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",))
|
||||||
|
self.requestVCard(buddy)
|
||||||
|
else:
|
||||||
|
if (".jpg" in message.lower()) or (".webp" in message.lower()):
|
||||||
|
#waId = self.call("message_imageSend", (buddy + "@s.whatsapp.net", message, None, 0, None))
|
||||||
|
#waId = self.call("message_send", (buddy + "@s.whatsapp.net", message))
|
||||||
|
if (".jpg" in message.lower()):
|
||||||
|
self.imgType = "jpg"
|
||||||
|
if (".webp" in message.lower()):
|
||||||
|
self.imgType = "webp"
|
||||||
|
self.imgMsgId = ID
|
||||||
|
self.imgBuddy = buddy + "@s.whatsapp.net"
|
||||||
|
|
||||||
|
downloader = MediaDownloader(self.onDlsuccess, self.onDlerror)
|
||||||
|
downloader.download(message)
|
||||||
|
#self.imgMsgId = ID
|
||||||
|
#self.imgBuddy = buddy + "@s.whatsapp.net"
|
||||||
|
elif "geo:" in message.lower():
|
||||||
|
self._sendLocation(buddy + "@s.whatsapp.net", message, ID)
|
||||||
|
else:
|
||||||
|
waId = self.sendTextMessage(sender + '@s.whatsapp.net', message)
|
||||||
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
||||||
|
|
||||||
|
self.logger.info("WA Message send to %s with ID %s", buddy, waId)
|
||||||
|
#self.sendTextMessage(sender + '@s.whatsapp.net', message)
|
||||||
|
|
||||||
|
def executeCommand(self, command, room):
|
||||||
|
if command == '\\leave':
|
||||||
|
self.logger.debug("Leaving room %s", room)
|
||||||
|
# Leave group on whatsapp side
|
||||||
|
self.leaveGroup(room)
|
||||||
|
# Delete Room on spectrum side
|
||||||
|
group = self.groups[room]
|
||||||
|
for jid in group.participants:
|
||||||
|
buddy = jid.split("@")[0]
|
||||||
|
try:
|
||||||
|
nick = self.buddies[buddy].nick
|
||||||
|
except KeyError:
|
||||||
|
nick = buddy
|
||||||
|
if nick == "":
|
||||||
|
nick = buddy
|
||||||
|
if buddy == self.legacyName:
|
||||||
|
nick = group.nick
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_ROOM_NOT_FOUND
|
||||||
|
self.backend.handleParticipantChanged(
|
||||||
|
self.user, nick, self._shortenGroupId(room), flags,
|
||||||
|
protocol_pb2.STATUS_NONE, buddy)
|
||||||
|
del self.groups[room]
|
||||||
|
|
||||||
|
def _requestLastSeen(self, buddy):
|
||||||
|
|
||||||
|
def onSuccess(buddy, lastseen):
|
||||||
|
timestamp = time.localtime(time.localtime()-lastseen)
|
||||||
|
timestring = time.strftime("%a, %d %b %Y %H:%M:%S", timestamp)
|
||||||
|
self.sendMessageToXMPP(buddy, "%s (%s) %s" % (timestring, utils.ago(lastseen),str(lastseen)))
|
||||||
|
def onError(errorIqEntity, originalIqEntity):
|
||||||
|
self.sendMessageToXMPP(errorIqEntity.getFrom(), "LastSeen Error")
|
||||||
|
|
||||||
|
self.requestLastSeen(buddy, onSuccess, onError)
|
||||||
|
|
||||||
|
def _sendLocation(self, buddy, message, ID):
|
||||||
|
#with open('/opt/transwhat/map.jpg', 'rb') as imageFile:
|
||||||
|
# raw = base64.b64encode(imageFile.read())
|
||||||
|
latitude,longitude = message.split(':')[1].split(',')
|
||||||
|
waId = self.sendLocation(buddy, float(latitude), float(longitude))
|
||||||
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
||||||
|
self.logger.info("WA Location Message send to %s with ID %s", buddy, waId)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def sendMessageToXMPP(self, buddy, messageContent, timestamp = "", nickname = ""):
|
def sendMessageToXMPP(self, buddy, messageContent, timestamp = "", nickname = ""):
|
||||||
if timestamp:
|
if timestamp:
|
||||||
|
@ -368,26 +721,53 @@ class Session(YowsupApp):
|
||||||
"", timestamp)
|
"", timestamp)
|
||||||
|
|
||||||
def sendGroupMessageToXMPP(self, room, buddy, messageContent, timestamp = ""):
|
def sendGroupMessageToXMPP(self, room, buddy, messageContent, timestamp = ""):
|
||||||
|
# self._refreshParticipants(room)
|
||||||
|
try:
|
||||||
|
nick = self.buddies[buddy].nick
|
||||||
|
except KeyError:
|
||||||
|
nick = buddy
|
||||||
|
if nick == "":
|
||||||
|
nick = buddy
|
||||||
|
|
||||||
if timestamp:
|
if timestamp:
|
||||||
timestamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime(timestamp))
|
timestamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime(timestamp))
|
||||||
|
|
||||||
if self.initialized == False:
|
if self.initialized == False:
|
||||||
self.logger.debug("Group message queued from %s to %s: %s", buddy, room, messageContent)
|
self.logger.debug("Group message queued from %s to %s: %s",
|
||||||
|
buddy, room, messageContent)
|
||||||
|
|
||||||
if room not in self.groupOfflineQueue:
|
if room not in self.groupOfflineQueue:
|
||||||
self.groupOfflineQueue[room] = [ ]
|
self.groupOfflineQueue[room] = [ ]
|
||||||
|
|
||||||
self.groupOfflineQueue[room].append((buddy, messageContent, timestamp))
|
self.groupOfflineQueue[room].append(
|
||||||
|
(buddy, messageContent, timestamp)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.logger.debug("Group message sent from %s to %s: %s", buddy, room, messageContent)
|
self.logger.debug("Group message sent from %s (%s) to %s: %s",
|
||||||
self.backend.handleMessage(self.user, room, messageContent, buddy, "", timestamp)
|
buddy, nick, room, messageContent)
|
||||||
|
try:
|
||||||
|
group = self.groups[room]
|
||||||
|
if group.joined:
|
||||||
|
self.backend.handleMessage(self.user,room, messageContent,
|
||||||
|
nick, "", timestamp)
|
||||||
|
else:
|
||||||
|
self.bot.send("You have received a message in group: %s@%s"
|
||||||
|
% (room, self.backend.spectrum_jid))
|
||||||
|
self.bot.send("Join the group in order to reply")
|
||||||
|
self.bot.send("%s: %s" % (nick, messageContent))
|
||||||
|
except KeyError:
|
||||||
|
self.logger.warn("Group is not in group list")
|
||||||
|
self.backend.handleMessage(self.user, self._shortenGroupId(room),
|
||||||
|
messageContent, nick, "", timestamp)
|
||||||
|
|
||||||
|
|
||||||
def changeStatus(self, status):
|
def changeStatus(self, status):
|
||||||
if status != self.status:
|
if status != self.status:
|
||||||
self.logger.info("Status changed: %s", status)
|
self.logger.info("Status changed: %s", status)
|
||||||
self.status = status
|
self.status = status
|
||||||
|
|
||||||
if status == protocol_pb2.STATUS_ONLINE or status == protocol_pb2.STATUS_FFC:
|
if status == protocol_pb2.STATUS_ONLINE \
|
||||||
|
or status == protocol_pb2.STATUS_FFC:
|
||||||
self.sendPresence(True)
|
self.sendPresence(True)
|
||||||
else:
|
else:
|
||||||
self.sendPresence(False)
|
self.sendPresence(False)
|
||||||
|
@ -398,10 +778,10 @@ class Session(YowsupApp):
|
||||||
self.setStatus(statusMessage.encode('utf-8'))
|
self.setStatus(statusMessage.encode('utf-8'))
|
||||||
self.logger.info("Status message changed: %s", statusMessage)
|
self.logger.info("Status message changed: %s", statusMessage)
|
||||||
|
|
||||||
if self.initialized == False:
|
#if self.initialized == False:
|
||||||
self.sendOfflineMessages()
|
# self.sendOfflineMessages()
|
||||||
self.bot.call("welcome")
|
# self.bot.call("welcome")
|
||||||
self.initialized = True
|
# self.initialized = True
|
||||||
|
|
||||||
def sendOfflineMessages(self):
|
def sendOfflineMessages(self):
|
||||||
# Flush Queues
|
# Flush Queues
|
||||||
|
@ -410,9 +790,10 @@ class Session(YowsupApp):
|
||||||
self.backend.handleMessage(self.user, msg[0], msg[1], "", "", msg[2])
|
self.backend.handleMessage(self.user, msg[0], msg[1], "", "", msg[2])
|
||||||
|
|
||||||
# also for adding a new buddy
|
# also for adding a new buddy
|
||||||
def updateBuddy(self, buddy, nick, groups, image_hash =""):
|
def updateBuddy(self, buddy, nick, groups, image_hash = None):
|
||||||
if buddy != "bot":
|
if buddy != "bot":
|
||||||
self.buddies.update(buddy, nick, groups, image_hash)
|
self.buddies.update(buddy, nick, groups, image_hash)
|
||||||
|
if self.initialized == True:
|
||||||
self.updateRoster()
|
self.updateRoster()
|
||||||
|
|
||||||
def removeBuddy(self, buddy):
|
def removeBuddy(self, buddy):
|
||||||
|
@ -421,26 +802,14 @@ class Session(YowsupApp):
|
||||||
self.buddies.remove(buddy)
|
self.buddies.remove(buddy)
|
||||||
self.updateRoster()
|
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)
|
|
||||||
|
|
||||||
def requestVCard(self, buddy, ID):
|
|
||||||
|
|
||||||
|
def requestVCard(self, buddy, ID=None):
|
||||||
def onSuccess(response, request):
|
def onSuccess(response, request):
|
||||||
self.logger.debug('Sending VCard (%s) with image id %s',
|
self.logger.debug('Sending VCard (%s) with image id %s',
|
||||||
ID, response.pictureId)
|
ID, response.pictureId)
|
||||||
image_hash = utils.sha1hash(response.pictureData)
|
image_hash = utils.sha1hash(response.pictureData)
|
||||||
self.logger.debug('Image hash is %s', image_hash)
|
self.logger.debug('Image hash is %s', image_hash)
|
||||||
|
if ID != None:
|
||||||
self.backend.handleVCard(self.user, ID, buddy, "", "", response.pictureData)
|
self.backend.handleVCard(self.user, ID, buddy, "", "", response.pictureData)
|
||||||
obuddy = self.buddies[buddy]
|
obuddy = self.buddies[buddy]
|
||||||
self.updateBuddy(buddy, obuddy.nick, obuddy.groups, image_hash)
|
self.updateBuddy(buddy, obuddy.nick, obuddy.groups, image_hash)
|
||||||
|
@ -448,6 +817,68 @@ class Session(YowsupApp):
|
||||||
self.logger.debug('Requesting profile picture of %s', buddy)
|
self.logger.debug('Requesting profile picture of %s', buddy)
|
||||||
self.requestProfilePicture(buddy, onSuccess = onSuccess)
|
self.requestProfilePicture(buddy, onSuccess = onSuccess)
|
||||||
|
|
||||||
|
def onDlsuccess(self, path):
|
||||||
|
self.logger.info("Success: Image downloaded to %s", path)
|
||||||
|
os.rename(path, path+"."+self.imgType)
|
||||||
|
if self.imgType != "jpg":
|
||||||
|
im = Image.open(path+"."+self.imgType)
|
||||||
|
im.save(path+".jpg")
|
||||||
|
self.imgPath = path+".jpg"
|
||||||
|
statinfo = os.stat(self.imgPath)
|
||||||
|
name=os.path.basename(self.imgPath)
|
||||||
|
self.logger.info("Buddy %s",self.imgBuddy)
|
||||||
|
self.image_send(self.imgBuddy, self.imgPath)
|
||||||
|
|
||||||
|
#self.logger.info("Sending picture %s of size %s with name %s",self.imgPath, statinfo.st_size, name)
|
||||||
|
#mtype = "image"
|
||||||
|
|
||||||
|
#sha1 = hashlib.sha256()
|
||||||
|
#fp = open(self.imgPath, 'rb')
|
||||||
|
#try:
|
||||||
|
# sha1.update(fp.read())
|
||||||
|
# hsh = base64.b64encode(sha1.digest())
|
||||||
|
# self.call("media_requestUpload", (hsh, mtype, os.path.getsize(self.imgPath)))
|
||||||
|
#finally:
|
||||||
|
# fp.close()
|
||||||
|
|
||||||
|
|
||||||
|
def onDlerror(self):
|
||||||
|
self.logger.info("Download Error. Sending message as is.")
|
||||||
|
waId = self.sendTextMessage(self.dlerror_sender + '@s.whatsapp.net', self.dlerror_message)
|
||||||
|
self.msgIDs[waId] = MsgIDs(self.dlerror_ID, waId)
|
||||||
|
|
||||||
|
|
||||||
|
def _doSendImage(self, filePath, url, to, ip = None, caption = None):
|
||||||
|
waId = self.doSendImage(filePath, url, to, ip, caption)
|
||||||
|
self.msgIDs[waId] = MsgIDs(self.imgMsgId, waId)
|
||||||
|
|
||||||
|
def _doSendAudio(self, filePath, url, to, ip = None, caption = None):
|
||||||
|
waId = self.doSendAudio(filePath, url, to, ip, caption)
|
||||||
|
self.msgIDs[waId] = MsgIDs(self.imgMsgId, waId)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def createThumb(self, size=100, raw=False):
|
||||||
|
img = Image.open(self.imgPath)
|
||||||
|
width, height = img.size
|
||||||
|
img_thumbnail = self.imgPath + '_thumbnail'
|
||||||
|
|
||||||
|
if width > height:
|
||||||
|
nheight = float(height) / width * size
|
||||||
|
nwidth = size
|
||||||
|
else:
|
||||||
|
nwidth = float(width) / height * size
|
||||||
|
nheight = size
|
||||||
|
|
||||||
|
img.thumbnail((nwidth, nheight), Image.ANTIALIAS)
|
||||||
|
img.save(img_thumbnail, 'JPEG')
|
||||||
|
|
||||||
|
with open(img_thumbnail, 'rb') as imageFile:
|
||||||
|
raw = base64.b64encode(imageFile.read())
|
||||||
|
|
||||||
|
return raw
|
||||||
|
|
||||||
# Not used
|
# Not used
|
||||||
def onLocationReceived(self, messageId, jid, name, preview, latitude, longitude, receiptRequested, isBroadcast):
|
def onLocationReceived(self, messageId, jid, name, preview, latitude, longitude, receiptRequested, isBroadcast):
|
||||||
buddy = jid.split("@")[0]
|
buddy = jid.split("@")[0]
|
||||||
|
@ -458,36 +889,6 @@ class Session(YowsupApp):
|
||||||
if receiptRequested: self.call("message_ack", (jid, messageId))
|
if receiptRequested: self.call("message_ack", (jid, messageId))
|
||||||
|
|
||||||
|
|
||||||
def onGroupGotParticipants(self, gjid, jids):
|
|
||||||
room = gjid.split("@")[0]
|
|
||||||
group = self.groups[room]
|
|
||||||
|
|
||||||
for jid in jids:
|
|
||||||
buddy = jid.split("@")[0]
|
|
||||||
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
|
|
||||||
|
|
||||||
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])
|
|
||||||
|
|
||||||
def onGroupMessageReceived(self, messageId, gjid, jid, messageContent, timestamp, receiptRequested, pushName):
|
|
||||||
buddy = jid.split("@")[0]
|
|
||||||
room = gjid.split("@")[0]
|
|
||||||
|
|
||||||
self.logger.info("Group message received in %s from %s: %s", room, buddy, messageContent)
|
|
||||||
|
|
||||||
self.sendGroupMessageToXMPP(room, buddy, utils.softToUni(messageContent), timestamp)
|
|
||||||
if receiptRequested: self.call("message_ack", (gjid, messageId))
|
|
||||||
|
|
||||||
def onGroupSubjectReceived(self, messageId, gjid, jid, subject, timestamp, receiptRequested):
|
def onGroupSubjectReceived(self, messageId, gjid, jid, subject, timestamp, receiptRequested):
|
||||||
room = gjid.split("@")[0]
|
room = gjid.split("@")[0]
|
||||||
buddy = jid.split("@")[0]
|
buddy = jid.split("@")[0]
|
||||||
|
@ -512,3 +913,6 @@ class Session(YowsupApp):
|
||||||
def onGroupPictureUpdated(self, jid, author, timestamp, messageId, pictureId, receiptRequested):
|
def onGroupPictureUpdated(self, jid, author, timestamp, messageId, pictureId, receiptRequested):
|
||||||
# TODO
|
# TODO
|
||||||
if receiptRequested: self.call("notification_ack", (jid, messageId))
|
if receiptRequested: self.call("notification_ack", (jid, messageId))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ def connectionClosed():
|
||||||
db = MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_TABLE)
|
db = MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_TABLE)
|
||||||
io = IOChannel(args.host, args.port, handleTransportData, connectionClosed)
|
io = IOChannel(args.host, args.port, handleTransportData, connectionClosed)
|
||||||
|
|
||||||
plugin = WhatsAppBackend(io, db)
|
plugin = WhatsAppBackend(io, db, args.j)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -30,12 +30,13 @@ from session import Session
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
class WhatsAppBackend(SpectrumBackend):
|
class WhatsAppBackend(SpectrumBackend):
|
||||||
def __init__(self, io, db):
|
def __init__(self, io, db, spectrum_jid):
|
||||||
SpectrumBackend.__init__(self)
|
SpectrumBackend.__init__(self)
|
||||||
self.logger = logging.getLogger(self.__class__.__name__)
|
self.logger = logging.getLogger(self.__class__.__name__)
|
||||||
self.io = io
|
self.io = io
|
||||||
self.db = db
|
self.db = db
|
||||||
self.sessions = { }
|
self.sessions = { }
|
||||||
|
self.spectrum_jid = spectrum_jid
|
||||||
# Used to prevent duplicate messages
|
# Used to prevent duplicate messages
|
||||||
self.lastMessage = {}
|
self.lastMessage = {}
|
||||||
|
|
||||||
|
@ -58,7 +59,7 @@ class WhatsAppBackend(SpectrumBackend):
|
||||||
self.sessions[user].logout()
|
self.sessions[user].logout()
|
||||||
del self.sessions[user]
|
del self.sessions[user]
|
||||||
|
|
||||||
def handleMessageSendRequest(self, user, buddy, message, xhtml = ""):
|
def handleMessageSendRequest(self, user, buddy, message, xhtml = "", ID = 0):
|
||||||
self.logger.debug("handleMessageSendRequest(user=%s, buddy=%s, message=%s, xhtml = %s)", user, buddy, message, xhtml)
|
self.logger.debug("handleMessageSendRequest(user=%s, buddy=%s, message=%s, xhtml = %s)", user, buddy, message, xhtml)
|
||||||
# For some reason spectrum occasionally sends to identical messages to
|
# For some reason spectrum occasionally sends to identical messages to
|
||||||
# a buddy, one to the bare jid and one to /bot. This causes duplicate
|
# a buddy, one to the bare jid and one to /bot. This causes duplicate
|
||||||
|
@ -67,15 +68,20 @@ class WhatsAppBackend(SpectrumBackend):
|
||||||
#
|
#
|
||||||
# TODO Proper fix, this work around drops all duplicate messages even
|
# TODO Proper fix, this work around drops all duplicate messages even
|
||||||
# intentional ones.
|
# intentional ones.
|
||||||
|
# IDEA there is an ID field in ConvMessage. If it is extracted it will work
|
||||||
usersMessage = self.lastMessage[user]
|
usersMessage = self.lastMessage[user]
|
||||||
if buddy not in usersMessage or usersMessage[buddy] != message:
|
if buddy not in usersMessage or usersMessage[buddy] != message:
|
||||||
self.sessions[user].sendMessageToWA(buddy, message)
|
self.sessions[user].sendMessageToWA(buddy, message, ID)
|
||||||
usersMessage[buddy] = message
|
usersMessage[buddy] = message
|
||||||
|
|
||||||
def handleJoinRoomRequest(self, user, room, nickname, pasword):
|
def handleJoinRoomRequest(self, user, room, nickname, pasword):
|
||||||
self.logger.debug("handleJoinRoomRequest(user=%s, room=%s, nickname=%s)", user, room, nickname)
|
self.logger.debug("handleJoinRoomRequest(user=%s, room=%s, nickname=%s)", user, room, nickname)
|
||||||
self.sessions[user].joinRoom(room, nickname)
|
self.sessions[user].joinRoom(room, nickname)
|
||||||
|
|
||||||
|
def handleLeaveRoomRequest(self, user, room):
|
||||||
|
self.logger.debug("handleLeaveRoomRequest(user=%s, room=%s)", user, room)
|
||||||
|
self.sessions[user].leaveRoom(room)
|
||||||
|
|
||||||
def handleStatusChangeRequest(self, user, status, statusMessage):
|
def handleStatusChangeRequest(self, user, status, statusMessage):
|
||||||
self.logger.debug("handleStatusChangeRequest(user=%s, status=%d, statusMessage=%s)", user, status, statusMessage)
|
self.logger.debug("handleStatusChangeRequest(user=%s, status=%d, statusMessage=%s)", user, status, statusMessage)
|
||||||
self.sessions[user].changeStatusMessage(statusMessage)
|
self.sessions[user].changeStatusMessage(statusMessage)
|
||||||
|
@ -105,13 +111,11 @@ class WhatsAppBackend(SpectrumBackend):
|
||||||
self.logger.debug("handleVCardRequest(user=%s, buddy=%s, ID=%s)", user, buddy, ID)
|
self.logger.debug("handleVCardRequest(user=%s, buddy=%s, ID=%s)", user, buddy, ID)
|
||||||
self.sessions[user].requestVCard(buddy, ID)
|
self.sessions[user].requestVCard(buddy, ID)
|
||||||
|
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
def handleBuddyBlockToggled(self, user, buddy, blocked):
|
def handleBuddyBlockToggled(self, user, buddy, blocked):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def handleLeaveRoomRequest(self, user, room):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def handleVCardUpdatedRequest(self, user, photo, nickname):
|
def handleVCardUpdatedRequest(self, user, photo, nickname):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -134,6 +138,9 @@ class WhatsAppBackend(SpectrumBackend):
|
||||||
def handleRawXmlRequest(self, xml):
|
def handleRawXmlRequest(self, xml):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def handleMessageAckRequest(self, user, legacyName, ID = 0):
|
||||||
|
self.logger.info("Meassage ACK request for %s !!",leagcyName)
|
||||||
|
|
||||||
def sendData(self, data):
|
def sendData(self, data):
|
||||||
self.io.sendData(data)
|
self.io.sendData(data)
|
||||||
|
|
||||||
|
|
152
yowsupwrapper.py
152
yowsupwrapper.py
|
@ -1,3 +1,5 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
from yowsup import env
|
from yowsup import env
|
||||||
from yowsup.stacks import YowStack
|
from yowsup.stacks import YowStack
|
||||||
from yowsup.common import YowConstants
|
from yowsup.common import YowConstants
|
||||||
|
@ -30,15 +32,19 @@ from yowsup.layers.protocol_calls import YowCallsProtocolLayer
|
||||||
|
|
||||||
from yowsup.layers.protocol_acks.protocolentities import *
|
from yowsup.layers.protocol_acks.protocolentities import *
|
||||||
from yowsup.layers.protocol_chatstate.protocolentities import *
|
from yowsup.layers.protocol_chatstate.protocolentities import *
|
||||||
|
from yowsup.layers.protocol_contacts.protocolentities import *
|
||||||
from yowsup.layers.protocol_groups.protocolentities import *
|
from yowsup.layers.protocol_groups.protocolentities import *
|
||||||
from yowsup.layers.protocol_media.protocolentities import *
|
from yowsup.layers.protocol_media.protocolentities import *
|
||||||
from yowsup.layers.protocol_messages.protocolentities import *
|
from yowsup.layers.protocol_messages.protocolentities import *
|
||||||
from yowsup.layers.protocol_presence.protocolentities import *
|
from yowsup.layers.protocol_presence.protocolentities import *
|
||||||
from yowsup.layers.protocol_profiles.protocolentities import *
|
from yowsup.layers.protocol_profiles.protocolentities import *
|
||||||
from yowsup.layers.protocol_receipts.protocolentities import *
|
from yowsup.layers.protocol_receipts.protocolentities import *
|
||||||
|
from yowsup.layers.protocol_media.mediauploader import MediaUploader
|
||||||
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
#from session import MsgIDs
|
||||||
|
|
||||||
class YowsupApp(object):
|
class YowsupApp(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
env.CURRENT_ENV = env.S40YowsupEnv()
|
env.CURRENT_ENV = env.S40YowsupEnv()
|
||||||
|
@ -55,7 +61,6 @@ class YowsupApp(object):
|
||||||
YowContactsIqProtocolLayer,
|
YowContactsIqProtocolLayer,
|
||||||
YowChatstateProtocolLayer,
|
YowChatstateProtocolLayer,
|
||||||
YowCallsProtocolLayer,
|
YowCallsProtocolLayer,
|
||||||
YowMediaProtocolLayer,
|
|
||||||
YowPrivacyProtocolLayer,
|
YowPrivacyProtocolLayer,
|
||||||
YowProfilesProtocolLayer,
|
YowProfilesProtocolLayer,
|
||||||
YowGroupsProtocolLayer,
|
YowGroupsProtocolLayer,
|
||||||
|
@ -66,6 +71,7 @@ class YowsupApp(object):
|
||||||
YowStanzaRegulator,
|
YowStanzaRegulator,
|
||||||
YowNetworkLayer
|
YowNetworkLayer
|
||||||
)
|
)
|
||||||
|
self.logger = logging.getLogger(self.__class__.__name__)
|
||||||
self.stack = YowStack(layers)
|
self.stack = YowStack(layers)
|
||||||
self.stack.broadcastEvent(
|
self.stack.broadcastEvent(
|
||||||
YowLayerEvent(YowsupAppLayer.EVENT_START, caller = self)
|
YowLayerEvent(YowsupAppLayer.EVENT_START, caller = self)
|
||||||
|
@ -131,6 +137,64 @@ class YowsupApp(object):
|
||||||
"""
|
"""
|
||||||
messageEntity = TextMessageProtocolEntity(message, to = to)
|
messageEntity = TextMessageProtocolEntity(message, to = to)
|
||||||
self.sendEntity(messageEntity)
|
self.sendEntity(messageEntity)
|
||||||
|
return messageEntity.getId()
|
||||||
|
|
||||||
|
def sendLocation(self, to, latitude, longitude):
|
||||||
|
messageEntity = LocationMediaMessageProtocolEntity(latitude,longitude, None, None, "raw", to = to)
|
||||||
|
self.sendEntity(messageEntity)
|
||||||
|
return messageEntity.getId()
|
||||||
|
|
||||||
|
def image_send(self, jid, path, caption = None):
|
||||||
|
entity = RequestUploadIqProtocolEntity(RequestUploadIqProtocolEntity.MEDIA_TYPE_IMAGE, filePath=path)
|
||||||
|
successFn = lambda successEntity, originalEntity: self.onRequestUploadResult(jid, path, successEntity, originalEntity, caption)
|
||||||
|
errorFn = lambda errorEntity, originalEntity: self.onRequestUploadError(jid, path, errorEntity, originalEntity)
|
||||||
|
|
||||||
|
self.sendIq(entity, successFn, errorFn)
|
||||||
|
|
||||||
|
def onRequestUploadResult(self, jid, filePath, resultRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity, caption = None):
|
||||||
|
|
||||||
|
if requestUploadIqProtocolEntity.mediaType == RequestUploadIqProtocolEntity.MEDIA_TYPE_AUDIO:
|
||||||
|
doSendFn = self._doSendAudio
|
||||||
|
else:
|
||||||
|
doSendFn = self._doSendImage
|
||||||
|
|
||||||
|
if resultRequestUploadIqProtocolEntity.isDuplicate():
|
||||||
|
doSendFn(filePath, resultRequestUploadIqProtocolEntity.getUrl(), jid,
|
||||||
|
resultRequestUploadIqProtocolEntity.getIp(), caption)
|
||||||
|
else:
|
||||||
|
successFn = lambda filePath, jid, url: doSendFn(filePath, url, jid, resultRequestUploadIqProtocolEntity.getIp(), caption)
|
||||||
|
mediaUploader = MediaUploader(jid, self.legacyName, filePath,
|
||||||
|
resultRequestUploadIqProtocolEntity.getUrl(),
|
||||||
|
resultRequestUploadIqProtocolEntity.getResumeOffset(),
|
||||||
|
successFn, self.onUploadError, self.onUploadProgress, async=False)
|
||||||
|
mediaUploader.start()
|
||||||
|
|
||||||
|
def onRequestUploadError(self, jid, path, errorRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):
|
||||||
|
self.logger.error("Request upload for file %s for %s failed" % (path, jid))
|
||||||
|
|
||||||
|
def onUploadError(self, filePath, jid, url):
|
||||||
|
#logger.error("Upload file %s to %s for %s failed!" % (filePath, url, jid))
|
||||||
|
self.logger.error("Upload Error!")
|
||||||
|
|
||||||
|
def onUploadProgress(self, filePath, jid, url, progress):
|
||||||
|
#sys.stdout.write("%s => %s, %d%% \r" % (os.path.basename(filePath), jid, progress))
|
||||||
|
#sys.stdout.flush()
|
||||||
|
pass
|
||||||
|
|
||||||
|
def doSendImage(self, filePath, url, to, ip = None, caption = None):
|
||||||
|
entity = ImageDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, ip, to, caption = caption)
|
||||||
|
self.sendEntity(entity)
|
||||||
|
#self.msgIDs[entity.getId()] = MsgIDs(self.imgMsgId, entity.getId())
|
||||||
|
return entity.getId()
|
||||||
|
|
||||||
|
|
||||||
|
def doSendAudio(self, filePath, url, to, ip = None, caption = None):
|
||||||
|
entity = AudioDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, ip, to)
|
||||||
|
self.sendEntity(entity)
|
||||||
|
#self.msgIDs[entity.getId()] = MsgIDs(self.imgMsgId, entity.getId())
|
||||||
|
return entity.getId()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def sendPresence(self, available):
|
def sendPresence(self, available):
|
||||||
"""
|
"""
|
||||||
|
@ -168,6 +232,16 @@ class YowsupApp(object):
|
||||||
entity = UnsubscribePresenceProtocolEntity(jid)
|
entity = UnsubscribePresenceProtocolEntity(jid)
|
||||||
self.sendEntity(entity)
|
self.sendEntity(entity)
|
||||||
|
|
||||||
|
def leaveGroup(self, group):
|
||||||
|
"""
|
||||||
|
Permanently leave a WhatsApp group
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- group: (str) the group id (e.g. 27831788123-144024456)
|
||||||
|
"""
|
||||||
|
entity = LeaveGroupsIqProtocolEntity([group + '@g.us'])
|
||||||
|
self.sendEntity(entity)
|
||||||
|
|
||||||
def setStatus(self, statusText):
|
def setStatus(self, statusText):
|
||||||
"""
|
"""
|
||||||
Send status to whatsapp
|
Send status to whatsapp
|
||||||
|
@ -197,6 +271,26 @@ class YowsupApp(object):
|
||||||
)
|
)
|
||||||
self.sendEntity(state)
|
self.sendEntity(state)
|
||||||
|
|
||||||
|
def sendSync(self, contacts, delta = False, interactive = True):
|
||||||
|
"""
|
||||||
|
You need to sync new contacts before you interact with
|
||||||
|
them, failure to do so could result in a temporary ban.
|
||||||
|
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
# TODO: Implement callbacks
|
||||||
|
mode = GetSyncIqProtocolEntity.MODE_DELTA if delta else GetSyncIqProtocolEntity.MODE_FULL
|
||||||
|
context = GetSyncIqProtocolEntity.CONTEXT_INTERACTIVE if interactive else GetSyncIqProtocolEntity.CONTEXT_REGISTRATION
|
||||||
|
iq = GetSyncIqProtocolEntity(contacts, mode, context)
|
||||||
|
self.sendIq(iq)
|
||||||
|
|
||||||
def requestLastSeen(self, phoneNumber, success = None, failure = None):
|
def requestLastSeen(self, phoneNumber, success = None, failure = None):
|
||||||
"""
|
"""
|
||||||
Requests when user was last seen.
|
Requests when user was last seen.
|
||||||
|
@ -219,8 +313,8 @@ class YowsupApp(object):
|
||||||
Requests profile picture of whatsapp user
|
Requests profile picture of whatsapp user
|
||||||
Args:
|
Args:
|
||||||
- phoneNumber: (str) the phone number of the user
|
- phoneNumber: (str) the phone number of the user
|
||||||
- success: (func) called when request is successfully processed.
|
- onSuccess: (func) called when request is successfully processed.
|
||||||
- failure: (func) called when request has failed
|
- onFailure: (func) called when request has failed
|
||||||
"""
|
"""
|
||||||
iq = GetPictureIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
|
iq = GetPictureIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
|
||||||
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
||||||
|
@ -229,6 +323,18 @@ class YowsupApp(object):
|
||||||
iq = ListGroupsIqProtocolEntity()
|
iq = ListGroupsIqProtocolEntity()
|
||||||
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
||||||
|
|
||||||
|
def requestGroupInfo(self, group, onSuccess = None, onFailure = None):
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
iq = InfoGroupsIqProtocolEntity(group + '@g.us')
|
||||||
|
self.sendIq(iq, onSuccess = onSuccess, onError = onFailure)
|
||||||
|
|
||||||
def onAuthSuccess(self, status, kind, creation, expiration, props, nonce, t):
|
def onAuthSuccess(self, status, kind, creation, expiration, props, nonce, t):
|
||||||
"""
|
"""
|
||||||
Called when login is successful.
|
Called when login is successful.
|
||||||
|
@ -362,6 +468,15 @@ class YowsupApp(object):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def onLocation(self, entity):
|
||||||
|
"""
|
||||||
|
Called when location message is received
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- entity: LocationMediaMessageProtocolEntity
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
def onVCard(self, _id, _from, name, card_data, to, notify, timestamp, participant):
|
||||||
"""
|
"""
|
||||||
Called when VCard message is received
|
Called when VCard message is received
|
||||||
|
@ -378,6 +493,23 @@ class YowsupApp(object):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def onAddedToGroup(self, entity):
|
||||||
|
"""Called when the user has been added to a new group"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def onParticipantsAddedToGroup(self, entity):
|
||||||
|
"""Called when participants have been added to a group"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def onParticipantsRemovedFromGroup(self, group, participants):
|
||||||
|
"""Called when participants have been removed from a group
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- group: (str) id of the group (e.g. 27831788123-144024456)
|
||||||
|
- participants: (list) jids of participants that are removed
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
def sendEntity(self, entity):
|
def sendEntity(self, entity):
|
||||||
"""Sends an entity down the stack (as if YowsupAppLayer called toLower)"""
|
"""Sends an entity down the stack (as if YowsupAppLayer called toLower)"""
|
||||||
self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT,
|
self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT,
|
||||||
|
@ -408,6 +540,7 @@ class YowsupAppLayer(YowInterfaceLayer):
|
||||||
# return True otherwise
|
# return True otherwise
|
||||||
if layerEvent.getName() == YowsupAppLayer.EVENT_START:
|
if layerEvent.getName() == YowsupAppLayer.EVENT_START:
|
||||||
self.caller = layerEvent.getArg('caller')
|
self.caller = layerEvent.getArg('caller')
|
||||||
|
self.logger = logging.getLogger(self.__class__.__name__)
|
||||||
return True
|
return True
|
||||||
elif layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
|
elif layerEvent.getName() == YowNetworkLayer.EVENT_STATE_DISCONNECTED:
|
||||||
self.caller.onDisconnect()
|
self.caller.onDisconnect()
|
||||||
|
@ -472,10 +605,21 @@ class YowsupAppLayer(YowInterfaceLayer):
|
||||||
"""
|
"""
|
||||||
Sends ack automatically
|
Sends ack automatically
|
||||||
"""
|
"""
|
||||||
|
self.logger.debug("Received notification (%s): %s", type(entity), entity)
|
||||||
self.toLower(entity.ack())
|
self.toLower(entity.ack())
|
||||||
|
if isinstance(entity, CreateGroupsNotificationProtocolEntity):
|
||||||
|
self.caller.onAddedToGroup(entity)
|
||||||
|
elif isinstance(entity, AddGroupsNotificationProtocolEntity):
|
||||||
|
self.caller.onParticipantsAddedToGroup(entity)
|
||||||
|
elif isinstance(entity, RemoveGroupsNotificationProtocolEntity):
|
||||||
|
self.caller.onParticipantsRemovedFromGroup(
|
||||||
|
entity.getGroupId().split('@')[0],
|
||||||
|
entity.getParticipants().keys()
|
||||||
|
)
|
||||||
|
|
||||||
@ProtocolEntityCallback('message')
|
@ProtocolEntityCallback('message')
|
||||||
def onMessageReceived(self, entity):
|
def onMessageReceived(self, entity):
|
||||||
|
self.logger.debug("Received Message: %s", entity)
|
||||||
if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT:
|
if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT:
|
||||||
self.caller.onTextMessage(
|
self.caller.onTextMessage(
|
||||||
entity._id,
|
entity._id,
|
||||||
|
@ -508,6 +652,8 @@ class YowsupAppLayer(YowInterfaceLayer):
|
||||||
entity.timestamp,
|
entity.timestamp,
|
||||||
entity.participant
|
entity.participant
|
||||||
)
|
)
|
||||||
|
elif isinstance(entity, LocationMediaMessageProtocolEntity):
|
||||||
|
self.caller.onLocation(entity)
|
||||||
|
|
||||||
@ProtocolEntityCallback('presence')
|
@ProtocolEntityCallback('presence')
|
||||||
def onPresenceReceived(self, presence):
|
def onPresenceReceived(self, presence):
|
||||||
|
|
Loading…
Reference in a new issue