扭曲的IRC服务器的好例子?

时间:2010-06-29 14:08:37

标签: python twisted irc

我正在尝试使用IRC服务器/客户端的扭曲库。我已经找到了一些如何实现IRC客户端的好例子,但似乎在服务器方面找到了一些好东西。任何人都可以提供一些有关如何在twisted中创建基本IRC服务器的见解吗?

编辑:如何建立这个?我在这里正确的方向吗?

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()

当我尝试加入频道时,我无法做到。我得到一个错误,没有一个命令的处理程序,所以我写了irc_USER和irc_NICK方法,但只是摆脱了错误,它没有解决不连接/不工作的问题。

3 个答案:

答案 0 :(得分:8)

也许是这样的?

exarkun@boson:/tmp/irc-server$ cat > passwd
alice:secret
bob:19820522
exarkun@boson:/tmp/irc-server$ twistd -n words --irc-port 6667 --auth file:passwd
2010-06-29 11:51:26-0400 [-] Log opened.
2010-06-29 11:51:26-0400 [-] twistd 10.0.0+r29436 (/usr/bin/python 2.6.4) starting up.
2010-06-29 11:51:26-0400 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2010-06-29 11:51:26-0400 [-] twisted.words.service.IRCFactory starting on 6667
2010-06-29 11:51:26-0400 [-] Starting factory <twisted.words.service.IRCFactory instance at 0x9ddbf8c>

如果您想了解具体方法,请参阅twisted/words/tap.py

twisted.words.protocols.irc.IRC是IRC服务器解析部分的一个非常基本的实现。它不实现任何实际的服务器逻辑,如通道,模式,消息等。您可以在其上构建服务器,但您必须构建几乎所有的东西。这正是twistd words调用的代码所做的。您可能希望参考其实现,以查看问题中代码所针对的成功示例。

答案 1 :(得分:0)

我遇到了这本书,其中包含以下代码,这些代码将在扭曲的单词服务器上运行,并允许您创建频道等。这是本书中的代码。

from twisted.cred import checkers, portal
from twisted.internet import reactor
from twisted.words import service

wordsRealm = service.InMemoryWordsRealm("example.com")
wordsRealm.createGroupOnRequest = True

checker = checkers.FilePasswordDB("authfile.txt")
portal = portal.Portal(wordsRealm, [checker])

reactor.listenTCP(6667, service.IRCFactory(wordsRealm, portal))
reactor.run()

这本书:http://books.google.com/books?id=_g5UNxWUKsMC&printsec=frontcover#v=onepage 转到第119页,您将找到它的描述。买这本书,这是一本好书。

答案 2 :(得分:0)

如果你想要一个简单的匿名&#39;扭曲的IRC服务器,这基本上是最简单的方法:

from twisted.application import internet, service
from twisted.cred import checkers, portal, credentials
from twisted.cred.checkers import ICredentialsChecker
from twisted.internet import defer
from twisted.words import service as wordsservice
from zope.interface import implements

wordsRealm = wordsservice.InMemoryWordsRealm("example.com")
wordsRealm.createGroupOnRequest = True
wordsRealm.createUserOnRequest = True

class UserAnon:
  implements(ICredentialsChecker)
  credentialInterfaces = (credentials.IUsernamePassword, credentials.IUsernameHashedPassword)

  def __init__(self):
    pass

  def addUser(self, username, password):
    pass

  def _cbPasswordMatch(self, matched, username):
    return username

  def requestAvatarId(self, credentials):
    return defer.succeed(credentials.username)

class IRCAnonymous(wordsservice.IRCUser):
  def irc_NICK(self, prefix, params):
    self.password = 'doesntmatter'
    wordsservice.IRCUser.irc_NICK(self, prefix, params)


checker = UserAnon()
portal = portal.Portal(wordsRealm, [checker])

servicefactory = wordsservice.IRCFactory(wordsRealm, portal)
servicefactory.protocol=IRCAnonymous

application = service.Application("ircserver")
ircservice = internet.TCPServer(6667, servicefactory)
ircservice.setServiceParent(application)

然后,您可以使用twistd -nol- -y irc_server.py来扭曲执行此操作。

其他答案中提到的棘手的一点是,扭曲协议对象上的各种消息消息对其输入/返回有期望,因此您必须转到模块文档,有时需要输入源代码那里需要什么。