Conflicts:
	libtoorchat.py
	toorchat.py
master
Pickles 2012-10-21 12:32:04 -07:00
commit cf72a8c6f6
2 changed files with 71 additions and 17 deletions

View File

@ -1,6 +1,21 @@
import os
import httplib
from threading import Thread
USER_NAME_SIZE = 9
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
class ToorChatProtocol():
''' This is a class to allow for easy of use with anything to do with messaging '''
def __init__(self, device):
@ -8,9 +23,9 @@ class ToorChatProtocol():
self.PACKET_END = "\xFF\xBE\xEF\xFF"
self.device = device
def send_message(self, message = "", user = None):
def send_chat_message(self, message = "", user = None):
''' This is used to send a simple message over the toorchat protocol '''
msg = ToorChatMessage(message, user)
msg = ToorMessage(message, user)
self.device.RFxmit(msg.to_string())
return msg
@ -29,21 +44,34 @@ class ToorChatProtocol():
return None
else
return frequency
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())
@classmethod
def parse_message(cls, raw_message):
message = ToorChatMessage()
message = ToorMessage()
message.raw = raw_message
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]
message.user = raw_message[start_index + 12: start_index + 21]
message.data = raw_message[start_index + 21: end_index]
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]
message.end = raw_message[end_index: end_index+4]
return message
@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()
@classmethod
def get_packet_start(cls):
return "\xFF\xDE\xAD\xFF"
@ -52,13 +80,22 @@ class ToorChatProtocol():
def get_packet_end(cls):
return "\xFF\xBE\xEF\xFF"
class ToorChatMessage():
@classmethod
def get_chat_type(cls):
return "1"
@classmethod
def get_web_request_type(cls):
return "2"
class ToorMessage():
''' This is a simple Message object wrapper to make things cleaner '''
def __init__(self, message = "", user = None):
def __init__(self, message = "", user = None, type=ToorChatProtocol.get_chat_type()):
self.raw = None
self.start = ToorChatProtocol.get_packet_start()
self.xid = self.get_random_xid()
self.type = type
if user != None:
self.user = user
if len(self.user) < USER_NAME_SIZE:
@ -69,10 +106,10 @@ class ToorChatMessage():
self.end = ToorChatProtocol.get_packet_end()
def get_random_xid(self):
return "44444444"
return os.urandom(8)
def __str__(self):
return self.start + self.xid + self.user + self.data + self.end
return self.start +self.type+ self.xid + self.user + self.data + self.end
def to_string(self):
return self.__str__()
return self.__str__()

View File

@ -18,7 +18,13 @@ def thread_run(visual):
msg, timestamp = visual.badge.RFrecv()
toor_message = ToorChatProtocol.parse_message(msg)
if toor_message != None:
visual.message_queue.append(toor_message)
if toor_message.type == ToorChatProtocol.get_chat_type():
visual.message_queue.append(toor_message)
if toor_message.type == ToorChatProtocol.get_web_request_type():
#If we are registered as a server, lets typ to make that request
if visual.server:
ToorChatProtocol.get_web_messages(toor_message.data, visual)
except ChipconUsbTimeoutException:
pass
@ -26,8 +32,6 @@ class Visualizer():
def __init__(self):
self.screen = curses.initscr()
# curses.noecho()
# curses.cbreak()
self.screen.nodelay(1)
self.badge = RfCat(idx=0)
self.badge.setModeRX()
@ -38,6 +42,10 @@ class Visualizer():
self.frequency = None
#This when set to True will kill the thread
self.exit = False
self.server = False
def set_server(self):
self.server = True
def start(self):
self.__start_recv_thread__()
@ -56,7 +64,7 @@ class Visualizer():
self.last_message_index = 0
while True:
self.screen_max_y, self.screen_max_x = self.screen.getmaxyx()
self.screen.addstr(0, 1, "[S] Send Message [U] Set User Name [C] Set Channel [F] Set Frequency")
self.screen.addstr(0, 1, "[S] Send Message [U] Set User Name [C] Set Channel [F] Set Frequency [W] Load Webpage")
self.__add_message_to_screen__()
entry = self.screen.getch()
if entry == curses.KEY_RESIZE:
@ -64,7 +72,7 @@ class Visualizer():
if entry == ord('s'):
self.screen.nodelay(0)
user_input = self.screen.getstr(1, 1, 60)
old_message = self.protocol.send_message(user_input, self.user)
old_message = self.protocol.send_chat_message(user_input, self.user)
self.message_queue.append(old_message)
self.screen.nodelay(1)
self.screen.addstr(1,1," "*(self.screen_max_x-3))
@ -84,6 +92,10 @@ class Visualizer():
self.screen.nodelay(0)
user_input = self.screen.getstr(1, 1, 60)
self.frequency = self.protocol.change_frequency(user_input)
if entry == ord('w'):
self.screen.nodelay(0)
user_input = self.screen.getstr(1, 1, 60)
self.protocol.send_web_request(user_input)
self.screen.nodelay(1)
self.screen.addstr(1,1," "*(self.screen_max_x-3))
@ -113,4 +125,9 @@ class Visualizer():
if __name__ == '__main__':
visual = Visualizer()
visual.start()
if len(sys.argv) > 1:
if sys.argv[1] == "-s":
visual.set_server()
visual.start()