program a simple irc bot with ruby
i spend my time learning socket programming in many languages such as c, c++, perl, ruby and etc.. but i like ruby most since it's easier to handle especially when i can use regular expression to manipulate strings sent by server. after reading through ruby's tutorial and understand a little bit of its concept i manage to code this simple ruby irc bot that can connect to irc and join a channel automatically. i also include some command to control the bot like ~join ~part ~say ~msg and ~quit to terminate the bot. it even have auto-jawab salam function..hehe
here's the code
print "ruby irc bot\\nbrought to you by novatechnhttp://wan.pengganas.net\\n"
require 'socket'
server = "java.webchat.org"
port = "6667"
bot = TCPSocket.open(server,port)
bot.send("USER novaBot novaBot novaBot :Written in Ruby ;) \\r\\n", 0)
bot.send("NICK botcapayam\\r\\nJOIN #database\\r\\n", 0)
stay = 1
while stay == 1
str = String.new(bot.readline) #i don't know why i did this
#check if we have ping
if /^PING :(.*)/.match(str)
bot.send("PONG #$1\\r\\n", 0)
end
if /^:novatech!(.*)@(.*) PRIVMSG #(.*) :(.*)/.match(str)
command = "#$3 #$4"
bot.send("PRIVMSG ##$1 #$2\\r\\n", 0) if command =~ /(.*) ~say (.*)/
bot.send("PRIVMSG #$2\\r\\n", 0) if command =~ /(.*) ~msg (.*)/
bot.send("JOIN ##$2\\r\\n", 0) if command =~ /(.*) ~join (.*)/
bot.send("PART ##$2\\r\\n", 0) if command =~ /(.*) ~part (.*)/
stay = 0 if command =~ /(.*) ~quit/
end
if /^:(.*)!(.*)@(.*) PRIVMSG #(.*) :(askum|assalamualaikum|akum|a'kum)/.match(str)
bot.send("PRIVMSG ##$4 wa'alaikumussalam #$1\\r\\n", 0)
end
#puts bot.readline // for debug purpose
end #end while
bot.close
yea~ i'm suck in regular expressing..lol~!!
download the source code if you can't copy code above properly
if you want to expend its features, then all you need to do is learn yourself how to manage strings in ruby...and of course a lot of regex practice
hehe... see ya
to all banjirian peoples.. apa kata korang semua cuba try ekjas buat rimut tibai ruby plak? apa macam ada brani? aku dah cek... laju daripada rimut irc, tapi apakan daya aku dah pencen
![[flirt] [flirt]](http://wan.pengganas.net/smilies/flirt.gif)

![[color] [color]](http://wan.pengganas.net/smilies/color.gif)
![[dance] [dance]](http://wan.pengganas.net/smilies/dance.gif)

![[fish] [fish]](http://wan.pengganas.net/smilies/fish.gif)

![[rock] [rock]](http://wan.pengganas.net/smilies/rock.gif)
![[razz] [razz]](http://wan.pengganas.net/smilies/razz.gif)
![[blurb] [blurb]](http://wan.pengganas.net/smilies/blurb.gif)
![[present] [present]](http://wan.pengganas.net/smilies/present.gif)
![[kisses] [kisses]](http://wan.pengganas.net/smilies/sweetkisses.gif)
![[raincloud] [raincloud]](http://wan.pengganas.net/smilies/raincloud.gif)
ada dua aar kat bar ni. buang ar satu. hehehe
13 Comments