del 0.1.16 → 0.1.17

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.
data/lib/del/user.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Del
4
+ # An XMPP user.
2
5
  class User
3
6
  attr_reader :jid, :attributes
4
7
 
@@ -14,5 +17,10 @@ module Del
14
17
  def to_s
15
18
  YAML.dump(attributes)
16
19
  end
20
+
21
+ def self.map_from(attributes)
22
+ return nil if attributes.nil?
23
+ new(attributes['jid'], attributes)
24
+ end
17
25
  end
18
26
  end
data/lib/del/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Del
2
- VERSION = "0.1.16"
4
+ VERSION = '0.1.17'
3
5
  end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Del
4
+ # An XMPP Connection
5
+ class XMPPConnection
6
+ attr_reader :configuration
7
+
8
+ def initialize(configuration:)
9
+ @configuration = configuration
10
+ @rooms = {}
11
+ end
12
+
13
+ def connect(robot)
14
+ record_exceptions
15
+ connect_to_xmpp_server
16
+ roster = discover_users
17
+ listen_for_direct_messages(robot)
18
+ update_status(:chat)
19
+ discover_rooms(robot, roster)
20
+ end
21
+
22
+ def deliver(jid, message)
23
+ message = Jabber::Message.new(jid, encode_string(message))
24
+ message.type = :chat
25
+ client.send(message)
26
+ end
27
+
28
+ def deliver_to_room(jid, message)
29
+ @rooms[jid.strip.to_s]&.say(encode_string(message))
30
+ end
31
+
32
+ def disconnect
33
+ Del.logger.info('byte me!')
34
+ client.close
35
+ rescue IOError, SystemCallError => error
36
+ Del.logger.error(error)
37
+ end
38
+
39
+ # :chat, :away, :dnd, :xa
40
+ def update_status(show = :chat, message: nil)
41
+ client.send(Jabber::Presence.new(show, message))
42
+ end
43
+
44
+ private
45
+
46
+ def client
47
+ @client ||= Jabber::Client.new(jid)
48
+ end
49
+
50
+ def jid
51
+ @jid ||= jid_for(configuration.jid, 'chat.hipchat.com', 'bot')
52
+ end
53
+
54
+ def encode_string(item)
55
+ item.to_s.encode('UTF-8', invalid: :replace, undef: :replace)
56
+ end
57
+
58
+ def jid_for(jid, domain, resource)
59
+ jid = Jabber::JID.new(jid)
60
+ jid.resource = resource
61
+ unless jid.node
62
+ jid.node = jid.domain
63
+ jid.domain = domain
64
+ end
65
+ jid
66
+ end
67
+
68
+ def record_exceptions
69
+ client.on_exception do |error, _connection, _error_source|
70
+ Del.logger.error(error)
71
+ disconnect
72
+ end
73
+ end
74
+
75
+ def connect_to_xmpp_server
76
+ client.connect(configuration.host)
77
+ sleep 0.0001 until client.is_connected?
78
+ client.auth(configuration.password)
79
+ end
80
+
81
+ def discover_users
82
+ roster = Jabber::Roster::Helper.new(client, false)
83
+ roster.add_update_callback do |_old_item, item|
84
+ configuration.users.upsert(item['jid'], item.attributes) if item
85
+ end
86
+ roster.get_roster
87
+ roster.wait_for_roster
88
+ roster
89
+ end
90
+
91
+ def listen_for_direct_messages(robot)
92
+ client.add_message_callback do |message|
93
+ next if message.type == :error || message.body.nil?
94
+ user = configuration.users.find(message.from.strip)
95
+ robot.receive(message.body, source: Source.new(user: user))
96
+ end
97
+ end
98
+
99
+ def discover_rooms(robot, roster)
100
+ configuration.default_rooms.each do |room|
101
+ room_jid = jid_for(room, configuration.muc_domain.dup, robot.name)
102
+ stripped_jid = room_jid.strip.to_s
103
+ next if @rooms[stripped_jid]
104
+
105
+ muc = @rooms[stripped_jid] = Jabber::MUC::SimpleMUCClient.new(client)
106
+ listen_for_room_messages(muc, room_jid, robot, roster)
107
+ end
108
+ end
109
+
110
+ def listen_for_room_messages(multi_user_chat, room_jid, robot, roster)
111
+ multi_user_chat.on_message do |_, nickname, message|
112
+ Del.logger.debug([nickname, message].inspect)
113
+ user = find_user_with(nickname, roster)
114
+ source = Source.new(user: user, room: room_jid.strip.to_s)
115
+ robot.receive(message, source: source)
116
+ end
117
+ multi_user_chat.join(room_jid)
118
+ end
119
+
120
+ def find_user_with(nickname, roster)
121
+ other_jid = roster.items.find { |_jid, item| item.iname == nickname }
122
+ user = configuration.users.find(other_jid)
123
+ user.nil? ? User.new(other_jid[0], other_jid[1]) : user
124
+ end
125
+ end
126
+ end
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: del
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-10 00:00:00.000000000 Z
11
+ date: 2018-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: xmpp4r
14
+ name: bundler-audit
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.5'
19
+ version: '0.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.5'
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-hippie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.55'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.55'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: thor
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +66,20 @@ dependencies:
38
66
  - - "~>"
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0.20'
69
+ - !ruby/object:Gem::Dependency
70
+ name: xmpp4r
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
41
83
  - !ruby/object:Gem::Dependency
42
84
  name: bundler
43
85
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +122,20 @@ dependencies:
80
122
  - - "~>"
81
123
  - !ruby/object:Gem::Version
82
124
  version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.16'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.16'
83
139
  description: Del is a funky robosapien.
84
140
  email:
85
141
  - mo@mokhan.ca
@@ -89,30 +145,40 @@ extensions: []
89
145
  extra_rdoc_files: []
90
146
  files:
91
147
  - ".gitignore"
148
+ - ".gitlab-ci.yml"
92
149
  - ".rspec"
150
+ - ".rubocop.yml"
93
151
  - ".travis.yml"
94
152
  - Gemfile
95
153
  - LICENSE.txt
96
154
  - README.md
97
155
  - Rakefile
156
+ - bin/cibuild
98
157
  - bin/console
158
+ - bin/lint
159
+ - bin/publish
99
160
  - bin/setup
161
+ - bin/test
100
162
  - del.gemspec
101
163
  - exe/del
102
164
  - lib/del.rb
103
165
  - lib/del/cli.rb
104
166
  - lib/del/configuration.rb
105
- - lib/del/connection.rb
106
167
  - lib/del/default_router.rb
107
168
  - lib/del/examples/routes.rb
108
169
  - lib/del/message.rb
109
170
  - lib/del/repository.rb
110
171
  - lib/del/robot.rb
172
+ - lib/del/send_message.rb
173
+ - lib/del/shell_command.rb
111
174
  - lib/del/socket_connection.rb
175
+ - lib/del/socket_message.rb
112
176
  - lib/del/socket_server.rb
113
177
  - lib/del/source.rb
178
+ - lib/del/tron.rb
114
179
  - lib/del/user.rb
115
180
  - lib/del/version.rb
181
+ - lib/del/xmpp_connection.rb
116
182
  homepage: https://www.mokhan.ca
117
183
  licenses:
118
184
  - MIT
@@ -125,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
191
  requirements:
126
192
  - - ">="
127
193
  - !ruby/object:Gem::Version
128
- version: '0'
194
+ version: 2.5.0
129
195
  required_rubygems_version: !ruby/object:Gem::Requirement
130
196
  requirements:
131
197
  - - ">="
@@ -1,98 +0,0 @@
1
- module Del
2
- class Connection
3
- attr_reader :configuration
4
-
5
- def initialize(configuration:)
6
- @configuration = configuration
7
- @mucs = {}
8
- end
9
-
10
- def connect(robot)
11
- client.on_exception do |error, connection, error_source|
12
- Del.logger.error(error)
13
- disconnect
14
- end
15
- client.connect(configuration.host)
16
- sleep 0.0001 until client.is_connected?
17
- client.auth(configuration.password)
18
- roster = Jabber::Roster::Helper.new(client, false)
19
- roster.add_update_callback do |old_item, item|
20
- configuration.users.upsert(item['jid'], item.attributes) if item
21
- end
22
- roster.get_roster
23
- roster.wait_for_roster
24
- client.add_message_callback do |message|
25
- next if message.type == :error || message.body.nil?
26
- user = configuration.users.find_by(message.from.strip)
27
- robot.receive(message.body, source: Source.new(user: user))
28
- end
29
- client.send(Jabber::Presence.new(:chat))
30
- configuration.default_rooms.each do |room|
31
- Del.logger.debug("Joining room '#{room}' as '#{robot.name}'")
32
- room_jid = jid_for(room, configuration.muc_domain.dup, robot.name)
33
- stripped_jid = room_jid.strip.to_s
34
- next if @mucs[stripped_jid]
35
-
36
- muc = Jabber::MUC::SimpleMUCClient.new(client)
37
- @mucs[stripped_jid] = muc
38
- muc.on_message do |_, nickname, message|
39
- Del.logger.debug([nickname, message].inspect)
40
- other_jid = roster.items.find { |jid, item| item.iname == nickname }
41
- robot.receive(message, source: Source.new(user: User.new(other_jid[0], other_jid[1]), room: stripped_jid))
42
- end
43
- muc.join(room_jid)
44
- end
45
- #list_rooms(configuration.muc_domain).each do |room|
46
- #rooms.upsert(room)
47
- #end
48
- end
49
-
50
- def deliver(jid, message)
51
- message = Jabber::Message.new(jid, encode_string(message))
52
- message.type = :chat
53
- client.send(message)
54
- end
55
-
56
- def deliver_to_room(jid, message)
57
- muc = @mucs[jid.strip.to_s]
58
- muc.say(encode_string(message)) if muc
59
- end
60
-
61
- def disconnect
62
- Del.logger.info("byte me!")
63
- client.close
64
- rescue IOError, SystemCallError => error
65
- Del.logger.error(error)
66
- end
67
-
68
- private
69
-
70
- def client
71
- @client ||= Jabber::Client.new(jid)
72
- end
73
-
74
- def jid
75
- @jid ||= jid_for(configuration.jid, "chat.hipchat.com", "bot")
76
- end
77
-
78
- #def list_rooms(muc_domain)
79
- #Jabber::MUC::MUCBrowser.new(client).muc_rooms(muc_domain).map do |jid, name|
80
- #jid.to_s
81
- #end
82
- #end
83
-
84
- def encode_string(s)
85
- s.to_s.encode("UTF-8", invalid: :replace, undef: :replace)
86
- end
87
-
88
- def jid_for(jid, domain, resource)
89
- jid = Jabber::JID.new(jid)
90
- jid.resource = resource
91
- unless jid.node
92
- jid.node = jid.domain
93
- jid.domain = domain
94
- end
95
- jid
96
- end
97
- end
98
- end