Home arrow Python arrow Page 4 - IRC on a Higher Level Continued

Sending to Another Client - Python

In the last article, you were taught about events in Python-IRCLib. You were taught what some common events are, how to catch them and how to properly respond to them. While a grasp of events is crucial to developing applications that interact through IRC, it's important to realize that Python-IRCLib doesn't just deal with responding to events. The library is a few dozen kilobytes, and all of that space obviously does not contain code that deals with events. The library contains many more features, and we'll take a look at some of them in this article.

TABLE OF CONTENTS:
  1. IRC on a Higher Level Continued
  2. More Action Methods
  3. Client-To-Client Protocol
  4. Sending to Another Client
By: Peyton McCullough
Rating: starstarstarstarstar / 3
October 19, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: