I’m in the process of experimenting a bit with the twisted libraries for IRC servers/clients. I’ve found a few good examples of how to implement an IRC client but seem to find anything good on the server side of things. Could anybody provide some insight into how to create a basic IRC server in twisted?
Edit: What about building off of this? Am I going the right direction here?
from twisted.internet.protocol import ServerFactory
from twisted.internet import reactor
from twisted.words.protocols.irc import IRC
class IRCServer(IRC):
def connectionMade(self):
print "client connected"
def handleCommand(self, command, prefix, params):
print "handle comm"
IRC.handleCommand(self, command, prefix, params)
def dataReceived(self, data):
print "data: %s" % data
IRC.dataReceived(self, data)
def irc_unknown(self, prefix, command, params):
print "%s, %s, %s, IRC UNKNOWN" % (prefix, command, params)
def irc_USER(self, prefix, params):
print "USER: %s, %s" % (prefix, params)
def irc_NICK(self, prefix, params):
print "NICK: %s, %s" % (prefix, params)
class IRCServerFactory(ServerFactory):
protocol = IRCServer
factory = IRCServerFactory()
reactor.listenTCP(8002, factory)
reactor.run()
When I try to join the channel I am never able to. I was getting an error relating to not having a handler for a command, so I wrote up the irc_USER and irc_NICK methods but that merely got rid of the error, it didn’t solve the problem of not connecting/not working.
Perhaps something like this?
If you’d like to see how this is implemented, see twisted/words/tap.py
twisted.words.protocols.irc.IRCis a very basic implementation of just the parsing parts of an IRC server. It implements no actual server logic such as channels, modes, messages, etc. You can build a server on it, but you have to build almost the whole thing. This is precisely what the code invoked bytwistd wordsdoes. You may want to refer to its implementation to see a successful example of what the code in your question is aiming towards.