NOGDUS $1670.00 has been donated to NOGDUS!
August 19, 2017, 09:55:42 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: RubyBot  (Read 1658 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: October 04, 2009, 09:59:15 PM »

RubyBot

Code:
#!/usr/bin/env ruby
# rubybot1.rb
# a simple IRC bot in Ruby by Richard Marks ccpsceo@gmail.com

require 'socket'

class RubyBot
def initialize
@server = ''
@port = ''
@nick = ''
@password = ''
@host = ''
@real = ''
@nickServ = ''
@ident = 'IDENTIFY ' + @password
@channel = ''
@prefix = '%'
@pwprompt = '(This nickname is registered and protected|This nickname is owned by someone else)'
@pwaccept = 'Password accepted \- you are now recognized'
@connectLog = ''
@joined = 0
@connection = null
end

def Send(text)
x = text.gsub(/\n/, "")
x = x.gsub(/\r/, "")
@connection.send x + "\n", 0
puts ": -->" + x
end

def PrivMsg(user, message)
Send("PRIVMSG #{user} :#{message}")
end

def Connect
@connection = TCPSocket.new(@server, @port)
Send("NICK " + @nick)
Send("USER " + @nick + " 0 * :" + @real)
Send("JOIN " + @channel)
end

def Parse(text)
nickNormal = @nick
nickLower = @nick.downcase
nick = 'no value'
channel = 'no value'
fullMessageLower = 'no value'
fullMessageNormal = 'no value'
messageLower = 'no value'
messageNormal = 'no value'

sub = text.split("\:", 3)

# get nick
if sub[1] =~ /!/ then nick = sub[1].split("!")[0] end
# get channel
if sub[1] =~ /\ / then channel = sub[1].split("\ ")[2] end
# get message
if sub[2] != nil then
puts sub[2]
fullMessageLower = sub[2].downcase
fullMessageNormal = sub[2]
end
if sub[3] != nil then
puts sub[3]
fullMessageLower += sub[3].downcase
fullMessageNormal += sub[3]
end

fullMessageNormal = fullMessageNormal.strip
fullMessageLower = fullMessageLower.strip

# msg directed at bot?
if fullMessageNormal =~ /^(#{nickNormal}\:|#{@prefix})(\ )*/ then
direct = true
messageNormal = fullMessageNormal.split(/^(#{nickNormal}\:|#{@prefix})*/, 2)
messageNormal = messageNormal[2].strip
message = messageNormal.downcase.strip
else
messageNormal = fullMessageNormal.strip
message = fullMessageLower.strip
end

if !direct then return nil end

# handle %help

if message =~ /^(help|about|\?)/ then Handle_HELP nick end

end

def Handle_HELP(nick)
PrivMsg(nick, "I know commands: HELP, ABOUT, and ? -- prefix each with %")
end

def Run
self.Connect

while true do
@connectLog = @connection.recv(512)
logSplit = @connectLog.split("\r")
for i in logSplit
if i =~ /PING\ / then
a = i.split("\:")
Send("PONG #{@nickServ} :#{a[1]}")
end
if i =~ /#{@pwprompt}/ then
Send("PRIVMSG #{@nickServ} \:#{@ident}")
end
if i =~ /#{@pwaccept}/ && @joined == 0 then
Send("JOIN #{@channel}")
@joined = 1
end
Parse(i)
end
if @connectLog.length > 0 then
@connectLog = ''
end
end
end
end

class Demo
def initialize
@bot = RubyBot.new
end
def Run
@bot.Run
end
end

if __FILE__ == $0 then
demo = Demo.new
demo.Run
end
Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!