Guest User

Untitled

a guest
Feb 28th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. ##jbot.rb
  2. #!/usr/bin/ruby
  3.  
  4. require 'xmpp4r/client'
  5. include Jabber
  6. require 'bebysku.rb'
  7.  
  8. Bebysku.new
  9.  
  10. ##bebysku.rb
  11. require 'xmpp4r/client'
  12. include Jabber
  13. require 'brain'
  14.  
  15. class Bebysku
  16. def die message
  17. puts message
  18. exit 1
  19. end
  20.  
  21. def read_conf
  22. File.open("bebysku.conf", "r") do |file|
  23. file.each_line do |line|
  24. if not line =~ /^\s*".+"\s*=\s*".*"\s*$/
  25. #throw something
  26. die('Configuration file is invalid: ' + line)
  27. elsif line.count('"') != 4
  28. die('Configuration file is invalid: ' + line)
  29. else
  30. @conf[line[/^\s*"(.+)"\s*=\s*".*"\s*$/,1]] = line[/^\s*".+"\s*=\s*"(.*)"\s*$/,1]
  31. end
  32. end
  33. end
  34. end
  35.  
  36. def connect
  37. jid = JID::new(@conf["jid"])
  38. #converting string to JID, back and forth... Just so that our fancy
  39. #processors had something to do. Human factor is the weakest link in
  40. #computing.
  41. #p @conf["jid"]
  42. #p username = @conf["jid"][/^(.+)@.+\....?\/.*$/,1]
  43. #p server = @conf["jid"][/^.+@(.+\....?)\/.*$/,1]
  44. #p resource = @conf["jid"][/^.+@.+\....?\/(.*)$/,1]
  45. password = @conf["password"]
  46.  
  47. @client = Client::new(jid)
  48. @client.connect
  49. @client.auth(password)
  50. @client.send(Presence::new)
  51. puts "Connected to #{@conf["jid"]}"
  52. end
  53. def startup
  54. mainthread = Thread.current
  55. @client.add_message_callback do |m|
  56. @client.send(@brain.think(m))
  57. mainthread.wakeup
  58. end
  59. Thread.stop
  60. end
  61.  
  62. def initialize
  63. @conf = {}
  64. @brain = Brain.new
  65. read_conf
  66. connect
  67. #the main loop is here
  68. startup
  69. #and the cleanup follows:
  70. @client.close
  71. end
  72. end
  73.  
  74. ##brain.rb
  75. require 'xmpp4r/client'
  76.  
  77. class Brain
  78. def initialize
  79. archive = []
  80. knowledge = {}
  81. end
  82. def think(m)
  83. p m.body
  84. if m.type != :error
  85. return Message::new(m.from, "You sent: #{m.body}")
  86. end
  87. end
  88. end
Add Comment
Please, Sign In to add comment