toxon
/
lita-tox
Archived
1
0
Fork 0

Merge pull request #20 from braiden-vasco/implement_group_chats

Implement group chats
This commit is contained in:
Braiden Vasco 2015-09-14 10:47:48 +00:00
commit c25501c2b3
5 changed files with 169 additions and 4 deletions

View File

@ -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

View File

@ -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,10 @@ 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
have_library LIBTOXCORE, 'tox_group_peernumber_is_ours' and
create_makefile "#{NAME}/#{NAME}" or exit(1)

View File

@ -40,6 +40,9 @@ 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 VALUE cTox_group_peernumber_is_ours(VALUE self, VALUE group_number, VALUE peer_number);
static void on_friend_request(
Tox *tox,
@ -56,6 +59,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;
@ -80,6 +101,9 @@ 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);
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);
@ -138,6 +162,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;
}
@ -281,6 +313,65 @@ 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)
));
}
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,
@ -327,6 +418,51 @@ 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"),
3,
LONG2FIX(group_number),
LONG2FIX(peer_number),
rb_str_new((char*)text, length)
);
}
/******************************************************************************
* Tox::Options
******************************************************************************/

View File

@ -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

View File

@ -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