Guest User

Untitled

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