meshchat 0.8.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -11
  3. data/lib/meshchat.rb +48 -42
  4. data/lib/meshchat/configuration.rb +14 -0
  5. data/lib/meshchat/configuration/app_config.rb +63 -0
  6. data/lib/meshchat/configuration/database.rb +41 -0
  7. data/lib/meshchat/{config → configuration}/hash_file.rb +7 -6
  8. data/lib/meshchat/configuration/identity.rb +79 -0
  9. data/lib/meshchat/{config → configuration}/settings.rb +22 -26
  10. data/lib/meshchat/debug.rb +69 -0
  11. data/lib/meshchat/encryption.rb +7 -2
  12. data/lib/meshchat/encryption/aes_rsa.rb +2 -1
  13. data/lib/meshchat/encryption/passthrough.rb +5 -3
  14. data/lib/meshchat/locale/en.yml +14 -0
  15. data/lib/meshchat/models/node.rb +140 -0
  16. data/lib/meshchat/network.rb +19 -0
  17. data/lib/meshchat/network/dispatcher.rb +83 -0
  18. data/lib/meshchat/network/errors.rb +11 -0
  19. data/lib/meshchat/network/incoming.rb +13 -0
  20. data/lib/meshchat/network/incoming/message_decryptor.rb +51 -0
  21. data/lib/meshchat/network/incoming/message_processor.rb +75 -0
  22. data/lib/meshchat/network/incoming/request_processor.rb +30 -0
  23. data/lib/meshchat/network/local.rb +12 -0
  24. data/lib/meshchat/network/local/connection.rb +58 -0
  25. data/lib/meshchat/network/local/server.rb +69 -0
  26. data/lib/meshchat/network/message.rb +34 -0
  27. data/lib/meshchat/network/message/base.rb +139 -0
  28. data/lib/meshchat/network/message/chat.rb +9 -0
  29. data/lib/meshchat/network/message/disconnect.rb +21 -0
  30. data/lib/meshchat/network/message/emote.rb +9 -0
  31. data/lib/meshchat/network/message/factory.rb +80 -0
  32. data/lib/meshchat/network/message/node_list.rb +75 -0
  33. data/lib/meshchat/network/message/node_list_diff.rb +18 -0
  34. data/lib/meshchat/network/message/node_list_hash.rb +32 -0
  35. data/lib/meshchat/network/message/ping.rb +31 -0
  36. data/lib/meshchat/network/message/ping_reply.rb +12 -0
  37. data/lib/meshchat/network/message/whisper.rb +28 -0
  38. data/lib/meshchat/network/remote.rb +13 -0
  39. data/lib/meshchat/network/remote/connection.rb +28 -0
  40. data/lib/meshchat/network/remote/relay.rb +109 -0
  41. data/lib/meshchat/network/remote/relay_pool.rb +52 -0
  42. data/lib/meshchat/ui.rb +13 -0
  43. data/lib/meshchat/ui/cli.rb +48 -0
  44. data/lib/meshchat/ui/cli/base.rb +39 -0
  45. data/lib/meshchat/ui/cli/input_factory.rb +50 -0
  46. data/lib/meshchat/ui/cli/keyboard_line_input.rb +14 -0
  47. data/lib/meshchat/ui/command.rb +51 -0
  48. data/lib/meshchat/ui/command/base.rb +77 -0
  49. data/lib/meshchat/ui/command/bind.rb +47 -0
  50. data/lib/meshchat/ui/command/chat.rb +31 -0
  51. data/lib/meshchat/ui/command/config.rb +37 -0
  52. data/lib/meshchat/ui/command/emote.rb +23 -0
  53. data/lib/meshchat/ui/command/exit.rb +16 -0
  54. data/lib/meshchat/ui/command/help.rb +20 -0
  55. data/lib/meshchat/ui/command/identity.rb +16 -0
  56. data/lib/meshchat/ui/command/import.rb +42 -0
  57. data/lib/meshchat/ui/command/irb.rb +22 -0
  58. data/lib/meshchat/ui/command/offline.rb +23 -0
  59. data/lib/meshchat/ui/command/online.rb +18 -0
  60. data/lib/meshchat/ui/command/ping.rb +65 -0
  61. data/lib/meshchat/ui/command/ping_all.rb +19 -0
  62. data/lib/meshchat/ui/command/send_disconnect.rb +20 -0
  63. data/lib/meshchat/ui/command/server.rb +22 -0
  64. data/lib/meshchat/ui/command/share.rb +16 -0
  65. data/lib/meshchat/ui/command/whisper.rb +40 -0
  66. data/lib/meshchat/ui/display.rb +78 -0
  67. data/lib/meshchat/ui/display/base.rb +58 -0
  68. data/lib/meshchat/ui/display/manager.rb +59 -0
  69. data/lib/meshchat/ui/notifier.rb +9 -0
  70. data/lib/meshchat/ui/notifier/base.rb +33 -0
  71. data/lib/meshchat/version.rb +3 -2
  72. metadata +150 -80
  73. data/lib/meshchat/cli.rb +0 -188
  74. data/lib/meshchat/cli/base.rb +0 -13
  75. data/lib/meshchat/cli/input.rb +0 -37
  76. data/lib/meshchat/command/base.rb +0 -80
  77. data/lib/meshchat/command/bind.rb +0 -44
  78. data/lib/meshchat/command/chat.rb +0 -30
  79. data/lib/meshchat/command/config.rb +0 -34
  80. data/lib/meshchat/command/emote.rb +0 -20
  81. data/lib/meshchat/command/exit.rb +0 -13
  82. data/lib/meshchat/command/help.rb +0 -17
  83. data/lib/meshchat/command/identity.rb +0 -13
  84. data/lib/meshchat/command/import.rb +0 -41
  85. data/lib/meshchat/command/init.rb +0 -34
  86. data/lib/meshchat/command/irb.rb +0 -23
  87. data/lib/meshchat/command/listen.rb +0 -13
  88. data/lib/meshchat/command/offline.rb +0 -20
  89. data/lib/meshchat/command/online.rb +0 -15
  90. data/lib/meshchat/command/ping.rb +0 -65
  91. data/lib/meshchat/command/ping_all.rb +0 -15
  92. data/lib/meshchat/command/send_disconnect.rb +0 -15
  93. data/lib/meshchat/command/server.rb +0 -20
  94. data/lib/meshchat/command/share.rb +0 -13
  95. data/lib/meshchat/command/stop_listening.rb +0 -13
  96. data/lib/meshchat/command/whisper.rb +0 -38
  97. data/lib/meshchat/database.rb +0 -30
  98. data/lib/meshchat/display.rb +0 -33
  99. data/lib/meshchat/display/base.rb +0 -60
  100. data/lib/meshchat/display/manager.rb +0 -55
  101. data/lib/meshchat/instance.rb +0 -40
  102. data/lib/meshchat/message.rb +0 -41
  103. data/lib/meshchat/message/base.rb +0 -97
  104. data/lib/meshchat/message/chat.rb +0 -19
  105. data/lib/meshchat/message/disconnect.rb +0 -13
  106. data/lib/meshchat/message/emote.rb +0 -9
  107. data/lib/meshchat/message/node_list.rb +0 -63
  108. data/lib/meshchat/message/node_list_diff.rb +0 -15
  109. data/lib/meshchat/message/node_list_hash.rb +0 -33
  110. data/lib/meshchat/message/ping.rb +0 -32
  111. data/lib/meshchat/message/ping_reply.rb +0 -9
  112. data/lib/meshchat/message/relay.rb +0 -43
  113. data/lib/meshchat/message/whisper.rb +0 -36
  114. data/lib/meshchat/models/entry.rb +0 -104
  115. data/lib/meshchat/net/client.rb +0 -83
  116. data/lib/meshchat/net/listener/errors.rb +0 -11
  117. data/lib/meshchat/net/listener/request.rb +0 -48
  118. data/lib/meshchat/net/listener/request_processor.rb +0 -50
  119. data/lib/meshchat/net/listener/server.rb +0 -114
  120. data/lib/meshchat/net/request.rb +0 -29
  121. data/lib/meshchat/notifier/base.rb +0 -31
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Emote < Command::Chat
6
+ def self.description
7
+ 'send an emote to the current chat'
8
+ end
9
+
10
+ def initialize(input, message_dispatcher, message_factory, input_factory)
11
+ super
12
+ input = input.chomp
13
+ emote_message = input.gsub(/\A\/me /, '').chomp
14
+ @_input = emote_message
15
+ end
16
+
17
+ def show_myself(message)
18
+ Display.info message.display
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Exit < Command::Base
6
+ def self.description
7
+ 'exits the program'
8
+ end
9
+
10
+ def handle
11
+ _input_factory._cli.shutdown
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Help < Command::Base
6
+ def self.description
7
+ 'displays this help message'
8
+ end
9
+
10
+ def handle
11
+ Meshchat::CLI::COMMAND_MAP.each do |key, klass|
12
+ if klass.respond_to?(:description)
13
+ Display.info "/#{key}\t\t" + klass.description
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Identity < Command::Base
6
+ def self.description
7
+ 'displays your identity'
8
+ end
9
+
10
+ def handle
11
+ Display.success APP_CONFIG.user.identity
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Import < Command::Base
6
+ def self.description
7
+ 'imports an identity file (formatted as json)'
8
+ end
9
+
10
+ def handle
11
+ if command_valid?
12
+ node = Node.import_from_file(filename)
13
+ if node.valid? && node.persisted?
14
+ Display.success "#{node.alias_name} successfully imported"
15
+
16
+ # send the server list to this new node
17
+ node_list = _message_factory.create(Network::Message::NODE_LIST)
18
+ _message_dispatcher.send_message(node: node, message: node_list)
19
+ else
20
+ Display.alert "#{node.alias_name} is invalid"
21
+ Display.alert node.errors.full_messages.join("\n")
22
+ end
23
+ else
24
+ Display.alert usage
25
+ end
26
+ end
27
+
28
+ def usage
29
+ 'Usage: /import {filename}'
30
+ end
31
+
32
+ def command_valid?
33
+ filename.present?
34
+ end
35
+
36
+ def filename
37
+ sub_command
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ # TODO: remove this command before release
6
+ class Irb < Command::Base
7
+ def self.description
8
+ 'runs ruby commands (useful for debugging)'
9
+ end
10
+
11
+ def handle
12
+ code = command_args[1..command_args.length].join(' ')
13
+ ap eval(code)
14
+ ''
15
+ rescue => e
16
+ ap e.message
17
+ ap e.backtrace
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Offline < Command::Base
6
+ def self.description
7
+ 'shows offline users'
8
+ end
9
+
10
+ def handle
11
+ list = Node.offline.map(&:as_info)
12
+ msg = if list.present?
13
+ list.join(', ')
14
+ else
15
+ 'no one is offline'
16
+ end
17
+
18
+ Display.info msg
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Online < Command::Base
6
+ def self.description
7
+ 'shows online users'
8
+ end
9
+
10
+ def handle
11
+ msg = Node.online.map(&:as_info).join(', ').presence || 'no one is online'
12
+
13
+ Display.info msg
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Ping < Command::Base
6
+ def self.description
7
+ 'pings a particular user'
8
+ end
9
+
10
+ def handle
11
+ if command_valid?
12
+ msg = _message_factory.create(Network::Message::PING)
13
+
14
+ field, value = parse_ping_command
15
+
16
+ node =
17
+ if field == 'location'
18
+ Node.find_by_location_on_network(lookup_value)
19
+ else
20
+ Node.find_by_alias_name(lookup_value)
21
+ end
22
+
23
+ location = node.try(:location)
24
+
25
+ unless location
26
+ return Display.alert "#{lookup_value} could not be found"
27
+ end
28
+
29
+ _message_dispatcher.send_message(node: node, message: msg)
30
+ else
31
+ Display.alert usage
32
+ end
33
+ end
34
+
35
+ def usage
36
+ 'Usage: /ping {field} {value} e.g.: /ping alias neurotek or /ping location 10.10.10.10:8080'
37
+ end
38
+
39
+ def lookup_field
40
+ sub_command
41
+ end
42
+
43
+ def lookup_value
44
+ value = command_args.last
45
+ value if value != sub_command
46
+ end
47
+
48
+ def command_valid?
49
+ parse_ping_command.compact.length == 2
50
+ end
51
+
52
+ def parse_ping_command
53
+ @parsed_args ||=
54
+ if lookup_field == 'location' || lookup_field == 'alias'
55
+ [lookup_field, lookup_value]
56
+ elsif lookup_field =~ /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/
57
+ ['location', lookup_field]
58
+ else
59
+ ['alias', lookup_field]
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class PingAll < Command::Base
6
+ def self.description
7
+ 'pings all known users'
8
+ end
9
+
10
+ def handle
11
+ ping = _message_factory.create(Network::Message::PING)
12
+ Node.all.each do |n|
13
+ _message_dispatcher.send_message(node: n, message: ping)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class SendDisconnect < Command::Base
6
+ def self.description
7
+ 'sends a disconnect message to all users'
8
+ end
9
+
10
+ def handle
11
+ message = _message_factory.create(Network::Message::DISCONNECT)
12
+
13
+ Node.all.each do |n|
14
+ _message_dispatcher.send_message(node: n, message: message)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Server < Command::Base
6
+ ONLINE = 'online'
7
+ def self.description
8
+ 'known server statuses'
9
+ end
10
+
11
+ def handle
12
+ case sub_command
13
+ when ONLINE
14
+ Display.info Node.online.map(&:as_info).join(', ') || 'no one is online'
15
+ else
16
+ Display.info Node.all.map(&:as_info).join(', ') || 'there are no nodes'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Share < Command::Base
6
+ def self.description
7
+ 'exports your identity to a json file to give to another user'
8
+ end
9
+
10
+ def handle
11
+ APP_CONFIG.user.share
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Command
5
+ class Whisper < Command::Base
6
+ def self.description
7
+ 'sends a private message to a spepcific person'
8
+ end
9
+
10
+ def target
11
+ # get first arg
12
+ command
13
+ end
14
+
15
+ def message
16
+ command_args[1..command_args.length].try(:join, ' ')
17
+ end
18
+
19
+ def handle
20
+ node = Node.find_by_alias_name(target)
21
+
22
+ if node
23
+ m = _message_factory.create(
24
+ Network::Message::WHISPER,
25
+ data: {
26
+ message: message,
27
+ to: target
28
+ })
29
+
30
+ Display.whisper m.display
31
+
32
+ _message_dispatcher.send_message(node: node, message: m)
33
+ else
34
+ Display.alert "node for #{target} not found or is not online"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+ module Meshchat
3
+ module Ui
4
+ module Display
5
+ extend ActiveSupport::Autoload
6
+
7
+ eager_autoload do
8
+ autoload :Base
9
+ autoload :Manager
10
+ end
11
+
12
+ module_function
13
+
14
+ # delegate :start, :add_line,
15
+ # :info, :warning, :alert, :success, :chat, :whisper,
16
+ # :present_message, to: :current
17
+
18
+ # TODO: Delegate doesn't work on modules?
19
+ def start(*args)
20
+ current.start(*args)
21
+ end
22
+
23
+ def add_line(*args)
24
+ current.add_line(*args)
25
+ end
26
+
27
+ def info(*args)
28
+ current.info(*args)
29
+ end
30
+
31
+ def emote(*args)
32
+ current.emote(*args)
33
+ end
34
+
35
+ def warning(*args)
36
+ current.warning(*args)
37
+ end
38
+
39
+ def alert(*args)
40
+ current.alert(*args)
41
+ end
42
+
43
+ def success(*args)
44
+ current.success(*args)
45
+ end
46
+
47
+ def chat(*args)
48
+ current.chat(*args)
49
+ end
50
+
51
+ def whisper(*args)
52
+ current.whisper(*args)
53
+ end
54
+
55
+ def present_message(*args)
56
+ current.present_message(*args)
57
+ end
58
+
59
+ # TODO: break these out in to their own Logger class, and not
60
+ # on the display object
61
+ def fatal(*args)
62
+ current.fatal(*args)
63
+ end
64
+
65
+ def debug(*args)
66
+ current.debug(*args)
67
+ end
68
+
69
+ def error(*args)
70
+ current.error(*args)
71
+ end
72
+
73
+ def current
74
+ Meshchat::Display
75
+ end
76
+ end
77
+ end
78
+ end