├── README.markdown └── watcher.rb /README.markdown: -------------------------------------------------------------------------------- 1 | # IRCFileNotify 2 | 3 | ## About 4 | 5 | This is a simple script that watches a directory for new files. If any new 6 | files are found, it sends a notice to a given IRC channel on a given server. 7 | 8 | ## Usage 9 | 10 | Edit the `watcher.rb` file to set your directory, and IRC connection details. 11 | Then just run it. 12 | -------------------------------------------------------------------------------- /watcher.rb: -------------------------------------------------------------------------------- 1 | require "socket" 2 | 3 | class Watcher 4 | 5 | attr_accessor :directory 6 | 7 | def initialize(directory=Dir.pwd) 8 | @directory = directory 9 | end 10 | 11 | def watch 12 | if ! block_given? 13 | raise 14 | else 15 | old_files = get_files 16 | loop do 17 | files = get_files 18 | diff = files - old_files 19 | 20 | # yield with all the added files (possibly empty) 21 | yield diff 22 | 23 | old_files = files 24 | end 25 | 26 | end 27 | end 28 | 29 | private 30 | 31 | def get_files 32 | Dir.entries(directory) 33 | end 34 | 35 | end 36 | 37 | class IRCFileBot 38 | 39 | def initialize(server,bot_name,channel,port,directory) 40 | @server,@bot_name,@channel,@port = server,bot_name,channel,port 41 | @watcher = Watcher.new(directory) 42 | 43 | connect 44 | end 45 | 46 | def run 47 | @watcher.watch do |files| 48 | mainloop(files) 49 | sleep 1 50 | end 51 | end 52 | 53 | private 54 | 55 | def pipe(content) 56 | puts content 57 | content 58 | end 59 | 60 | def connect 61 | @socket = TCPSocket.open(@server,@port) 62 | @socket.puts pipe "NICK #{@bot_name}" 63 | @socket.puts pipe "USER #{@bot_name.downcase} ignored ignored :#{@bot_name}" 64 | 65 | @listenthread = Thread.new(@socket) do |socket| 66 | until @socket.eof? 67 | if pipe(@socket.gets) =~ /PING :(.*)$/ 68 | @socket.puts pipe "PONG :#{$1}" 69 | end 70 | end 71 | end 72 | end 73 | 74 | def mainloop(files) 75 | unless files.empty? 76 | @socket.puts pipe "JOIN #{@channel}" 77 | sleep 1 78 | if files.length > 1 79 | msg = "\"#{files[0..-2].join "\", \""}\" and \"#{files[-1]}\" were added" 80 | else 81 | msg = "\"#{files[-1]}\" was added" 82 | end 83 | @socket.puts pipe "PRIVMSG #{@channel} :Just now, #{msg}." 84 | sleep 1 85 | @socket.puts pipe "PART #{@channel}" 86 | end 87 | end 88 | 89 | def disconnect 90 | @socket.puts pipe "QUIT" 91 | @socket.close # Probably exception due to socket.gets while closing 92 | @listenthread.join 93 | end 94 | end 95 | 96 | # Main 97 | if __FILE__ == $0 98 | bot = IRCFileBot.new "wina.ugent.be", "ZeusFileBot", "#zeus", 6666, "/srv/ftp" 99 | bot.run 100 | end 101 | --------------------------------------------------------------------------------