forestbot/questbook.rb

84 lines
1.8 KiB
Ruby
Raw Normal View History

2017-10-17 20:30:42 +03:00
require 'cinch'
require 'cinch-dicebag'
require 'net/http'
require 'uri'
require 'json'
require 'formdata'
require_relative 'plugins/link_info'
require_relative 'plugins/help'
class QuestBookChat
def initialize(bot)
@bot = bot
end
def start
while true
2017-10-17 20:44:40 +03:00
sleep 5
2017-10-17 20:30:42 +03:00
@bot.handlers.dispatch(:questbook)
end
end
end
class QuestBook
include Cinch::Plugin
listen_to :questbook
def listen(m)
params = FormData.new
params.append('msg', '')
2017-10-17 20:49:06 +03:00
params.append('from', "#{Time.now.to_i - 5}00")
2017-10-17 20:30:42 +03:00
params.append('away', 0)
req = params.post_request("/forum/chat/message.php")
http = Net::HTTP.new('quest-book.ru', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req.add_field 'Referer', 'https://quest-book.ru/forum/chat/'
req.add_field 'X-Requested-With', 'XMLHttpRequest'
response = http.request(req)
response = JSON.parse response.body
begin
if response['msgs'] then
for msg in response['msgs'] do
who = '[Система]'
if msg[1] != '' then
who = "[#{msg[1]}]"
end
Channel('#questbook').send("#{who}: #{msg[2]}")
end
end
rescue NoMethodError
puts response['msgs'].inspect
end
end
end
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.forestnet.org"
c.port = 6667
c.channels = ["#questbook"]
c.nick = 'Квестбот'
2017-10-17 20:30:42 +03:00
c.plugins.plugins = [
Cinch::Plugins::Dicebag,
Cinch::Help,
Cinch::LinkInfo,
QuestBook
]
c.plugins.options[Cinch::LinkInfo] = {
:blacklist => [/\.xz$/i, /\.zip$/i, /\.rar$/i],
:no_description => true,
}
end
on :message do |m|
end
end
bot.loggers.level = :info
Thread.new {
QuestBookChat.new(bot).start
}
bot.start