Python
  Home arrow Python arrow Page 5 - Basic IRC Tasks
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Basic IRC Tasks
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2005-04-18


    Table of Contents:
  • Basic IRC Tasks
  • Communication Tasks
  • Channel Related Tasks
  • User Related Tasks
  • Processing Information

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Basic IRC Tasks - Processing Information
    ( Page 5 of 5 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek