Browse Source

moved Spectrum2 package to new repo

yowsup-3
Steffen Vogel 3 years ago
parent
commit
b93645285c
  1. 0
      Spectrum2/__init__.py
  2. 656
      Spectrum2/backend.py
  3. 141
      Spectrum2/config.py
  4. 66
      Spectrum2/iochannel.py
  5. 203
      Spectrum2/protocol.proto
  6. 1459
      Spectrum2/protocol_pb2.py
  7. 4
      setup.py

0
Spectrum2/__init__.py

656
Spectrum2/backend.py

@ -1,656 +0,0 @@
__author__ = "Steffen Vogel"
__copyright__ = "Copyright 2015-2017, Steffen Vogel"
__license__ = "GPLv3"
__maintainer__ = "Steffen Vogel"
__email__ = "post@steffenvogel.de"
"""
This file is part of transWhat
transWhat is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
transwhat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
"""
import protocol_pb2
import socket
import struct
import sys
import os
import logging
import google.protobuf
import resource
def WRAP(MESSAGE, TYPE):
wrap = protocol_pb2.WrapperMessage()
wrap.type = TYPE
wrap.payload = bytes(MESSAGE)
return wrap.SerializeToString()
class SpectrumBackend:
"""
Creates new NetworkPlugin and connects the Spectrum2 NetworkPluginServer.
@param loop: Event loop.
@param host: Host where Spectrum2 NetworkPluginServer runs.
@param port: Port.
"""
def __init__(self):
self.m_pingReceived = False
self.m_data = bytes("")
self.m_init_res = 0
self.logger = logging.getLogger(self.__class__.__name__)
def handleMessage(self, user, legacyName, msg, nickname = "", xhtml = "", timestamp = ""):
m = protocol_pb2.ConversationMessage()
m.userName = user
m.buddyName = legacyName
m.message = msg
m.nickname = nickname
m.xhtml = xhtml
m.timestamp = str(timestamp)
message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONV_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):
m = protocol_pb2.ConversationMessage()
m.userName = user
m.buddyName = buddyName
m.message = msg
message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_ATTENTION)
self.send(message)
def handleVCard(self, user, ID, legacyName, fullName, nickname, photo):
vcard = protocol_pb2.VCard()
vcard.userName = user
vcard.buddyName = legacyName
vcard.id = ID
vcard.fullname = fullName
vcard.nickname = nickname
vcard.photo = bytes(photo)
message = WRAP(vcard.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_VCARD)
self.send(message)
def handleSubject(self, user, legacyName, msg, nickname = ""):
m = protocol_pb2.ConversationMessage()
m.userName = user
m.buddyName = legacyName
m.message = msg
m.nickname = nickname
message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_ROOM_SUBJECT_CHANGED)
self.send(message)
def handleBuddyChanged(self, user, buddyName, alias, groups, status, statusMessage = "", iconHash = "", blocked = False):
buddy = protocol_pb2.Buddy()
buddy.userName = user
buddy.buddyName = buddyName
buddy.alias = alias
buddy.group.extend(groups)
buddy.status = status
buddy.statusMessage = statusMessage
buddy.iconHash = iconHash
buddy.blocked = blocked
message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_CHANGED)
self.send(message)
def handleBuddyRemoved(self, user, buddyName):
buddy = protocol_pb2.Buddy()
buddy.userName = user
buddy.buddyName = buddyName
message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_REMOVED)
self.send(message);
def handleBuddyTyping(self, user, buddyName):
buddy = protocol_pb2.Buddy()
buddy.userName = user
buddy.buddyName = buddyName
message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPING)
self.send(message);
def handleBuddyTyped(self, user, buddyName):
buddy = protocol_pb2.Buddy()
buddy.userName = user
buddy.buddyName = buddyName
message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPED)
self.send(message);
def handleBuddyStoppedTyping(self, user, buddyName):
buddy = protocol_pb2.Buddy()
buddy.userName = user
buddy.buddyName = buddyName
message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_STOPPED_TYPING)
self.send(message)
def handleAuthorization(self, user, buddyName):
buddy = protocol_pb2.Buddy()
buddy.userName = user
buddy.buddyName = buddyName
message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_AUTH_REQUEST)
self.send(message)
def handleConnected(self, user):
d = protocol_pb2.Connected()
d.user = user
message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONNECTED)
self.send(message);
def handleDisconnected(self, user, error = 0, msg = ""):
d = protocol_pb2.Disconnected()
d.user = user
d.error = error
d.message = msg
message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_DISCONNECTED)
self.send(message);
def handleParticipantChanged(self, user, nickname, room, flags, status, statusMessage = "", newname = "", iconHash = ""):
d = protocol_pb2.Participant()
d.userName = user
d.nickname = nickname
d.room = room
d.flag = flags
d.newname = newname
d.iconHash = iconHash
d.status = status
d.statusMessage = statusMessage
message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_PARTICIPANT_CHANGED)
self.send(message);
def handleRoomNicknameChanged(self, user, r, nickname):
room = protocol_pb2.Room()
room.userName = user
room.nickname = nickname
room.room = r
room.password = ""
message = WRAP(room.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_ROOM_NICKNAME_CHANGED)
self.send(message);
def handleRoomList(self, rooms):
roomList = protocol_pb2.RoomList()
for room in rooms:
roomList.room.append(room[0])
roomList.name.append(room[1])
message = WRAP(roomList.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_ROOM_LIST)
self.send(message);
def handleFTStart(self, user, buddyName, fileName, size):
room = protocol_pb2.File()
room.userName = user
room.buddyName = buddyName
room.fileName = fileName
room.size = size
message = WRAP(room.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_FT_START)
self.send(message);
def handleFTFinish(self, user, buddyName, fileName, size, ftid):
room = protocol_pb2.File()
room.userName = user
room.buddyName = buddyName
room.fileName = fileName
room.size = size
# Check later
if ftid != 0:
room.ftID = ftid
message = WRAP(room.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_FT_FINISH)
self.send(message)
def handleFTData(self, ftID, data):
d = protocol_pb2.FileTransferData()
d.ftid = ftID
d.data = bytes(data)
message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_FT_DATA);
self.send(message)
def handleBackendConfig(self, data):
"""
data is a dictionary, whose keys are sections and values are a list of
tuples of configuration key and configuration value.
"""
c = protocol_pb2.BackendConfig()
config = []
for section, rest in data.items():
config.append('[%s]' % section)
for key, value in rest:
config.append('%s = %s' % (key, value))
c.config = '\n'.join(config)
message = WRAP(c.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BACKEND_CONFIG);
self.send(message)
def handleQuery(self, command):
c = protocol_pb2.BackendConfig()
c.config = command
message = WRAP(c.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_QUERY);
self.send(message)
def handleLoginPayload(self, data):
payload = protocol_pb2.Login()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleLoginRequest(payload.user, payload.legacyName, payload.password, payload.extraFields)
def handleLogoutPayload(self, data):
payload = protocol_pb2.Logout()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleLogoutRequest(payload.user, payload.legacyName)
def handleStatusChangedPayload(self, data):
payload = protocol_pb2.Status()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleStatusChangeRequest(payload.userName, payload.status, payload.statusMessage)
def handleConvMessagePayload(self, data):
payload = protocol_pb2.ConversationMessage()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
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):
payload = protocol_pb2.ConversationMessage()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleAttentionRequest(payload.userName, payload.buddyName, payload.message)
def handleFTStartPayload(self, data):
payload = protocol_pb2.File()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleFTStartRequest(payload.userName, payload.buddyName, payload.fileName, payload.size, payload.ftID);
def handleFTFinishPayload(self, data):
payload = protocol_pb2.File()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleFTFinishRequest(payload.userName, payload.buddyName, payload.fileName, payload.size, payload.ftID)
def handleFTPausePayload(self, data):
payload = protocol_pb2.FileTransferData()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleFTPauseRequest(payload.ftID)
def handleFTContinuePayload(self, data):
payload = protocol_pb2.FileTransferData()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleFTContinueRequest(payload.ftID)
def handleJoinRoomPayload(self, data):
payload = protocol_pb2.Room()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleJoinRoomRequest(payload.userName, payload.room, payload.nickname, payload.password)
def handleLeaveRoomPayload(self, data):
payload = protocol_pb2.Room()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleLeaveRoomRequest(payload.userName, payload.room)
def handleVCardPayload(self, data):
payload = protocol_pb2.VCard()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
if payload.HasField('photo'):
self.handleVCardUpdatedRequest(payload.userName, payload.photo, payload.nickname)
elif len(payload.buddyName) > 0:
self.handleVCardRequest(payload.userName, payload.buddyName, payload.id)
def handleBuddyChangedPayload(self, data):
payload = protocol_pb2.Buddy()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
if payload.HasField('blocked'):
self.handleBuddyBlockToggled(payload.userName, payload.buddyName, payload.blocked)
else:
groups = [g for g in payload.group]
self.handleBuddyUpdatedRequest(payload.userName, payload.buddyName, payload.alias, groups);
def handleBuddyRemovedPayload(self, data):
payload = protocol_pb2.Buddy()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
groups = [g for g in payload.group]
self.handleBuddyRemovedRequest(payload.userName, payload.buddyName, groups);
def handleBuddiesPayload(self, data):
payload = protocol_pb2.Buddies()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
self.handleBuddies(payload);
def handleChatStatePayload(self, data, msgType):
payload = protocol_pb2.Buddy()
if (payload.ParseFromString(data) == False):
#TODO: ERROR
return
if msgType == protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPING:
self.handleTypingRequest(payload.userName, payload.buddyName)
elif msgType == protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPED:
self.handleTypedRequest(payload.userName, payload.buddyName)
elif msgType == protocol_pb2.WrapperMessage.TYPE_BUDDY_STOPPED_TYPING:
self.handleStoppedTypingRequest(payload.userName, payload.buddyName)
def handleDataRead(self, data):
self.m_data += data
while len(self.m_data) != 0:
expected_size = 0
if (len(self.m_data) >= 4):
expected_size = struct.unpack('!I', self.m_data[0:4])[0]
if (len(self.m_data) - 4 < expected_size):
self.logger.debug("Data packet incomplete")
return
else:
self.logger.debug("Data packet incomplete")
return
packet = self.m_data[4:4+expected_size]
wrapper = protocol_pb2.WrapperMessage()
try:
parseFromString = wrapper.ParseFromString(packet)
except:
self.m_data = self.m_data[expected_size+4:]
self.logger.error("Parse from String exception. Skipping packet.")
return
if parseFromString == False:
self.m_data = self.m_data[expected_size+4:]
self.logger.error("Parse from String failed. Skipping packet.")
return
self.m_data = self.m_data[4+expected_size:]
if wrapper.type == protocol_pb2.WrapperMessage.TYPE_LOGIN:
self.handleLoginPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_LOGOUT:
self.handleLogoutPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_PING:
self.sendPong()
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE:
self.handleConvMessagePayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_JOIN_ROOM:
self.handleJoinRoomPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_LEAVE_ROOM:
self.handleLeaveRoomPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_VCARD:
self.handleVCardPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDY_CHANGED:
self.handleBuddyChangedPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDY_REMOVED:
self.handleBuddyRemovedPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_STATUS_CHANGED:
self.handleStatusChangedPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPING:
self.handleChatStatePayload(wrapper.payload, protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPING)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPED:
self.handleChatStatePayload(wrapper.payload, protocol_pb2.WrapperMessage.TYPE_BUDDY_TYPED)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDY_STOPPED_TYPING:
self.handleChatStatePayload(wrapper.payload, protocol_pb2.WrapperMessage.TYPE_BUDDY_STOPPED_TYPING)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_ATTENTION:
self.handleAttentionPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_FT_START:
self.handleFTStartPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_FT_FINISH:
self.handleFTFinishPayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_FT_PAUSE:
self.handleFTPausePayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_FT_CONTINUE:
self.handleFTContinuePayload(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_EXIT:
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:
self.handleRawXmlRequest(wrapper.payload)
elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDIES:
self.handleBuddiesPayload(wrapper.payload)
def send(self, data):
header = struct.pack('!I',len(data))
self.sendData(header + data)
def checkPing(self):
if (self.m_pingReceived == False):
self.handleExitRequest()
self.m_pingReceived = False
def sendPong(self):
self.m_pingReceived = True
wrap = protocol_pb2.WrapperMessage()
wrap.type = protocol_pb2.WrapperMessage.TYPE_PONG
message = wrap.SerializeToString()
self.send(message)
self.sendMemoryUsage()
def sendMemoryUsage(self):
stats = protocol_pb2.Stats()
stats.init_res = self.m_init_res
res = 0
shared = 0
e_res, e_shared = self.handleMemoryUsage()
stats.res = res + e_res
stats.shared = shared + e_shared
stats.id = str(os.getpid())
message = WRAP(stats.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_STATS)
self.send(message)
def handleLoginRequest(self, user, legacyName, password, extra):
"""
Called when XMPP user wants to connect legacy network.
You should connect him to legacy network and call handleConnected or handleDisconnected function later.
@param user: XMPP JID of user for which this event occurs.
@param legacyName: Legacy network name of this user used for login.
@param password: Legacy network password of this user.
"""
#\msc
#NetworkPlugin,YourNetworkPlugin,LegacyNetwork;
#NetworkPlugin->YourNetworkPlugin [label="handleLoginRequest(...)", URL="\ref NetworkPlugin::handleLoginRequest()"];
#YourNetworkPlugin->LegacyNetwork [label="connect the legacy network"];
#--- [label="If password was valid and user is connected and logged in"];
#YourNetworkPlugin<-LegacyNetwork [label="connected"];
#YourNetworkPlugin->NetworkPlugin [label="handleConnected()", URL="\ref NetworkPlugin::handleConnected()"];
#--- [label="else"];
#YourNetworkPlugin<-LegacyNetwork [label="disconnected"];
#YourNetworkPlugin->NetworkPlugin [label="handleDisconnected()", URL="\ref NetworkPlugin::handleDisconnected()"];
#\endmsc
raise NotImplementedError, "Implement me"
def handleBuddies(self, buddies):
pass
def handleLogoutRequest(self, user, legacyName):
"""
Called when XMPP user wants to disconnect legacy network.
You should disconnect him from legacy network.
@param user: XMPP JID of user for which this event occurs.
@param legacyName: Legacy network name of this user used for login.
"""
raise NotImplementedError, "Implement me"
def handleMessageSendRequest(self, user, legacyName, message, xhtml = "", 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 message: Plain text message.
@param xhtml: XHTML message.
@param ID: message ID
"""
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):
""" Called when XMPP user requests VCard of buddy.
@param user: XMPP JID of user for which this event occurs.
@param legacyName: Legacy network name of buddy whose VCard is requested.
@param ID: ID which is associated with this request. You have to pass it to handleVCard function when you receive VCard."""
#\msc
#NetworkPlugin,YourNetworkPlugin,LegacyNetwork;
#NetworkPlugin->YourNetworkPlugin [label="handleVCardRequest(...)", URL="\ref NetworkPlugin::handleVCardRequest()"];
#YourNetworkPlugin->LegacyNetwork [label="start VCard fetching"];
#YourNetworkPlugin<-LegacyNetwork [label="VCard fetched"];
#YourNetworkPlugin->NetworkPlugin [label="handleVCard()", URL="\ref NetworkPlugin::handleVCard()"];
#\endmsc
pass
def handleVCardUpdatedRequest(self, user, photo, nickname):
"""
Called when XMPP user updates his own VCard.
You should update the VCard in legacy network too.
@param user: XMPP JID of user for which this event occurs.
@param photo: Raw photo data.
"""
pass
def handleJoinRoomRequest(self, user, room, nickname, pasword):
pass
def handleLeaveRoomRequest(self, user, room):
pass
def handleStatusChangeRequest(self, user, status, statusMessage):
pass
def handleBuddyUpdatedRequest(self, user, buddyName, alias, groups):
pass
def handleBuddyRemovedRequest(self, user, buddyName, groups):
pass
def handleBuddyBlockToggled(self, user, buddyName, blocked):
pass
def handleTypingRequest(self, user, buddyName):
pass
def handleTypedRequest(self, user, buddyName):
pass
def handleStoppedTypingRequest(self, user, buddyName):
pass
def handleAttentionRequest(self, user, buddyName, message):
pass
def handleFTStartRequest(self, user, buddyName, fileName, size, ftID):
pass
def handleFTFinishRequest(self, user, buddyName, fileName, size, ftID):
pass
def handleFTPauseRequest(self, ftID):
pass
def handleFTContinueRequest(self, ftID):
pass
def handleMemoryUsage(self):
return (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss, 0)
def handleExitRequest(self):
sys.exit(1)
def handleRawXmlRequest(self, xml):
pass
def sendData(self, data):
pass

141
Spectrum2/config.py

@ -1,141 +0,0 @@
__author__ = "Steffen Vogel"
__copyright__ = "Copyright 2015-2017, Steffen Vogel"
__license__ = "GPLv3"
__maintainer__ = "Steffen Vogel"
__email__ = "post@steffenvogel.de"
"""
This file is part of transWhat
transWhat is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
transwhat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
"""
# I'm guessing this is the format of the spectrum config file in BNF
# <config_file> ::= <line>*
# <line> ::= <space>* <expr> <space>* <newline> | <space*>
# <expr> ::= <section> | <assignment>
# <section> ::= [<identifier>*]
# <assignment> ::= <identifier> <space>* = <space>* <value>
class SpectrumConfig:
"""
Represents spectrum2 configuration options.
"""
def __init__(self, path_to_config_file):
"""
Initialises configuration file.
Args:
path_to_config_file: The absolute path to the configuration file.
"""
self.config_path = path_to_config_file
self.options = self.loadConfig(self.config_path)
# Load backend_logging information
self.options.update(self.loadConfig(self['logging.backend_config']))
def loadConfig(self, file_name):
section = {'a': ""} # Current section heading,
# It's a dictionary because variables in python closures can't be
# assigned to.
options = dict()
# Recursive descent parser
def consume_spaces(line):
i = 0
for c in line:
if c != ' ':
break
i += 1
return line[i:]
def read_identifier(line):
i = 0
for c in line:
if c == ' ' or c==']' or c=='[' or c=='=':
break
i += 1
# no identifier
if i == 0:
return (None, 'No identifier')
return (line[:i], line[i:])
def parse_section(line):
if len(line) == 0 or line[0] != '[':
return (None, 'expected [')
line = line[1:]
identifier, line = read_identifier(line)
if len(line) == 0 or line[0] != ']' or identifier is None:
return (None, line)
return (identifier, line[1:])
def parse_assignment(line):
key, line = read_identifier(line)
if key is None:
return (None, None, line)
line = consume_spaces(line)
if len(line) == 0 or line[0] != '=':
return (None, None, 'Expected =')
line = consume_spaces(line[1:])
value = line[:-1]
return (key, value, '\n')
def expr(line):
sec, newline = parse_section(line)
if sec is not None:
section['a'] = sec
else:
key, value, newline = parse_assignment(line)
if key is not None:
if section['a'] != '':
options[section['a'] + '.' + key] = value
else:
options[key] = value
else:
return (None, newline)
return (newline, None)
def parse_line(line, line_number):
line = consume_spaces(line)
if line == '\n':
return
newline, error = expr(line)
if newline is None:
raise ConfigParseError(str(line_number) + ': ' + error + ': ' + repr(line))
newline = consume_spaces(newline)
if newline != '\n':
raise ConfigParseError(str(line_number) + ': Expected newline got ' + repr(newline))
def strip_comments(line):
i = 0
for c in line:
if c == '#' or c == '\n':
break
i += 1
return line[:i] + '\n'
with open(file_name, 'r') as f:
i = 1
while True:
line = f.readline()
if line == '':
break
parse_line(strip_comments(line), i)
i += 1
return options
def __getitem__(self, key):
return self.options[key]
class ConfigParseError(Exception):
pass

66
Spectrum2/iochannel.py

@ -1,66 +0,0 @@
__author__ = "Steffen Vogel"
__copyright__ = "Copyright 2015-2017, Steffen Vogel"
__license__ = "GPLv3"
__maintainer__ = "Steffen Vogel"
__email__ = "post@steffenvogel.de"
"""
This file is part of transWhat
transWhat is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
transwhat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
"""
import asyncore, socket
import logging
import sys
class IOChannel(asyncore.dispatcher):
def __init__(self, host, port, callback, closeCallback):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
self.logger = logging.getLogger(self.__class__.__name__)
self.callback = callback
self.closeCallback = closeCallback
self.buffer = bytes("")
def sendData(self, data):
self.buffer += data
def handle_connect(self):
pass
def handle_close(self):
self.close()
def handle_read(self):
data = self.recv(65536)
self.callback(data)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
def handle_close(self):
self.logger.info('Connection to backend closed, terminating.')
self.close()
self.closeCallback()
def writable(self):
return (len(self.buffer) > 0)
def readable(self):
return True

203
Spectrum2/protocol.proto

@ -1,203 +0,0 @@
package pbnetwork;
enum ConnectionError {
CONNECTION_ERROR_NETWORK_ERROR = 0;
CONNECTION_ERROR_INVALID_USERNAME = 1;
CONNECTION_ERROR_AUTHENTICATION_FAILED = 2;
CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE = 3;
CONNECTION_ERROR_NO_SSL_SUPPORT = 4;
CONNECTION_ERROR_ENCRYPTION_ERROR = 5;
CONNECTION_ERROR_NAME_IN_USE = 6;
CONNECTION_ERROR_INVALID_SETTINGS = 7;
CONNECTION_ERROR_CERT_NOT_PROVIDED = 8;
CONNECTION_ERROR_CERT_UNTRUSTED = 9;
CONNECTION_ERROR_CERT_EXPIRED = 10;
CONNECTION_ERROR_CERT_NOT_ACTIVATED = 11;
CONNECTION_ERROR_CERT_HOSTNAME_MISMATCH = 12;
CONNECTION_ERROR_CERT_FINGERPRINT_MISMATCH = 13;
CONNECTION_ERROR_CERT_SELF_SIGNED = 14;
CONNECTION_ERROR_CERT_OTHER_ERROR = 15;
CONNECTION_ERROR_OTHER_ERROR = 16;
}
enum StatusType {
STATUS_ONLINE = 0;
STATUS_AWAY = 1;
STATUS_FFC = 2;
STATUS_XA = 3;
STATUS_DND = 4;
STATUS_NONE = 5;
STATUS_INVISIBLE = 6;
}
message Connected {
required string user = 1;
}
message Disconnected {
required string user = 1;
required int32 error = 2;
optional string message = 3;
}
message Login {
required string user = 1;
required string legacyName = 2;
required string password = 3;
repeated string extraFields = 4;
}
message Logout {
required string user = 1;
required string legacyName = 2;
}
message Buddy {
required string userName = 1;
required string buddyName = 2;
optional string alias = 3;
repeated string group = 4;
optional StatusType status = 5;
optional string statusMessage = 6;
optional string iconHash = 7;
optional bool blocked = 8;
}
message Buddies {
repeated Buddy buddy = 1;
}
message ConversationMessage {
required string userName = 1;
required string buddyName = 2;
required string message = 3;
optional string nickname = 4;
optional string xhtml = 5;
optional string timestamp = 6;
optional bool headline = 7;
optional string id = 8;
optional bool pm = 9;
optional bool carbon = 10;
}
message Room {
required string userName = 1;
required string nickname = 2;
required string room = 3;
optional string password = 4;
}
message RoomList {
repeated string room = 1;
repeated string name = 2;
optional string user = 3;
}
enum ParticipantFlag {
PARTICIPANT_FLAG_NONE = 0;
PARTICIPANT_FLAG_MODERATOR = 1;
PARTICIPANT_FLAG_CONFLICT = 2;
PARTICIPANT_FLAG_BANNED = 4;
PARTICIPANT_FLAG_NOT_AUTHORIZED = 8;
PARTICIPANT_FLAG_ME = 16;
PARTICIPANT_FLAG_KICKED = 32;
PARTICIPANT_FLAG_ROOM_NOT_FOUND = 64;
}
message Participant {
required string userName = 1;
required string room = 2;
required string nickname = 3;
required int32 flag = 4;
required StatusType status = 5;
optional string statusMessage = 6;
optional string newname = 7;
optional string iconHash = 8;
optional string alias = 9;
}
message VCard {
required string userName = 1;
required string buddyName = 2;
required int32 id = 3;
optional string fullname = 4;
optional string nickname = 5;
optional bytes photo = 6;
}
message Status {
required string userName = 1;
required StatusType status = 3;
optional string statusMessage = 4;
}
message Stats {
required int32 res = 1;
required int32 init_res = 2;
required int32 shared = 3;
required string id = 4;
}
message File {
required string userName = 1;
required string buddyName = 2;
required string fileName = 3;
required int32 size = 4;
optional int32 ftID = 5;
}
message FileTransferData {
required int32 ftID = 1;
required bytes data = 2;
}
message BackendConfig {
required string config = 1;
}
message APIVersion {
required int32 version = 1;
}
message WrapperMessage {
enum Type {
TYPE_CONNECTED = 1;
TYPE_DISCONNECTED = 2;
TYPE_LOGIN = 3;
TYPE_LOGOUT = 4;
TYPE_BUDDY_CHANGED = 6;
TYPE_BUDDY_REMOVED = 7;
TYPE_CONV_MESSAGE = 8;
TYPE_PING = 9;
TYPE_PONG = 10;
TYPE_JOIN_ROOM = 11;
TYPE_LEAVE_ROOM = 12;
TYPE_PARTICIPANT_CHANGED = 13;
TYPE_ROOM_NICKNAME_CHANGED = 14;
TYPE_ROOM_SUBJECT_CHANGED = 15;
TYPE_VCARD = 16;
TYPE_STATUS_CHANGED = 17;
TYPE_BUDDY_TYPING = 18;
TYPE_BUDDY_STOPPED_TYPING = 19;
TYPE_BUDDY_TYPED = 20;
TYPE_AUTH_REQUEST = 21;
TYPE_ATTENTION = 22;
TYPE_STATS = 23;
TYPE_FT_START = 24;
TYPE_FT_FINISH = 25;
TYPE_FT_DATA = 26;
TYPE_FT_PAUSE = 27;
TYPE_FT_CONTINUE = 28;
TYPE_EXIT = 29;
TYPE_BACKEND_CONFIG = 30;
TYPE_QUERY = 31;
TYPE_ROOM_LIST = 32;
TYPE_CONV_MESSAGE_ACK = 33;
TYPE_RAW_XML = 34;
TYPE_BUDDIES = 35;
TYPE_API_VERSION = 36;
}
required Type type = 1;
optional bytes payload = 2;
}
;

1459
Spectrum2/protocol_pb2.py

File diff suppressed because one or more lines are too long

4
setup.py

@ -30,8 +30,7 @@ setup(name='transwhat',
],
license='GPL-3+',
packages=[
'transWhat',
'Spectrum2'
'transWhat'
],
scripts=[
'transWhat/transwhat.py'
@ -39,6 +38,7 @@ setup(name='transwhat',
install_requires=[
'protobuf',
'yowsup',
'pyspectrum2'
'Pillow',
'python-dateutil'
],

Loading…
Cancel
Save