2

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.

joaquin
  • 82,968
  • 29
  • 138
  • 152
Felix
  • 25
  • 3
  • Please, post relevant code. I'm sure you didn't mess up the logging so it's probably an error inside whois or the arguments userJoined is receiving. – kaoD Feb 01 '11 at 18:06
  • Sorry about that. Here is the pastebin: http://pastebin.com/P48DXWaJ – Felix Feb 01 '11 at 18:13
  • The problem is probably in the whois method. It's part of the library, isn't it? Is logging the username working? Try doing a whois on yourself upon login and see the results. Can you link to the docs or at least webpage of Twisted's IRC module? I am unable to find it. – kaoD Feb 01 '11 at 18:33

1 Answers1

2

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.

Community
  • 1
  • 1
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122