Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "socket"
- class User
- attr_reader :nick, :realname, :login, :password
- def initialize(nick, realname, login, password)
- @nick = nick
- @realname = realname
- @login = login
- @password = password
- end
- end
- class Server
- attr_reader :user, :server, :port, :channels
- def initialize(user, server, port, channels=[])
- @user = user
- @server = server
- @port = port
- @channels = channels
- @bot = Bot.new(@user)
- end
- def connect
- @connection = TCPSocket.open(@server, @port)
- print("addr: ", @connection.addr.join(":"), "\n")
- print("peer: ", @connection.peeraddr.join(":"), "\n")
- @connection.puts "USER #{@user.login} 0 * :#{@user.realname}"
- @connection.puts "NICK #{@user.nick}"
- @connection.puts "PRIVMSG NickServ :IDENTIFY #{@user.password}"
- sleep 2
- @connection.puts "JOIN #{@channels.join(",")}"
- maintain
- end
- def maintain
- until @connection.eof? do
- msg = @connection.gets
- puts msg
- response = @bot.send(msg)
- if response
- puts response
- send(response)
- end
- end
- end
- def send(msg)
- @connection.puts("#{msg}")
- end
- private :maintain
- end
- class Bot
- def initialize(user)
- @nick = user.nick
- @realname = user.realname
- @login = user.login
- end
- def send(msg)
- a = msg.split(/\s+/)
- ping = check_ping(a[0])
- if ping
- "PONG #{a[1]}"
- else
- t = Time.new
- user = a[0].split(/!/)
- nick = user[0].sub(/^:/, "")
- type = a[1]
- target = a[2]
- msg = a[3..-1].join(" ").sub(/^:/, "")
- puts "[#{t.strftime("%H:%M:%S")}] #{a[2]} <#{nick}> #{msg}"
- msgbytes = msg.bytes.to_a
- if type == "PRIVMSG"
- if msgbytes[0] == 1 && msgbytes[-1] == 1 #ctcp
- msg = clear_ctcp_bytes msgbytes
- if msg == "VERSION"
- r = add_ctcp_bytes "VERSION oneechan:ichiban-saiko:ruby".bytes.to_a
- "NOTICE #{nick} #{r}"
- elsif msg == "FINGER"
- r = add_ctcp_bytes "FINGER :\u3884\u3863\u3868\u3888~"
- "NOTICE #{nick} #{r}"
- end
- else #privmsg
- nil
- end
- elsif type == "NOTICE"
- nil
- end
- end
- end
- def clear_ctcp_bytes(msgbytes)
- msgbytes.shift
- msgbytes.pop
- msgbytes.pack("U*")
- end
- def add_ctcp_bytes(msg)
- r = msg.bytes.to_a
- r.unshift 1
- r.push 1
- r.pack("U*")
- end
- def check_ping(msg)
- if msg == "PING"
- 1
- end
- end
- end
- def sigint_handler
- system "stty echo" # just in case
- puts ObjectSpace.each_object(Server) { |a| a.send("QUIT :Fare ye well"); }
- exit
- end
- Signal.trap("INT") { sigint_handler }
- print "IRC Bot: Enter id password: "
- system "stty -echo"
- password = gets
- system "stty echo"
- servers = []
- me = User.new("oneechan", "Your favourite oneechan~", "b", password)
- rizon = Server.new(me, "irc.rizon.net", "6667", ["#oneechan"])
- servers.push(rizon)
- servers.each {|s| s.connect}
Add Comment
Please, Sign In to add comment