def initFromUI(self): data = Messages.Message() data.setMessage(0, self.txtItem1.GetLabel().encode('ascii')) data.setMessage(1, self.txtItem2.GetLabel().encode('ascii')) data.setMessage(2, self.txtItem3.GetLabel().encode('ascii')) data.setMessage(3, self.txtItem4.GetLabel().encode('ascii')) self.messages.setData(data)
def create_message(self, i): # creating object of type Message msg = Messages.Message(self, i, 0) # sending message after pre-calculated interarrival time has passed t = threading.Timer(msg.interarrival_time, self.send_message, args=( msg, i, )) t.start()
def create_dummy_message(self, i): # same logic as above msg = Messages.Message(self, i, 1) # sending of a message after the precaculated (exp. distributed) arrival time has passed t = threading.Timer(msg.interarrival_time, self.send_message, args=( msg, i, )) t.start()
def add_message(): session = bottle.request.environ.get('beaker.session') #@UndefinedVariable form = bottle.request.forms required = ['username', 'message', 'topic'] for r in required: if not r in form or len(form.get(r)) == 0: session['alert'] = 'Failed to add Topic. Missing ' + r bottle.redirect('/') topic = Topics.get(id=int(form.get('topic'))) if topic: message = Messages.Message(username=form.get('username'), message=form.get('message'), reply_to=form.get('reply_to', None), topic=topic.id) if message.save(): session['alert'] = 'Successfully added Reply' bottle.redirect('/topic/' + str(topic.id)) session['alert'] = 'Failed to add Message' bottle.redirect('/')
except: pass for i in range(0, len(conn)): # Close connection if conn[i].end == True: conn[i].socket.shutdown(socket.SHUT_RDWR) conn[i].socket.close() conn[i].active = False conn[i].join() del conn[i] break try: #wait for messages data = conn[i].data_list.pop(0) mes = Messages.Message() #Then process them #DAT is forwarded instantly to everybody if data[0:3] == "DAT".encode('utf-8'): for j in range(len(conn)): if i != j: conn[j].socket.sendall(data) #Key is forwarded to the last connector elif data[0:3] == "KEY".encode('utf-8'): conn[len(conn) - 1].socket.sendall(data) else: mes.process_message(data, aes_key, sign_keys) # Process INI messages if mes.msgtype == "INI":
def print_group_msg(msg): ''' 群聊信息处理 :param msg: :return: ''' print("Group") print(msg) try: dict_msg = msg.raw # message对象字典 menber = dict_msg['ActualNickName'] # 发言人 group_name = msg.chat.name # 群名称 times = time.localtime(dict_msg['CreateTime']) # 发言时间戳 dt = "%s-%s-%s %s:%s:%s" % (times[0], times[1], times[2], times[3], times[4], times[5]) # 格式化时间 print dt filename = dict_msg['FileName'] # 发送文件名称 group = groupDao.findGroup(group_name) # 获取当前群信息 主要是group_id group_id = 0 if group == None: # 群在数据库中不存在,则添加新的群信息 newgroup = Groups.Group() newgroup.group_name = group_name newgroup.number = len(msg.chat) print groupDao.addGroup(newgroup) group_id = groupDao.findGroup(group_name)[0] else: group_id = group[0] except Exception as e: print e.message message = Messages.Message() message.group_id = group_id message.member = menber message.create_time = dt try: if msg.type == TEXT: # 发言保存到数据库 text = dict_msg['Content'] # 发言内容 message.text = text messageDao.addMessage(message) if u"@%s" % bot.self.name in msg.text: # @机器人信息 text_key = msg.text[len(u"@%s" % bot.self.name):] print text_key deal_reply(msg, keywords=text_key) # url_1 = ' http://test2.bicoin.info/robot/autoReply?keyword=%s'% text # r = requests.get(url_1) # dict_result = r.json() # print dict_result # if dict_result['code'] and dict_result['type'] == "2": # return dict_result['data'] else: # 普通信息 deal_reply(msg) if msg.type == NOTE: group = msg.chat group.update_group(True) print len(group) # bot.groups()[0].update_group(True) if msg.type == RECORDING: # 记录录音 if not os.path.exists("recording"): os.mkdir("recording") filename = u"recording/%s" % filename msg.get_file(filename) # filename 存储路径 duration = int(math.ceil(dict['VoiceLength'] / 1000)) message.recording = filename message.duration = duration messageDao.addMessage(message) if msg.type == PICTURE: # 存储图片 if not os.path.exists("image"): os.mkdir("image") filename = u"image/%s" % filename msg.get_file(filename) # filename 存储路径 message.picture = filename messageDao.addMessage(message) if msg.type == ATTACHMENT: # 存储文件 if not os.path.exists("file"): os.mkdir("file") filename = u"file/%s" % filename msg.get_file(filename) # filename 存储路径 if msg.type == VIDEO: # 存储视频 if not os.path.exists("video"): os.mkdir("video") filename = u"video/%s" % filename msg.get_file(filename) # filename 存储路径 if msg.type == SHARING: print dict_msg['Url'] except Exception as e: print e, e.message pass
def main(): print("Welcome to the most secure multi chat application!") name = "" while len(name) != 5: name = input("Please enter your name(exactly 5 character): ") # ip = input("Please enter the IP address of the secure server: ") # port = input("Please enter the port number of the secure server: ") ip = "localhost" port = "8080" sender = name # import own keys try: rsa_key = RSA.import_key(sender + "_rsa_key.pem") sign_key = RSA.import_key(sender + "_sign_key.pem") except: #only for debug, and the program dont crash, cannot decode anything with theese keys rsa_key = RSA.import_key("keypair.pem") sign_key = RSA.import_key("keypair.pem") #Make socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) print("Connected to server\n") #initialize the Connection init = Messages.create_INI_message(rsa_key.publickey(), sign_key, sender) s.sendall(init) #Start user input monitoring helper input_thread = Threads.input_reader(True) input_thread.start() #wait for the response #If first client, the the server answer with init #If not, then another client will send the key while True: data = s.recv(4096) mes = Messages.Message() mes.process_message(data, None, sign_keys) if mes.msgtype == "INI" and mes.sender == "Serve": aes_key = AES.generate_key() break elif mes.msgtype == "KEY": aes_key = mes.msg break print("Init Done\n") #Start socket monitoring thread data_thread = Threads.data_reader(s, True) data_thread.start() while True: try: #Waiting for messages data = data_thread.data_list.pop(0) mes = Messages.Message() #and process them mes.process_message(data, aes_key, sign_keys) if mes.msgtype == "INI": #If INI, then we send the key data = Messages.create_KEY_message(aes_key, Crypto.PublicKey.RSA.import_key(mes.msg.encode('utf-8')), sign_key, sender) s.sendall(data) #And broadcast, that there is a new participant data = Messages.create_DAT_message(mes.sender + " connected.", aes_key, sign_key, sender) print(mes.sender + " connected.") s.sendall(data) #If DAT, then we write the message to the console if mes.msgtype == "DAT": print(mes.sender + ": " + mes.msg) #If key, then we use it as AES key if mes.msgtype == "KEY": aes_key = mes.msg except: pass try: msg = input_thread.input_list.pop(0) #END_CONN is the quit message if msg == "END_CONN": print("Connection is closed.\n") break #Send out the user input data = Messages.create_DAT_message(msg, aes_key, sign_key, sender) s.sendall(data) except: pass if data_thread.end == True: print("Server closed connection.\n") break #Terminate threads input_thread.active = False data_thread.active = False input_thread.join() data_thread.join() #Terminate socket s.shutdown(socket.SHUT_RDWR) s.close() return
def search(self, ): flag = 0 counter = 1 name = input('Enter The Contact Name :') for x in self.AllContacts: flag = x.Name.find(name) if (flag) > -1: print(counter, ':') print('Name :', x.Name) print('Number :', x.Number) print() break counter = counter + 1 selection = input('Enter Contact Selection Number : ') selection = int(selection) selection = selection - 1 print('1. To Call Contact') print('2. To Message Contact') print('3. To Edit Contact') print('4. To Delete Contact') choice = input('Enter Choice : ') if (choice == '1'): number = self.AllContacts[int(selection)].Number file = open('action.txt', 'w') file.writelines('dialNumber' + '\n') file.writelines(number) file.close() ftp.connect('192.168.43.1', 2121) ftp.login(user='******', passwd='password') ftp.cwd('/storage/emulated/0/com.hipipal.qpyplus/project/') ftp.storbinary('STOR action.txt', open('action.txt', 'rb')) elif (choice == '2'): number = self.AllContacts[int(selection)].Number obj = Messages.Message() container = Messages.MessageObjects() obj.setRecipient(number) message = input('Enter Message Contents: ') obj.setMessage(message) status = input( 'Enter "send" to send and "save" to save in drafts and "delete" to delete: ' ) obj.setState(status) obj.setTime() if (obj.getState() == 'send'): file = open('action.txt', 'w') file.writelines('smsSend' + '\n') file.writelines(obj.recipient + '\n') file.writelines(obj.message) file.close() ftp.connect('192.168.43.1', 2121) ftp.login(user='******', passwd='password') ftp.cwd('/storage/emulated/0/com.hipipal.qpyplus/project/') ftp.storbinary('STOR action.txt', open('action.txt', 'rb')) ftp.close() container.SentObjects.append(obj) print('Message Sent!') elif (obj.getState() == 'save'): self.DraftObjects.append(obj) elif (choice == '3'): pass elif (choice == '4'): pass else: print('Wrong Choice') if (flag == '0'): print('No Contact Found') '''