plugins/dictbot.nb
# -*- ruby; coding: utf-8 -*- vim:set ft=ruby:
=begin
This plugin is test version.
=end
require 'uri'
require 'open-uri'
require 'kconv'
class DictBot < Nadoka::NDK_Bot
def bot_initialize
@available_channel = @bot_config[:ch] || /.*/
@nkf = @bot_config[:nkf] || "-Wj"
end
def on_privmsg prefix, ch, msg
if @available_channel === ch
msg = msg.toutf8
if /\Adic(.)>\s*(.+)\s*/ =~ msg
res = yahoo_dict $1, $2
send_notice(ch, NKF.nkf(@nkf, res))
end
end
end
YAHOO_DICT_TYPE ={
't' => 2,
'e' => 1,
'j' => 0,
'w' => 3,
'r' => 5,
}
def yahoo_dict type, word
"dict bot> " +
if type = YAHOO_DICT_TYPE[type]
word = URI.encode(word)
uri = "http://dic.yahoo.co.jp/dsearch?ei=UTF-8&p=#{word}&stype=0&dtype=#{type}"
open(uri){|f|
if /<meta name="description" content=\"(.+?)\">/ =~ f.read
"#{$1} - #{uri}"
else
uri
end
}
else
"unknown type: #{type}"
end
end
end