Guest User

Untitled

a guest
Feb 28th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 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
  89.  
  90. ##the original echo_threaded.rb
  91. #!/usr/bin/ruby
  92.  
  93. # This bot will reply to every message it receives. To end the game, send 'exit'
  94. # THREADED VERSION
  95.  
  96. require 'xmpp4r/client'
  97. include Jabber
  98.  
  99. # settings
  100. if ARGV.length != 2
  101. puts "Run with ./echo_thread.rb user@server/resource password"
  102. exit 1
  103. end
  104. myJID = JID::new(ARGV[0])
  105. myPassword = ARGV[1]
  106. cl = Client::new(myJID)
  107. cl.connect
  108. cl.auth(myPassword)
  109. cl.send(Presence::new)
  110. puts "Connected ! send messages to #{myJID.strip.to_s}."
  111. mainthread = Thread.current
  112. cl.add_message_callback do |m|
  113. if m.type != :error
  114. cl.send(Message::new(m.from, "You sent: #{m.body}"))
  115. if m.body == 'exit'
  116. cl.send(Message::new(m.from, "Exiting ..."))
  117. mainthread.wakeup
  118. end
  119. end
  120. end
  121. Thread.stop
  122. cl.close
Add Comment
Please, Sign In to add comment