Facebook
From anti_gay_programmer, 4 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 173
  1. import websocket
  2. import json
  3. import threading
  4. import time
  5.  
  6. ##########    infos    ##########
  7. channel=input('channel:\n>>')
  8. trip=input('passwd:\n>>')
  9. nick=input('nick:\n>>')
  10.  
  11. if trip=='':
  12.     pass
  13. else:
  14.     nick=nick+'#'+trip
  15.  
  16. on_message = [] #msgs from ur join to now
  17. online_users = [] #online since u joined
  18. on_join = []
  19. on_leave= []
  20. on_whisper = []
  21.  
  22.  
  23. ##########        ##########
  24.  
  25. ##########    DEFS    ##########
  26.  
  27. def _info():
  28.     while True:
  29.         result = json.loads(ws.recv()) #get stuff
  30.         if result["cmd"] == "chat" and not result["nick"] == nick: #if otheres send shit
  31.             for handler in list(on_message):
  32.                 handler(result["text"], result["nick"])
  33.             print(result)
  34.         elif result["cmd"] == "onlineAdd":
  35.             online_users.append(result["nick"])
  36.             for handler in list(on_join):
  37.                 handler(result["nick"])
  38.             print(result)
  39.         elif result["cmd"] == "onlineRemove":
  40.             online_users.remove(result["nick"])
  41.             for handler in list(on_leave):
  42.                 handler(result["nick"])
  43.             print(result)
  44.         elif result["cmd"] == "onlineSet":
  45.             for nick in result["nicks"]:
  46.                 online_users.append(nick)
  47.             print(result)
  48.         elif result["cmd"]=="info" and result["type"]=="whisper":
  49.             for handler in list(on_whisper):
  50.                 handler(result["text"],result["from"],result)
  51.             print(result)
  52.  
  53. #def send_to(target, msg):
  54. #        _send({"cmd": "whisper", "nick": target, "text": msg})
  55.  
  56. def _send(pkg):
  57.     encoded=json.dumps(pkg)
  58.     ws.send(encoded)
  59.    
  60. def _stay_in_chat():
  61.     while ws.connected:
  62.         _send({"cmd": "ping"})
  63.         time.sleep(60)
  64.  
  65. def _send_msg(msg):
  66.     _send({"cmd": "chat", "text": msg})
  67. ##########        ##########
  68.  
  69. ##########     CONNECT    ##########
  70. ws = websocket.create_connection('wss://hack.chat/chat-ws')
  71.  
  72. _send({"cmd": "join", "channel": channel, "nick": nick})
  73. threading.Thread(target=_stay_in_chat).start()
  74.  
  75.  
  76. #def _daemon():
  77. #    daemon_thread=threading.Thread(target=info)
  78. #    daemon_thread.start()
  79.  
  80. daemon_thread=threading.Thread(target=_info);daemon_thread.start()