/usr/share/nadoka/plugins/opensearchbot.nb is in nadoka 0.7.6-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # -*-ruby-*- #
# Copyright (C) 2011 Kazuhiro NISHIYAMA
#
# This program is free software with ABSOLUTELY NO WARRANTY.
# You can re-distribute and/or modify this program under
# the same terms of the Ruby's license.
#
=begin
== Usage with irc client
bing> keyword
-> search keyword by bing
googlecode> keyword
-> search keyword by Google Code Search Data API (Deprecated)
koders> keyword
-> search keyword by koders
== Configuration:
BotConfig << {
:name => :OpenSearchBot,
:bot_name => 'bing',
:ch => //,
:referer => 'http://rubyforge.org/projects/nadoka/',
:ch_kcode => :jis,
:html => 'http://www.bing.com/search?q={searchTerms}',
:rss => 'http://api.search.live.com/rss.aspx?source=web&query={searchTerms}'
,
}
BotConfig << {
:name => :OpenSearchBot,
:bot_name => 'googlecode',
:ch => //,
:ch_kcode => :jis,
:referer => 'http://rubyforge.org/projects/nadoka/',
# Google Code Search Data API (Deprecated)
# http://code.google.com/intl/ja/apis/codesearch/docs/2.0/developers_guide.h
tml
:html => 'http://www.google.com/codesearch?q={searchTerms}',
:rss => 'http://www.google.com/codesearch/feeds/search?q={searchTerms}',
}
BotConfig << {
:name => :OpenSearchBot,
:bot_name => 'koders',
:ch => //,
:referer => 'http://rubyforge.org/projects/nadoka/',
:ch_kcode => :jis,
# http://www.koders.com/search/KodersDescriptionOS1_1.xml
:html => 'http://www.koders.com/?s={searchTerms}',
:rss => 'http://www.koders.com/?s={searchTerms}&results=code&output=rss&OSve
rsion=1.1',
}
=end
require 'open-uri'
require 'uri'
require 'cgi'
class OpenSearch
def initialize(options)
@html = options[:html]
@rss = options[:rss]
@referer = options[:referer] || 'http://rubyforge.org/projects/nadoka/'
end
def result(key)
escaped_key = CGI.escape(key)
link = @html.sub(/\{searchTerms\}/) { escaped_key }
uri = @rss.sub(/\{searchTerms\}/) { escaped_key }
open(uri, "Referer" => @referer) do |f|
result = f.read
if /<([A-Za-z]+):totalResults>(\d+)<\/\1:totalResults>/ =~ result
total = $2.to_i
return "#{total} result#{total > 1 ? 's' : ''} in #{link}"
else
return "#{key} - not found in #{link}"
end
end
end
end
if __FILE__ == $0
h = {
'bing' => {
:referer => 'http://rubyforge.org/projects/nadoka/',
:html => 'http://www.bing.com/search?q={searchTerms}',
:rss => 'http://api.search.live.com/rss.aspx?source=web&query={searchTerms}',
},
'googlecode' => {
:referer => 'http://rubyforge.org/projects/nadoka/',
# Google Code Search Data API (Deprecated)
# http://code.google.com/intl/ja/apis/codesearch/docs/2.0/developers_guide.html
:html => 'http://www.google.com/codesearch?q={searchTerms}',
:rss => 'http://www.google.com/codesearch/feeds/search?q={searchTerms}',
},
'koders' => {
:referer => 'http://rubyforge.org/projects/nadoka/',
# http://www.koders.com/search/KodersDescriptionOS1_1.xml
:html => 'http://www.koders.com/?s={searchTerms}',
:rss => 'http://www.koders.com/?s={searchTerms}&results=code&output=rss&OSversion=1.1',
},
'youtube' => {
:referer => 'http://rubyforge.org/projects/nadoka/',
:html => 'http://www.youtube.com/results?search_query={searchTerms}',
:rss => 'http://gdata.youtube.com/feeds/api/videos?q={searchTerms}',
},
}
engine = ARGV.shift
if h.key?(engine)
open_search = OpenSearch.new(h[engine])
ARGV.each do |key|
result = open_search.result(key)
puts result
end
else
STDERR.puts "usage: #{$0} {#{h.keys.sort.join('|')}} key ..."
end
exit
end
class OpenSearchBot < Nadoka::NDK_Bot
def bot_initialize
if @bot_config.key?(:channels)
channels = '\A(?:' + @bot_config[:channels].collect{|ch|
Regexp.quote(ch)
}.join('|') + ')\z'
@available_channel = Regexp.compile(channels)
else
@available_channel = @bot_config[:ch] || //
end
@bot_name = @bot_config[:bot_name] || 'OpenSearchBot'
@open_search = OpenSearch.new(@bot_config)
@pattern = @bot_config[:pattern] || /\A#{Regexp.quote(@bot_name)}\s*[<:>]\s*(.+)/
@ch_kcode = @bot_config[:ch_kcode]
end
def on_privmsg prefix, ch, msg
if @pattern =~ msg
key = $1
if @ch_kcode == :jis
ret = @open_search.result(key.toutf8).tojis
else
ret = @open_search.result(key)
end
send_notice ch, "#{@bot_name} bot: #{ret}"
end
end
end
|