diff --git a/transWhat/buddy.py b/transWhat/buddy.py index 1aa95dd..de012d1 100644 --- a/transWhat/buddy.py +++ b/transWhat/buddy.py @@ -1,15 +1,10 @@ -from Spectrum2 import protocol_pb2 - import logging import time import base64 import hashlib +import Spectrum2 -import deferred -from deferred import call - -def sha1hash(data): - hashlib.sha1(data).hexdigest() +from . import deferred class Buddy(): def __init__(self, owner, number, nick, statusMsg, groups, image_hash): @@ -120,11 +115,11 @@ class BuddyList(dict): def updateSpectrum(self, buddy): if buddy.presence == 0: - status = protocol_pb2.STATUS_NONE + status = Spectrum2.protocol_pb2.STATUS_NONE elif buddy.presence == 'unavailable': - status = protocol_pb2.STATUS_AWAY + status = Spectrum2.protocol_pb2.STATUS_AWAY else: - status = protocol_pb2.STATUS_ONLINE + status = Spectrum2.protocol_pb2.STATUS_ONLINE statusmsg = buddy.statusMsg if buddy.lastseen != 0: @@ -144,7 +139,7 @@ class BuddyList(dict): buddy = self[number] del self[number] self.backend.handleBuddyChanged(self.user, number, "", [], - protocol_pb2.STATUS_NONE) + Spectrum2.protocol_pb2.STATUS_NONE) self.backend.handleBuddyRemoved(self.user, number) self.session.unsubscribePresence(number) # TODO Sync remove @@ -182,9 +177,9 @@ class BuddyList(dict): pictureData = response.pictureData() # Send VCard if ID != None: - call(self.logger.debug, 'Sending VCard (%s) with image id %s: %s' % + deferred.call(self.logger.debug, 'Sending VCard (%s) with image id %s: %s' % (ID, response.pictureId(), pictureData.then(base64.b64encode))) - call(self.backend.handleVCard, self.user, ID, buddy, "", "", + deferred.call(self.backend.handleVCard, self.user, ID, buddy, "", "", pictureData) # If error error.when(self.logger.debug, 'Sending VCard (%s) without image' % ID) @@ -199,9 +194,14 @@ class BuddyList(dict): except KeyError: nick = "" groups = [] + + def sha1hash(data): + hashlib.sha1(data).hexdigest() + image_hash = pictureData.then(sha1hash) - call(self.logger.debug, 'Image hash is %s' % image_hash) - call(self.update, buddynr, nick, groups, image_hash) + + deferred.call(self.logger.debug, 'Image hash is %s' % image_hash) + deferred.call(self.update, buddynr, nick, groups, image_hash) # No image error.when(self.logger.debug, 'No image') error.when(self.update, buddynr, nick, groups, '') diff --git a/transWhat/group.py b/transWhat/group.py index f2964f5..271d050 100644 --- a/transWhat/group.py +++ b/transWhat/group.py @@ -1,4 +1,4 @@ -from Spectrum2 import protocol_pb2 +import Spectrum2 class Group(): @@ -39,40 +39,40 @@ class Group(): def sendParticipantsToSpectrum(self, yourNumber): for number, nick in self.participants.iteritems(): if number == self.owner: - flags = protocol_pb2.PARTICIPANT_FLAG_MODERATOR + flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_MODERATOR else: - flags = protocol_pb2.PARTICIPANT_FLAG_NONE + flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE if number == yourNumber: - flags = flags | protocol_pb2.PARTICIPANT_FLAG_ME + flags = flags | Spectrum2.protocol_pb2.PARTICIPANT_FLAG_ME try: - self._updateParticipant(number, flags, protocol_pb2.STATUS_ONLINE, + self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_ONLINE, self.backend.sessions[self.user].buddies[number].image_hash) except KeyError: - self._updateParticipant(number, flags, protocol_pb2.STATUS_ONLINE) + self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_ONLINE) def removeParticipants(self, participants): for jid in participants: number = jid.split('@')[0] nick = self.participants[number] - flags = protocol_pb2.PARTICIPANT_FLAG_NONE - self._updateParticipant(number, flags, protocol_pb2.STATUS_NONE) + flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE + self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_NONE) del self.participants[number] def leaveRoom(self): for number in self.participants: nick = self.participants[number] - flags = protocol_pb2.PARTICIPANT_FLAG_ROOM_NOT_FOUND - self._updateParticipant(number, flags, protocol_pb2.STATUS_NONE) + flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_ROOM_NOT_FOUND + self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_NONE) def changeNick(self, number, new_nick): if self.participants[number] == new_nick: return if number == self.owner: - flags = protocol_pb2.PARTICIPANT_FLAG_MODERATOR + flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_MODERATOR else: - flags = protocol_pb2.PARTICIPANT_FLAG_NONE - self._updateParticipant(number, flags, protocol_pb2.STATUS_ONLINE, new_nick) + flags = Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE + self._updateParticipant(number, flags, Spectrum2.protocol_pb2.STATUS_ONLINE, new_nick) self.participants[number] = new_nick def _updateParticipant(self, number, flags, status, imageHash = "", newNick = ""): diff --git a/transWhat/registersession.py b/transWhat/registersession.py index dfadec1..74bfd8f 100644 --- a/transWhat/registersession.py +++ b/transWhat/registersession.py @@ -1,9 +1,11 @@ -from Spectrum2 import protocol_pb2 - -from yowsupwrapper import YowsupApp -import logging -import threadutils import sys +import logging +import Spectrum2 + +from .yowsupwrapper import YowsupApp + +from . import threadutils + class RegisterSession(YowsupApp): """ @@ -22,7 +24,7 @@ class RegisterSession(YowsupApp): def login(self, password=""): self.backend.handleConnected(self.user) self.backend.handleBuddyChanged(self.user, 'bot', 'bot', - ['Admin'], protocol_pb2.STATUS_ONLINE) + ['Admin'], Spectrum2.protocol_pb2.STATUS_ONLINE) self.backend.handleMessage(self.user, 'bot', 'Please enter your country code') diff --git a/transWhat/session.py b/transWhat/session.py index c5507ee..d8f32d4 100644 --- a/transWhat/session.py +++ b/transWhat/session.py @@ -1,24 +1,18 @@ import logging import urllib import time -import sys import os -reload(sys) -sys.setdefaultencoding("utf-8") - from yowsup.layers.protocol_media.mediauploader import MediaUploader from yowsup.layers.protocol_media.mediadownloader import MediaDownloader -from Spectrum2 import protocol_pb2 +import Spectrum2 -from buddy import BuddyList -from threading import Timer -from group import Group -from bot import Bot -import deferred -from deferred import call -from yowsupwrapper import YowsupApp +from . import deferred +from .buddy import BuddyList +from .group import Group +from .bot import Bot +from .yowsupwrapper import YowsupApp def ago(secs): periods = ["second", "minute", "hour", "day", "week", "month", "year", "decade"] @@ -56,7 +50,7 @@ class Session(YowsupApp): self.user = user self.legacyName = legacyName - self.status = protocol_pb2.STATUS_NONE + self.status = Spectrum2.protocol_pb2.STATUS_NONE self.statusMessage = '' self.groups = {} @@ -210,7 +204,7 @@ class Session(YowsupApp): self.backend.handleConnected(self.user) self.backend.handleBuddyChanged(self.user, "bot", self.bot.name, - ["Admin"], protocol_pb2.STATUS_ONLINE) + ["Admin"], Spectrum2.protocol_pb2.STATUS_ONLINE) # Initialisation? self.requestPrivacyList() self.requestClientConfig() @@ -418,7 +412,7 @@ class Session(YowsupApp): self.logger.info("Paused typing: %s" % buddy) if buddy != 'bot': self.backend.handleBuddyTyped(self.user, buddy) - self.timer = Timer(3, self.backend.handleBuddyStoppedTyping, + self.timer = threading.Timer(3, self.backend.handleBuddyStoppedTyping, (self.user, buddy)).start() # Called by superclass @@ -744,8 +738,8 @@ class Session(YowsupApp): self.logger.info("Status changed: %s" % status) self.status = status - if status == protocol_pb2.STATUS_ONLINE \ - or status == protocol_pb2.STATUS_FFC: + if status == Spectrum2.protocol_pb2.STATUS_ONLINE \ + or status == Spectrum2.protocol_pb2.STATUS_FFC: self.sendPresence(True) else: self.sendPresence(False) @@ -829,7 +823,7 @@ class Session(YowsupApp): self.logger.info("Removed %s from room %s" % (buddy, room)) - self.backend.handleParticipantChanged(self.user, buddy, room, protocol_pb2.PARTICIPANT_FLAG_NONE, protocol_pb2.STATUS_NONE) # TODO + self.backend.handleParticipantChanged(self.user, buddy, room, Spectrum2.protocol_pb2.PARTICIPANT_FLAG_NONE, Spectrum2.protocol_pb2.STATUS_NONE) # TODO if receiptRequested: self.call("notification_ack", (gjid, messageId)) diff --git a/transWhat/threadutils.py b/transWhat/threadutils.py index 59e7d51..4053d05 100644 --- a/transWhat/threadutils.py +++ b/transWhat/threadutils.py @@ -1,8 +1,8 @@ -import Queue +import queue import threading # This queue is for other threads that want to execute code in the main thread -eventQueue = Queue.Queue() +eventQueue = queue.Queue() def runInThread(threadFunc, callback): """ @@ -15,5 +15,6 @@ def runInThread(threadFunc, callback): result = threadFunc() # Queue callback to be call in main thread eventQueue.put(lambda: callback(result)) + thread = threading.Thread(target=helper) thread.start() diff --git a/transWhat/transwhat.py b/transWhat/transwhat.py index dc4dcd4..3741216 100755 --- a/transWhat/transwhat.py +++ b/transWhat/transwhat.py @@ -4,18 +4,16 @@ import argparse import traceback import logging import asyncore -import sys, os -import Queue -import transWhat.threadutils - -sys.path.insert(0, os.getcwd()) +import sys from Spectrum2.iochannel import IOChannel from Spectrum2.config import SpectrumConfig -from transWhat.whatsappbackend import WhatsAppBackend from yowsup.common import YowConstants from yowsup.stacks import YowStack +from .whatsappbackend import WhatsAppBackend +from . import threadutils + # Arguments parser = argparse.ArgumentParser() parser.add_argument('--debug', action='store_true') diff --git a/transWhat/whatsappbackend.py b/transWhat/whatsappbackend.py index 7e1b613..00be791 100644 --- a/transWhat/whatsappbackend.py +++ b/transWhat/whatsappbackend.py @@ -1,11 +1,8 @@ -from Spectrum2.backend import SpectrumBackend -from Spectrum2 import protocol_pb2 - -from session import Session -from registersession import RegisterSession - import logging +from Spectrum2.backend import SpectrumBackend +from .session import Session +from .registersession import RegisterSession class WhatsAppBackend(SpectrumBackend): def __init__(self, io, spectrum_jid, specConf): diff --git a/transWhat/yowsupwrapper.py b/transWhat/yowsupwrapper.py index 17129cf..a96aa27 100644 --- a/transWhat/yowsupwrapper.py +++ b/transWhat/yowsupwrapper.py @@ -178,7 +178,7 @@ class YowsupApp(object): mediaUploader = MediaUploader(jid, ownNumber, filePath, resultRequestUploadIqProtocolEntity.getUrl(), resultRequestUploadIqProtocolEntity.getResumeOffset(), - successFn, self.onUploadError, self.onUploadProgress, async=False) + successFn, self.onUploadError, self.onUploadProgress, asynchronous=False) mediaUploader.start() def onRequestUploadError(self, jid, path, errorRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):