| 1 | #!/usr/bin/env ruby |
|---|
| 2 | $LOAD_PATH << "lib" |
|---|
| 3 | |
|---|
| 4 | require "net/irc" |
|---|
| 5 | |
|---|
| 6 | class Chokan < Net::IRC::Client |
|---|
| 7 | def on_privmsg(m) |
|---|
| 8 | super |
|---|
| 9 | target = m[0] |
|---|
| 10 | mes = m[1] |
|---|
| 11 | |
|---|
| 12 | case |
|---|
| 13 | when m.ctcp? |
|---|
| 14 | call_plugins :on_ctcp [m.prefix, target, ctcp_decoding(mes)] |
|---|
| 15 | when target =~ /^[^#+&!]/ |
|---|
| 16 | call_plugins :on_talk [m.prefix, target, mes] |
|---|
| 17 | else |
|---|
| 18 | call_plugins :on_privmsg [m.prefix, target, mes] |
|---|
| 19 | end |
|---|
| 20 | end |
|---|
| 21 | |
|---|
| 22 | def on_notice(m) |
|---|
| 23 | super |
|---|
| 24 | call_plugins :on_notice [m.prefix, m[0], m[1]] |
|---|
| 25 | end |
|---|
| 26 | |
|---|
| 27 | def on_join(m) |
|---|
| 28 | super |
|---|
| 29 | call_plugins :on_join [m.prefix, m[0]] |
|---|
| 30 | end |
|---|
| 31 | |
|---|
| 32 | def on_part(m) |
|---|
| 33 | super |
|---|
| 34 | call_plugins :on_part [m.prefix, m[0], m[1]] |
|---|
| 35 | end |
|---|
| 36 | |
|---|
| 37 | def on_kick(m) |
|---|
| 38 | super |
|---|
| 39 | call_plugins :on_kick [m.prefix, m[0], m[1], m[2]] |
|---|
| 40 | end |
|---|
| 41 | |
|---|
| 42 | def on_invite(m) |
|---|
| 43 | super |
|---|
| 44 | call_plugins :on_invite [m.prefix, m[0], m[1]] |
|---|
| 45 | end |
|---|
| 46 | |
|---|
| 47 | def on_nick(m) |
|---|
| 48 | super |
|---|
| 49 | call_plugins :on_nick [m.prefix, m[0]] |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | def on_mode(m) |
|---|
| 53 | negative_mode, positive_mode, = *super |
|---|
| 54 | call_plugins :on_mode [m.prefix, m[0], positive_mode, negative_mode] |
|---|
| 55 | end |
|---|
| 56 | |
|---|
| 57 | def on_connected |
|---|
| 58 | super |
|---|
| 59 | call_plugins :on_uped, [] |
|---|
| 60 | end |
|---|
| 61 | |
|---|
| 62 | def on_disconnected |
|---|
| 63 | super |
|---|
| 64 | call_plugins :on_downed, [] |
|---|
| 65 | end |
|---|
| 66 | |
|---|
| 67 | def call_plugins(name, args) |
|---|
| 68 | @plugins.each do |n, i| |
|---|
| 69 | i.send(n, *args) |
|---|
| 70 | end |
|---|
| 71 | end |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | class Plugin |
|---|
| 76 | include Net::IRC |
|---|
| 77 | include Constants |
|---|
| 78 | |
|---|
| 79 | def initialize(socket, config) |
|---|
| 80 | @socket, @config = socket, OpenStruct.new(config) |
|---|
| 81 | @log = @config.log |
|---|
| 82 | end |
|---|
| 83 | |
|---|
| 84 | def post(command, *params) |
|---|
| 85 | m = Message.new(nil, command, params.map {|s| |
|---|
| 86 | s.gsub(/\r|\n/, " ") |
|---|
| 87 | }) |
|---|
| 88 | @log.debug "SEND: #{m.to_s.chomp}" if @log |
|---|
| 89 | @socket << m |
|---|
| 90 | end |
|---|
| 91 | |
|---|
| 92 | %w[ |
|---|
| 93 | privmsg |
|---|
| 94 | notice |
|---|
| 95 | join |
|---|
| 96 | part |
|---|
| 97 | kick |
|---|
| 98 | invite |
|---|
| 99 | ].each do |command| |
|---|
| 100 | eval <<-EOS |
|---|
| 101 | def #{command}(*params) |
|---|
| 102 | post #{command.upcase}, *params |
|---|
| 103 | end |
|---|
| 104 | EOS |
|---|
| 105 | end |
|---|
| 106 | end |
|---|
| 107 | |
|---|
| 108 | #require "rubygems" |
|---|
| 109 | #gem "rspec" |
|---|
| 110 | #require "spec" |
|---|
| 111 | #require "stringio" |
|---|
| 112 | # |
|---|
| 113 | #require "chokan.rb" |
|---|
| 114 | # |
|---|
| 115 | #class String; def clear; replace ""; end; end |
|---|
| 116 | # |
|---|
| 117 | #describe Plugin, "shorthand methods" do |
|---|
| 118 | # @socket = "" |
|---|
| 119 | # |
|---|
| 120 | # @plugin = Plugin.new(@socket, {}) |
|---|
| 121 | # @plugin.privmsg("#test", "message") |
|---|
| 122 | # @socket.should == "PRIVMSG #test message\r\n" |
|---|
| 123 | # |
|---|
| 124 | # @socket.clear |
|---|
| 125 | # @plugin.privmsg("#test", "message with space") |
|---|
| 126 | # @socket.should == "PRIVMSG #test :message with space\r\n" |
|---|
| 127 | #end |
|---|
| 128 | |
|---|
| 129 | #Internet Relay Chat |
|---|
| 130 | # |
|---|
| 131 | #Reto |
|---|
| 132 | #Dialogas |
|---|
| 133 | # |
|---|
| 134 | #Sage |
|---|
| 135 | #Viola |
|---|
| 136 | #Tricolor |
|---|