0

Trying to make a basic irc bot in python (3.9.2) that can response specific commands when invoked. It works although when a response contains whitespace or a space, only the first word gets displayed by the bot, for example;

me > @hello
bot > hi
me > @how is the weather?
bot > the

it was supposed to say, the weather seems nice today

Here's the code

import sys
import time
import socket
import string

server_address="irc.libera.chat"
server_port = 6667

botnick="lamebot"
channel_name="##megadouched"

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((server_address,server_port))
irc.setblocking(False)
time.sleep(1)

irc.send(bytes("USER "+botnick+" "+botnick+" "+botnick+" bot has joined the chat\r\n", "UTF-8"))

time.sleep(1)

irc.send(bytes("NICK "+botnick+"\n", "UTF-8"))
time.sleep(1)

irc.send(bytes("JOIN "+channel_name+"\n", "UTF-8"))

irc_privmsg = 'b"PRIVMSG "+channel_name+" Hello\r\n"'

while True:
    try:
        text = irc.recv(4096)
    except Exception:
        pass
    if text.find(bytes(":@hi", "UTF-8"))!=-1:
        irc.sendall(bytes("PRIVMSG "+channel_name+" Hello\r\n", "UTF-8"))
        text=b""
    elif text.find(bytes(":@how is the weather?", "UTF-8"))!=-1:
        irc.sendall(bytes("PRIVMSG "+channel_name+" the weather today seems nice\r\n", "UTF-8"))
        text=b""

input()
3
  • 1
    Looking briefly at the protocol, the examples have a : character before the text message. Commented Jul 19, 2022 at 14:19
  • @icarus holy!! it works!! Thank you! Would you mind writing a proper answer so I could mark it as accepted one? Commented Jul 19, 2022 at 14:36
  • @ilkkachu has beaten me to it. I suggest accepting that answer. Commented Jul 19, 2022 at 18:24

1 Answer 1

2

The IRC protocol splits the messages into a command and a number of parameters, separated by whitespace. To allow for a trailing parameter that can contain whitespace itself, the protocol allows prefixing that with a colon. Like in this example in RFC 2812:

PRIVMSG Angel :yes I'm receiving it !
                                   ; Command to send a message to Angel.

Now that I looked, that's not actually spelled out too well in the RFC, but hidden in the BNF syntax for the messages at 2.3.1:

    params     =  *14( SPACE middle ) [ SPACE ":" trailing ]
               =/ 14( SPACE middle ) [ SPACE [ ":" ] trailing ]

    middle     =  nospcrlfcl *( ":" / nospcrlfcl )
    trailing   =  *( ":" / " " / nospcrlfcl )

The trailing syntax element can appear at the end of params after a colon, and unlike middle, it allows for the space too.

(Yes, that means that a message like PRIVMSG someone the weather today seems nice has more parameters than the PRIVMSG command takes, but for whatever reason, that's not treated as an error. Might be just simplicitly of the implementations, or silliness resulting from Postel's law.)

2
  • i guess people are still inside me. sad face :( Commented Jul 23, 2022 at 19:13
  • @losethos, I have no idea what you mean with that? Commented Jul 24, 2022 at 11:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.