Sha256: 1f49e2624527d563d645d6612c597ebf2efb4caca04327ff64147dd99206e9dd

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'http'
require 'nokogiri'

ProtonBot::Plugin.new do
  @name        = "Titler"
  @version     = ProtonBot::Titler::VERSION
  @description = 'A URL resolver for ProtonBot. Filters most bots automatically, so no botloops.'

  hook(type: :privmsg) do |dat|
    #core.privmsg_patch(dat)
    unless /(bot|serv)/i.match("#{dat[:nick]}!#{dat[:user]}@#{dat[:host]}")
      regex = %r((?:(?:https?):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?)i
      pattern = "%N%B[ :NICK%N#:URLNUM %B|%N :OUT %B]%N"
      dat[:message].scan(regex).each_with_index do |v, k|
        unless %r(http[s]?://).match(v)
          v = 'http://' + v
        end
        out = ''
        err = nil
        begin
          res  = HTTP.get(v)
          while [301, 302, 303, 307, 308].include? res.code
            res = HTTP.get(res["Location"])
          end
        rescue HTTP::ConnectionError => e
          err = e
        end
        if err
          dat.reply(pattern
            .gsub(':NICK', dat[:nick])
            .gsub(':URLNUM', k.to_s)
            .gsub(':OUT', "%C%RED\u200B#{err.message}%N"))
        elsif [200, 201, 203, 204, 205, 206, 207, 208, 226, 304].include?(res.code) && res["Content-Type"][/text\/html.*/]
          body = res.body.to_s
          html = Nokogiri::HTML(body)
          dat.reply(pattern
            .gsub(':NICK', dat[:nick])
            .gsub(':URLNUM', k.to_s)
            .gsub(':OUT', "%C%GREEN\u200B" + html.title + '%N'))
        else
          dat.reply(pattern
            .gsub(':NICK', dat[:nick])
            .gsub(':URLNUM', k.to_s)
            .gsub(':OUT', "%C%ORANGE\u200B#{res.code}%N"))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
protonbot-titler-0.1.1 lib/protonbot/titler/plugin.rb