net-irc2 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
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
+