Archived
1
0
Fork 0

Merge branch 'development'

This commit is contained in:
Braiden Vasco 2015-09-13 20:48:49 +00:00
commit aa0ce8985e
10 changed files with 592 additions and 7 deletions

4
.gitignore vendored
View file

@ -9,6 +9,10 @@
/test/version_tmp/
/tmp/
Makefile
*.o
*.so
## Specific to RubyMotion:
.dat*
.repl_history

22
CHANGELOG.md Normal file
View file

@ -0,0 +1,22 @@
# Change Log
## [v0.1.0](https://github.com/braiden-vasco/lita-tox/tree/v0.1.0) (2015-09-13)
**Merged pull requests:**
- Add instructions [\#13](https://github.com/braiden-vasco/lita-tox/pull/13) ([braiden-vasco](https://github.com/braiden-vasco))
- Basic implementation [\#12](https://github.com/braiden-vasco/lita-tox/pull/12) ([braiden-vasco](https://github.com/braiden-vasco))
- Basic "tox" extension implementation [\#11](https://github.com/braiden-vasco/lita-tox/pull/11) ([braiden-vasco](https://github.com/braiden-vasco))
- Add code documentation [\#10](https://github.com/braiden-vasco/lita-tox/pull/10) ([braiden-vasco](https://github.com/braiden-vasco))
- Initialize Lita adapter [\#9](https://github.com/braiden-vasco/lita-tox/pull/9) ([braiden-vasco](https://github.com/braiden-vasco))
- Add changelog generator [\#8](https://github.com/braiden-vasco/lita-tox/pull/8) ([braiden-vasco](https://github.com/braiden-vasco))
- Use YARD [\#7](https://github.com/braiden-vasco/lita-tox/pull/7) ([braiden-vasco](https://github.com/braiden-vasco))
- Configure Coveralls [\#6](https://github.com/braiden-vasco/lita-tox/pull/6) ([braiden-vasco](https://github.com/braiden-vasco))
- Configure Travis CI [\#5](https://github.com/braiden-vasco/lita-tox/pull/5) ([braiden-vasco](https://github.com/braiden-vasco))
- Add README [\#4](https://github.com/braiden-vasco/lita-tox/pull/4) ([braiden-vasco](https://github.com/braiden-vasco))
- Configure testing [\#3](https://github.com/braiden-vasco/lita-tox/pull/3) ([braiden-vasco](https://github.com/braiden-vasco))
- Configure linting [\#2](https://github.com/braiden-vasco/lita-tox/pull/2) ([braiden-vasco](https://github.com/braiden-vasco))
- Initialize gem [\#1](https://github.com/braiden-vasco/lita-tox/pull/1) ([braiden-vasco](https://github.com/braiden-vasco))
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*

View file

@ -5,4 +5,67 @@ Lita::Adapters::Tox
[![Build Status](https://travis-ci.org/braiden-vasco/lita-tox.svg)](https://travis-ci.org/braiden-vasco/lita-tox)
[![Coverage Status](https://coveralls.io/repos/braiden-vasco/lita-tox/badge.svg)](https://coveralls.io/r/braiden-vasco/lita-tox)
[Tox](https://tox.chat) adapter for the Lita chat bot
[Tox](https://tox.chat) adapter for the [Lita](http://lita.io) chat bot.
**WARNING**
Still in development. Read the README carefully.
TODO
----
Current development version have some limitations
which should be fixed in first release:
* Adapter doesn't save Tox state, so you have to send friendship
request again after each run. Current Tox ID is shown at start
as Lita's information message
* Only private chats are supported. Adapter will not respond to group invite
* **libtoxcore** is not included in the gem. It should be compiled manually
to build the gem native extension successfully (see the instructions below)
* JRuby is not supported. Only C extension for Tox is implemented
* Message length is limited to value of `TOX_MAX_MESSAGE_LENGTH`
(see [source code of **libtoxcore**](https://github.com/irungentoo/toxcore/blob/2ab3b14731061cc04d3ccc50a35093c11d018298/toxcore/tox.h#L252-L255))
* Adapter has incomplete API for Lita. Only basic methods are implemented
Usage
-----
At first, see the documentation for Lita: http://docs.lita.io/
### Installation
**libtoxcore** should be compiled manually at your computer or server.
Follow the instructions at
[that file](https://github.com/irungentoo/toxcore/blob/2ab3b14731061cc04d3ccc50a35093c11d018298/INSTALL.md)
When **libtoxcore** is installed, add **lita-tox**
to your Lita instance's Gemfile:
```ruby
gem 'lita-tox', '~> 0.1.0'
```
### Configuration
No configuration is needed. You only have to specify adapter as `:tox`
#### Example
This is an example `lita_config.rb` file:
```ruby
Lita.configure do |config|
config.robot.name = 'Lita'
config.robot.mention_name = 'lita'
config.robot.adapter = :tox
end
```

View file

@ -29,3 +29,9 @@ task :changelog, [:token] do |_t, args|
sh cmd
end
require 'rake/extensiontask'
Rake::ExtensionTask.new 'tox' do |ext|
ext.lib_dir = 'lib/tox'
end

View file

@ -1,14 +1,14 @@
#!/usr/bin/env ruby
require 'bundler/setup'
require 'lita/tox'
require 'lita-tox'
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require 'pry'
Pry.start
require 'irb'
IRB.start
# require 'irb'
# IRB.start

31
ext/tox/extconf.rb Executable file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env ruby
require 'mkmf'
NAME = 'tox'
LIBTOXCORE = 'toxcore'
have_header 'time.h' and
have_header 'tox/tox.h' and
have_func 'sprintf' and
have_func 'sscanf' and
have_func 'nanosleep' and
have_library LIBTOXCORE, 'tox_options_default' and
have_library LIBTOXCORE, 'tox_new' and
have_library LIBTOXCORE, 'tox_get_savedata_size' and
have_library LIBTOXCORE, 'tox_get_savedata' and
have_library LIBTOXCORE, 'tox_self_get_address' and
have_library LIBTOXCORE, 'tox_bootstrap' and
have_library LIBTOXCORE, 'tox_kill' and
have_library LIBTOXCORE, 'tox_version_is_compatible' and
have_library LIBTOXCORE, 'tox_iteration_interval' and
have_library LIBTOXCORE, 'tox_iterate' and
have_library LIBTOXCORE, 'tox_friend_add_norequest' and
have_library LIBTOXCORE, 'tox_friend_send_message' and
have_library LIBTOXCORE, 'tox_callback_friend_request' and
have_library LIBTOXCORE, 'tox_callback_friend_message' and
create_makefile "#{NAME}/#{NAME}" or exit(1)

372
ext/tox/tox.c Normal file
View file

@ -0,0 +1,372 @@
#include <ruby.h>
#include <time.h>
#include <tox/tox.h>
#define TOX_IS_COMPATIBLE TOX_VERSION_IS_API_COMPATIBLE
TOX_VERSION_REQUIRE(0, 0, 0);
void Init_tox();
#define IP_LENGTH_MAX 15
typedef char IP[IP_LENGTH_MAX + 1];
typedef char Key[(TOX_PUBLIC_KEY_SIZE * 2) + 1];
typedef struct Node {
IP ip;
int port;
Key key;
} Node;
typedef uint8_t KeyBin[TOX_PUBLIC_KEY_SIZE];
static void Key_to_KeyBin(const char *key, uint8_t *key_bin);
typedef struct cTox_ {
Tox *tox;
} cTox_;
static VALUE cTox;
static VALUE cTox_alloc(VALUE klass);
static void cTox_free(void *ptr);
static VALUE cTox_initialize_with(VALUE self, VALUE options);
static VALUE cTox_savedata(VALUE self);
static VALUE cTox_id(VALUE self);
static VALUE cTox_bootstrap(VALUE self, VALUE options);
static VALUE cTox_kill(VALUE self);
static VALUE cTox_loop(VALUE self);
static VALUE cTox_friend_add_norequest(VALUE self, VALUE key);
static VALUE cTox_friend_send_message(VALUE self, VALUE friend_number, VALUE text);
static void on_friend_request(
Tox *tox,
const uint8_t *key,
const uint8_t *data,
size_t length,
VALUE self);
static void on_friend_message(
Tox *tox,
uint32_t friend_number,
TOX_MESSAGE_TYPE type,
const uint8_t *text,
size_t length,
VALUE self);
typedef struct Tox_Options cTox_cOptions_;
static VALUE cTox_cOptions;
static VALUE cTox_cOptions_alloc(VALUE klass);
static void cTox_cOptions_free(void *ptr);
static VALUE cTox_cOptions_initialize(VALUE self);
static VALUE cTox_cOptions_data_EQUALS(VALUE self, VALUE data);
void Init_tox()
{
if (!TOX_VERSION_IS_ABI_COMPATIBLE())
rb_raise(rb_eLoadError, "incompatible Tox ABI version");
cTox = rb_define_class("Tox", rb_cObject);
rb_define_alloc_func(cTox, cTox_alloc);
rb_define_method(cTox, "initialize_with", cTox_initialize_with, 1);
rb_define_method(cTox, "savedata", cTox_savedata, 0);
rb_define_method(cTox, "id", cTox_id, 0);
rb_define_method(cTox, "bootstrap", cTox_bootstrap, 1);
rb_define_method(cTox, "kill", cTox_kill, 0);
rb_define_method(cTox, "loop", cTox_loop, 0);
rb_define_method(cTox, "friend_add_norequest", cTox_friend_add_norequest, 1);
rb_define_method(cTox, "friend_send_message", cTox_friend_send_message, 2);
cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject);
rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc);
rb_define_method(cTox_cOptions, "initialize", cTox_cOptions_initialize, 0);
rb_define_method(cTox_cOptions, "data=", cTox_cOptions_data_EQUALS, 1);
}
/******************************************************************************
* KeyBin
******************************************************************************/
void Key_to_KeyBin(const char *const key, uint8_t *const key_bin)
{
long i;
for (i = 0; i < TOX_PUBLIC_KEY_SIZE; ++i)
sscanf(&key[i * 2], "%2hhx", &key_bin[i]);
}
/******************************************************************************
* Tox
******************************************************************************/
VALUE cTox_alloc(const VALUE klass)
{
cTox_ *tox;
tox = ALLOC(cTox_);
return Data_Wrap_Struct(klass, NULL, cTox_free, tox);
}
void cTox_free(void *const ptr)
{
free(ptr);
}
VALUE cTox_initialize_with(const VALUE self, const VALUE options)
{
cTox_ *tox;
cTox_cOptions_ *tox_options;
TOX_ERR_NEW error;
// check if `options` is instance of `Tox::Options`
if (Qfalse == rb_funcall(options, rb_intern("is_a?"), 1, cTox_cOptions))
rb_raise(rb_eTypeError, "argument 1 should be Tox::Options");
Data_Get_Struct(self, cTox_, tox);
Data_Get_Struct(options, cTox_cOptions_, tox_options);
tox->tox = tox_new(tox_options, &error);
if (error != TOX_ERR_NEW_OK)
rb_raise(rb_eRuntimeError, "tox_new() failed");
tox_callback_friend_request(tox->tox, (tox_friend_request_cb*)on_friend_request, (void*)self);
tox_callback_friend_message(tox->tox, (tox_friend_message_cb*)on_friend_message, (void*)self);
return self;
}
VALUE cTox_savedata(const VALUE self)
{
cTox_ *tox;
size_t data_size;
char *data;
Data_Get_Struct(self, cTox_, tox);
data_size = tox_get_savedata_size(tox->tox);
data = ALLOC_N(char, data_size);
tox_get_savedata(tox->tox, (uint8_t*)data);
return rb_str_new(data, data_size);
}
VALUE cTox_id(const VALUE self)
{
cTox_ *tox;
char address[TOX_ADDRESS_SIZE];
char id[2 * TOX_ADDRESS_SIZE];
unsigned long i;
Data_Get_Struct(self, cTox_, tox);
tox_self_get_address(tox->tox, (uint8_t*)address);
for (i = 0; i < TOX_ADDRESS_SIZE; ++i)
sprintf(&id[2 * i], "%02X", address[i] & 0xFF);
return rb_str_new(id, 2 * TOX_ADDRESS_SIZE);
}
VALUE cTox_bootstrap(const VALUE self, const VALUE options)
{
cTox_ *tox;
VALUE ip;
VALUE port;
VALUE key;
Node node;
KeyBin key_bin;
TOX_ERR_BOOTSTRAP error;
Check_Type(options, T_HASH);
ip = rb_hash_aref(options, ID2SYM(rb_intern("ip")));
port = rb_hash_aref(options, ID2SYM(rb_intern("port")));
key = rb_hash_aref(options, ID2SYM(rb_intern("key")));
if (ip == Qnil) rb_raise(rb_eRuntimeError, "option \"ip\" is required");
if (port == Qnil) rb_raise(rb_eRuntimeError, "option \"port\" is required");
if (key == Qnil) rb_raise(rb_eRuntimeError, "option \"key\" is required");
Data_Get_Struct(self, cTox_, tox);
memcpy(node.ip, RSTRING_PTR(ip), RSTRING_LEN(ip) + 1);
node.port = NUM2INT(port);
memcpy(node.key, RSTRING_PTR(key), RSTRING_LEN(key) + 1);
Key_to_KeyBin(node.key, key_bin);
tox_bootstrap(tox->tox, node.ip, node.port, key_bin, &error);
if (error == TOX_ERR_BOOTSTRAP_OK)
return Qtrue;
else
return Qfalse;
}
VALUE cTox_kill(const VALUE self)
{
cTox_ *tox;
Data_Get_Struct(self, cTox_, tox);
tox_kill(tox->tox);
return self;
}
VALUE cTox_loop(const VALUE self)
{
cTox_ *tox;
struct timespec delay;
Data_Get_Struct(self, cTox_, tox);
rb_funcall(self, rb_intern("running="), 1, Qtrue);
delay.tv_sec = 0;
while (rb_funcall(self, rb_intern("running"), 0)) {
delay.tv_nsec = tox_iteration_interval(tox->tox) * 1000000;
nanosleep(&delay, NULL);
tox_iterate(tox->tox);
}
return self;
}
VALUE cTox_friend_add_norequest(const VALUE self, const VALUE key)
{
cTox_ *tox;
Check_Type(key, T_STRING);
Data_Get_Struct(self, cTox_, tox);
return LONG2FIX(tox_friend_add_norequest(tox->tox, (uint8_t*)RSTRING_PTR(key), NULL));
}
VALUE cTox_friend_send_message(const VALUE self, const VALUE friend_number, const VALUE text)
{
cTox_ *tox;
// Don't know yet how to check for integers
// Check_Type(friend_number, T_INTEGER);
Check_Type(text, T_STRING);
Data_Get_Struct(self, cTox_, tox);
return LONG2FIX(tox_friend_send_message(
tox->tox,
NUM2LONG(friend_number),
TOX_MESSAGE_TYPE_NORMAL,
(uint8_t*)RSTRING_PTR(text),
RSTRING_LEN(text),
NULL
));
}
void on_friend_request(
Tox *const tox,
const uint8_t *const key,
const uint8_t *const data,
const size_t length,
const VALUE self)
{
VALUE rb_on_friend_request;
rb_on_friend_request = rb_iv_get(self, "@on_friend_request");
if (Qnil != rb_on_friend_request)
rb_funcall(
rb_on_friend_request,
rb_intern("call"),
2,
rb_str_new((char*)key, TOX_PUBLIC_KEY_SIZE),
rb_str_new((char*)data, length)
);
}
void on_friend_message(
Tox *const tox,
const uint32_t friend_number,
const TOX_MESSAGE_TYPE type,
const uint8_t *const text,
const size_t length,
const VALUE self)
{
VALUE rb_on_friend_message;
if (type != TOX_MESSAGE_TYPE_NORMAL)
return;
rb_on_friend_message = rb_iv_get(self, "@on_friend_message");
if (Qnil != rb_on_friend_message)
rb_funcall(
rb_on_friend_message,
rb_intern("call"),
2,
LONG2FIX(friend_number),
rb_str_new((char*)text, length)
);
}
/******************************************************************************
* Tox::Options
******************************************************************************/
VALUE cTox_cOptions_alloc(const VALUE klass)
{
cTox_cOptions_ *tox_options;
tox_options = ALLOC(cTox_cOptions_);
return Data_Wrap_Struct(klass, NULL, cTox_cOptions_free, tox_options);
}
void cTox_cOptions_free(void *const ptr)
{
free(ptr);
}
VALUE cTox_cOptions_initialize(const VALUE self)
{
cTox_cOptions_ *tox_options;
Data_Get_Struct(self, cTox_cOptions_, tox_options);
tox_options_default(tox_options);
return self;
}
VALUE cTox_cOptions_data_EQUALS(const VALUE self, const VALUE savedata)
{
cTox_cOptions_ *tox_options;
Check_Type(savedata, T_STRING);
Data_Get_Struct(self, cTox_cOptions_, tox_options);
tox_options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
tox_options->savedata_data = (uint8_t*)RSTRING_PTR(savedata);
tox_options->savedata_length = RSTRING_LEN(savedata);
return savedata;
}

View file

@ -1,3 +1,5 @@
require 'tox'
##
# Lita module.
#
@ -10,6 +12,34 @@ module Lita
# Tox adapter for the Lita chat bot.
#
class Tox < Adapter
def initialize(robot)
@tox = ::Tox.new
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
end
def run
@tox.loop
end
def send_messages(target, messages)
messages.reject(&:empty?).each do |message|
@tox.friend_send_message(target.user.id.to_i, message)
end
end
end
Lita.register_adapter(:tox, Tox)

53
lib/tox.rb Normal file
View file

@ -0,0 +1,53 @@
require 'tox/tox'
class Tox
NODES = [
%w(192.254.75.102 33445 951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F),
%w(144.76.60.215 33445 04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F),
%w(23.226.230.47 33445 A09162D68618E742FFBCA1C2C70385E6679604B2D80EA6E84AD0996A1AC8A074),
%w(178.62.125.224 33445 10B20C49ACBD968D7C80F2E8438F92EA51F189F4E70CFBBB2C2C8C799E97F03E),
%w(178.21.112.187 33445 4B2C19E924972CB9B57732FB172F8A8604DE13EEDA2A6234E348983344B23057),
%w(195.154.119.113 33445 E398A69646B8CEACA9F0B84F553726C1C49270558C57DF5F3C368F05A7D71354),
%w(192.210.149.121 33445 F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67),
%w(76.191.23.96 33445 93574A3FAB7D612FEA29FD8D67D3DD10DFD07A075A5D62E8AF3DD9F5D0932E11),
%w(46.38.239.179 33445 F5A1A38EFB6BD3C2C8AF8B10D85F0F89E931704D349F1D0720C3C4059AF2440A),
%w(178.62.250.138 33445 788236D34978D1D5BD822F0A5BEBD2C53C64CC31CD3149350EE27D4D9A2F9B6B),
%w(78.225.128.39 33445 7A2306BFBA665E5480AE59B31E116BE9C04DCEFE04D9FE25082316FA34B4DA0C),
%w(130.133.110.14 33445 461FA3776EF0FA655F1A05477DF1B3B614F7D6B124F7DB1DD4FE3C08B03B640F),
%w(104.167.101.29 33445 5918AC3C06955962A75AD7DF4F80A5D7C34F7DB9E1498D2E0495DE35B3FE8A57),
%w(195.154.109.148 33445 391C96CB67AE893D4782B8E4495EB9D89CF1031F48460C06075AA8CE76D50A21),
%w(192.3.173.88 33445 3E1FFDEB667BFF549F619EC6737834762124F50A89C8D0DBF1DDF64A2DD6CD1B),
%w(205.185.116.116 33445 A179B09749AC826FF01F37A9613F6B57118AE014D4196A0E1105A98F93A54702),
%w(198.98.51.198 33445 1D5A5F2F5D6233058BF0259B09622FB40B482E4FA0931EB8FD3AB8E7BF7DAF6F),
%w(80.232.246.79 33445 A7A060D553B017D9D8F038E265C7AFB6C70BAAC55070197F9C007432D0038E0F),
%w(108.61.165.198 33445 8E7D0B859922EF569298B4D261A8CCB5FEA14FB91ED412A7603A585A25698832),
%w(212.71.252.109 33445 C4CEB8C7AC607C6B374E2E782B3C00EA3A63B80D4910B8649CCACDD19F260819),
%w(194.249.212.109 33445 3CEE1F054081E7A011234883BC4FC39F661A55B73637A5AC293DDF1251D9432B),
%w(194.249.212.109 443 3CEE1F054081E7A011234883BC4FC39F661A55B73637A5AC293DDF1251D9432B),
%w(103.38.216.87 33445 601AEE6FC8C17E8CD8F8F1FFC4D4AD84E59A73BE451F037194E7A404E3795320),
%w(185.25.116.107 33445 DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43),
%w(192.99.168.140 33445 6A4D0607A296838434A6A7DDF99F50EF9D60A2C510BBF31FE538A25CB6B4652F),
]
attr_accessor :running
def initialize(options = Tox::Options.new)
initialize_with(options)
NODES.each do |node|
bootstrap({
ip: node[0],
port: node[1].to_i,
key: node[2],
})
end
end
def on_friend_request(&block)
@on_friend_request = block
end
def on_friend_message(&block)
@on_friend_message = block
end
end

View file

@ -2,7 +2,7 @@
Gem::Specification.new do |spec|
spec.name = 'lita-tox'
spec.version = '0.0.0'
spec.version = '0.1.0'
spec.authors = ['Braiden Vasco']
spec.email = ['braiden-vasco@mailtor.net']
@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.extensions << 'ext/tox/extconf.rb'
spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rubocop', '~> 0.31'
@ -29,6 +31,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'yard', '~> 0.8'
spec.add_development_dependency 'redcarpet', '~> 3.3'
spec.add_development_dependency 'github_changelog_generator', '~> 1.6'
spec.add_development_dependency 'rake-compiler', '~> 0.9'
spec.add_development_dependency 'pry', '~> 0.10'
spec.add_runtime_dependency 'lita', '~> 4.4'
end