6

I am trying to write a function that will print the lists of nicks in an IRC channel to the channel using Twisted Python. How do I do this? I have read the API documentation and I have only seen one question similar to mine on this site, but it doesn't really answer my question. If I knew how to get the userlist (or whatever it is Twisted recognizes it as), I could simply iterate the list using a for loop, but I don't know how to get this list.

paul
  • 61
  • 1
  • 2
  • 1
    dupe http://stackoverflow.com/questions/5305050/how-to-use-twisted-to-get-an-irc-channels-user-list – Peter Le Bek Jul 12 '11 at 22:55
  • This isn't a dupe, I even mentioned that question you linked in question, because it IS NOT what I am trying to do and is NOT helpful. – paul Jul 13 '11 at 01:28
  • It is in fact a dup of that other question; I'm curious why you think it isn't. – Glyph Jul 13 '11 at 04:46
  • It isn't a dupe because the other question is not the same and the answer doesn't work. I am talking about an irc BOT that use the IRCClient protocol, so it is different. – paul Jul 13 '11 at 05:22
  • But it is the same. That answer does work for your question, exactly as written. You need to be more specific in your question: what happens when you try that solution? Why doesn't it solve your problem? – Glyph Jul 13 '11 at 06:29

1 Answers1

6

The linked example you seem to think is the same, uses WHO, different command, different purpose. The correct way is to use NAMES.

Extended IRCClient to support a names command.

from twisted.words.protocols import irc
from twisted.internet import defer

class NamesIRCClient(irc.IRCClient):
    def __init__(self, *args, **kwargs):
        self._namescallback = {}

    def names(self, channel):
        channel = channel.lower()
        d = defer.Deferred()
        if channel not in self._namescallback:
            self._namescallback[channel] = ([], [])

        self._namescallback[channel][0].append(d)
        self.sendLine("NAMES %s" % channel)
        return d

    def irc_RPL_NAMREPLY(self, prefix, params):
        channel = params[2].lower()
        nicklist = params[3].split(' ')

        if channel not in self._namescallback:
            return

        n = self._namescallback[channel][1]
        n += nicklist

    def irc_RPL_ENDOFNAMES(self, prefix, params):
        channel = params[1].lower()
        if channel not in self._namescallback:
            return

        callbacks, namelist = self._namescallback[channel]

        for cb in callbacks:
            cb.callback(namelist)

        del self._namescallback[channel]

Example:

def got_names(nicklist):
    log.msg(nicklist)
self.names("#some channel").addCallback(got_names)
smackshow
  • 576
  • 6
  • 5