Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lita-tox/lib/lita/adapters/tox.rb

94 lines
2.5 KiB
Ruby
Raw Permalink Normal View History

2017-07-20 15:33:29 -04:00
# frozen_string_literal: true
2017-07-20 15:07:10 -04:00
# lita-tox - Tox adapter for the Lita chat bot
# Copyright (C) 2015-2017 Braiden Vasco
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-09-13 15:40:46 -04:00
require 'tox'
2015-07-25 04:32:00 -04:00
##
# Lita module.
#
module Lita
2015-07-25 04:32:00 -04:00
##
# Lita adapters module.
#
module Adapters
2015-07-25 04:32:00 -04:00
##
# Tox adapter for the Lita chat bot.
#
class Tox < Adapter
2015-09-13 19:21:16 -04:00
config :savedata_filename, type: String
2015-09-15 16:08:48 -04:00
config :status, type: String
2015-09-13 19:21:16 -04:00
2017-07-20 15:55:05 -04:00
def initialize(robot) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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)
2017-07-20 16:26:59 -04:00
options.savedata = savedata_file.read
2015-09-13 19:21:16 -04:00
savedata_file.close
end
2017-07-20 16:26:59 -04:00
@tox = ::Tox::Client.new(options)
2015-09-13 15:40:46 -04:00
2017-07-20 16:26:59 -04:00
log.info("ID: #{@tox.address.to_hex}")
@tox.bootstrap_official
2015-09-13 15:40:46 -04:00
2015-09-15 16:08:48 -04:00
@tox.name = robot.name if robot.name
@tox.status_message = config.status if config.status
2017-07-20 16:26:59 -04:00
@tox.on_friend_request do |public_key|
@tox.friend_add_norequest(public_key)
2015-09-13 15:40:46 -04:00
end
2017-07-20 16:26:59 -04:00
@tox.on_friend_message do |friend, text|
user = User.new(friend.number)
2015-09-13 15:40:46 -04:00
source = Source.new(user: user)
message = Message.new(robot, text, source)
message.command!
robot.receive(message)
end
end
def run
2017-07-20 16:26:59 -04:00
@tox.run
2015-09-13 15:40:46 -04:00
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
2017-07-20 16:26:59 -04:00
@tox.stop
2015-09-13 19:21:16 -04:00
end
2015-09-13 15:40:46 -04:00
def send_messages(target, messages)
messages.reject(&:empty?).each do |message|
2017-07-20 16:26:59 -04:00
::Tox::Friend.new(@tox, target.user.id.to_i).send_message(message)
2015-09-13 15:40:46 -04:00
end
end
end
Lita.register_adapter(:tox, Tox)
end
end