lita-tox 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/tox/extconf.rb DELETED
@@ -1,43 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'mkmf'
4
-
5
- NAME = 'tox'
6
-
7
- LIBTOXCORE = 'toxcore'
8
-
9
- have_header 'time.h' and
10
- have_header 'tox/tox.h' and
11
- have_header 'tox/tox_old.h' and
12
-
13
- have_func 'sprintf' and
14
- have_func 'sscanf' and
15
- have_func 'nanosleep' and
16
-
17
- have_library LIBTOXCORE, 'tox_options_default' and
18
- have_library LIBTOXCORE, 'tox_new' and
19
- have_library LIBTOXCORE, 'tox_get_savedata_size' and
20
- have_library LIBTOXCORE, 'tox_get_savedata' and
21
- have_library LIBTOXCORE, 'tox_self_get_address' and
22
- have_library LIBTOXCORE, 'tox_bootstrap' and
23
- have_library LIBTOXCORE, 'tox_kill' and
24
- have_library LIBTOXCORE, 'tox_version_is_compatible' and
25
- have_library LIBTOXCORE, 'tox_iteration_interval' and
26
- have_library LIBTOXCORE, 'tox_iterate' and
27
- have_library LIBTOXCORE, 'tox_friend_add_norequest' and
28
- have_library LIBTOXCORE, 'tox_friend_send_message' and
29
- have_library LIBTOXCORE, 'tox_callback_friend_request' and
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
36
- have_library LIBTOXCORE, 'tox_self_get_name_size' and
37
- have_library LIBTOXCORE, 'tox_self_get_name' and
38
- have_library LIBTOXCORE, 'tox_self_set_name' and
39
- have_library LIBTOXCORE, 'tox_self_get_status_message_size' and
40
- have_library LIBTOXCORE, 'tox_self_get_status_message' and
41
- have_library LIBTOXCORE, 'tox_self_set_status_message' and
42
-
43
- create_makefile "#{NAME}/#{NAME}" or exit(1)
data/ext/tox/tox.c DELETED
@@ -1,588 +0,0 @@
1
- #include <ruby.h>
2
-
3
- #include <time.h>
4
-
5
- #include <tox/tox.h>
6
-
7
- #define TOX_IS_COMPATIBLE TOX_VERSION_IS_API_COMPATIBLE
8
- TOX_VERSION_REQUIRE(0, 0, 0);
9
-
10
- void Init_tox();
11
-
12
- #define IP_LENGTH_MAX 15
13
-
14
- typedef char IP[IP_LENGTH_MAX + 1];
15
- typedef char Key[(TOX_PUBLIC_KEY_SIZE * 2) + 1];
16
-
17
- typedef struct Node {
18
- IP ip;
19
- int port;
20
- Key key;
21
- } Node;
22
-
23
- typedef uint8_t KeyBin[TOX_PUBLIC_KEY_SIZE];
24
-
25
- static void Key_to_KeyBin(const char *key, uint8_t *key_bin);
26
-
27
- typedef struct cTox_ {
28
- Tox *tox;
29
- } cTox_;
30
-
31
- static VALUE cTox;
32
-
33
- static VALUE cTox_alloc(VALUE klass);
34
- static void cTox_free(void *ptr);
35
- static VALUE cTox_initialize_with(VALUE self, VALUE options);
36
- static VALUE cTox_savedata(VALUE self);
37
- static VALUE cTox_id(VALUE self);
38
- static VALUE cTox_bootstrap(VALUE self, VALUE options);
39
- static VALUE cTox_kill(VALUE self);
40
- static VALUE cTox_loop(VALUE self);
41
- static VALUE cTox_friend_add_norequest(VALUE self, VALUE key);
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);
46
- static VALUE cTox_name(VALUE self);
47
- static VALUE cTox_name_ASSIGN(VALUE self, VALUE name);
48
- static VALUE cTox_status_message(VALUE self);
49
- static VALUE cTox_status_message_ASSIGN(VALUE self, VALUE text);
50
-
51
- static void on_friend_request(
52
- Tox *tox,
53
- const uint8_t *key,
54
- const uint8_t *data,
55
- size_t length,
56
- VALUE self);
57
-
58
- static void on_friend_message(
59
- Tox *tox,
60
- uint32_t friend_number,
61
- TOX_MESSAGE_TYPE type,
62
- const uint8_t *text,
63
- size_t length,
64
- VALUE self);
65
-
66
- static void on_group_invite(
67
- Tox *tox,
68
- int32_t friend_number,
69
- uint8_t type,
70
- const uint8_t *data,
71
- uint16_t length,
72
- VALUE self
73
- );
74
-
75
- static void on_group_message(
76
- Tox *tox,
77
- int group_number,
78
- int peer_number,
79
- const uint8_t *text,
80
- uint16_t length,
81
- VALUE self
82
- );
83
-
84
- typedef struct Tox_Options cTox_cOptions_;
85
-
86
- static VALUE cTox_cOptions;
87
-
88
- static VALUE cTox_cOptions_alloc(VALUE klass);
89
- static void cTox_cOptions_free(void *ptr);
90
- static VALUE cTox_cOptions_initialize(VALUE self);
91
- static VALUE cTox_cOptions_data_EQUALS(VALUE self, VALUE data);
92
-
93
- void Init_tox()
94
- {
95
- if (!TOX_VERSION_IS_ABI_COMPATIBLE())
96
- rb_raise(rb_eLoadError, "incompatible Tox ABI version");
97
-
98
- cTox = rb_define_class("Tox", rb_cObject);
99
- rb_define_alloc_func(cTox, cTox_alloc);
100
- rb_define_method(cTox, "initialize_with", cTox_initialize_with, 1);
101
- rb_define_method(cTox, "savedata", cTox_savedata, 0);
102
- rb_define_method(cTox, "id", cTox_id, 0);
103
- rb_define_method(cTox, "bootstrap", cTox_bootstrap, 1);
104
- rb_define_method(cTox, "kill", cTox_kill, 0);
105
- rb_define_method(cTox, "loop", cTox_loop, 0);
106
- rb_define_method(cTox, "friend_add_norequest", cTox_friend_add_norequest, 1);
107
- rb_define_method(cTox, "friend_send_message", cTox_friend_send_message, 2);
108
- rb_define_method(cTox, "join_groupchat", cTox_join_groupchat, 2);
109
- rb_define_method(cTox, "group_message_send", cTox_group_message_send, 2);
110
- rb_define_method(cTox, "group_peernumber_is_ours", cTox_group_peernumber_is_ours, 2);
111
- rb_define_method(cTox, "name", cTox_name, 0);
112
- rb_define_method(cTox, "name=", cTox_name_ASSIGN, 1);
113
- rb_define_method(cTox, "status_message", cTox_status_message, 0);
114
- rb_define_method(cTox, "status_message=", cTox_status_message_ASSIGN, 1);
115
-
116
- cTox_cOptions = rb_define_class_under(cTox, "Options", rb_cObject);
117
- rb_define_alloc_func(cTox_cOptions, cTox_cOptions_alloc);
118
- rb_define_method(cTox_cOptions, "initialize", cTox_cOptions_initialize, 0);
119
- rb_define_method(cTox_cOptions, "data=", cTox_cOptions_data_EQUALS, 1);
120
- }
121
-
122
- /******************************************************************************
123
- * KeyBin
124
- ******************************************************************************/
125
-
126
- void Key_to_KeyBin(const char *const key, uint8_t *const key_bin)
127
- {
128
- long i;
129
-
130
- for (i = 0; i < TOX_PUBLIC_KEY_SIZE; ++i)
131
- sscanf(&key[i * 2], "%2hhx", &key_bin[i]);
132
- }
133
-
134
- /******************************************************************************
135
- * Tox
136
- ******************************************************************************/
137
-
138
- VALUE cTox_alloc(const VALUE klass)
139
- {
140
- cTox_ *tox;
141
-
142
- tox = ALLOC(cTox_);
143
-
144
- return Data_Wrap_Struct(klass, NULL, cTox_free, tox);
145
- }
146
-
147
- void cTox_free(void *const ptr)
148
- {
149
- free(ptr);
150
- }
151
-
152
- VALUE cTox_initialize_with(const VALUE self, const VALUE options)
153
- {
154
- cTox_ *tox;
155
- cTox_cOptions_ *tox_options;
156
-
157
- TOX_ERR_NEW error;
158
-
159
- // check if `options` is instance of `Tox::Options`
160
- if (Qfalse == rb_funcall(options, rb_intern("is_a?"), 1, cTox_cOptions))
161
- rb_raise(rb_eTypeError, "argument 1 should be Tox::Options");
162
-
163
- Data_Get_Struct(self, cTox_, tox);
164
- Data_Get_Struct(options, cTox_cOptions_, tox_options);
165
-
166
- tox->tox = tox_new(tox_options, &error);
167
-
168
- if (error != TOX_ERR_NEW_OK)
169
- rb_raise(rb_eRuntimeError, "tox_new() failed");
170
-
171
- tox_callback_friend_request(tox->tox, (tox_friend_request_cb*)on_friend_request, (void*)self);
172
- tox_callback_friend_message(tox->tox, (tox_friend_message_cb*)on_friend_message, (void*)self);
173
- tox_callback_group_invite(
174
- tox->tox,
175
- (void (*)(struct Tox *, int32_t, uint8_t, const uint8_t *, uint16_t, void *))on_group_invite,
176
- (void*)self);
177
- tox_callback_group_message(
178
- tox->tox,
179
- (void (*)(struct Tox *, int, int, const uint8_t *, uint16_t, void *))on_group_message,
180
- (void*)self);
181
-
182
- return self;
183
- }
184
-
185
- VALUE cTox_savedata(const VALUE self)
186
- {
187
- cTox_ *tox;
188
-
189
- size_t data_size;
190
- char *data;
191
-
192
- Data_Get_Struct(self, cTox_, tox);
193
-
194
- data_size = tox_get_savedata_size(tox->tox);
195
- data = ALLOC_N(char, data_size);
196
-
197
- tox_get_savedata(tox->tox, (uint8_t*)data);
198
-
199
- return rb_str_new(data, data_size);
200
- }
201
-
202
- VALUE cTox_id(const VALUE self)
203
- {
204
- cTox_ *tox;
205
-
206
- char address[TOX_ADDRESS_SIZE];
207
- char id[2 * TOX_ADDRESS_SIZE];
208
-
209
- unsigned long i;
210
-
211
- Data_Get_Struct(self, cTox_, tox);
212
-
213
- tox_self_get_address(tox->tox, (uint8_t*)address);
214
-
215
- for (i = 0; i < TOX_ADDRESS_SIZE; ++i)
216
- sprintf(&id[2 * i], "%02X", address[i] & 0xFF);
217
-
218
- return rb_str_new(id, 2 * TOX_ADDRESS_SIZE);
219
- }
220
-
221
- VALUE cTox_bootstrap(const VALUE self, const VALUE options)
222
- {
223
- cTox_ *tox;
224
-
225
- VALUE ip;
226
- VALUE port;
227
- VALUE key;
228
-
229
- Node node;
230
- KeyBin key_bin;
231
-
232
- TOX_ERR_BOOTSTRAP error;
233
-
234
- Check_Type(options, T_HASH);
235
-
236
- ip = rb_hash_aref(options, ID2SYM(rb_intern("ip")));
237
- port = rb_hash_aref(options, ID2SYM(rb_intern("port")));
238
- key = rb_hash_aref(options, ID2SYM(rb_intern("key")));
239
-
240
- if (ip == Qnil) rb_raise(rb_eRuntimeError, "option \"ip\" is required");
241
- if (port == Qnil) rb_raise(rb_eRuntimeError, "option \"port\" is required");
242
- if (key == Qnil) rb_raise(rb_eRuntimeError, "option \"key\" is required");
243
-
244
- Data_Get_Struct(self, cTox_, tox);
245
-
246
- memcpy(node.ip, RSTRING_PTR(ip), RSTRING_LEN(ip) + 1);
247
- node.port = NUM2INT(port);
248
- memcpy(node.key, RSTRING_PTR(key), RSTRING_LEN(key) + 1);
249
-
250
- Key_to_KeyBin(node.key, key_bin);
251
-
252
- tox_bootstrap(tox->tox, node.ip, node.port, key_bin, &error);
253
-
254
- if (error == TOX_ERR_BOOTSTRAP_OK)
255
- return Qtrue;
256
- else
257
- return Qfalse;
258
- }
259
-
260
- VALUE cTox_kill(const VALUE self)
261
- {
262
- cTox_ *tox;
263
-
264
- Data_Get_Struct(self, cTox_, tox);
265
-
266
- tox_kill(tox->tox);
267
-
268
- return self;
269
- }
270
-
271
- VALUE cTox_loop(const VALUE self)
272
- {
273
- cTox_ *tox;
274
-
275
- struct timespec delay;
276
-
277
- Data_Get_Struct(self, cTox_, tox);
278
-
279
- rb_funcall(self, rb_intern("running="), 1, Qtrue);
280
-
281
- delay.tv_sec = 0;
282
-
283
- while (rb_funcall(self, rb_intern("running"), 0)) {
284
- delay.tv_nsec = tox_iteration_interval(tox->tox) * 1000000;
285
- nanosleep(&delay, NULL);
286
-
287
- tox_iterate(tox->tox);
288
- }
289
-
290
- return self;
291
- }
292
-
293
- VALUE cTox_friend_add_norequest(const VALUE self, const VALUE key)
294
- {
295
- cTox_ *tox;
296
-
297
- Check_Type(key, T_STRING);
298
-
299
- Data_Get_Struct(self, cTox_, tox);
300
-
301
- return LONG2FIX(tox_friend_add_norequest(tox->tox, (uint8_t*)RSTRING_PTR(key), NULL));
302
- }
303
-
304
- VALUE cTox_friend_send_message(const VALUE self, const VALUE friend_number, const VALUE text)
305
- {
306
- cTox_ *tox;
307
-
308
- // Don't know yet how to check for integers
309
- // Check_Type(friend_number, T_INTEGER);
310
- Check_Type(text, T_STRING);
311
-
312
- Data_Get_Struct(self, cTox_, tox);
313
-
314
- return LONG2FIX(tox_friend_send_message(
315
- tox->tox,
316
- NUM2LONG(friend_number),
317
- TOX_MESSAGE_TYPE_NORMAL,
318
- (uint8_t*)RSTRING_PTR(text),
319
- RSTRING_LEN(text),
320
- NULL
321
- ));
322
- }
323
-
324
- VALUE cTox_join_groupchat(const VALUE self, const VALUE friend_number, const VALUE data)
325
- {
326
- cTox_ *tox;
327
-
328
- // Don't know yet how to check for integers
329
- // Check_Type(friend_number, T_INTEGER);
330
- Check_Type(data, T_STRING);
331
-
332
- Data_Get_Struct(self, cTox_, tox);
333
-
334
- return INT2FIX(tox_join_groupchat(
335
- tox->tox,
336
- NUM2LONG(friend_number),
337
- (uint8_t*)RSTRING_PTR(data),
338
- RSTRING_LEN(data)
339
- ));
340
- }
341
-
342
- VALUE cTox_group_message_send(const VALUE self, const VALUE group_number, const VALUE text)
343
- {
344
- cTox_ *tox;
345
-
346
- // Don't know yet how to check for integers
347
- // Check_Type(group_number, T_INTEGER);
348
- Check_Type(text, T_STRING);
349
-
350
- Data_Get_Struct(self, cTox_, tox);
351
-
352
- return INT2FIX(tox_group_message_send(
353
- tox->tox,
354
- NUM2LONG(group_number),
355
- (uint8_t*)RSTRING_PTR(text),
356
- RSTRING_LEN(text)
357
- ));
358
- }
359
-
360
- VALUE cTox_group_peernumber_is_ours(
361
- const VALUE self,
362
- const VALUE group_number,
363
- const VALUE peer_number)
364
- {
365
- cTox_ *tox;
366
-
367
- // Don't know yet how to check for integers
368
- // Check_Type(group_number, T_INTEGER);
369
- // Check_Type(peer_number, T_INTEGER);
370
-
371
- Data_Get_Struct(self, cTox_, tox);
372
-
373
- if (tox_group_peernumber_is_ours(
374
- tox->tox,
375
- NUM2INT(group_number),
376
- NUM2INT(peer_number))
377
- )
378
- return Qtrue;
379
- else
380
- return Qfalse;
381
- }
382
-
383
- VALUE cTox_name(const VALUE self)
384
- {
385
- cTox_ *tox;
386
-
387
- char name[TOX_MAX_NAME_LENGTH];
388
- size_t name_size;
389
-
390
- Data_Get_Struct(self, cTox_, tox);
391
-
392
- name_size = tox_self_get_name_size(tox->tox);
393
-
394
- if (name_size > 0)
395
- tox_self_get_name(tox->tox, (uint8_t*)name);
396
-
397
- return rb_str_new(name, name_size);
398
- }
399
-
400
- VALUE cTox_name_ASSIGN(const VALUE self, const VALUE name)
401
- {
402
- cTox_ *tox;
403
-
404
- bool result;
405
- TOX_ERR_SET_INFO error;
406
-
407
- Check_Type(name, T_STRING);
408
-
409
- Data_Get_Struct(self, cTox_, tox);
410
-
411
- result = tox_self_set_name(tox->tox, (uint8_t*)RSTRING_PTR(name), RSTRING_LEN(name), &error);
412
-
413
- if (error != TOX_ERR_SET_INFO_OK || !result)
414
- return Qfalse;
415
- else
416
- return Qtrue;
417
- }
418
-
419
- VALUE cTox_status_message(const VALUE self)
420
- {
421
- cTox_ *tox;
422
-
423
- char text[TOX_MAX_STATUS_MESSAGE_LENGTH];
424
- size_t text_size;
425
-
426
- Data_Get_Struct(self, cTox_, tox);
427
-
428
- text_size = tox_self_get_status_message_size(tox->tox);
429
-
430
- if (text_size > 0)
431
- tox_self_get_status_message(tox->tox, (uint8_t*)text);
432
-
433
- return rb_str_new(text, text_size);
434
- }
435
-
436
- VALUE cTox_status_message_ASSIGN(const VALUE self, const VALUE text)
437
- {
438
- cTox_ *tox;
439
-
440
- bool result;
441
- TOX_ERR_SET_INFO error;
442
-
443
- Check_Type(text, T_STRING);
444
-
445
- Data_Get_Struct(self, cTox_, tox);
446
-
447
- result = tox_self_set_status_message(tox->tox, (uint8_t*)RSTRING_PTR(text), RSTRING_LEN(text), &error);
448
-
449
- if (error != TOX_ERR_SET_INFO_OK || !result)
450
- return Qfalse;
451
- else
452
- return Qtrue;
453
- }
454
-
455
- void on_friend_request(
456
- Tox *const tox,
457
- const uint8_t *const key,
458
- const uint8_t *const data,
459
- const size_t length,
460
- const VALUE self)
461
- {
462
- VALUE rb_on_friend_request;
463
-
464
- rb_on_friend_request = rb_iv_get(self, "@on_friend_request");
465
-
466
- if (Qnil != rb_on_friend_request)
467
- rb_funcall(
468
- rb_on_friend_request,
469
- rb_intern("call"),
470
- 2,
471
- rb_str_new((char*)key, TOX_PUBLIC_KEY_SIZE),
472
- rb_str_new((char*)data, length)
473
- );
474
- }
475
-
476
- void on_friend_message(
477
- Tox *const tox,
478
- const uint32_t friend_number,
479
- const TOX_MESSAGE_TYPE type,
480
- const uint8_t *const text,
481
- const size_t length,
482
- const VALUE self)
483
- {
484
- VALUE rb_on_friend_message;
485
-
486
- if (type != TOX_MESSAGE_TYPE_NORMAL)
487
- return;
488
-
489
- rb_on_friend_message = rb_iv_get(self, "@on_friend_message");
490
-
491
- if (Qnil != rb_on_friend_message)
492
- rb_funcall(
493
- rb_on_friend_message,
494
- rb_intern("call"),
495
- 2,
496
- LONG2FIX(friend_number),
497
- rb_str_new((char*)text, length)
498
- );
499
- }
500
-
501
- static void on_group_invite(
502
- Tox *const tox,
503
- const int32_t friend_number,
504
- const uint8_t type,
505
- const uint8_t *const data,
506
- const uint16_t length,
507
- const VALUE self)
508
- {
509
- VALUE rb_on_group_invite;
510
-
511
- rb_on_group_invite = rb_iv_get(self, "@on_group_invite");
512
-
513
- if (Qnil != rb_on_group_invite)
514
- rb_funcall(
515
- rb_on_group_invite,
516
- rb_intern("call"),
517
- 2,
518
- LONG2FIX(friend_number),
519
- rb_str_new((char*)data, length)
520
- );
521
- }
522
-
523
- static void on_group_message(
524
- Tox *const tox,
525
- const int group_number,
526
- const int peer_number,
527
- const uint8_t *const text,
528
- const uint16_t length,
529
- const VALUE self)
530
- {
531
- VALUE rb_on_group_message;
532
-
533
- rb_on_group_message = rb_iv_get(self, "@on_group_message");
534
-
535
- if (Qnil != rb_on_group_message)
536
- rb_funcall(
537
- rb_on_group_message,
538
- rb_intern("call"),
539
- 3,
540
- LONG2FIX(group_number),
541
- LONG2FIX(peer_number),
542
- rb_str_new((char*)text, length)
543
- );
544
- }
545
-
546
- /******************************************************************************
547
- * Tox::Options
548
- ******************************************************************************/
549
-
550
- VALUE cTox_cOptions_alloc(const VALUE klass)
551
- {
552
- cTox_cOptions_ *tox_options;
553
-
554
- tox_options = ALLOC(cTox_cOptions_);
555
-
556
- return Data_Wrap_Struct(klass, NULL, cTox_cOptions_free, tox_options);
557
- }
558
-
559
- void cTox_cOptions_free(void *const ptr)
560
- {
561
- free(ptr);
562
- }
563
-
564
- VALUE cTox_cOptions_initialize(const VALUE self)
565
- {
566
- cTox_cOptions_ *tox_options;
567
-
568
- Data_Get_Struct(self, cTox_cOptions_, tox_options);
569
-
570
- tox_options_default(tox_options);
571
-
572
- return self;
573
- }
574
-
575
- VALUE cTox_cOptions_data_EQUALS(const VALUE self, const VALUE savedata)
576
- {
577
- cTox_cOptions_ *tox_options;
578
-
579
- Check_Type(savedata, T_STRING);
580
-
581
- Data_Get_Struct(self, cTox_cOptions_, tox_options);
582
-
583
- tox_options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
584
- tox_options->savedata_data = (uint8_t*)RSTRING_PTR(savedata);
585
- tox_options->savedata_length = RSTRING_LEN(savedata);
586
-
587
- return savedata;
588
- }