ToorChat/libtoorchat.py

116 lines
3.4 KiB
Python
Raw Normal View History

2012-10-21 18:23:58 +00:00
import os
import httplib
from threading import Thread
2012-10-20 22:38:28 +00:00
2012-10-21 04:46:59 +00:00
USER_NAME_SIZE = 9
2012-10-21 18:23:58 +00:00
def get_web_site(visual, site):
''' This is a threaded message to query a website and create message to pass back '''
try:
connection = httplib.HTTPConnection(site)
connection.request("GET", "/")
result = connection.getresponse().read()
visual.message_quque.append(result)
except Exception:
pass
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-21 18:23:58 +00:00
def send_chat_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 '''
2012-10-21 18:23:58 +00:00
msg = ToorMessage(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
def change_channel(self, channel):
''' This is used to change the channel that the user operates in '''
self.device.setChannel(int(channel))
if channel != int
return None
else
return channel
def change_frequency(self, frequency):
''' This is used to change the frequency that the user operates in '''
self.device.setFreq(int(change_frequency))
if frequency != int
return None
else
return frequency
2012-10-21 18:23:58 +00:00
def send_web_request(self, site = ""):
'''This is used to attempt to get anyone who is registered as a server to load a website on your behalf '''
if site != "":
request = ToorMessage(site, None, ToorChatProtocol.get_web_request_type())
self.device.RFxmit(request.to_string())
2012-10-21 03:59:50 +00:00
@classmethod
def parse_message(cls, raw_message):
2012-10-21 18:23:58 +00:00
message = ToorMessage()
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]
2012-10-21 18:23:58 +00:00
message.type = raw_message[start_index + 4: start_index + 5]
message.xid = raw_message[start_index + 5: start_index + 13]
message.user = raw_message[start_index + 13: start_index + 22]
message.data = raw_message[start_index + 22: 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-21 18:23:58 +00:00
@classmethod
def get_web_messages(cls, site, visual):
'''This is query the webpage and make thoose messages '''
thread = Thread(target=get_web_site, args=(visual, site))
thread.start()
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"
2012-10-21 18:23:58 +00:00
@classmethod
def get_chat_type(cls):
return "1"
@classmethod
def get_web_request_type(cls):
return "2"
class ToorMessage():
2012-10-20 22:38:28 +00:00
''' This is a simple Message object wrapper to make things cleaner '''
2012-10-21 18:23:58 +00:00
def __init__(self, message = "", user = None, type=ToorChatProtocol.get_chat_type()):
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()
2012-10-21 18:23:58 +00:00
self.type = type
2012-10-20 22:38:28 +00:00
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):
2012-10-21 18:23:58 +00:00
return os.urandom(8)
2012-10-20 22:45:01 +00:00
2012-10-20 22:38:28 +00:00
def __str__(self):
2012-10-21 18:23:58 +00:00
return self.start +self.type+ self.xid + self.user + self.data + self.end
2012-10-20 22:38:28 +00:00
def to_string(self):
2012-10-21 18:23:58 +00:00
return self.__str__()