Rework groupchats to support sending private messages
This commit is contained in:
parent
02a27ecac8
commit
eba8d24a92
67
group.py
67
group.py
|
@ -21,14 +21,77 @@ __email__ = "post@steffenvogel.de"
|
||||||
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
|
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from Spectrum2 import protocol_pb2
|
||||||
|
|
||||||
class Group():
|
class Group():
|
||||||
|
|
||||||
def __init__(self, id, owner, subject, subjectOwner):
|
def __init__(self, id, owner, subject, subjectOwner, backend, user):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.subject = subject
|
self.subject = subject
|
||||||
self.subjectOwner = subjectOwner
|
self.subjectOwner = subjectOwner
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.joined = False
|
self.joined = False
|
||||||
|
self.backend = backend
|
||||||
|
self.user = user
|
||||||
|
|
||||||
self.nick = "me"
|
self.nick = "me"
|
||||||
self.participants = []
|
# Participants is a number -> nickname dict
|
||||||
|
self.participants = {}
|
||||||
|
|
||||||
|
def addParticipants(self, participants, buddies, yourNumber):
|
||||||
|
"""
|
||||||
|
Adds participants to the group.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- participants: (Iterable) phone numbers of participants
|
||||||
|
- buddies: (dict) Used to get the nicknames of the participants
|
||||||
|
- yourNumber: The number you are using
|
||||||
|
"""
|
||||||
|
for jid in participants:
|
||||||
|
number = jid.split('@')[0]
|
||||||
|
try:
|
||||||
|
nick = buddies[number].nick
|
||||||
|
except KeyError:
|
||||||
|
nick = number
|
||||||
|
if number == yourNumber:
|
||||||
|
nick = self.nick
|
||||||
|
if nick == "":
|
||||||
|
nick = number
|
||||||
|
self.participants[number] = nick
|
||||||
|
|
||||||
|
def sendParticipantsToSpectrum(self, yourNumber):
|
||||||
|
for number, nick in self.participants.iteritems():
|
||||||
|
if number == self.owner:
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_MODERATOR
|
||||||
|
else:
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_NONE
|
||||||
|
if number == yourNumber:
|
||||||
|
flags = flags | protocol_pb2.PARTICIPANT_FLAG_ME
|
||||||
|
|
||||||
|
self._updateParticipant(number, flags, 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)
|
||||||
|
del self.participants[number]
|
||||||
|
|
||||||
|
def changeNick(self, number, new_nick):
|
||||||
|
if self.participants[number] == new_nick:
|
||||||
|
return
|
||||||
|
if number == self.owner:
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_MODERATOR
|
||||||
|
else:
|
||||||
|
flags = protocol_pb2.PARTICIPANT_FLAG_NONE
|
||||||
|
self._updateParticipant(number, flags, protocol_pb2.STATUS_ONLINE, new_nick)
|
||||||
|
self.participants[number] = new_nick
|
||||||
|
|
||||||
|
def _updateParticipant(self, number, flags, status, newNick = ""):
|
||||||
|
nick = self.participants[number]
|
||||||
|
# Notice the status message is the buddy's number
|
||||||
|
if self.joined:
|
||||||
|
self.backend.handleParticipantChanged(
|
||||||
|
self.user, nick, self.id, flags,
|
||||||
|
status, number, newname = newNick)
|
||||||
|
|
123
session.py
123
session.py
|
@ -143,9 +143,10 @@ class Session(YowsupApp):
|
||||||
oroom.subjectOwner = subjectOwner
|
oroom.subjectOwner = subjectOwner
|
||||||
oroom.subject = subject
|
oroom.subject = subject
|
||||||
else:
|
else:
|
||||||
self.groups[room] = Group(room, owner, subject, subjectOwner)
|
self.groups[room] = Group(room, owner, subject, subjectOwner, self.backend, self.user)
|
||||||
# self.joinRoom(self._shortenGroupId(room), self.user.split("@")[0])
|
# self.joinRoom(self._shortenGroupId(room), self.user.split("@")[0])
|
||||||
self.groups[room].participants = group.getParticipants().keys()
|
self.groups[room].addParticipants(group.getParticipants().keys(),
|
||||||
|
self.buddies, self.legacyName)
|
||||||
|
|
||||||
#self._addParticipantsToRoom(room, group.getParticipants())
|
#self._addParticipantsToRoom(room, group.getParticipants())
|
||||||
|
|
||||||
|
@ -172,13 +173,15 @@ class Session(YowsupApp):
|
||||||
self.legacyName, room, nick)
|
self.legacyName, room, nick)
|
||||||
|
|
||||||
group = self.groups[room]
|
group = self.groups[room]
|
||||||
|
group.joined = True
|
||||||
group.nick = nick
|
group.nick = nick
|
||||||
|
group.participants[self.legacyName] = nick
|
||||||
try:
|
try:
|
||||||
ownerNick = self.buddies[group.subjectOwner].nick
|
ownerNick = group.participants[group.subjectOwner]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
ownerNick = group.subjectOwner
|
ownerNick = group.subjectOwner
|
||||||
|
|
||||||
self._refreshParticipants(room)
|
group.sendParticipantsToSpectrum(self.legacyName)
|
||||||
self.backend.handleSubject(self.user, self._shortenGroupId(room),
|
self.backend.handleSubject(self.user, self._shortenGroupId(room),
|
||||||
group.subject, ownerNick)
|
group.subject, ownerNick)
|
||||||
self.logger.debug("Room subject: room=%s, subject=%s",
|
self.logger.debug("Room subject: room=%s, subject=%s",
|
||||||
|
@ -186,7 +189,6 @@ class Session(YowsupApp):
|
||||||
self.backend.handleRoomNicknameChanged(
|
self.backend.handleRoomNicknameChanged(
|
||||||
self.user, self._shortenGroupId(room), group.subject
|
self.user, self._shortenGroupId(room), group.subject
|
||||||
)
|
)
|
||||||
group.joined = True
|
|
||||||
else:
|
else:
|
||||||
self.logger.warn("Room doesn't exist: %s", room)
|
self.logger.warn("Room doesn't exist: %s", room)
|
||||||
|
|
||||||
|
@ -198,29 +200,6 @@ class Session(YowsupApp):
|
||||||
else:
|
else:
|
||||||
self.logger.warn("Room doesn't exist: %s. Unable to leave.", room)
|
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)))
|
||||||
if seconds < 60:
|
if seconds < 60:
|
||||||
|
@ -300,17 +279,6 @@ class Session(YowsupApp):
|
||||||
buddy, self.legacyName, messageContent, timestamp)
|
buddy, self.legacyName, messageContent, timestamp)
|
||||||
if participant is not None: # Group message
|
if participant is not None: # Group message
|
||||||
partname = participant.split('@')[0]
|
partname = participant.split('@')[0]
|
||||||
try:
|
|
||||||
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,
|
self.sendGroupMessageToXMPP(buddy, partname, messageContent,
|
||||||
timestamp)
|
timestamp)
|
||||||
else:
|
else:
|
||||||
|
@ -441,11 +409,8 @@ class Session(YowsupApp):
|
||||||
subjectOwner = group.getSubjectOwnerJid(full = False)
|
subjectOwner = group.getSubjectOwnerJid(full = False)
|
||||||
subject = utils.softToUni(group.getSubject())
|
subject = utils.softToUni(group.getSubject())
|
||||||
|
|
||||||
self.groups[room] = Group(room, owner, subject, subjectOwner)
|
self.groups[room] = Group(room, owner, subject, subjectOwner, self.backend, self.user)
|
||||||
self.groups[room].participants = group.getParticipants().keys()
|
self.groups[room].addParticipants(group.getParticipants, self.buddies, self.legacyName)
|
||||||
# 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.bot.send("You have been added to group: %s@%s (%s)"
|
||||||
% (self._shortenGroupId(room), subject, self.backend.spectrum_jid))
|
% (self._shortenGroupId(room), subject, self.backend.spectrum_jid))
|
||||||
|
|
||||||
|
@ -453,29 +418,14 @@ class Session(YowsupApp):
|
||||||
def onParticipantsAddedToGroup(self, group):
|
def onParticipantsAddedToGroup(self, group):
|
||||||
self.logger.debug("Participants added to group: %s", group)
|
self.logger.debug("Participants added to group: %s", group)
|
||||||
room = group.getGroupId().split('@')[0]
|
room = group.getGroupId().split('@')[0]
|
||||||
self.groups[room].participants.extend(group.getParticipants())
|
self.groups[room].addParticipants(group.getParticipants(), self.buddies, self.legacyName)
|
||||||
self._refreshParticipants(room)
|
self.groups[room].sendParticipantsToSpectrum(self.legacyName)
|
||||||
|
|
||||||
# Called by superclass
|
# Called by superclass
|
||||||
def onParticipantsRemovedFromGroup(self, room, participants):
|
def onParticipantsRemovedFromGroup(self, room, participants):
|
||||||
self.logger.debug("Participants removed from group: %s, %s",
|
self.logger.debug("Participants removed from group: %s, %s",
|
||||||
room, participants)
|
room, participants)
|
||||||
group = self.groups[room]
|
self.groups[room].removeParticipants(participants)
|
||||||
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)
|
||||||
|
@ -542,13 +492,19 @@ class Session(YowsupApp):
|
||||||
elif "-" in sender: # group msg
|
elif "-" in sender: # group msg
|
||||||
if "/" in sender: # directed at single user
|
if "/" in sender: # directed at single user
|
||||||
room, nick = sender.split("/")
|
room, nick = sender.split("/")
|
||||||
for buddy, buddy3 in self.buddies.iteritems():
|
group = self.groups[room]
|
||||||
self.logger.info("Group buddy=%s nick=%s", buddy,
|
number = None
|
||||||
buddy3.nick)
|
for othernumber, othernick in group.participants.iteritems():
|
||||||
if buddy3.nick == nick:
|
if othernick == nick:
|
||||||
nick = buddy
|
number = othernumber
|
||||||
waId = self.sendTextMessage(nick + '@s.whatsapp.net', message)
|
break
|
||||||
self.msgIDs[waId] = MsgIDs( ID, waId)
|
if number is not None:
|
||||||
|
self.logger.debug("Private message sent from %s to %s", self.legacyName, number)
|
||||||
|
waId = self.sendTextMessage(number + '@s.whatsapp.net', message)
|
||||||
|
self.msgIDs[waId] = MsgIDs( ID, waId)
|
||||||
|
else:
|
||||||
|
self.logger.error("Attempted to send private message to non-existent user")
|
||||||
|
self.logger.debug("%s to %s in %s", self.legacyName, nick, room)
|
||||||
else:
|
else:
|
||||||
room = sender
|
room = sender
|
||||||
if message[0] == '\\' and message[:1] != '\\\\':
|
if message[0] == '\\' and message[:1] != '\\\\':
|
||||||
|
@ -680,35 +636,34 @@ class Session(YowsupApp):
|
||||||
self.backend.handleMessage(self.user, buddy, messageContent, "",
|
self.backend.handleMessage(self.user, buddy, messageContent, "",
|
||||||
"", timestamp)
|
"", timestamp)
|
||||||
|
|
||||||
def sendGroupMessageToXMPP(self, room, buddy, messageContent, timestamp = ""):
|
def sendGroupMessageToXMPP(self, room, number, 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",
|
self.logger.debug("Group message queued from %s to %s: %s",
|
||||||
buddy, room, messageContent)
|
number, room, messageContent)
|
||||||
|
|
||||||
if room not in self.groupOfflineQueue:
|
if room not in self.groupOfflineQueue:
|
||||||
self.groupOfflineQueue[room] = [ ]
|
self.groupOfflineQueue[room] = [ ]
|
||||||
|
|
||||||
self.groupOfflineQueue[room].append(
|
self.groupOfflineQueue[room].append(
|
||||||
(buddy, messageContent, timestamp)
|
(number, messageContent, timestamp)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.logger.debug("Group message sent from %s (%s) to %s: %s",
|
self.logger.debug("Group message sent from %s to %s: %s",
|
||||||
buddy, nick, room, messageContent)
|
number, room, messageContent)
|
||||||
try:
|
try:
|
||||||
group = self.groups[room]
|
group = self.groups[room]
|
||||||
|
# Update nickname
|
||||||
|
try:
|
||||||
|
if self.buddies[number].nick != "":
|
||||||
|
group.changeNick(number, self.buddies[number].nick)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
nick = group.participants[number]
|
||||||
if group.joined:
|
if group.joined:
|
||||||
self.backend.handleMessage(self.user,room, messageContent,
|
self.backend.handleMessage(self.user, room, messageContent,
|
||||||
nick, "", timestamp)
|
nick, "", timestamp)
|
||||||
else:
|
else:
|
||||||
self.bot.send("You have received a message in group: %s@%s"
|
self.bot.send("You have received a message in group: %s@%s"
|
||||||
|
@ -718,7 +673,7 @@ class Session(YowsupApp):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.logger.warn("Group is not in group list")
|
self.logger.warn("Group is not in group list")
|
||||||
self.backend.handleMessage(self.user, self._shortenGroupId(room),
|
self.backend.handleMessage(self.user, self._shortenGroupId(room),
|
||||||
messageContent, nick, "", timestamp)
|
messageContent, number, "", timestamp)
|
||||||
|
|
||||||
|
|
||||||
def changeStatus(self, status):
|
def changeStatus(self, status):
|
||||||
|
|
Loading…
Reference in a new issue