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 # Handle a CTCP query # Check to see if it is a "VERSION" query # Reply to the query, giving version information # Create and IRC object and add the handler # Connect # Loop 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 network = 'irc.freenode.net def handleCTCP ( connection, event ): if event.arguments() [ 0 ].upper() == 'VERSION': # Check to see if it is a "TIME" query # Reply to the query, giving the time irc = irclib.IRC() server = irc.server() irc.process_forever()
blog comments powered by Disqus |
|
|
|
|
|
|
|