5

我正在对 IRC 服务器/客户端的扭曲库进行一些试验。我找到了一些关于如何实现 IRC 客户端的好例子,但似乎在服务器端发现了一些好的东西。任何人都可以提供一些关于如何在扭曲中创建基本 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 方法,但这只是消除了错误,并没有解决无法连接/无法工作的问题。

4

3 回答 3

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所做的。您可能想参考它的实现,以查看问题中代码目标的成功示例。

于 2010-06-29T15:53:35.403 回答
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 页,您会找到它的说明。买书吧,挺好的。

于 2014-10-26T01:48:46.747 回答
0

如果你想要一个简单的“匿名”扭曲 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 with 执行此操作twistd -nol- -y irc_server.py

其他答案中提到的棘手之处在于,扭曲协议对象上的各种消息消息对其输入/返回有预期,因此您必须转到模块文档,有时还需要查看源代码来找出那里需要什么。

于 2015-06-24T02:36:07.000 回答