Python
  Home arrow Python arrow Page 3 - IRC on a Higher Level Continued
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

IRC on a Higher Level Continued
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2005-10-19


    Table of Contents:
  • IRC on a Higher Level Continued
  • More Action Methods
  • Client-To-Client Protocol
  • Sending to Another Client

  • 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


    IRC on a Higher Level Continued - Client-To-Client Protocol
    ( Page 3 of 4 )

    The client-to-client  (CTCP) protocol allows clients to send each other special messages and queries. For example, I'm sure you've encountered the command “/me action” (or, alternatively, “/describe #channel action”). This command utilizes the client-to-client protocol.

    We can utilize CTCP in our application. For example, one common CTCP query is “VERSION”. This requests specifics about the target user's IRC client. If someone queries your application, he or she will not get any data unless you work a response into your application. This is very simple to do. All it involves is setting up a function to handle a CTCP message and then checking to see if the “VERSION” command is part of the message. If it is, then we need to send a reply to the source. Let's put this into Python by building a simple application:

    import irclib

    # Connection data
    network = 'irc.freenode.net'
    port = 6667
    channel = '#irclib'
    nick = 'PyTest'
    name = 'Python Test'

    # Handle a CTCP query
    def handleCTCP ( connection, event ):

       # Check to see if it is a "VERSION" query
       if event.arguments() [ 0 ].upper() == 'VERSION':

          # Reply to the query, giving version information
          connection.ctcp_reply ( event.source().split ( '!' ) [ 0 ], 'VERSION Python-IRCLib' )

    # Create and IRC object and add the handler
    irc = irclib.IRC()
    irc.add_global_handler ( 'ctcp', handleCTCP )

    # Connect
    server = irc.server()
    server.connect ( network, port, nick, ircname = name )
    server.join ( channel )

    # Loop
    irc.process_forever()

    Send your application a “VERSION” request (“/ctcp PyTest VERSION”) and check the response. As you can see, Python-IRCLib uncomplicates the process. We simply set our application up to handle the appropriate event. The function called then checks to see whether the “VERSION” request was sent. If it was, it responds with the ctcp_reply method. The ctcp_reply method takes two arguments, the target and the message.

    Another common query is the “TIME” query. When your application receives a “TIME” query, it should return a string with the current local time. A readable string must be sent, not a timestamp. Let's modify our test application to respond to “TIME” queries:

    import irclib
    import time

    network = 'irc.freenode.net
    port = 6667
    channel = '#irclib'
    nick = 'PyTest'
    name = 'Python Test'

    def handleCTCP ( connection, event ):

       if event.arguments() [ 0 ].upper() == 'VERSION':
          connection.ctcp_reply ( event.source().split ( '!' ) [ 0 ], 'VERSION Python-IRCLib' )

       # Check to see if it is a "TIME" query
       elif event.arguments() [ 0 ].upper() == 'TIME':

          # Reply to the query, giving the time
          connection.ctcp_reply ( event.source().split ( '!' ) [ 0 ], 'TIME ' + time.ctime() )

    irc = irclib.IRC()
    irc.add_global_handler ( 'ctcp', handleCTCP )

    server = irc.server()
    server.connect ( network, port, nick, ircname = name )
    server.join ( channel )

    irc.process_forever()



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