Skip to content

Instantly share code, notes, and snippets.

@relrod
Created January 17, 2009 01:15
Show Gist options
  • Save relrod/48229 to your computer and use it in GitHub Desktop.
Save relrod/48229 to your computer and use it in GitHub Desktop.
#/usr/bin/ruby
require "socket"
# Here we require things for use later on...
require "net/http" # For parsing things...
require "rexml/document" # For XML parsing.
nick = "CB|RubyBot"
server = "irc.freenode.net"
port = 6667
user = "CodeBlock"
realname = "CodeBlock's Ruby Bot"
# Make Socket
@socket = TCPSocket::new(server,port)
def raw(text)
@socket.send("#{text}\n",0)
puts "--> #{text}"
end
def say(text)
text = text.gsub("/\n/","")
text = text.gsub("/\n/","")
raw "PRIVMSG #{@channel} :#{text}"
end
raw "USER #{user} #{user} #{user} #{user} :#{realname}\n"
raw "NICK #{nick}\n"
raw "JOIN #botters-ruby2\n"
while true
data = @socket.recv(1024)
dataregex = data.match(/:(.*)!(.*)@(.*) (.*) (.*) :(.*)/i)
data_array = dataregex.to_a
said = data_array[6].to_s.gsub("\r","").gsub("\n","")
nick = data_array[1].to_s
@channel = data_array[5].to_s
#p said
if said == "hi"
raw "PRIVMSG #botters-ruby :Hello, #{nick}."
end
if said =~ /^!weather (.*)/i
inputzip = $1
zipcode = inputzip.gsub("\s","%20")
end
if said =~ /^!callsign (.*)/i
callsign = $1.to_s.gsub("\n","").gsub("\r","")
hamurl = "http://callook.info/index.php?display=xml&callsign=#{callsign}"
hamxml = Net::HTTP.get_response(URI.parse(hamurl)).body
cs = REXML::Document.new(hamxml)
callclass = []
name = []
addline1 = []
addline2 = []
grant = []
expire = []
status = []
trusteecall = []
trusteename = []
cs.elements.each('/callook/status') do |ele|
status << ele.text
end
cs.elements.each('callook/callsign[@status="current"]') do |ele|
callclass << ele.attributes["type"]
end
cs.elements.each('callook/trustee/callsign') do |ele|
trusteecall << ele.text
end
cs.elements.each('callook/trustee/name') do |ele|
trusteename << ele.text
end
cs.elements.each('callook/name') do |ele|
name << ele.text
end
cs.elements.each('callook/address/line1') do |ele|
addline1 << ele.text
end
cs.elements.each('callook/address/line2') do |ele|
addline2 << ele.text
end
cs.elements.each('callook/grantdate') do |ele|
grant << ele.text
end
cs.elements.each('callook/expirydate') do |ele|
expire << ele.text
end
if callclass.to_s =~ /Type: CLUB/i
say "#{callsign.upcase} (club) : Trustee -> #{trusteecall}/#{trusteename} : Club Address: #{addline1}, #{addline2}"
else
callclass = callclass.to_s.gsub("Operator Class: ","")
say "#{callsign.upcase} - #{callclass.capitalize}: #{name} - #{addline1}, #{addline2} : Granted #{grant} : Expires #{expire}"
end
end
if said =~ /^!wxalerts (.*)/i
inputzip = $1
zipcode = inputzip.gsub("\s","%20")
alertsurl = "http://api.wunderground.com/auto/wui/geo/AlertsXML/index.xml?query=#{zipcode}"
alertsxml = Net::HTTP.get_response(URI.parse(alertsurl)).body
alerts = REXML::Document.new(alertsxml)
alertcount = []
alerttype = []
alertstart = []
alertstop = []
alerts.elements.each('alerts/alert') do |ele|
alertcount << ele.attributes["count"]
end
alerts.elements.each('alerts/alert/AlertItem/description') do |ele|
alerttype << ele.text
end
alertcount.each_with_index do |alertcount,idx|
case alertcount.to_i
when 0 then say "There are no alerts for #{inputzip.upcase}."
when 1 then say "There is#{3.chr}02 1#{3.chr} alert for #{inputzip.upcase}: #{3.chr}05#{alerttype.join(', ')}#{3.chr}"
else say "There are #{3.chr}02 #{alertcount} #{3.chr}alerts for #{inputzip.upcase}: #{3.chr}05#{alerttype.join(', ')}#{3.chr}"
end
end
end
if said =~ /^!yahoo (.*)/i
searchterms = $1.gsub("\s","%20")
searchurl = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=#{searchterms}&results=1"
searchxml = Net::HTTP.get_response(URI.parse(searchurl)).body
results = REXML::Document.new(searchxml)
title = []
link = []
summary = []
results.elements.each('ResultSet/Result/Title') do |ele|
title << ele.text
end
results.elements.each('ResultSet/Result/Summary') do |ele|
summary << ele.text
end
results.elements.each('ResultSet/Result/Url') do |ele|
link << ele.text
end
title.each_with_index do |title,idx|
say "#{title} - #{summary} - #{link}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment