Problemy z ping'em - prosty irc bot

Witam.
Uczę się języka ruby. Podstawy znam, ale wygodniej uczyć się na czymś co przynosi efekty.
Znalazłem w sieci kod irc bot’a.

[code]#!/usr/local/bin/ruby

require “socket”

Don’t allow use of “tainted” data by potentially dangerous operations

$SAFE=1

The irc class, which talks to the server and holds the main event loop

class IRC
def initialize(server, port, nick, channel)
@server = server
@port = port
@nick = nick
@channel = channel
end
def send(s)
# Send a message to the irc server and print it to the screen
puts “–> #{s}”
@irc.send “#{s}\n”, 0
end
def connect()
# Connect to the IRC server
@irc = TCPSocket.open(@server, @port)
send “USER blah blah blah :blah blah”
send “NICK #{@nick}”
send “JOIN #{@channel}”
end
def evaluate(s)
# Make sure we have a valid expression (for security reasons), and
# evaluate it if we do, otherwise return an error message
if s =~ /^[-+/\d\s\eE.()]$/ then
begin
s.untaint
return eval(s).to_s
rescue Exception => detail
puts detail.message()
end
end
return “Error”
end
def handle_server_input(s)
# This isn’t at all efficient, but it shows what we can do with Ruby
# (Dave Thomas calls this construct “a multiway if on steroids”)
case s.strip
when /^PING :(.+)$/i
puts “[ Server ping ]”
send “PONG :#{$1}”
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
puts “[ CTCP PING from #{$1}!#{$2}@#{$3} ]”
send “NOTICE #{$1} :\001PING #{$4}\001”
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]VERSION[\001]$/i
puts “[ CTCP VERSION from #{$1}!#{$2}@#{$3} ]”
send “NOTICE #{$1} :\001VERSION Ruby-irc v0.042\001”
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
puts “[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]”
send “PRIVMSG #{(($4==@nick)?$1:$4)} :#{evaluate($5)}”
else
puts s
end
end
def main_loop()
# Just keep on truckin’ until we disconnect
while true
ready = select([@irc, $stdin], nil, nil, nil)
next if !ready
for s in ready[0]
if s == $stdin then
return if $stdin.eof
s = $stdin.gets
send s
elsif s == @irc then
return if @irc.eof
s = @irc.gets
handle_server_input(s)
end
end
end
end
end

The main program

If we get an exception, then print it out and keep going (we do NOT want

to disconnect unexpectedly!)

irc = IRC.new(‘irc.jakas.siec’, 6667, ‘Alt-255’, ‘#Test123’)
irc.connect()
begin
irc.main_loop()
rescue Interrupt
rescue Exception => detail
puts detail.message()
print detail.backtrace.join("\n")
retry
end[/code]
Wcześniej uczyłem się C++ (też jestem w nim początkujący) i jako tako zasady działania
sieci i protokoły irc znam. Powyższy kod łączy się z irc lecz nie wykonuje
żadnego z poleceń pod case s.strip. A ja nie mam zielonego pojęcia dlaczego.
Więc proszę o pomoc.

Wszystko dziala tak jak powinno.
/ctcp NICK ping czy tez /ctcp NICK version z Twojego klienta irc…
p.

Wydaje mi się, że linijka:

puts "[ Server ping ]"

powinna wyświetlić w oknie konsoli tekst, gdy jest /ctcp NICK ping.
Jednak u mnie tego nie ma.

Wyswietli taki komunikat tylko i wylacznie w momencie kiedy bot zostanie spingowany przez irc server. Pingowanie przez serwer opisane jest w RFC.
Specjalnie dla Ciebie :slight_smile:

NOTICE AUTH :*** Checking Ident
[ Server ping ]
–> PONG :1798920897


:Alt-255!~blah@HOST MODE Alt-255 +i
[ CTCP PING from NICK!~nick@HOST ]
–> NOTICE NICK :PING 1255643722
[ CTCP VERSION from NICK!~nick@HOST ]
–> NOTICE NICK :VERSION Ruby-irc v0.042

$ ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i686-linux]

A u mnie nie działa. ;/
Może to wina starszej wersji ruby?
A konkretnie 1.8.6. Bo instalowałem dla windows z instalatora graficznego i to była jedyna dostępna w ten sposób wersja.
A z tym komunikatem powyżej się pomyliłem, miało być:

puts "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"

Między innymi to też u mnie nie działa.

Sorry, że post pod postem, ale rozwiązałem problem. :wink:
Przyczyną był system. Bot nie działa prawidłowo na windows xp.
Na shellu na Debianie już działa poprawnie. :smiley: