Written in Ruby. You also can read the source code on GitHub (for more convenience).
PHP Code:
require 'socket'
require 'open-uri'
=begin
Really infirm IRC bot implementation.
=end
class IRCBot
# Initialize required parameters.
#
def initialize(host, port, nick, real_name, ident, channels)
@host = host
@port = port
@nick = nick
@real_name = real_name
@ident = ident
@channels = channels # An array of channels, which should be visited by a bot.
end
# Connect to server with given parameters.
#
def connect
@server = TCPSocket.new(@host, @port)
# First two messages, that every client should send to a server.
# For more details see RFC2812.
send "NICK #{@nick}"
send "USER #{@ident} 0 * :#{@real_name}"
# Join every provided channel.
@channels.each do |channel|
send "JOIN #{channel}"
say channel, "#{@nick} arrived." # Welcome message.
end
end
# Message interchange between client and server.
#
def main_loop
loop do
recieved_message = @server.gets
puts recieved_message
# Capture a channel's name, where the keywords were
# said, to address a response to the proper channel.
recieved_message.match(/(#\w+)/)
channel = $1
recieved_message.match(/:(\w+)!/) # Extract the applicant's nick.
applicant = $1
# Keywords handling.
case recieved_message
# '!help' message.
when /:!help/ then say channel, "No way!"
# If someone would try to talk to the bot, the latter will
# respond to the applicant with a one of the predefined phrases.
#
# Example:
#
# <Bob> Glitch, how are you?
# <Glitch> La-la-la!
# <Bob> Glitch: Are you stupid?
# <Glitch> WHAAAAT?!
# <Bob> Whoa! Okay-okay, could you forgive me, Glitch?
# <Glitch> Sure.
#
when /:.+[,\s]?#{@nick}[:|,|\.]/
# Predefined replies.
replies = ["WHAAAAT?!", "Sure.", "Yes, I know, my name is #{@nick}.",
"Nice to meet you. My name is #{@real_name}.", "La-la-la!",
"Are you sure about that, #{applicant}?"]
# On every call, throw a random reply.
say channel, replies.sample
# Display some information about author of this program.
when /:!author/
say channel, "My master is Kirill Silin <curacaothe@gmail.com>."
# Ability to throw something at someoglitchne.
#
# Example:
#
# <Bob> !throw a stone Hexadecimal
# <Glitch> Bob threw a stone at Hexadecimal.
# <Bob> !throw an egg Megabyte
# <Glitch> Bob threw an egg at Megabyte.
#
when /:!throw ((a|an) \w+) (\w+)/i
say channel, "#{applicant} threw #{$1} at #{$3}."
# Required for persistent connection to server.
when /^PING :(.+)$/ then send "PONG #{$1}"
# URL handling.
#
# Example:
#
# <Bob> Guys, there is an article about us!
# <Bob> Check this out: http://en.wikipedia.org/wiki/ReBoot
# <Glitch> Title: ReBoot - Wikipedia, the free encyclopedia
#
when /PRIVMSG #\w+ :.*(https?:\/\/[\S]+)/
begin
url = open($1)
rescue OpenURI::HTTPError => err
puts "--> Error: #{err}"
end
title = url.readlines.join.match(/<title>(.+)<\/title>/)[1] rescue nil
unless title.nil?
message = "Title: #{title}"
say channel, message
end
end
end
end
# Quit from server.
#
def quit
@server.close
end
private
# A wrapper around existing 'send' method for more convenience.
#
def send(msg)
@server.send("#{msg}\r\n", 0)
end
# Say something on channel. Visible to all channel participants.
#
def say(channel, text)
send "PRIVMSG #{channel} :#{text}\r\n"
end
end
channels = %w[ #ufpc ]
bot = IRCBot.new("chat.freenode.net", 6667,
"GlitchIRCbot", "Unnamed Unit",
"Mainframe", channels)
begin
bot.connect
bot.main_loop
rescue Exception => err
puts "--> Error: #{err}"
ensure
bot.quit
end
Bookmarks