Skip to content

Instantly share code, notes, and snippets.

@tomykaira
Created May 28, 2011 15:08
Show Gist options
  • Save tomykaira/996927 to your computer and use it in GitHub Desktop.
Save tomykaira/996927 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "socket"
require "rubygems"
require "twitter"
require 'oauth'
require 'pp'
# Don't allow use of "tainted" data by potentially dangerous operations
$SAFE=1
# The irc class, which talks to the server and holds the main event loop
class IRC
def initialize(server, port, nick, channel)
@server = server
@port = port
@nick = nick
@channel = channel
end
def send(s)
puts "--> #{s}"
@irc.send "#{s}\n", 0
end
def connect
# Connect to the IRC server
@irc = TCPSocket.open(@server, @port)
send "USER irctwitter blah blah :IRCTo Itter"
send "NICK #{@nick}"
send "JOIN #{@channel}"
end
def sendDM(input)
# this succeeds with any params (even if wrong)
Twitter.configure do |config|
#https://dev.twitter.com/
config.consumer_key = "YOUR CONSUMER KEY HERE"
config.consumer_secret = "SECRET HERE"
#http://atoken4me.heroku.com/
config.oauth_token = "YOUR USER TOKEN HERE"
config.oauth_token_secret = "USER SECRET HERE"
end
Twitter.direct_message_create(287606751, input)
end
def handle_server_input(input)
# This isn't at all efficient, but it shows what we can do with Ruby
# (Dave Thomas calls this construct "a multiway if on steroids")
case input.strip
when /^PING :(.+)$/i
puts "[ Server ping ]"
send "PONG :#{$1}"
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
puts "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"
send "NOTICE #{$1} :\001PING #{$4}\001"
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]VERSION[\001]$/i
puts "[ CTCP VERSION from #{$1}!#{$2}@#{$3} ]"
send "NOTICE #{$1} :\001VERSION IRC to Itter v0.010\001"
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:(.+)$/i # ordinal echo
input = "#{$1} says #{$5} on #{$4}"
puts input
sendDM(input)
else
puts input
end
end
def main_loop()
# Just keep on truckin' until we disconnect
loop do
ready = select([@irc, $stdin], nil, nil, nil)
next unless ready
ready[0].each do |s|
if s == $stdin then
return if $stdin.eof
s = $stdin.gets
send s
elsif s == @irc then
return if @irc.eof
s = @irc.gets
handle_server_input(s)
end
end
end
end
end
irc = IRC.new('localhost', 16667, 'IRCtoItter', '#hoge')
irc.connect()
begin
irc.main_loop()
rescue Interrupt
rescue Exception => detail
puts detail.message()
print detail.backtrace.join("\n")
retry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment