2012-10-20 22:38:28 +00:00
|
|
|
|
2012-10-21 04:46:59 +00:00
|
|
|
USER_NAME_SIZE = 9
|
|
|
|
|
2012-10-20 22:38:28 +00:00
|
|
|
class ToorChatProtocol():
|
|
|
|
''' This is a class to allow for easy of use with anything to do with messaging '''
|
|
|
|
def __init__(self, device):
|
|
|
|
self.PACKET_START = "\xFF\xDE\xAD\xFF"
|
|
|
|
self.PACKET_END = "\xFF\xBE\xEF\xFF"
|
|
|
|
self.device = device
|
|
|
|
|
2012-10-20 22:45:01 +00:00
|
|
|
def send_message(self, message = "", user = None):
|
2012-10-20 22:38:28 +00:00
|
|
|
''' This is used to send a simple message over the toorchat protocol '''
|
|
|
|
msg = ToorChatMessage(message, user)
|
2012-10-20 22:45:01 +00:00
|
|
|
self.device.RFxmit(msg.to_string())
|
2012-10-21 05:03:08 +00:00
|
|
|
return msg
|
2012-10-20 22:38:28 +00:00
|
|
|
|
2012-10-21 03:59:50 +00:00
|
|
|
@classmethod
|
|
|
|
def parse_message(cls, raw_message):
|
2012-10-21 03:39:59 +00:00
|
|
|
message = ToorChatMessage()
|
2012-10-21 03:59:50 +00:00
|
|
|
message.raw = raw_message
|
2012-10-21 03:39:59 +00:00
|
|
|
start_index = raw_message.find(ToorChatProtocol.get_packet_start())
|
|
|
|
end_index = raw_message.find(ToorChatProtocol.get_packet_end())
|
|
|
|
if start_index == -1 or end_index == -1:
|
|
|
|
return None
|
|
|
|
message.start = raw_message[start_index:start_index + 4]
|
|
|
|
message.xid = raw_message[start_index + 4: start_index + 12]
|
2012-10-21 04:46:59 +00:00
|
|
|
message.user = raw_message[start_index + 12: start_index + 21]
|
|
|
|
message.data = raw_message[start_index + 21: end_index]
|
2012-10-21 03:59:50 +00:00
|
|
|
message.end = raw_message[end_index: end_index+4]
|
2012-10-21 03:39:59 +00:00
|
|
|
return message
|
|
|
|
|
2012-10-20 22:38:28 +00:00
|
|
|
@classmethod
|
|
|
|
def get_packet_start(cls):
|
|
|
|
return "\xFF\xDE\xAD\xFF"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_packet_end(cls):
|
|
|
|
return "\xFF\xBE\xEF\xFF"
|
|
|
|
|
|
|
|
class ToorChatMessage():
|
|
|
|
''' This is a simple Message object wrapper to make things cleaner '''
|
|
|
|
|
|
|
|
def __init__(self, message = "", user = None):
|
2012-10-21 03:59:50 +00:00
|
|
|
self.raw = None
|
2012-10-20 22:38:28 +00:00
|
|
|
self.start = ToorChatProtocol.get_packet_start()
|
|
|
|
self.xid = self.get_random_xid()
|
|
|
|
if user != None:
|
|
|
|
self.user = user
|
2012-10-21 04:49:19 +00:00
|
|
|
if len(self.user) < USER_NAME_SIZE:
|
|
|
|
self.user = self.user + " "* (USER_NAME_SIZE-len(self.user))
|
2012-10-20 22:38:28 +00:00
|
|
|
else:
|
2012-10-21 04:46:59 +00:00
|
|
|
self.user = "anonymous"
|
2012-10-20 22:38:28 +00:00
|
|
|
self.data = message
|
|
|
|
self.end = ToorChatProtocol.get_packet_end()
|
|
|
|
|
2012-10-20 22:45:01 +00:00
|
|
|
def get_random_xid(self):
|
|
|
|
return "44444444"
|
|
|
|
|
2012-10-20 22:38:28 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.start + self.xid + self.user + self.data + self.end
|
|
|
|
|
|
|
|
def to_string(self):
|
2012-10-21 03:39:59 +00:00
|
|
|
return self.__str__()
|