Skip to content

Instantly share code, notes, and snippets.

@pietern
Created June 16, 2010 15:58
Show Gist options
  • Save pietern/440885 to your computer and use it in GitHub Desktop.
Save pietern/440885 to your computer and use it in GitHub Desktop.
# 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