From 4242bcb144522857f7bfe4fbfa33e9e7b8ba0b84 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 00:34:37 +0000 Subject: [PATCH 1/8] Check environment for group API --- ext/tox/extconf.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/tox/extconf.rb b/ext/tox/extconf.rb index 6fb1c32..1a7d3b0 100755 --- a/ext/tox/extconf.rb +++ b/ext/tox/extconf.rb @@ -8,6 +8,7 @@ LIBTOXCORE = 'toxcore' have_header 'time.h' and have_header 'tox/tox.h' and +have_header 'tox/tox_old.h' and have_func 'sprintf' and have_func 'sscanf' and @@ -27,5 +28,9 @@ 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 +have_library LIBTOXCORE, 'tox_join_groupchat' and +have_library LIBTOXCORE, 'tox_group_message_send' and +have_library LIBTOXCORE, 'tox_callback_group_invite' and +have_library LIBTOXCORE, 'tox_callback_group_message' and create_makefile "#{NAME}/#{NAME}" or exit(1) From 99285a06a90a40c410437a134e72c6bf9b65ec2a Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 00:38:32 +0000 Subject: [PATCH 2/8] Add callback setters --- lib/tox.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/tox.rb b/lib/tox.rb index ccab134..f390ecf 100644 --- a/lib/tox.rb +++ b/lib/tox.rb @@ -50,4 +50,12 @@ class Tox def on_friend_message(&block) @on_friend_message = block end + + def on_group_invite(&block) + @on_group_invite = block + end + + def on_group_message(&block) + @on_group_message = block + end end From 44b4773c9d1c55b45a5eaaeabb21061971a29aa0 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 00:52:43 +0000 Subject: [PATCH 3/8] Implement Tox#join_groupchat, #group_send_message --- ext/tox/tox.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ext/tox/tox.c b/ext/tox/tox.c index d6c8343..b753538 100644 --- a/ext/tox/tox.c +++ b/ext/tox/tox.c @@ -40,6 +40,8 @@ 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 VALUE cTox_join_groupchat(VALUE self, VALUE friend_number, VALUE data); +static VALUE cTox_group_message_send(VALUE self, VALUE group_number, VALUE text); static void on_friend_request( Tox *tox, @@ -80,6 +82,8 @@ void Init_tox() 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); + rb_define_method(cTox, "join_groupchat", cTox_join_groupchat, 2); + rb_define_method(cTox, "group_message_send", cTox_group_message_send, 2); cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject); rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc); @@ -281,6 +285,42 @@ VALUE cTox_friend_send_message(const VALUE self, const VALUE friend_number, cons )); } +VALUE cTox_join_groupchat(const VALUE self, const VALUE friend_number, const VALUE data) +{ + cTox_ *tox; + + // Don't know yet how to check for integers + // Check_Type(friend_number, T_INTEGER); + Check_Type(data, T_STRING); + + Data_Get_Struct(self, cTox_, tox); + + return INT2FIX(tox_join_groupchat( + tox->tox, + NUM2LONG(friend_number), + (uint8_t*)RSTRING_PTR(data), + RSTRING_LEN(data) + )); +} + +VALUE cTox_group_message_send(const VALUE self, const VALUE group_number, const VALUE text) +{ + cTox_ *tox; + + // Don't know yet how to check for integers + // Check_Type(group_number, T_INTEGER); + Check_Type(text, T_STRING); + + Data_Get_Struct(self, cTox_, tox); + + return INT2FIX(tox_group_message_send( + tox->tox, + NUM2LONG(group_number), + (uint8_t*)RSTRING_PTR(text), + RSTRING_LEN(text) + )); +} + void on_friend_request( Tox *const tox, const uint8_t *const key, From 3acd610539c73697a31cf043dc50201265810708 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 01:11:28 +0000 Subject: [PATCH 4/8] Implement group callbacks --- ext/tox/tox.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/ext/tox/tox.c b/ext/tox/tox.c index b753538..eef7418 100644 --- a/ext/tox/tox.c +++ b/ext/tox/tox.c @@ -58,6 +58,24 @@ static void on_friend_message( size_t length, VALUE self); +static void on_group_invite( + Tox *tox, + int32_t friend_number, + uint8_t type, + const uint8_t *data, + uint16_t length, + VALUE self + ); + +static void on_group_message( + Tox *tox, + int group_number, + int peer_number, + const uint8_t *text, + uint16_t length, + VALUE self + ); + typedef struct Tox_Options cTox_cOptions_; static VALUE cTox_cOptions; @@ -142,6 +160,14 @@ VALUE cTox_initialize_with(const VALUE self, const VALUE options) 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); + tox_callback_group_invite( + tox->tox, + (void (*)(struct Tox *, int32_t, uint8_t, const uint8_t *, uint16_t, void *))on_group_invite, + (void*)self); + tox_callback_group_message( + tox->tox, + (void (*)(struct Tox *, int, int, const uint8_t *, uint16_t, void *))on_group_message, + (void*)self); return self; } @@ -367,6 +393,50 @@ void on_friend_message( ); } +static void on_group_invite( + Tox *const tox, + const int32_t friend_number, + const uint8_t type, + const uint8_t *const data, + const uint16_t length, + const VALUE self) +{ + VALUE rb_on_group_invite; + + rb_on_group_invite = rb_iv_get(self, "@on_group_invite"); + + if (Qnil != rb_on_group_invite) + rb_funcall( + rb_on_group_invite, + rb_intern("call"), + 2, + LONG2FIX(friend_number), + rb_str_new((char*)data, length) + ); +} + +static void on_group_message( + Tox *const tox, + const int group_number, + const int peer_number, + const uint8_t *const text, + const uint16_t length, + const VALUE self) +{ + VALUE rb_on_group_message; + + rb_on_group_message = rb_iv_get(self, "@on_group_message"); + + if (Qnil != rb_on_group_message) + rb_funcall( + rb_on_group_message, + rb_intern("call"), + 2, + LONG2FIX(group_number), + rb_str_new((char*)text, length) + ); +} + /****************************************************************************** * Tox::Options ******************************************************************************/ From c4449c4e0a2ac86d8bf09feaf7ce402008230977 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 07:42:48 +0000 Subject: [PATCH 5/8] Add argument "peer_number" for "on_group_message" to distinguish self messages --- ext/tox/tox.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/tox/tox.c b/ext/tox/tox.c index eef7418..235440c 100644 --- a/ext/tox/tox.c +++ b/ext/tox/tox.c @@ -431,8 +431,9 @@ static void on_group_message( rb_funcall( rb_on_group_message, rb_intern("call"), - 2, + 3, LONG2FIX(group_number), + LONG2FIX(peer_number), rb_str_new((char*)text, length) ); } From 55f2c562599693d40bca7a57945f4727ec904e26 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 07:57:43 +0000 Subject: [PATCH 6/8] Implement Tox#group_peernumber_is_ours --- ext/tox/extconf.rb | 1 + ext/tox/tox.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/ext/tox/extconf.rb b/ext/tox/extconf.rb index 1a7d3b0..46cb461 100755 --- a/ext/tox/extconf.rb +++ b/ext/tox/extconf.rb @@ -32,5 +32,6 @@ have_library LIBTOXCORE, 'tox_join_groupchat' and have_library LIBTOXCORE, 'tox_group_message_send' and have_library LIBTOXCORE, 'tox_callback_group_invite' and have_library LIBTOXCORE, 'tox_callback_group_message' and +have_library LIBTOXCORE, 'tox_group_peernumber_is_ours' and create_makefile "#{NAME}/#{NAME}" or exit(1) diff --git a/ext/tox/tox.c b/ext/tox/tox.c index 235440c..24e0dca 100644 --- a/ext/tox/tox.c +++ b/ext/tox/tox.c @@ -42,6 +42,7 @@ static VALUE cTox_friend_add_norequest(VALUE self, VALUE key); static VALUE cTox_friend_send_message(VALUE self, VALUE friend_number, VALUE text); static VALUE cTox_join_groupchat(VALUE self, VALUE friend_number, VALUE data); static VALUE cTox_group_message_send(VALUE self, VALUE group_number, VALUE text); +static VALUE cTox_group_peernumber_is_ours(VALUE self, VALUE group_number, VALUE peer_number); static void on_friend_request( Tox *tox, @@ -102,6 +103,7 @@ void Init_tox() rb_define_method(cTox, "friend_send_message", cTox_friend_send_message, 2); rb_define_method(cTox, "join_groupchat", cTox_join_groupchat, 2); rb_define_method(cTox, "group_message_send", cTox_group_message_send, 2); + rb_define_method(cTox, "group_peernumber_is_ours", cTox_group_peernumber_is_ours, 2); cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject); rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc); @@ -347,6 +349,29 @@ VALUE cTox_group_message_send(const VALUE self, const VALUE group_number, const )); } +VALUE cTox_group_peernumber_is_ours( + const VALUE self, + const VALUE group_number, + const VALUE peer_number) +{ + cTox_ *tox; + + // Don't know yet how to check for integers + // Check_Type(group_number, T_INTEGER); + // Check_Type(peer_number, T_INTEGER); + + Data_Get_Struct(self, cTox_, tox); + + if (tox_group_peernumber_is_ours( + tox->tox, + NUM2INT(group_number), + NUM2INT(peer_number)) + ) + return Qtrue; + else + return Qfalse; +} + void on_friend_request( Tox *const tox, const uint8_t *const key, From 67941b4cc8dfa18de3e2f00517e9079cbde622d3 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 08:10:18 +0000 Subject: [PATCH 7/8] Implement groupchats in the adapter for Lita --- lib/lita/adapters/tox.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/lita/adapters/tox.rb b/lib/lita/adapters/tox.rb index 1b3f998..deffaeb 100644 --- a/lib/lita/adapters/tox.rb +++ b/lib/lita/adapters/tox.rb @@ -41,6 +41,20 @@ module Lita message.command! robot.receive(message) end + + @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 end def run @@ -59,7 +73,11 @@ module Lita def send_messages(target, messages) messages.reject(&:empty?).each do |message| - @tox.friend_send_message(target.user.id.to_i, message) + 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 end end end From b2cb4f341f128bc556d543cd5ef64ee035f6aad3 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 14 Sep 2015 10:40:42 +0000 Subject: [PATCH 8/8] Remove TODO --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 26c0aac..384b76c 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,6 @@ TODO Current development version have some limitations which should be fixed in first release: -* [\[issue #15\]](https://github.com/braiden-vasco/lita-tox/issues/15) - Only private chats are supported. Adapter will not respond to group invite - * [\[issue #16\]](https://github.com/braiden-vasco/lita-tox/issues/16) **libtoxcore** is not included in the gem. It should be compiled manually to build the gem native extension successfully. Follow the instructions in