Save it as bot.py and run with python bot.py. If "PRIVMSG" in text and channel in text and "hello" in text: The bot will reply “Hello!” if it gets the message “hello” in the channel it resides. We will keep our (ro)bot simple for explanatory purposes. Now that we have the network connectivity class, we can use it as an instance. ( "JOIN " + channel + "n") #join the chan ( "USER " + botnick + " " + botnick + " " + botnick + " :This is a fun bot!n") #user authentication ( "PRIVMSG " + chan + " " + msg + "n")ĭef connect( self, server, channel, botnick): Self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Summing up, we get this class (save it as irc.py): import socket Sometimes the IDENT command is neccesary too. We then must use the commands to authenticate with the server: 1 The second argument tells the library to use stream sockets, which are traditionally implemented on the TCP protocol. Socket.AF_INET tells the library to use the network protocol IPv4. To create a socket we use the command: irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) The IRC protocol is a layer on top of the IP protocol. But following this tutorial may be a lot simpler ) Authentication is achieved using only a few steps: If you are curious, you could read the entire protocol. The IRC protocol has distinct messages such as PRIVMSG, USER, NICK and JOIN. To communicate with an IRC server, you need to use the IRC protocol. Foundations of Python Network Programming.The script itself will use network sockets, a library that is often used to provide network interactions in many programming languages including Python and C/C++. IRC servers never ask for any type of complicated human verification such as solving captchas, which is why we can simply connect with a script. You will need a program that connects with an IRC server and acts like a traditional IRC client. So how do you start and build one in Python, just for fun? Just send the JOINs immediately after NICK, USER, and things like CAP END.Īnd by the gods, do not use something as silly as a 10 second timeout for this.There are tons of (ro)bots out there for IRC (Internet Relay Chat). # parsed input is :Ĭonn.send("JOIN %s\r\n" % ",".join(channels))īut as said above, this should never be necessary. You should also check for the "no /MOTD" message.įor example, you would have: # raw input is ": 376 grawity :End of /MOTD.\r\n" Instead, parse the entire line as specified in RFC 1459 § 2.3.1 (here's a not-particularly-Pythonic parser) and check the command in each line, since the descriptive text can vary between different IRC daemons. If you're looking for the "End of /MOTD" string – don't. Instead, you would just keep track of whether you have already seen an "end of MOTD" line, and send the JOINs the first time you see it.Īlso, from your description, it does sound like you're using a somewhat unreliable method to detect the end of MOTD. In any case, you wouldn't need to delay the main loop to achieve this – indeed, all received input should be handled by the same loop. It is perfectly valid to start sending commands as soon as you can. send NICK and USER) – some of them merely delay acting on those commands by a second or two. However, it shouldn't even be necessary, because your assumption that "The network will not accept any commands until the MOTD is finished" is not true in the first place.Īll networks start accepting commands immediately as soon as you register (i.e. No, there isn't a way to turn off the MOTD. Is there a way to tell the server to not send the MOTD, or indeed a better way to wait for the end of the MOTD to tell the server what channel I wish to connect to Is there a better way to handle the MOTD's? I can't help but feel that this way is rather clumsy. It takes this list and searches each string object for a certain string that appears at the end of FreeNode's MOTD, if it is found, the loop ends and a command to join a channel is sent before going into the main loop. My current code for waiting for the end of MOTD consists of a while loop reading the input buffer and parsing each full command into a list. Is there a way to tell the server to not send the MOTD, or indeed a better way to wait for the end of the MOTD to tell the server what channel I wish to connect to? This isn't particularly modular at all, as in my experience not all servers end their MOTD's the same way. So with my bot, I have to have a loop that checks for the end of the MOTD. The network will not accept any commands until the MOTD is finished. When you first connect to an IRC network, a MOTD usually gets displayed. I think the issue is with my limited knowledge of the IRC protocol, so IRC gurus are particularly welcome here :D I have an IRC bot written in Python that works fairly well.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |