Home arrow Python arrow Page 5 - Basic IRC Tasks

Processing Information - 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

Now that I finished boring you with IRC commands and the ever-so-obvious Python methods that use them, it's time to get information back from the server and process it. Before we get to that though, make sure your irchat module looks something like this:

import socket

class IRC:

   queue = []

   partial = ''

   def __init__ ( self, network, port, name, hostName, serverName, realName ):

      self.network =  network

      self.port = port

      self.hostName = hostName

      self.serverName = serverName

      self.realName = realName

      self.socket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

      self.socket.connect ( ( self.network, self.port ) )

      self.nick ( name )

      self.send ( 'USER ' + self.name + ' ' + self.serverName + ' ' + self.hostName + ' :' + self.realName )

   def quit ( self ):

      self.send ( 'QUIT' )

      self.socket.close()

   def send ( self, text ):

      self.socket.send ( text + '\r\n' )

   def nick ( self, name ):

      self.name = name

      self.send ( 'NICK ' + self.name )

   def recv ( self, size = 2048 ):

      commands = self.socket.recv ( size ).split ( '\r\n' )

      if len ( self.partial ):

         commands [ 0 ] = self.partial + commands [ 0 ]

         self.partial = ''

      if len ( commands [ -1 ] ):

         self.partial = commands [ -1 ]

         self.queue.extend ( commands [ :-1 ] )

      else:

         self.queue.extend ( commands )

   def retrieve ( self ):

      if len ( self.queue ):

         command = self.queue [ 0 ]

         self.queue.pop ( 0 )

         return command

      else:

         return False

   def dismantle ( self, command ):

      if command:

         source = command.split ( ':' ) [ 1 ].split ( ' ' ) [ 0 ]

         parameters = command.split ( ':' ) [ 1 ].split ( ' ' ) [ 1: ]

         if not len ( parameters [ -1 ] ):

            parameters.pop()

         if command.count ( ':' ) > 1:

            parameters.append ( ''.join ( command.split ( ':' ) [ 2: ] ) )

         return source, parameters

   def privmsg ( self, destination, message ):

      self.send ( 'PRIVMSG ' + destination + ' :' + message )

   def notice ( self, destination, message ):

      self.send ( 'NOTICE ' + destination + ' :' + message )

   def join ( self, channel ):

      self.send ( 'JOIN ' + channel )

   def part ( self, channel ):

      self.send ( 'PART ' + channel )

   def topic ( self, channel, topic = '' ):

      self.send ( 'TOPIC ' + channel + ' ' + topic )

   def names ( self, channel ):

      self.send ( 'NAMES ' + channel )

   def invite ( self, nick, channel ):

      self.send ( 'INVITE ' + nick + ' ' + channel )

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

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

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

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

   def who ( self, pattern ):

      self.send ( 'WHO ' + pattern )

   def whois ( self, nick ):

      self.send ( 'WHOIS ' + nick )

   def whowas ( self, nick ):

      self.send ( 'WHOWAS ' + nick )

Now, as you can see, the recv method gets data from the socket and adds it to the queue variable. The dismantle method is responsible for pulling it all apart. It returns a tuple containing the source and a list of parameters (which is a bit misleading, since the first thing in the list is the command itself). It may seem like quite a task to go from here, but it isn't. All we need to do is match the first item in the list of parameters with a command and then go from there:

if parameterList [ 0 ] == 'DUMMY':

   pass

We are, however, required to come up with a method to retrieve and process data efficiently. This can be done with a bit of threading. One thread can call the recv method, and the main script can process the data:

import irchat

import thread

def autoRecv():

   while True:

      conn.recv()

conn = irchat.IRC ( 'irc.network.com', 6667, 'PyIRC', 'PyIRC', 'PyIRC', 'John Doe' )

thread.start_new_thread ( autoRecv, () )

while True:

   source, parameters = conn.dismantle ( conn.retrieve() )

   # Data should be processed here

Let's create a script that processes the “PRIVMSG” command, displaying data received from it:

import irchat

import thread

def autoRecv():

   while True:

      conn.recv()

conn = irchat.IRC ( 'irc.network.com', 6667, 'PyIRC', 'PyIRC', 'PyIRC', 'John Doe' )

thread.start_new_thread ( autoRecv, () )

conn.join ( '#pybot' )

while True:

   data = conn.dismantle ( conn.retrieve() )

   if data:

      if data [ 1 ] [ 0 ] == 'PRIVMSG':

         print data [ 0 ] + '->', data [ 1 ] [ 2 ]

Conclusion

I've explained basic IRC commands beyond those explained in “Python and IRC” and have presented a method to send and receive those commands. In the process, we have created a module to assist us in the development of IRC applications. What you do from here on is totally up to you, but if you ever need a reference, see RFC 1459, which explains the Internet Relay Chat Protocol:

http://www.irchelp.org/irchelp/rfc/rfc.html

Once again, good luck.



 
 
>>> 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: