N O O B --- W A T C H E R

because noobs shouldn't pwn pros

Noobwatcher is software in emergency development to avoid having your code or server configuration ruined by noobs.

Specifically, noobwatcher will:


#!/usr/bin/env ruby
#
# noobwatcher.rb
# --------------
# by: barce[a t]codebelay.com
# date created: 09/25/2008
# last update:  10/23/2008
# version: experimental
#
# requires showchange.pl from here
# http://svn.collab.net/repos/svn/trunk/tools/client-side/showchange.pl
#
# this will let you know the moment someone's updated the repo and will
# save a diff
#
# We now have twitter notification.
#
# Please feel free to edit / use the following yaml settings in 
# settings.yaml
# 
# path: /directory/to/local/svn/that/noobwatcher/svn/updates
# repo: http://svn.example.com/example
# diffs: /directory/where/the/revision/diffs/live
# twitter_email: twitteraccount_to_send_messages_from@example.com
# twitter_password: your_password
# twitter_recipient: twitter_user_who_gets_noobwatcher_update
# sleepseconds: 5
#
#


require 'date'
require 'yaml'
require 'net/http'
require 'uri'

# functions
def send_twitter (latest, settings)

  # http://twitter.com/direct_messages/new.format

  twitter_email     = settings['twitter_email']
  twitter_password  = settings['twitter_password']
  twitter_recipient = settings['twitter_recipient']
  repo_text         = settings['repo']

  update_text = "Noob Alert: showchange.pl #{latest} #{repo_text}"
  

begin
    url = URI.parse('http://twitter.com/direct_messages/new.xml')
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth twitter_email, twitter_password
    req.set_form_data({'user' => twitter_recipient, 'text' => update_text})

    begin
        res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }

        case res
        when Net::HTTPSuccess, Net::HTTPRedirection
            if res.body.empty?
                # Twitter is not responding properly
            else
                # message sent
            end

        else
            # Twitter update failed for an unknown reason
            res.error!
        end

    rescue
        # Twitter update failed - check username/password
    end

rescue SocketError
    # Twitter is currently unavailable
end


end


yamlstring = ''
File.open("settings.yml", "r") { |f|
    yamlstring = f.read
}

settings = YAML::load(yamlstring)
puts settings.inspect
puts settings["path"]

Dir.chdir(settings["path"])
i = 0
1000.times do
  results = %x[svn up]
  puts "----[ " + i.to_s + " ]----"
  stuff = results.scan(/At revision ([0-9]+)/)
  if i == 0
    @current = stuff
  end
  puts @current.to_s + " : " + stuff.to_s
  if (@current.to_s != stuff.to_s)
    # TODO: code here will run showchange.pl
    puts results
    latest = @current.to_s.to_i 
    latest += 1
    puts "running showchange.pl #{latest} #{settings['repo']}"
    changetext = %x[showchange.pl #{latest} #{settings['repo']}]
    f = File.open("../diff.log", "a")
    f.write(changetext)
    f.close
    dt = DateTime.now
    date_string = DateTime.now.strftime("%Y_%m_%d__%H_%M_%S__revision_#{latest}")

    f = File.open("#{settings['diffs']}/" + date_string, "a");
    f.write(changetext)
    f.close

    # send direct message to barce
    send_twitter(latest, settings)
    exit
  end
  # end of current != stuff


  i += 1

  sleep settings["sleepseconds"].to_i

end
# end of 1000 times