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 # Handle the reply print event.source().split ( '!' ) [ 0 ] + '-> ' + event.arguments() [ 0 ] + ': ' + event.arguments() [ 1 ] # Create an IRC object # Create a server object and connect # Send the query # Loop 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: ... 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: ... The “SOURCE” command is sent to users when you want to figure out where to obtain their client: ... 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: ... if event.arguments() [ 0 ].upper() == 'PING': 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: ... 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: ... 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: ... Finally, the “CLIENTINFO” command is used to query a client for information about what commands it accepts: ... 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|