Python IRC Bot不会在某些服务器上加入频道,我不知道如何让它响应用户输入

时间:2012-06-20 22:40:09

标签: python irc bots

我有一个python IRC机器人,我正在开始练习我的python。但是,对于某些服务器,虽然它成功登录,但它不会加入频道。我也想让它对像!test这样的用户命令做出反应,但我不知道该怎么做。在它运行的服务器上,它成功地回击。

from socket import socket, AF_INET, SOCK_STREAM
network = raw_input("Server: ")
port = 6667
chan = raw_input("Channel: ")
nick = raw_input("Nick: ")
irc=socket(AF_INET,SOCK_STREAM)
irc.connect((network, port))
a=irc.recv (4096)
print a
irc.send('NICK ' + nick + '\r\n')
irc.send('USER john_bot john_bot bla :john_bot\r\n')
irc.send('JOIN :' + chan + '\r\n')
irc.send('PRIVMSG ' + chan + ' :Hello.\r\n')

def ircSend(msg):
     print(msg)
     irc.send(msg)

while True:
    data = irc.recv(4096)
    print data
    if data.find('PING') != -1:
       ircSend('PONG ' + data.split()[1] + '\r\n')

1 个答案:

答案 0 :(得分:4)

在某些服务器上,您必须先使用PONG响应PING才能实际执行任何操作。

ircSend('NICK ' + nick + '\r\n')
ircSend('USER john_bot john_bot bla :john_bot\r\n')

data = ircRecv() # explained later
if data.find('PING') != -1:
    ircSend('PONG ' + data.split()[1] + '\r\n')

ircSend('JOIN :' + chan + '\r\n')
ircSend('PRIVMSG ' + chan + ' :Hello.\r\n')

你真的不需要这个

a=irc.recv (4096)
print a

有时IRC服务器一次发送多行(例如MOTD或NAMES)。只要总字节数不超过4096(某些行将分成两行),这将很好地处理它

data = irc.recv(4096)
for line in data.split('\r\n'):
    # process the line

如果问题是一条线会减少一半(很少就像PING恰好在那里),我们可以一次收到一行并将剩下的字符留给socket的缓冲区。然而,这可能效率稍低(我没有测试过,所以也许根本不重要)

def ircRecv():
    line = ''
    while 1: # same as while True:
        character = irc.recv(1)
        if character == '\n':
            break # exit the loop
        elif character != '\r':
            line += character
    print line
    return line

来自IRC RFC的第8页:

<message>  ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix>   ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command>  ::= <letter> { <letter> } | <number> <number> <number>
<SPACE>    ::= ' ' { ' ' }
<params>   ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle>   ::= <Any *non-empty* sequence of octets not including SPACE
           or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
             NUL or CR or LF>
<crlf>     ::= CR LF

这意味着您可以随时轻松获取邮件的发件人:

# put this inside the main loop
# this will throw an IndexError when the connection is closed,
# an empty line does not contain any spaces
line = ircRecv()
if line.split()[0].find('!') != -1:
    # the first character must be a colon because <command> can't include
    # an exclamation mark
    someOneElsesNick = line[1:line.find('!')]
    command = line.split()[1]

当有人问候时回答!

    if command == 'PRIVMSG':
        destination = line.split()[2] # channel or bot's nick
        # <trailing>, in this case, the message
        message = line[line[1:].find(':')+2 : ] # everything after the 2nd colon

        # we add one since we don't want include the <trailing> colon in
        # the message and an other one because line[1:].find() is one smaller 
        # number than line.find() would be

        # if we receive a private message, we have to respond to the sender,
        # not to ourself
        if destination == nick:
            destination = someOneElsesNick

        if message.startswith('hi!'):
            ircSend('PRIVMSG ' + destination + ' :Hi, '
            + someOneElsesNick + '!\r\n')

有关详细信息,请查看IRC RFC:http://www.ietf.org/rfc/rfc1459.txt(特别是第2.3.1和4节)。如果您不想处理IRC协议,请使用Twisted :) http://twistedmatrix.com/trac/