IRC消息随机不从b发送

2024-10-04 15:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python编写了一个非常简单的bot来接收webhook,解析数据,然后向IRC发送消息。一切正常,除了在完全随机的时间消息将无法发送。我没有从IRC服务器得到任何错误消息,但是它们根本不会出现在IRC中。我构造了一个要发送的消息数组,然后循环发送它们。我通过SASL向IRC认证。我确认所有邮件都在列表中,应该发送。有时他们都送好的,有时只送一些,有时他们都不送。我试图从服务器上取回消息,但似乎没有。你知道吗

当我打印消息数组时,它看起来像:

['[\x0313repo\x0f] \x0315user-name\x0f force pushed \x021\x0f commit(s) to \x036master\x0f: \x032\x1fhttps://github.com/owner/repo/compare/e1a06c001733...387b204c4303\x0f\r\n', '\x0313repo\x0f/\x036master\x0f \x0314387b204\x0f \x0315user-name\x0f: commit message here\r\n']

我的send命令:

self.irc.send(bytes("PRIVMSG {} :{}\r\n".format(channel, message), "UTF-8"))

完整代码:

import base64
import json
import re
import socket
import ssl
from time import sleep


class IRC(object):

    def __init__(self, sslConfig):
        if sslConfig is True:
            self.irc = ssl.wrap_socket(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM),
                ssl_version=ssl.PROTOCOL_TLSv1_2
            )
        else:
            self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def getText(self):
        text=self.irc.recv(2040)
        return text.decode("UTF-8")

    def waitAndSee(self, search):
        tries = 0
        while True:
            text = self.getText()
            if tries > 20:
                raise ConnectionError("Unable to connect to IRC: %s" % text) 
            ack = re.search(search, text, re.MULTILINE)
            if ack:
                return
            sleep(0.25)
            tries += 1

    def authenticate(self, nick, password):
        self.irc.send(bytes("CAP REQ :sasl\r\n", "UTF-8"))
        self.waitAndSee(r'(.*)CAP(.*)ACK(.*)')
        self.irc.send(bytes("AUTHENTICATE PLAIN\r\n", "UTF-8"))
        self.waitAndSee(r'(.*)AUTHENTICATE \+(.*)')
        auth = (
            "{nick}\0{nick}\0{password}"
        ).format(
            nick=nick,
            password=password
        )
        auth = base64.encodestring(auth.encode("UTF-8"))
        auth = auth.decode("UTF-8").rstrip("\n")
        self.irc.send(bytes("AUTHENTICATE "+auth+"\r\n", "UTF-8"))
        self.waitAndSee(r'(.*)903(.*):SASL authentication successful(.*)')
        self.irc.send(bytes("CAP END\r\n", "UTF-8"))

    def sendMessage(self, channel, message):
        self.irc.send(bytes("PRIVMSG {} :{}\r\n".format(channel, message), "UTF-8"))

    def sendPong(self, text):
        self.irc.send(bytes(text.replace('PING', 'PONG'), "UTF-8"))

    def connect(self, host, port, channels, nick, password):
        print("Connecting to {}:{} with nick {} and channels: {}".format(host, port, nick, ','.join(channels)))
        self.irc.connect((host, port))
        if password != None:
            self.authenticate(nick, password)                                             
        self.irc.send(bytes("USER {nick} {nick} {nick} {nick}\r\n".format(nick=nick), "UTF-8"))
        self.irc.send(bytes("NICK {}\r\n".format(nick), "UTF-8"))
        for channel in channels:          
            self.irc.send(bytes("JOIN {}\r\n".format(channel), "UTF-8"))

    def disconnect(self, channels):
        for channel in channels:               
            self.irc.send(bytes("PART {}\r\n".format(channel), "UTF-8"))
        self.irc.send(bytes("QUIT\r\n", "UTF-8"))
        self.irc.close()



try:
    irc = IRC(pool.ssl)
    irc.connect(pool.host, pool.port, pool.channels, pool.nick, pool.password)

    # Wait until connection is established
    while True:    
        text = irc.getText()
        if re.search(r'(.*)End of /NAMES list.(.*)', text, re.MULTILINE):
            break
        elif re.search(r'(.*)PING(.*)', text, re.MULTILINE):
            irc.sendPong(text)
        elif re.search(r'(.*)433(.*)Nickname is already in use(.*)', text, re.MULTILINE):
            raise ConnectionError("Nickname is already in use")
        elif re.search(r'(.*)ERROR :(.*)', text, re.MULTILINE):
            raise ConnectionError(text)
        sleep(0.25)
    for channel in pool.channels:
        for message in messages:
            irc.sendMessage(channel, message)

    irc.disconnect(pool.channels)

    return 'success'

except Exception as e:
    print(e)
    return 'error'

编辑

当我研究这个的时候,似乎信息的内容就是问题所在。在示例数组中,第一条消息有两个冒号。这是通常不起作用的信息。如果我这样做message.replace(":","")它似乎发送罚款。但我有另一个消息,有两个冒号,似乎工作良好,所以不确定这是否是一个红鲱鱼。你知道吗

编辑2

信息内容绝对是一条红鲱鱼。请参阅下面的解决方案。你知道吗


Tags: textselfresendformatbytesdefirc
1条回答
网友
1楼 · 发布于 2024-10-04 15:23:00

TL;DR

if re.search(r'(.*)End of /NAMES list.(.*)', text, re.MULTILINE)更改为if re.search(r'(.*)00[1-4] '+pool.nick+'(.*)', text, re.MULTILINE)修复了问题。这会在连接后更快地发送消息。你知道吗

请参见下面的说明。你知道吗


经过大量的测试,我发现问题,这不是我所期望的。问题是:while True:循环中的if re.search(r'(.*)End of /NAMES list.(.*)', text, re.MULTILINE):行。你知道吗

在我连接到IRC并加入一个通道之后,我循环处理从IRC返回的消息,以确保在发送消息之前成功连接。包含“结束/名称列表”的消息是IRC服务器最后发送的消息之一。(在莫特和其他人之后)。有时这花了太长时间,就像我的连接被闲置或什么的。然后发送消息唤醒了连接,但实际上并没有发送消息。这就是为什么它会随机发生。有时接收消息会很快,有时会很慢(可能是基于IRC服务器的负载)。你知道吗

因此,我将搜索的行改为if re.search(r'(.*)00[1-4] '+pool.nick+'(.*)', text, re.MULTILINE),因为这是成功连接到IRC后发送的第一条消息之一。这发生在莫特和其他人之前,但仍然意味着我有联系。现在,当我在连接后立即发送消息时,它们都会一致地通过。你知道吗

相关问题 更多 >

    热门问题