Python
  Home arrow Python arrow Page 4 - 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 - Sending to Another Client
    ( Page 4 of 4 )

    Now, what if your application needs to send another client a CTCP query? First, you would call the ctcp method. Then, you would have to catch the client's reply to your query –- if any. This is easily done. Let's build a simple application that sends another user a “TIME” query:

    import irclib

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

    # Handle the reply
    def handleReply ( connection, event ):

       print event.source().split ( '!' ) [ 0 ] + '-> ' + event.arguments() [ 0 ] + ': ' + event.arguments() [ 1 ]

    # Create an IRC object
    irc = irclib.IRC()
    irc.add_global_handler ( 'ctcpreply', handleReply )

    # Create a server object and connect
    server = irc.server()
    server.connect ( network, port, nick, ircname = name )
    server.join ( channel )

    # Send the query
    # Note that the username comes last
    server.ctcp ( 'TIME', 'User_Name' )

    # Loop
    irc.process_forever()

    Since the first argument sent in a reply will be the query type (“TIME”, etc.), the above handler will work on almost everything. You are not limited to the “TIME” command with the above application.

    There are a few other specified commands that you might be interested in using and responding to, so I'll describe them here. The first such command is the “ACTION” command, and I briefly mentioned it earlier. To send an “ACTION” command, you may use the action method. It is simply a shortcut method, and it is used like this:

    ...
    server.action ( '#channel', 'does something interesting.' )
    ...

    The “FINGER” command is sent to users to see how long they have been idle. The user's full name and idle time are supposed to be sent in the reply:

    ...
    server.ctcp ( 'FINGER', 'User_Name' )
    ...

    The “SOURCE” command is sent to users when you want to figure out where to obtain their client:

    ...
    server.ctcp ( 'SOURCE', 'User_Name' )
    ...

    The “PING” command is used to figure out how long it takes for a message to reach a given user. It takes a timestamp as an argument. The target client sends back the message in a reply, and the source client compares the received timestamp with the current time. The difference is the time in seconds that the message took to travel:

    ...
    import time
    ...
    def handleReply ( connection, event ):

       if event.arguments() [ 0 ].upper() == 'PING':
          print event.source().split ( '!' ) [ 0 ] + '-> ' + str ( time.time() - float ( event.arguments() [ 1 ] ) )
       else:
          print event.source().split ( '!' ) [ 0 ] + '-> ' + event.arguments() [ 0 ] + ': ' + event.arguments() [ 1 ]
    ...
    irc.add_global_handler ( 'ctcpreply', handleReply )
    ...
    server.ctcp ( 'PING', 'User_Name', str ( time.time() ) )
    ...

    The “ERRMSG” comand is used as a reply. If your application receives a CTCP query that it is not familiar with, you might want to consider sending an “ERRMSG” command, along with a message explaining that the query type is not recognized:

    ...
    server.ctcp_reply ( 'ERRMSG', 'User_Name', 'Query uknown.' )
    ...

    If the command is received as a query, then you should reply saying that no error has ocurred.

    The “USERINFO” command asks for information about the user of the target client:

    ...
    server.ctcp ( 'USERINFO', 'User_Name' )
    ...

    When your application is given the command, you should return information about the user of your application, according to the IRC protocol documentation. However, instead of returning information about you for an automated bot, I don't see why you couldn't return information about your application:

    ...
    server.ctcp_reply ( 'USERINFO', 'User_Name', 'I am just a bot.' )
    ...

    Finally, the “CLIENTINFO” command is used to query a client for information about what commands it accepts:

    ...
    server.ctcp ( 'CLIENTINFO', 'User_Name' )
    ...

    You are, of course, not limited to the above commands in your application. You can create new commands that your applications can share with each other –- a medium for communication.

    Conclusion

    In the first article, you learned how to connect to an IRC network and stay connected. You also learned about events and how to catch and respond to them. In this article, you learned about methods that act as shortcuts around the IRC protocol, simplifying it so that a full understanding of the protocol is not necessary to develop an application that communicates through IRC. You also learned about the client-to-client protocol, which your application can use to query other users or other applications. Users and other applications can also query and communicate with your application.

    These skills allow for the construction of a variety of IRC applications, from simple and primitive to complex and complete. The library we are using eliminates much of the tricky work, allowing us to concentrate on the logic of the applications rather than the medium through which they are communicating.



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