nadoka/nadoka

View on GitHub
plugins/pastebot.nb

Summary

Maintainability
Test Coverage
=begin

This plugin is test version.

=end


require 'open-uri'

class PasteBot < Nadoka::NDK_Bot

  def bot_initialize
    @ch          = @bot_config[:ch]  || /./
    @msg         = @bot_config[:mgs] || /\Apaste>/
    @service_uri = @bot_config[:service_uri] ||
                   'http://www.atdot.net/sp'
    @fmsg         = @bot_config[:mgs] || /\Afpaste>/
    @fservice_uri = @bot_config[:service_uri] ||
                   'http://www.atdot.net/fp'
  end

  def nick_escape nick
    nick.gsub(/[^A-Za-z\d\-_]/, '_')
  end

  def on_privmsg prefix, ch, msg
    nick = nick_escape prefix.nick

    if @ch === ch
      if @msg === msg
        nid = ''
        open("#{@service_uri}/check/newid"){|f|
          nid = f.gets
        }
        send_notice ch, "#{@service_uri}/view/#{nid}_#{nick}"
      end
      if @fmsg === msg
        nid = ''
        open("#{@fservice_uri}/check/newid"){|f|
          nid = f.gets
        }
        send_notice ch, "#{@fservice_uri}/view/#{nid}_#{nick}"
      end
    end
  end
end