nadoka 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,11 @@
1
- #######################################################
2
1
  # Nadoka: IRC Client server program.
3
- #
4
- # Written by SASADA Koichi <ko1 at atdot.net>
5
- #
6
- #
7
- # $Id$
8
- #
9
2
 
10
- * What's this?
3
+ Written by SASADA Koichi <ko1 at atdot.net>
4
+
5
+ ## What's this?
11
6
 
12
7
  Nadoka is IRC Client Server program.
13
- It's same concept as [[http://www.madoka.org/][madoka]].
8
+ It's same concept as [madoka](http://www.madoka.org/).
14
9
 
15
10
  You can do with this software:
16
11
 
@@ -18,14 +13,10 @@ You can do with this software:
18
13
  - easy to make a bot with ruby
19
14
 
20
15
 
21
- * more about
16
+ ## more about
22
17
 
23
18
  See Web pages:
19
+
24
20
  - https://github.com/nadoka/nadoka
25
21
  - http://rubyforge.org/projects/nadoka/
26
22
  - http://www.atdot.net/nadoka/
27
-
28
-
29
- : Thank you,
30
- : --
31
- : SASADA Koichi at atdot dot net
data/nadoka.rb CHANGED
@@ -16,14 +16,6 @@
16
16
  # Create : K.S. 03/07/10 20:29:07
17
17
  #
18
18
 
19
- unless "".respond_to?(:force_encoding)
20
- class String
21
- def force_encoding(enc)
22
- self
23
- end
24
- end
25
- end
26
-
27
19
  $LOAD_PATH.unshift File.dirname(__FILE__)
28
20
  require 'ndk/version'
29
21
 
data/ndk/config.rb CHANGED
@@ -58,7 +58,7 @@ module Nadoka
58
58
  Away_Message = 'away'
59
59
  Away_Nick = nil
60
60
 
61
- Quit_Message = "Quit Nadoka #{::Nadoka::NDK_Version} - http://www.atdot.net/nadoka/"
61
+ Quit_Message = "Quit Nadoka #{::Nadoka::NDK_Version}"
62
62
 
63
63
  #
64
64
  Channel_info = {}
@@ -418,7 +418,11 @@ module Nadoka
418
418
 
419
419
  def identical_channel_name ch
420
420
  # use 4 gsub() because of the compatibility of RFC2813(3.2)
421
- ch.toeuc.downcase.tr('[]\\~', '{}|^').tojis.force_encoding('ASCII-8BIT')
421
+ ch = ch.toeuc.downcase.tr('[]\\~', '{}|^').tojis
422
+ if ch.respond_to?(:force_encoding)
423
+ ch.force_encoding('ASCII-8BIT')
424
+ end
425
+ ch
422
426
  end
423
427
 
424
428
  RName = { # ('&','#','+','!')
data/ndk/version.rb CHANGED
@@ -11,12 +11,21 @@
11
11
  #
12
12
 
13
13
  module Nadoka
14
- VERSION = '0.8.0'
14
+ VERSION = '0.8.1'
15
15
  NDK_Version = VERSION.dup
16
16
  NDK_Created = Time.now
17
17
 
18
18
  if File.directory?(File.expand_path('../../.git', __FILE__))
19
- NDK_Version.concat("+git")
19
+ git_describe = nil
20
+ Dir.chdir(File.expand_path('../..', __FILE__)) do
21
+ git_describe = `git describe --tags --long --dirty=-dt`
22
+ if $?.success?
23
+ git_describe = "(#{git_describe.strip})"
24
+ else
25
+ git_describe = nil
26
+ end
27
+ end
28
+ NDK_Version.concat("+git#{git_describe}")
20
29
  end
21
30
  if /trunk/ =~ '$HeadURL$'
22
31
  NDK_Version.concat('-trunk')
data/plugins/googlebot.nb CHANGED
@@ -162,6 +162,7 @@ class GoogleBot < Nadoka::NDK_Bot
162
162
  e = result["responseData"]["results"][0]
163
163
  url = e['unescapedUrl'] || e['url'] || e['postUrl']
164
164
  title = show_char_code_and_erase_tag(e['titleNoFormatting'])
165
+ url = shorten_url(url)
165
166
  "#{title} - #{url} (and #{count} hit#{(count.to_i > 1) ? 's' : ''})"
166
167
  else
167
168
  "no match"
@@ -342,4 +343,14 @@ class GoogleBot < Nadoka::NDK_Bot
342
343
  raise
343
344
  end
344
345
  end
346
+
347
+ def shorten_url(url)
348
+ case url
349
+ when %r!\Ahttp://www\.amazon\.co\.jp/.*(/dp/.+)\z!
350
+ "http://amazon.jp#{$1}"
351
+ else
352
+ # default: do nothing
353
+ url
354
+ end
355
+ end
345
356
  end
data/plugins/titlebot.nb CHANGED
@@ -1,6 +1,7 @@
1
1
  # -*-ruby-*-
2
+ # vim:set ft=ruby:
2
3
  #
3
- # Copyright (c) 2009, 2011 Kazuhiro NISHIYAMA
4
+ # Copyright (c) 2009, 2011, 2012 Kazuhiro NISHIYAMA
4
5
  #
5
6
  # This program is free software with ABSOLUTELY NO WARRANTY.
6
7
  # You can re-distribute and/or modify this program under
@@ -19,10 +20,14 @@ Reply title of URL.
19
20
  :name => :TitleBot,
20
21
  :ch => //,
21
22
  :timeout => 10,
23
+ :headers => {
24
+ #"User-Agent" => "Ruby/#{RUBY_VERSION}",
25
+ }
22
26
  }
23
27
 
24
28
  =end
25
29
 
30
+ require 'iconv'
26
31
  require 'nkf'
27
32
  require 'open-uri'
28
33
  require 'timeout'
@@ -37,7 +42,7 @@ end
37
42
  module URL2Title
38
43
  module_function
39
44
 
40
- def get_title(url)
45
+ def get_title(url, headers)
41
46
  uri = URI(url)
42
47
  info = { :uri => uri }
43
48
  info[:errors] = []
@@ -46,7 +51,10 @@ module URL2Title
46
51
  info[:title] = "(ignored)"
47
52
  return info
48
53
  end
49
- uri.open(:content_length_proc => proc{|x| raise Errno::EFBIG if x && x > 1048576}) do |f|
54
+ headers = {
55
+ :content_length_proc => proc{|x| raise Errno::EFBIG if x && x > 1048576},
56
+ }.merge!(headers)
57
+ uri.open(headers) do |f|
50
58
  info[:uri] = f.base_uri
51
59
  body = f.read
52
60
 
@@ -71,15 +79,52 @@ module URL2Title
71
79
  body = Zlib::GzipReader.new(StringIO.new(body)).read || ''
72
80
  end
73
81
 
82
+
74
83
  # encoding
75
- if NKF.guess(body) == NKF::BINARY
76
- info[:title] = "(binary)"
77
- return info
84
+ begin
85
+ if charset
86
+ # ok
87
+ elsif /charset=[\'\"]?([^;\'\">]+)/ni =~ body
88
+ charset = $1
89
+ end
90
+ case charset
91
+ when /shift_jis/i
92
+ charset = "cp932"
93
+ when /euc-jp/i # euc-jp, x-euc-jp
94
+ charset = "eucjp-ms"
95
+ end
96
+ if /\Autf-8\z/i =~ charset
97
+ # avoid #<ArgumentError: invalid byte sequence in UTF-8>
98
+ # or Iconv::IllegalSequence
99
+ body = NKF.nkf("-Wwm0x", body)
100
+ elsif charset
101
+ charset.sub!(/\Ax-?/i, '')
102
+ body = Iconv.conv("utf-8", charset, body)
103
+ else
104
+ body = NKF.nkf("-wm0x", body)
105
+ end
106
+ rescue Iconv::IllegalSequence => e
107
+ info[:errors] << e
108
+ rescue Iconv::InvalidEncoding => e
109
+ info[:errors] << e
78
110
  end
79
- body = NKF.nkf("-wm0x --numchar-input", body)
80
111
 
81
112
  title = nil
82
113
  case uri.host
114
+ when /\A(?:twitter\.com)\z/
115
+ if %r"<title\b(?>[^<>]*)>(.*?)</title(?>[^<>]*)>"miu =~ body
116
+ title = $1
117
+ end
118
+ if defined?(::Nokogiri)
119
+ doc = Nokogiri::HTML(body, uri.to_s, 'utf-8')
120
+ tweet = doc.css('.tweet-text')
121
+ unless tweet.empty?
122
+ tweet = tweet.inner_html
123
+ tweet.gsub!(/<.*?>/, '')
124
+ tweet.strip!
125
+ title = tweet unless tweet.empty?
126
+ end
127
+ end
83
128
  when /\A(?:www\.so-net\.ne\.jp)\z/
84
129
  if %r"<title\b(?>[^<>]*)>(.*?)</title(?>[^<>]*)>"miu =~ body
85
130
  title = $1
@@ -138,17 +183,23 @@ module URL2Title
138
183
  url.sub(/\/\#!\//, '/')
139
184
  end
140
185
 
141
- def url2title(url)
186
+ def url2title(url, headers)
142
187
  url = prepare_url(url)
143
- info = get_title(url)
188
+ info = get_title(url, headers)
144
189
  info[:title] = truncate(info[:title])
145
190
  info
146
191
  end
147
192
  end
148
193
 
149
194
  if __FILE__ == $0
195
+ require File.expand_path('../../ndk/version', __FILE__)
150
196
  def u2t(url)
151
- URL2Title.url2title(url)
197
+ firefox = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0"
198
+ ua = "NadokaTitleBot/#{Nadoka::VERSION}"
199
+ headers = {
200
+ "User-Agent" => "#{firefox} Ruby/#{RUBY_VERSION} #{ua}",
201
+ }
202
+ URL2Title.url2title(url, headers)
152
203
  rescue
153
204
  $!.inspect
154
205
  end
@@ -179,9 +230,10 @@ class TitleBot < Nadoka::NDK_Bot
179
230
  end
180
231
 
181
232
  @same_bot = @bot_config.fetch(:same_bot, /(?!)/)
182
- @nkf_options = @bot_config.fetch(:nkf, "--oc=CP50221 --numchar-input --fb-xml")
233
+ @nkf_options = @bot_config.fetch(:nkf, "--oc=CP50221 --ic=UTF-8 --numchar-input --fb-xml")
183
234
  @timeout = @bot_config.fetch(:timeout, 10)
184
- @too_long_threshold = @bot_config.fetch(:too_long_threshold, 250)
235
+ @fragment_size_range = @bot_config.fetch(:fragment_size_range, 5..100)
236
+ @headers = @bot_config.fetch(:headers, {})
185
237
  end
186
238
 
187
239
  def send_notice(ch, msg)
@@ -195,12 +247,15 @@ class TitleBot < Nadoka::NDK_Bot
195
247
  def on_privmsg prefix, ch, msg
196
248
  return unless @available_channel === ch
197
249
 
198
- if /https?:/ === msg
250
+ case msg
251
+ when /^\S+>/
252
+ # ignore messages to other bots
253
+ when /https?:/
199
254
  return if @state.channel_users(ccn(ch)).find{|x| @same_bot =~ x }
200
255
 
201
256
  url, = URI.extract(msg, ["http", "https"])
202
257
  info = Timeout.timeout(@timeout) do
203
- url2title(url)
258
+ url2title(url, @headers)
204
259
  end
205
260
  return unless info[:title]
206
261
  if url != info[:uri].to_s
@@ -209,8 +264,8 @@ class TitleBot < Nadoka::NDK_Bot
209
264
  send_notice(ch, "title bot: #{info[:title]}")
210
265
  end
211
266
  if info[:fragment_text]
212
- # ignore when fragment_text is too long
213
- if info[:fragment_text].size < @too_long_threshold
267
+ # ignore when fragment_text is too long or too short
268
+ if @fragment_size_range === info[:fragment_text].size
214
269
  send_notice(ch, "title bot:: #{info[:fragment_text]}")
215
270
  end
216
271
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nadoka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-29 00:00:00.000000000 Z
13
+ date: 2012-05-29 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Nadoka is a tool for monitoring and logging IRC conversations and responding
16
16
  to specially formatted requests. You define and customize these responses in Ruby.
@@ -25,7 +25,7 @@ files:
25
25
  - .gitignore
26
26
  - ChangeLog.old
27
27
  - Gemfile
28
- - README.org
28
+ - README.md
29
29
  - Rakefile
30
30
  - bin/nadoka
31
31
  - lib/rss_check.rb
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project: nadoka
98
- rubygems_version: 1.8.11
98
+ rubygems_version: 1.8.23
99
99
  signing_key:
100
100
  specification_version: 3
101
101
  summary: IRC logger, monitor and proxy program ("bot")