2012-12-01 49 views
1

我正試圖在本地服務器上實現IRC Bot。我正在使用的機器人與在Eric Florenzano's Blog找到的機器人相同。這是簡化的代碼(其應該運行)扭曲的IRC Bot連接重複丟失到本地主機

import sys 
import re 
from twisted.internet import reactor 
from twisted.words.protocols import irc 
from twisted.internet import protocol 

class MomBot(irc.IRCClient): 
    def _get_nickname(self): 
     return self.factory.nickname 
    nickname = property(_get_nickname) 

    def signedOn(self): 
     print "attempting to sign on" 
     self.join(self.factory.channel) 
     print "Signed on as %s." % (self.nickname,) 

    def joined(self, channel): 
     print "attempting to join" 
     print "Joined %s." % (channel,) 

    def privmsg(self, user, channel, msg): 
     if not user: 
      return 
     if self.nickname in msg: 
      msg = re.compile(self.nickname + "[:,]* ?", re.I).sub('', msg) 
      prefix = "%s: " % (user.split('!', 1)[0],) 
     else: 
      prefix = '' 
     self.msg(self.factory.channel, prefix + "hello there") 


class MomBotFactory(protocol.ClientFactory): 
    protocol = MomBot 

    def __init__(self, channel, nickname='YourMomDotCom', chain_length=3, 
     chattiness=1.0, max_words=10000): 
     self.channel = channel 
     self.nickname = nickname 
     self.chain_length = chain_length 
     self.chattiness = chattiness 
     self.max_words = max_words 

    def startedConnecting(self, connector): 
     print "started connecting on {0}:{1}" 
      .format(str(connector.host),str(connector.port)) 

    def clientConnectionLost(self, connector, reason): 
     print "Lost connection (%s), reconnecting." % (reason,) 
     connector.connect() 

    def clientConnectionFailed(self, connector, reason): 
     print "Could not connect: %s" % (reason,) 

if __name__ == "__main__": 
    chan = sys.argv[1] 
    reactor.connectTCP("localhost", 6667, MomBotFactory('#' + chan, 
     'YourMomDotCom', 2, chattiness=0.05)) 
    reactor.run() 

我在客戶工廠,它被到達並打印出正確的地址添加startedConnection方法:主機。然後它斷開並進入clientConnectionLost並打印錯誤:

Lost connection ([Failure instance: Traceback (failure with no frames): 
    <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly. 
    ]), reconnecting. 

如果正常工作應該登錄到適當的信道,指定爲在命令中第一個參數(例如蟒module2.py botwar將信道#。 botwar)。如果頻道中的任何人發送任何內容,它應該以「你好,」迴應。

我有NGIRC在服務器上運行,它工作,如果我從mIRC或任何其他IRC客戶端連接。

我無法找到一個解決方案,爲什麼它不斷斷開連接。任何幫助爲什麼將不勝感激。謝謝!

+0

您正在運行哪臺IRC服務器?它是開源的嗎? – fmoo

+1

*有*是因爲它斷開連接的原因,否則它不會斷開連接。然而,沒有足夠的信息讓任何人猜測可能的原因。請參閱http://sscce.org/和http://www.catb.org/esr/faqs/smart-questions.html –

+0

@ Jean-PaulCalderone - 感謝您訪問這些網站。我添加了更多信息。它不屬於「短」的要求,但我不知道如何以任何其他方式做到這一點。希望這是一個更好的措辭問題。 – RyGuy

回答

2

你可能想要做的一件事是確保你的機器人連接到它時,你會看到服務器產生的任何錯誤輸出。我的直覺是,這個問題與認證有關,或者ngirc處理IRCClient所使用的登錄/認證命令之一的一個意想不到的差異。

幾乎總是適用的一種方法是捕獲流量日誌。使用像tcpdump或wireshark這樣的工具。

您可以嘗試的另一種方法是在Twisted應用程序本身內啓用日誌記錄。使用twisted.protocols.policies.TrafficLoggingFactory這個:

from twisted.protocols.policies import TrafficLoggingFactory 
appFactory = MomBotFactory(...) 
logFactory = TrafficLoggingFactory(appFactory, "irc-") 
reactor.connectTCP(..., logFactory) 

這將輸出記錄到開始「IRC-」(不同的文件爲每個連接)文件。

您也可以直接掛鉤到您的協議實現,在任何一個級別。例如,能夠顯示收到的所有的所有字節:

class MomBot(irc.IRCClient): 
    def dataReceived(self, bytes): 
     print "Got", repr(bytes) 
     # Make sure to up-call - otherwise all of the IRC logic is disabled! 
     return irc.IRCClient.dataReceived(self, bytes) 

有了這些方法之一,希望你能看到類似這樣的:

:irc.example.net 451 * :Connection not registered 

我想辦法......你需要驗證?即使你看到別的東西,希望這會幫助你更仔細地瞭解連接關閉的確切原因。

此外,您可以使用tcpdumpwireshark來捕獲ngirc和其中一個工作的IRC客戶端(例如mIRC)之間的流量日誌,然後比較這兩個日誌。無論mIRC發送什麼不同的命令都應該清楚你需要對你的機器人做什麼改變。

+0

謝謝!它現在有效。問題出在配置上,或者說我缺乏對其設置的瞭解。它返回的錯誤是「aerreneous nickname」。 conf文件的最大暱稱長度爲9個字符,我的類似13。 – RyGuy