net-irc2 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/AUTHORS.txt +33 -0
- data/ChangeLog +95 -0
- data/README +91 -0
- data/Rakefile +69 -0
- data/examples/2ch.rb +225 -0
- data/examples/2ig.rb +267 -0
- data/examples/client.rb +23 -0
- data/examples/echo_bot.rb +31 -0
- data/examples/echo_bot_celluloid.rb +33 -0
- data/examples/gig.rb +192 -0
- data/examples/gmail.rb +202 -0
- data/examples/gtig.rb +420 -0
- data/examples/hatena-star-stream.rb +270 -0
- data/examples/hcig.rb +285 -0
- data/examples/hig.rb +771 -0
- data/examples/iig.rb +819 -0
- data/examples/ircd.rb +358 -0
- data/examples/lig.rb +551 -0
- data/examples/lingr.rb +327 -0
- data/examples/mixi.rb +252 -0
- data/examples/sig.rb +188 -0
- data/examples/tig.rb +2712 -0
- data/lib/net/irc/client/channel_manager.rb +144 -0
- data/lib/net/irc/client.rb +117 -0
- data/lib/net/irc/constants.rb +214 -0
- data/lib/net/irc/message/modeparser.rb +85 -0
- data/lib/net/irc/message/serverconfig.rb +30 -0
- data/lib/net/irc/message.rb +109 -0
- data/lib/net/irc/pattern.rb +68 -0
- data/lib/net/irc/server.rb +186 -0
- data/lib/net/irc.rb +77 -0
- data/spec/channel_manager_spec.rb +184 -0
- data/spec/modeparser_spec.rb +165 -0
- data/spec/net-irc_spec.rb +337 -0
- data/spec/spec.opts +1 -0
- metadata +91 -0
data/examples/sig.rb
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# vim:encoding=UTF-8:
|
3
|
+
=begin
|
4
|
+
# sig.rb
|
5
|
+
|
6
|
+
ServerLog IRC Gateway
|
7
|
+
|
8
|
+
# Usage
|
9
|
+
|
10
|
+
* Connect.
|
11
|
+
* Join a channel (you can name it as you like)
|
12
|
+
* Set topic "filename regexp"
|
13
|
+
* You will see the log at the channel only matching the regexp.
|
14
|
+
|
15
|
+
=end
|
16
|
+
|
17
|
+
$LOAD_PATH << "lib"
|
18
|
+
$LOAD_PATH << "../lib"
|
19
|
+
|
20
|
+
$KCODE = "u" unless defined? ::Encoding
|
21
|
+
|
22
|
+
require "rubygems"
|
23
|
+
require "net/irc"
|
24
|
+
require "logger"
|
25
|
+
require "pathname"
|
26
|
+
require "yaml"
|
27
|
+
|
28
|
+
class ServerLogIrcGateway < Net::IRC::Server::Session
|
29
|
+
def server_name
|
30
|
+
"serverlog"
|
31
|
+
end
|
32
|
+
|
33
|
+
def server_version
|
34
|
+
"0.0.0"
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def initialize(*args)
|
39
|
+
super
|
40
|
+
@channels = {}
|
41
|
+
@config = Pathname.new(ENV["HOME"]) + ".sig"
|
42
|
+
end
|
43
|
+
|
44
|
+
def on_disconnected
|
45
|
+
@channels.each do |chan, info|
|
46
|
+
begin
|
47
|
+
info[:observer].kill if info[:observer]
|
48
|
+
rescue
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def on_user(m)
|
54
|
+
super
|
55
|
+
@real, *@opts = @real.split(/\s+/)
|
56
|
+
@opts ||= []
|
57
|
+
end
|
58
|
+
|
59
|
+
def on_join(m)
|
60
|
+
channels = m.params.first.split(/,/)
|
61
|
+
channels.each do |channel|
|
62
|
+
@channels[channel] = {
|
63
|
+
:topic => "",
|
64
|
+
:observer => nil,
|
65
|
+
} unless @channels.key?(channel)
|
66
|
+
post @prefix, JOIN, channel
|
67
|
+
post nil, RPL_NAMREPLY, @prefix.nick, "=", channel, "@#{@prefix.nick}"
|
68
|
+
post nil, RPL_ENDOFNAMES, @prefix.nick, channel, "End of NAMES list"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def on_topic(m)
|
73
|
+
channel, topic, = m.params
|
74
|
+
p m.params
|
75
|
+
if @channels.key?(channel)
|
76
|
+
post @prefix, TOPIC, channel, topic
|
77
|
+
@channels[channel][:topic] = topic
|
78
|
+
create_observer(channel)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def create_observer(channel)
|
83
|
+
return unless @channels.key?(channel)
|
84
|
+
info = @channels[channel]
|
85
|
+
@log.debug "create_observer<#{channel}>#{info.inspect}"
|
86
|
+
begin
|
87
|
+
info[:observer].kill if info[:observer]
|
88
|
+
rescue
|
89
|
+
end
|
90
|
+
info[:observer] = Thread.start(channel, info) do |chan, i|
|
91
|
+
file, reg, = i[:topic].split(/\s+/)
|
92
|
+
name = File.basename(file)
|
93
|
+
grep = Regexp.new(reg.to_s)
|
94
|
+
@log.info "#{file} / grep => #{grep.inspect}"
|
95
|
+
File.open(file) do |f|
|
96
|
+
size = File.size(f)
|
97
|
+
f.seek size
|
98
|
+
loop do
|
99
|
+
sleep 1
|
100
|
+
nsize = File.size(f)
|
101
|
+
if nsize > size
|
102
|
+
@log.debug "follow up log"
|
103
|
+
while l = f.gets
|
104
|
+
if grep === l
|
105
|
+
post name, PRIVMSG, chan, l
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
size = nsize
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
if __FILE__ == $0
|
117
|
+
require "optparse"
|
118
|
+
|
119
|
+
opts = {
|
120
|
+
:port => 16700,
|
121
|
+
:host => "localhost",
|
122
|
+
:log => nil,
|
123
|
+
:debug => false,
|
124
|
+
:foreground => false,
|
125
|
+
}
|
126
|
+
|
127
|
+
OptionParser.new do |parser|
|
128
|
+
parser.instance_eval do
|
129
|
+
self.banner = <<-EOB.gsub(/^\t+/, "")
|
130
|
+
Usage: #{$0} [opts]
|
131
|
+
|
132
|
+
EOB
|
133
|
+
|
134
|
+
separator ""
|
135
|
+
|
136
|
+
separator "Options:"
|
137
|
+
on("-p", "--port [PORT=#{opts[:port]}]", "port number to listen") do |port|
|
138
|
+
opts[:port] = port
|
139
|
+
end
|
140
|
+
|
141
|
+
on("-h", "--host [HOST=#{opts[:host]}]", "host name or IP address to listen") do |host|
|
142
|
+
opts[:host] = host
|
143
|
+
end
|
144
|
+
|
145
|
+
on("-l", "--log LOG", "log file") do |log|
|
146
|
+
opts[:log] = log
|
147
|
+
end
|
148
|
+
|
149
|
+
on("--debug", "Enable debug mode") do |debug|
|
150
|
+
opts[:log] = $stdout
|
151
|
+
opts[:debug] = true
|
152
|
+
end
|
153
|
+
|
154
|
+
on("-f", "--foreground", "run foreground") do |foreground|
|
155
|
+
opts[:log] = $stdout
|
156
|
+
opts[:foreground] = true
|
157
|
+
end
|
158
|
+
|
159
|
+
parse!(ARGV)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
opts[:logger] = Logger.new(opts[:log], "daily")
|
164
|
+
opts[:logger].level = opts[:debug] ? Logger::DEBUG : Logger::INFO
|
165
|
+
|
166
|
+
def daemonize(foreground=false)
|
167
|
+
trap("SIGINT") { exit! 0 }
|
168
|
+
trap("SIGTERM") { exit! 0 }
|
169
|
+
trap("SIGHUP") { exit! 0 }
|
170
|
+
return yield if $DEBUG || foreground
|
171
|
+
Process.fork do
|
172
|
+
Process.setsid
|
173
|
+
Dir.chdir "/"
|
174
|
+
File.open("/dev/null") {|f|
|
175
|
+
STDIN.reopen f
|
176
|
+
STDOUT.reopen f
|
177
|
+
STDERR.reopen f
|
178
|
+
}
|
179
|
+
yield
|
180
|
+
end
|
181
|
+
exit! 0
|
182
|
+
end
|
183
|
+
|
184
|
+
daemonize(opts[:debug] || opts[:foreground]) do
|
185
|
+
Net::IRC::Server.new(opts[:host], opts[:port], ServerLogIrcGateway, opts).start
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|