Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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, password, owner, commandchar)
- @server = server
- @port = port
- @nick = nick
- @channel = channel
- @password = password
- @owner = owner
- @cmdc = commandchar
- end
- def send(s)
- # Send a message to the irc server and print it to the screen
- @irc.send "#{s}\n", 0
- consoleLog("---> #{s}")
- puts("---> #{s}")
- #Logs all sent data
- end
- def connect()
- # Connect to the IRC server
- @irc = TCPSocket.open(@server, @port)
- #Set Nick, then join channel, then login with nickserv (sleep for response), then join
- #the channel + say hello, then say hello to
- #the owner.
- send "USER TagBot TagBot TagBot :TagBot"
- send "NICK #{@nick}"
- send "PRIVMSG Nickserv :identify #{@password}"
- sleep 5
- send "JOIN #{@channel}"
- chanMsg("Hello!")
- end
- def parseRawInput(s)
- #Parses raw server input, then decides if it should go to one of the parse functions. Also checks if
- #the server is pinging us.
- if s.include?("PRIVMSG #{@channel}") #Channel msg
- parseChannelMsg(s)
- elsif s.include?("PRIVMSG #{@nick}") #Private msg
- parsePrivMsg(s)
- elsif s.include?("JOIN") #Someone joined
- parseJoin(s)
- elsif s.include?("PART") #Someone parted
- parsePart(s)
- elsif s.include?("QUIT") #Someone quit
- parsePart(s)
- elsif s.include?("PING") #Got a server ping
- @irc.send "PONG sky.com\n", 0
- end
- end
- def parseRawMsg(raw)
- #Parses PRIVMSG to get the user who sent it and the message.
- raw = raw.split
- user = raw[0].split("!")[0][1..-1]
- raw = raw.join(" ")
- raw = raw.split
- msg = raw[3..-1].join(" ")[1..-1]
- return [user, msg]
- end
- def parsePrivMsg(raw)
- #Parses receieving (non-channel specific) PRIVMSG. Checks if it is a command, if not
- #doesn't respond.
- x = parseRawMsg(raw)
- user = x[0]
- cmd = x[1]
- source = "priv"
- load "commands.rb"
- end
- def doLog(file, msg)
- #Logs a line to @channel.txt
- logFile = File.new("logs/#{file.downcase}.txt", "a")
- timestamp = Time.now.ctime[11..18]
- logFile.puts("[#{timestamp}] #{msg}")
- logFile.close
- end
- def consoleLog(msg)
- #Console logfile, logs every server message and all sent messages.
- logFile = File.new("logs/console.txt", "a") # Could be more dynamic
- timestamp = Time.now.ctime[11..18]
- logFile.puts("[#{timestamp}] #{msg}")
- logFile.close
- end
- def parseChannelMsg(raw)
- x = parseRawMsg(raw)
- user = x[0]
- cmd = x[1]
- source = "chan"
- load "commands.rb"
- doLog("#{@channel}", "#{user}: #{msg}") # Log channel message
- end
- def parseJoin(raw)
- raw = raw.split
- user = raw[0].split("!")[0][1..-1]
- doLog("#{@channel}", "#{user} joined #{@channel}.")
- end
- def parsePart(raw)
- raw = raw.split
- user = raw[0].split("!")[0][1..-1]
- doLog("#{@channel}", "#{user} left #{@channel}.")
- end
- def runningLoop()
- 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
- parseRawInput(s)
- consoleLog("<--- #{s}")
- puts("<--- #{s}")
- end
- end
- end
- end
- def chanMsg(msg)
- send "PRIVMSG #{@channel} :#{msg}"
- doLog("#{@channel}", "#{@nick}: #{msg}")
- end
- def privMsg(msg, user = "#{@owner}")
- #Send a message to a user, if no user is given send a message to the owner. This saves
- #making a seperate ownerMsg function.
- send "PRIVMSG #{user} :#{msg}"
- 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.freenode.org', 6667, 'TagBot', '#Mortomes', "password", "Taggard", ".")
- irc.connect()
- begin
- irc.runningLoop()
- rescue Interrupt
- rescue Exception => detail
- puts detail.message()
- print detail.backtrace.join("\n")
- retry
- end
Add Comment
Please, Sign In to add comment