title: Adding private chat functionality to simple twisted chat server
body: <p>I am trying to learn twisted library from its documentation. I've created a simple chat server that allows users to public chat. However, I wonder if I can add private chat functionality to that. I am looking for your ideas. Thanks.</p>
  
  <pre><code>from twisted.internet.protocol import Factory
  from twisted.protocols.basic import LineReceiver
  from twisted.internet import reactor
  
  class Chat(LineReceiver):
      def __init__(self,users):
          self.users = users
          self.name = None
          self.state = "GETNAME"
  
      def connectionMade(self):
          self.sendLine("What's your name?")
  
      def connectionLost(self, reason):
          if self.users.has_key(self.name):
              del self.users[self.name]
  
      def lineReceived(self,line):
          if self.state == "GETNAME":
              self.handle_GETNAME(line)
          else:
              self.handle_CHAT(line)
  
      def handle_GETNAME(self,name):
          if self.users.has_key(name):
              self.sendLine("Name taken, please choose another.")
              return
          self.sendLine("Welcome, %s!" % (name))
          self.name = name
          self.users[name] = self
          self.state = "CHAT"
  
      def handle_CHAT(self,msg):
          msg = "&lt;%s&gt; %s" % (self.name,msg)
          for name,protocol in self.users.iteritems():
              if protocol != self:
                  protocol.sendLine(msg)
  
  class ChatFactory(Factory):
      def __init__(self):
          self.users = {}
      def buildProtocol(self, addr):
          return Chat(self.users)
  
  reactor.listenTCP(8123,ChatFactory())  #@UndefinedVariable
  reactor.run() #@UndefinedVariable
  </code></pre>