tox 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,3 @@
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
  #if !(TOX_VERSION_IS_API_COMPATIBLE(0, 2, 1))
@@ -28,27 +10,37 @@ VALUE mTox;
28
10
 
29
11
  VALUE mTox_eNullError;
30
12
  VALUE mTox_eUnknownError;
31
- VALUE mTox_eUnknownSecurityError;
32
13
 
33
14
  VALUE mTox_mVersion;
34
15
  VALUE mTox_mUserStatus;
16
+ VALUE mTox_mConnectionStatus;
17
+ VALUE mTox_mProxyType;
35
18
  VALUE mTox_cOptions;
36
19
  VALUE mTox_cClient;
37
20
  VALUE mTox_cNode;
38
21
  VALUE mTox_cFriend;
39
22
  VALUE mTox_cAddress;
23
+ VALUE mTox_cNospam;
40
24
  VALUE mTox_cPublicKey;
41
25
  VALUE mTox_mOutMessage;
26
+ VALUE mTox_cOutFriendMessage;
42
27
 
43
28
  VALUE mTox_mUserStatus_NONE;
44
29
  VALUE mTox_mUserStatus_AWAY;
45
30
  VALUE mTox_mUserStatus_BUSY;
46
31
 
32
+ VALUE mTox_mProxyType_NONE;
33
+ VALUE mTox_mProxyType_HTTP;
34
+ VALUE mTox_mProxyType_SOCKS5;
35
+
36
+ VALUE mTox_mConnectionStatus_NONE;
37
+ VALUE mTox_mConnectionStatus_TCP;
38
+ VALUE mTox_mConnectionStatus_UDP;
39
+
47
40
  VALUE mTox_cClient_eBadSavedataError;
48
41
 
49
42
  VALUE mTox_cFriend_eNotFoundError;
50
43
  VALUE mTox_cFriend_eNotConnectedError;
51
- VALUE mTox_cFriend_cOutMessage;
52
44
 
53
45
  VALUE mTox_mOutMessage_eSendQueueAllocError;
54
46
  VALUE mTox_mOutMessage_eTooLongError;
@@ -72,29 +64,39 @@ void Init_tox()
72
64
 
73
65
  mTox = rb_const_get(rb_cObject, rb_intern("Tox"));
74
66
 
75
- mTox_eNullError = rb_const_get(mTox, rb_intern("NullError"));
76
- mTox_eUnknownError = rb_const_get(mTox, rb_intern("UnknownError"));
77
- mTox_eUnknownSecurityError = rb_const_get(mTox, rb_intern("UnknownSecurityError"));
78
-
79
- mTox_mVersion = rb_const_get(mTox, rb_intern("Version"));
80
- mTox_mUserStatus = rb_const_get(mTox, rb_intern("UserStatus"));
81
- mTox_cOptions = rb_const_get(mTox, rb_intern("Options"));
82
- mTox_cClient = rb_const_get(mTox, rb_intern("Client"));
83
- mTox_cNode = rb_const_get(mTox, rb_intern("Node"));
84
- mTox_cFriend = rb_const_get(mTox, rb_intern("Friend"));
85
- mTox_cAddress = rb_const_get(mTox, rb_intern("Address"));
86
- mTox_cPublicKey = rb_const_get(mTox, rb_intern("PublicKey"));
87
- mTox_mOutMessage = rb_const_get(mTox, rb_intern("OutMessage"));
67
+ mTox_eNullError = rb_const_get(mTox, rb_intern("NullError"));
68
+ mTox_eUnknownError = rb_const_get(mTox, rb_intern("UnknownError"));
69
+
70
+ mTox_mVersion = rb_const_get(mTox, rb_intern("Version"));
71
+ mTox_mUserStatus = rb_const_get(mTox, rb_intern("UserStatus"));
72
+ mTox_mConnectionStatus = rb_const_get(mTox, rb_intern("ConnectionStatus"));
73
+ mTox_mProxyType = rb_const_get(mTox, rb_intern("ProxyType"));
74
+ mTox_cOptions = rb_const_get(mTox, rb_intern("Options"));
75
+ mTox_cClient = rb_const_get(mTox, rb_intern("Client"));
76
+ mTox_cNode = rb_const_get(mTox, rb_intern("Node"));
77
+ mTox_cFriend = rb_const_get(mTox, rb_intern("Friend"));
78
+ mTox_cAddress = rb_const_get(mTox, rb_intern("Address"));
79
+ mTox_cNospam = rb_const_get(mTox, rb_intern("Nospam"));
80
+ mTox_cPublicKey = rb_const_get(mTox, rb_intern("PublicKey"));
81
+ mTox_mOutMessage = rb_const_get(mTox, rb_intern("OutMessage"));
82
+ mTox_cOutFriendMessage = rb_const_get(mTox, rb_intern("OutFriendMessage"));
88
83
 
89
84
  mTox_mUserStatus_NONE = rb_const_get(mTox_mUserStatus, rb_intern("NONE"));
90
85
  mTox_mUserStatus_AWAY = rb_const_get(mTox_mUserStatus, rb_intern("AWAY"));
91
86
  mTox_mUserStatus_BUSY = rb_const_get(mTox_mUserStatus, rb_intern("BUSY"));
92
87
 
88
+ mTox_mProxyType_NONE = rb_const_get(mTox_mProxyType, rb_intern("NONE"));
89
+ mTox_mProxyType_HTTP = rb_const_get(mTox_mProxyType, rb_intern("HTTP"));
90
+ mTox_mProxyType_SOCKS5 = rb_const_get(mTox_mProxyType, rb_intern("SOCKS5"));
91
+
92
+ mTox_mConnectionStatus_NONE = rb_const_get(mTox_mConnectionStatus, rb_intern("NONE"));
93
+ mTox_mConnectionStatus_TCP = rb_const_get(mTox_mConnectionStatus, rb_intern("TCP"));
94
+ mTox_mConnectionStatus_UDP = rb_const_get(mTox_mConnectionStatus, rb_intern("UDP"));
95
+
93
96
  mTox_cClient_eBadSavedataError = rb_const_get(mTox_cClient, rb_intern("BadSavedataError"));
94
97
 
95
98
  mTox_cFriend_eNotFoundError = rb_const_get(mTox_cFriend, rb_intern("NotFoundError"));
96
99
  mTox_cFriend_eNotConnectedError = rb_const_get(mTox_cFriend, rb_intern("NotConnectedError"));
97
- mTox_cFriend_cOutMessage = rb_const_get(mTox_cFriend, rb_intern("OutMessage"));
98
100
 
99
101
  mTox_mOutMessage_eSendQueueAllocError = rb_const_get(mTox_mOutMessage, rb_intern("SendQueueAllocError"));
100
102
  mTox_mOutMessage_eTooLongError = rb_const_get(mTox_mOutMessage, rb_intern("TooLongError"));
@@ -121,9 +123,11 @@ VALUE mTox_hash(const VALUE self, const VALUE data)
121
123
 
122
124
  const uint8_t result[TOX_HASH_LENGTH];
123
125
 
124
- if (true != tox_hash(result, (const uint8_t*)RSTRING_PTR(data), RSTRING_LEN(data))) {
125
- rb_raise(mTox_eUnknownSecurityError, "tox_hash() failed");
126
+ if (true != tox_hash(result, RSTRING_PTR(data), RSTRING_LEN(data))) {
127
+ RAISE_FUNC_RESULT("tox_hash");
126
128
  }
127
129
 
128
- return rb_str_new(result, TOX_HASH_LENGTH);
130
+ const VALUE hash = rb_str_new(result, TOX_HASH_LENGTH);
131
+
132
+ return hash;
129
133
  }
@@ -1,21 +1,3 @@
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 <ruby.h>
20
2
 
21
3
  #include <tox/tox.h>
@@ -30,7 +12,12 @@ void mTox_cFriend_INIT();
30
12
 
31
13
  // C data
32
14
 
33
- typedef struct Tox_Options mTox_cOptions_CDATA;
15
+ #define mTox_cOptions_CDATA_PROXY_HOST_BUFFER_SIZE 256
16
+
17
+ typedef struct {
18
+ struct Tox_Options *tox_options;
19
+ char proxy_host[mTox_cOptions_CDATA_PROXY_HOST_BUFFER_SIZE];
20
+ } mTox_cOptions_CDATA;
34
21
 
35
22
  typedef struct {
36
23
  Tox *tox;
@@ -42,28 +29,221 @@ extern VALUE mTox;
42
29
 
43
30
  extern VALUE mTox_eNullError;
44
31
  extern VALUE mTox_eUnknownError;
45
- extern VALUE mTox_eUnknownSecurityError;
46
32
 
47
33
  extern VALUE mTox_mVersion;
48
34
  extern VALUE mTox_mUserStatus;
35
+ extern VALUE mTox_mConnectionStatus;
36
+ extern VALUE mTox_mProxyType;
49
37
  extern VALUE mTox_cOptions;
50
38
  extern VALUE mTox_cClient;
51
39
  extern VALUE mTox_cNode;
52
40
  extern VALUE mTox_cFriend;
53
41
  extern VALUE mTox_cAddress;
54
42
  extern VALUE mTox_cPublicKey;
43
+ extern VALUE mTox_cNospam;
55
44
  extern VALUE mTox_mOutMessage;
45
+ extern VALUE mTox_cOutFriendMessage;
56
46
 
57
47
  extern VALUE mTox_mUserStatus_NONE;
58
48
  extern VALUE mTox_mUserStatus_AWAY;
59
49
  extern VALUE mTox_mUserStatus_BUSY;
60
50
 
51
+ extern VALUE mTox_mProxyType_NONE;
52
+ extern VALUE mTox_mProxyType_HTTP;
53
+ extern VALUE mTox_mProxyType_SOCKS5;
54
+
55
+ extern VALUE mTox_mConnectionStatus_NONE;
56
+ extern VALUE mTox_mConnectionStatus_TCP;
57
+ extern VALUE mTox_mConnectionStatus_UDP;
58
+
61
59
  extern VALUE mTox_cClient_eBadSavedataError;
62
60
 
63
61
  extern VALUE mTox_cFriend_eNotFoundError;
64
62
  extern VALUE mTox_cFriend_eNotConnectedError;
65
- extern VALUE mTox_cFriend_cOutMessage;
66
63
 
67
64
  extern VALUE mTox_mOutMessage_eSendQueueAllocError;
68
65
  extern VALUE mTox_mOutMessage_eTooLongError;
69
66
  extern VALUE mTox_mOutMessage_eEmptyError;
67
+
68
+ // Inline functions
69
+
70
+ static inline VALUE mTox_mUserStatus_FROM_DATA(TOX_USER_STATUS data);
71
+ static inline VALUE mTox_mUserStatus_TRY_DATA(TOX_USER_STATUS data);
72
+ static inline TOX_USER_STATUS mTox_mUserStatus_TO_DATA(VALUE value);
73
+
74
+ static inline VALUE mTox_mProxyType_FROM_DATA(TOX_PROXY_TYPE data);
75
+ static inline VALUE mTox_mProxyType_TRY_DATA(TOX_PROXY_TYPE data);
76
+ static inline TOX_PROXY_TYPE mTox_mProxyType_TO_DATA(VALUE value);
77
+
78
+ static inline VALUE mTox_mConnectionStatus_FROM_DATA(TOX_CONNECTION data);
79
+ static inline VALUE mTox_mConnectionStatus_TRY_DATA(TOX_CONNECTION data);
80
+ static inline TOX_CONNECTION mTox_mConnectionStatus_TO_DATA(VALUE value);
81
+
82
+ // Macros
83
+
84
+ #define CDATA(value, cdata_type, cdata) \
85
+ cdata_type *(cdata); \
86
+ Data_Get_Struct((value), cdata_type, (cdata));
87
+
88
+ #define RAISE_TYPECHECK(method_name, arg_name, expected_type) \
89
+ rb_raise( \
90
+ rb_eTypeError, \
91
+ "Expected method "method_name \
92
+ " argument \""arg_name \
93
+ "\" to be a "expected_type \
94
+ )
95
+
96
+ #define RAISE_ENUM(enum_name) \
97
+ rb_raise( \
98
+ rb_eNotImpError, \
99
+ enum_name" has unknown value" \
100
+ )
101
+
102
+ #define RAISE_OPTION(enum_module) \
103
+ rb_raise( \
104
+ rb_eArgError, \
105
+ "Invalid value from "enum_module \
106
+ )
107
+
108
+ #define RAISE_FUNC_RESULT(func_name) \
109
+ rb_raise( \
110
+ mTox_eUnknownError, \
111
+ func_name"() failed" \
112
+ )
113
+
114
+ #define RAISE_FUNC_ERROR_DEFAULT(func_name) \
115
+ rb_raise( \
116
+ mTox_eUnknownError, \
117
+ func_name"() failed" \
118
+ )
119
+
120
+ #define RAISE_FUNC_ERROR(func_name, exception_class, error) \
121
+ rb_raise( \
122
+ exception_class, \
123
+ func_name"() failed with "error \
124
+ )
125
+
126
+ // Inline function implementations
127
+
128
+ VALUE mTox_mUserStatus_FROM_DATA(const TOX_USER_STATUS data)
129
+ {
130
+ const VALUE result = mTox_mUserStatus_TRY_DATA(data);
131
+
132
+ if (result == Qnil) {
133
+ RAISE_ENUM("TOX_USER_STATUS");
134
+ }
135
+
136
+ return result;
137
+ }
138
+
139
+ VALUE mTox_mUserStatus_TRY_DATA(const TOX_USER_STATUS data)
140
+ {
141
+ switch (data) {
142
+ case TOX_USER_STATUS_NONE:
143
+ return mTox_mUserStatus_NONE;
144
+ case TOX_USER_STATUS_AWAY:
145
+ return mTox_mUserStatus_AWAY;
146
+ case TOX_USER_STATUS_BUSY:
147
+ return mTox_mUserStatus_BUSY;
148
+ default:
149
+ return Qnil;
150
+ }
151
+ }
152
+
153
+ TOX_USER_STATUS mTox_mUserStatus_TO_DATA(const VALUE value)
154
+ {
155
+ if (rb_funcall(mTox_mUserStatus_NONE, rb_intern("=="), 1, value)) {
156
+ return TOX_USER_STATUS_NONE;
157
+ }
158
+ else if (rb_funcall(mTox_mUserStatus_AWAY, rb_intern("=="), 1, value)) {
159
+ return TOX_USER_STATUS_AWAY;
160
+ }
161
+ else if (rb_funcall(mTox_mUserStatus_BUSY, rb_intern("=="), 1, value)) {
162
+ return TOX_USER_STATUS_BUSY;
163
+ }
164
+ else {
165
+ RAISE_OPTION("Tox::UserStatus");
166
+ }
167
+ }
168
+
169
+ VALUE mTox_mProxyType_FROM_DATA(const TOX_PROXY_TYPE data)
170
+ {
171
+ const VALUE result = mTox_mProxyType_TRY_DATA(data);
172
+
173
+ if (result == Qnil) {
174
+ RAISE_ENUM("TOX_PROXY_TYPE");
175
+ }
176
+
177
+ return result;
178
+ }
179
+
180
+ VALUE mTox_mProxyType_TRY_DATA(const TOX_PROXY_TYPE data)
181
+ {
182
+ switch (data) {
183
+ case TOX_PROXY_TYPE_NONE:
184
+ return mTox_mProxyType_NONE;
185
+ case TOX_PROXY_TYPE_HTTP:
186
+ return mTox_mProxyType_HTTP;
187
+ case TOX_PROXY_TYPE_SOCKS5:
188
+ return mTox_mProxyType_SOCKS5;
189
+ default:
190
+ return Qnil;
191
+ }
192
+ }
193
+
194
+ TOX_PROXY_TYPE mTox_mProxyType_TO_DATA(const VALUE value)
195
+ {
196
+ if (value == mTox_mProxyType_NONE) {
197
+ return TOX_PROXY_TYPE_NONE;
198
+ }
199
+ else if (value == mTox_mProxyType_HTTP) {
200
+ return TOX_PROXY_TYPE_HTTP;
201
+ }
202
+ else if (value == mTox_mProxyType_SOCKS5) {
203
+ return TOX_PROXY_TYPE_SOCKS5;
204
+ }
205
+ else {
206
+ RAISE_OPTION("Tox::ProxyType");
207
+ }
208
+ }
209
+
210
+ VALUE mTox_mConnectionStatus_FROM_DATA(const TOX_CONNECTION data)
211
+ {
212
+ const VALUE result = mTox_mConnectionStatus_TRY_DATA(data);
213
+
214
+ if (result == Qnil) {
215
+ RAISE_ENUM("TOX_CONNECTION");
216
+ }
217
+
218
+ return result;
219
+ }
220
+
221
+ VALUE mTox_mConnectionStatus_TRY_DATA(const TOX_CONNECTION data)
222
+ {
223
+ switch (data) {
224
+ case TOX_CONNECTION_NONE:
225
+ return mTox_mConnectionStatus_NONE;
226
+ case TOX_CONNECTION_TCP:
227
+ return mTox_mConnectionStatus_TCP;
228
+ case TOX_CONNECTION_UDP:
229
+ return mTox_mConnectionStatus_UDP;
230
+ default:
231
+ return Qnil;
232
+ }
233
+ }
234
+
235
+ TOX_CONNECTION mTox_mConnectionStatus_TO_DATA(const VALUE value)
236
+ {
237
+ if (value == mTox_mConnectionStatus_NONE) {
238
+ return TOX_CONNECTION_NONE;
239
+ }
240
+ else if (value == mTox_mConnectionStatus_TCP) {
241
+ return TOX_CONNECTION_TCP;
242
+ }
243
+ else if (value == mTox_mConnectionStatus_UDP) {
244
+ return TOX_CONNECTION_UDP;
245
+ }
246
+ else {
247
+ RAISE_OPTION("Tox::ConnectionStatus");
248
+ }
249
+ }
@@ -1,21 +1,3 @@
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
  // Singleton methods
data/lib/tox.rb CHANGED
@@ -1,41 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # tox.rb - Ruby interface for libtoxcore
4
- # Copyright (C) 2015-2017 Braiden Vasco
5
- #
6
- # This program is free software: you can redistribute it and/or modify
7
- # it under the terms of the GNU General Public License as published by
8
- # the Free Software Foundation, either version 3 of the License, or
9
- # (at your option) any later version.
10
- #
11
- # This program is distributed in the hope that it will be useful,
12
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- # GNU General Public License for more details.
15
- #
16
- # You should have received a copy of the GNU General Public License
17
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
-
19
- require 'thread'
20
3
  require 'uri'
21
4
  require 'net/http'
22
5
  require 'json'
23
6
  require 'resolv'
24
7
 
25
8
  require 'tox/version'
26
- require 'tox/user_status'
27
9
  require 'tox/options'
28
10
  require 'tox/client'
29
11
  require 'tox/status'
30
12
  require 'tox/node'
31
13
  require 'tox/out_message'
14
+ require 'tox/out_friend_message'
32
15
  require 'tox/friend'
33
- require 'tox/friend/out_message'
16
+
17
+ require 'tox/proxies/base'
18
+ require 'tox/proxies/http'
19
+ require 'tox/proxies/socks5'
20
+
21
+ # Enumerations
22
+ require 'tox/user_status'
23
+ require 'tox/proxy_type'
24
+ require 'tox/connection_status'
34
25
 
35
26
  # Primitives
36
27
  require 'tox/binary'
37
28
  require 'tox/public_key'
38
29
  require 'tox/nospam'
30
+ require 'tox/address_checksum'
39
31
  require 'tox/address'
40
32
 
41
33
  ##
@@ -61,15 +53,6 @@ module Tox
61
53
  # upgrade of libtoxcore. Specific handling is not needed for the time beeing.
62
54
  #
63
55
  class UnknownError < RuntimeError; end
64
-
65
- ##
66
- # Exception of this type is raised in similar cases to {Tox::UnknownError}
67
- # when it can have security implications. This should happen rarely after
68
- # the gem release. However, for now it can be raised when key-related
69
- # functions fail because of unknown reason for better feedback
70
- # during development.
71
- #
72
- class UnknownSecurityError < SecurityError; end
73
56
  end
74
57
 
75
58
  # C extension