Beispiel #1
0
 def announcement(self, client, From):
     To = recvOnSocket(client)
     content = recvOnSocket(client)
     if To == '' or content == '':
         return False
     t = time.time()
     self.database.announcement(From, To, content, t)
     return True
Beispiel #2
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 #3
0
 def loadHistory(self, friend):
     size = recvOnSocket(self.parent.client)
     if size != '':
         lines = []
         for _ in range(int(size)):
             lines.append(recvOnSocket(self.parent.client))
         t = recvOnSocket(self.parent.client)
         win = self.parent.chatingwindow[friend]
         self.cache.append(('inserthistory', win, lines))
         self.cache.append(('appendtime', win, t))
         #win.text.insertHistory(lines, 1, 'history')
         #win.text.appendTime(t)
         return True
     else:
         return False
Beispiel #4
0
 def startChat(self, friend):
     client = self.parent.client
     avatar_me = recvOnSocket(client)
     avatar_you = recvOnSocket(client)
     t = recvOnSocket(client)
     if avatar_me == '' or avatar_you == '' or t == '':
         print('error')
         return False
     win = self.parent.chatingwindow[friend]
     win.text.avatar_me = wx.Bitmap(
         PyEmbeddedImage(avatar_me).GetImage().Rescale(32, 32))
     win.text.avatar_you = wx.Bitmap(
         PyEmbeddedImage(avatar_you).GetImage().Rescale(32, 32))
     self.cache.append(('appendtime', win, t))
     #win.text.appendTime(t)
     return True
Beispiel #5
0
 def changeAvatar(self, client, user):
     avatar = recvOnSocket(client)
     if avatar == '':
         return False
     self.database.changeAvatar(user, avatar)
     if not self.getAvatar(client, user):
         return False
     self.database.setOnline(user, True)
     return True
Beispiel #6
0
 def getAvatar(self):
     client = self.parent.client
     data = recvOnSocket(client)
     if data == '':
         return False
     self.avatar.SetBitmap(PyEmbeddedImage(data).GetBitmap())
     self.userlbl.SetLabel(self.user)
     self.Layout()
     return True
Beispiel #7
0
 def loadFriendList(self):
     client = self.parent.client
     number = recvOnSocket(client)
     self.friends = []
     self.contacts.clearFriends()
     if number == '':
         return False
     elif number != '0':
         number = int(number)
         for i in range(number):
             user = recvOnSocket(client)
             avatar = recvOnSocket(client)
             online = recvOnSocket(client)
             if user != '' and avatar != '':
                 self.contacts.addFriend(user, avatar, online)
                 self.friends.append(user)
             else:
                 return False
     return True
Beispiel #8
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 #9
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 #10
0
 def sendMsg(self, To, content):
     t = recvOnSocket(self.parent.client)
     if t == '':
         return False
     c = ''.join(('send', t, '::', content))
     #if To not in self.parent.chatingwindow.keys():
     #    win = None
     #else:
     #    win = self.parent.chatingwindow[To]
     #if not win:
     #    win = ChattingWin(self.parent, To)
     #    self.parent.chatingwindow.update({To:win})
     self.cache.append(('appendhistory', To, c))
     #win.text.appendHistory(c)
     return True
Beispiel #11
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 #12
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 #13
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 #14
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 #15
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