tox 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,31 +1,42 @@
1
- /*
2
- * tox.rb - Ruby interface for libtoxcore
3
- * Copyright (C) 2015-2017 Braiden Vasco
4
- *
5
- * This program is free software: you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License as published by
7
- * the Free Software Foundation, either version 3 of the License, or
8
- * (at your option) any later version.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- * GNU General Public License for more details.
14
- *
15
- * You should have received a copy of the GNU General Public License
16
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
- */
18
-
19
1
  #include "tox.h"
20
2
 
21
3
  // Memory management
22
4
  static VALUE mTox_cOptions_alloc(VALUE klass);
23
- static void mTox_cOptions_free(void *free_cdata);
5
+ static void mTox_cOptions_free(mTox_cOptions_CDATA *free_cdata);
24
6
 
25
7
  // Public methods
26
8
 
9
+ static VALUE mTox_cOptions_initialize(VALUE self);
10
+
27
11
  static VALUE mTox_cOptions_savedata(VALUE self);
28
- static VALUE mTox_cOptions_savedata_EQUALS(VALUE self, VALUE savedata);
12
+ static VALUE mTox_cOptions_savedata_ASSIGN(VALUE self, VALUE savedata);
13
+
14
+ static VALUE mTox_cOptions_ipv6_enabled(VALUE self);
15
+ static VALUE mTox_cOptions_ipv6_enabled_ASSIGN(VALUE self, VALUE enabled);
16
+
17
+ static VALUE mTox_cOptions_udp_enabled(VALUE self);
18
+ static VALUE mTox_cOptions_udp_enabled_ASSIGN(VALUE self, VALUE enabled);
19
+
20
+ static VALUE mTox_cOptions_local_discovery_enabled(VALUE self);
21
+ static VALUE mTox_cOptions_local_discovery_enabled_ASSIGN(VALUE self, VALUE enabled);
22
+
23
+ static VALUE mTox_cOptions_proxy_type(VALUE self);
24
+ static VALUE mTox_cOptions_proxy_host(VALUE self);
25
+ static VALUE mTox_cOptions_proxy_port(VALUE self);
26
+
27
+ static VALUE mTox_cOptions_start_port(VALUE self);
28
+ static VALUE mTox_cOptions_end_port(VALUE self);
29
+ static VALUE mTox_cOptions_tcp_port(VALUE self);
30
+
31
+ // Private methods
32
+
33
+ static VALUE mTox_cOptions_proxy_type_ASSIGN(VALUE self, VALUE proxy_type);
34
+ static VALUE mTox_cOptions_proxy_host_ASSIGN(VALUE self, VALUE proxy_host);
35
+ static VALUE mTox_cOptions_proxy_port_ASSIGN(VALUE self, VALUE proxy_port);
36
+
37
+ static VALUE mTox_cOptions_start_port_internal_ASSIGN(VALUE self, VALUE start_port);
38
+ static VALUE mTox_cOptions_end_port_internal_ASSIGN(VALUE self, VALUE end_port);
39
+ static VALUE mTox_cOptions_tcp_port_internal_ASSIGN(VALUE self, VALUE tcp_port);
29
40
 
30
41
  /*************************************************************
31
42
  * Initialization
@@ -37,8 +48,35 @@ void mTox_cOptions_INIT()
37
48
  rb_define_alloc_func(mTox_cOptions, mTox_cOptions_alloc);
38
49
 
39
50
  // Public methods
51
+ rb_define_method(mTox_cOptions, "initialize", mTox_cOptions_initialize, 0);
52
+
40
53
  rb_define_method(mTox_cOptions, "savedata", mTox_cOptions_savedata, 0);
41
- rb_define_method(mTox_cOptions, "savedata=", mTox_cOptions_savedata_EQUALS, 1);
54
+ rb_define_method(mTox_cOptions, "savedata=", mTox_cOptions_savedata_ASSIGN, 1);
55
+
56
+ rb_define_method(mTox_cOptions, "ipv6_enabled", mTox_cOptions_ipv6_enabled, 0);
57
+ rb_define_method(mTox_cOptions, "ipv6_enabled=", mTox_cOptions_ipv6_enabled_ASSIGN, 1);
58
+
59
+ rb_define_method(mTox_cOptions, "udp_enabled", mTox_cOptions_udp_enabled, 0);
60
+ rb_define_method(mTox_cOptions, "udp_enabled=", mTox_cOptions_udp_enabled_ASSIGN, 1);
61
+
62
+ rb_define_method(mTox_cOptions, "local_discovery_enabled", mTox_cOptions_local_discovery_enabled, 0);
63
+ rb_define_method(mTox_cOptions, "local_discovery_enabled=", mTox_cOptions_local_discovery_enabled_ASSIGN, 1);
64
+
65
+ rb_define_method(mTox_cOptions, "proxy_type", mTox_cOptions_proxy_type, 0);
66
+ rb_define_method(mTox_cOptions, "proxy_host", mTox_cOptions_proxy_host, 0);
67
+ rb_define_method(mTox_cOptions, "proxy_port", mTox_cOptions_proxy_port, 0);
68
+ rb_define_method(mTox_cOptions, "start_port", mTox_cOptions_start_port, 0);
69
+ rb_define_method(mTox_cOptions, "end_port", mTox_cOptions_end_port, 0);
70
+ rb_define_method(mTox_cOptions, "tcp_port", mTox_cOptions_tcp_port, 0);
71
+
72
+ // Private methods
73
+ rb_define_private_method(mTox_cOptions, "proxy_type=", mTox_cOptions_proxy_type_ASSIGN, 1);
74
+ rb_define_private_method(mTox_cOptions, "proxy_host=", mTox_cOptions_proxy_host_ASSIGN, 1);
75
+ rb_define_private_method(mTox_cOptions, "proxy_port=", mTox_cOptions_proxy_port_ASSIGN, 1);
76
+
77
+ rb_define_private_method(mTox_cOptions, "start_port_internal=", mTox_cOptions_start_port_internal_ASSIGN, 1);
78
+ rb_define_private_method(mTox_cOptions, "end_port_internal=", mTox_cOptions_end_port_internal_ASSIGN, 1);
79
+ rb_define_private_method(mTox_cOptions, "tcp_port_internal=", mTox_cOptions_tcp_port_internal_ASSIGN, 1);
42
80
  }
43
81
 
44
82
  /*************************************************************
@@ -49,13 +87,17 @@ VALUE mTox_cOptions_alloc(const VALUE klass)
49
87
  {
50
88
  mTox_cOptions_CDATA *alloc_cdata = ALLOC(mTox_cOptions_CDATA);
51
89
 
52
- tox_options_default(alloc_cdata);
90
+ alloc_cdata->tox_options = NULL;
53
91
 
54
92
  return Data_Wrap_Struct(klass, NULL, mTox_cOptions_free, alloc_cdata);
55
93
  }
56
94
 
57
- void mTox_cOptions_free(void *const free_cdata)
95
+ void mTox_cOptions_free(mTox_cOptions_CDATA *const free_cdata)
58
96
  {
97
+ if (free_cdata->tox_options) {
98
+ tox_options_free(free_cdata->tox_options);
99
+ }
100
+
59
101
  free(free_cdata);
60
102
  }
61
103
 
@@ -63,43 +105,320 @@ void mTox_cOptions_free(void *const free_cdata)
63
105
  * Public methods
64
106
  *************************************************************/
65
107
 
108
+ // Tox::Options#initialize
109
+ VALUE mTox_cOptions_initialize(const VALUE self)
110
+ {
111
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
112
+
113
+ TOX_ERR_OPTIONS_NEW error;
114
+
115
+ self_cdata->tox_options = tox_options_new(&error);
116
+
117
+ tox_options_set_proxy_host(self_cdata->tox_options, NULL);
118
+
119
+ switch (error) {
120
+ case TOX_ERR_OPTIONS_NEW_OK:
121
+ break;
122
+ case TOX_ERR_OPTIONS_NEW_MALLOC:
123
+ RAISE_FUNC_ERROR(
124
+ "tox_options_new",
125
+ rb_eNoMemError,
126
+ "TOX_ERR_OPTIONS_NEW_MALLOC"
127
+ );
128
+ default:
129
+ RAISE_FUNC_ERROR_DEFAULT("tox_options_new");
130
+ }
131
+
132
+ if (!self_cdata->tox_options) {
133
+ RAISE_FUNC_RESULT("tox_options_new");
134
+ }
135
+
136
+ return self;
137
+ }
138
+
66
139
  // Tox::Options#savedata
67
140
  VALUE mTox_cOptions_savedata(const VALUE self)
68
141
  {
69
- mTox_cOptions_CDATA *self_cdata;
70
-
71
- Data_Get_Struct(self, mTox_cOptions_CDATA, self_cdata);
142
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
72
143
 
73
- switch (self_cdata->savedata_type) {
144
+ switch (self_cdata->tox_options->savedata_type) {
74
145
  case TOX_SAVEDATA_TYPE_NONE:
75
146
  return Qnil;
76
147
  case TOX_SAVEDATA_TYPE_TOX_SAVE:
77
- return rb_str_new(self_cdata->savedata_data, self_cdata->savedata_length);
148
+ {
149
+ const VALUE savedata = rb_str_new(
150
+ self_cdata->tox_options->savedata_data,
151
+ self_cdata->tox_options->savedata_length
152
+ );
153
+ return savedata;
154
+ }
78
155
  default:
79
- rb_raise(rb_eNotImpError, "Tox::Options#savedata has unknown type");
156
+ RAISE_ENUM("TOX_SAVEDATA_TYPE");
80
157
  }
81
158
  }
82
159
 
83
160
  // Tox::Options#savedata=
84
- VALUE mTox_cOptions_savedata_EQUALS(const VALUE self, const VALUE savedata)
161
+ VALUE mTox_cOptions_savedata_ASSIGN(const VALUE self, const VALUE savedata)
85
162
  {
86
- mTox_cOptions_CDATA *self_cdata;
87
-
88
- Data_Get_Struct(self, mTox_cOptions_CDATA, self_cdata);
163
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
89
164
 
90
165
  if (Qnil == savedata) {
91
- self_cdata->savedata_type = TOX_SAVEDATA_TYPE_NONE;
92
- self_cdata->savedata_data = NULL;
93
- self_cdata->savedata_length = 0;
166
+ self_cdata->tox_options->savedata_type = TOX_SAVEDATA_TYPE_NONE;
167
+ self_cdata->tox_options->savedata_data = NULL;
168
+ self_cdata->tox_options->savedata_length = 0;
94
169
 
95
170
  return Qnil;
96
171
  }
97
172
 
98
173
  Check_Type(savedata, T_STRING);
99
174
 
100
- self_cdata->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
101
- self_cdata->savedata_data = (uint8_t*)RSTRING_PTR(savedata);
102
- self_cdata->savedata_length = RSTRING_LEN(savedata);
175
+ self_cdata->tox_options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
176
+ self_cdata->tox_options->savedata_data = RSTRING_PTR(savedata);
177
+ self_cdata->tox_options->savedata_length = RSTRING_LEN(savedata);
103
178
 
104
179
  return savedata;
105
180
  }
181
+
182
+ // Tox::Options#ipv6_enabled
183
+ VALUE mTox_cOptions_ipv6_enabled(const VALUE self)
184
+ {
185
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
186
+
187
+ if (tox_options_get_ipv6_enabled(self_cdata->tox_options)) {
188
+ return Qtrue;
189
+ }
190
+ else {
191
+ return Qfalse;
192
+ }
193
+ }
194
+
195
+ // Tox::Options#ipv6_enabled=
196
+ VALUE mTox_cOptions_ipv6_enabled_ASSIGN(const VALUE self, const VALUE enabled)
197
+ {
198
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
199
+
200
+ tox_options_set_ipv6_enabled(self_cdata->tox_options, RTEST(enabled));
201
+
202
+ return enabled;
203
+ }
204
+
205
+ // Tox::Options#udp_enabled
206
+ VALUE mTox_cOptions_udp_enabled(const VALUE self)
207
+ {
208
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
209
+
210
+ if (tox_options_get_udp_enabled(self_cdata->tox_options)) {
211
+ return Qtrue;
212
+ }
213
+ else {
214
+ return Qfalse;
215
+ }
216
+ }
217
+
218
+ // Tox::Options#udp_enabled=
219
+ VALUE mTox_cOptions_udp_enabled_ASSIGN(const VALUE self, const VALUE enabled)
220
+ {
221
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
222
+
223
+ tox_options_set_udp_enabled(self_cdata->tox_options, RTEST(enabled));
224
+
225
+ return enabled;
226
+ }
227
+
228
+ // Tox::Options#local_discovery_enabled
229
+ VALUE mTox_cOptions_local_discovery_enabled(const VALUE self)
230
+ {
231
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
232
+
233
+ if (tox_options_get_local_discovery_enabled(self_cdata->tox_options)) {
234
+ return Qtrue;
235
+ }
236
+ else {
237
+ return Qfalse;
238
+ }
239
+ }
240
+
241
+ // Tox::Options#local_discovery_enabled=
242
+ VALUE mTox_cOptions_local_discovery_enabled_ASSIGN(const VALUE self, const VALUE enabled)
243
+ {
244
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
245
+
246
+ tox_options_set_local_discovery_enabled(self_cdata->tox_options, RTEST(enabled));
247
+
248
+ return enabled;
249
+ }
250
+
251
+ // Tox::Options#proxy_type
252
+ VALUE mTox_cOptions_proxy_type(const VALUE self)
253
+ {
254
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
255
+
256
+ const TOX_PROXY_TYPE proxy_type_data = tox_options_get_proxy_type(self_cdata->tox_options);
257
+
258
+ const VALUE proxy_type = mTox_mProxyType_FROM_DATA(proxy_type_data);
259
+
260
+ return proxy_type;
261
+ }
262
+
263
+ // Tox::Options#proxy_host
264
+ VALUE mTox_cOptions_proxy_host(const VALUE self)
265
+ {
266
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
267
+
268
+ const char *proxy_host_data = tox_options_get_proxy_host(self_cdata->tox_options);
269
+
270
+ if (!proxy_host_data) {
271
+ return Qnil;
272
+ }
273
+
274
+ const VALUE proxy_host = rb_str_new_cstr(self_cdata->tox_options->proxy_host);
275
+
276
+ return proxy_host;
277
+ }
278
+
279
+ // Tox::Options#proxy_port
280
+ VALUE mTox_cOptions_proxy_port(const VALUE self)
281
+ {
282
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
283
+
284
+ const uint16_t proxy_port_data = tox_options_get_proxy_port(self_cdata->tox_options);
285
+
286
+ const VALUE proxy_port = LONG2FIX(proxy_port_data);
287
+
288
+ return proxy_port;
289
+ }
290
+
291
+ // Tox::Options#start_port
292
+ VALUE mTox_cOptions_start_port(const VALUE self)
293
+ {
294
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
295
+
296
+ const uint16_t start_port_data = tox_options_get_start_port(self_cdata->tox_options);
297
+
298
+ const VALUE start_port = LONG2FIX(start_port_data);
299
+
300
+ return start_port;
301
+ }
302
+
303
+ // Tox::Options#end_port
304
+ VALUE mTox_cOptions_end_port(const VALUE self)
305
+ {
306
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
307
+
308
+ const uint16_t end_port_data = tox_options_get_end_port(self_cdata->tox_options);
309
+
310
+ const VALUE end_port = LONG2FIX(end_port_data);
311
+
312
+ return end_port;
313
+ }
314
+
315
+ // Tox::Options#tcp_port
316
+ VALUE mTox_cOptions_tcp_port(const VALUE self)
317
+ {
318
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
319
+
320
+ const uint16_t tcp_port_data = tox_options_get_tcp_port(self_cdata->tox_options);
321
+
322
+ const VALUE tcp_port = LONG2FIX(tcp_port_data);
323
+
324
+ return tcp_port;
325
+ }
326
+
327
+ /*************************************************************
328
+ * Private methods
329
+ *************************************************************/
330
+
331
+ // Tox::Options#proxy_type=
332
+ VALUE mTox_cOptions_proxy_type_ASSIGN(const VALUE self, const VALUE proxy_type)
333
+ {
334
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
335
+
336
+ const TOX_PROXY_TYPE proxy_type_data = mTox_mProxyType_TO_DATA(proxy_type);
337
+
338
+ tox_options_set_proxy_type(self_cdata->tox_options, proxy_type_data);
339
+
340
+ return proxy_type;
341
+ }
342
+
343
+ // Tox::Options#proxy_host=
344
+ VALUE mTox_cOptions_proxy_host_ASSIGN(const VALUE self, const VALUE proxy_host)
345
+ {
346
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
347
+
348
+ tox_options_set_proxy_host(self_cdata->tox_options, NULL);
349
+
350
+ if (proxy_host == Qnil) {
351
+ return Qnil;
352
+ }
353
+
354
+ Check_Type(proxy_host, T_STRING);
355
+
356
+ const char *proxy_host_data = RSTRING_PTR(proxy_host);
357
+ const size_t proxy_host_length_data = RSTRING_LEN(proxy_host);
358
+
359
+ if (proxy_host_length_data == 0 || proxy_host_data[0] == '\0') {
360
+ return Qnil;
361
+ }
362
+
363
+ if (proxy_host_length_data >= mTox_cOptions_CDATA_PROXY_HOST_BUFFER_SIZE) {
364
+ rb_raise(
365
+ rb_eRuntimeError,
366
+ "Proxy host string can not be longer than 255 bytes"
367
+ );
368
+ }
369
+
370
+ memcpy(self_cdata->proxy_host, proxy_host_data, proxy_host_length_data);
371
+ self_cdata->proxy_host[proxy_host_length_data] = '\0';
372
+
373
+ tox_options_set_proxy_host(self_cdata->tox_options, self_cdata->proxy_host);
374
+
375
+ return proxy_host;
376
+ }
377
+
378
+ // Tox::Options#proxy_port=
379
+ VALUE mTox_cOptions_proxy_port_ASSIGN(const VALUE self, const VALUE proxy_port)
380
+ {
381
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
382
+
383
+ const uint16_t proxy_port_data = FIX2LONG(proxy_port);
384
+
385
+ tox_options_set_proxy_port(self_cdata->tox_options, proxy_port_data);
386
+
387
+ return proxy_port;
388
+ }
389
+
390
+ // Tox::Options#start_port_internal=
391
+ VALUE mTox_cOptions_start_port_internal_ASSIGN(const VALUE self, const VALUE start_port)
392
+ {
393
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
394
+
395
+ const uint16_t start_port_data = FIX2LONG(start_port);
396
+
397
+ tox_options_set_start_port(self_cdata->tox_options, start_port_data);
398
+
399
+ return start_port;
400
+ }
401
+
402
+ // Tox::Options#end_port_internal=
403
+ VALUE mTox_cOptions_end_port_internal_ASSIGN(const VALUE self, const VALUE end_port)
404
+ {
405
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
406
+
407
+ const uint16_t end_port_data = FIX2LONG(end_port);
408
+
409
+ tox_options_set_end_port(self_cdata->tox_options, end_port_data);
410
+
411
+ return end_port;
412
+ }
413
+
414
+ // Tox::Options#tcp_port_internal=
415
+ VALUE mTox_cOptions_tcp_port_internal_ASSIGN(const VALUE self, const VALUE tcp_port)
416
+ {
417
+ CDATA(self, mTox_cOptions_CDATA, self_cdata);
418
+
419
+ const uint16_t tcp_port_data = FIX2LONG(tcp_port);
420
+
421
+ tox_options_set_tcp_port(self_cdata->tox_options, tcp_port_data);
422
+
423
+ return tcp_port;
424
+ }