i cant think A blog belong to Mohammad Azwan bin Ali. Most of his writings are about his real life story, ideas, thoughts, moderate political views, computer, programming, tips & tricks, particularly in Linux…. and of course it includes some random unspecified crappy stuffs too! Don't forget to check Movies and Junkyard pages out!
note: most of entries is not suitable for faint hearted person or anybody with iq below 100.

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


13 Comments

tolong tunjuk ajar la sikit regex utk banjir tu
saya budak baru belajar [flirt]

Posted by deiy on 30 May 2007 @ 2am

saya budak baru belajar jugak. mintak tolong diajarkan... hu3. :p

Posted by CypherHackz on 30 May 2007 @ 9am

kuat main irc mamat ni ropanya... [color][dance]:D:D

Posted by azrn on 30 May 2007 @ 4pm

@cypher : saya pun budak baru belajar kekokeokeoe
@azrn : biasa la.. ym tak syok daa :D [fish]

Posted by novatech on 30 May 2007 @ 4pm

aku masih tak brape faham ngan IRC.. :(

Posted by aman on 31 May 2007 @ 2pm

aku tak pernah pun terniat nak beljar programming nih... tapi teringin gak tuk tambah pengetahuan. tapi masalahyer takde member yg pakar programming.. >_<

Posted by shadowX_19 on 1 June 2007 @ 2pm

zaman irc aku berakhir mase aku form2. haha

Posted by flisterz on 1 June 2007 @ 7pm

@aman : hehe.. mirc la paling simple aku rasa
@shadowx : nak belajar banyak resource ada dalam tenet ni,tak semestinya kena ada member expert programing. aku pun baca tutorial je..
@flisterz : aku sampai hari ni men irc lagi, simple [dance]

Posted by novatech on 1 June 2007 @ 10pm

irc penin aar..
dah la tulisan pon uduh2...huhu,
takde icon kiyut2 [rock][razz][blurb][blurb][present][present][kisses][raincloud]>_<

p/s: icon yg [scratchchin] ada dua aar kat bar ni. buang ar satu. hehehe -_-

Posted by azrn on 1 June 2007 @ 11pm

hahaha.. aku dah perasan lama ada 2 icon ni.. tapi aku selalu lupa nak fix... biar je la gitu haha!!

Posted by novatech on 2 June 2007 @ 4pm

@flisterz , me too! me too!

but after a while, aku kena gune balik irc, coz kena join channel ubuntu, ade a few questions nak tanye..

haha.. rindu zaman muda2, cari awek gune irc..

=)

Posted by -fuQaHa- on 2 June 2007 @ 5pm

see.. itulah tandanya irc popular lagi.. lagipun men irc ni.. oldskol sket~ muahahhaha!! :D

Posted by novatech on 2 June 2007 @ 10pm

Hi! I've used your code for a bot I'm working on.
the line "puts bot.readline" used for debugging
is quite bad, it's better with "print str" or something like that, since "puts bot.readline" reads a line from the bot which will never be evaluated by the if-conditions.

Posted by Calle on 2 July 2007 @ 9am