Skip to content

Instantly share code, notes, and snippets.

@zQueal
Forked from lukeledet/jabber_bot.rb
Created April 21, 2012 22:40
Show Gist options
  • Save zQueal/2440024 to your computer and use it in GitHub Desktop.
Save zQueal/2440024 to your computer and use it in GitHub Desktop.
Minecraft Jabber Bot
# Simple jabber bot to let me talk to users of my minecraft server from gtalk
# (c) Luke Ledet: https://github.com/lukeledet
require 'xmpp4r-simple'
BOT_USERNAME = '...' #Jabber Bot Username
BOT_PASSWORD = '...' #Jabber Bot Password
ADMIN_USERNAME = '...' #Jabber Admin Account Username
SCREEN_NAME = 'minecraft' #screen -list
# The idea for this method came from here but I blockified it:
# http://goo.gl/mJ2IO
def watch_file(file, &block)
timeout = 1
f = File.open(file, "r")
f.seek(0, IO::SEEK_END)
while true do
select [f]
yield f.gets
sleep timeout
end
end
def say(message)
%x{screen -S #{SCREEN_NAME} -p 0 -X stuff "`printf "say #{message}\r"`"}
end
jabber = Jabber::Simple.new(BOT_USERNAME, BOT_PASSWORD, nil, 'Minecraft Bot')
admin_online = false
# Trap CTRL-C to logoff cleanly.
trap("INT") do
jabber.disconnect
puts "\nLogging off."
exit
end
watch_file('server.log') do |line|
case line
when /\[INFO\] (.*?) \[[0-9\/\.:]+\] logged in/
jabber.deliver(ADMIN_USERNAME, "#{$1} logged in")
when /\[INFO\] (.*?) lost connection: (.*)/
jabber.deliver(ADMIN_USERNAME, "#{$1} lost connection: #{$2}")
when /\[INFO\] (?:\[.*?\] )?(.*?): (.*)/
jabber.deliver(ADMIN_USERNAME, "#{$1}: #{$2}")
end
# Crudely determine if the admin is online
jabber.presence_updates do |update|
admin_online = true if update[0] == ADMIN_USERNAME && update[1] == :online
admin_online = false if update[0] == ADMIN_USERNAME && update[1] == :unavailable
end
jabber.received_messages {|msg| say msg.body if admin_online }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment