def sendFile(self, filename): """This function calls target's receiveFile function and then determine message receipt depending on response""" global chat_target, error_sending, log_in_name, seen response = self.pingReceiver(chat_target) if response != 0: # Ping error_sending = 1 # Create the file in static directory file = filename.file.read() create = open('files/' + filename.filename, 'wb') create.write(file) create.close() # Check file size size = os.stat('files/' + filename.filename) if size.st_size > 5000000: # Stop the sending process if the file size exceeds 5MB error_sending = 2 raise cherrypy.HTTPRedirect("/form?name=" + chat_target) # Encode file into base64 data = base64.b64encode(file) stamp = time.time() output_dict = { "sender": log_in_name, "destination": chat_target, "file": data, "filename": filename.filename, "content_type": str(filename.content_type), "stamp": stamp } json_data = json.dumps(output_dict) ip = databaseControl.fetchIP(chat_target) port = databaseControl.fetchPort(chat_target) link = "http://" + str(ip) + ":" + str(port) + "/receiveFile" header = {'Content-Type': 'application/json'} req = urllib2.Request(link, json_data, header) try: res = urllib2.urlopen( req) # Prevent target for not having receiveFile except: error_sending = 1 raise cherrypy.HTTPRedirect("/form?name=" + log_in_name) read = res.read() if read == '0': # Sent Successfully error_sending = 3 databaseControl.storeMessages(log_in_name, "/static/files/" + filename.filename, chat_target, stamp) seen = 'Unseen' elif read == '11': # Rate Limited error_sending = 4 else: error_sending = 1 raise cherrypy.HTTPRedirect("/form?name=" + chat_target)
def pingReceiver(self, name): """This function calls target's ping function to determine their online status""" global log_in_name ip = databaseControl.fetchIP(name) if ip == '1': # No ip address can be fetched return 1 port = databaseControl.fetchPort(name) link = ("http://" + str(ip) + ":" + str(port) + "/ping?sender=" + log_in_name) try: result = urllib2.urlopen(link, timeout=3) # Send ping except: return 1 return 0
def send(self, message=None): """This function calls target's receiveMessage function and read response to determine message status""" Page = "sending..." global error_sending, chat_target, log_in_name, seen if not message.strip(): # Prevent empty message from being sent raise cherrypy.HTTPRedirect("/form?name=" + chat_target) res = self.pingReceiver(chat_target) if res != 0: # Error sending error_sending = 1 raise cherrypy.HTTPRedirect("/form?name=" + chat_target) ip = databaseControl.fetchIP(chat_target) port = databaseControl.fetchPort(chat_target) stamp = time.time() output_dict = { "sender": log_in_name, "message": message, "destination": chat_target, "stamp": stamp } data = json.dumps(output_dict) link = ("http://" + str(ip) + ":" + str(port) + "/receiveMessage") header = {'Content-Type': 'application/json'} req = urllib2.Request(link, data, header) try: # Prevent target has no receiveMessage function response = urllib2.urlopen(req, timeout=3) except: error_sending = 1 raise cherrypy.HTTPRedirect("/form?name=" + chat_target) read = response.read() if read == '0': # Successfully sent error_sending = 3 databaseControl.storeMessages(log_in_name, message, chat_target, stamp) seen = 'Unseen' raise cherrypy.HTTPRedirect("/form?name=" + chat_target) elif read == '11': # Spamming detected error_sending = 4 raise cherrypy.HTTPRedirect("/form?name=" + chat_target) else: # Database error, Timeout error or other errors detected error_sending = 1 raise cherrypy.HTTPRedirect("/form?name=" + chat_target)
def sentReceipt(self): """This function calls target's acknowledge function to notify that last message is seen.""" global chat_target, log_in_name, seen ping = self.pingReceiver(chat_target) if ping == 0: time = databaseControl.fetchTime(chat_target) if time != '1': message = databaseControl.fetchLastMessage( chat_target, log_in_name) hash = 3 if message.find( "/static/", 0) != -1: # If it's a file, hash the content instead message = message[8:] file = open(message, 'rb') read = file.read() message = base64.b64encode(read) hashedMessage = hashlib.sha512(message + log_in_name).hexdigest() output_dict = { 'stamp': time, 'sender': log_in_name, 'hashing': hash, 'destination': chat_target, 'hash': hashedMessage } data = json.dumps(output_dict) header = {'Content-Type': 'application/json'} ip = databaseControl.fetchIP(chat_target) port = databaseControl.fetchPort(chat_target) link = "http://" + str(ip) + ":" + str(port) + "/acknowledge" req = urllib2.Request(link, data, header) try: res = urllib2.urlopen(req, timeout=1) except: pass else: pass
def collectProfile(self): """Calls target's getProfile function and store it into the database.""" global chat_target, log_in_name ping = self.pingReceiver(chat_target) if ping == 0: # Ping output_dict = { "sender": log_in_name, "profile_username": chat_target } data = json.dumps(output_dict) ip = databaseControl.fetchIP(chat_target) port = databaseControl.fetchPort(chat_target) link = "http://" + str(ip) + ":" + str(port) + "/getProfile" header = {'Content-Type': 'application/json'} req = urllib2.Request(link, data, header) try: response = urllib2.urlopen( req, timeout=3) # Prevent target from not having getProfile try: result = json.loads(response.read( )) # Prevent target from returning non json object except: return '1' except: return '1' if not isinstance(result, dict) == False: picture = result['picture'] if result['fullname'] == None or len(result['fullname']) == 0: result['fullname'] = "N/A" else: result['fullname'] = self.injectionDefence( result['fullname']) if result['position'] == None or len(result['position']) == 0: result['position'] = "N/A" else: result['position'] = self.injectionDefence( result['position']) if result['description'] == None or len( result['description']) == 0: result['description'] = "N/A" else: result['description'] = self.injectionDefence( result['description']) if result['location'] == None or len(result['location']) == 0: result['location'] = "N/A" else: result['location'] = self.injectionDefence( result['location']) if picture == None: result['picture'] = "N/A" elif picture.find("http://", 0) == 0 or picture.find( "https://", 0) == 0: path = "files/" + chat_target + "_profile_pic.jpg" try: # Download target's profile picture and store it in static directory urllib.urlretrieve(result['picture'], path) except: pass # Ignore databaseControl.storeProfile( chat_target, result['fullname'], result['position'], result['description'], result['location'], result['picture'], result['lastUpdated']) else: picture = "N/A" else: return '1' return result return '1'