del 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c574a58daf5543a9f5d1f49c78d7fd78f67864eb1050e7188dcb4e14b7039c24
4
- data.tar.gz: d55ed1168a7a9b1aaeb5a0acc6fa250660e91d0911620a6a9f58ac1a0f0dada4
3
+ metadata.gz: 7f262a1fbdb6bb5a8928920aaa055ac37d1fde2cd789b17bbd66bb9ed0216d6b
4
+ data.tar.gz: 525e7f6afa5e20b3f59bc6df19b013b0eea9b8e33208dbb100ddd819f2df2e6f
5
5
  SHA512:
6
- metadata.gz: a7a6b876e15aa871a6269535dad1a5c2fbdd8271aa65bef32ae6b24f57bc300c482619351e03484aaff40cadbaa89a743242e6f653481105469a707c08ed9421
7
- data.tar.gz: c9033ec43b82049cc6c5adbb1a77236f7c615929139a9b4493221840860aeb85000cdf13cb795d9e71f14c69e27ab65c10cc341af7b8a29cd456a2642afed6d0
6
+ metadata.gz: 4916e972d8a2f76b43b74c26fecc74491defdde8664039a7278ea17962e42a823430e106c9c0dd004ad744ea11de7a88ef3a90e06fc4b3fd8a60e077569e6351
7
+ data.tar.gz: bfd3f1bda27c2b65a6222aa1cedc79f004e592a91880fddac74ffca32f2f59154bb32c3c14fc787cdf6f61fa7efd0b565eb5e160a25b866928357b92032e7e94
data/lib/del.rb CHANGED
@@ -7,8 +7,11 @@ require "xmpp4r/roster/helper/roster"
7
7
 
8
8
  require "del/connection"
9
9
  require "del/default_router"
10
- require "del/robot"
10
+ require "del/message"
11
11
  require "del/repository"
12
+ require "del/robot"
13
+ require "del/source"
14
+ require "del/user"
12
15
  require "del/version"
13
16
 
14
17
  module Del
@@ -16,6 +19,7 @@ module Del
16
19
  puts "Loading... #{dotenv_file}"
17
20
  Dotenv.load(dotenv_file.to_s)
18
21
  puts "It's fire! 🔥"
22
+ Del.logger.level = Logger::INFO
19
23
  del = Robot.new(configuration: configuration)
20
24
  del.get_funky!
21
25
  end
@@ -26,10 +30,12 @@ module Del
26
30
 
27
31
  def self.configuration
28
32
  @configuration ||= {
33
+ default_rooms: ENV.fetch("DEL_ROOMS", '').split(','),
29
34
  host: ENV.fetch("DEL_HOST"),
30
35
  jid: ENV.fetch("DEL_JID"),
31
36
  logger: Logger.new(STDOUT),
32
37
  muc_domain: ENV.fetch("DEL_MUC_DOMAIN"),
38
+ name: ENV.fetch("DEL_FULL_NAME"),
33
39
  password: ENV.fetch("DEL_PASSWORD"),
34
40
  rooms: Repository.new,
35
41
  router: DefaultRouter.new,
@@ -6,6 +6,7 @@ module Del
6
6
  @configuration = configuration
7
7
  @rooms = configuration[:rooms]
8
8
  @users = configuration[:users]
9
+ @mucs = {}
9
10
  end
10
11
 
11
12
  def connect(robot)
@@ -18,15 +19,31 @@ module Del
18
19
  client.auth(configuration[:password])
19
20
  roster = Jabber::Roster::Helper.new(client, false)
20
21
  roster.add_update_callback do |old_item, item|
21
- users.upsert(item['jid'], item.attributes.to_h) if item
22
+ users.upsert(item['jid'], User.new(item['jid'], item)) if item
22
23
  end
23
24
  roster.get_roster
24
25
  roster.wait_for_roster
25
26
  client.add_message_callback do |message|
26
27
  next if message.type == :error || message.body.nil?
27
- robot.receive(message)
28
+ user = configuration[:users].find_by(message.from.strip)
29
+ robot.receive(message.body, source: Source.new(user: user))
28
30
  end
29
31
  client.send(Jabber::Presence.new(:chat))
32
+ configuration[:default_rooms].each do |room|
33
+ Del.logger.debug("Joining #{room} as #{robot.name}")
34
+ room_jid = jid_for(room, configuration[:muc_domain].dup, robot.name)
35
+ stripped_jid = room_jid.strip.to_s
36
+ next if @mucs[stripped_jid]
37
+
38
+ muc = Jabber::MUC::SimpleMUCClient.new(client)
39
+ @mucs[stripped_jid] = muc
40
+ muc.on_message do |_, nickname, message|
41
+ Del.logger.debug([nickname, message].inspect)
42
+ other_jid = roster.items.find { |jid, item| item.iname == nickname }
43
+ robot.receive(message, source: Source.new(user: User.new(other_jid[0], other_jid[1]), room: stripped_jid))
44
+ end
45
+ muc.join(room_jid)
46
+ end
30
47
  list_rooms(configuration[:muc_domain]).each do |room|
31
48
  rooms.upsert(room)
32
49
  end
@@ -38,9 +55,16 @@ module Del
38
55
  client.send(message)
39
56
  end
40
57
 
58
+ def deliver_to_room(jid, message)
59
+ muc = @mucs[jid.strip.to_s]
60
+ muc.say(encode_string(message)) if muc
61
+ end
62
+
41
63
  def disconnect
42
64
  puts "byte me!"
43
65
  client.close
66
+ rescue IOError, SystemCallError => error
67
+ Del.logger.error(error)
44
68
  end
45
69
 
46
70
  private
@@ -50,9 +74,7 @@ module Del
50
74
  end
51
75
 
52
76
  def jid
53
- jid = Jabber::JID.new(configuration[:jid])
54
- jid.resource = "bot"
55
- jid
77
+ @jid ||= jid_for(configuration[:jid], "chat.hipchat.com", "bot")
56
78
  end
57
79
 
58
80
  def list_rooms(muc_domain)
@@ -64,5 +86,15 @@ module Del
64
86
  def encode_string(s)
65
87
  s.encode("UTF-8", invalid: :replace, undef: :replace)
66
88
  end
89
+
90
+ def jid_for(jid, domain, resource)
91
+ jid = Jabber::JID.new(jid)
92
+ jid.resource = resource
93
+ unless jid.node
94
+ jid.node = jid.domain
95
+ jid.domain = domain
96
+ end
97
+ jid
98
+ end
67
99
  end
68
100
  end
@@ -1,7 +1,9 @@
1
1
  module Del
2
2
  class DefaultRouter
3
3
  def route(message)
4
- Del.logger.debug(message)
4
+ Del.logger.info(message.to_s)
5
+
6
+ message.reply(message.text.reverse)
5
7
  end
6
8
  end
7
9
  end
@@ -0,0 +1,19 @@
1
+ module Del
2
+ class Message
3
+ attr_reader :text, :robot, :source
4
+
5
+ def initialize(text, robot:, source:)
6
+ @text = text
7
+ @robot = robot
8
+ @source = source
9
+ end
10
+
11
+ def reply(message)
12
+ source.reply(robot, message)
13
+ end
14
+
15
+ def to_s
16
+ "#{source}: #{text}"
17
+ end
18
+ end
19
+ end
@@ -2,15 +2,26 @@ module Del
2
2
  class Repository
3
3
  def initialize(storage = {})
4
4
  @storage = storage
5
+ @lock = Mutex.new
6
+ end
7
+
8
+ def [](id)
9
+ find_by(id)
5
10
  end
6
11
 
7
12
  def find_by(id)
8
- @storage[id]
13
+ @lock.synchronize { @storage[id.to_s] }
14
+ end
15
+
16
+ def find_all
17
+ @lock.synchronize { @storage.keys }
9
18
  end
10
19
 
11
20
  def upsert(id, attributes = {})
12
21
  Del.logger.debug([id, attributes].inspect)
13
- @storage[id] = attributes
22
+ @lock.synchronize do
23
+ @storage[id.to_s] = attributes
24
+ end
14
25
  end
15
26
  end
16
27
  end
@@ -2,9 +2,12 @@ module Del
2
2
  class Robot
3
3
  attr_reader :connection, :router
4
4
  attr_reader :users, :rooms
5
+ attr_reader :jid, :name
5
6
 
6
7
  def initialize(configuration:)
7
8
  @connection = Connection.new(configuration: configuration)
9
+ @jid = configuration[:jid]
10
+ @name = configuration[:name]
8
11
  @router = configuration[:router]
9
12
  @users = configuration[:users]
10
13
  @rooms = configuration[:rooms]
@@ -17,12 +20,17 @@ module Del
17
20
  connection.disconnect
18
21
  end
19
22
 
20
- def receive(message)
21
- router.route(message)
23
+ def receive(message, source:)
24
+ return if source.from?(self)
25
+ router.route(Message.new(message, robot: self, source: source))
22
26
  end
23
27
 
24
- def send_message(jid, message)
25
- connection.deliver(jid, message)
28
+ def send_message(jid, message, room: nil)
29
+ if room.nil?
30
+ connection.deliver(jid, message)
31
+ else
32
+ connection.deliver_to_room(room, message)
33
+ end
26
34
  end
27
35
  end
28
36
  end
@@ -0,0 +1,22 @@
1
+ module Del
2
+ class Source
3
+ attr_reader :user, :room
4
+
5
+ def initialize(user:, room: nil)
6
+ @user = user
7
+ @room = room
8
+ end
9
+
10
+ def from?(robot)
11
+ user.attributes.jid == robot.jid.to_s
12
+ end
13
+
14
+ def reply(robot, message)
15
+ robot.send_message(user.jid, message, room: room)
16
+ end
17
+
18
+ def to_s
19
+ "#{user.mention_name}:#{room}:"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Del
2
+ class User
3
+ attr_reader :jid, :attributes
4
+
5
+ def initialize(jid, attributes)
6
+ @jid = jid
7
+ @attributes = attributes || {}
8
+ end
9
+
10
+ def mention_name
11
+ attributes[:mention_name]
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Del
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: del
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-27 00:00:00.000000000 Z
11
+ date: 2018-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -102,8 +102,11 @@ files:
102
102
  - lib/del.rb
103
103
  - lib/del/connection.rb
104
104
  - lib/del/default_router.rb
105
+ - lib/del/message.rb
105
106
  - lib/del/repository.rb
106
107
  - lib/del/robot.rb
108
+ - lib/del/source.rb
109
+ - lib/del/user.rb
107
110
  - lib/del/version.rb
108
111
  homepage: https://www.mokhan.ca
109
112
  licenses: