Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. import json
  2. import socket
  3. import _thread
  4. import tkinter
  5. from tkinter import ttk
  6. from tkinter import *
  7.  
  8. class Account:
  9. def __init__(self, username, firstName, lastName, email, password):
  10. self.username = username
  11. self.firstName = firstName
  12. self.lastName = lastName
  13. self.email = email
  14. self.password = password
  15. def message(self):
  16. print("This function is not avaliable yet")
  17. with open('config.json') as jsonConfig:
  18. config = json.load(jsonConfig)
  19.  
  20. def sendMessage(msgInput):
  21. def add(MESSAGE, colour=colourTheme):
  22. ChatLog.config(state=NORMAL)
  23. ChatLog.insert(END, '\n')
  24. LineNumber = float(ChatLog.index(END))-1.0
  25. ChatLog.insert(END, MESSAGE)
  26. num = len(MESSAGE)
  27. ChatLog.tag_add(MESSAGE, LineNumber, LineNumber+num)
  28. ChatLog.tag_config(MESSAGE, foreground=colour, font=("courier new", 11, "bold"))
  29. ChatLog.config(state=DISABLED)
  30. ChatLog.see(END)
  31.  
  32. entryBox.delete(0, END)
  33. sendData = msgInput
  34.  
  35. if msgInput == '.help':
  36. HELP_MSG = '''.help - prints the help menu
  37. .quit - exit the server gracefully
  38. .name - change current username (unavailable)
  39. .clear - clear chat (client-side)
  40. .online - view online users
  41. '''
  42.  
  43. ADMIN_MSG = '''.kick - kick a client off
  44. .clearall - clears messages for everyone
  45. .fq - force quits all clients
  46. .status - view server status
  47. .message - private message a user
  48. '''
  49. add(HELP_MSG, '#FFFFFF')
  50. elif msgInput == '.clear':
  51. ChatLog.config(state=NORMAL)
  52. ChatLog.delete(1.0, END)
  53. CLEAR_MSG = 'Chat was cleared successfully.'
  54. add(CLEAR_MSG, '#FFFFFF')
  55. elif msgInput == '.name':
  56. username = input("Enter username")
  57. UN_MSG = USERNAME + " has changed username to " + username
  58. add(UN_MSG, '#FFFFFF')
  59. USERNAME = username2;
  60. elif msgInput == '.quit':
  61. Window.destroy()
  62. clientSocket.close()
  63. elif msgInput == '.colour':
  64. CL_MSG = 'Open UI-Settings to change the theme.'
  65. add(CL_MSG, '#FFFFFF')
  66. elif len(msgInput) > 150:
  67. SPAM_MSG = 'Your message was not sent due to potential spam.'
  68. add(SPAM_MSG, '#FFFFFF')
  69. elif msgInput == '.online':
  70. clientSocket.send(str.encode('$-$online'))
  71. else:
  72. CODES = USERNAME + ': ' + sendData
  73. clientSocket.send(str.encode('\n'))
  74. clientSocket.send(str.encode(CODES))
  75.  
  76. def PressAction(event):
  77. entryBox.config(state=NORMAL)
  78. sendMessage(entryBox.get())
  79. def DisableEntry(event):
  80. entryBox.config(state=DISABLED)
  81.  
  82. # Config incorporation.
  83. colourTheme = config['window']['theme']
  84. windowResolution = config['window']['resolution']
  85. windowTitle = config['window']['title']
  86. windowForeground = config['window']['foreground']
  87. windowBackground = config['window']['background']
  88.  
  89. programVersion = config['information']['version']
  90. programStage = config['information']['stage']
  91.  
  92. Window = Tk()
  93. Window.configure(bg=windowBackground)
  94. Window.geometry(windowResolution)
  95. Window.title(windowTitle)
  96.  
  97. titleText = StringVar()
  98. titleText.set('C H A T')
  99.  
  100. lineText = StringVar()
  101. lineText.set('___________________________________________________________________________')
  102.  
  103. enterMessageText = StringVar()
  104. enterMessageText.set('M E S S A G E: ')
  105.  
  106. buttonText = StringVar()
  107. buttonText.set('S E N D')
  108.  
  109. connectingText = StringVar()
  110. connectingText.set('Connecting to the chat server...')
  111.  
  112. versionText = StringVar()
  113. versionText.set('V E R S I O N ' + programVersion)
  114.  
  115. bLabel = Label(Window, bg=windowBackground, fg=windowBackground).pack()
  116.  
  117. lineLabel = Label(Window, textvariable=lineText, font='Arial 15 bold', fg=colourTheme, bg=windowBackground)
  118. lineLabel.place(relx=.04, rely=.14)
  119.  
  120. titleLabel = Label(Window, textvariable=titleText, font='Arial 20 bold', bg=windowBackground, fg=windowForeground)
  121. titleLabel.place(relx=.04,rely=.09)
  122.  
  123. enterMessageLabel = Label(Window, textvariable=enterMessageText, font='Arial 13 bold', bg=windowBackground, fg=windowForeground)
  124. enterMessageLabel.place(relx=.04,rely=.85)
  125.  
  126. entryBox = Entry(Window, width=100)
  127. entryBox.place(relx=.18,rely=.855)
  128.  
  129. entryBox.bind("<Return>", DisableEntry)
  130. entryBox.bind("<KeyRelease-Return>", PressAction)
  131.  
  132. entryBox.insert(END, '.help')
  133.  
  134. connectingLabel = Label(Window, textvariable=connectingText, font='Arial 8 bold', fg=colourTheme, bg=windowBackground)
  135. connectingLabel.place(relx=.040,rely=.225)
  136.  
  137. sendButton = Button(Window, textvariable=buttonText, font='Arial 7 bold', width=13, height=1, command=lambda:PressAction("<Return>"))
  138. sendButton.place(relx=.86,rely=.855)
  139.  
  140. verionLabel = Label(Window, textvariable=versionText, font='Arial 11 bold', bg=windowBackground, fg=colourTheme)
  141. verionLabel.place(relx=.08, rely=.17)
  142.  
  143. ChatLog = Text(Window, bd=1, bg="#141414", height="13", width="91", font="Arial")
  144. ChatLog.place(relx=.043,rely=.23)
  145.  
  146. # Receiving data from other clients.
  147. def Receive():
  148. while True:
  149. try:
  150. receiveData = clientSocket.recv(4096)
  151. except:
  152. # When the server goes down.
  153. print("INFO: Server closed connection")
  154. # When the connection closes, interrupt the main thread.
  155. _thread.interrupt_main()
  156. break
  157. # If not data is returned, close the connection.
  158. if not receiveData:
  159. print("INFO: Server closed connection")
  160. _thread.interrupt_main()
  161. break
  162. else:
  163. ChatLog.config(state=NORMAL)
  164. LineNumber = float(ChatLog.index(END))-1.0
  165. ChatLog.insert(END, receiveData)
  166. num = len(receiveData)
  167. ChatLog.tag_add("Them", LineNumber, LineNumber+num)
  168. ChatLog.tag_config("Them", foreground='#ffffff', font=("courier new", 11, "bold"))
  169. ChatLog.config(state=DISABLED)
  170. ChatLog.see(END)
  171.  
  172. IP = '86.153.124.215'
  173. PORT = 6666
  174. # USERNAME = 'NO'
  175.  
  176. print("WELCOME: Ready to connect.")
  177. print("INFO: Connecting to ", str(IP) + ":" + str(PORT))
  178. FINAL_NAME = ''
  179.  
  180. USERNAME = self.username
  181. for char in USERNAME.upper():
  182. FINAL_NAME = '$$$' + USERNAME
  183.  
  184. ADMIN_MSG = 'An Admin has joined with elevated permissions'
  185. JOIN_MSG = USERNAME + ' has joined the server'
  186.  
  187. clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  188. try:
  189. clientSocket.connect((IP, PORT))
  190. clientSocket.send(str.encode(FINAL_NAME))
  191. print("INFO: Sending client information...")
  192. print("INFO: Connected to ", str(IP) + ':' + str(PORT))
  193. clientSocket.send(str.encode('\n'))
  194. clientSocket.send(str.encode(JOIN_MSG))
  195. clientSocket.send(str.encode('\n'))
  196. # clientSocket.send(str.encode('$$$Latest'))
  197. # clientSocket.send(str.encode('\n'))
  198. # clientSocket.send(str.encode(ADMIN_MSG))
  199.  
  200. _thread.start_new_thread(Receive, ())
  201. _thread.start_new_thread(Window.mainloop())
  202. except:
  203. print('ERROR: Unable to connect to the requested server.')
  204.  
  205. try:
  206. while True:
  207. continue
  208. except:
  209. print("INFO: Client program quits....")
  210. clientSocket.close()
  211.  
  212. def accountSettings(self):
  213. print("This function is not availiable yet")
  214.  
  215.  
  216. user = Account("d_akinfeimwa", "David", "Akinfemiwa", "daveak1602@gmail.com", "BloodOfJesus")
  217. user.message()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement