tox 0.0.1

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.
@@ -0,0 +1,25 @@
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
+ #include <ruby.h>
20
+
21
+ #include <tox/tox.h>
22
+
23
+ void Init_tox();
24
+
25
+ extern VALUE mTox;
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
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
+ require 'uri'
21
+ require 'net/http'
22
+ require 'json'
23
+ require 'resolv'
24
+
25
+ require 'tox/version'
26
+ require 'tox/status'
27
+ require 'tox/node'
28
+ require 'tox/tox'
29
+ require 'tox/client'
30
+ require 'tox/friend'
31
+
32
+ # Primitives
33
+ require 'tox/binary'
34
+ require 'tox/public_key'
35
+ require 'tox/address'
36
+
37
+ ##
38
+ # Ruby interface for libtoxcore. It can be used to create Tox chat client or bot.
39
+ # The interface is object-oriented instead of C-style (raises exceptions
40
+ # instead of returning error codes, uses classes to represent primitives, etc.)
41
+ #
42
+ module Tox
43
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Address primitive.
22
+ #
23
+ class Address < Binary
24
+ def self.bytesize
25
+ 38
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Binary primitive representation.
22
+ #
23
+ class Binary < String
24
+ def self.bytesize
25
+ raise NotImplementedError, "#{self}.bytesize"
26
+ end
27
+
28
+ def self.hex_re
29
+ /\A[\da-fA-F]{#{2 * bytesize}}\z/
30
+ end
31
+
32
+ def initialize(value)
33
+ raise TypeError, "expected value to be a #{String}" unless value.is_a? String
34
+
35
+ if value.bytesize == self.class.bytesize
36
+ super value
37
+ else
38
+ raise ArgumentError, 'expected value to be a hex string' unless value =~ self.class.hex_re
39
+ super [value].pack('H*')
40
+ end
41
+
42
+ to_hex
43
+ freeze
44
+ end
45
+
46
+ def to_hex
47
+ @to_hex ||= unpack('H*').first.upcase.freeze
48
+ end
49
+
50
+ def inspect
51
+ "#<#{self.class}: \"#{to_hex}\">"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Tox client.
22
+ #
23
+ class Client
24
+ def initialize(options = Tox::Options.new)
25
+ initialize_with options
26
+ self.running = false
27
+ end
28
+
29
+ def bootstrap_official
30
+ Status.new.nodes.each do |node|
31
+ bootstrap node
32
+ end
33
+ end
34
+
35
+ def running?
36
+ @running
37
+ end
38
+
39
+ def stop
40
+ return false unless running?
41
+ self.running = false
42
+ true
43
+ end
44
+
45
+ def run
46
+ raise AlreadyRunningError, "already running in #{thread}" unless mutex.try_lock
47
+
48
+ begin
49
+ self.running = true
50
+ self.thread = Thread.current
51
+ run_loop
52
+ ensure
53
+ self.running = false
54
+ self.thread = nil
55
+ end
56
+
57
+ mutex.unlock
58
+ end
59
+
60
+ def on_friend_request(&block)
61
+ @on_friend_request = block
62
+ end
63
+
64
+ def on_friend_message(&block)
65
+ @on_friend_message = block
66
+ end
67
+
68
+ private
69
+
70
+ attr_accessor :thread
71
+
72
+ def mutex
73
+ @mutex ||= Mutex.new
74
+ end
75
+
76
+ def running=(value)
77
+ @running = !!value
78
+ end
79
+
80
+ class Error < RuntimeError; end
81
+ class BadSavedataError < Error; end
82
+ class AlreadyRunningError < Error; end
83
+ end
84
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Friend representation in Tox client.
22
+ #
23
+ class Friend
24
+ attr_reader :client, :number
25
+
26
+ def initialize(client, number)
27
+ self.client = client
28
+ self.number = number
29
+ end
30
+
31
+ private
32
+
33
+ def client=(value)
34
+ raise TypeError, "expected client to be a #{Client}" unless value.is_a? Client
35
+ @client = value
36
+ end
37
+
38
+ def number=(value)
39
+ raise TypeError, "expected number to be a #{Integer}" unless value.is_a? Integer
40
+ raise ArgumentError, 'expected number to be greater than or equal to zero' unless value >= 0
41
+ @number = value
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Tox node credentials.
22
+ #
23
+ class Node
24
+ # Range of valid port numbers.
25
+ PORT_RANGE = 0..65_535
26
+
27
+ def initialize(data)
28
+ @data = data.map { |k, v| [k.to_sym, v] }.to_h.freeze
29
+ end
30
+
31
+ def ipv4
32
+ @ipv4 ||=
33
+ begin
34
+ value = @data[:ipv4]
35
+ raise TypeError, "expected value to be a #{String}" unless value.is_a? String
36
+ value.frozen? ? value : value.dup.freeze
37
+ end
38
+ end
39
+
40
+ def port
41
+ @port ||=
42
+ begin
43
+ value = @data[:port]
44
+ raise TypeError, "expected value to be an #{Integer}" unless value.is_a? Integer
45
+ raise ArgumentError, 'expected value to be between 0 and 65535' unless PORT_RANGE.cover? value
46
+ value
47
+ end
48
+ end
49
+
50
+ def public_key
51
+ @public_key ||=
52
+ begin
53
+ value = @data[:public_key]
54
+ PublicKey.new value
55
+ end
56
+ end
57
+
58
+ def status_udp
59
+ @status_udp ||= !!@data[:status_udp]
60
+ end
61
+
62
+ def resolv_ipv4
63
+ @resolv_ipv4 ||= Resolv.getaddress ipv4
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Public key primitive.
22
+ #
23
+ class PublicKey < Binary
24
+ def self.bytesize
25
+ 32
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ ##
21
+ # Tox network status received from server running https://github.com/Tox/ToxStatus
22
+ #
23
+ class Status
24
+ # JSON API endpoint of the official network status server.
25
+ OFFICIAL_URL = 'https://nodes.tox.chat/json'
26
+
27
+ attr_reader :url
28
+
29
+ def initialize(url = OFFICIAL_URL)
30
+ self.url = url
31
+ end
32
+
33
+ def inspect
34
+ @inspect ||= "#<#{self.class} last_refresh: #{last_refresh}, last_scan: #{last_scan}>"
35
+ end
36
+
37
+ def last_refresh
38
+ @last_refresh ||= Time.at(data['last_refresh']).utc.freeze
39
+ end
40
+
41
+ def last_scan
42
+ @last_scan ||= Time.at(data['last_scan']).utc.freeze
43
+ end
44
+
45
+ def nodes
46
+ @nodes ||= data['nodes'].map { |node_data| Node.new node_data }.select(&:status_udp).compact.freeze
47
+ end
48
+
49
+ private
50
+
51
+ def url=(value)
52
+ @url = value.frozen? ? value : value.dup.freeze
53
+ end
54
+
55
+ def data
56
+ @data ||= JSON.parse Net::HTTP.get URI.parse url
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
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
+ module Tox
20
+ # Gem version.
21
+ VERSION = '0.0.1'
22
+ end