Created
June 16, 2010 15:58
-
-
Save pietern/440885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Pieter Noordhuis | |
# Description: Bot designed to pipe CI build status to IRC, but | |
# can be used for anything. | |
# | |
# Inspiration from: http://github.com/purzelrakete/mini | |
# | |
require 'eventmachine' | |
require 'active_support' | |
require 'ostruct' | |
class IRC < EventMachine::Connection | |
include EventMachine::Protocols::LineText2 | |
attr_accessor :config | |
cattr_accessor :connection | |
def initialize(options) | |
self.config = OpenStruct.new(options) | |
end | |
def say(msg) | |
msg.split("\n").each do |msg| | |
command("PRIVMSG #{ config.channel } :#{ msg }") if msg.present? | |
end | |
end | |
def command(*cmd) | |
puts cmd.inspect | |
send_data "#{ cmd.flatten.join(' ') }\r\n" | |
end | |
def self.connect(options) | |
self.connection = EM.connect(options[:server], options[:port].to_i, self, options) | |
end | |
# callbacks | |
def post_init | |
command "USER", [config.user]*4 | |
command "NICK", config.user | |
command("NickServ IDENTIFY", config.user, config.password) if config.password | |
command("JOIN", "#{config.channel}") | |
end | |
def receive_line(line) | |
case line | |
when /^PING (.*)/ : command('PONG', $1) | |
else puts line; end | |
end | |
def unbind | |
EM.add_timer(3) do | |
reconnect(config.server, config.port) | |
post_init | |
end | |
end | |
end | |
module Pipe | |
def receive_data(data) | |
return if data.size > 1024 # protect from heavy flooding ;) | |
IRC.connection.say(data.strip) | |
close_connection | |
end | |
end | |
EventMachine.run do | |
channel = ENV['CHANNEL'] | |
channel = "##{channel}" if !channel.start_with?('#') | |
IRC.connect( | |
:user => ENV['USER'], | |
:password => ENV['PASSWORD'], | |
:server => 'irc.freenode.net', | |
:port => 6667, | |
:channel => channel) | |
# pipe incoming traffic to irc | |
EventMachine.start_server('127.0.0.1', 6668, Pipe) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment