Beispiel #1
0
 def testHost(self):
     s = sk.socket()
     try:
         s.connect((self.HOST, self.POST))
     except:
         s.close()
         self.HOST = ''
         return False
     sendOnSocket(s, 'test')
     s.close()
     return True
Beispiel #2
0
 def testIP(self, ip):
     s = sk.socket()
     try:
         s.connect((ip, self.POST))
     except:
         s.close()
         return
     if self.HOST=='':
         self.HOST = ip
     sendOnSocket(s, 'test')
     s.close()
Beispiel #3
0
 def addFriend(self, client, user):
     friend = recvOnSocket(client)
     if friend == '':
         return False
     if self.database.checkAccount(friend) == 'user is available':
         self.database.addFriend(user, friend)
         if not sendOnSocket(client, 'OK'):
             return False
     else:
         if not sendOnSocket(client, 'Username is not available'):
             return False
     return True
Beispiel #4
0
 def startChat(self, client, user, history):
     friend = recvOnSocket(client)
     if friend not in history[0].keys():
         history[0].update({friend: time.time()})
         history[1].update({friend: time.time() - 86400})
     if friend == '':
         return False
     avatars = self.database.getAvatar(((user, ), (friend, )))
     for data in avatars:
         if not sendOnSocket(client, data[0]):
             return False
     if not sendOnSocket(client, time.ctime(history[0][friend])):
         return False
     return True
Beispiel #5
0
 def loadFriendList(self, client, user):
     data = self.database.getFriendList(user)
     l = len(data)
     if not sendOnSocket(client, str(l)):
         return False
     if l != 0:
         avatar = self.database.getAvatar(data)
         online = self.database.getOnline(data)
         for i in range(l):
             if not sendOnSocket(client, data[i][0]):
                 return False
             if not sendOnSocket(client, avatar[i][0]):
                 return False
             if not sendOnSocket(client, online[i][0]):
                 return False
     return True
Beispiel #6
0
 def connectToServer(self, var):
     self.client = sk.socket()
     try:
         self.client.connect((self.setting.HOST, self.setting.POST))
     except:
         self.client.close()
         return 'can not connect to server'
     sendOnSocket(self.client, 'connect')
     for string in var:
         if not sendOnSocket(self.client, string):
             return 'can not connect to server'
     data = recvOnSocket(self.client)
     if data == '':
         return 'can not connect to server'
     else:
         return data
Beispiel #7
0
 def loadHistory(self, client, user, history):
     friend = recvOnSocket(client)
     if friend == '':
         return False
     data = self.database.loadHistory(user, friend, history[0][friend],
                                      history[1][friend])
     history[0][friend] -= 86400
     history[1][friend] -= 86400
     l = len(data)
     if not sendOnSocket(client, str(l)):
         return False
     for line in data:
         if not sendOnSocket(client, line):
             return False
     if not sendOnSocket(client, time.ctime(history[0][friend])):
         return False
     return True
Beispiel #8
0
 def sendMsg(self, client, From):
     To = recvOnSocket(client)
     content = recvOnSocket(client)
     if To == '' or content == '':
         return False
     t = time.time()
     self.database.sendMsg(From, To, content, t)
     if not sendOnSocket(client, time.ctime(t)):
         return False
     return True
Beispiel #9
0
 def check(self):
     client = self.parent.client
     if not sendOnSocket(client, 'check'):
         return False
     data = recvOnSocket(client)
     if data == '':
         return False
     elif data == 'addfriend':
         user = recvOnSocket(client)
         if user == '':
             return False
         if wx.MessageBox(
                 "%s would like to become your friend.\nDo you agree?" %
             (user, ),
                 'Add Friend',
                 style=wx.YES_NO) == wx.YES:
             self.parent.cache.append(('addfriend', user))
     elif data == 'online':
         user = recvOnSocket(client)
         online = recvOnSocket(client)
         avatar = recvOnSocket(client)
         if user == '' or online == '' or avatar == '':
             return False
         self.contacts.updateFriend(user, avatar, online)
     elif data == 'newmsg':
         From = recvOnSocket(client)
         date = recvOnSocket(client)
         content = recvOnSocket(client)
         if From == '' or date == '' or content == '':
             return False
         c = ''.join(('recv', date, '::', content))
         #if From not in self.parent.chatingwindow.keys():
         #    win = None
         #else:
         #    win = self.parent.chatingwindow[From]
         #if not win:
         #    win = ChattingWin(self.parent, From)
         #    self.parent.chatingwindow.update({From:win})
         self.cache.append(('appendhistory', From, c))
         #win.text.appendHistory(c)
     elif data == 'announcement':
         From = recvOnSocket(client)
         date = recvOnSocket(client)
         content = recvOnSocket(client)
         if From == '' or date == '' or content == '':
             return False
         self.cache.append(('announcement', From, date, content))
     return True
Beispiel #10
0
 def openConnection(self, event=0):
     if self.lock:
         return
     self.lock = True
     print(self.user, 'in', time.time())
     if len(self.parent.cache) == 0:
         if not self.check():
             return self.disconnect()
     else:
         for data in self.parent.cache[0]:
             if not sendOnSocket(self.parent.client, data):
                 return self.disconnect()
         if self.parent.cache[0][0] == 'avatar':
             if not self.getAvatar():
                 return self.disconnect()
         elif self.parent.cache[0][0] == 'loadfriendlist':
             if not self.loadFriendList():
                 return self.disconnect()
         elif self.parent.cache[0][0] == 'addfriend':
             data = recvOnSocket(self.parent.client)
             if data == 'OK':
                 self.parent.cache.append(('loadfriendlist', ))
             else:
                 wx.MessageBox(data, 'Error')
         elif self.parent.cache[0][0] == 'changeavatar':
             if not self.getAvatar():
                 return self.disconnect()
         elif self.parent.cache[0][0] == 'newmsg':
             if not self.sendMsg(self.parent.cache[0][1],
                                 self.parent.cache[0][2]):
                 return self.disconnect()
         elif self.parent.cache[0][0] == 'loadhistory':
             if not self.loadHistory(self.parent.cache[0][1]):
                 return self.disconnect()
         elif self.parent.cache[0][0] == 'startchat':
             if not self.startChat(self.parent.cache[0][1]):
                 return self.disconnect()
         self.parent.cache.pop(0)
     self.lock = False
     print(self.user, 'out', time.time())
Beispiel #11
0
 def openConnection(self, client, addr):
     self.statusbar.SetStatusText('%s connection(s) currently' %
                                  len(self.clients))
     if recvOnSocket(client) != 'test':
         online = False
         status = recvOnSocket(client)
         user = recvOnSocket(client)
         pasw = recvOnSocket(client)
         if user != '' and pasw != '':
             if status == 'register':
                 sex = recvOnSocket(client)
                 check = self.database.checkAccount(user)
                 if check == 'OK':
                     self.database.addAccount(user, pasw, sex)
             elif status == 'log in':
                 check = self.database.checkAccount(user, pasw)
                 if check == 'OK':
                     self.text.Append((addr[0], addr[1], user))
                     online = True
             if not sendOnSocket(client, check):
                 online = False
         if online:
             self.database.setOnline(user, True)
             history = [{}, {}]  #0.after 1.before
         while online and self.notexitted:
             #print(user, check)
             data = recvOnSocket(client)
             #if data!='check':
             print(user + ': ' + data, time.time())
             if data == '':
                 break
             elif data == 'check':
                 if not self.check(client, user, history):
                     break
             elif data == 'avatar':
                 if not self.getAvatar(client, user):
                     break
             elif data == 'loadfriendlist':
                 if not self.loadFriendList(client, user):
                     break
             elif data == 'addfriend':
                 if not self.addFriend(client, user):
                     break
             elif data == 'changeavatar':
                 if not self.changeAvatar(client, user):
                     break
             elif data == 'newmsg':
                 if not self.sendMsg(client, user):
                     break
             elif data == 'loadhistory':
                 if not self.loadHistory(client, user, history):
                     break
             elif data == 'startchat':
                 if not self.startChat(client, user, history):
                     break
             elif data == 'announcement':
                 if not self.announcement(client, user):
                     break
         if online:
             self.database.setOnline(user, False)
         for item in range(self.text.GetItemCount()):
             if (self.text.GetItemText(item, 0) == addr[0]
                     and self.text.GetItemText(item, 1) == str(addr[1])
                     and self.text.GetItemText(item, 2) == user):
                 self.text.DeleteItem(item)
                 break
     client.close()
     self.clients.remove(client)
     self.statusbar.SetStatusText('%s connection(s) currently' %
                                  len(self.clients))
Beispiel #12
0
 def getAvatar(self, client, user):
     if not sendOnSocket(client, self.database.getAvatar([(user, )])[0][0]):
         return False
     return True
Beispiel #13
0
 def check(self, client, user, history):
     data = self.database.checkNewMsg(user)
     #print(user+': ', data)
     if data:
         if not sendOnSocket(client, data[0]):
             return False
         if data[0] == 'addfriend':
             if not sendOnSocket(client, data[1]):
                 return False
         elif data[0] == 'online':
             if not sendOnSocket(client, data[1]):
                 return False
             if not sendOnSocket(client, data[3]):
                 return False
             if not sendOnSocket(
                     client,
                     self.database.getAvatar([(data[1], )])[0][0]):
                 return False
         elif data[0] == 'newmsg':
             if data[1] not in history[0].keys():
                 history[0].update({data[1]: data[2]})
                 history[1].update({data[1]: data[2] - 86400})
             if not sendOnSocket(client, data[1]):
                 return False
             if not sendOnSocket(client, time.ctime(data[2])):
                 return False
             if not sendOnSocket(client, data[3]):
                 return False
         elif data[0] == 'announcement':
             if not sendOnSocket(client, data[1]):
                 return False
             if not sendOnSocket(client, time.ctime(data[2])):
                 return False
             if not sendOnSocket(client, data[3]):
                 return False
     else:
         if not sendOnSocket(client, 'NO'):
             return False
     return True