[Twisted-Python] Standalone IRC server, or Passport/Words/Something sucks

Itamar Shtull-Trauring twisted at itamarst.org
Mon Dec 3 08:12:22 EST 2001


Attached is an IRC server that acts more or less like a regular IRC server - 
anyone can login with no prior account.

Issues:
1. It takes 3 seconds until connectionLost is called, so you can't 
immediately relogin.
2. The automatic user creation is horribly verbose, hard to understand, and 
hacky (getPerspectiveNamed, specifically.)
3. You get contacted by "*login*" every time you login.
4. You still need a password, though in this setup it knows its own password 
so the user doesn't deal with (and for my use this is good since I need a 
password protected IRC server.)

===============================================
from twisted.protocols import protocol
from twisted.words import service, ircservice
from twisted.internet import passport

class SimpleService(service.Service):
     """A simple IRC service that creates users on the fly."""

     def removePerspective(self, name):
         if self.participants.has_key(name):
             del self.participants[name]
             self.application.authorizer.removeIdentity(name)

     def createParticipant(self, name):
         if not self.participants.has_key(name):
             log.msg("Created New Participant: %s" % name)

     def getPerspectiveNamed(self, name):
         if self.participants.has_key(name):
             raise service.WordsError, "user exists"
         else:
             p = service.Participant(name)
             p.setService(self)
             ident = passport.Identity(name, self.application)
             ident.setPassword("ugly hack")
             self.application.authorizer.addIdentity(ident)
             p.setIdentity(ident)
             ident.addKeyForPerspective(p)
             self.participants[name] = p
             return p


class IRCChatter(ircservice.IRCChatter):

     passwd = "ugly hack" # remove this to force user to send password

     def connectionLost(self):
         ircservice.IRCChatter.connectionLost(self)
         print self.nickname
         self.service.removePerspective(self.nickname)


class IRCGateway(protocol.Factory):

     def __init__(self, service):
         self.service = service

     def buildProtocol(self, connection):
         """Build an IRC protocol to talk to my chat service.
         """
         i = IRCChatter()
         i.service = self.service
         return i


def main():
     """Run an IRC server"""
     from twisted.internet import main
     app = main.Application("irc")
     svc = SimpleService("twisted.words", app)
     irc = IRCGateway(svc)
     app.listenTCP(6667, irc)
     app.run(0)


if __name__ == '__main__':
     main()






More information about the Twisted-Python mailing list