lita-tox 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91236f6acf23d6db25acc452a0de0e1426abf5c4
4
- data.tar.gz: 07e2890f818d7865861a1044be77a3fa1fdecca7
3
+ metadata.gz: 7594dc6e702a18b828fe46e1b8b64d9ad508690b
4
+ data.tar.gz: 53a3a8abf586220ae055867fbbb7608fe432072b
5
5
  SHA512:
6
- metadata.gz: 5fef9b677b83c02c1b65d0fa3a8d29c97734c18968c886e0ca9c9bc6f5c043a21863a5547b97d46dbd6857520ab8e19e12e36b1489d92765a9ea33cf8cacba35
7
- data.tar.gz: a31101312fce11274b71815770028ad35c8d4d1ec0ba632930b5d7837e81e1f09134bb9ac75b613bf126d0350454635a28f8535392d4cc1dfbf9fd09b3f6ac42
6
+ metadata.gz: 89053fb6c2db27721f1e51d8a5cccea01f5cdc2604b89ad499ea00b6b733bd16690aee497f9ab24d98994656baeec7a1ffd2b963cfff5333ebd960c7425c82d9
7
+ data.tar.gz: 45f399f7696d54b47c19815da80716cdc9140ce4b4c030f16ff412d821c31fbf1799d979d2267d9626cf45a04208cf0ab13446a55d059768e6f6579ee32d44af
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## [v0.2.0](https://github.com/braiden-vasco/lita-tox/tree/v0.2.0) (2015-09-14)
4
+ [Full Changelog](https://github.com/braiden-vasco/lita-tox/compare/v0.1.0...v0.2.0)
5
+
6
+ **Merged pull requests:**
7
+
8
+ - Implement group chats [\#20](https://github.com/braiden-vasco/lita-tox/pull/20) ([braiden-vasco](https://github.com/braiden-vasco))
9
+ - Save Tox state [\#19](https://github.com/braiden-vasco/lita-tox/pull/19) ([braiden-vasco](https://github.com/braiden-vasco))
10
+
3
11
  ## [v0.1.0](https://github.com/braiden-vasco/lita-tox/tree/v0.1.0) (2015-09-13)
4
12
  **Merged pull requests:**
5
13
 
data/README.md CHANGED
@@ -18,18 +18,16 @@ TODO
18
18
  Current development version have some limitations
19
19
  which should be fixed in first release:
20
20
 
21
- * Adapter doesn't save Tox state, so you have to send friendship
22
- request again after each run. Current Tox ID is shown at start
23
- as Lita's information message
21
+ * [\[issue #16\]](https://github.com/braiden-vasco/lita-tox/issues/16)
22
+ **libtoxcore** is not included in the gem. It should be compiled manually
23
+ to build the gem native extension successfully. Follow the instructions in
24
+ [that file](https://github.com/irungentoo/toxcore/blob/2ab3b14731061cc04d3ccc50a35093c11d018298/INSTALL.md)
24
25
 
25
- * Only private chats are supported. Adapter will not respond to group invite
26
+ * [\[issue #17\]](https://github.com/braiden-vasco/lita-tox/issues/17)
27
+ JRuby is not supported. Only C extension for Tox is implemented
26
28
 
27
- * **libtoxcore** is not included in the gem. It should be compiled manually
28
- to build the gem native extension successfully (see the instructions below)
29
-
30
- * JRuby is not supported. Only C extension for Tox is implemented
31
-
32
- * Message length is limited to value of `TOX_MAX_MESSAGE_LENGTH`
29
+ * [\[issue #18\]](https://github.com/braiden-vasco/lita-tox/issues/18)
30
+ Message length is limited to value of `TOX_MAX_MESSAGE_LENGTH`
33
31
  (see [source code of **libtoxcore**](https://github.com/irungentoo/toxcore/blob/2ab3b14731061cc04d3ccc50a35093c11d018298/toxcore/tox.h#L252-L255))
34
32
 
35
33
  * Adapter has incomplete API for Lita. Only basic methods are implemented
@@ -43,19 +41,21 @@ At first, see the documentation for Lita: http://docs.lita.io/
43
41
  ### Installation
44
42
 
45
43
  **libtoxcore** should be compiled manually at your computer or server.
46
- Follow the instructions at
44
+ Follow the instructions in
47
45
  [that file](https://github.com/irungentoo/toxcore/blob/2ab3b14731061cc04d3ccc50a35093c11d018298/INSTALL.md)
48
46
 
49
47
  When **libtoxcore** is installed, add **lita-tox**
50
48
  to your Lita instance's Gemfile:
51
49
 
52
50
  ```ruby
53
- gem 'lita-tox', '~> 0.1.0'
51
+ gem 'lita-tox', '~> 0.2.0'
54
52
  ```
55
53
 
56
54
  ### Configuration
57
55
 
58
- No configuration is needed. You only have to specify adapter as `:tox`
56
+ #### Optional attributes
57
+
58
+ - `savedata_filename` (String) - Path to file where Tox state will be stored (if provided)
59
59
 
60
60
  #### Example
61
61
 
@@ -67,5 +67,7 @@ Lita.configure do |config|
67
67
  config.robot.mention_name = 'lita'
68
68
 
69
69
  config.robot.adapter = :tox
70
+
71
+ config.savedata_filename = 'savedata'
70
72
  end
71
73
  ```
@@ -8,6 +8,7 @@ LIBTOXCORE = 'toxcore'
8
8
 
9
9
  have_header 'time.h' and
10
10
  have_header 'tox/tox.h' and
11
+ have_header 'tox/tox_old.h' and
11
12
 
12
13
  have_func 'sprintf' and
13
14
  have_func 'sscanf' and
@@ -27,5 +28,10 @@ have_library LIBTOXCORE, 'tox_friend_add_norequest' and
27
28
  have_library LIBTOXCORE, 'tox_friend_send_message' and
28
29
  have_library LIBTOXCORE, 'tox_callback_friend_request' and
29
30
  have_library LIBTOXCORE, 'tox_callback_friend_message' and
31
+ have_library LIBTOXCORE, 'tox_join_groupchat' and
32
+ have_library LIBTOXCORE, 'tox_group_message_send' and
33
+ have_library LIBTOXCORE, 'tox_callback_group_invite' and
34
+ have_library LIBTOXCORE, 'tox_callback_group_message' and
35
+ have_library LIBTOXCORE, 'tox_group_peernumber_is_ours' and
30
36
 
31
37
  create_makefile "#{NAME}/#{NAME}" or exit(1)
@@ -40,6 +40,9 @@ static VALUE cTox_kill(VALUE self);
40
40
  static VALUE cTox_loop(VALUE self);
41
41
  static VALUE cTox_friend_add_norequest(VALUE self, VALUE key);
42
42
  static VALUE cTox_friend_send_message(VALUE self, VALUE friend_number, VALUE text);
43
+ static VALUE cTox_join_groupchat(VALUE self, VALUE friend_number, VALUE data);
44
+ static VALUE cTox_group_message_send(VALUE self, VALUE group_number, VALUE text);
45
+ static VALUE cTox_group_peernumber_is_ours(VALUE self, VALUE group_number, VALUE peer_number);
43
46
 
44
47
  static void on_friend_request(
45
48
  Tox *tox,
@@ -56,6 +59,24 @@ static void on_friend_message(
56
59
  size_t length,
57
60
  VALUE self);
58
61
 
62
+ static void on_group_invite(
63
+ Tox *tox,
64
+ int32_t friend_number,
65
+ uint8_t type,
66
+ const uint8_t *data,
67
+ uint16_t length,
68
+ VALUE self
69
+ );
70
+
71
+ static void on_group_message(
72
+ Tox *tox,
73
+ int group_number,
74
+ int peer_number,
75
+ const uint8_t *text,
76
+ uint16_t length,
77
+ VALUE self
78
+ );
79
+
59
80
  typedef struct Tox_Options cTox_cOptions_;
60
81
 
61
82
  static VALUE cTox_cOptions;
@@ -80,6 +101,9 @@ void Init_tox()
80
101
  rb_define_method(cTox, "loop", cTox_loop, 0);
81
102
  rb_define_method(cTox, "friend_add_norequest", cTox_friend_add_norequest, 1);
82
103
  rb_define_method(cTox, "friend_send_message", cTox_friend_send_message, 2);
104
+ rb_define_method(cTox, "join_groupchat", cTox_join_groupchat, 2);
105
+ rb_define_method(cTox, "group_message_send", cTox_group_message_send, 2);
106
+ rb_define_method(cTox, "group_peernumber_is_ours", cTox_group_peernumber_is_ours, 2);
83
107
 
84
108
  cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject);
85
109
  rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc);
@@ -138,6 +162,14 @@ VALUE cTox_initialize_with(const VALUE self, const VALUE options)
138
162
 
139
163
  tox_callback_friend_request(tox->tox, (tox_friend_request_cb*)on_friend_request, (void*)self);
140
164
  tox_callback_friend_message(tox->tox, (tox_friend_message_cb*)on_friend_message, (void*)self);
165
+ tox_callback_group_invite(
166
+ tox->tox,
167
+ (void (*)(struct Tox *, int32_t, uint8_t, const uint8_t *, uint16_t, void *))on_group_invite,
168
+ (void*)self);
169
+ tox_callback_group_message(
170
+ tox->tox,
171
+ (void (*)(struct Tox *, int, int, const uint8_t *, uint16_t, void *))on_group_message,
172
+ (void*)self);
141
173
 
142
174
  return self;
143
175
  }
@@ -281,6 +313,65 @@ VALUE cTox_friend_send_message(const VALUE self, const VALUE friend_number, cons
281
313
  ));
282
314
  }
283
315
 
316
+ VALUE cTox_join_groupchat(const VALUE self, const VALUE friend_number, const VALUE data)
317
+ {
318
+ cTox_ *tox;
319
+
320
+ // Don't know yet how to check for integers
321
+ // Check_Type(friend_number, T_INTEGER);
322
+ Check_Type(data, T_STRING);
323
+
324
+ Data_Get_Struct(self, cTox_, tox);
325
+
326
+ return INT2FIX(tox_join_groupchat(
327
+ tox->tox,
328
+ NUM2LONG(friend_number),
329
+ (uint8_t*)RSTRING_PTR(data),
330
+ RSTRING_LEN(data)
331
+ ));
332
+ }
333
+
334
+ VALUE cTox_group_message_send(const VALUE self, const VALUE group_number, const VALUE text)
335
+ {
336
+ cTox_ *tox;
337
+
338
+ // Don't know yet how to check for integers
339
+ // Check_Type(group_number, T_INTEGER);
340
+ Check_Type(text, T_STRING);
341
+
342
+ Data_Get_Struct(self, cTox_, tox);
343
+
344
+ return INT2FIX(tox_group_message_send(
345
+ tox->tox,
346
+ NUM2LONG(group_number),
347
+ (uint8_t*)RSTRING_PTR(text),
348
+ RSTRING_LEN(text)
349
+ ));
350
+ }
351
+
352
+ VALUE cTox_group_peernumber_is_ours(
353
+ const VALUE self,
354
+ const VALUE group_number,
355
+ const VALUE peer_number)
356
+ {
357
+ cTox_ *tox;
358
+
359
+ // Don't know yet how to check for integers
360
+ // Check_Type(group_number, T_INTEGER);
361
+ // Check_Type(peer_number, T_INTEGER);
362
+
363
+ Data_Get_Struct(self, cTox_, tox);
364
+
365
+ if (tox_group_peernumber_is_ours(
366
+ tox->tox,
367
+ NUM2INT(group_number),
368
+ NUM2INT(peer_number))
369
+ )
370
+ return Qtrue;
371
+ else
372
+ return Qfalse;
373
+ }
374
+
284
375
  void on_friend_request(
285
376
  Tox *const tox,
286
377
  const uint8_t *const key,
@@ -327,6 +418,51 @@ void on_friend_message(
327
418
  );
328
419
  }
329
420
 
421
+ static void on_group_invite(
422
+ Tox *const tox,
423
+ const int32_t friend_number,
424
+ const uint8_t type,
425
+ const uint8_t *const data,
426
+ const uint16_t length,
427
+ const VALUE self)
428
+ {
429
+ VALUE rb_on_group_invite;
430
+
431
+ rb_on_group_invite = rb_iv_get(self, "@on_group_invite");
432
+
433
+ if (Qnil != rb_on_group_invite)
434
+ rb_funcall(
435
+ rb_on_group_invite,
436
+ rb_intern("call"),
437
+ 2,
438
+ LONG2FIX(friend_number),
439
+ rb_str_new((char*)data, length)
440
+ );
441
+ }
442
+
443
+ static void on_group_message(
444
+ Tox *const tox,
445
+ const int group_number,
446
+ const int peer_number,
447
+ const uint8_t *const text,
448
+ const uint16_t length,
449
+ const VALUE self)
450
+ {
451
+ VALUE rb_on_group_message;
452
+
453
+ rb_on_group_message = rb_iv_get(self, "@on_group_message");
454
+
455
+ if (Qnil != rb_on_group_message)
456
+ rb_funcall(
457
+ rb_on_group_message,
458
+ rb_intern("call"),
459
+ 3,
460
+ LONG2FIX(group_number),
461
+ LONG2FIX(peer_number),
462
+ rb_str_new((char*)text, length)
463
+ );
464
+ }
465
+
330
466
  /******************************************************************************
331
467
  * Tox::Options
332
468
  ******************************************************************************/
@@ -12,8 +12,20 @@ module Lita
12
12
  # Tox adapter for the Lita chat bot.
13
13
  #
14
14
  class Tox < Adapter
15
+ config :savedata_filename, type: String
16
+
15
17
  def initialize(robot)
16
- @tox = ::Tox.new
18
+ super
19
+
20
+ options = ::Tox::Options.new
21
+
22
+ if config.savedata_filename && File.exist?(config.savedata_filename)
23
+ savedata_file = open(config.savedata_filename)
24
+ options.data = savedata_file.read
25
+ savedata_file.close
26
+ end
27
+
28
+ @tox = ::Tox.new(options)
17
29
 
18
30
  log.info("ID: #{@tox.id}")
19
31
 
@@ -29,15 +41,43 @@ module Lita
29
41
  message.command!
30
42
  robot.receive(message)
31
43
  end
44
+
45
+ @tox.on_group_invite do |friend_number, data|
46
+ @tox.join_groupchat(friend_number, data)
47
+ end
48
+
49
+ @tox.on_group_message do |group_number, peer_number, text|
50
+ unless @tox.group_peernumber_is_ours(group_number, peer_number)
51
+ user = User.new(-1 - peer_number) # TODO
52
+ source = Source.new(user: user, room: group_number)
53
+ message = Message.new(robot, text, source)
54
+
55
+ robot.receive(message)
56
+ end
57
+ end
32
58
  end
33
59
 
34
60
  def run
35
61
  @tox.loop
36
62
  end
37
63
 
64
+ def shut_down
65
+ if config.savedata_filename
66
+ savedata_file = open(config.savedata_filename, 'w')
67
+ savedata_file.write(@tox.savedata)
68
+ savedata_file.close
69
+ end
70
+
71
+ @tox.kill
72
+ end
73
+
38
74
  def send_messages(target, messages)
39
75
  messages.reject(&:empty?).each do |message|
40
- @tox.friend_send_message(target.user.id.to_i, message)
76
+ if target.user.id.to_i >= 0
77
+ @tox.friend_send_message(target.user.id.to_i, message)
78
+ else
79
+ @tox.group_message_send(target.room.to_i, message)
80
+ end
41
81
  end
42
82
  end
43
83
  end
data/lib/tox.rb CHANGED
@@ -50,4 +50,12 @@ class Tox
50
50
  def on_friend_message(&block)
51
51
  @on_friend_message = block
52
52
  end
53
+
54
+ def on_group_invite(&block)
55
+ @on_group_invite = block
56
+ end
57
+
58
+ def on_group_message(&block)
59
+ @on_group_message = block
60
+ end
53
61
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'lita-tox'
5
- spec.version = '0.1.0'
5
+ spec.version = '0.2.0'
6
6
  spec.authors = ['Braiden Vasco']
7
7
  spec.email = ['braiden-vasco@mailtor.net']
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-tox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braiden Vasco
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-13 00:00:00.000000000 Z
11
+ date: 2015-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler