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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PYTHON

Basic IRC Tasks
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · One thing though, your code does not take into account the IRC PING command, so your...
       · The source (...!...@...) on the beginning of the message is not mandatory. The first...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway