Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. import socket
  2. import threading
  3. import time
  4. import random
  5. from Tkinter import *
  6.  
  7. #Chatango Client
  8. #Made by MegaLoler
  9. #Alpha
  10.  
  11. #Changes:
  12. #Fixed some formatting and style bugs, made your name underlined, converted some html codes, and made the windows fixed-size. The chat window will soon be properly resiable
  13. #Added BBCode support, [b], [u], and [i]
  14.  
  15. #Todo:
  16. #Cleanup weird box characters that sometimes appear at end of messages
  17. #limit the amount of messages
  18. #Click on names to put @username in the message field
  19. #If a message contains @yourusername then it will be highlighted
  20. #make hyperlinks clickable
  21. #Add style options
  22. #Add people here list
  23. #On connect, load previous messages and other data about the room
  24. #Add times
  25. #add avatars
  26. #add image embedding, and youtube embedding (maybe)
  27. #catch login errors
  28. #Preferences window
  29. #some preferences will unclude... Bypass censor, ...
  30. #Save prefs, styles, and user info
  31. #Add smilies, and custom smileys (maybe)
  32. #Sound
  33. #PM window
  34. #window that accesses all your actualy chatango settings
  35.  
  36. cons = []
  37.  
  38. HOST = '72.172.232.90'
  39. PORT = 443
  40.  
  41. COLOR = '6A0'
  42. SIZE = '10'
  43. NAMECOLOR = '8F0'
  44. FONT = "0"
  45.  
  46. fonts = ("Arial", "Comic", "Georgia", "Handwriting", "Impact", "Palatino", "Papyrus", "Times", "Typewriter")
  47.  
  48. running = 1
  49.  
  50. class IdleThread (threading.Thread):
  51. def run(self):
  52. while running:
  53. time.sleep(30)
  54. if running:
  55. for i in cons:
  56. i[0].send(chr(13) + chr(10) + chr(0))
  57.  
  58. def con(room, USER, PASS):
  59. print "Connecting..."
  60. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  61. s.connect((HOST, PORT))
  62. s.send("v" + chr(0))
  63. data = s.recv(1024)
  64. print "Connected!"
  65. print "Logging in..."
  66. s.send("bauth:"+room+":7202053138505182:"+USER+":"+PASS + chr(0))
  67. rid = random.random()
  68. cons.append((s,room,[], rid))
  69. print "You are now talking in " + room + "!"
  70. return (s, rid)
  71.  
  72. class ConWin:
  73. def __init__(self, master, s):
  74. master.resizable(width=FALSE, height=FALSE)
  75. self.s = s[0]
  76. self.NAME = s[1]
  77. self.rid = s[2]
  78. self.master = master
  79. frame = Frame(master)
  80. frame.pack()
  81.  
  82. self.e = Entry(frame)
  83. self.e.focus_set()
  84. self.e.pack(fill=X, side=BOTTOM, expand=1)
  85.  
  86. scrollbar = Scrollbar(frame)
  87. scrollbar.pack(side=RIGHT, fill=Y, expand=1)
  88.  
  89. self.t = Text(frame)
  90. self.t.config(font=('courier', 10, 'normal'), state=DISABLED)
  91. self.t.pack(fill=BOTH, expand=1)
  92.  
  93. self.t.config(yscrollcommand=scrollbar.set)
  94. scrollbar.config(command=self.t.yview)
  95. def sendd(self, event):
  96. global NAMECOLOR, COLOR, SIZE, FONT, cons
  97. ddd = self.e.get()
  98. if ddd != "":
  99. if ddd[0] == "/":
  100. ddd = ddd[1:]
  101. ddds = ddd.split(" ")
  102. if len(ddds) == 1:
  103. print "Set to what?"
  104. else:
  105. if ddds[0] == "name":
  106. NAMECOLOR = ddds[1]
  107. if len(NAMECOLOR) < 3:
  108. NAMECOLOR = "0" + NAMECOLOR
  109. if len(NAMECOLOR) < 3:
  110. NAMECOLOR = "0" + NAMECOLOR
  111. if len(NAMECOLOR) < 3:
  112. NAMECOLOR = "0" + NAMECOLOR
  113. print "Name color set to " + NAMECOLOR
  114. elif ddds[0] == "color":
  115. COLOR = ddds[1]
  116. if len(COLOR) < 3:
  117. COLOR = "0" + COLOR
  118. if len(COLOR) < 3:
  119. COLOR = "0" + COLOR
  120. if len(COLOR) < 3:
  121. COLOR = "0" + COLOR
  122. print "Color set to " + COLOR
  123. elif ddds[0] == "size":
  124. SIZE = ddds[1]
  125. if len(SIZE) == 1:
  126. SIZE = "0" + SIZE
  127. print "Size set to " + SIZE
  128. elif ddds[0] == "font":
  129. FONT = ddds[1]
  130. if FONT == "":
  131. FONT = "0"
  132. print "Font set to " + FONT
  133. else:
  134. print "Unknown command!"
  135. else:
  136. ddd = ddd.replace("\"",""")
  137. ddd = ddd.replace("'","&apos;")
  138. ddd = ddd.replace(">",">")
  139. ddd = ddd.replace("<","<")
  140. ddd = ddd.replace("[b]","<B>")
  141. ddd = ddd.replace("[u]","<U>")
  142. ddd = ddd.replace("[i]","<I>")
  143. ddd = ddd.replace("[/b]","</B>")
  144. ddd = ddd.replace("[/u]","</U>")
  145. ddd = ddd.replace("[/i]","</I>")
  146. ddd = ddd.replace("[B]","<B>")
  147. ddd = ddd.replace("[U]","<U>")
  148. ddd = ddd.replace("[I]","<I>")
  149. ddd = ddd.replace("[/B]","</B>")
  150. ddd = ddd.replace("[/U]","</U>")
  151. ddd = ddd.replace("[/I]","</I>")
  152. ppp = "bmsg:<n"+NAMECOLOR+"/><f x"+SIZE+COLOR+"=\""+FONT+"\">" + ddd + chr(13) + chr(10) + chr(0)
  153. self.s.send(ppp)
  154. self.e.delete(0, END)
  155. def startloop(self):
  156. while running:
  157. try:
  158. for i in cons:
  159. if i[3] == self.rid:
  160. buf = i[2]
  161. for a in buf:
  162. if a[0] == "b":
  163. self.t.config(state=NORMAL)
  164. aa = a.split(":")
  165. an = aa[2]
  166. am = ":".join(aa[9:len(aa)])
  167. am = am[0:len(am)-3]
  168. n = am.count("<n")
  169. nc = "#000"
  170. if n > 0:
  171. nc = "#" + am[2:5]
  172. am = am[7:len(am)]
  173. nametag = str(random.random())
  174. un = ""
  175. if an.lower() == self.NAME.lower():
  176. un = " underline"
  177. self.t.tag_config(nametag, foreground=nc, font=('courier', 12, 'bold' + un))
  178. self.t.tag_bind(nametag, "<Button-1>", self.clickname)
  179. self.t.insert(END, an, nametag)
  180. self.t.insert(END, ": ")
  181. styles = am.split("<f ")
  182. for st in styles:
  183. if st == '':
  184. continue
  185. pos = st.split(">")
  186. std = pos[0]
  187. st = ">".join(pos[1:len(pos)])
  188. std = std.split('="')
  189. d1 = std[0][1:len(std[0])]
  190. d2 = std[1][0:len(std[1])-1]
  191. siz = 10
  192. col = "#000"
  193. fon = "Arial"
  194. if len(d1) == 2:
  195. siz = int(d1)
  196. elif len(d1) == 3:
  197. col = "#" + d1
  198. elif len(d1) == 5:
  199. siz = int(d1[0:2])
  200. col = "#" + d1[2:5]
  201. elif len(d1) == 6:
  202. col = "#" + d1
  203. elif len(d1) == 8:
  204. siz = int(d1[0:2])
  205. col = "#" + d1[2:8]
  206. if d2 != '':
  207. try:
  208. d2 = int(d2)
  209. except ValueError:
  210. pass
  211. else:
  212. d2 = fonts[d2]
  213. fon = d2
  214. st = "".join(st.split("</f>"))
  215. if st.count("<") > 0:
  216. B = 0
  217. U = 0
  218. I = 0
  219. st2 = st.split("<")
  220. for st in st2:
  221. if st == '':
  222. continue
  223. op = "normal"
  224. if st[1] == ">":
  225. if st[0].lower() == "b":
  226. B = B + 1
  227. if st[0].lower() == "u":
  228. U = U + 1
  229. if st[0].lower() == "i":
  230. I = I + 1
  231. st = st[2:len(st)]
  232. elif st[0] == "/" and st[2] == ">":
  233. if st[1].lower() == "b":
  234. B = B - 1
  235. if st[1].lower() == "u":
  236. U = U - 1
  237. if st[1].lower() == "i":
  238. I = I - 1
  239. st = st[3:len(st)]
  240. if st == '':
  241. continue
  242. if B or U or I:
  243. B2 = B
  244. U2 = U
  245. I2 = I
  246. if B2 > 1: B2 = 1
  247. if U2 > 1: U2 = 1
  248. if I2 > 1: I2 = 1
  249. bt = (""," bold")[B2]
  250. ut = (""," underline")[U2]
  251. it = (""," italic")[I2]
  252. op = bt + ut + it
  253. mestag = str(random.random())
  254. self.t.tag_config(mestag, foreground=col, font=(fon, siz, op))
  255. st = st.replace(""","\"")
  256. st = st.replace("&apos;","'")
  257. st = st.replace(">",">")
  258. st = st.replace("<","<")
  259. self.t.insert(END, st, mestag)
  260. else:
  261. mestag = str(random.random())
  262. self.t.tag_config(mestag, foreground=col, font=(fon, siz, 'normal'))
  263. st = st.replace(""","\"")
  264. st = st.replace("&apos;","'")
  265. st = st.replace(">",">")
  266. st = st.replace("<","<")
  267. self.t.insert(END, st, mestag)
  268. self.t.insert(END, "\n", mestag)
  269. self.t.config(state=DISABLED)
  270. self.t.yview(END)
  271. del i[2][0:len(i[2])]
  272. break
  273. self.master.update()
  274. except TclError:
  275. break
  276. def clickname(self, event):
  277. self.e.insert(END, "@" + self.NAME)
  278.  
  279. class ListenThread (threading.Thread):
  280. def run(self):
  281. while running and self.runnin:
  282. s = self.s
  283. data = s[0].recv(1024)
  284. if self.runnin and running:
  285. for i in cons:
  286. if i[3] == s[2]:
  287. i[2].append(data)
  288.  
  289. def conwin(USER, PASS, ROOM):
  290. s = con(ROOM, USER, PASS)
  291. rid = s[1]
  292. s = s[0]
  293. root2 = Tk()
  294. root2.title("Chatango - " + ROOM + " - " + USER)
  295. thewin = ConWin(root2,(s,USER,rid))
  296. root2.bind("<Return>", thewin.sendd)
  297. l = ListenThread()
  298. l.s = (s,ROOM,rid)
  299. l.runnin = 1
  300. l.start()
  301. thewin.startloop()
  302. print "Disconnecting from " + ROOM + "..."
  303. s.send(chr(13) + chr(10) + chr(0))
  304. s.close()
  305. l.runnin = 0
  306. i = 0
  307. for a in cons:
  308. if a[1] == ROOM:
  309. del cons[i]
  310. break
  311. i = i + 1
  312. print "Disconnected from " + ROOM
  313.  
  314. root = Tk()
  315. root.title("Login/Join")
  316. root.resizable(width=FALSE, height=FALSE)
  317.  
  318. def makeentry(parent, caption, r, **options):
  319. Label(parent, text=caption).grid(row=r, sticky=E)
  320. entry = Entry(parent, **options)
  321. entry.config(width=20)
  322. entry.grid(row=r, column=1)
  323. return entry
  324.  
  325. frame = Frame(root)
  326. frame.pack()
  327. user = makeentry(frame, "Username:", 0)
  328. password = makeentry(frame, "Password:", 1, show="*")
  329. room = makeentry(frame, "Room:", 2)
  330. user.focus_set()
  331.  
  332. def login(event=None):
  333. USER = user.get()
  334. PASS = password.get()
  335. ROOM = room.get()
  336. conwin(USER, PASS, ROOM)
  337.  
  338. Button(frame, text="Login/Join", command=login).grid(row=3, column=1, sticky=E)
  339. root.bind("<Return>", login)
  340.  
  341. IdleThread().start()
  342. root.mainloop()
  343.  
  344. print "Closing all connections..."
  345. running = 0
  346. for s in cons:
  347. s[0].close()
  348. print "Bye!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement