Guest User

Untitled

a guest
Feb 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. module Octopus
  2. class Irc
  3. attr_accessor :http, :bot, :logger
  4.  
  5. def initialize(http, logger, options = {})
  6. self.http = http
  7. self.logger = logger
  8.  
  9. # Initialize array for threads
  10. self.threads = []
  11.  
  12. bot = Cinch::Bot.new do
  13. configure do |c|
  14. c.server = options["server"]
  15. c.channels = options["channels"]
  16. c.nick = options["nick"]
  17. c.realname = options["real_name"]
  18. end
  19. end
  20.  
  21. self.bot = bot
  22. self.logger.debug("Initialized IRC")
  23. end
  24.  
  25. def clean_up_threads
  26. # Clean up all threads
  27. self.threads.each {|t| t.terminate}
  28. end
  29.  
  30. def run!
  31. should_run = true
  32.  
  33. self.logger.debug("Running bot!")
  34. @Thread.new do
  35. self.bot.start
  36. end
  37.  
  38. # Trap ctrl+c SIGINT
  39. Signal.trap("SIGINT") do
  40. # Stop while loop in run!
  41. should_run = false
  42.  
  43. clean_up_threads
  44. end
  45.  
  46. sleep 3
  47. while should_run
  48. if http.message
  49. message = http.message[:message]
  50. channel = http.message[:channel]
  51. if message
  52. self.logger.debug("Sending: #{http.message}")
  53. puts channel
  54. if channel
  55. channel = self.bot.channels.find{|x| x == "##{channel}"}
  56. end
  57. channel ||= self.bot.channels.first
  58. channel.safe_send(message)
  59. http.message = nil
  60. end
  61. sleep 1
  62. end
  63. end
  64. end
  65. end
  66. end
Add Comment
Please, Sign In to add comment