2015-09-13 15:40:46 -04:00
|
|
|
require 'tox'
|
|
|
|
|
2015-07-25 04:32:00 -04:00
|
|
|
##
|
|
|
|
# Lita module.
|
|
|
|
#
|
2015-07-25 03:09:57 -04:00
|
|
|
module Lita
|
2015-07-25 04:32:00 -04:00
|
|
|
##
|
|
|
|
# Lita adapters module.
|
|
|
|
#
|
2015-07-25 03:09:57 -04:00
|
|
|
module Adapters
|
2015-07-25 04:32:00 -04:00
|
|
|
##
|
|
|
|
# Tox adapter for the Lita chat bot.
|
|
|
|
#
|
2015-07-25 03:09:57 -04:00
|
|
|
class Tox < Adapter
|
2015-09-13 19:21:16 -04:00
|
|
|
config :savedata_filename, type: String
|
|
|
|
|
2015-09-13 15:40:46 -04:00
|
|
|
def initialize(robot)
|
2015-09-13 19:10:25 -04:00
|
|
|
super
|
|
|
|
|
2015-09-13 19:21:16 -04:00
|
|
|
options = ::Tox::Options.new
|
|
|
|
|
|
|
|
if config.savedata_filename && File.exist?(config.savedata_filename)
|
|
|
|
savedata_file = open(config.savedata_filename)
|
|
|
|
options.data = savedata_file.read
|
|
|
|
savedata_file.close
|
|
|
|
end
|
|
|
|
|
|
|
|
@tox = ::Tox.new(options)
|
2015-09-13 15:40:46 -04:00
|
|
|
|
|
|
|
log.info("ID: #{@tox.id}")
|
|
|
|
|
|
|
|
@tox.on_friend_request do |key|
|
|
|
|
@tox.friend_add_norequest(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
@tox.on_friend_message do |friend_number, text|
|
|
|
|
user = User.new(friend_number)
|
|
|
|
source = Source.new(user: user)
|
|
|
|
message = Message.new(robot, text, source)
|
|
|
|
|
|
|
|
message.command!
|
|
|
|
robot.receive(message)
|
|
|
|
end
|
2015-09-14 04:10:18 -04:00
|
|
|
|
|
|
|
@tox.on_group_invite do |friend_number, data|
|
|
|
|
@tox.join_groupchat(friend_number, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
@tox.on_group_message do |group_number, peer_number, text|
|
|
|
|
unless @tox.group_peernumber_is_ours(group_number, peer_number)
|
|
|
|
user = User.new(-1 - peer_number) # TODO
|
|
|
|
source = Source.new(user: user, room: group_number)
|
|
|
|
message = Message.new(robot, text, source)
|
|
|
|
|
|
|
|
robot.receive(message)
|
|
|
|
end
|
|
|
|
end
|
2015-09-13 15:40:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@tox.loop
|
|
|
|
end
|
|
|
|
|
2015-09-13 19:21:16 -04:00
|
|
|
def shut_down
|
|
|
|
if config.savedata_filename
|
|
|
|
savedata_file = open(config.savedata_filename, 'w')
|
|
|
|
savedata_file.write(@tox.savedata)
|
|
|
|
savedata_file.close
|
|
|
|
end
|
|
|
|
|
|
|
|
@tox.kill
|
|
|
|
end
|
|
|
|
|
2015-09-13 15:40:46 -04:00
|
|
|
def send_messages(target, messages)
|
|
|
|
messages.reject(&:empty?).each do |message|
|
2015-09-14 04:10:18 -04:00
|
|
|
if target.user.id.to_i >= 0
|
|
|
|
@tox.friend_send_message(target.user.id.to_i, message)
|
|
|
|
else
|
|
|
|
@tox.group_message_send(target.room.to_i, message)
|
|
|
|
end
|
2015-09-13 15:40:46 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-25 03:09:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Lita.register_adapter(:tox, Tox)
|
|
|
|
end
|
|
|
|
end
|