Guest User

Untitled

a guest
Mar 8th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. ## main.rb
  2. #!/usr/bin/ruby
  3.  
  4. require 'irc_bot.rb'
  5.  
  6. puts "Loading IRC bot class..."
  7. i = IRC.new "irc://Vanniz:p255w0rd@irc.freenode.net:6667/kongbots"
  8. puts "Loaded, connecting..."
  9. i.connect
  10. i.loop
  11. puts "Terminated"
  12.  
  13. ## irc_bot.rb
  14. #!/usr/bin/ruby
  15.  
  16. require 'uri'
  17. require 'socket'
  18.  
  19. class IRC
  20. @nick = ""
  21. @password = nil
  22. @server = ""
  23. @port = 6667
  24. @channel = ""
  25. @irc = nil
  26. @pong_pid = 0
  27.  
  28. def initialize(uri)
  29. u = URI.parse(uri)
  30. if u.scheme != "irc" || u.user == "" || u.path == "" || u.host == ""
  31. raise "Incorrect IRC URI"
  32. end
  33. # Required parts
  34. @nick = u.user
  35. @server = u.host
  36. @channel = "##{u.path.split('/')[1]}"
  37. # Optional, but are still set
  38. @password = u.password || nil
  39. @port = u.port || 6667
  40. end
  41.  
  42. def connect
  43. puts "Connecting to #{@server}..."
  44. @irc = TCPSocket.new @server, @port
  45. raw "NICK #{@nick}"
  46. raw "USER foo bar baz :borked"
  47. @pong_pid = fork do
  48. pong_count = 1
  49. while true
  50. raw "PONG :#{pong_count}"
  51. pong_count += 1
  52. sleep 15
  53. end
  54. end
  55. end
  56.  
  57. def raw(msg)
  58. begin
  59. @irc.puts msg
  60. puts "-- #{msg}"
  61. rescue Exception => e
  62. # Do error stuff
  63. puts "Error writing to TCP socket at #{@server}"
  64. puts e.messagee
  65. puts e.backtrace.join("\n")
  66. end
  67. end
  68.  
  69. def loop
  70. while true
  71. break if @irc.eof
  72. line = @irc.gets
  73. break if line == nil || line == ""
  74. if line =~ /^PING\s:(.*)/
  75. puts "Server PING #{$1}"
  76. raw "PONG #{$1}"
  77. elsif line =~ /^:(.*?)\s([0-9]*?)\s(.*?)\s:(.*)/
  78. case $2
  79. when "375" : puts "+++ MOTD"
  80. when "376" : puts "--- MOTD"; introduce
  81. when "372" : puts "(MOTD) #{$4}"
  82. when "001" : puts $4
  83. when "002" : puts $4
  84. when "003" : puts $4
  85. when "005" : # Nothing
  86. when "251" : puts $4
  87. when "252" : puts $3.split(' ')[1] + " " + $4
  88. when "253" : puts $3.split(' ')[1] + " " + $4
  89. when "254" : puts $3.split(' ')[1] + " " + $4
  90. when "255" : puts $4
  91. when "265" : puts $4
  92. when "266" : puts $4
  93. when "353" : puts "Current users: #{$4}"
  94. when "366" : # Nothing
  95. else puts line
  96. end
  97. elsif line =~ /.*? 004 (.*?) (.*?) (.*?) (.*?)$/
  98. puts "Connected to #{$2} version #{$3}"
  99. # elsif line =~ /:(.*?)\s([A-Z]+?)\s[#{@nick}|(#.*?)]\s:(.*?)/
  100. # puts $1, $2, $3, $4
  101. else
  102. puts line
  103. end
  104. end
  105. end
  106.  
  107. def introduce
  108. raw "JOIN #{@channel}"
  109. say "The #{@nick} is watching"
  110. end
  111.  
  112. def say(msg, target=@channel)
  113. raw "PRIVMSG #{target} :#{msg}"
  114. end
  115.  
  116. def killall
  117. @irc.close
  118. Process.kill("HUP", @pong_pid)
  119. end
  120. end
  121.  
  122. if __FILE__ == $0
  123. i = IRC.new "irc://Vanniz:p255w0rd@irc.frenode.net:6667/kongbots"
  124. trap(:INT) do
  125. i.say "My owner has called the quits, bye bye"
  126. i.killall
  127. end
  128. begin
  129. i.connect
  130. i.loop
  131. rescue Exception => e
  132. # Neccessary because a fork{}-ed process will continue to run
  133. # so the error is printed AND the process is completely killed
  134. puts e.message
  135. puts e.backtrace.join("\n")
  136. i.killall
  137. end
  138. puts "Terminated!"
  139. end
Add Comment
Please, Sign In to add comment