Guest User

Untitled

a guest
Mar 5th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # Marshmallow, the campfire chatbot
  4. #
  5. # You need to know one the following:
  6. # (a) the secret public URL, or
  7. # (b) an account/password for your room and the room number.
  8. #
  9. # Usage:
  10. # to login with a password:
  11. #
  12. # bot = Marshmallow.new( :domain => 'mydomain', :ssl => true )
  13. # bot.login :method => :login,
  14. # :username => "yourbot@email.com",
  15. # :password => "passw0rd",
  16. # :room => "11234"
  17. # bot.say("So many ponies in here! I want one!")
  18. #
  19. # to use the public url:
  20. #
  21. # Marshmallow.domain = 'mydomain'
  22. # bot = Marshmallow.new
  23. # bot.login( :url => 'aDxf3' )
  24. # bot.say "Ponies!!"
  25. # bot.paste "<script type='text/javascript'>\nalert('Ponies!')\n</script>"
  26. #
  27.  
  28. class Marshmallow
  29. require 'net/https'
  30. require 'open-uri'
  31. require 'cgi'
  32. require 'yaml'
  33.  
  34. def self.version
  35. "0.2"
  36. end
  37.  
  38. def self.domain
  39. @domain
  40. end
  41.  
  42. def self.domain=(value)
  43. @domain = value
  44. end
  45.  
  46. def initialize(options={})
  47. @debug = options[:debug]
  48. @domain = options[:domain] || @@domain
  49. @ssl = options[:ssl]
  50. end
  51.  
  52. def login(options)
  53. options = { :method => :url, :username => 'Marshmallow' }.merge(options)
  54.  
  55. @req = Net::HTTP::new("#{@domain}.campfirenow.com", @ssl ? 443 : 80)
  56. @req.use_ssl = @ssl
  57. headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
  58.  
  59. case options[:method]
  60. when :url
  61. res = @req.post("/#{options[:url]}", "name=#{options[:username]}", headers)
  62. # parse our response headers for the room number.. magic!
  63. @room_id = res['location'].scan(/room\/(\d+)/).to_s
  64. puts res.body if @debug
  65.  
  66. when :login
  67. params = "email_address=#{CGI.escape(options[:username])}&password=#{CGI.escape(options[:password])}"
  68. puts params if @debug
  69. res = @req.post("/login/", params, headers)
  70. @room_id = options[:room]
  71. puts "Logging into room #{@room_id}" if @debug
  72. puts res.body if @debug
  73. end
  74.  
  75. @headers = { 'Cookie' => res.response['set-cookie'] }
  76. res2 = @req.get(res['location'], @headers)
  77. puts res2.body if @debug
  78.  
  79. # refresh our headers
  80. @headers = { 'Cookie' => res.response['set-cookie'] }
  81. @req.get("/room/#{@room_id}/") # join the room if necessary
  82. return @headers
  83. end
  84.  
  85. def paste(message)
  86. say(message, true)
  87. end
  88.  
  89. def say(message, paste=false)
  90. puts "Posting #{message}" if @debug
  91. res = @req.post("/room/#{@room_id}/speak", "#{'paste=true&' if paste}message=#{CGI.escape(message.to_s)}", @headers)
  92. puts res.body if @debug
  93. end
  94. end
  95.  
  96. # run this like script/runner script/marshmallow "something you want to say"
  97. bot = Marshmallow.new(:domain => "foo.campfirenow.com", :ssl => false)
  98. bot.login :method => :login, :username => "foo@example.com", :password => "t3hsekrit", :room => 12345
  99. bot.say ARGV[0]
Add Comment
Please, Sign In to add comment