cinchrb / cinch

The IRC Bot Building Framework
http://www.rubydoc.info/gems/cinch
MIT License
1k stars 180 forks source link

Make IRC Capabilities configurable #217

Closed romanzipp closed 7 years ago

romanzipp commented 8 years ago

I was getting in some struggle while connecting to a IRCv3 server where i needed to request custom IRC capabilities.

For this I worked something out:

cinch/configuration/bot.rb: Line 14

  def self.default_config
    {
      :server => "localhost",
      :port   => 6667,
      # ...
      :caps => []
    }

cinch/irc.rb: Line 114

    def send_cap_req
      # Getting capabilities from bot config
      caps = @bot.config.caps & @network.capabilities
      if caps.size > 0
        send "CAP REQ :" + caps.join(" ")
      else
        send_cap_end
      end
    end

Example:

bot = Cinch::Bot.new do
    configure do |c|
        c.server    = "irc.example.com"
        c.password  = "password"
        c.channels  = ["#channel"]
        c.nick      = "nick"
        c.verbose   = true
        # awesome custom caps
        c.caps      = [:"example.com/customcap1", :"example.com/customcap2"]
    end
    # ...
end
dominikh commented 8 years ago

The issue with client capabilities is that most of them need to be supported and handled by Cinch itself and can't be dealt with in user code, and enabling the wrong ones could break it. What client capabilities are you trying to enable that do not need to be handled by Cinch itself?

romanzipp commented 8 years ago

There are some Caps that must be sent to receive specific user details etc. on Twitch IRC. https://github.com/justintv/Twitch-API/blob/master/IRC.md#twitch-capabilities

dominikh commented 8 years ago

Okay, looking at that list:

Do you see any problem with enabling twitch.tv/membership and twitch.tv/commands unconditionally in Cinch, and not providing a way for users to specify their own caps?

Defman21 commented 8 years ago

Would want to see tags support in Cinch. I assume it's part of IRC v3.2? #201

erichie commented 8 years ago

How would you enable twitch.tv/membership? I can't figure out how to set or enable that.

romanzipp commented 8 years ago

@dominikh you're right, because of the tag cap, you can't access some message methods anymore like message.user

romanzipp commented 8 years ago

@erichie just edit the cinch sourcecode as mentioned above. You will need to download the cinch repository in order to do that. When installed it with gem, you could also search for the install path.

As @dominikh mentioned, some methods will break or even the whole script won't run. I haven't had any issues with the twitch capatbilities.

When enabling all caps, you will not have access to several message methods. Here's my example:

on :catchall do |m|
    mode = m.raw.scan(/twitch\.tv[\W]([A-Z]{1,})[\W]#/)[0]
    case mode
        when 'PRIVMSG'
            user     = m.raw.scan(/@([A-z0-9_]{1,})\./)[0]
            message  = m.raw.scan(/PRIVMSG #[A-z0-9_]{1,} :(.*)/)[0]
            time     = m.time
            if !Bot.userExcluded(user)
                puts "#{time} - <#{user}> #{message}"
            end

        when 'CLEARCHAT'
            user         = m.raw.scan(/CLEARCHAT #[A-z0-9_]{1,} :(.*)/)[0]
            message      = m.raw.scan(/@([A-z0-9_]{1,})\./)[0]
            duration     = m.raw.scan(/ban-duration=([0-9]{1,});/)[0]
            reason       = m.raw.scan(/ban-reason=([\w\d\\s,]{1,}) :/)[0]
            time         = m.time
            puts "#{time} - <#{user}> #{message} (#{duration}, #{reason})"
    end
end