Crude implementation of groupchat

This commit is contained in:
moyamo 2015-09-06 14:06:16 +02:00
parent 4fd313e516
commit 646f45434b
2 changed files with 77 additions and 55 deletions

View file

@ -86,7 +86,7 @@ class Session(YowsupApp):
rooms = [] rooms = []
for room, group in self.groups.iteritems(): for room, group in self.groups.iteritems():
rooms.append([room, group.subject]) rooms.append([room, group.subject])
self.logger.debug("Got rooms: %s", rooms)
self.backend.handleRoomList(rooms) self.backend.handleRoomList(rooms)
def updateRoster(self): def updateRoster(self):
@ -111,7 +111,44 @@ class Session(YowsupApp):
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):
self.logger.debug('Received groups list %s', response)
# This XMPP client is not receiving this for some reason.
groups = response.getGroups()
for group in groups:
room = group.getId()
owner = group.getOwner()
subjectOwner = group.getSubjectOwner()
subject = group.getSubject()
if room in self.groups:
oroom = self.groups[room]
oroom.owner = owner
oroom.subjectOwner = subjectOwner
oroom.subject = subject
else:
self.groups[room] = Group(room, owner, subject, subjectOwner)
# A crude implemtation of groups that act like buddies
self.backend.handleBuddyChanged(self.user, room, subject, [], protocol_pb2.STATUS_NONE)
# This XMPP client is not receiving this for some reason.
# self.updateRoomList()
# for group in groups:
# room = group.getId()
# subjectOwner = group.getSubjectOwner()
# subject = group.getSubject()
# self.backend.handleSubject(self.user, room, subject, subjectOwner)
# for participant in group.getParticipants():
# buddy = participant.split('@')[0]
# self.logger.debug("Added %s to room %s", buddy, room)
# self.backend.handleParticipantChanged(self.user, buddy, room,
# protocol_pb2.PARTICIPANT_FLAG_NONE, protocol_pb2.STATUS_ONLINE)
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)))
@ -173,7 +210,12 @@ 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)
self.sendMessageToXMPP(buddy, messageContent, timestamp) if participant is not None:
partname = participant.split('@')[0]
message = partname + ': ' + messageContent
self.sendMessageToXMPP(buddy, message, timestamp)
else:
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
# message. # message.
#if messageEntity.isBroadcast(): #if messageEntity.isBroadcast():
@ -291,28 +333,27 @@ class Session(YowsupApp):
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")
# 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:
# room, buddy = sender.split("/") room, buddy = sender.split("/")
# self.sendTextMessage(buddy + '@s.whatsapp.net', message) self.sendTextMessage(buddy + '@s.whatsapp.net', message)
# else: else:
# room = sender room = sender
# group = self.groups[room] # group = self.groups[room]
#
# self.backend.handleMessage(self.user, room, message, group.nick) # self.backend.handleMessage(self.user, room, message, group.nick)
# self.sendTextMessage(room + '@g.us', message) self.sendTextMessage(room + '@g.us', message)
#
# else: # private msg else: # private msg
# buddy = sender buddy = sender
# if message == "\\lastseen": # if message == "\\lastseen":
# self.presenceRequested.append(buddy)
# 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) self.sendTextMessage(sender + '@s.whatsapp.net', message)
def sendMessageToXMPP(self, buddy, messageContent, timestamp = ""): def sendMessageToXMPP(self, buddy, messageContent, timestamp = "", nickname = ""):
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))
@ -417,21 +458,6 @@ class Session(YowsupApp):
if receiptRequested: self.call("message_ack", (jid, messageId)) if receiptRequested: self.call("message_ack", (jid, messageId))
def onGroupGotInfo(self, gjid, owner, subject, subjectOwner, subjectTimestamp, creationTimestamp):
room = gjid.split("@")[0]
owner = owner.split("@")[0]
subjectOwner = subjectOwner.split("@")[0]
if room in self.groups:
room = self.groups[room]
room.owner = owner
room.subjectOwner = subjectOwner
room.subject = subject
else:
self.groups[room] = Group(room, owner, subject, subjectOwner)
self.updateRoomList()
def onGroupGotParticipants(self, gjid, jids): def onGroupGotParticipants(self, gjid, jids):
room = gjid.split("@")[0] room = gjid.split("@")[0]
group = self.groups[room] group = self.groups[room]
@ -470,15 +496,6 @@ class Session(YowsupApp):
if receiptRequested: self.call("subject_ack", (gjid, messageId)) if receiptRequested: self.call("subject_ack", (gjid, messageId))
# Yowsup Notifications # Yowsup Notifications
def onGroupParticipantAdded(self, gjid, jid, author, timestamp, messageId, receiptRequested):
room = gjid.split("@")[0]
buddy = jid.split("@")[0]
self.logger.info("Added %s to room %s", buddy, room)
self.backend.handleParticipantChanged(self.user, buddy, room, protocol_pb2.PARTICIPANT_FLAG_NONE, protocol_pb2.STATUS_ONLINE)
if receiptRequested: self.call("notification_ack", (gjid, messageId))
def onGroupParticipantRemoved(self, gjid, jid, author, timestamp, messageId, receiptRequested): def onGroupParticipantRemoved(self, gjid, jid, author, timestamp, messageId, receiptRequested):
room = gjid.split("@")[0] room = gjid.split("@")[0]
buddy = jid.split("@")[0] buddy = jid.split("@")[0]

View file

@ -1,4 +1,3 @@
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
@ -29,13 +28,16 @@ from yowsup.layers.protocol_calls import YowCallsProtocolLayer
# ProtocolEntities # ProtocolEntities
from yowsup.layers.protocol_presence.protocolentities import * from yowsup.layers.protocol_acks.protocolentities import *
from yowsup.layers.protocol_messages.protocolentities import *
from yowsup.layers.protocol_media.protocolentities import *
from yowsup.layers.protocol_chatstate.protocolentities import * from yowsup.layers.protocol_chatstate.protocolentities import *
from yowsup.layers.protocol_acks.protocolentities import * from yowsup.layers.protocol_groups.protocolentities import *
from yowsup.layers.protocol_receipts.protocolentities import * from yowsup.layers.protocol_media.protocolentities import *
from yowsup.layers.protocol_messages.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 functools import partial
class YowsupApp(object): class YowsupApp(object):
def __init__(self): def __init__(self):
@ -206,12 +208,11 @@ class YowsupApp(object):
- failure: (func) called when request has failed - failure: (func) called when request has failed
""" """
iq = LastseenIqProtocolEntity(phoneNumber + '@s.whatsapp.net') iq = LastseenIqProtocolEntity(phoneNumber + '@s.whatsapp.net')
self.sendIq(iq, onSuccess = self._lastSeenSuccess(success), onError = failure) self.sendIq(iq, onSuccess = partial(self._lastSeenSuccess, success),
onError = failure)
def _lastSeenSuccess(self, success): def _lastSeenSuccess(self, success, response, request):
def func(response, request): success(response._from.split('@')[0], response.seconds)
success(response._from.split('@')[0], response.seconds)
return func
def requestProfilePicture(self, phoneNumber, onSuccess = None, onFailure = None): def requestProfilePicture(self, phoneNumber, onSuccess = None, onFailure = None):
""" """
@ -223,6 +224,10 @@ class YowsupApp(object):
""" """
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)
def requestGroupsList(self, onSuccess = None, onFailure = None):
iq = ListGroupsIqProtocolEntity()
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):
""" """
@ -322,7 +327,7 @@ class YowsupApp(object):
- to: - to:
- notify: (str) human readable name of _from (e.g. John Smith) - notify: (str) human readable name of _from (e.g. John Smith)
- timestamp: - timestamp:
- participant: - participant: (str) jid of user who sent the message in a groupchat
- offline: - offline:
- retry: - retry:
- body: The content of the message - body: The content of the message