UPDATE: this time without the rio class

Just because I kinda like ruby…
The bot basically looks if new files/folders get added to a folder and the notifies persons via jabber. The code itself could be nicer but it tends to work…

#!/usr/bin/env ruby require 'rubygems' require 'xmpp4r' require 'xmlsimple'

<ol>
    <li><span class="caps">READ</span> <span class="caps">CONFIGURATION</span> <span class="caps">STUFF</span> <span class="caps">FROM</span> <span class="caps">XML</span> <span class="caps">FILE</span><br />

config = XmlSimple.xml_in(‘UpdateBot.conf', {'ForceArray'=>false})

</ol><p>botjid = config[&#8216;botjid']<br />

botpass = config[‘botpass']
botstatus = config[‘statusmsg']
directoryname = config[‘checkdir']
jids_to_send_to = config[‘recipient']

#no need for all the debugging stuff
Jabber::debug = false

<ol>
    <li><span class="caps">END</span> OF <span class="caps">CONFIGURATION</span> <span class="caps">PART</span> OF <span class="caps">THE</span> <span class="caps">SCRIPT</span></li>
</ol><p>#this is the starting content of our directory<br />

lastarray = Dir.entries(directoryname)

#here goes the loop…
while true

#this is the possible "new"; state of the directory
newarray = Dir.entries(directoryname)

#let's see if something was added
if newarray.size>lastarray.size
puts ‘debug: a change was detected!'

#that's how we get the items that changed
changes = (newarray.compact-lastarray.compact)

changes.each{|change| puts "this item is new: " + change}

#log into our jabber account
client = Jabber::Client.new(Jabber::JID.new(botjid))
client.connect
client.auth(botpass)
client.send(Jabber::Presence.new.set_show(:chat).set_status(botstatus))

#and that's how we tell everybody
jids_to_send_to.each do |jid|
answer = Jabber::Message.new("#{jid}";)
answer.type = :chat
answer.body = "New stuff:\n"; + changes.join("\n";)
client.send(answer)
end

#disconect from jabber again
client.close

end
#ok, we told the user about the changes, let's "reset"; the current state of the directory
lastarray = newarray
#to keep the polling down to a minimum (time in secs)
sleep 30
end

Thread.stop

and here’s the .xml
< code>
< ?xml version=“1.0”; encoding=“ISO-8859-15”;?>
< configuration>
< botjid>[email protected]/rubyrubyruby< /botjid>
< botpass>password< /botpass>
< statusmsg>I’m the Messenger!< /statusmsg>
< checkdir>monitordir< /checkdir>
< recipient>user1@server.tld< /recipient>
< recipient>user2@server.tld< /recipient>
< /configuration>
< /code>

Comments