| 1 | #!/usr/local/bin/ruby |
|---|
| 2 | # coding: UTF-8 |
|---|
| 3 | require 'rubygems' |
|---|
| 4 | require 'net/irc' |
|---|
| 5 | require 'kconv' |
|---|
| 6 | |
|---|
| 7 | require 'modules' |
|---|
| 8 | require 'monkey-patch/net-irc-client' |
|---|
| 9 | |
|---|
| 10 | # |
|---|
| 11 | # Notifier bridge module |
|---|
| 12 | # |
|---|
| 13 | module Notifier |
|---|
| 14 | class BasicNotifier |
|---|
| 15 | include Runnable |
|---|
| 16 | |
|---|
| 17 | # notify message via the notifier object. |
|---|
| 18 | def notify(msg) |
|---|
| 19 | puts "BasicNotifier says: #{msg}" |
|---|
| 20 | end |
|---|
| 21 | end # Notifier::BasicNotifier |
|---|
| 22 | |
|---|
| 23 | class IRCClient < BasicNotifier; end |
|---|
| 24 | |
|---|
| 25 | # |
|---|
| 26 | # IRC client |
|---|
| 27 | # |
|---|
| 28 | class IRCClient::Client < Net::IRC::Client |
|---|
| 29 | attr_accessor :channel, :msgq |
|---|
| 30 | attr_accessor :owner |
|---|
| 31 | |
|---|
| 32 | def initialize(*args) |
|---|
| 33 | super |
|---|
| 34 | @channel = @opts.channel if !@opts.channel.nil? |
|---|
| 35 | @msgq = @opts.msgq if !@opts.msgq.nil? |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | def on_idle |
|---|
| 39 | return if @msgq.empty? |
|---|
| 40 | msg = @msgq.shift |
|---|
| 41 | post NOTICE, @channel, msg |
|---|
| 42 | end |
|---|
| 43 | |
|---|
| 44 | def on_rpl_welcome(m) |
|---|
| 45 | post JOIN, @channel |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | def on_privmsg(m) |
|---|
| 49 | channel, message = m.to_a |
|---|
| 50 | nick = m.prefix.nick |
|---|
| 51 | |
|---|
| 52 | return unless channel == @channel |
|---|
| 53 | return unless /peerbot/ =~ message |
|---|
| 54 | |
|---|
| 55 | result = [] |
|---|
| 56 | rest = message.dup |
|---|
| 57 | rest.gsub!(/AS(\d+)/i) do |s| |
|---|
| 58 | asn = $1.to_i |
|---|
| 59 | if resolver.has_key?(asn) then |
|---|
| 60 | result.push "#{s.upcase}: #{resolver[asn]}" |
|---|
| 61 | end |
|---|
| 62 | '' |
|---|
| 63 | end |
|---|
| 64 | if !result.empty? then |
|---|
| 65 | post NOTICE, @channel, result.join(', ') |
|---|
| 66 | end |
|---|
| 67 | rescue |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | def on_disconnected |
|---|
| 71 | end |
|---|
| 72 | |
|---|
| 73 | private |
|---|
| 74 | |
|---|
| 75 | def resolver |
|---|
| 76 | @owner.resolver |
|---|
| 77 | # { 64512 => 'mera-chan' } |
|---|
| 78 | end |
|---|
| 79 | end # IRCClient::Client |
|---|
| 80 | |
|---|
| 81 | # |
|---|
| 82 | # IRC Client. Client class of Notifier |
|---|
| 83 | # |
|---|
| 84 | class IRCClient < BasicNotifier |
|---|
| 85 | extend Attributes |
|---|
| 86 | attributes :server, :port, :nick, :realname, :channel, :charcode |
|---|
| 87 | attr_reader :client, :thread, :msgq |
|---|
| 88 | attr_accessor :owner |
|---|
| 89 | |
|---|
| 90 | def initialize |
|---|
| 91 | init_attributes |
|---|
| 92 | @client = Client.new(@server, @port, { |
|---|
| 93 | :nick => @nick, |
|---|
| 94 | :user => @nick, |
|---|
| 95 | :real => @realname || @nick, |
|---|
| 96 | :channel => @channel, |
|---|
| 97 | :msgq => @msgq = Queue.new |
|---|
| 98 | }) |
|---|
| 99 | @client.owner = self if @client.respond_to? :owner= |
|---|
| 100 | |
|---|
| 101 | # <XXX> |
|---|
| 102 | @client.meta_def(:charconv_in) do |str| |
|---|
| 103 | Kconv.kconv(str, Kconv::UTF8, @charcode || Kconv::AUTO) |
|---|
| 104 | end |
|---|
| 105 | @client.meta_def(:charconv_out) do |str| |
|---|
| 106 | return str if @charcode.nil? |
|---|
| 107 | Kconv.kconv(str, @charcode, Kconv::UTF8) |
|---|
| 108 | end |
|---|
| 109 | # </XXX> |
|---|
| 110 | super |
|---|
| 111 | end |
|---|
| 112 | |
|---|
| 113 | def run |
|---|
| 114 | @thread = Thread.start { @client.start } |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | def shutdown |
|---|
| 118 | @thread.kill |
|---|
| 119 | end |
|---|
| 120 | |
|---|
| 121 | def notify(msg) |
|---|
| 122 | @msgq << msg |
|---|
| 123 | end |
|---|
| 124 | |
|---|
| 125 | def resolver |
|---|
| 126 | @owner.resolver |
|---|
| 127 | end |
|---|
| 128 | end # Notifier::IRCClient |
|---|
| 129 | |
|---|
| 130 | # |
|---|
| 131 | # Twitter Client. Client class of Notifier |
|---|
| 132 | # |
|---|
| 133 | class TwitterNotifier < BasicNotifier |
|---|
| 134 | extend Attributes |
|---|
| 135 | attributes :user, :password, :hash_tag |
|---|
| 136 | attr_reader :twitter, :auth |
|---|
| 137 | require 'twitter' |
|---|
| 138 | |
|---|
| 139 | def initialize |
|---|
| 140 | init_attributes |
|---|
| 141 | @auth = Twitter::HTTPAuth.new(@user, @password) |
|---|
| 142 | @twitter = Twitter::Base.new(@auth) |
|---|
| 143 | super |
|---|
| 144 | end |
|---|
| 145 | |
|---|
| 146 | def notify(msg) |
|---|
| 147 | msg += ' ' + @hash_tag unless @hash_tag.nil? |
|---|
| 148 | begin |
|---|
| 149 | @twitter.update(msg) |
|---|
| 150 | rescue => e |
|---|
| 151 | # log error |
|---|
| 152 | end |
|---|
| 153 | end |
|---|
| 154 | end # Notifier::TwitterClient |
|---|
| 155 | end # Notifier |
|---|