SHARE
TWEET

Untitled

a guest Feb 4th, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################################################
  2. # File: ch.py
  3. # Title: Chatango Library
  4. # Original Author: Lumirayz/Lumz <lumirayz@gmail.com>
  5. # Current Maintainers and Contributors:
  6. #  Nullspeaker <import codecs;codecs.encode('aunzzbaq129@tznvy.pbz','rot_13')>
  7. #  asl97 <asl97@outlook.com>
  8. #  pystub
  9. #  dani87
  10. #  domzy
  11. #  kamijoutouma
  12. #  piks
  13. # Version: 1.3.8
  14. # Description:
  15. #  An event-based library for connecting to one or multiple Chatango rooms, has
  16. #  support for several things including: messaging, message font,
  17. #  name color, deleting, banning, recent history, 2 userlist modes,
  18. #  flagging, avoiding flood bans, detecting flags.
  19. # Contact Info:
  20. #  Any question, comment, or suggestion should be directed to the current
  21. #  maintainers and contributors, located at:
  22. #   https://github.com/Nullspeaker/ch.py
  23. #  Where a more satisfactory response to all bug reports (which can be made on the
  24. #  issues page) and other statements can be garnered. For things not specific or
  25. #  in direct reference to this library, 'ch.py', a direct response can be filed
  26. #  to the individual persons listed above as 'Current Maintainers and Contributors.'
  27. ################################################################
  28.  
  29. ################################################################
  30. # License
  31. ################################################################
  32. # Copyright 2011 Lumirayz
  33. # This program is distributed under the terms of the GNU GPL.
  34.  
  35. ################################################################
  36. # Imports
  37. ################################################################
  38. import socket
  39. import threading
  40. import time
  41. import random
  42. import re
  43. import sys
  44. import select
  45.  
  46. ################################################################
  47. # Debug stuff
  48. ################################################################
  49. debug = False
  50.  
  51. ################################################################
  52. # Python 2 compatibility
  53. ################################################################
  54. if sys.version_info[0] < 3:
  55.   class urllib:
  56.     parse = __import__("urllib")
  57.     request = __import__("urllib2")
  58.   input = raw_input
  59.   import codecs
  60.   import Queue as queue
  61. else:
  62.   import queue
  63.   import urllib.request
  64.   import urllib.parse
  65.  
  66. ################################################################
  67. # Constants
  68. ################################################################
  69. Userlist_Recent = 0
  70. Userlist_All = 1
  71.  
  72. BigMessage_Multiple = 0
  73. BigMessage_Cut = 1
  74.  
  75. # minimum of 1 thread needed
  76. Number_of_Threads = 1
  77. ################################################################
  78. # Struct class
  79. ################################################################
  80. class Struct:
  81.   def __init__(self, **entries):
  82.     self.__dict__.update(entries)
  83.  
  84. ################################################################
  85. # Tagserver stuff
  86. ################################################################
  87. specials = {'mitvcanal': 56, 'animeultimacom': 34, 'cricket365live': 21, 'pokemonepisodeorg': 22, 'animelinkz': 20, 'sport24lt': 56, 'narutowire': 10, 'watchanimeonn': 22, 'cricvid-hitcric-': 51, 'narutochatt': 70, 'leeplarp': 27, 'stream2watch3': 56, 'ttvsports': 56, 'ver-anime': 8, 'vipstand': 21, 'eafangames': 56, 'soccerjumbo': 21, 'myfoxdfw': 67, 'kiiiikiii': 21, 'de-livechat': 5, 'rgsmotrisport': 51, 'dbzepisodeorg': 10, 'watch-dragonball': 8, 'peliculas-flv': 69, 'tvanimefreak': 54, 'tvtvanimefreak': 54}
  88. tsweights = [['5', 75], ['6', 75], ['7', 75], ['8', 75], ['16', 75], ['17', 75], ['18', 75], ['9', 95], ['11', 95], ['12', 95], ['13', 95], ['14', 95], ['15', 95], ['19', 110], ['23', 110], ['24', 110], ['25', 110], ['26', 110], ['28', 104], ['29', 104], ['30', 104], ['31', 104], ['32', 104], ['33', 104], ['35', 101], ['36', 101], ['37', 101], ['38', 101], ['39', 101], ['40', 101], ['41', 101], ['42', 101], ['43', 101], ['44', 101], ['45', 101], ['46', 101], ['47', 101], ['48', 101], ['49', 101], ['50', 101], ['52', 110], ['53', 110], ['55', 110], ['57', 110], ['58', 110], ['59', 110], ['60', 110], ['61', 110], ['62', 110], ['63', 110], ['64', 110], ['65', 110], ['66', 110], ['68', 95], ['71', 116], ['72', 116], ['73', 116], ['74', 116], ['75', 116], ['76', 116], ['77', 116], ['78', 116], ['79', 116], ['80', 116], ['81', 116], ['82', 116], ['83', 116], ['84', 116]]
  89.  
  90. def getServer(group):
  91.   """
  92.   Get the server host for a certain room.
  93.  
  94.   @type group: str
  95.   @param group: room name
  96.  
  97.   @rtype: str
  98.   @return: the server's hostname
  99.   """
  100.   try:
  101.     sn = specials[group]
  102.   except KeyError:
  103.     group = group.replace("_", "q")
  104.     group = group.replace("-", "q")
  105.     fnv = float(int(group[0:min(5, len(group))], 36))
  106.     lnv = group[6: (6 + min(3, len(group) - 5))]
  107.     if(lnv):
  108.       lnv = float(int(lnv, 36))
  109.       lnv = max(lnv,1000)
  110.     else:
  111.       lnv = 1000
  112.     num = (fnv % lnv) / lnv
  113.     maxnum = sum(map(lambda x: x[1], tsweights))
  114.     cumfreq = 0
  115.     sn = 0
  116.     for wgt in tsweights:
  117.       cumfreq += float(wgt[1]) / maxnum
  118.       if(num <= cumfreq):
  119.         sn = int(wgt[0])
  120.         break
  121.   return "s" + str(sn) + ".chatango.com"
  122.  
  123. ################################################################
  124. # Uid
  125. ################################################################
  126. def _genUid():
  127.   """
  128.   generate a uid
  129.   """
  130.   return str(random.randrange(10 ** 15, 10 ** 16))
  131.  
  132. ################################################################
  133. # Message stuff
  134. ################################################################
  135. def _clean_message(msg):
  136.   """
  137.   Clean a message and return the message, n tag and f tag.
  138.  
  139.   @type msg: str
  140.   @param msg: the message
  141.  
  142.   @rtype: str, str, str
  143.   @returns: cleaned message, n tag contents, f tag contents
  144.   """
  145.   n = re.search("<n(.*?)/>", msg)
  146.   if n: n = n.group(1)
  147.   f = re.search("<f(.*?)>", msg)
  148.   if f: f = f.group(1)
  149.   msg = re.sub("<n.*?/>", "", msg)
  150.   msg = re.sub("<f.*?>", "", msg)
  151.   msg = _strip_html(msg)
  152.   msg = msg.replace("&lt;", "<")
  153.   msg = msg.replace("&gt;", ">")
  154.   msg = msg.replace("&quot;", "\"")
  155.   msg = msg.replace("&apos;", "'")
  156.   msg = msg.replace("&amp;", "&")
  157.   return msg, n, f
  158.  
  159. def _strip_html(msg):
  160.   """Strip HTML."""
  161.   li = msg.split("<")
  162.   if len(li) == 1:
  163.     return li[0]
  164.   else:
  165.     ret = list()
  166.     for data in li:
  167.       data = data.split(">", 1)
  168.       if len(data) == 1:
  169.         ret.append(data[0])
  170.       elif len(data) == 2:
  171.         ret.append(data[1])
  172.     return "".join(ret)
  173.  
  174. def _parseNameColor(n):
  175.   """This just returns its argument, should return the name color."""
  176.   #probably is already the name
  177.   return n
  178.  
  179. def _parseFont(f):
  180.   """Parses the contents of a f tag and returns color, face and size."""
  181.   #' xSZCOL="FONT"'
  182.   try: #TODO: remove quick hack
  183.     sizecolor, fontface = f.split("=", 1)
  184.     sizecolor = sizecolor.strip()
  185.     size = int(sizecolor[1:3])
  186.     col = sizecolor[3:6]
  187.     if col == "": col = None
  188.     face = f.split("\"", 2)[1]
  189.     return col, face, size
  190.   except:
  191.     return None, None, None
  192.  
  193. ################################################################
  194. # Anon id
  195. ################################################################
  196. def _getAnonId(n, ssid):
  197.   """Gets the anon's id."""
  198.   if n == None: n = "5504"
  199.   try:
  200.     return "".join(list(
  201.       map(lambda x: str(x[0] + x[1])[-1], list(zip(
  202.         list(map(lambda x: int(x), n)),
  203.         list(map(lambda x: int(x), ssid[4:]))
  204.       )))
  205.     ))
  206.   except ValueError:
  207.     return "NNNN"
  208.  
  209. ################################################################
  210. # ANON PM class
  211. ################################################################
  212.  
  213. class _ANON_PM_OBJECT:
  214.   """Manages connection with Chatango anon PM."""
  215.   def __init__(self, mgr, name):
  216.     self._connected = False
  217.     self._mgr = mgr
  218.     self._wlock = False
  219.     self._firstCommand = True
  220.     self._wbuf = b""
  221.     self._wlockbuf = b""
  222.     self._rbuf = b""
  223.     self._pingTask = None
  224.     self._name = name
  225.  
  226.   def _auth(self):
  227.     self._sendCommand("mhs","mini","unknown","%s" % (self._name))
  228.     self._setWriteLock(True)
  229.     return True
  230.  
  231.   def disconnect(self):
  232.     """Disconnect the bot from PM"""
  233.     self._disconnect()
  234.     self._callEvent("onAnonPMDisconnect", User(self._name))
  235.  
  236.   def _disconnect(self):
  237.     self._connected = False
  238.     self._sock.close()
  239.     self._sock = None
  240.  
  241.   def ping(self):
  242.     """send a ping"""
  243.     self._sendCommand("")
  244.     self._callEvent("onPMPing")
  245.  
  246.   def message(self, user, msg):
  247.     """send a pm to a user"""
  248.     if msg!=None:
  249.       self._sendCommand("msg", user.name, msg)
  250.  
  251.   ####
  252.   # Feed
  253.   ####
  254.   def _feed(self, data):
  255.     """
  256.     Feed data to the connection.
  257.  
  258.     @type data: bytes
  259.     @param data: data to be fed
  260.     """
  261.     self._rbuf += data
  262.     while self._rbuf.find(b"\x00") != -1:
  263.       data = self._rbuf.split(b"\x00")
  264.       for food in data[:-1]:
  265.         self._process(food.decode(errors="replace").rstrip("\r\n"))
  266.       self._rbuf = data[-1]
  267.  
  268.   def _process(self, data):
  269.     """
  270.     Process a command string.
  271.  
  272.     @type data: str
  273.     @param data: the command string
  274.     """
  275.     self._callEvent("onRaw", data)
  276.     data = data.split(":")
  277.     cmd, args = data[0], data[1:]
  278.     func = "_rcmd_" + cmd
  279.     if hasattr(self, func):
  280.       getattr(self, func)(args)
  281.     else:
  282.       if debug:
  283.         print("unknown data: "+str(data))
  284.  
  285.   def _getManager(self): return self._mgr
  286.  
  287.   mgr = property(_getManager)
  288.  
  289.   ####
  290.   # Received Commands
  291.   ####
  292.  
  293.   def _rcmd_mhs(self, args):
  294.     """
  295.     note to future maintainers
  296.    
  297.     args[1] is ether "online" or "offline"
  298.     """
  299.     self._connected = True
  300.     self._setWriteLock(False)
  301.  
  302.   def _rcmd_msg(self, args):
  303.     user = User(args[0])
  304.     body = _strip_html(":".join(args[5:]))
  305.     self._callEvent("onPMMessage", user, body)
  306.  
  307.   ####
  308.   # Util
  309.   ####
  310.   def _callEvent(self, evt, *args, **kw):
  311.     getattr(self.mgr, evt)(self, *args, **kw)
  312.     self.mgr.onEventCalled(self, evt, *args, **kw)
  313.  
  314.   def _write(self, data):
  315.     if self._wlock:
  316.       self._wlockbuf += data
  317.     else:
  318.       self.mgr._write(self, data)
  319.  
  320.   def _setWriteLock(self, lock):
  321.     self._wlock = lock
  322.     if self._wlock == False:
  323.       self._write(self._wlockbuf)
  324.       self._wlockbuf = b""
  325.  
  326.   def _sendCommand(self, *args):
  327.     """
  328.     Send a command.
  329.  
  330.     @type args: [str, str, ...]
  331.     @param args: command and list of arguments
  332.     """
  333.     if self._firstCommand:
  334.       terminator = b"\x00"
  335.       self._firstCommand = False
  336.     else:
  337.       terminator = b"\r\n\x00"
  338.     self._write(":".join(args).encode() + terminator)
  339.  
  340. class ANON_PM:
  341.   """Comparable wrapper for anon Chatango PM"""
  342.   ####
  343.   # Init
  344.   ####
  345.   def __init__(self, mgr):
  346.     self._mgr = mgr
  347.     self._wlock = False
  348.     self._firstCommand = True
  349.     self._persons = dict()
  350.     self._wlockbuf = b""
  351.     self._pingTask = None
  352.  
  353.   ####
  354.   # Connections
  355.   ####
  356.   def _connect(self,name):
  357.     self._persons[name] = _ANON_PM_OBJECT(self._mgr,name)
  358.     sock = socket.socket()
  359.     sock.connect((self._mgr._anonPMHost, self._mgr._PMPort))
  360.     sock.setblocking(False)
  361.     self._persons[name]._sock = sock
  362.     if not self._persons[name]._auth(): return
  363.     self._persons[name]._pingTask = self._mgr.setInterval(self._mgr._pingDelay, self._persons[name].ping)
  364.     self._persons[name]._connected = True
  365.  
  366.   def message(self, user, msg):
  367.     """send a pm to a user"""
  368.     if not user.name in self._persons:
  369.       self._connect(user.name)
  370.     self._persons[user.name].message(user,msg)
  371.  
  372.   def getConnections(self):
  373.     return list(self._persons.values())
  374.  
  375. ################################################################
  376. # PM class
  377. ################################################################
  378. class PM:
  379.   """Manages a connection with Chatango PM."""
  380.   ####
  381.   # Init
  382.   ####
  383.   def __init__(self, mgr):
  384.     self._auth_re = re.compile(r"auth\.chatango\.com ?= ?([^;]*)", re.IGNORECASE)
  385.     self._connected = False
  386.     self._mgr = mgr
  387.     self._auid = None
  388.     self._blocklist = set()
  389.     self._contacts = set()
  390.     self._status = dict()
  391.     self._wlock = False
  392.     self._firstCommand = True
  393.     self._wbuf = b""
  394.     self._wlockbuf = b""
  395.     self._rbuf = b""
  396.     self._pingTask = None
  397.     self._connect()
  398.  
  399.   ####
  400.   # Connections
  401.   ####
  402.   def _connect(self):
  403.     self._wbuf = b""
  404.     self._sock = socket.socket()
  405.     self._sock.connect((self._mgr._PMHost, self._mgr._PMPort))
  406.     self._sock.setblocking(False)
  407.     self._firstCommand = True
  408.     if not self._auth(): return
  409.     self._pingTask = self.mgr.setInterval(self._mgr._pingDelay, self.ping)
  410.     self._connected = True
  411.  
  412.  
  413.   def _getAuth(self, name, password):
  414.     """
  415.     Request an auid using name and password.
  416.  
  417.     @type name: str
  418.     @param name: name
  419.     @type password: str
  420.     @param password: password
  421.  
  422.     @rtype: str
  423.     @return: auid
  424.     """
  425.     data = urllib.parse.urlencode({
  426.       "user_id": name,
  427.       "password": password,
  428.       "storecookie": "on",
  429.       "checkerrors": "yes"
  430.     }).encode()
  431.     try:
  432.       resp = urllib.request.urlopen("http://chatango.com/login", data)
  433.       headers = resp.headers
  434.     except Exception:
  435.       return None
  436.     for header, value in headers.items():
  437.       if header.lower() == "set-cookie":
  438.         m = self._auth_re.search(value)
  439.         if m:
  440.           auth = m.group(1)
  441.           if auth == "":
  442.             return None
  443.           return auth
  444.     return None
  445.  
  446.   def _auth(self):
  447.     self._auid = self._getAuth(self._mgr.name, self._mgr.password)
  448.     if self._auid == None:
  449.       self._sock.close()
  450.       self._callEvent("onLoginFail")
  451.       self._sock = None
  452.       return False
  453.     self._sendCommand("tlogin", self._auid, "2")
  454.     self._setWriteLock(True)
  455.     return True
  456.  
  457.   def disconnect(self):
  458.     """Disconnect the bot from PM"""
  459.     self._disconnect()
  460.     self._callEvent("onPMDisconnect")
  461.  
  462.   def _disconnect(self):
  463.     self._connected = False
  464.     self._sock.close()
  465.     self._sock = None
  466.  
  467.   ####
  468.   # Feed
  469.   ####
  470.   def _feed(self, data):
  471.     """
  472.     Feed data to the connection.
  473.  
  474.     @type data: bytes
  475.     @param data: data to be fed
  476.     """
  477.     self._rbuf += data
  478.     while self._rbuf.find(b"\x00") != -1:
  479.       data = self._rbuf.split(b"\x00")
  480.       for food in data[:-1]:
  481.         self._process(food.decode(errors="replace").rstrip("\r\n"))
  482.       self._rbuf = data[-1]
  483.  
  484.   def _process(self, data):
  485.     """
  486.     Process a command string.
  487.  
  488.     @type data: str
  489.     @param data: the command string
  490.     """
  491.     self._callEvent("onRaw", data)
  492.     data = data.split(":")
  493.     cmd, args = data[0], data[1:]
  494.     func = "_rcmd_" + cmd
  495.     if hasattr(self, func):
  496.       getattr(self, func)(args)
  497.     else:
  498.       if debug:
  499.         print("unknown data: "+str(data))
  500.  
  501.   ####
  502.   # Properties
  503.   ####
  504.   def _getManager(self): return self._mgr
  505.   def _getContacts(self): return self._contacts
  506.   def _getBlocklist(self): return self._blocklist
  507.  
  508.   mgr = property(_getManager)
  509.   contacts = property(_getContacts)
  510.   blocklist = property(_getBlocklist)
  511.  
  512.   ####
  513.   # Received Commands
  514.   ####
  515.   def _rcmd_OK(self, args):
  516.     self._setWriteLock(False)
  517.     self._sendCommand("wl")
  518.     self._sendCommand("getblock")
  519.     self._callEvent("onPMConnect")
  520.  
  521.   def _rcmd_wl(self, args):
  522.     self._contacts = set()
  523.     for i in range(len(args) // 4):
  524.       name, last_on, is_on, idle = args[i * 4: i * 4 + 4]
  525.       user = User(name)
  526.       if last_on=="None":pass#in case chatango gives a "None" as data argument
  527.       elif not is_on == "on": self._status[user] = [int(last_on), False, 0]
  528.       elif idle == '0': self._status[user] = [int(last_on), True, 0]
  529.       else: self._status[user] = [int(last_on), True, time.time() - int(idle) * 60]
  530.       self._contacts.add(user)
  531.     self._callEvent("onPMContactlistReceive")
  532.  
  533.   def _rcmd_block_list(self, args):
  534.     self._blocklist = set()
  535.     for name in args:
  536.       if name == "": continue
  537.       self._blocklist.add(User(name))
  538.  
  539.   def _rcmd_idleupdate(self, args):
  540.     user = User(args[0])
  541.     last_on, is_on, idle = self._status[user]
  542.     if args[1] == '1':
  543.       self._status[user] = [last_on, is_on, 0]
  544.     else:
  545.       self._status[user] = [last_on, is_on, time.time()]
  546.  
  547.   def _rcmd_track(self, args):
  548.     user = User(args[0])
  549.     if user in self._status:
  550.       last_on = self._status[user][0]
  551.     else:
  552.       last_on = 0
  553.     if args[1] == '0':
  554.       idle = 0
  555.     else:
  556.       idle = time.time() - int(args[1]) * 60
  557.     if args[2] == "online":
  558.       is_on = True
  559.     else:
  560.       is_on = False
  561.     self._status[user] = [last_on, is_on, idle]
  562.  
  563.   def _rcmd_DENIED(self, args):
  564.     self._disconnect()
  565.     self._callEvent("onLoginFail")
  566.  
  567.   def _rcmd_msg(self, args):
  568.     user = User(args[0])
  569.     body = _strip_html(":".join(args[5:]))
  570.     self._callEvent("onPMMessage", user, body)
  571.  
  572.   def _rcmd_msgoff(self, args):
  573.     user = User(args[0])
  574.     body = _strip_html(":".join(args[5:]))
  575.     self._callEvent("onPMOfflineMessage", user, body)
  576.  
  577.   def _rcmd_wlonline(self, args):
  578.     user = User(args[0])
  579.     last_on = float(args[1])
  580.     self._status[user] = [last_on,True,last_on]
  581.     self._callEvent("onPMContactOnline", user)
  582.  
  583.   def _rcmd_wloffline(self, args):
  584.     user = User(args[0])
  585.     last_on = float(args[1])
  586.     self._status[user] = [last_on,False,0]
  587.     self._callEvent("onPMContactOffline", user)
  588.  
  589.   def _rcmd_kickingoff(self, args):
  590.     self.disconnect()
  591.  
  592.   def _rcmd_toofast(self, args):
  593.     self.disconnect()
  594.  
  595.   def _rcmd_unblocked(self, user):
  596.     """call when successfully unblocked"""
  597.     if user in self._blocklist:
  598.       self._blocklist.remove(user)
  599.       self._callEvent("onPMUnblock", user)
  600.  
  601.  
  602.   ####
  603.   # Commands
  604.   ####
  605.   def ping(self):
  606.     """send a ping"""
  607.     self._sendCommand("")
  608.     self._callEvent("onPMPing")
  609.  
  610.   def message(self, user, msg):
  611.     """send a pm to a user"""
  612.     if msg!=None:
  613.       self._sendCommand("msg", user.name, msg)
  614.  
  615.   def addContact(self, user):
  616.     """add contact"""
  617.     if user not in self._contacts:
  618.       self._sendCommand("wladd", user.name)
  619.       self._contacts.add(user)
  620.       self._callEvent("onPMContactAdd", user)
  621.  
  622.   def removeContact(self, user):
  623.     """remove contact"""
  624.     if user in self._contacts:
  625.       self._sendCommand("wldelete", user.name)
  626.       self._contacts.remove(user)
  627.       self._callEvent("onPMContactRemove", user)
  628.  
  629.   def block(self, user):
  630.     """block a person"""
  631.     if user not in self._blocklist:
  632.       self._sendCommand("block", user.name, user.name, "S")
  633.       self._blocklist.add(user)
  634.       self._callEvent("onPMBlock", user)
  635.  
  636.   def unblock(self, user):
  637.     """unblock a person"""
  638.     if user in self._blocklist:
  639.       self._sendCommand("unblock", user.name)
  640.  
  641.   def track(self, user):
  642.     """get and store status of person for future use"""
  643.     self._sendCommand("track", user.name)
  644.  
  645.   def checkOnline(self, user):
  646.     """return True if online, False if offline, None if unknown"""
  647.     if user in self._status:
  648.       return self._status[user][1]
  649.     else:
  650.       return None
  651.  
  652.   def getIdle(self, user):
  653.     """return last active time, time.time() if isn't idle, 0 if offline, None if unknown"""
  654.     if not user in self._status: return None
  655.     if not self._status[user][1]: return 0
  656.     if not self._status[user][2]: return time.time()
  657.     else: return self._status[user][2]
  658.  
  659.   ####
  660.   # Util
  661.   ####
  662.   def _callEvent(self, evt, *args, **kw):
  663.     getattr(self.mgr, evt)(self, *args, **kw)
  664.     self.mgr.onEventCalled(self, evt, *args, **kw)
  665.  
  666.   def _write(self, data):
  667.     if self._wlock:
  668.       self._wlockbuf += data
  669.     else:
  670.       self.mgr._write(self, data)
  671.  
  672.   def _setWriteLock(self, lock):
  673.     self._wlock = lock
  674.     if self._wlock == False:
  675.       self._write(self._wlockbuf)
  676.       self._wlockbuf = b""
  677.  
  678.   def _sendCommand(self, *args):
  679.     """
  680.     Send a command.
  681.  
  682.     @type args: [str, str, ...]
  683.     @param args: command and list of arguments
  684.     """
  685.     if self._firstCommand:
  686.       terminator = b"\x00"
  687.       self._firstCommand = False
  688.     else:
  689.       terminator = b"\r\n\x00"
  690.     self._write(":".join(args).encode() + terminator)
  691.  
  692.   def getConnections(self):
  693.     return [self]
  694.  
  695. ################################################################
  696. # Room class
  697. ################################################################
  698. class Room:
  699.   """Manages a connection with a Chatango room."""
  700.   ####
  701.   # Init
  702.   ####
  703.   def __init__(self, room, uid = None, server = None, port = None, mgr = None):
  704.     """init, don't overwrite"""
  705.     # Basic stuff
  706.     self._name = room
  707.     self._server = server or getServer(room)
  708.     self._port = port or 443
  709.     self._mgr = mgr
  710.  
  711.     # Under the hood
  712.     self._connected = False
  713.     self._reconnecting = False
  714.     self._uid = uid or _genUid()
  715.     self._rbuf = b""
  716.     self._wbuf = b""
  717.     self._wlockbuf = b""
  718.     self._owner = None
  719.     self._mods = set()
  720.     self._mqueue = dict()
  721.     self._history = list()
  722.     self._userlist = list()
  723.     self._firstCommand = True
  724.     self._connectAmmount = 0
  725.     self._premium = False
  726.     self._userCount = 0
  727.     self._pingTask = None
  728.     self._botname = None
  729.     self._currentname = None
  730.     self._users = dict()
  731.     self._msgs = dict()
  732.     self._wlock = False
  733.     self._silent = False
  734.     self._banlist = dict()
  735.     self._unbanlist = dict()
  736.  
  737.     # Inited vars
  738.     if self._mgr: self._connect()
  739.  
  740.   ####
  741.   # Connect/disconnect
  742.   ####
  743.   def _connect(self):
  744.     """Connect to the server."""
  745.     self._sock = socket.socket()
  746.     self._sock.connect((self._server, self._port))
  747.     self._sock.setblocking(False)
  748.     self._firstCommand = True
  749.     self._wbuf = b""
  750.     self._auth()
  751.     self._pingTask = self.mgr.setInterval(self.mgr._pingDelay, self.ping)
  752.     if not self._reconnecting: self.connected = True
  753.  
  754.   def reconnect(self):
  755.     """Reconnect."""
  756.     self._reconnect()
  757.  
  758.   def _reconnect(self):
  759.     """Reconnect."""
  760.     self._reconnecting = True
  761.     if self.connected:
  762.       self._disconnect()
  763.     self._uid = _genUid()
  764.     self._connect()
  765.     self._reconnecting = False
  766.  
  767.   def disconnect(self):
  768.     """Disconnect."""
  769.     self._disconnect()
  770.     self._callEvent("onDisconnect")
  771.  
  772.   def _disconnect(self):
  773.     """Disconnect from the server."""
  774.     if not self._reconnecting: self.connected = False
  775.     for user in self._userlist:
  776.       user.clearSessionIds(self)
  777.     self._userlist = list()
  778.     self._pingTask.cancel()
  779.     self._sock.close()
  780.     if not self._reconnecting: del self.mgr._rooms[self.name]
  781.  
  782.   def _auth(self):
  783.     """Authenticate."""
  784.     # login as name with password
  785.     if self.mgr.name and self.mgr.password:
  786.       self._sendCommand("bauth", self.name, self._uid, self.mgr.name, self.mgr.password)
  787.       self._currentname = self.mgr.name
  788.     # login as anon
  789.     else:
  790.       self._sendCommand("bauth", self.name)
  791.  
  792.     self._setWriteLock(True)
  793.  
  794.   ####
  795.   # Properties
  796.   ####
  797.   def _getName(self): return self._name
  798.   def _getBotName(self):
  799.     if self.mgr.name and self.mgr.password:
  800.       return self.mgr.name
  801.     elif self.mgr.name and self.mgr.password == None:
  802.       return "#" + self.mgr.name
  803.     elif self.mgr.name == None:
  804.       return self._botname
  805.   def _getCurrentname(self): return self._currentname
  806.   def _getManager(self): return self._mgr
  807.   def _getUserlist(self, mode = None, unique = None, memory = None):
  808.     ul = None
  809.     if mode == None: mode = self.mgr._userlistMode
  810.     if unique == None: unique = self.mgr._userlistUnique
  811.     if memory == None: memory = self.mgr._userlistMemory
  812.     if mode == Userlist_Recent:
  813.       ul = map(lambda x: x.user, self._history[-memory:])
  814.     elif mode == Userlist_All:
  815.       ul = self._userlist
  816.     if unique:
  817.       return list(set(ul))
  818.     else:
  819.       return ul
  820.   def _getUserNames(self):
  821.     ul = self.userlist
  822.     return list(map(lambda x: x.name, ul))
  823.   def _getUser(self): return self.mgr.user
  824.   def _getOwner(self): return self._owner
  825.   def _getOwnerName(self): return self._owner.name
  826.   def _getMods(self):
  827.     newset = set()
  828.     for mod in self._mods:
  829.       newset.add(mod)
  830.     return newset
  831.   def _getModNames(self):
  832.     mods = self._getMods()
  833.     return [x.name for x in mods]
  834.   def _getUserCount(self): return self._userCount
  835.   def _getSilent(self): return self._silent
  836.   def _setSilent(self, val): self._silent = val
  837.   def _getBanlist(self): return list(self._banlist.keys())
  838.   def _getUnBanlist(self): return [[record["target"], record["src"]] for record in self._unbanlist.values()]
  839.  
  840.   name = property(_getName)
  841.   botname = property(_getBotName)
  842.   currentname = property(_getCurrentname)
  843.   mgr = property(_getManager)
  844.   userlist = property(_getUserlist)
  845.   usernames = property(_getUserNames)
  846.   user = property(_getUser)
  847.   owner = property(_getOwner)
  848.   ownername = property(_getOwnerName)
  849.   mods = property(_getMods)
  850.   modnames = property(_getModNames)
  851.   usercount = property(_getUserCount)
  852.   silent = property(_getSilent, _setSilent)
  853.   banlist = property(_getBanlist)
  854.   unbanlist = property(_getUnBanlist)
  855.  
  856.   ####
  857.   # Feed/process
  858.   ####
  859.   def _feed(self, data):
  860.     """
  861.     Feed data to the connection.
  862.  
  863.     @type data: bytes
  864.     @param data: data to be fed
  865.     """
  866.     self._rbuf += data
  867.     while self._rbuf.find(b"\x00") != -1:
  868.       data = self._rbuf.split(b"\x00")
  869.       for food in data[:-1]:
  870.         self._process(food.decode(errors="replace").rstrip("\r\n"))
  871.       self._rbuf = data[-1]
  872.  
  873.   def _process(self, data):
  874.     """
  875.     Process a command string.
  876.    
  877.     @type data: str
  878.     @param data: the command string
  879.     """
  880.     self._callEvent("onRaw", data)
  881.     data = data.split(":")
  882.     cmd, args = data[0], data[1:]
  883.     func = "_rcmd_" + cmd
  884.     if hasattr(self, func):
  885.       getattr(self, func)(args)
  886.     else:
  887.       if debug:
  888.         print("unknown data: "+str(data))
  889.  
  890.   ####
  891.   # Received Commands
  892.   ####
  893.   def _rcmd_ok(self, args):
  894.     # if no name, join room as anon and no password
  895.     if args[2] == "N" and self.mgr.password == None and self.mgr.name == None:
  896.       n = args[4].rsplit('.', 1)[0]
  897.       n = n[-4:]
  898.       aid = args[1][0:8]
  899.       pid = "!anon" + _getAnonId(n, aid)
  900.       self._botname = pid
  901.       self._currentname = pid
  902.       self.user._nameColor = n
  903.     # if got name, join room as name and no password
  904.     elif args[2] == "N" and self.mgr.password == None:
  905.       self._sendCommand("blogin", self.mgr.name)
  906.       self._currentname = self.mgr.name
  907.     # if got password but fail to login
  908.     elif args[2] != "M": #unsuccesful login
  909.       self._callEvent("onLoginFail")
  910.       self.disconnect()
  911.     self._owner = User(args[0])
  912.     self._uid = args[1]
  913.     self._aid = args[1][4:8]
  914.     self._mods = set(map(lambda x: User(x.split(",")[0]), args[6].split(";")))
  915.     self._i_log = list()
  916.  
  917.   def _rcmd_denied(self, args):
  918.     self._disconnect()
  919.     self._callEvent("onConnectFail")
  920.  
  921.   def _rcmd_inited(self, args):
  922.     self._sendCommand("g_participants", "start")
  923.     self._sendCommand("getpremium", "1")
  924.     self.requestBanlist()
  925.     self.requestUnBanlist()
  926.     if self._connectAmmount == 0:
  927.       self._callEvent("onConnect")
  928.       for msg in reversed(self._i_log):
  929.         user = msg.user
  930.         self._callEvent("onHistoryMessage", user, msg)
  931.         self._addHistory(msg)
  932.       del self._i_log
  933.     else:
  934.       self._callEvent("onReconnect")
  935.     self._connectAmmount += 1
  936.     self._setWriteLock(False)
  937.  
  938.   def _rcmd_premium(self, args):
  939.     if float(args[1]) > time.time():
  940.       self._premium = True
  941.       if self.user._mbg: self.setBgMode(1)
  942.       if self.user._mrec: self.setRecordingMode(1)
  943.     else:
  944.       self._premium = False
  945.  
  946.   def _rcmd_mods(self, args):
  947.     modnames = args
  948.     mods = set(map(lambda x: User(x.split(",")[0]), modnames))
  949.     premods = self._mods
  950.     for user in mods - premods: #modded
  951.       self._mods.add(user)
  952.       self._callEvent("onModAdd", user)
  953.     for user in premods - mods: #demodded
  954.       self._mods.remove(user)
  955.       self._callEvent("onModRemove", user)
  956.     self._callEvent("onModChange")
  957.  
  958.   def _rcmd_b(self, args):
  959.     mtime = float(args[0])
  960.     puid = args[3]
  961.     ip = args[6]
  962.     name = args[1]
  963.     rawmsg = ":".join(args[9:])
  964.     msg, n, f = _clean_message(rawmsg)
  965.     if name == "":
  966.       nameColor = None
  967.       name = "#" + args[2]
  968.       if name == "#":
  969.         name = "!anon" + _getAnonId(n, puid)
  970.     else:
  971.       if n: nameColor = _parseNameColor(n)
  972.       else: nameColor = None
  973.     i = args[5]
  974.     unid = args[4]
  975.     user = User(name)
  976.     if puid:
  977.       user.updatePuid(puid)
  978.     #Create an anonymous message and queue it because msgid is unknown.
  979.     if f: fontColor, fontFace, fontSize = _parseFont(f)
  980.     else: fontColor, fontFace, fontSize = None, None, None
  981.     msg = Message(
  982.       time = mtime,
  983.       user = user,
  984.       body = msg,
  985.       raw = rawmsg,
  986.       ip = ip,
  987.       nameColor = nameColor,
  988.       fontColor = fontColor,
  989.       fontFace = fontFace,
  990.       fontSize = fontSize,
  991.       unid = unid,
  992.       room = self
  993.     )
  994.     self._mqueue[i] = msg
  995.  
  996.   def _rcmd_u(self, args):
  997.     temp = Struct(**self._mqueue)
  998.     if hasattr(temp, args[0]):
  999.       msg = getattr(temp, args[0])
  1000.       if msg.user != self.user:
  1001.         msg.user._fontColor = msg.fontColor
  1002.         msg.user._fontFace = msg.fontFace
  1003.         msg.user._fontSize = msg.fontSize
  1004.         msg.user._nameColor = msg.nameColor
  1005.       del self._mqueue[args[0]]
  1006.       msg.attach(self, args[1])
  1007.       self._addHistory(msg)
  1008.       self._callEvent("onMessage", msg.user, msg)
  1009.  
  1010.   def _rcmd_i(self, args):
  1011.     mtime = float(args[0])
  1012.     puid = args[3]
  1013.     ip = args[6]
  1014.     name = args[1]
  1015.     rawmsg = ":".join(args[9:])
  1016.     msg, n, f = _clean_message(rawmsg)
  1017.     if name == "":
  1018.       nameColor = None
  1019.       name = "#" + args[2]
  1020.       if name == "#":
  1021.         name = "!anon" + _getAnonId(n, puid)
  1022.     else:
  1023.       if n: nameColor = _parseNameColor(n)
  1024.       else: nameColor = None
  1025.     i = args[5]
  1026.     unid = args[4]
  1027.     user = User(name)
  1028.     if puid:
  1029.       user.updatePuid(puid)
  1030.     #Create an anonymous message and queue it because msgid is unknown.
  1031.     if f: fontColor, fontFace, fontSize = _parseFont(f)
  1032.     else: fontColor, fontFace, fontSize = None, None, None
  1033.     msg = Message(
  1034.       time = mtime,
  1035.       user = user,
  1036.       body = msg,
  1037.       raw = rawmsg,
  1038.       ip = ip,
  1039.       nameColor = nameColor,
  1040.       fontColor = fontColor,
  1041.       fontFace = fontFace,
  1042.       fontSize = fontSize,
  1043.       unid = unid,
  1044.       room = self
  1045.     )
  1046.     self._i_log.append(msg)
  1047.  
  1048.   def _rcmd_g_participants(self, args):
  1049.     args = ":".join(args)
  1050.     args = args.split(";")
  1051.     for data in args:
  1052.       data = data.split(":")
  1053.       name = data[3].lower()
  1054.       if name == "none": continue
  1055.       user = User(
  1056.         name = name,
  1057.         room = self
  1058.       )
  1059.       user.addSessionId(self, data[0])
  1060.       self._userlist.append(user)
  1061.  
  1062.   def _rcmd_participant(self, args):
  1063.     name = args[3].lower()
  1064.     if name == "none": return
  1065.     user = User(name)
  1066.     puid = args[2]
  1067.     if puid:
  1068.       user.updatePuid(puid)
  1069.  
  1070.     if args[0] == "0": #leave
  1071.       user.removeSessionId(self, args[1])
  1072.       self._userlist.remove(user)
  1073.       if user not in self._userlist or not self.mgr._userlistEventUnique:
  1074.         self._callEvent("onLeave", user)
  1075.     else: #join
  1076.       user.addSessionId(self, args[1])
  1077.       if user not in self._userlist: doEvent = True
  1078.       else: doEvent = False
  1079.       self._userlist.append(user)
  1080.       if doEvent or not self.mgr._userlistEventUnique:
  1081.         self._callEvent("onJoin", user)
  1082.  
  1083.   def _rcmd_show_fw(self, args):
  1084.     self._callEvent("onFloodWarning")
  1085.  
  1086.   def _rcmd_show_tb(self, args):
  1087.     self._callEvent("onFloodBan")
  1088.  
  1089.   def _rcmd_tb(self, args):
  1090.     self._callEvent("onFloodBanRepeat")
  1091.  
  1092.   def _rcmd_delete(self, args):
  1093.     msg = self._msgs.get(args[0])
  1094.     if msg:
  1095.       if msg in self._history:
  1096.         self._history.remove(msg)
  1097.         self._callEvent("onMessageDelete", msg.user, msg)
  1098.         msg.detach()
  1099.  
  1100.   def _rcmd_deleteall(self, args):
  1101.     for msgid in args:
  1102.       self._rcmd_delete([msgid])
  1103.  
  1104.   def _rcmd_n(self, args):
  1105.     self._userCount = int(args[0], 16)
  1106.     self._callEvent("onUserCountChange")
  1107.  
  1108.   def _rcmd_blocklist(self, args):
  1109.     self._banlist = dict()
  1110.     sections = ":".join(args).split(";")
  1111.     for section in sections:
  1112.       params = section.split(":")
  1113.       if len(params) != 5: continue
  1114.       if params[2] == "": continue
  1115.       user = User(params[2])
  1116.       self._banlist[user] = {
  1117.         "unid":params[0],
  1118.         "ip":params[1],
  1119.         "target":user,
  1120.         "time":float(params[3]),
  1121.         "src":User(params[4])
  1122.       }
  1123.     self._callEvent("onBanlistUpdate")
  1124.  
  1125.   def _rcmd_unblocklist(self, args):
  1126.     self._unbanlist = dict()
  1127.     sections = ":".join(args).split(";")
  1128.     for section in sections:
  1129.       params = section.split(":")
  1130.       if len(params) != 5: continue
  1131.       if params[2] == "": continue
  1132.       user = User(params[2])
  1133.       self._unbanlist[user] = {
  1134.         "unid":params[0],
  1135.         "ip":params[1],
  1136.         "target":user,
  1137.         "time":float(params[3]),
  1138.         "src":User(params[4])
  1139.       }
  1140.     self._callEvent("onUnBanlistUpdate")
  1141.  
  1142.   def _rcmd_blocked(self, args):
  1143.     if args[2] == "": return
  1144.     target = User(args[2])
  1145.     user = User(args[3])
  1146.     self._banlist[target] = {"unid":args[0], "ip":args[1], "target":target, "time":float(args[4]), "src":user}
  1147.     self._callEvent("onBan", user, target)
  1148.  
  1149.   def _rcmd_unblocked(self, args):
  1150.     if args[2] == "": return
  1151.     target = User(args[2])
  1152.     user=User(args[3])
  1153.     del self._banlist[target]
  1154.     self._unbanlist[user] = {"unid":args[0], "ip":args[1], "target":target, "time":float(args[4]), "src":user}
  1155.     self._callEvent("onUnban", user, target)
  1156.  
  1157.   ####
  1158.   # Commands
  1159.   ####
  1160.   def login(self, NAME, PASS = None):
  1161.     """login as a user or set a name in room"""
  1162.     if PASS:
  1163.       self._sendCommand("blogin", NAME, PASS)
  1164.     else:
  1165.       self._sendCommand("blogin", NAME)
  1166.     self._currentname = NAME
  1167.  
  1168.   def logout(self):
  1169.     """logout of user in a room"""
  1170.     self._sendCommand("blogout")
  1171.     self._currentname = self._botname
  1172.  
  1173.   def ping(self):
  1174.     """Send a ping."""
  1175.     self._sendCommand("")
  1176.     self._callEvent("onPing")
  1177.  
  1178.   def rawMessage(self, msg):
  1179.     """
  1180.     Send a message without n and f tags.
  1181.  
  1182.     @type msg: str
  1183.     @param msg: message
  1184.     """
  1185.     if not self._silent:
  1186.       self._sendCommand("bmsg:tl2r", msg)
  1187.  
  1188.   def message(self, msg, html = False):
  1189.     """
  1190.     Send a message. (Use "\n" for new line)
  1191.  
  1192.     @type msg: str
  1193.     @param msg: message
  1194.     """
  1195.     if msg==None:
  1196.       return
  1197.     msg = msg.rstrip()
  1198.     if not html:
  1199.       msg = msg.replace("<", "&lt;").replace(">", "&gt;")
  1200.     if len(msg) > self.mgr._maxLength:
  1201.       if self.mgr._tooBigMessage == BigMessage_Cut:
  1202.         self.message(msg[:self.mgr._maxLength], html = html)
  1203.       elif self.mgr._tooBigMessage == BigMessage_Multiple:
  1204.         while len(msg) > 0:
  1205.           sect = msg[:self.mgr._maxLength]
  1206.           msg = msg[self.mgr._maxLength:]
  1207.           self.message(sect, html = html)
  1208.       return
  1209.     msg = "<n" + self.user.nameColor + "/>" + msg
  1210.     if self._currentname != None and not self._currentname.startswith("!anon"):
  1211.       font_properties = "<f x%0.2i%s=\"%s\">" %(self.user.fontSize, self.user.fontColor, self.user.fontFace)
  1212.       if "\n" in msg:
  1213.         msg.replace("\n", "</f></p><p>%s" %(font_properties))
  1214.       msg = font_properties + msg
  1215.     msg.replace("~","&#126;")
  1216.     self.rawMessage(msg)
  1217.  
  1218.   def setBgMode(self, mode):
  1219.     """turn on/off bg"""
  1220.     self._sendCommand("msgbg", str(mode))
  1221.  
  1222.   def setRecordingMode(self, mode):
  1223.     """turn on/off rcecording"""
  1224.     self._sendCommand("msgmedia", str(mode))
  1225.  
  1226.   def addMod(self, user):
  1227.     """
  1228.     Add a moderator.
  1229.  
  1230.     @type user: User
  1231.     @param user: User to mod.
  1232.     """
  1233.     if self.getLevel(User(self.currentname)) == 2:
  1234.       self._sendCommand("addmod", user.name)
  1235.  
  1236.   def removeMod(self, user):
  1237.     """
  1238.     Remove a moderator.
  1239.  
  1240.     @type user: User
  1241.     @param user: User to demod.
  1242.     """
  1243.     if self.getLevel(User(self.currentname)) == 2:
  1244.       self._sendCommand("removemod", user.name)
  1245.  
  1246.   def flag(self, message):
  1247.     """
  1248.     Flag a message.
  1249.  
  1250.     @type message: Message
  1251.     @param message: message to flag
  1252.     """
  1253.     self._sendCommand("g_flag", message.msgid)
  1254.  
  1255.   def flagUser(self, user):
  1256.     """
  1257.     Flag a user.
  1258.  
  1259.     @type user: User
  1260.     @param user: user to flag
  1261.  
  1262.     @rtype: bool
  1263.     @return: whether a message to flag was found
  1264.     """
  1265.     msg = self.getLastMessage(user)
  1266.     if msg:
  1267.       self.flag(msg)
  1268.       return True
  1269.     return False
  1270.  
  1271.   def deleteMessage(self, message):
  1272.     """
  1273.     Delete a message. (Moderator only)
  1274.  
  1275.     @type message: Message
  1276.     @param message: message to delete
  1277.     """
  1278.     if self.getLevel(self.user) > 0:
  1279.       self._sendCommand("delmsg", message.msgid)
  1280.  
  1281.   def deleteUser(self, user):
  1282.     """
  1283.     Delete a message. (Moderator only)
  1284.  
  1285.     @type message: User
  1286.     @param message: delete user's last message
  1287.     """
  1288.     if self.getLevel(self.user) > 0:
  1289.       msg = self.getLastMessage(user)
  1290.       if msg:
  1291.         self._sendCommand("delmsg", msg.msgid)
  1292.       return True
  1293.     return False
  1294.  
  1295.   def delete(self, user):
  1296.     """
  1297.     compatibility wrapper for deleteUser
  1298.     """
  1299.     print("[obsolete] the delete function is obsolete, please use deleteUser")
  1300.     return self.deleteUser(user)
  1301.  
  1302.   def rawClearUser(self, unid, ip, user):
  1303.     self._sendCommand("delallmsg", unid, ip, user)
  1304.   def clearUser(self, user):
  1305.     """
  1306.     Clear all of a user's messages. (Moderator only)
  1307.  
  1308.     @type user: User
  1309.     @param user: user to delete messages of
  1310.  
  1311.     @rtype: bool
  1312.     @return: whether a message to delete was found
  1313.     """
  1314.     if self.getLevel(self.user) > 0:
  1315.       msg = self.getLastMessage(user)
  1316.       if msg:
  1317.         if msg.user.name[0] in ["!","#"]:self.rawClearUser(msg.unid, msg.ip,"")
  1318.         else:self.rawClearUser(msg.unid,msg.ip,msg.user.name)
  1319.         return True
  1320.     return False
  1321.  
  1322.   def clearall(self):
  1323.     """Clear all messages. (Owner only)"""
  1324.     if self.getLevel(self.user) == 2:
  1325.       self._sendCommand("clearall")
  1326.  
  1327.   def rawBan(self, name, ip, unid):
  1328.     """
  1329.     Execute the block command using specified arguments.
  1330.     (For advanced usage)
  1331.  
  1332.     @type name: str
  1333.     @param name: name
  1334.     @type ip: str
  1335.     @param ip: ip address
  1336.     @type unid: str
  1337.     @param unid: unid
  1338.     """
  1339.     self._sendCommand("block", unid, ip, name)
  1340.  
  1341.   def ban(self, msg):
  1342.     """
  1343.     Ban a message's sender. (Moderator only)
  1344.  
  1345.     @type message: Message
  1346.     @param message: message to ban sender of
  1347.     """
  1348.     if self.getLevel(self.user) > 0:
  1349.       self.rawBan(msg.user.name, msg.ip, msg.unid)
  1350.  
  1351.   def banUser(self, user):
  1352.     """
  1353.     Ban a user. (Moderator only)
  1354.  
  1355.     @type user: User
  1356.     @param user: user to ban
  1357.  
  1358.     @rtype: bool
  1359.     @return: whether a message to ban the user was found
  1360.     """
  1361.     msg = self.getLastMessage(user)
  1362.     if msg:
  1363.       self.ban(msg)
  1364.       return True
  1365.     return False
  1366.  
  1367.   def requestBanlist(self):
  1368.     """Request an updated banlist."""
  1369.     self._sendCommand("blocklist", "block", "", "next", "500")
  1370.  
  1371.   def requestUnBanlist(self):
  1372.     """Request an updated banlist."""
  1373.     self._sendCommand("blocklist", "unblock", "", "next", "500")
  1374.  
  1375.   def rawUnban(self, name, ip, unid):
  1376.     """
  1377.     Execute the unblock command using specified arguments.
  1378.     (For advanced usage)
  1379.  
  1380.     @type name: str
  1381.     @param name: name
  1382.     @type ip: str
  1383.     @param ip: ip address
  1384.     @type unid: str
  1385.     @param unid: unid
  1386.     """
  1387.     self._sendCommand("removeblock", unid, ip, name)
  1388.  
  1389.   def unban(self, user):
  1390.     """
  1391.     Unban a user. (Moderator only)
  1392.  
  1393.     @type user: User
  1394.     @param user: user to unban
  1395.  
  1396.     @rtype: bool
  1397.     @return: whether it succeeded
  1398.     """
  1399.     rec = self._getBanRecord(user)
  1400.     if rec:
  1401.       self.rawUnban(rec["target"].name, rec["ip"], rec["unid"])
  1402.       return True
  1403.     else:
  1404.       return False
  1405.  
  1406.   ####
  1407.   # Util
  1408.   ####
  1409.   def _getBanRecord(self, user):
  1410.     if user in self._banlist:
  1411.       return self._banlist[user]
  1412.     return None
  1413.  
  1414.   def _callEvent(self, evt, *args, **kw):
  1415.     getattr(self.mgr, evt)(self, *args, **kw)
  1416.     self.mgr.onEventCalled(self, evt, *args, **kw)
  1417.  
  1418.   def _write(self, data):
  1419.     if self._wlock:
  1420.       self._wlockbuf += data
  1421.     else:
  1422.       self.mgr._write(self, data)
  1423.  
  1424.   def _setWriteLock(self, lock):
  1425.     self._wlock = lock
  1426.     if self._wlock == False:
  1427.       self._write(self._wlockbuf)
  1428.       self._wlockbuf = b""
  1429.  
  1430.   def _sendCommand(self, *args):
  1431.     """
  1432.     Send a command.
  1433.  
  1434.     @type args: [str, str, ...]
  1435.     @param args: command and list of arguments
  1436.     """
  1437.     if self._firstCommand:
  1438.       terminator = b"\x00"
  1439.       self._firstCommand = False
  1440.     else:
  1441.       terminator = b"\r\n\x00"
  1442.     self._write(":".join(args).encode() + terminator)
  1443.  
  1444.   def getLevel(self, user):
  1445.     """get the level of user in a room"""
  1446.     if user == self._owner: return 2
  1447.     if user.name in self.modnames: return 1
  1448.     return 0
  1449.  
  1450.   def getLastMessage(self, user = None):
  1451.     """get last message said by user in a room"""
  1452.     if user:
  1453.       try:
  1454.         i = 1
  1455.         while True:
  1456.           msg = self._history[-i]
  1457.           if msg.user == user:
  1458.             return msg
  1459.           i += 1
  1460.       except IndexError:
  1461.         return None
  1462.     else:
  1463.       try:
  1464.         return self._history[-1]
  1465.       except IndexError:
  1466.         return None
  1467.     return None
  1468.  
  1469.   def findUser(self, name):
  1470.     """check if user is in the room
  1471.    
  1472.     return User(name) if name in room else None"""
  1473.     name = name.lower()
  1474.     ul = self._getUserlist()
  1475.     udi = dict(zip([u.name for u in ul], ul))
  1476.     cname = None
  1477.     for n in udi.keys():
  1478.       if name in n:
  1479.         if cname: return None #ambiguous!!
  1480.         cname = n
  1481.     if cname: return udi[cname]
  1482.     else: return None
  1483.  
  1484.   ####
  1485.   # History
  1486.   ####
  1487.   def _addHistory(self, msg):
  1488.     """
  1489.     Add a message to history.
  1490.  
  1491.     @type msg: Message
  1492.     @param msg: message
  1493.     """
  1494.     self._history.append(msg)
  1495.     if len(self._history) > self.mgr._maxHistoryLength:
  1496.       rest, self._history = self._history[:-self.mgr._maxHistoryLength], self._history[-self.mgr._maxHistoryLength:]
  1497.       for msg in rest: msg.detach()
  1498.  
  1499. ################################################################
  1500. # RoomManager class
  1501. ################################################################
  1502. class RoomManager:
  1503.   """Class that manages multiple connections."""
  1504.   ####
  1505.   # Config
  1506.   ####
  1507.   _Room = Room
  1508.   _PM = PM
  1509.   _ANON_PM = ANON_PM
  1510.   _anonPMHost = "b1.chatango.com"
  1511.   _PMHost = "c1.chatango.com"
  1512.   _PMPort = 5222
  1513.   _TimerResolution = 0.2 #at least x times per second
  1514.   _pingDelay = 20
  1515.   _userlistMode = Userlist_Recent
  1516.   _userlistUnique = True
  1517.   _userlistMemory = 50
  1518.   _userlistEventUnique = False
  1519.   _tooBigMessage = BigMessage_Multiple
  1520.   _maxLength = 1800
  1521.   _maxHistoryLength = 150
  1522.  
  1523.   ####
  1524.   # Init
  1525.   ####
  1526.   def __init__(self, name = None, password = None, pm = True):
  1527.     self._name = name
  1528.     self._password = password
  1529.     self._running = False
  1530.     self._tasks = set()
  1531.     self._rooms = dict()
  1532.     self._rooms_queue = queue.Queue()
  1533.     self._rooms_lock = threading.Lock()
  1534.     if pm:
  1535.       if self._password:
  1536.         self._pm = self._PM(mgr = self)
  1537.       else:
  1538.         self._pm = self._ANON_PM(mgr = self)
  1539.     else:
  1540.       self._pm = None
  1541.  
  1542.   def _joinThread(self):
  1543.     while True:
  1544.       room = self._rooms_queue.get()
  1545.       with self._rooms_lock:
  1546.         con = self._Room(room, mgr = self)
  1547.         self._rooms[room] = con
  1548.  
  1549.   ####
  1550.   # Join/leave
  1551.   ####
  1552.   def joinRoom(self, room):
  1553.     """
  1554.     Join a room or return None if already joined.
  1555.  
  1556.     @type room: str
  1557.     @param room: room to join
  1558.  
  1559.     @rtype: Room or None
  1560.     @return: True or nothing
  1561.     """
  1562.     room = room.lower()
  1563.     if room not in self._rooms:
  1564.       self._rooms_queue.put(room)
  1565.       return True
  1566.     else:
  1567.       return None
  1568.  
  1569.   def leaveRoom(self, room):
  1570.     """
  1571.     Leave a room.
  1572.  
  1573.     @type room: str
  1574.     @param room: room to leave
  1575.     """
  1576.     room = room.lower()
  1577.     if room in self._rooms:
  1578.       with self._rooms_lock:
  1579.         con = self._rooms[room]
  1580.         con.disconnect()
  1581.  
  1582.   def getRoom(self, room):
  1583.     """
  1584.     Get room with a name, or None if not connected to this room.
  1585.  
  1586.     @type room: str
  1587.     @param room: room
  1588.  
  1589.     @rtype: Room
  1590.     @return: the room
  1591.     """
  1592.     room = room.lower()
  1593.     if room in self._rooms:
  1594.       return self._rooms[room]
  1595.     else:
  1596.       return None
  1597.  
  1598.   ####
  1599.   # Properties
  1600.   ####
  1601.   def _getUser(self): return User(self._name)
  1602.   def _getName(self): return self._name
  1603.   def _getPassword(self): return self._password
  1604.   def _getRooms(self): return set(self._rooms.values())
  1605.   def _getRoomNames(self): return set(self._rooms.keys())
  1606.   def _getPM(self): return self._pm
  1607.  
  1608.   user = property(_getUser)
  1609.   name = property(_getName)
  1610.   password = property(_getPassword)
  1611.   rooms = property(_getRooms)
  1612.   roomnames = property(_getRoomNames)
  1613.   pm = property(_getPM)
  1614.  
  1615.   ####
  1616.   # Virtual methods
  1617.   ####
  1618.   def onInit(self):
  1619.     """Called on init."""
  1620.     pass
  1621.  
  1622.   def safePrint(self, text):
  1623.     """Use this to safely print text with unicode"""
  1624.     while True:
  1625.       try:
  1626.         print(text)
  1627.         break
  1628.       except UnicodeEncodeError as ex:
  1629.         text = (text[0:ex.start]+'(unicode)'+text[ex.end:])
  1630.  
  1631.   def onConnect(self, room):
  1632.     """
  1633.     Called when connected to the room.
  1634.    
  1635.     @type room: Room
  1636.     @param room: room where the event occured
  1637.     """
  1638.     pass
  1639.  
  1640.   def onReconnect(self, room):
  1641.     """
  1642.     Called when reconnected to the room.
  1643.    
  1644.     @type room: Room
  1645.     @param room: room where the event occured
  1646.     """
  1647.     pass
  1648.  
  1649.   def onConnectFail(self, room):
  1650.     """
  1651.     Called when the connection failed.
  1652.    
  1653.     @type room: Room
  1654.     @param room: room where the event occured
  1655.     """
  1656.     pass
  1657.  
  1658.   def onDisconnect(self, room):
  1659.     """
  1660.     Called when the client gets disconnected.
  1661.    
  1662.     @type room: Room
  1663.     @param room: room where the event occured
  1664.     """
  1665.     pass
  1666.  
  1667.   def onLoginFail(self, room):
  1668.     """
  1669.     Called on login failure, disconnects after.
  1670.    
  1671.     @type room: Room
  1672.     @param room: room where the event occured
  1673.     """
  1674.     pass
  1675.  
  1676.   def onFloodBan(self, room):
  1677.     """
  1678.     Called when either flood banned or flagged.
  1679.    
  1680.     @type room: Room
  1681.     @param room: room where the event occured
  1682.     """
  1683.     pass
  1684.  
  1685.   def onFloodBanRepeat(self, room):
  1686.     """
  1687.     Called when trying to send something when floodbanned.
  1688.  
  1689.     @type room: Room
  1690.     @param room: room where the event occured
  1691.     """
  1692.     pass
  1693.  
  1694.   def onFloodWarning(self, room):
  1695.     """
  1696.     Called when an overflow warning gets received.
  1697.  
  1698.     @type room: Room
  1699.     @param room: room where the event occured
  1700.     """
  1701.     pass
  1702.  
  1703.   def onMessageDelete(self, room, user, message):
  1704.     """
  1705.     Called when a message gets deleted.
  1706.  
  1707.     @type room: Room
  1708.     @param room: room where the event occured
  1709.     @type user: User
  1710.     @param user: owner of deleted message
  1711.     @type message: Message
  1712.     @param message: message that got deleted
  1713.     """
  1714.     pass
  1715.  
  1716.   def onModChange(self, room):
  1717.     """
  1718.     Called when the moderator list changes.
  1719.  
  1720.     @type room: Room
  1721.     @param room: room where the event occured
  1722.     """
  1723.     pass
  1724.  
  1725.   def onModAdd(self, room, user):
  1726.     """
  1727.     Called when a moderator gets added.
  1728.  
  1729.     @type room: Room
  1730.     @param room: room where the event occured
  1731.     """
  1732.     pass
  1733.  
  1734.   def onModRemove(self, room, user):
  1735.     """
  1736.     Called when a moderator gets removed.
  1737.  
  1738.     @type room: Room
  1739.     @param room: room where the event occured
  1740.     """
  1741.     pass
  1742.  
  1743.   def onMessage(self, room, user, message):
  1744.     """
  1745.     Called when a message gets received.
  1746.  
  1747.     @type room: Room
  1748.     @param room: room where the event occured
  1749.     @type user: User
  1750.     @param user: owner of message
  1751.     @type message: Message
  1752.     @param message: received message
  1753.     """
  1754.     pass
  1755.  
  1756.   def onHistoryMessage(self, room, user, message):
  1757.     """
  1758.     Called when a message gets received from history.
  1759.  
  1760.     @type room: Room
  1761.     @param room: room where the event occured
  1762.     @type user: User
  1763.     @param user: owner of message
  1764.     @type message: Message
  1765.     @param message: the message that got added
  1766.     """
  1767.     pass
  1768.  
  1769.   def onJoin(self, room, user):
  1770.     """
  1771.     Called when a user joins. Anonymous users get ignored here.
  1772.  
  1773.     @type room: Room
  1774.     @param room: room where the event occured
  1775.     @type user: User
  1776.     @param user: the user that has joined
  1777.     """
  1778.     pass
  1779.  
  1780.   def onLeave(self, room, user):
  1781.     """
  1782.     Called when a user leaves. Anonymous users get ignored here.
  1783.  
  1784.     @type room: Room
  1785.     @param room: room where the event occured
  1786.     @type user: User
  1787.     @param user: the user that has left
  1788.     """
  1789.     pass
  1790.  
  1791.   def onRaw(self, room, raw):
  1792.     """
  1793.     Called before any command parsing occurs.
  1794.  
  1795.     @type room: Room
  1796.     @param room: room where the event occured
  1797.     @type raw: str
  1798.     @param raw: raw command data
  1799.     """
  1800.     pass
  1801.  
  1802.   def onPing(self, room):
  1803.     """
  1804.     Called when a ping gets sent.
  1805.  
  1806.     @type room: Room
  1807.     @param room: room where the event occured
  1808.     """
  1809.     pass
  1810.  
  1811.   def onUserCountChange(self, room):
  1812.     """
  1813.     Called when the user count changes.
  1814.  
  1815.     @type room: Room
  1816.     @param room: room where the event occured
  1817.     """
  1818.     pass
  1819.  
  1820.   def onBan(self, room, user, target):
  1821.     """
  1822.     Called when a user gets banned.
  1823.  
  1824.     @type room: Room
  1825.     @param room: room where the event occured
  1826.     @type user: User
  1827.     @param user: user that banned someone
  1828.     @type target: User
  1829.     @param target: user that got banned
  1830.     """
  1831.     pass
  1832.  
  1833.   def onUnban(self, room, user, target):
  1834.     """
  1835.     Called when a user gets unbanned.
  1836.  
  1837.     @type room: Room
  1838.     @param room: room where the event occured
  1839.     @type user: User
  1840.     @param user: user that unbanned someone
  1841.     @type target: User
  1842.     @param target: user that got unbanned
  1843.     """
  1844.     pass
  1845.  
  1846.   def onBanlistUpdate(self, room):
  1847.     """
  1848.     Called when a banlist gets updated.
  1849.  
  1850.     @type room: Room
  1851.     @param room: room where the event occured
  1852.     """
  1853.     pass
  1854.  
  1855.   def onUnBanlistUpdate(self, room):
  1856.     """
  1857.     Called when a unbanlist gets updated.
  1858.  
  1859.     @type room: Room
  1860.     @param room: room where the event occured
  1861.     """
  1862.     pass
  1863.  
  1864.   def onPMConnect(self, pm):
  1865.     """
  1866.     Called when connected to the pm
  1867.    
  1868.     @type pm: PM
  1869.     @param pm: the pm
  1870.     """
  1871.     pass
  1872.  
  1873.   def onAnonPMDisconnect(self, pm, user):
  1874.     """
  1875.     Called when disconnected from the pm
  1876.    
  1877.     @type pm: PM
  1878.     @param pm: the pm
  1879.     """
  1880.     pass
  1881.  
  1882.   def onPMDisconnect(self, pm):
  1883.     """
  1884.     Called when disconnected from the pm
  1885.    
  1886.     @type pm: PM
  1887.     @param pm: the pm
  1888.     """
  1889.     pass
  1890.  
  1891.   def onPMPing(self, pm):
  1892.     """
  1893.     Called when sending a ping to the pm
  1894.    
  1895.     @type pm: PM
  1896.     @param pm: the pm
  1897.     """
  1898.     pass
  1899.  
  1900.   def onPMMessage(self, pm, user, body):
  1901.     """
  1902.     Called when a message is received
  1903.    
  1904.     @type pm: PM
  1905.     @param pm: the pm
  1906.     @type user: User
  1907.     @param user: owner of message
  1908.     @type message: Message
  1909.     @param message: received message
  1910.     """
  1911.     pass
  1912.  
  1913.   def onPMOfflineMessage(self, pm, user, body):
  1914.     """
  1915.     Called when connected if a message is received while offline
  1916.    
  1917.     @type pm: PM
  1918.     @param pm: the pm
  1919.     @type user: User
  1920.     @param user: owner of message
  1921.     @type message: Message
  1922.     @param message: received message
  1923.     """
  1924.     pass
  1925.  
  1926.   def onPMContactlistReceive(self, pm):
  1927.     """
  1928.     Called when the contact list is received
  1929.    
  1930.     @type pm: PM
  1931.     @param pm: the pm
  1932.     """
  1933.     pass
  1934.  
  1935.   def onPMBlocklistReceive(self, pm):
  1936.     """
  1937.     Called when the block list is received
  1938.    
  1939.     @type pm: PM
  1940.     @param pm: the pm
  1941.     """
  1942.     pass
  1943.  
  1944.   def onPMContactAdd(self, pm, user):
  1945.     """
  1946.     Called when the contact added message is received
  1947.    
  1948.     @type pm: PM
  1949.     @param pm: the pm
  1950.     @type user: User
  1951.     @param user: the user that gotten added
  1952.     """
  1953.     pass
  1954.  
  1955.   def onPMContactRemove(self, pm, user):
  1956.     """
  1957.     Called when the contact remove message is received
  1958.    
  1959.     @type pm: PM
  1960.     @param pm: the pm
  1961.     @type user: User
  1962.     @param user: the user that gotten remove
  1963.     """
  1964.     pass
  1965.  
  1966.   def onPMBlock(self, pm, user):
  1967.     """
  1968.     Called when successfully block a user
  1969.    
  1970.     @type pm: PM
  1971.     @param pm: the pm
  1972.     @type user: User
  1973.     @param user: the user that gotten block
  1974.     """
  1975.     pass
  1976.  
  1977.   def onPMUnblock(self, pm, user):
  1978.     """
  1979.     Called when successfully unblock a user
  1980.    
  1981.     @type pm: PM
  1982.     @param pm: the pm
  1983.     @type user: User
  1984.     @param user: the user that gotten unblock
  1985.     """
  1986.     pass
  1987.  
  1988.   def onPMContactOnline(self, pm, user):
  1989.     """
  1990.     Called when a user from the contact come online
  1991.    
  1992.     @type pm: PM
  1993.     @param pm: the pm
  1994.     @type user: User
  1995.     @param user: the user that came online
  1996.     """
  1997.     pass
  1998.  
  1999.   def onPMContactOffline(self, pm, user):
  2000.     """
  2001.     Called when a user from the contact go offline
  2002.    
  2003.     @type pm: PM
  2004.     @param pm: the pm
  2005.     @type user: User
  2006.     @param user: the user that went offline
  2007.     """
  2008.     pass
  2009.  
  2010.   def onEventCalled(self, room, evt, *args, **kw):
  2011.     """
  2012.     Called on every room-based event.
  2013.  
  2014.     @type room: Room
  2015.     @param room: room where the event occured
  2016.     @type evt: str
  2017.     @param evt: the event
  2018.     """
  2019.     pass
  2020.  
  2021.   ####
  2022.   # Deferring
  2023.   ####
  2024.   def deferToThread(self, callback, func, *args, **kw):
  2025.     """
  2026.     Defer a function to a thread and callback the return value.
  2027.  
  2028.     @type callback: function
  2029.     @param callback: function to call on completion
  2030.     @type cbargs: tuple or list
  2031.     @param cbargs: arguments to get supplied to the callback
  2032.     @type func: function
  2033.     @param func: function to call
  2034.     """
  2035.     def f(func, callback, *args, **kw):
  2036.       ret = func(*args, **kw)
  2037.       self.setTimeout(0, callback, ret)
  2038.     threading._start_new_thread(f, (func, callback) + args, kw)
  2039.  
  2040.   ####
  2041.   # Scheduling
  2042.   ####
  2043.   class _Task:
  2044.     def cancel(self):
  2045.       """Sugar for removeTask."""
  2046.       self.mgr.removeTask(self)
  2047.  
  2048.   def _tick(self):
  2049.     now = time.time()
  2050.     for task in set(self._tasks):
  2051.       if task.target <= now:
  2052.         task.func(*task.args, **task.kw)
  2053.         if task.isInterval:
  2054.           task.target = now + task.timeout
  2055.         else:
  2056.           self._tasks.remove(task)
  2057.  
  2058.   def setTimeout(self, timeout, func, *args, **kw):
  2059.     """
  2060.     Call a function after at least timeout seconds with specified arguments.
  2061.  
  2062.     @type timeout: int
  2063.     @param timeout: timeout
  2064.     @type func: function
  2065.     @param func: function to call
  2066.  
  2067.     @rtype: _Task
  2068.     @return: object representing the task
  2069.     """
  2070.     task = self._Task()
  2071.     task.mgr = self
  2072.     task.target = time.time() + timeout
  2073.     task.timeout = timeout
  2074.     task.func = func
  2075.     task.isInterval = False
  2076.     task.args = args
  2077.     task.kw = kw
  2078.     self._tasks.add(task)
  2079.     return task
  2080.  
  2081.   def setInterval(self, timeout, func, *args, **kw):
  2082.     """
  2083.     Call a function at least every timeout seconds with specified arguments.
  2084.  
  2085.     @type timeout: int
  2086.     @param timeout: timeout
  2087.     @type func: function
  2088.     @param func: function to call
  2089.  
  2090.     @rtype: _Task
  2091.     @return: object representing the task
  2092.     """
  2093.     task = self._Task()
  2094.     task.mgr = self
  2095.     task.target = time.time() + timeout
  2096.     task.timeout = timeout
  2097.     task.func = func
  2098.     task.isInterval = True
  2099.     task.args = args
  2100.     task.kw = kw
  2101.     self._tasks.add(task)
  2102.     return task
  2103.  
  2104.   def removeTask(self, task):
  2105.     """
  2106.     Cancel a task.
  2107.  
  2108.     @type task: _Task
  2109.     @param task: task to cancel
  2110.     """
  2111.     self._tasks.remove(task)
  2112.  
  2113.   ####
  2114.   # Util
  2115.   ####
  2116.   def _write(self, room, data):
  2117.     room._wbuf += data
  2118.  
  2119.   def getConnections(self):
  2120.     li = list(self._rooms.values())
  2121.     if self._pm:
  2122.       li.extend(self._pm.getConnections())
  2123.     return [c for c in li if c._sock != None]
  2124.  
  2125.   ####
  2126.   # Main
  2127.   ####
  2128.   def main(self):
  2129.     self.onInit()
  2130.     self._running = True
  2131.     for l in range(0,Number_of_Threads):
  2132.       t = threading.Thread(target=self._joinThread)
  2133.       t.daemon = True
  2134.       t.start()
  2135.     while self._running:
  2136.       conns = self.getConnections()
  2137.       socks = [x._sock for x in conns]
  2138.       wsocks = [x._sock for x in conns if x._wbuf != b""]
  2139.       rd, wr, sp = select.select(socks, wsocks, [], self._TimerResolution)
  2140.       for sock in rd:
  2141.         con = [c for c in conns if c._sock == sock][0]
  2142.         try:
  2143.           data = sock.recv(1024)
  2144.           if(len(data) > 0):
  2145.             con._feed(data)
  2146.           else:
  2147.             con.disconnect()
  2148.         except socket.error:
  2149.           pass
  2150.       for sock in wr:
  2151.         con = [c for c in conns if c._sock == sock][0]
  2152.         try:
  2153.           size = sock.send(con._wbuf)
  2154.           con._wbuf = con._wbuf[size:]
  2155.         except socket.error:
  2156.           pass
  2157.       self._tick()
  2158.  
  2159.   @classmethod
  2160.   def easy_start(cl, rooms = None, name = None, password = None, pm = True):
  2161.     """
  2162.     Prompts the user for missing info, then starts.
  2163.  
  2164.     @type rooms: list
  2165.     @param room: rooms to join
  2166.     @type name: str
  2167.     @param name: name to join as ("" = None, None = unspecified)
  2168.     @type password: str
  2169.     @param password: password to join with ("" = None, None = unspecified)
  2170.     """
  2171.     if not rooms: rooms = str(input("Room names separated by semicolons: ")).split(";")
  2172.     if len(rooms) == 1 and rooms[0] == "": rooms = []
  2173.     if not name: name = str(input("User name: "))
  2174.     if name == "": name = None
  2175.     if not password: password = str(input("User password: "))
  2176.     if password == "": password = None
  2177.     self = cl(name, password, pm = pm)
  2178.     for room in rooms:
  2179.       self.joinRoom(room)
  2180.     self.main()
  2181.  
  2182.   def stop(self):
  2183.     for conn in list(self._rooms.values()):
  2184.       conn.disconnect()
  2185.     self._running = False
  2186.  
  2187.   ####
  2188.   # Commands
  2189.   ####
  2190.   def enableBg(self):
  2191.     """Enable background if available."""
  2192.     self.user._mbg = True
  2193.     for room in self.rooms:
  2194.       room.setBgMode(1)
  2195.  
  2196.   def disableBg(self):
  2197.     """Disable background."""
  2198.     self.user._mbg = False
  2199.     for room in self.rooms:
  2200.       room.setBgMode(0)
  2201.  
  2202.   def enableRecording(self):
  2203.     """Enable recording if available."""
  2204.     self.user._mrec = True
  2205.     for room in self.rooms:
  2206.       room.setRecordingMode(1)
  2207.  
  2208.   def disableRecording(self):
  2209.     """Disable recording."""
  2210.     self.user._mrec = False
  2211.     for room in self.rooms:
  2212.       room.setRecordingMode(0)
  2213.  
  2214.   def setNameColor(self, color3x):
  2215.     """
  2216.     Set name color.
  2217.  
  2218.     @type color3x: str
  2219.     @param color3x: a 3-char RGB hex code for the color
  2220.     """
  2221.     self.user._nameColor = color3x
  2222.  
  2223.   def setFontColor(self, color3x):
  2224.     """
  2225.     Set font color.
  2226.  
  2227.     @type color3x: str
  2228.     @param color3x: a 3-char RGB hex code for the color
  2229.     """
  2230.     self.user._fontColor = color3x
  2231.  
  2232.   def setFontFace(self, face):
  2233.     """
  2234.     Set font face/family.
  2235.  
  2236.     @type face: str
  2237.     @param face: the font face
  2238.     """
  2239.     self.user._fontFace = face
  2240.  
  2241.   def setFontSize(self, size):
  2242.     """
  2243.     Set font size.
  2244.  
  2245.     @type size: int
  2246.     @param size: the font size (limited: 9 to 22)
  2247.     """
  2248.     if size < 9: size = 9
  2249.     if size > 22: size = 22
  2250.     self.user._fontSize = size
  2251.  
  2252. ################################################################
  2253. # User class (well, yeah, I lied, it's actually _User)
  2254. ################################################################
  2255. _users = dict()
  2256. def User(name, *args, **kw):
  2257.   if name == None: name = ""
  2258.   name = name.lower()
  2259.   user = _users.get(name)
  2260.   if not user:
  2261.     user = _User(name = name, *args, **kw)
  2262.     _users[name] = user
  2263.   return user
  2264.  
  2265. class _User:
  2266.   """Class that represents a user."""
  2267.   ####
  2268.   # Init
  2269.   ####
  2270.   def __init__(self, name, **kw):
  2271.     self._name = name.lower()
  2272.     self._puid = ""
  2273.     self._sids = dict()
  2274.     self._msgs = list()
  2275.     self._nameColor = "000"
  2276.     self._fontSize = 12
  2277.     self._fontFace = "0"
  2278.     self._fontColor = "000"
  2279.     self._mbg = False
  2280.     self._mrec = False
  2281.     for attr, val in kw.items():
  2282.       if val == None: continue
  2283.       setattr(self, "_" + attr, val)
  2284.  
  2285.   ####
  2286.   # Properties
  2287.   ####
  2288.   def _getName(self): return self._name
  2289.   def _getPuid(self): return self._puid
  2290.   def _getSessionIds(self, room = None):
  2291.     if room:
  2292.       return self._sids.get(room, set())
  2293.     else:
  2294.       return set.union(*self._sids.values())
  2295.   def _getRooms(self): return self._sids.keys()
  2296.   def _getRoomNames(self): return [room.name for room in self._getRooms()]
  2297.   def _getFontColor(self): return self._fontColor
  2298.   def _getFontFace(self): return self._fontFace
  2299.   def _getFontSize(self): return self._fontSize
  2300.   def _getNameColor(self): return self._nameColor
  2301.  
  2302.   name = property(_getName)
  2303.   puid = property(_getPuid)
  2304.   sessionids = property(_getSessionIds)
  2305.   rooms = property(_getRooms)
  2306.   roomnames = property(_getRoomNames)
  2307.   fontColor = property(_getFontColor)
  2308.   fontFace = property(_getFontFace)
  2309.   fontSize = property(_getFontSize)
  2310.   nameColor = property(_getNameColor)
  2311.  
  2312.   ####
  2313.   # Util
  2314.   ####
  2315.   def addSessionId(self, room, sid):
  2316.     if room not in self._sids:
  2317.       self._sids[room] = set()
  2318.     self._sids[room].add(sid)
  2319.  
  2320.   def removeSessionId(self, room, sid):
  2321.     try:
  2322.       self._sids[room].remove(sid)
  2323.       if len(self._sids[room]) == 0:
  2324.         del self._sids[room]
  2325.     except KeyError:
  2326.       pass
  2327.  
  2328.   def clearSessionIds(self, room):
  2329.     try:
  2330.       del self._sids[room]
  2331.     except KeyError:
  2332.       pass
  2333.  
  2334.   def hasSessionId(self, room, sid):
  2335.     try:
  2336.       if sid in self._sids[room]:
  2337.         return True
  2338.       else:
  2339.         return False
  2340.     except KeyError:
  2341.       return False
  2342.  
  2343.   def updatePuid(self, puid):
  2344.     self._puid = puid
  2345.  
  2346.   ####
  2347.   # Repr
  2348.   ####
  2349.   def __repr__(self):
  2350.     return "<User: %s>" %(self.name)
  2351.  
  2352. ################################################################
  2353. # Message class
  2354. ################################################################
  2355. class Message:
  2356.   """Class that represents a message."""
  2357.   ####
  2358.   # Attach/detach
  2359.   ####
  2360.   def attach(self, room, msgid):
  2361.     """
  2362.     Attach the Message to a message id.
  2363.  
  2364.     @type msgid: str
  2365.     @param msgid: message id
  2366.     """
  2367.     if self._msgid == None:
  2368.       self._room = room
  2369.       self._msgid = msgid
  2370.       self._room._msgs[msgid] = self
  2371.  
  2372.   def detach(self):
  2373.     """Detach the Message."""
  2374.     if self._msgid != None and self._msgid in self._room._msgs:
  2375.       del self._room._msgs[self._msgid]
  2376.       self._msgid = None
  2377.  
  2378.   def delete(self):
  2379.     self._room.deleteMessage(self)
  2380.  
  2381.   ####
  2382.   # Init
  2383.   ####
  2384.   def __init__(self, **kw):
  2385.     """init, don't overwrite"""
  2386.     self._msgid = None
  2387.     self._time = None
  2388.     self._user = None
  2389.     self._body = None
  2390.     self._room = None
  2391.     self._raw = ""
  2392.     self._ip = None
  2393.     self._unid = ""
  2394.     self._nameColor = "000"
  2395.     self._fontSize = 12
  2396.     self._fontFace = "0"
  2397.     self._fontColor = "000"
  2398.     for attr, val in kw.items():
  2399.       if val == None: continue
  2400.       setattr(self, "_" + attr, val)
  2401.  
  2402.   ####
  2403.   # Properties
  2404.   ####
  2405.   def _getId(self): return self._msgid
  2406.   def _getTime(self): return self._time
  2407.   def _getUser(self): return self._user
  2408.   def _getBody(self): return self._body
  2409.   def _getIP(self): return self._ip
  2410.   def _getFontColor(self): return self._fontColor
  2411.   def _getFontFace(self): return self._fontFace
  2412.   def _getFontSize(self): return self._fontSize
  2413.   def _getNameColor(self): return self._nameColor
  2414.   def _getRoom(self): return self._room
  2415.   def _getRaw(self): return self._raw
  2416.   def _getUnid(self): return self._unid
  2417.  
  2418.   msgid = property(_getId)
  2419.   time = property(_getTime)
  2420.   user = property(_getUser)
  2421.   body = property(_getBody)
  2422.   room = property(_getRoom)
  2423.   ip = property(_getIP)
  2424.   fontColor = property(_getFontColor)
  2425.   fontFace = property(_getFontFace)
  2426.   fontSize = property(_getFontSize)
  2427.   raw = property(_getRaw)
  2428.   nameColor = property(_getNameColor)
  2429.   unid = property(_getUnid)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top