Felix
Felix

Reputation: 25

Twisted python IRC logging

I'm making an IRC bot which will log the hostname of a user that just joined the channel, and then log it to a file. The method I'm trying to do this in, is perform a whois command, and eventually it will seperated the hostname, then resolve it to an IP, and also log that.

I'm fairly new to both Python and Twisted, and this is the part of my code that is supposed to log the hostname of the user who just joined (or just log the whois for now):

def userJoined(self, user, channel):
    self.logger.log("%s" % (self.whois(user)))

However, when I check the logs, it writes None. Does anybody know what's wrong with the code, and how to fix it? Thanks.

Upvotes: 2

Views: 960

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48335

The IRCClient.whois method always returns None. So what you're seeing is exactly what I would expect from this code. :)

IRCClient.whois sends a WHOIS command to the server. When it returns, the result is not known because the server has not sent it yet (it very likely has not yet even received the request).

In order to get the data in the response, you need to override a few methods on your IRCClient subclass.

The way a lot of information from the IRC server is exposed by IRCClient is via irc_-prefixed callback methods. For example, one of the several responses to a WHOIS IRC command, as documented by the IRC RFC, has the mnemonic RPL_WHOISCHANNELS. To get this response, you would override the irc_RPL_WHOISCHANNELS method. When the client receives this response from the server, the method is called with the parameters of the response.

See also this related question for more details about the irc_ callbacks.

Consult the IRC RFC for the list of all the responses you should expect (though various IRC servers may give you more or less). Then override the necessary methods.

Unfortunately, this is not nearly as convenient as a whois method which simply returns the user data, but it is what is necessary to get the information with IRCClient in its current form.

Upvotes: 2

Related Questions