class Bbiff::Executable

Constants

RETRY_INTERVAL_SECONDS

Public Class Methods

new() click to toggle source
# File lib/bbiff/executable.rb, line 76
def initialize
  @settings  = Settings.new
end

Public Instance Methods

get_board_settings(board) click to toggle source
# File lib/bbiff/executable.rb, line 94
def get_board_settings(board)
  return board.settings
rescue Bbs::Downloader::DownloadFailure => e
  STDERR.puts "Warning: 以下の場所から掲示板の設定が取得できませんでした。(#{e.response.message})"
  STDERR.puts board.settings_url
  return {'BBS_TITLE'=>'<不明>'}
end
main() click to toggle source
# File lib/bbiff/executable.rb, line 187
def main
  args = []
  ARGV.each do |arg|
    case arg
    when '-h', '--help'
      raise UsageError
    when '-v', '--version'
      version
      exit 0
    when '--no-render'
      @settings.current['no_render'] = true
    when '--debug'
      $DEBUG = true
    when '--long-polling'
      @settings.current['long_polling'] = true
    when /\A--delay-seconds=(.+)\z/
      s = $1
      if s =~ /\A\d+\z/
        @settings.current['delay_seconds'] = s.to_i
      else
        STDERR.puts "delay-seconds must be a non-negative integer"
        raise UsageError
      end
    when /\A-/
      STDERR.puts "invalid option #{arg.inspect}"
      raise UsageError
    else
      args << arg
    end
  end

  if args.size < 1
    raise UsageError
  else
    url = args[0]
  end

  begin
    thread = Bbs::create_thread(url)
    @settings.current['thread_url'] = url
  rescue Bbs::Downloader::DownloadFailure => e
    STDERR.puts "#{e.response.code} #{e.response.msg}: #{e.response.uri}"
    exit 1
  rescue => e
    STDERR.puts e.message
    exit 1
  end
  if thread.nil?
    STDERR.puts "スレッドのURLとして解釈できませんでした。(#{url})"
    exit 1
  end

  start_no = args[1] ? args[1].to_i : thread.last + 1
  start_polling(thread, start_no)
rescue UsageError
  usage
  exit 1
end
parse_range(str) click to toggle source
# File lib/bbiff/executable.rb, line 80
def parse_range(str)
  if str == "all"
    1..Float::INFINITY
  elsif str =~ /^\d+$/
    str.to_i..str.to_i
  elsif str =~ /^\d+-$/
    str.to_i..Float::INFINITY
  elsif str =~ /^(\d+)-(\d+)$/
    $1.to_i..$2.to_i
  else
    fail ArgumentError
  end
end
show(title, post) click to toggle source
# File lib/bbiff/executable.rb, line 102
def show(title, post)
  notify_send = ENV['BBIFF_NOTIFY_SEND'] || (`which notify-send` != "" ? 'notify-send' : 'echo')
  system("#{notify_send} #{Shellwords.escape(title)} #{Shellwords.escape(render_post(post))}")
end
start_polling(thread, start_no) click to toggle source
# File lib/bbiff/executable.rb, line 109
def start_polling(thread, start_no)
  out = LineIndicator.new
  begin
    delay = @settings.current['delay_seconds']
    board_settings = get_board_settings(thread.board)
    thread_stop = (board_settings['BBS_THREAD_STOP'] || '1000').to_i

    puts "#{board_settings['BBS_TITLE']} − #{thread.title}(#{thread.last})"
    puts "    #{@settings.current['thread_url']}"

    loop do
      out.set_line "#{thread.title}(#{thread.last}) 新着レス確認中"

      posts = thread.posts(parse_range("#{start_no}-"),
        { long_polling: @settings.current['long_polling'] })
      t = Time.now
      posts.each do |post|
        out.puts "-----"
        unless @settings.current['no_render']
          puts render_post(post)
        end

        show(thread.title, post)
      end

      start_no = thread.last + 1
      if start_no > thread_stop
        out.puts "スレッドストップ"
        break
      end

      d = [delay-(Time.now-t), 0].max.round
      d.times do |i|
        j = i + 1
        out.set_line "#{thread.title}(#{thread.last}) 待機中 [#{'.'*j}#{' '*(d - j)}]"
        sleep 1
      end
    end
  ensure
    out.close
  end
rescue Interrupt
  STDERR.puts "ユーザー割り込みにより停止"
rescue => e
  STDERR.print "Error: "
  STDERR.puts e.message
  STDERR.puts e.backtrace if $DEBUG
  STDERR.puts "#{RETRY_INTERVAL_SECONDS}秒後にリトライ"
  sleep RETRY_INTERVAL_SECONDS
  retry
end
usage() click to toggle source
# File lib/bbiff/executable.rb, line 163
  def usage
    STDERR.puts "Usage: bbiff [OPTIONS] [http://jbbs.shitaraba.net/bbs/read.cgi/CATEGORY/BOARD_ID/THREAD_ID/] [START_NUMBER]"

    STDERR.puts <<"EOD"

Bbiff version #{Bbiff::VERSION}
#{COPYRIGHT}

          -h, --help
          -v, --version
          --no-render
          --debug
          --long-polling (for Genkai)
          --delay-seconds=N
EOD
  end
version() click to toggle source
# File lib/bbiff/executable.rb, line 180
  def version
    STDERR.puts <<"EOD"
Bbiff version #{Bbiff::VERSION}
#{COPYRIGHT}
EOD
  end