import socket
import select
import sys
import tkinter as tk
#from tkinter import *
import tkinter.messagebox
from emoji import emojize
#from PIL import Image,ImageTk
import PIL.Image
import PIL.ImageTk
from emoji_page import *
from tkinter import filedialog
from LoginPage import *
from PrivateChatPage import *
import _thread
import time
import os
import method
HOST = "127.0.0.1"
PORT = 65435
User = None
user2room = {}
#user2room_history = {}
groups = []
def send_emoji_1():
emoji = "[emoji-happy]"
header = str(SEND_EMOJI) + "" + "\r\n" + str(len(emoji))
msg = emoji.encode("utf-8")
try:
method.send(sock, header, msg)
except Exception as e:
messagebox.showerror('Error', 'Send emoji error')
print('error,', e)
print("got this emoji", emoji)
show_msg('<Me>', emoji, 'blue', is_file=False, msg_time=None)
def send_emoji_2():
emoji = '[emoji-HappyWithHalo]'
header = str(SEND_EMOJI) + "" + "\r\n" + str(len(emoji))
msg = emoji.encode("utf-8")
try:
method.send(sock, header, msg)
except Exception as e:
messagebox.showerror('Error', 'Send emoji error')
print('error,', e)
print("got this emoji", emoji)
show_msg('<Me>', emoji, 'blue', is_file=False, msg_time=None)
def send_emoji_3():
emoji = '[emoji-cake]'
header = str(SEND_EMOJI) + "" + "\r\n" + str(len(emoji))
msg = emoji.encode("utf-8")
try:
method.send(sock, header, msg)
except Exception as e:
messagebox.showerror('Error', 'Send emoji error')
print('error,', e)
print("got this emoji", emoji)
show_msg('<Me>', emoji, 'blue', is_file=False, msg_time=None)
def send_emoji_4():
emoji = "[emoji-hotpot]"
header = str(SEND_EMOJI) + "" + "\r\n" + str(len(emoji))
msg = emoji.encode("utf-8")
try:
method.send(sock, header, msg)
except Exception as e:
messagebox.showerror('Error', 'Send emoji error')
print('error,', e)
print("got this emoji", emoji)
show_msg('<Me>', emoji, 'blue', is_file=False, msg_time=None)
def get_users(sock,user):
header = str(GET_ALL_USERS)
method.send(sock,header)
try:
data, rest = method.receive(sock)
except Exception as e:
messagebox.showerror('Error','Get users error!')
return None
state = data
if state == str(GET_SUCCESS):
#rest = rest.decode("utf-8")
users = rest.split("\r\n")
print("find users: ")
for user in users:
print(user)
return users
def get_chat_history(sock,user):
header = str(GET_ALL_CHAT_HISTORY)
msg = user.encode("utf-8")
method.send(sock,header,msg)
try:
data, rest = method.receive(sock)
except Exception as e:
messagebox.showerror('Error','Get chat history error!')
print('get chat history error',e)
return
state = data[:3]
if state == str(RET_HISTORY_ERROR):
messagebox.showerror('Error','--Get chat history error!')
return
if state == str(RET_HISTORY_SUCCESS):
msgs = rest.split("\r\n")
count = 0
for msg in msgs:
detail = msg.split("\t")
if len(detail)>=2 and count<5:
name = detail[0]
time = detail[1]
content = detail[2]
count += 1
if name != user:
show_msg(name,content,'green',msg_time = time)
else :
show_msg('<Me>',content,'blue',msg_time = time)
return
def get_history_files():
header = str(GET_ALL_FILE_HISTORY)
method.send(sock,header)
try:
data,rest = method.receive(sock)
except Exception as e:
print("get history file errors")
messagebox.showerror('Error','Get history files error!')
return
names = rest.split("\r\n")
return names
def show_msg(sender,msg,color,is_file = False,msg_time = None):
if msg_time == None:
msg_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
text_recv.config(state = NORMAL)
text_recv.see(END)
text_recv.insert('end',"{0} {1} \n".format(sender,msg_time),color)
if is_file == False :
text_recv.insert('end','{0} \n'.format(msg))
text_recv.see('end')
text_recv.config(state = DISABLED)
def send_message():
text = input_text.get('0.0','end')
header = str(SEND_MESSAGE) + "\r\n" + str(len(text))
if text is None:
return
try:
send_text = text.encode("utf-8")
method.send(sock,header,send_text)
show_msg('<Me>',text,'blue',is_file = False)
except Exception as e:
messagebox.showerror('Error','Sending message error!')
print('send msg error,',e)
input_text.delete('0.0','end')
def send_file():
file_path = filedialog.askopenfilename(title = "Choose Files ��")
if os.path.exists(file_path) == False:
return
state = method.upload_file(sock,file_path,"")
if state == False:
messagebox.showerror('Error','File size can not exceed 50 M!')
show_msg("<Me>","send file: {0}\n".format(file_path),'blue')
print(file_path)
filename = os.path.basename(file_path)
if filename not in files:
files.append(filename)
fileListbox.insert('end',filename)
def room_exists(room):
if room is None:
return False
if int(room.master.winfo_exists())==0:
return False
return True
def private_chat(event):
choice = friend_list.get(friend_list.curselection())
if choice == User:
return
if room_exists(user2room.get(choice))==False:
# init - get chat history
user2room[choice] = PrivateChatPage(window,User,choice,sock,user2room_history)
def send_add_friend_request():
header = str(ADD_FRIEND)
name_ = friend_name.get()
msg = User + "\r\n" + name_
msg = msg.encode("utf-8")
try:
method.send(sock,header,msg)
except Exception as e:
print("!!Send add-friend-request error:",e)
def add_friend():
win_ = Toplevel(window)
win_.title("Add Friends�不")
win_.geometry("300x200")
tk.Label(win_,text = 'User name:',font = ('Arial',14)).place(x=10,y=40)
entry_usr_name = tk.Entry(win_,textvariable = friend_name,font = ('Arial',12))
entry_usr_name.place(x=90,y=40)
add_button = tk.Button(win_,text = 'Add',font = ('Arial',12),width=10,height=2,command = send_add_friend_request)
add_button.place(x=80,y=80)
def get_all_friends():
try:
msg = User.encode("utf-8")
method.send(sock,str(SHOW_ALL_FRIENDS),msg)
except Exception as e:
print("get friends error ",e)
try:
data,rest = method.receive(sock)
except Exception as e:
print("receive friends error, ",e)
friends = []
if data[:3] == str(SHOW_FRIENDS_SUCCESS):
friends = rest.split("\r\n")
return friends
if data[:3] == str(SHOW_FRIENDS_ERROR):
print("server get friends error")
if data[:3] == str(NO_FRIENDS):
print(User+" have no friends at this time ")
return friends
def download_file(event):
#fileListbox.bind('<Double-Button-1>',download_file)
choice = fileListbox.get(fileListbox.curselection())
print("select: ",choice,type(choice),User)
msg = User + "\r\n" + choice
msg = msg.encode("utf-8")
try:
method.send(sock,str(DOWNFILE),msg)
except Exception as e:
print("send downfile request error ",e)
messagebox.showerror('Error','Download file error!')
return
def listener(sock,root):
print("?? then ??")
listen_this = [sock]
while True:
reads, writes, errors = select.select(listen_this,[],[])
for master in reads:
if master == sock:
try:
data, rest = method.receive(master)
print(data,rest)
print("--- check which s