forked from jaspervdj/IRCFileNotify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.rb
75 lines (57 loc) · 1.3 KB
/
watcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require "ircsocket"
class Watcher
attr_accessor :directory
def initialize(directory=Dir.pwd)
@directory = directory
end
def watch
if ! block_given?
raise
else
old_files = get_files
loop do
files = get_files
diff = files - old_files
# yield with all the added files
# do not yield if nothing is added
yield diff if diff.size > 0
old_files = files
sleep 1
end
end
end
private
def get_files
Dir.entries(directory)
end
end
class IRCFileBot
def initialize(server,bot_name,channel,port,directory)
@server,@bot_name,@channel,@port = server,bot_name,channel,port
@watcher = Watcher.new(directory)
@ircsocket = IRCSocket.new server,port,channel,bot_name,true
end
def run
@watcher.watch do |files|
files_added(files)
sleep 1
end
end
def interrupt
@ircsocket.close
end
private
def files_added(files)
@ircsocket.join
sleep 1
@ircsocket.say "update: #{files.join ", "} #{files.size == 1 ? "was" : "were"} added"
sleep 1
@ircsocket.leave
end
end
# Main
if __FILE__ == $0
bot = IRCFileBot.new "wina.ugent.be", "NuddedTestBot", "#zeus", 6666, Dir.pwd
trap("INT") {bot.interrupt}
bot.run
end