Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.67 KB | None | 0 0
  1.  
  2. import socket
  3. import threading
  4. import time
  5. import random
  6. from Tkinter import *
  7.  
  8. #Chatango Client
  9. #Made by MegaLoler
  10. #Alpha
  11.  
  12. #Changes:
  13. #Added a lot of formatting and styles to the window; you can't change your own style yet, though, other than basic html tags(except with / cmomands)
  14.  
  15. #Todo:
  16. #Bold italic and underline styles
  17. #Convert html codes
  18. #Cleanup weird box characters that sometimes appear at end of messages
  19. #limit the amount of messages
  20. #Click on names to put @username in the message field
  21. #Add people here list
  22. #On connect, load previous messages and other data about the room
  23. #Add style options
  24. #Preferences window
  25. #Save prefs, styles, and user info
  26. #Add smilies, and custom smileys (maybe)
  27. #PM page
  28.  
  29. cons = []
  30.  
  31. HOST = '72.172.232.90'
  32. PORT = 443
  33.  
  34. COLOR = '6A0'
  35. SIZE = '10'
  36. NAMECOLOR = '8F0'
  37. FONT = "0"
  38.  
  39. fonts = ("Arial", "Comic", "Georgia", "Handwriting", "Impact", "Palatino", "Papyrus", "Times", "Typewriter")
  40.  
  41. running = 1
  42.  
  43. class IdleThread (threading.Thread):
  44.     def run(self):
  45.         while running:
  46.             time.sleep(30)
  47.             if running:
  48.                 for i in cons:
  49.                     i[0].send(chr(13) + chr(10) + chr(0))
  50.  
  51. def con(room, USER, PASS):
  52.     print "Connecting..."
  53.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  54.     s.connect((HOST, PORT))
  55.     s.send("v" + chr(0))
  56.     data = s.recv(1024)
  57.     print "Connected!"
  58.     print "Logging in..."
  59.     s.send("bauth:"+room+":7202053138505182:"+USER+":"+PASS + chr(0))
  60.     rid = random.random()
  61.     cons.append((s,room,[], rid))
  62.     print "You are now talking in " + room + "!"
  63.     return (s, rid)
  64.  
  65. class ConWin:
  66.     def __init__(self, master, s):
  67.         self.s = s
  68.         frame = Frame(master)
  69.         frame.pack()
  70.        
  71.         self.e = Entry(frame)
  72.         self.e.focus_set()
  73.         self.e.pack(fill=X, side=BOTTOM)
  74.  
  75.         scrollbar = Scrollbar(frame)
  76.         scrollbar.pack(side=RIGHT, fill=Y)
  77.  
  78.         self.t = Text(frame)
  79.         self.t.config(font=('courier', 10, 'normal'),state=DISABLED)
  80.         self.t.pack(fill=BOTH)
  81.  
  82.         self.t.config(yscrollcommand=scrollbar.set)
  83.         scrollbar.config(command=self.t.yview)
  84.     def sendd(self, event):
  85.         global NAMECOLOR, COLOR, SIZE, FONT, cons
  86.         ddd = self.e.get()
  87.         if ddd != "":
  88.             if ddd[0] == "/":
  89.                 ddd = ddd[1:]
  90.                 ddds = ddd.split(" ")
  91.                 if len(ddds) == 1:
  92.                     print "Set to what?"
  93.                 else:
  94.                     if ddds[0] == "name":
  95.                         NAMECOLOR = ddds[1]
  96.                         if len(NAMECOLOR) < 3:
  97.                             NAMECOLOR = "0" + NAMECOLOR
  98.                         if len(NAMECOLOR) < 3:
  99.                             NAMECOLOR = "0" + NAMECOLOR
  100.                         if len(NAMECOLOR) < 3:
  101.                             NAMECOLOR = "0" + NAMECOLOR
  102.                         print "Name color set to " + NAMECOLOR
  103.                     elif ddds[0] == "color":
  104.                         COLOR = ddds[1]
  105.                         if len(COLOR) < 3:
  106.                             COLOR = "0" + COLOR
  107.                         if len(COLOR) < 3:
  108.                             COLOR = "0" + COLOR
  109.                         if len(COLOR) < 3:
  110.                             COLOR = "0" + COLOR
  111.                         print "Color set to " + COLOR
  112.                     elif ddds[0] == "size":
  113.                         SIZE = ddds[1]
  114.                         if len(SIZE) == 1:
  115.                             SIZE = "0" + SIZE
  116.                         print "Size set to " + SIZE
  117.                     elif ddds[0] == "font":
  118.                         FONT = ddds[1]
  119.                         if FONT == "":
  120.                             FONT = "0"
  121.                         print "Font set to " + FONT
  122.                     else:
  123.                         print "Unknown command!"
  124.             else:
  125.                 ppp = "bmsg:<n"+NAMECOLOR+"/><f x"+SIZE+COLOR+"=\""+FONT+"\">" + ddd + chr(13) + chr(10) + chr(0)
  126.                 self.s.send(ppp)
  127.             self.e.delete(0, END)
  128.  
  129. class ListenThread (threading.Thread):
  130.     def run(self):
  131.         while running and self.runnin:
  132.             s = self.s
  133.             data = s[0].recv(1024)
  134.             if self.runnin and running:
  135.                 for i in cons:
  136.                     if i[3] == s[2]:
  137.                         i[2].append(data)
  138.  
  139. def conwin(USER, PASS, ROOM):
  140.     s = con(ROOM, USER, PASS)
  141.     rid = s[1]
  142.     s = s[0]
  143.     root2 = Tk()
  144.     root2.title(ROOM + " - " + USER)
  145.     thewin = ConWin(root2,s)
  146.     root2.bind("<Return>", thewin.sendd)
  147.     l = ListenThread()
  148.     l.s = (s,ROOM,rid)
  149.     l.runnin = 1
  150.     l.start()
  151.     while running:
  152.         try:
  153.             for i in cons:
  154.                 if i[3] == rid:
  155.                     buf = i[2]
  156.                     for a in buf:
  157.                         if a[0] == "b":
  158.                             thewin.t.config(state=NORMAL)
  159.                             aa = a.split(":")
  160.                             an = aa[2]
  161.                             am = ":".join(aa[9:len(aa)])
  162.                             am = am[0:len(am)-3]
  163.                             n = am.count("<n")
  164.                             nc = "#000"
  165.                             if n > 0:
  166.                                 nc = "#" + am[2:5]
  167.                                 am = am[7:len(am)]
  168.                             nametag = str(random.random())
  169.                             thewin.t.tag_config(nametag, foreground=nc, font=('courier', 10, 'bold'))
  170.                             thewin.t.insert(END, an, nametag)
  171.                             thewin.t.insert(END, ": ")
  172.                             styles = am.split("<f ")
  173.                             print styles
  174.                             for st in styles:
  175.                                 if st == '':
  176.                                     continue
  177.                                 pos = st.split(">")
  178.                                 std = pos[0]
  179.                                 st = ">".join(pos[1:len(pos)])
  180.                                 std = std.split('="')
  181.                                 d1 = std[0][1:len(std[0])]
  182.                                 d2 = std[1][0:len(std[1])-1]
  183.                                 siz = 10
  184.                                 col = "#000"
  185.                                 fon = "Arial"
  186.                                 if len(d1) == 2:
  187.                                     siz = int(d1)
  188.                                 elif len(d1) == 3:
  189.                                     col = "#" + d1
  190.                                 elif len(d1) == 5:
  191.                                     siz = int(d1[0:2])
  192.                                     col = "#" + d1[2:5]
  193.                                 if d2 != '':
  194.                                     try:
  195.                                         d2 = int(d2)
  196.                                     except ValueError:
  197.                                         pass
  198.                                     else:
  199.                                         d2 = fonts[d2]
  200.                                     fon = d2
  201.                                 st = "".join(st.split("</f>"))
  202.                                 mestag = str(random.random())
  203.                                 thewin.t.tag_config(mestag, foreground=col, font=(fon, siz, 'normal'))
  204.                                 thewin.t.insert(END, st, mestag)
  205.                             thewin.t.insert(END, "\n", mestag)
  206.                             thewin.t.config(state=DISABLED)
  207.                             thewin.t.yview(END)
  208.                     del i[2][0:len(i[2])]
  209.             root2.update()
  210.         except TclError:
  211.             break
  212.     print "Disconnecting from " + ROOM + "..."
  213.     l.runnin = 0
  214.     i = 0
  215.     for a in cons:
  216.         if a[1] == ROOM:
  217.             del cons[i]
  218.             break
  219.         i = i + 1
  220.     s.close()
  221.     print "Disconnected from " + ROOM
  222.  
  223. root = Tk()
  224. root.title("Login/Join")
  225.  
  226. def makeentry(parent, caption, r, **options):
  227.     Label(parent, text=caption).grid(row=r, sticky=E)
  228.     entry = Entry(parent, **options)
  229.     entry.config(width=20)
  230.     entry.grid(row=r, column=1)
  231.     return entry
  232.  
  233. frame = Frame(root)
  234. frame.pack()
  235. user = makeentry(frame, "Username:", 0)
  236. password = makeentry(frame, "Password:", 1, show="*")
  237. room = makeentry(frame, "Room:", 2)
  238. user.focus_set()
  239.  
  240. def login(event):
  241.     USER = user.get()
  242.     PASS = password.get()
  243.     ROOM = room.get()
  244.     conwin(USER, PASS, ROOM)
  245.    
  246. Button(frame, text="Login", command=login).grid(row=3, column=1, sticky=E)
  247. root.bind("<Return>", login)
  248.  
  249. IdleThread().start()
  250. root.mainloop()
  251.  
  252. print "Closing all connections..."
  253. running = 0
  254. for s in cons:
  255.     s[0].close()
  256. print "Bye!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement