def __readDir(self): self.__msg_resp.new_msg("We handle one-to-one chats only, so we are looking for same name of yours in the files, if you have different names the results won't be as expected.") for friend, filepath in self.__friendfilemap.items(): data = elementsOf(filepath, self.__msg_resp) names_appeared_inChat=set([d['Author'] for d in data]) self.__clientName, status = self.__validate_names(friend, names_appeared_inChat) if status: self.__data[friend] = data for friend, data in self.__data.items(): friends=list(set([d['Author'] for d in data])) if friend in friends: friends.remove(friend) if len(friends): if self.__clientName != friends[0]: self.__msg_resp.new_msg("Sorry, You have sent file with incorrect filename.") log.info("Rejected incorrect filename") self.__data.pop(friend) for data in self.__data.values(): self.__friends+=list(set([d['Author'] for d in data])) if self.__clientName in self.__friends: self.__friends.remove(self.__clientName)
def __IMAPlogin(self): self.__mail = imaplib.IMAP4_SSL(self.IMAP4_SERVER, self.IMAP4_PORT) self.__mail.login( super(MailReader, self).USER, super(MailReader, self).PASSWORD) log.info("Logged into IMAP4 server successfully")
def checkNewDir(): newentry = True TIMEOUT = DIRECTORYCHECKINGTIMEOUT attachmentdir = '../data/attachments' pastdirs = set(listdir(attachmentdir)) while True: if not newentry: sleep(TIMEOUT) # print('...') newentry = False newdirs = set(listdir(attachmentdir)) for newdir in newdirs: if newdir not in pastdirs: newentry = True log.info('New entry in ' + attachmentdir + ': ' + newdir) while (not (isfile(pathjoin(attachmentdir, newdir, 'ends')))): log.info("'ends' file not found in " + pathjoin(attachmentdir, newdir)) start_new_thread(startAnalyzer, (newdir, )) pastdirs = newdirs
def __SMTPlogin(self): self.__session = smtplib.SMTP(self.SMTP_SERVER, self.SMTP_PORT) self.__session.starttls() self.__session.login( super(MailSender, self).USER, super(MailSender, self).PASSWORD) log.info("Logged into SMTP server successfully")
def __leave_trace(self, sender): endAckFilepath = pathjoin(self.__dirpath, 'ends') try: open(endAckFilepath, 'w+') except FileNotFoundError: log.info("No suitable attachment found, dummy file not created") else: log.info("Dummy file for completion acknowledgement created")
def __storeIntoFile(self, filename, content): if not isdir(self.__dirpath): makedirs(self.__dirpath) filepath = pathjoin(self.__dirpath, filename) with open(filepath, 'wb+') as outfile: outfile.write(encryption.encryptBytes(content)) log.info("Contents written in " + filepath)
def __init__(self, dirpath): self.__clientName = 'You' self.__friends = [] self.__dir = dirpath log.info("Reading " + self.__dir) self.__data = {} self.__msg_resp=Msg() self.__getFiles() self.__readDir()
def __validate_names(self, friend, names_appeared_inChat): names_inChat=list(names_appeared_inChat) if friend in names_inChat: names_inChat.remove(friend) if len(names_inChat)>1 : self.__msg_resp.new_msg("Sorry, You have sent a group chat or your filename doesn't have your friend name.") log.info("Rejected group chat or incorrect filename") return self.__clientName, 0 if len(names_inChat) == 0: return self.__clientName, 1 return names_inChat[0], 1
def constrMail(self, recipient, addr, attachment_dir, fileExtension, msg): message = self.init_msg(recipient, addr, msg) if attachment_dir: if not (isdir(attachment_dir)): log.error(attachment_dir + "doesn't exist") return message attach_files = [(filename, pathjoin(attachment_dir, filename)) for filename in listdir(attachment_dir) if filename.endswith(fileExtension)] for attach_file_name, filepath in attach_files: data = open(filepath, 'rb').read() attachment = MIMEImage(data, name=attach_file_name) message.attach(attachment) log.info(filepath + " has been attached") return message
def startAnalyzer(newdir): global semaphore semaphore.acquire() start = time.time() print( "\n\n\n--------------------------Total analyzers running: {}-----------------------------\n\n\n" .format(MAXANALYZERTHREADS - semaphore._value)) proc = multiprocessing.Process(target=analyze, args=(newdir, )) proc.start() proc.join() end = time.time() log.info("Time consumed: {} seconds".format(end - start)) print( "\n\n\n--------------------------Total analyzers running: {}-----------------------------\n\n\n" .format(MAXANALYZERTHREADS - semaphore._value - 1)) semaphore.release()
def __extractTXT(self, msg): if msg.is_multipart(): self.__timestampForThisMsg = curtime() self.__dirpath = pathjoin( '../data/attachments', self.__timestampForThisMsg + '_' + msg['From'].replace('/', ':')) for part in msg.walk(): ctype, filename = part.get_content_type(), part.get_filename() if (ctype == 'text/plain') and not (filename == None): log.info("Got a file attachment: " + filename) filename, encoding = decode_header(filename)[0] if encoding: filename = filename.decode(encoding) if (filename.endswith('.txt') and filename.startswith('WhatsApp Chat with ')): log.info( "The file attachment is as per naming conventions of WhatsApp chat exports" ) chatFileWith = filename[19:] filename = (curtime() + '_with_' + chatFileWith).replace('/', ':') self.__storeIntoFile(filename, part.get_payload(decode=True)) else: log.info( "The file was rejected for not matching with naming conventions of WhatsApp chat exports" ) self.__leave_trace(msg["From"])
def readmail(self): self.__mail.select('inbox') _, data = self.__mail.search(None, '(UNSEEN)') mail_ids = data[0].split() if len(mail_ids) == 0: # print('...') sleep(self.TIMEOUT) return else: log.info("Unread messages in Inbox") for id in mail_ids: _, data = self.__mail.fetch(id, '(BODY.PEEK[])') curmsg = email.message_from_string(data[0][1].decode('utf-8')) log.info("Recived message from " + curmsg["From"] + "with sub: " + curmsg["Subject"] + "\nMessage ID: " + id.decode('utf-8')) self.__extractTXT(curmsg) self.__mail.store(id, '+FLAGS', '(\\SEEN)') return
def sendmail(self, recipient, fullAddr, attachment_dir=None, fileExtension='.png', msg=[]): addr = extractAddr(fullAddr) log.info("Mailid extracted: " + addr + " for recipient " + recipient) msg = self.constrMail(recipient, addr, attachment_dir, fileExtension, msg) text = msg.as_string() log.debug("Constructed message: " + text) log.info("Sending to " + addr + ".....") self.__session.sendmail(self.USER, addr, text) log.info("Message has been sent to " + addr + " successfully") self.__session.quit()
def analyze(dirpath): analyzer = Analyst(dirpath) analyzer.start() log.info("Ended")