Guest User

Untitled

a guest
Mar 9th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. require "socket"
  4.  
  5. # Don't allow use of "tainted" data by potentially dangerous operations
  6. $SAFE=1
  7.  
  8. # The irc class, which talks to the server and holds the main event loop
  9. class IRC
  10. def initialize(server, port, nick, channel, password, owner, commandchar)
  11. @server = server
  12. @port = port
  13. @nick = nick
  14. @channel = channel
  15. @password = password
  16. @owner = owner
  17. @cmdc = commandchar
  18. end
  19. def send(s)
  20. # Send a message to the irc server and print it to the screen
  21. @irc.send "#{s}\n", 0
  22. consoleLog("---> #{s}")
  23. puts("---> #{s}")
  24. #Logs all sent data
  25. end
  26. def connect()
  27. # Connect to the IRC server
  28. @irc = TCPSocket.open(@server, @port)
  29. #Set Nick, then join channel, then login with nickserv (sleep for response), then join
  30. #the channel + say hello, then say hello to
  31. #the owner.
  32. send "USER TagBot TagBot TagBot :TagBot"
  33. send "NICK #{@nick}"
  34. send "PRIVMSG Nickserv :identify #{@password}"
  35. sleep 5
  36. send "JOIN #{@channel}"
  37. chanMsg("Hello!")
  38. end
  39. def parseRawInput(s)
  40. #Parses raw server input, then decides if it should go to one of the parse functions. Also checks if
  41. #the server is pinging us.
  42. if s.include?("PRIVMSG #{@channel}") #Channel msg
  43. parseChannelMsg(s)
  44. elsif s.include?("PRIVMSG #{@nick}") #Private msg
  45. parsePrivMsg(s)
  46. elsif s.include?("JOIN") #Someone joined
  47. parseJoin(s)
  48. elsif s.include?("PART") #Someone parted
  49. parsePart(s)
  50. elsif s.include?("QUIT") #Someone quit
  51. parsePart(s)
  52. elsif s.include?("PING") #Got a server ping
  53. @irc.send "PONG sky.com\n", 0
  54. end
  55. end
  56. def parseRawMsg(raw)
  57. #Parses PRIVMSG to get the user who sent it and the message.
  58. raw = raw.split
  59. user = raw[0].split("!")[0][1..-1]
  60. raw = raw.join(" ")
  61. raw = raw.split
  62. msg = raw[3..-1].join(" ")[1..-1]
  63. return [user, msg]
  64. end
  65. def parsePrivMsg(raw)
  66. #Parses receieving (non-channel specific) PRIVMSG. Checks if it is a command, if not
  67. #doesn't respond.
  68. x = parseRawMsg(raw)
  69. user = x[0]
  70. cmd = x[1]
  71. source = "priv"
  72. load "commands.rb"
  73. end
  74. def doLog(file, msg)
  75. #Logs a line to @channel.txt
  76. logFile = File.new("logs/#{file.downcase}.txt", "a")
  77. timestamp = Time.now.ctime[11..18]
  78. logFile.puts("[#{timestamp}] #{msg}")
  79. logFile.close
  80. end
  81. def consoleLog(msg)
  82. #Console logfile, logs every server message and all sent messages.
  83. logFile = File.new("logs/console.txt", "a") # Could be more dynamic
  84. timestamp = Time.now.ctime[11..18]
  85. logFile.puts("[#{timestamp}] #{msg}")
  86. logFile.close
  87. end
  88. def parseChannelMsg(raw)
  89. x = parseRawMsg(raw)
  90. user = x[0]
  91. cmd = x[1]
  92. source = "chan"
  93. load "commands.rb"
  94. doLog("#{@channel}", "#{user}: #{msg}") # Log channel message
  95. end
  96. def parseJoin(raw)
  97. raw = raw.split
  98. user = raw[0].split("!")[0][1..-1]
  99. doLog("#{@channel}", "#{user} joined #{@channel}.")
  100. end
  101. def parsePart(raw)
  102. raw = raw.split
  103. user = raw[0].split("!")[0][1..-1]
  104. doLog("#{@channel}", "#{user} left #{@channel}.")
  105. end
  106. def runningLoop()
  107. while true
  108. ready = select([@irc, $stdin], nil, nil, nil)
  109. next if !ready
  110. for s in ready[0]
  111. if s == $stdin then
  112. return if $stdin.eof
  113. s = $stdin.gets
  114. send s
  115. elsif s == @irc then
  116. return if @irc.eof
  117. s = @irc.gets
  118. parseRawInput(s)
  119. consoleLog("<--- #{s}")
  120. puts("<--- #{s}")
  121. end
  122. end
  123. end
  124. end
  125. def chanMsg(msg)
  126. send "PRIVMSG #{@channel} :#{msg}"
  127. doLog("#{@channel}", "#{@nick}: #{msg}")
  128. end
  129. def privMsg(msg, user = "#{@owner}")
  130. #Send a message to a user, if no user is given send a message to the owner. This saves
  131. #making a seperate ownerMsg function.
  132. send "PRIVMSG #{user} :#{msg}"
  133. end
  134. end
  135.  
  136. # The main program
  137. # If we get an exception, then print it out and keep going (we do NOT want
  138. # to disconnect unexpectedly!)
  139. irc = IRC.new('irc.freenode.org', 6667, 'TagBot', '#Mortomes', "password", "Taggard", ".")
  140. irc.connect()
  141. begin
  142. irc.runningLoop()
  143. rescue Interrupt
  144. rescue Exception => detail
  145. puts detail.message()
  146. print detail.backtrace.join("\n")
  147. retry
  148. end
Add Comment
Please, Sign In to add comment