Remove MySQL from Buddy.py
This commit is contained in:
parent
06388685d4
commit
da8a415d32
136
buddy.py
136
buddy.py
|
@ -25,34 +25,10 @@ __status__ = "Prototype"
|
||||||
from Spectrum2 import protocol_pb2
|
from Spectrum2 import protocol_pb2
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import threading
|
|
||||||
|
|
||||||
|
|
||||||
class Number():
|
|
||||||
|
|
||||||
def __init__(self, number, state, db):
|
|
||||||
self.number = number
|
|
||||||
self.db = db
|
|
||||||
self.state = state
|
|
||||||
|
|
||||||
cur = self.db.cursor()
|
|
||||||
cur.execute("SELECT id FROM numbers WHERE number = %s AND state = %s", (self.number, self.state))
|
|
||||||
if (cur.rowcount):
|
|
||||||
self.id = cur.fetchone()[0]
|
|
||||||
else:
|
|
||||||
cur.execute("REPLACE numbers (number, state) VALUES (%s, %s)", (self.number, self.state))
|
|
||||||
self.db.commit()
|
|
||||||
self.id = cur.lastrowid
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return "%s (id=%s)" % (self.number, self.id)
|
|
||||||
|
|
||||||
|
|
||||||
class Buddy():
|
class Buddy():
|
||||||
def __init__(self, owner, number, nick, groups, image_hash, id, db):
|
def __init__(self, owner, number, nick, statusMsg, groups, image_hash):
|
||||||
self.id = id
|
|
||||||
self.db = db
|
|
||||||
|
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.number = number
|
self.number = number
|
||||||
|
@ -69,121 +45,37 @@ class Buddy():
|
||||||
if image_hash is not None:
|
if image_hash is not None:
|
||||||
self.image_hash = image_hash
|
self.image_hash = image_hash
|
||||||
|
|
||||||
groups = u",".join(groups).encode("latin-1")
|
|
||||||
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, self.image_hash, self.owner.id, self.number.id))
|
|
||||||
self.db.commit()
|
|
||||||
|
|
||||||
def delete(self):
|
|
||||||
cur = self.db.cursor()
|
|
||||||
cur.execute("DELETE FROM buddies WHERE owner_id = %s AND buddy_id = %s", (self.owner.id, self.number.id))
|
|
||||||
self.db.commit()
|
|
||||||
self.id = None
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def create(owner, number, nick, groups, image_hash, db):
|
|
||||||
groups = u",".join(groups).encode("latin-1")
|
|
||||||
cur = db.cursor()
|
|
||||||
cur.execute("REPLACE buddies (owner_id, buddy_id, nick, groups, image_hash) VALUES (%s, %s, %s, %s, %s)", (owner.id, number.id, nick, groups, image_hash))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
return Buddy(owner, number, nick, groups, image_hash, cur.lastrowid, db)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s (nick=%s, id=%s)" % (self.number, self.nick, self.id)
|
return "%s (nick=%s)" % (self.number, self.nick)
|
||||||
|
|
||||||
class BuddyList(dict):
|
class BuddyList(dict):
|
||||||
|
|
||||||
def __init__(self, owner, db):
|
def __init__(self, owner):
|
||||||
self.db = db
|
self.owner = owner
|
||||||
self.owner = Number(owner, 1, db)
|
|
||||||
self.lock = threading.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def load(self):
|
|
||||||
self.clear()
|
|
||||||
self.lock.acquire()
|
|
||||||
|
|
||||||
cur = self.db.cursor()
|
|
||||||
cur.execute("""SELECT
|
|
||||||
b.id AS id,
|
|
||||||
n.number AS number,
|
|
||||||
b.nick AS nick,
|
|
||||||
b.groups AS groups,
|
|
||||||
n.state AS state,
|
|
||||||
b.image_hash AS image_hash
|
|
||||||
FROM buddies AS b
|
|
||||||
LEFT JOIN numbers AS n
|
|
||||||
ON b.buddy_id = n.id
|
|
||||||
WHERE
|
|
||||||
b.owner_id IN (%s, 0)
|
|
||||||
AND n.state >= 1
|
|
||||||
ORDER BY b.owner_id DESC""", self.owner.id)
|
|
||||||
|
|
||||||
for i in range(cur.rowcount):
|
|
||||||
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.lock.release()
|
|
||||||
|
|
||||||
|
def load(self, buddies):
|
||||||
|
for buddy in buddies:
|
||||||
|
number = buddy.buddyName
|
||||||
|
nick = buddy.alias
|
||||||
|
statusMsg = buddy.statusMessage
|
||||||
|
groups = [g for g in buddy.group]
|
||||||
|
image_hash = buddy.iconHash
|
||||||
|
self[number] = Buddy(self.owner, number, nick, statusMsg,
|
||||||
|
groups, image_hash)
|
||||||
|
|
||||||
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 = Buddy(self.owner, number, nick, "", groups, image_hash)
|
||||||
self.lock.release()
|
|
||||||
|
|
||||||
return buddy
|
return buddy
|
||||||
|
|
||||||
def add(self, number, nick, groups = [], state = 0, image_hash = ""):
|
|
||||||
return Buddy.create(self.owner, Number(number, state, self.db), nick, groups, image_hash, self.db)
|
|
||||||
|
|
||||||
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):
|
|
||||||
self.lock.acquire()
|
|
||||||
|
|
||||||
cur = self.db.cursor()
|
|
||||||
cur.execute("DELETE FROM buddies WHERE owner_id = %s", self.owner.id)
|
|
||||||
self.db.commit()
|
|
||||||
self.lock.release()
|
|
||||||
|
|
||||||
|
|
||||||
def sync(self, user, password):
|
|
||||||
self.lock.acquire()
|
|
||||||
cur = self.db.cursor()
|
|
||||||
cur.execute("""SELECT
|
|
||||||
n.number AS number,
|
|
||||||
n.state AS state
|
|
||||||
FROM buddies AS r
|
|
||||||
LEFT JOIN numbers AS n
|
|
||||||
ON r.buddy_id = n.id
|
|
||||||
WHERE
|
|
||||||
r.owner_id = %s""", self.owner.id)
|
|
||||||
|
|
||||||
# prefix every number with leading 0 to force internation format
|
|
||||||
numbers = dict([("+" + number, state) for number, state in cur.fetchall()])
|
|
||||||
|
|
||||||
if len(numbers) == 0:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
result = WAContactsSyncRequest(user, password, numbers.keys()).send()
|
|
||||||
|
|
||||||
using = 0
|
|
||||||
for number in result['c']:
|
|
||||||
cur = self.db.cursor()
|
|
||||||
cur.execute("UPDATE numbers SET state = %s WHERE number = %s", (number['w'], number['n']))
|
|
||||||
self.db.commit()
|
|
||||||
using += number['w']
|
|
||||||
self.lock.release()
|
|
||||||
return using
|
|
||||||
|
|
16
session.py
16
session.py
|
@ -87,7 +87,7 @@ class Session(YowsupApp):
|
||||||
self.lastMsgId = None
|
self.lastMsgId = None
|
||||||
self.synced = False
|
self.synced = False
|
||||||
|
|
||||||
self.buddies = BuddyList(self.legacyName, self.db)
|
self.buddies = BuddyList(self.legacyName)
|
||||||
self.bot = Bot(self)
|
self.bot = Bot(self)
|
||||||
|
|
||||||
self.imgMsgId = None
|
self.imgMsgId = None
|
||||||
|
@ -523,8 +523,8 @@ class Session(YowsupApp):
|
||||||
|
|
||||||
if (lastseen == str(buddy.lastseen)) and (_type == buddy.presence):
|
if (lastseen == str(buddy.lastseen)) and (_type == buddy.presence):
|
||||||
return
|
return
|
||||||
|
|
||||||
if ((lastseen != "deny") and (lastseen != None) and (lastseen != "none")):
|
if ((lastseen != "deny") and (lastseen != None) and (lastseen != "none")):
|
||||||
buddy.lastseen = int(lastseen)
|
buddy.lastseen = int(lastseen)
|
||||||
if (_type == None):
|
if (_type == None):
|
||||||
buddy.lastseen = time.time()
|
buddy.lastseen = time.time()
|
||||||
|
@ -533,15 +533,15 @@ class Session(YowsupApp):
|
||||||
|
|
||||||
timestamp = time.localtime(buddy.lastseen)
|
timestamp = time.localtime(buddy.lastseen)
|
||||||
statusmsg = buddy.statusMsg + time.strftime("\n Last seen: %a, %d %b %Y %H:%M:%S", timestamp)
|
statusmsg = buddy.statusMsg + time.strftime("\n Last seen: %a, %d %b %Y %H:%M:%S", timestamp)
|
||||||
|
|
||||||
if _type == "unavailable":
|
if _type == "unavailable":
|
||||||
self.onPresenceUnavailable(buddy, statusmsg)
|
self.onPresenceUnavailable(buddy, statusmsg)
|
||||||
else:
|
else:
|
||||||
self.onPresenceAvailable(buddy, statusmsg)
|
self.onPresenceAvailable(buddy, statusmsg)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def onPresenceAvailable(self, buddy, statusmsg):
|
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,
|
self.backend.handleBuddyChanged(self.user, buddy.number.number,
|
||||||
buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE, statusmsg, buddy.image_hash)
|
buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE, statusmsg, buddy.image_hash)
|
||||||
|
@ -789,6 +789,10 @@ class Session(YowsupApp):
|
||||||
msg = self.offlineQueue.pop(0)
|
msg = self.offlineQueue.pop(0)
|
||||||
self.backend.handleMessage(self.user, msg[0], msg[1], "", "", msg[2])
|
self.backend.handleMessage(self.user, msg[0], msg[1], "", "", msg[2])
|
||||||
|
|
||||||
|
# Called when user logs in to initialize the roster
|
||||||
|
def loadBuddies(self, buddy):
|
||||||
|
self.buddies.load(buddy)
|
||||||
|
|
||||||
# also for adding a new buddy
|
# also for adding a new buddy
|
||||||
def updateBuddy(self, buddy, nick, groups, image_hash = None):
|
def updateBuddy(self, buddy, nick, groups, image_hash = None):
|
||||||
if buddy != "bot":
|
if buddy != "bot":
|
||||||
|
|
|
@ -87,6 +87,12 @@ class WhatsAppBackend(SpectrumBackend):
|
||||||
self.sessions[user].changeStatusMessage(statusMessage)
|
self.sessions[user].changeStatusMessage(statusMessage)
|
||||||
self.sessions[user].changeStatus(status)
|
self.sessions[user].changeStatus(status)
|
||||||
|
|
||||||
|
def handleBuddies(self, buddies):
|
||||||
|
"""Called when user logs in. Used to initialize roster."""
|
||||||
|
self.logger.debug("handleBuddies(buddies=%s)", buddies)
|
||||||
|
user = buddies[0].userName
|
||||||
|
self.sessions[user].loadBuddies(buddies)
|
||||||
|
|
||||||
def handleBuddyUpdatedRequest(self, user, buddy, nick, groups):
|
def handleBuddyUpdatedRequest(self, user, buddy, nick, groups):
|
||||||
self.logger.debug("handleBuddyUpdatedRequest(user=%s, buddy=%s, nick=%s, groups=%s)", user, buddy, nick, str(groups))
|
self.logger.debug("handleBuddyUpdatedRequest(user=%s, buddy=%s, nick=%s, groups=%s)", user, buddy, nick, str(groups))
|
||||||
self.sessions[user].updateBuddy(buddy, nick, groups)
|
self.sessions[user].updateBuddy(buddy, nick, groups)
|
||||||
|
@ -139,7 +145,7 @@ class WhatsAppBackend(SpectrumBackend):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def handleMessageAckRequest(self, user, legacyName, ID = 0):
|
def handleMessageAckRequest(self, user, legacyName, ID = 0):
|
||||||
self.logger.info("Meassage ACK request for %s !!",leagcyName)
|
self.logger.info("Meassage ACK request for %s !!",legacyName)
|
||||||
|
|
||||||
def sendData(self, data):
|
def sendData(self, data):
|
||||||
self.io.sendData(data)
|
self.io.sendData(data)
|
||||||
|
|
Loading…
Reference in a new issue