Home arrow Python arrow Page 3 - Basic IRC Tasks

Channel Related Tasks - Python

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.

TABLE OF CONTENTS:
  1. Basic IRC Tasks
  2. Communication Tasks
  3. Channel Related Tasks
  4. User Related Tasks
  5. Processing Information
By: Peyton McCullough
Rating: starstarstarstarstar / 4
April 18, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

def mode ( self, channel, mode, nick = '' ):

   self.send ( 'MODE ' + channel + ' ' + mode + ' ' + nick )

If you have operator status in a channel, you may kick a user from the channel:

KICK channel user

A reason can also be specified:

KICK channel user reason

Here's the Python way:

def kick ( self, channel, nick, reason = '' ):

    self.send ( 'KICK ' + channel + ' ' + nick + ' ' + reason )



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: