Following up on an earlier DevShed article covering the basics of Python and Internet Relay Chat, this article takes some common IRC tasks, such as listing the users in a given channel or manipulating a channel's modes, and shows how to turn them into Pyton code.
The two most basic channel related tasks are joining and parting a channel. The commands are “JOIN” and “PART” and look like this:
JOIN channel
PART channel
No surprises there. Let's put it into Python code:
def join ( self, channel ):
self.send ( 'JOIN ' + channel )
def part ( self, channel ):
self.send ( 'PART ' + channel )
Getting and setting the topic is also pretty simple. The same command, “TOPIC”, is used for both operations. Here is how to get and set a topic, in the respective order:
TOPIC channel
TOPIC channel topic
Our topic should not actually retrieve the data, though. Instead, the recv and retrieve methods should catch it, so creating a method to “get” and set the topic should be pretty simple.
def topic ( self, channel, topic = '' ):
self.send ( 'TOPIC ' + channel + ' ' + topic )
To get a list of names of everyone in a given channel, the “NAMES” command is used:
NAMES #channel
The Python version is short and sweet:
def names ( self, channel ):
self.send ( 'NAMES ' + channel )
IRC allows people to invite their friends to a certain channel using the “INVITE” command:
INVITE person channel
Let's create an invite method:
def invite ( self, nick, channel ):
self.send ( 'INVITE ' + nick + ' ' + channel )
Setting a channel's mode is done with the “MODE” command:
MODE channel mode
Sometimes, a user must be specified:
MODE channel user
To make that a bit more clear, here's an example:
MODE #python +o Peyton
The above command would give “Peyton” operator status in a channel. Let's create a mode method: